3 import java
.awt
.event
.*;
8 import com
.easysw
.cups
.*;
10 public class GLPprinters
implements ActionListener
13 private Cups cups
= null;
14 public String cupsServerName
= "";
16 private JScrollPane scrollPane
= null;
17 private JPanel mainPanel
= null;
18 private JPanel serverPanel
= null;
19 private JPanel maskPanel
= null;
21 private GridBagLayout mainLayout
= null;
22 private GridBagConstraints mainConst
= null;
23 private GridBagLayout maskLayout
= null;
24 private GridBagConstraints maskConst
= null;
26 private JLabel serverLabel
= null;
28 JTextField nameTextField
= null;
29 protected static final String maskFieldString
= "Printer Name:";
30 protected static final String maskButtonString
= "Apply";
32 private String currentMask
= "";
38 cupsServerName
= GLPvars
.getServerName();
44 String
[] printer_names
;
45 String default_printer
;
52 // -----------------------------------------------------------
54 // First get a list of printer names.
58 u
= new URL("http://" + GLPvars
.getServerName() + ":631/");
61 // If authorization is required ....
62 cups
.setUser(GLPvars
.cupsUser
);
63 cups
.setPasswd(GLPvars
.cupsPasswd
);
65 printer_names
= cups
.cupsGetPrinters();
66 if (printer_names
!= null)
67 num_printers
= printer_names
.length
;
73 mainPanel
= new JPanel();
74 mainPanel
.setLayout(new BorderLayout());
75 mainPanel
.setBorder(BorderFactory
.createEmptyBorder(5,5,5,5));
76 mainPanel
.setBackground(GLPcolors
.backgroundColor
);
77 JLabel errorLabel
= new JLabel("Error loading printers from " +
78 GLPvars
.getServerName());
79 errorLabel
.setForeground(Color
.red
);
80 mainPanel
.add( errorLabel
, BorderLayout
.CENTER
);
81 scrollPane
= new JScrollPane(mainPanel
);
85 // -----------------------------------------------------------
87 // Now get the printer objects
89 CupsPrinter
[] printers
= new CupsPrinter
[num_printers
];
90 for (i
=0; i
< num_printers
; i
++)
94 u
= new URL("http://" + GLPvars
.getServerName() +
95 ":631/printers/" + printer_names
[i
] );
98 // If authorization is required ....
99 cups
.setUser(GLPvars
.cupsUser
);
100 cups
.setPasswd(GLPvars
.cupsPasswd
);
102 printers
[i
] = new CupsPrinter( cups
, printer_names
[i
] );
104 catch (IOException e
)
106 // System.out.println("GLPprinters: IOException");
113 // Keep track in case it changes.
115 cupsServerName
= GLPvars
.getServerName();
117 if (printer_names
!= null)
118 num_printers
= printer_names
.length
;
122 // default_printer = c.cupsGetDefault();
124 // Create the main panel to contain the two sub panels.
125 mainPanel
= new JPanel();
126 mainLayout
= new GridBagLayout();
127 mainConst
= new GridBagConstraints();
129 mainPanel
.setLayout(mainLayout
);
130 mainPanel
.setBorder(BorderFactory
.createEmptyBorder(5,5,5,5));
131 mainPanel
.setBackground(GLPcolors
.backgroundColor
);
133 // --------------------------------------------------------------
135 // Add the server name label
137 serverPanel
= new JPanel();
138 serverPanel
.setLayout( new BorderLayout());
139 serverPanel
.setBackground(GLPcolors
.backgroundColor
);
140 serverLabel
= new JLabel("Printers on " + GLPvars
.getServerName());
141 serverLabel
.setForeground(GLPcolors
.foregroundColor
);
142 serverPanel
.add(serverLabel
, BorderLayout
.NORTH
);
144 mainConst
.gridwidth
= GridBagConstraints
.RELATIVE
;
146 mainConst
.gridy
= y
++;
147 mainConst
.fill
= GridBagConstraints
.BOTH
;
148 mainConst
.weightx
= 0.0;
149 mainConst
.weighty
= 0.0;
152 mainLayout
.setConstraints( serverPanel
, mainConst
);
153 mainPanel
.add(serverPanel
);
155 // --------------------------------------------------------------
157 // Add the printer masking panel
159 maskPanel
= new JPanel();
160 maskLayout
= new GridBagLayout();
161 maskConst
= new GridBagConstraints();
163 maskPanel
.setLayout(maskLayout
);
164 maskPanel
.setBorder(BorderFactory
.createEmptyBorder(5,5,5,5));
165 maskPanel
.setBackground(GLPcolors
.backgroundColor
);
167 JPanel localMaskPanel
= buildMaskPanel();
168 maskConst
.gridwidth
= GridBagConstraints
.RELATIVE
;
171 maskConst
.fill
= GridBagConstraints
.NONE
;
172 maskConst
.weightx
= 0.0;
173 maskConst
.weighty
= 0.0;
176 maskLayout
.setConstraints( localMaskPanel
, maskConst
);
177 maskPanel
.add(localMaskPanel
);
180 // Add the masking panel to the main panel.
182 mainConst
.gridwidth
= GridBagConstraints
.RELATIVE
;
184 mainConst
.gridy
= y
++;
185 mainConst
.fill
= GridBagConstraints
.BOTH
;
186 mainConst
.weightx
= 0.0;
187 mainConst
.weighty
= 0.0;
190 mainLayout
.setConstraints( maskPanel
, mainConst
);
191 mainPanel
.add(maskPanel
);
195 // --------------------------------------------------------------
199 double weight
= 1.0 / (double)printers
.length
;
200 for (i
=0; i
< printers
.length
; i
++)
202 JPanel subPanel
= printerInfoPanel( printers
[i
] );
203 mainConst
.gridwidth
= GridBagConstraints
.RELATIVE
;
205 mainConst
.gridy
= y
++;
206 mainConst
.fill
= GridBagConstraints
.BOTH
;
207 mainConst
.weightx
= 1.0;
208 mainConst
.weighty
= weight
;
210 mainLayout
.setConstraints( subPanel
, mainConst
);
211 mainPanel
.add(subPanel
);
214 // ------------------------------------------------
216 // Put the whole thing into a scroll pane.
218 scrollPane
= new JScrollPane(mainPanel
);
223 // -----------------------------------------------------------
225 // Build an info panel for an individual printer.
227 private JPanel
printerInfoPanel( CupsPrinter cp
)
229 JPanel printerPanel
= new JPanel();
230 BoxLayout printerBox
;
232 JPanel leftHeader
= new JPanel();
233 JPanel rightHeader
= new JPanel();
235 JPanel leftPane
= new JPanel();
236 JPanel rightPane
= new JPanel();
238 GridBagLayout leftLayout
= new GridBagLayout();
239 GridBagLayout rightLayout
= new GridBagLayout();
241 GridBagConstraints leftConst
= new GridBagConstraints();
242 GridBagConstraints rightConst
= new GridBagConstraints();
245 JLabel printerIconLabel
= null;
246 JLabel printerInfoLabel
= null;
247 JLabel printerNameLabel
= null;
248 JLabel printerMakeLabel
= null;
250 JTable printerStatusTable
= null;
252 printerBox
= new BoxLayout(printerPanel
, BoxLayout
.X_AXIS
);
253 printerPanel
.setLayout(printerBox
);
254 printerPanel
.setBorder(BorderFactory
.createEmptyBorder(5,5,5,5));
255 printerPanel
.setBackground(GLPcolors
.backgroundColor
);
257 // Add border around the panel.
260 // ------------------------------------------------------------
262 // ------------------------------------------------------------
263 leftPane
.setLayout(leftLayout
);
264 leftPane
.setBackground(GLPcolors
.backgroundColor
);
266 leftHeader
.setLayout(new BorderLayout());
267 leftHeader
.setBackground(GLPcolors
.highlightColor
);
268 leftHeader
.setBorder(BorderFactory
.createEmptyBorder(3,3,3,3));
270 printerNameLabel
= new JLabel(cp
.getPrinterName());
271 printerNameLabel
.setForeground(Color
.black
);
272 leftHeader
.add( printerNameLabel
, BorderLayout
.WEST
);
273 leftConst
.gridwidth
= GridBagConstraints
.RELATIVE
;
276 leftConst
.fill
= GridBagConstraints
.HORIZONTAL
;
277 leftConst
.weightx
= 0.0;
278 leftConst
.weighty
= 0.0;
280 leftLayout
.setConstraints( leftHeader
, leftConst
);
281 leftPane
.add(leftHeader
);
283 String imageName
= "./images/printer-" + cp
.getStateText() + ".gif";
284 URL iconURL
= ClassLoader
.getSystemResource(imageName
);
285 ImageIcon icon
= new ImageIcon(iconURL
);
286 JButton printerButton
= new JButton( "<html><center><b>" +
287 cp
.getPrinterName() +
288 "</b></center></html>",
290 printerButton
.setBorder(BorderFactory
.createEmptyBorder(3,3,3,3));
291 printerButton
.setBackground(GLPcolors
.backgroundColor
);
292 printerButton
.setActionCommand( cp
.getPrinterName() );
293 printerButton
.addActionListener(this);
294 printerButton
.setToolTipText("Click to go to " + cp
.getPrinterName() +
295 "'s extended informtion page.");
297 leftConst
.gridwidth
= GridBagConstraints
.REMAINDER
;
300 leftConst
.fill
= GridBagConstraints
.BOTH
;
301 leftConst
.weightx
= 1.0;
302 leftConst
.weighty
= 1.0;
304 leftLayout
.setConstraints( printerButton
, leftConst
);
305 leftPane
.add(printerButton
);
308 // ------------------------------------------------------------
310 // ------------------------------------------------------------
311 rightPane
.setLayout(rightLayout
);
312 rightPane
.setBackground(GLPcolors
.backgroundColor
);
314 rightHeader
.setLayout(new BorderLayout());
315 rightHeader
.setBackground(GLPcolors
.highlightColor
);
316 rightHeader
.setBorder(BorderFactory
.createEmptyBorder(3,3,3,3));
318 printerMakeLabel
= new JLabel(cp
.getMakeAndModel());
319 printerMakeLabel
.setForeground(Color
.black
);
320 rightHeader
.add( printerMakeLabel
, BorderLayout
.WEST
);
322 rightConst
.gridwidth
= GridBagConstraints
.RELATIVE
;
323 rightConst
.gridx
= 0;
324 rightConst
.gridy
= 0;
325 rightConst
.fill
= GridBagConstraints
.HORIZONTAL
;
326 rightConst
.weightx
= 0.0;
327 rightConst
.weighty
= 0.0;
328 rightConst
.ipady
= 4;
329 rightLayout
.setConstraints( rightHeader
, rightConst
);
330 rightPane
.add(rightHeader
);
333 Font labelFont
= new Font("Serif",Font
.BOLD
, 12 );
334 // Font textFont = new Font("Serif", Font.NORMAL, 12 );
335 Font messageFont
= new Font("Serif", Font
.ITALIC
, 12 );
337 JLabel pdNameLabel
= new JLabel("Name");
338 JLabel pdLocationLabel
= new JLabel("Location");
339 JLabel pdStatusLabel
= new JLabel("Status");
340 JLabel pdMessageLabel
= new JLabel("Message");
342 JLabel pdNameText
= new JLabel(cp
.getPrinterName());
343 JLabel pdLocationText
= new JLabel(cp
.getLocation());
344 JLabel pdStatusText
= new JLabel(cp
.getStateText());
345 JLabel pdMessageText
= new JLabel(cp
.getStateReasons());
347 pdNameLabel
.setFont(labelFont
);
348 pdLocationLabel
.setFont(labelFont
);
349 pdStatusLabel
.setFont(labelFont
);
350 pdMessageLabel
.setFont(labelFont
);
351 pdMessageText
.setFont(messageFont
);
353 pdNameLabel
.setForeground(Color
.black
);
354 pdLocationLabel
.setForeground(Color
.black
);
355 pdStatusLabel
.setForeground(Color
.black
);
356 pdMessageLabel
.setForeground(Color
.black
);
359 if ((cp
.getStateReasons().length() > 0) &&
360 (!cp
.getStateReasons().equals("none")))
362 tablePane
= new JPanel(new GridLayout(4,2,2,2));
363 tablePane
.add(pdNameLabel
);
364 tablePane
.add(pdNameText
);
366 tablePane
.add(pdLocationLabel
);
367 tablePane
.add(pdLocationText
);
369 tablePane
.add(pdStatusLabel
);
370 tablePane
.add(pdStatusText
);
372 tablePane
.add(pdMessageLabel
);
373 tablePane
.add(pdMessageText
);
377 tablePane
= new JPanel(new GridLayout(3,2,2,2));
378 tablePane
.add(pdNameLabel
);
379 tablePane
.add(pdNameText
);
381 tablePane
.add(pdLocationLabel
);
382 tablePane
.add(pdLocationText
);
384 tablePane
.add(pdStatusLabel
);
385 tablePane
.add(pdStatusText
);
388 tablePane
.setBackground(GLPcolors
.backgroundColor
);
390 // printerStatusTable.setShowGrid(false);
391 rightConst
.gridwidth
= GridBagConstraints
.REMAINDER
;
392 rightConst
.gridx
= 0;
393 rightConst
.gridy
= 1;
394 rightConst
.fill
= GridBagConstraints
.BOTH
;
395 rightConst
.weightx
= 1.0;
396 rightConst
.weighty
= 1.0;
397 rightConst
.ipady
= 4;
398 rightLayout
.setConstraints( tablePane
, rightConst
);
399 rightPane
.add(tablePane
);
401 printerPanel
.add(leftPane
);
402 printerPanel
.add(rightPane
);
404 return(printerPanel
);
411 public JPanel
buildMaskPanel()
414 // Create the main panel to contain the two sub panels.
415 JPanel namePanel
= new JPanel();
416 GridBagLayout nameLayout
= new GridBagLayout();
417 GridBagConstraints nameConst
= new GridBagConstraints();
419 namePanel
.setLayout(nameLayout
);
420 namePanel
.setBorder(BorderFactory
.createEmptyBorder(5,5,5,5));
421 namePanel
.setBackground(GLPcolors
.backgroundColor
);
424 //Create a regular text field.
425 nameTextField
= new JTextField(16);
426 nameTextField
.setActionCommand(maskFieldString
);
427 nameTextField
.addActionListener(this);
428 nameTextField
.setText("");
430 //Create some labels for the fields.
431 JLabel nameFieldLabel
= new JLabel(maskFieldString
);
432 nameFieldLabel
.setForeground(GLPcolors
.foregroundColor
);
433 nameFieldLabel
.setLabelFor(nameTextField
);
436 nameConst
.gridwidth
= GridBagConstraints
.RELATIVE
;
439 nameConst
.fill
= GridBagConstraints
.HORIZONTAL
;
440 nameConst
.weightx
= 0.0;
441 nameConst
.weighty
= 0.0;
444 nameLayout
.setConstraints( nameFieldLabel
, nameConst
);
445 namePanel
.add(nameFieldLabel
);
447 nameConst
.gridwidth
= GridBagConstraints
.RELATIVE
;
450 nameConst
.fill
= GridBagConstraints
.HORIZONTAL
;
451 nameConst
.weightx
= 0.0;
452 nameConst
.weighty
= 0.0;
455 nameLayout
.setConstraints( nameTextField
, nameConst
);
456 namePanel
.add(nameTextField
);
458 JButton applyButton
= new JButton(maskButtonString
);
459 applyButton
.setBorder(BorderFactory
.createCompoundBorder(
460 BorderFactory
.createRaisedBevelBorder(),
461 BorderFactory
.createEmptyBorder(2,2,2,2)));
462 applyButton
.setActionCommand(maskButtonString
);
463 applyButton
.addActionListener(this);
466 nameConst
.fill
= GridBagConstraints
.NONE
;
467 nameLayout
.setConstraints( applyButton
, nameConst
);
468 nameConst
.weightx
= 0.0;
469 nameConst
.weighty
= 0.0;
472 namePanel
.add(applyButton
);
478 public void actionPerformed(ActionEvent e
)
480 if (e
.getActionCommand().equals(maskFieldString
))
482 String s
= nameTextField
.getText();
488 else if (e
.getActionCommand().equals(maskButtonString
))
490 String s
= nameTextField
.getText();
498 GLPvars
.selectedPrinterName
= e
.getActionCommand();
499 GLPvars
.tabs
.updateDetailPanel();
500 GLPvars
.tabs
.tabPanel
.setSelectedIndex(2);
505 public JScrollPane
getPanel()