Load cups into easysw/current.
[thirdparty/cups.git] / scripting / java / example / GLPprinters.java
1
2 import java.awt.*;
3 import java.awt.event.*;
4 import javax.swing.*;
5 import java.net.URL;
6 import java.net.*;
7 import java.io.*;
8 import com.easysw.cups.*;
9
10 public class GLPprinters implements ActionListener 
11 {
12
13     private Cups   cups = null;
14     public  String cupsServerName = "";
15
16     private JScrollPane        scrollPane = null;
17     private JPanel             mainPanel = null;
18     private JPanel             serverPanel = null;
19     private JPanel             maskPanel = null;
20
21     private GridBagLayout      mainLayout = null;
22     private GridBagConstraints mainConst = null;
23     private GridBagLayout      maskLayout = null;
24     private GridBagConstraints maskConst = null;
25
26     private JLabel             serverLabel = null;
27
28     JTextField nameTextField = null;
29     protected static final String maskFieldString  = "Printer Name:";
30     protected static final String maskButtonString = "Apply";
31
32     private String currentMask = "";
33
34
35     // Constructor
36     public GLPprinters() 
37     {
38       cupsServerName = GLPvars.getServerName();
39       load();
40     }
41
42     public void load() 
43     {
44         String[]      printer_names;
45         String        default_printer;
46         int           num_printers = 0;
47         int           y = 0, i = 0;
48         URL           u;
49         CupsPrinter   cp;
50
51
52         //  -----------------------------------------------------------
53         //
54         //  First get a list of printer names.
55         //
56         try
57         {
58           u = new URL("http://" + GLPvars.getServerName() + ":631/");
59           cups = new Cups(u);
60
61           // If authorization is required ....
62           cups.setUser(GLPvars.cupsUser);
63           cups.setPasswd(GLPvars.cupsPasswd);
64
65           printer_names = cups.cupsGetPrinters();
66           if (printer_names != null)
67             num_printers  = printer_names.length;
68           else
69             num_printers = 0;
70         }
71         catch (IOException e)
72         {
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);
82           return;
83         }
84
85         //  -----------------------------------------------------------
86         //
87         //  Now get the printer objects
88         //
89         CupsPrinter[] printers = new CupsPrinter[num_printers];
90         for (i=0; i < num_printers; i++)
91         {
92           try
93           {
94             u    = new URL("http://" + GLPvars.getServerName() + 
95                            ":631/printers/" + printer_names[i] );  
96             cups = new Cups(u);
97
98             // If authorization is required ....
99             cups.setUser(GLPvars.cupsUser);
100             cups.setPasswd(GLPvars.cupsPasswd);
101
102             printers[i] = new CupsPrinter( cups, printer_names[i] );
103           }
104           catch (IOException e)
105           {
106             // System.out.println("GLPprinters: IOException");
107             // return(null);
108           }
109         }
110
111
112         //
113         //  Keep track in case it changes.
114         //
115         cupsServerName = GLPvars.getServerName();
116
117         if (printer_names != null)
118           num_printers = printer_names.length;
119         else
120           num_printers = 0;
121
122         // default_printer = c.cupsGetDefault();
123
124         // Create the main panel to contain the two sub panels.
125         mainPanel   = new JPanel();
126         mainLayout  = new GridBagLayout();
127         mainConst   = new GridBagConstraints();
128
129         mainPanel.setLayout(mainLayout);
130         mainPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
131         mainPanel.setBackground(GLPcolors.backgroundColor);
132
133         //  --------------------------------------------------------------
134         //
135         //  Add the server name label
136         //
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 );
143
144         mainConst.gridwidth = GridBagConstraints.RELATIVE;
145         mainConst.gridx     = 0;
146         mainConst.gridy     = y++;
147         mainConst.fill      = GridBagConstraints.BOTH;
148         mainConst.weightx   = 0.0;
149         mainConst.weighty   = 0.0;
150         mainConst.ipadx     = 4;
151         mainConst.ipady     = 4;
152         mainLayout.setConstraints( serverPanel, mainConst );
153         mainPanel.add(serverPanel);
154
155         //  --------------------------------------------------------------
156         //
157         //  Add the printer masking panel
158         //
159         maskPanel   = new JPanel();
160         maskLayout  = new GridBagLayout();
161         maskConst   = new GridBagConstraints();
162
163         maskPanel.setLayout(maskLayout);
164         maskPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
165         maskPanel.setBackground(GLPcolors.backgroundColor);
166
167         JPanel localMaskPanel = buildMaskPanel();
168         maskConst.gridwidth = GridBagConstraints.RELATIVE;
169         maskConst.gridx     = 0;
170         maskConst.gridy     = 0;
171         maskConst.fill      = GridBagConstraints.NONE;
172         maskConst.weightx   = 0.0;
173         maskConst.weighty   = 0.0;
174         maskConst.ipadx     = 4;
175         maskConst.ipady     = 4;
176         maskLayout.setConstraints( localMaskPanel, maskConst );
177         maskPanel.add(localMaskPanel);
178
179         //
180         //  Add the masking panel to the main panel.
181         //
182         mainConst.gridwidth = GridBagConstraints.RELATIVE;
183         mainConst.gridx     = 0;
184         mainConst.gridy     = y++;
185         mainConst.fill      = GridBagConstraints.BOTH;
186         mainConst.weightx   = 0.0;
187         mainConst.weighty   = 0.0;
188         mainConst.ipadx     = 4;
189         mainConst.ipady     = 4;
190         mainLayout.setConstraints( maskPanel, mainConst );
191         mainPanel.add(maskPanel);
192
193
194
195         //  --------------------------------------------------------------
196         //
197         //  Add the printers
198         //
199         double weight = 1.0 / (double)printers.length;
200         for (i=0; i < printers.length; i++)
201         {
202           JPanel subPanel = printerInfoPanel( printers[i] );
203           mainConst.gridwidth = GridBagConstraints.RELATIVE;
204           mainConst.gridx     = 0;
205           mainConst.gridy     = y++;
206           mainConst.fill      = GridBagConstraints.BOTH;
207           mainConst.weightx   = 1.0;
208           mainConst.weighty   = weight;
209           mainConst.ipady     = 4;
210           mainLayout.setConstraints( subPanel, mainConst );
211           mainPanel.add(subPanel);
212         }
213
214         //  ------------------------------------------------
215         //
216         //  Put the whole thing into a scroll pane.
217         //
218         scrollPane = new JScrollPane(mainPanel);
219     }
220
221
222
223     //  -----------------------------------------------------------
224     //
225     //  Build an info panel for an individual printer.
226     //
227     private JPanel printerInfoPanel( CupsPrinter cp ) 
228     {
229         JPanel             printerPanel = new JPanel();
230         BoxLayout          printerBox;
231
232         JPanel             leftHeader   = new JPanel();
233         JPanel             rightHeader  = new JPanel();
234
235         JPanel             leftPane     = new JPanel();
236         JPanel             rightPane    = new JPanel();
237
238         GridBagLayout      leftLayout   = new GridBagLayout();
239         GridBagLayout      rightLayout  = new GridBagLayout();
240
241         GridBagConstraints leftConst    = new GridBagConstraints();
242         GridBagConstraints rightConst   = new GridBagConstraints();
243
244
245         JLabel      printerIconLabel   = null;
246         JLabel      printerInfoLabel   = null;
247         JLabel      printerNameLabel   = null;
248         JLabel      printerMakeLabel   = null;
249
250         JTable      printerStatusTable = null;
251
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);
256
257         // Add border around the panel.
258
259
260         //  ------------------------------------------------------------
261         //  Left pane
262         //  ------------------------------------------------------------
263         leftPane.setLayout(leftLayout);
264         leftPane.setBackground(GLPcolors.backgroundColor);
265
266         leftHeader.setLayout(new BorderLayout());
267         leftHeader.setBackground(GLPcolors.highlightColor);
268         leftHeader.setBorder(BorderFactory.createEmptyBorder(3,3,3,3));
269
270         printerNameLabel = new JLabel(cp.getPrinterName());
271         printerNameLabel.setForeground(Color.black);
272         leftHeader.add( printerNameLabel, BorderLayout.WEST);
273         leftConst.gridwidth = GridBagConstraints.RELATIVE;
274         leftConst.gridx     = 0;
275         leftConst.gridy     = 0;
276         leftConst.fill      = GridBagConstraints.HORIZONTAL;
277         leftConst.weightx   = 0.0;
278         leftConst.weighty   = 0.0;
279         leftConst.ipady     = 4;
280         leftLayout.setConstraints( leftHeader, leftConst );
281         leftPane.add(leftHeader);
282
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>",
289                                             icon );
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.");
296
297         leftConst.gridwidth = GridBagConstraints.REMAINDER;
298         leftConst.gridx     = 0;
299         leftConst.gridy     = 1;
300         leftConst.fill      = GridBagConstraints.BOTH;
301         leftConst.weightx   = 1.0;
302         leftConst.weighty   = 1.0;
303         leftConst.ipady     = 4;
304         leftLayout.setConstraints( printerButton, leftConst );
305         leftPane.add(printerButton);
306
307
308         //  ------------------------------------------------------------
309         //  Right pane
310         //  ------------------------------------------------------------
311         rightPane.setLayout(rightLayout);
312         rightPane.setBackground(GLPcolors.backgroundColor);
313
314         rightHeader.setLayout(new BorderLayout());
315         rightHeader.setBackground(GLPcolors.highlightColor);
316         rightHeader.setBorder(BorderFactory.createEmptyBorder(3,3,3,3));
317
318         printerMakeLabel = new JLabel(cp.getMakeAndModel());
319         printerMakeLabel.setForeground(Color.black);
320         rightHeader.add( printerMakeLabel, BorderLayout.WEST);
321
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);
331
332
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 );
336
337         JLabel pdNameLabel     = new JLabel("Name");
338         JLabel pdLocationLabel = new JLabel("Location");
339         JLabel pdStatusLabel   = new JLabel("Status");
340         JLabel pdMessageLabel  = new JLabel("Message");
341
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());
346
347         pdNameLabel.setFont(labelFont);
348         pdLocationLabel.setFont(labelFont);
349         pdStatusLabel.setFont(labelFont);
350         pdMessageLabel.setFont(labelFont);
351         pdMessageText.setFont(messageFont);
352
353         pdNameLabel.setForeground(Color.black);
354         pdLocationLabel.setForeground(Color.black);
355         pdStatusLabel.setForeground(Color.black);
356         pdMessageLabel.setForeground(Color.black);
357
358         JPanel tablePane;
359         if ((cp.getStateReasons().length() > 0) &&
360             (!cp.getStateReasons().equals("none")))
361         {
362           tablePane = new JPanel(new GridLayout(4,2,2,2));
363           tablePane.add(pdNameLabel);
364           tablePane.add(pdNameText);
365
366           tablePane.add(pdLocationLabel);
367           tablePane.add(pdLocationText);
368
369           tablePane.add(pdStatusLabel);
370           tablePane.add(pdStatusText);
371
372           tablePane.add(pdMessageLabel);
373           tablePane.add(pdMessageText);
374         }
375         else
376         {
377           tablePane = new JPanel(new GridLayout(3,2,2,2));
378           tablePane.add(pdNameLabel);
379           tablePane.add(pdNameText);
380
381           tablePane.add(pdLocationLabel);
382           tablePane.add(pdLocationText);
383
384           tablePane.add(pdStatusLabel);
385           tablePane.add(pdStatusText);
386
387         }
388         tablePane.setBackground(GLPcolors.backgroundColor);
389
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);
400
401         printerPanel.add(leftPane);
402         printerPanel.add(rightPane);
403
404         return(printerPanel);
405     }
406
407
408
409
410
411     public JPanel buildMaskPanel()
412     {
413
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();
418
419       namePanel.setLayout(nameLayout);
420       namePanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
421       namePanel.setBackground(GLPcolors.backgroundColor);
422
423
424       //Create a regular text field.
425       nameTextField = new JTextField(16);
426       nameTextField.setActionCommand(maskFieldString);
427       nameTextField.addActionListener(this);
428       nameTextField.setText("");
429
430       //Create some labels for the fields.
431       JLabel nameFieldLabel = new JLabel(maskFieldString);
432       nameFieldLabel.setForeground(GLPcolors.foregroundColor);
433       nameFieldLabel.setLabelFor(nameTextField);
434
435       //  Text
436       nameConst.gridwidth = GridBagConstraints.RELATIVE;
437       nameConst.gridx     = 0;
438       nameConst.gridy     = 0;
439       nameConst.fill      = GridBagConstraints.HORIZONTAL;
440       nameConst.weightx   = 0.0;
441       nameConst.weighty   = 0.0;
442       nameConst.ipadx     = 4;
443       nameConst.ipady     = 4;
444       nameLayout.setConstraints( nameFieldLabel, nameConst );
445       namePanel.add(nameFieldLabel);
446
447       nameConst.gridwidth = GridBagConstraints.RELATIVE;
448       nameConst.gridx     = 1;
449       nameConst.gridy     = 0;
450       nameConst.fill      = GridBagConstraints.HORIZONTAL;
451       nameConst.weightx   = 0.0;
452       nameConst.weighty   = 0.0;
453       nameConst.ipadx     = 4;
454       nameConst.ipady     = 4;
455       nameLayout.setConstraints( nameTextField, nameConst );
456       namePanel.add(nameTextField);
457
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);
464       nameConst.gridx     = 2;
465       nameConst.gridy     = 0;
466       nameConst.fill      = GridBagConstraints.NONE;
467       nameLayout.setConstraints( applyButton, nameConst );
468       nameConst.weightx   = 0.0;
469       nameConst.weighty   = 0.0;
470       nameConst.ipadx     = 4;
471       nameConst.ipady     = 4;
472       namePanel.add(applyButton);
473
474       return(namePanel);
475     }
476
477
478     public void actionPerformed(ActionEvent e) 
479     {
480         if (e.getActionCommand().equals(maskFieldString)) 
481         {
482           String s = nameTextField.getText();
483           if (s.length() > 1)
484           {
485             currentMask = s;
486           }
487         } 
488         else if (e.getActionCommand().equals(maskButtonString)) 
489         {
490           String s = nameTextField.getText();
491           if (s.length() > 1)
492           {
493             currentMask = s;
494           }
495         } 
496         else
497         {
498           GLPvars.selectedPrinterName = e.getActionCommand();
499           GLPvars.tabs.updateDetailPanel();
500           GLPvars.tabs.tabPanel.setSelectedIndex(2);
501         }
502     }
503
504
505     public JScrollPane getPanel()
506     {
507       return(scrollPane);
508     }
509 }