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
34
35
36
37
38
39 public class TextAreaDialog extends JDialog {
40
41 private JTextArea textArea;
42
43
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
63
64
65
66 public TextAreaDialog() throws HeadlessException {
67 super();
68 }
69
70
71
72
73
74
75
76 public TextAreaDialog(Frame owner) throws HeadlessException {
77 super(owner);
78 this.initLog();
79 }
80
81
82
83
84
85
86
87 public TextAreaDialog(Dialog owner) throws HeadlessException {
88 super(owner);
89 this.initLog();
90 }
91
92
93
94
95
96
97
98
99 public TextAreaDialog(Frame owner, boolean modal) throws HeadlessException {
100 super(owner, modal);
101 this.initLog();
102 }
103
104
105
106
107
108
109
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
119
120
121
122
123
124 public TextAreaDialog(Dialog owner, boolean modal) throws HeadlessException {
125 super(owner, modal);
126 this.initLog();
127 }
128
129
130
131
132
133
134
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
144
145
146
147
148
149
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
160
161
162
163
164
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
175
176
177
178
179
180
181
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
192
193
194
195
196
197
198
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
209
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
274
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
299
300
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
323
324 public ResourceBundle getResource() {
325 return resource;
326 }
327
328
329
330
331 public void setResource(ResourceBundle resource) {
332 this.resource = resource;
333 }
334
335
336
337
338
339 public JTextArea getTextArea() {
340 if (this.textArea == null) {
341 this.textArea = new JTextArea();
342 }
343 return this.textArea;
344 }
345
346
347
348
349
350 public void setTextArea(JTextArea textArea) {
351 this.textArea = textArea;
352 }
353
354
355
356
357
358
359 public String getTextContent() {
360 return this.getTextArea().getText();
361 }
362
363
364
365
366
367
368 public void setTextContent(String textContent) {
369 this.getTextArea().setText(textContent);
370 }
371
372
373
374
375 public JScrollPane getJspText() {
376 return jspText;
377 }
378
379
380
381
382 public void setJspText(JScrollPane jspText) {
383 this.jspText = jspText;
384 }
385
386
387
388
389 public IconController getIconController() {
390 return iconController;
391 }
392
393
394
395
396 public void setIconController(IconController iconController) {
397 this.iconController = iconController;
398 }
399
400
401
402
403 public JPanel getTopPanel() {
404 return topPanel;
405 }
406
407
408
409
410 public void setTopPanel(JPanel topPanel) {
411 this.topPanel = topPanel;
412 }
413
414
415
416
417 public JButton getPrintButton() {
418 return printButton;
419 }
420
421
422
423
424 public void setPrintButton(JButton printButton) {
425 this.printButton = printButton;
426 }
427
428
429
430
431 public JButton getPreviewButton() {
432 return previewButton;
433 }
434
435
436
437
438 public void setPreviewButton(JButton previewButton) {
439 this.previewButton = previewButton;
440 }
441
442
443
444
445 public JPanel getButtonPanel() {
446 return buttonPanel;
447 }
448
449
450
451
452 public void setButtonPanel(JPanel buttonPanel) {
453 this.buttonPanel = buttonPanel;
454 }
455
456
457
458
459 public JPanel getBottomPanel() {
460 return bottomPanel;
461 }
462
463
464
465
466 public void setBottomPanel(JPanel bottomPanel) {
467 this.bottomPanel = bottomPanel;
468 }
469
470 }