1
2
3
4
5
6
7
8
9
10
11
12
13
14 package com.cosylab.vdct;
15
16 import java.awt.Component;
17 import java.awt.GridBagConstraints;
18 import java.awt.GridBagLayout;
19 import java.awt.Insets;
20 import java.awt.event.ActionEvent;
21 import java.awt.event.ActionListener;
22 import java.awt.print.PrinterJob;
23
24 import javax.print.PrintService;
25 import javax.swing.JButton;
26 import javax.swing.JComboBox;
27 import javax.swing.JDialog;
28 import javax.swing.JLabel;
29 import javax.swing.JPanel;
30
31
32
33
34
35
36
37
38
39
40 public class PrinterSelector extends JDialog implements ActionListener {
41
42 private JPanel canvas;
43 private JComboBox servicesCombo;
44 private JLabel servicesLabel;
45 private JButton okButton;
46 private JButton cancelButton;
47 private static final String OK_COMMAND = "Ok";
48 private static final String CANCEL_COMMAND = "Cancel";
49
50 private PrintService service = null;
51
52 private static PrinterSelector selector = new PrinterSelector();
53
54
55
56
57
58
59
60
61 public static PrinterSelector getPrinterSelector(Component parent) {
62 selector.setLocationRelativeTo(parent);
63 return selector;
64 }
65
66 private PrinterSelector() {
67 this.setTitle("Select printer");
68 this.setSize(430,120);
69 this.setModal(true);
70 this.setResizable(false);
71 this.setContentPane(getCanvas());
72 }
73
74 private JPanel getCanvas() {
75 if (canvas == null) {
76 canvas = new JPanel();
77 canvas.setLayout(new GridBagLayout());
78
79 servicesLabel = new JLabel("Available printers: ");
80 canvas.add(servicesLabel, new GridBagConstraints(0,0,1,1,1,0,GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(5,5,5,5), 1,1));
81 servicesCombo = new JComboBox();
82 canvas.add(servicesCombo, new GridBagConstraints(1,0,3,1,0.8,0,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5,5,5,5), 1,1));
83
84 okButton = new JButton("OK");
85 canvas.add(okButton, new GridBagConstraints(2,1,1,1,1,0,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5,5,5,5), 1,1));
86 okButton.addActionListener(this);
87 okButton.setActionCommand(OK_COMMAND);
88 cancelButton = new JButton("Cancel");
89 canvas.add(cancelButton, new GridBagConstraints(3,1,1,1,1,0,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5,5,5,5), 1,1));
90 cancelButton.addActionListener(this);
91 cancelButton.setActionCommand(CANCEL_COMMAND);
92
93 }
94 return canvas;
95 }
96
97
98
99
100
101
102
103
104 public PrintService getPrintService(PrintService previousService) {
105 refresh(previousService);
106 this.setVisible(true);
107 return service;
108 }
109
110
111
112
113
114 public void actionPerformed(ActionEvent e) {
115 String command = e.getActionCommand();
116 if (command.equals(CANCEL_COMMAND)) {
117 service = null;
118 this.dispose();
119 } else if (command.equals(OK_COMMAND)) {
120 service = (PrintService) servicesCombo.getSelectedItem();
121 this.dispose();
122 }
123 }
124
125 private void refresh(PrintService previousService) {
126 service = null;
127 servicesCombo.removeAllItems();
128 PrintService[] ps = PrinterJob.lookupPrintServices();
129 for (int i = 0; i < ps.length; i++) {
130 servicesCombo.addItem(ps[i]);
131 }
132 if (previousService != null) {
133 servicesCombo.setSelectedItem(previousService);
134 }
135
136 }
137 }