View Javadoc

1   /**
2    * 
3    */
4   package unbbayes.gui.util;
5   
6   import java.awt.BorderLayout;
7   import java.awt.Cursor;
8   import java.awt.Dialog;
9   import java.awt.Dimension;
10  import java.awt.FlowLayout;
11  import java.awt.Frame;
12  import java.awt.GraphicsConfiguration;
13  import java.awt.HeadlessException;
14  import java.awt.event.ActionEvent;
15  import java.awt.event.ActionListener;
16  import java.awt.print.PageFormat;
17  import java.awt.print.PrinterException;
18  import java.util.ResourceBundle;
19  
20  import javax.swing.JButton;
21  import javax.swing.JDialog;
22  import javax.swing.JOptionPane;
23  import javax.swing.JPanel;
24  import javax.swing.JScrollPane;
25  import javax.swing.JTextArea;
26  
27  import unbbayes.controller.IconController;
28  import unbbayes.controller.PrintMonitor;
29  import unbbayes.controller.PrintPreviewer;
30  import unbbayes.controller.PrintText;
31  
32  /**
33   * This is a default dialog containing a text field
34   * to show string messages.
35   * This can be useful to present a simple text message to a user.
36   * @author Shou Matsumoto
37   *
38   */
39  public class TextAreaDialog extends JDialog {
40  
41  	private JTextArea textArea;
42  	
43  	/** Load resource file from this package */
44  	private ResourceBundle resource = unbbayes.util.ResourceController.newInstance().getBundle(
45  			unbbayes.gui.resources.GuiResources.class.getName());
46  
47  	private JScrollPane jspText;
48  
49  	private IconController iconController;
50  
51  	private JPanel topPanel;
52  
53  	private JButton printButton;
54  
55  	private JButton previewButton;
56  
57  	private JPanel buttonPanel;
58  
59  	private JPanel bottomPanel;
60  
61  	/**
62  	 * Creates a non-modal dialog without a title and without a specified Frame owner. A shared, hidden frame will be set as the owner of the dialog. 
63  	 * This constructor sets the component's locale property to the value returned by JComponent.getDefaultLocale. 
64  	 * @throws HeadlessException :  if GraphicsEnvironment.isHeadless() returns true
65  	 */
66  	public TextAreaDialog() throws HeadlessException {
67  		super();
68  	}
69  	
70  	/**
71  	 * Creates a non-modal dialog without a title with the specified Frame as its owner. If owner is null, a shared, hidden frame will be set as the owner of the dialog. 
72  	 * This constructor sets the component's locale property to the value returned by JComponent.getDefaultLocale
73  	 * @param owner
74  	 * @throws HeadlessException : if GraphicsEnvironment.isHeadless() returns true
75  	 */
76  	public TextAreaDialog(Frame owner) throws HeadlessException {
77  		super(owner);
78  		this.initLog();
79  	}
80  
81  	/**
82  	 * Creates a non-modal dialog without a title with the specified Dialog as its owner. 
83  	 * This constructor sets the component's locale property to the value returned by JComponent.getDefaultLocale. 
84  	 * @param owner
85  	 * @throws HeadlessException : if GraphicsEnvironment.isHeadless() returns true
86  	 */
87  	public TextAreaDialog(Dialog owner) throws HeadlessException {
88  		super(owner);
89  		this.initLog();
90  	}
91  
92  	/**
93  	 * Creates a modal or non-modal dialog without a title and with the specified owner Frame. If owner is null, a shared, hidden frame will be set as the owner of the dialog. 
94  	 * This constructor sets the component's locale property to the value returned by JComponent.getDefaultLocale. 
95  	 * @param owner
96  	 * @param modal : true for a modal dialog, false for one that allows others windows to be active at the same time
97  	 * @throws HeadlessException : if GraphicsEnvironment.isHeadless() returns true
98  	 */
99  	public TextAreaDialog(Frame owner, boolean modal) throws HeadlessException {
100 		super(owner, modal);
101 		this.initLog();
102 	}
103 
104 	/**
105 	 * Creates a non-modal dialog with the specified title and with the specified owner frame. If owner is null, a shared, hidden frame will be set as the owner of the dialog. 
106 	 * This constructor sets the component's locale property to the value returned by JComponent.getDefaultLocale. 
107 	 * @param owner
108 	 * @param title
109 	 * @throws HeadlessException : if GraphicsEnvironment.isHeadless() returns true
110 	 */
111 	public TextAreaDialog(Frame owner, String title) throws HeadlessException {
112 		super(owner, title);
113 		this.initLog();
114 		this.setTitle(title);
115 	}
116 
117 	/**
118 	 * Creates a modal or non-modal dialog without a title and with the specified owner dialog. 
119 	 * This constructor sets the component's locale property to the value returned by JComponent.getDefaultLocale. 
120 	 * @param owner
121 	 * @param modal : true for a modal dialog, false for one that allows others windows to be active at the same time
122 	 * @throws HeadlessException : if GraphicsEnvironment.isHeadless() returns true
123 	 */
124 	public TextAreaDialog(Dialog owner, boolean modal) throws HeadlessException {
125 		super(owner, modal);
126 		this.initLog();
127 	}
128 
129 	/**
130 	 * Creates a non-modal dialog with the specified title and with the specified owner dialog. 
131 	 * This constructor sets the component's locale property to the value returned by JComponent.getDefaultLocale. 
132 	 * @param owner
133 	 * @param title
134 	 * @throws HeadlessException : if GraphicsEnvironment.isHeadless() returns true
135 	 */
136 	public TextAreaDialog(Dialog owner, String title) throws HeadlessException {
137 		super(owner, title);
138 		this.initLog();
139 		this.setTitle(title);
140 	}
141 
142 	/**
143 	 * Creates a modal or non-modal dialog with the specified title and the specified owner Frame. If owner is null, a shared, hidden frame will be set as the owner of this dialog. All constructors defer to this one. 
144 	 * NOTE: Any popup components (JComboBox, JPopupMenu, JMenuBar) created within a modal dialog will be forced to be lightweight. 
145 	 * This constructor sets the component's locale property to the value returned by JComponent.getDefaultLocale. 
146 	 * @param owner
147 	 * @param title
148 	 * @param modal : true for a modal dialog, false for one that allows others windows to be active at the same time
149 	 * @throws HeadlessException : if GraphicsEnvironment.isHeadless() returns true
150 	 */
151 	public TextAreaDialog(Frame owner, String title, boolean modal)
152 			throws HeadlessException {
153 		super(owner, title, modal);
154 		this.initLog();
155 		this.setTitle(title);
156 	}
157 
158 	/**
159 	 * Creates a modal or non-modal dialog with the specified title and the specified owner frame. 
160 	 * This constructor sets the component's locale property to the value returned by JComponent.getDefaultLocale. 
161 	 * @param owner
162 	 * @param title
163 	 * @param modal : true for a modal dialog, false for one that allows others windows to be active at the same time
164 	 * @throws HeadlessException : if GraphicsEnvironment.isHeadless() returns true
165 	 */
166 	public TextAreaDialog(Dialog owner, String title, boolean modal)
167 			throws HeadlessException {
168 		super(owner, title, modal);
169 		this.initLog();
170 		this.setTitle(title);
171 	}
172 
173 	/**
174 	 * Creates a modal or non-modal dialog with the specified title, owner Frame, and GraphicsConfiguration. 
175 	 * NOTE: Any popup components (JComboBox, JPopupMenu, JMenuBar) created within a modal dialog will be forced to be lightweight. 
176 	 * This constructor sets the component's locale property to the value returned by JComponent.getDefaultLocale. 
177 	 * @param owner
178 	 * @param title
179 	 * @param modal : true for a modal dialog, false for one that allows others windows to be active at the same time
180 	 * @param gc
181 	 * @throws HeadlessException : if GraphicsEnvironment.isHeadless() returns true
182 	 */
183 	public TextAreaDialog(Frame owner, String title, boolean modal,
184 			GraphicsConfiguration gc) {
185 		super(owner, title, modal, gc);
186 		this.initLog();
187 		this.setTitle(title);
188 	}
189 
190 	/**
191 	 * Creates a modal or non-modal dialog with the specified title, owner Dialog, and GraphicsConfiguration. 
192 	 * NOTE: Any popup components (JComboBox, JPopupMenu, JMenuBar) created within a modal dialog will be forced to be lightweight. 
193 	 * This constructor sets the component's locale property to the value returned by JComponent.getDefaultLocale. 
194 	 * @param owner
195 	 * @param title
196 	 * @param modal : true for a modal dialog, false for one that allows others windows to be active at the same time
197 	 * @param gc
198 	 * @throws HeadlessException : if GraphicsEnvironment.isHeadless() returns true
199 	 */
200 	public TextAreaDialog(Dialog owner, String title, boolean modal,
201 			GraphicsConfiguration gc) throws HeadlessException {
202 		super(owner, title, modal, gc);
203 		this.initLog();
204 		this.setTitle(title);
205 	}
206 	
207 	/**
208      * Initializes a default log dialog setting its attributes.
209      * This is called by every constructors
210      */
211     protected void initLog() {
212     	this.getOwner().setCursor(new Cursor(Cursor.WAIT_CURSOR));
213 
214     	this.getTextArea().setEditable(false);
215         
216     	this.getTextArea().moveCaretPosition(0);
217     	this.getTextArea().setSelectionEnd(0);
218 
219     	this.getTextArea().setSize(this.getTextArea().getPreferredSize());
220     	this.getTextArea().append("\n");
221 
222         jspText = new JScrollPane(this.getTextArea());
223         jspText.setPreferredSize(new Dimension(450, 400));
224 
225         iconController = IconController.getInstance();
226         topPanel = new JPanel(new BorderLayout());
227         printButton = new JButton(iconController.getPrintIcon());
228         printButton.setToolTipText(resource.getString("printLogToolTip"));
229         previewButton = new JButton(iconController.getVisualizeIcon());
230         previewButton.setToolTipText(resource.getString("previewLogToolTip"));
231         printButton.addActionListener(
232             new ActionListener() {
233                 public void actionPerformed(ActionEvent ae) {
234                     printText(getTextArea());
235                 }
236             });
237         previewButton.addActionListener(
238             new ActionListener() {
239                 public void actionPerformed(ActionEvent ae) {
240                     previewPrintText(getTextArea(), TextAreaDialog.this);
241                 }
242             });
243 
244         topPanel.add(jspText, BorderLayout.CENTER);
245 
246         buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
247         buttonPanel.add(printButton);
248         buttonPanel.add(previewButton);
249         topPanel.add(buttonPanel, BorderLayout.NORTH);
250 
251         bottomPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
252         JButton botaoOK = new JButton(resource.getString("closeButtonLabel"));
253         botaoOK.addActionListener(
254             new ActionListener() {
255                 public void actionPerformed(ActionEvent ae) {
256                     TextAreaDialog.this.dispose();
257                 }
258             });
259 
260 
261         bottomPanel.add(botaoOK);
262         topPanel.add(bottomPanel, BorderLayout.SOUTH);
263 
264         this.getContentPane().add(topPanel);
265         this.setTitle(resource.getString("logDialogTitle")); 
266         
267         this.getOwner().setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
268         
269     }
270     
271     
272     /**
273      * Preview the log printing.
274      * This is called inside the action listener of {@link #getPreviewButton()}
275      */
276     protected void previewPrintText(final JTextArea texto, final JDialog dialog) {
277         this.setCursor(new Cursor(Cursor.WAIT_CURSOR));
278         Thread t = new Thread(new Runnable() {
279           public void run() {
280             PrintText it = new PrintText(texto,
281                 new PageFormat());
282             PrintPreviewer pp = new PrintPreviewer(
283                 it, 0);
284 
285             JDialog dlg = new JDialog(dialog,
286                 resource.getString("previewLogToolTip"));
287             dlg.getContentPane().add(pp);
288             dlg.setSize(640, 480);
289             dlg.setVisible(true);
290           }
291         });
292 
293         t.start();
294         this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
295     }
296     
297     /**
298      * Print the log contained in the given <code>JTextArea</code>.
299      * This is called inside the action listener of {@link #getPrintButton()}
300      * @param textArea The text area containing the log.
301      */
302     public void printText(final JTextArea textArea) {
303         Thread t = new Thread(new Runnable() {
304           public void run() {
305             PrintText it = new PrintText(textArea,
306                 new PageFormat());
307             PrintMonitor pm = new PrintMonitor(it);
308             try {
309               pm.performPrint(true);
310             } catch (PrinterException pe) {
311               JOptionPane.showMessageDialog(
312             	  TextAreaDialog.this.getOwner(),
313                   resource.getString("printException") +
314                   pe.getMessage());
315             }
316           }
317         });
318         t.start();
319     }
320 
321 	/**
322 	 * @return the resource
323 	 */
324 	public ResourceBundle getResource() {
325 		return resource;
326 	}
327 
328 	/**
329 	 * @param resource the resource to set
330 	 */
331 	public void setResource(ResourceBundle resource) {
332 		this.resource = resource;
333 	}
334 
335 	/**
336 	 * This is the main text area
337 	 * @return the textArea : non-null value
338 	 */
339 	public JTextArea getTextArea() {
340 		if (this.textArea == null) {
341 			this.textArea = new JTextArea();
342 		}
343 		return this.textArea;
344 	}
345 
346 	/**
347 	 * This is the main text area
348 	 * @param textArea the textArea to set
349 	 */
350 	public void setTextArea(JTextArea textArea) {
351 		this.textArea = textArea;
352 	}
353 
354 	/**
355 	 * This is equivalent to {@link JTextArea#getText()} from {@link #getTextArea()}
356 	 * @return the logContent
357 	 * @see #getTextArea()
358 	 */
359 	public String getTextContent() {
360 		return this.getTextArea().getText();
361 	}
362 
363 	/**
364 	 * This is equivalent to {@link JTextArea#setText(String)} from {@link #getTextArea()}
365 	 * @param textContent the logContent to set
366 	 * @see #getTextArea()
367 	 */
368 	public void setTextContent(String textContent) {
369 		this.getTextArea().setText(textContent);
370 	}
371 
372 	/**
373 	 * @return the jspText
374 	 */
375 	public JScrollPane getJspText() {
376 		return jspText;
377 	}
378 
379 	/**
380 	 * @param jspText the jspText to set
381 	 */
382 	public void setJspText(JScrollPane jspText) {
383 		this.jspText = jspText;
384 	}
385 
386 	/**
387 	 * @return the iconController
388 	 */
389 	public IconController getIconController() {
390 		return iconController;
391 	}
392 
393 	/**
394 	 * @param iconController the iconController to set
395 	 */
396 	public void setIconController(IconController iconController) {
397 		this.iconController = iconController;
398 	}
399 
400 	/**
401 	 * @return the topPanel
402 	 */
403 	public JPanel getTopPanel() {
404 		return topPanel;
405 	}
406 
407 	/**
408 	 * @param topPanel the topPanel to set
409 	 */
410 	public void setTopPanel(JPanel topPanel) {
411 		this.topPanel = topPanel;
412 	}
413 
414 	/**
415 	 * @return the printButton
416 	 */
417 	public JButton getPrintButton() {
418 		return printButton;
419 	}
420 
421 	/**
422 	 * @param printButton the printButton to set
423 	 */
424 	public void setPrintButton(JButton printButton) {
425 		this.printButton = printButton;
426 	}
427 
428 	/**
429 	 * @return the previewButton
430 	 */
431 	public JButton getPreviewButton() {
432 		return previewButton;
433 	}
434 
435 	/**
436 	 * @param previewButton the previewButton to set
437 	 */
438 	public void setPreviewButton(JButton previewButton) {
439 		this.previewButton = previewButton;
440 	}
441 
442 	/**
443 	 * @return the buttonPanel
444 	 */
445 	public JPanel getButtonPanel() {
446 		return buttonPanel;
447 	}
448 
449 	/**
450 	 * @param buttonPanel the buttonPanel to set
451 	 */
452 	public void setButtonPanel(JPanel buttonPanel) {
453 		this.buttonPanel = buttonPanel;
454 	}
455 
456 	/**
457 	 * @return the bottomPanel
458 	 */
459 	public JPanel getBottomPanel() {
460 		return bottomPanel;
461 	}
462 
463 	/**
464 	 * @param bottomPanel the bottomPanel to set
465 	 */
466 	public void setBottomPanel(JPanel bottomPanel) {
467 		this.bottomPanel = bottomPanel;
468 	}
469 
470 }