1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package unbbayes.controller;
22
23 import java.awt.print.PageFormat;
24 import java.awt.print.Pageable;
25 import java.awt.print.Printable;
26 import java.awt.print.PrinterException;
27 import java.awt.print.PrinterJob;
28 import java.util.ResourceBundle;
29
30 import javax.swing.JDialog;
31 import javax.swing.JOptionPane;
32
33
34
35
36
37
38
39
40
41 public class PrintMonitor implements Pageable {
42
43 protected PrinterJob printerJob;
44 protected Pageable pageable;
45 protected JOptionPane optionPane;
46 protected JDialog statusDialog;
47
48
49 private static ResourceBundle resource = unbbayes.util.ResourceController.newInstance().getBundle(
50 unbbayes.controller.resources.ControllerResources.class.getName());
51
52 public PrintMonitor(Pageable p) {
53 pageable = p;
54 printerJob = PrinterJob.getPrinterJob();
55 String[] options = {resource.getString("cancelOption")};
56 optionPane = new JOptionPane("",
57 JOptionPane.INFORMATION_MESSAGE,
58 JOptionPane.CANCEL_OPTION,
59 null, options);
60 statusDialog = optionPane.createDialog(null,
61 resource.getString("printerStatus"));
62 }
63
64
65
66
67
68
69 public void performPrint(boolean showDialog)
70 throws PrinterException {
71 printerJob.setPageable(this);
72 if (showDialog) {
73 boolean isOk = printerJob.printDialog();
74 if (!isOk) return;
75 }
76 optionPane.setMessage(resource.getString("initializingPrinter"));
77 Thread t = new Thread(new Runnable() {
78 public void run() {
79 statusDialog.setVisible(true);
80 if (optionPane.getValue() !=
81 JOptionPane.UNINITIALIZED_VALUE) {
82 printerJob.cancel();
83 }
84 }
85 });
86 t.start();
87 printerJob.print();
88 statusDialog.setVisible(false);
89 }
90
91 public int getNumberOfPages() {
92 return pageable.getNumberOfPages();
93 }
94
95
96
97
98 public Printable getPrintable(int index) {
99 optionPane.setMessage(resource.getString("printingPage") + (index + 1));
100 return pageable.getPrintable(index);
101 }
102
103 public PageFormat getPageFormat(int index) {
104 return pageable.getPageFormat(index);
105 }
106 }