]> git.ipfire.org Git - thirdparty/cups.git/blame - scripting/java/example/GLPoptions.java
Add missing files for test.
[thirdparty/cups.git] / scripting / java / example / GLPoptions.java
CommitLineData
ef416fc2 1
2import java.awt.*;
3import java.awt.event.*;
4import javax.swing.*;
5import javax.swing.filechooser.*;
6import java.net.URL;
7import java.net.*;
8import java.io.*;
9import com.easysw.cups.*;
10
11public class GLPoptions implements ActionListener
12{
13 Cups cups = null;
14 CupsJob job = null;
15 CupsPrinter printer = null;
16
17 String fileName = "";
18
19 JPanel mainPanel;
20 JTextField fileTextField;
21 JButton printButton;
22 GridBagLayout mainLayout;
23 GridBagConstraints mainConst;
24
25 //
26 // Print options;
27 //
28 String[] jobSheetsNames;
29 String[] orientationNames;
30 int[] orientationValues;
31 String[] qualityNames;
32 int[] qualityValues;
33
34 String jobSheetsOption = "";
35 int orientationOption = -1;
36 int qualityOption = -1;
37
38 int pageLowerOption = 0;
39 int pageUpperOption = 0;
40 boolean pagePrintAll = true;
41
42 int numCopiesOption = 1;
43 int numLowerCopiesOption = -1;
44 int numUpperCopiesOption = -1;
45
46 int selectedJobSheets = 0;
47 int selectedOrientation = 0;
48 int selectedQuality = 0;
49
50 JComboBox orientationBox;
51 JComboBox jobSheetsBox;
52 JTextField numCopiesField;
53 JCheckBox printAllCheckBox;
54 JTextField pageLowerField;
55 JTextField pageUpperField;
56 MyTextListener textListener = new MyTextListener();
57
58
59 // Constructor
60 public GLPoptions()
61 {
62 mainPanel = new JPanel();
63 mainPanel.setLayout(new BorderLayout());
64 mainPanel.setBackground(GLPcolors.backgroundColor);
65 JLabel label = new JLabel("No printer selected");
66 label.setForeground(GLPcolors.foregroundColor);
67 mainPanel.add(label,BorderLayout.CENTER);
68 }
69
70 // Constructor
71 public GLPoptions(CupsPrinter cp)
72 {
73 printer = cp;
74 if (printer != null)
75 {
76 load(printer);
77 }
78 else
79 {
80 mainPanel = new JPanel();
81 mainPanel.setLayout(new BorderLayout());
82 mainPanel.setBackground(GLPcolors.backgroundColor);
83 JLabel label = new JLabel("No printer selected");
84 label.setForeground(GLPcolors.foregroundColor);
85 mainPanel.add(label,BorderLayout.CENTER);
86 }
87 }
88
89
90 private void load( CupsPrinter cp )
91 {
92
93 fillOptionValues();
94
95 // Create the main panel to contain the two sub panels.
96 mainPanel = new JPanel();
97 mainLayout = new GridBagLayout();
98 mainConst = new GridBagConstraints();
99
100 mainPanel.setLayout(mainLayout);
101 mainPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
102 mainPanel.setBackground(GLPcolors.backgroundColor);
103
104 String tmp_s = "Printing to " + printer.getPrinterName() +
105 " on " + GLPvars.cupsServerName;
106 JLabel printerNameText = new JLabel(tmp_s);
107 printerNameText.setForeground(GLPcolors.foregroundColor);
108 mainConst.gridwidth = GridBagConstraints.RELATIVE;
109 mainConst.gridx = 0;
110 mainConst.gridy = 0;
111 mainConst.fill = GridBagConstraints.NONE;
112 mainConst.weightx = 0.0;
113 mainConst.weighty = 0.0;
114 mainConst.ipady = 4;
115 mainLayout.setConstraints( printerNameText, mainConst );
116 mainPanel.add(printerNameText);
117
118 JPanel filePanel = buildFilePanel();
119 mainConst.gridwidth = GridBagConstraints.RELATIVE;
120 mainConst.gridx = 0;
121 mainConst.gridy = 1;
122 mainConst.fill = GridBagConstraints.HORIZONTAL;
123 mainConst.weightx = 1.0;
124 mainConst.weighty = 0.1;
125 mainConst.ipady = 4;
126 mainLayout.setConstraints( filePanel, mainConst );
127 mainPanel.add(filePanel);
128
129 JPanel orientationPanel = buildOrientationComboBox();
130 mainConst.gridwidth = GridBagConstraints.RELATIVE;
131 mainConst.gridx = 0;
132 mainConst.gridy = 2;
133 mainConst.fill = GridBagConstraints.NONE;
134 mainConst.weightx = 0.8;
135 mainConst.weighty = 0.1;
136 mainConst.ipady = 4;
137 mainLayout.setConstraints( orientationPanel, mainConst );
138 mainPanel.add(orientationPanel);
139
140 JPanel jobSheetsPanel = buildJobSheetsComboBox();
141 mainConst.gridwidth = GridBagConstraints.RELATIVE;
142 mainConst.gridx = 0;
143 mainConst.gridy = 3;
144 mainConst.fill = GridBagConstraints.NONE;
145 mainConst.weightx = 0.8;
146 mainConst.weighty = 0.1;
147 mainConst.ipady = 4;
148 mainLayout.setConstraints( jobSheetsPanel, mainConst );
149 mainPanel.add(jobSheetsPanel);
150
151 JPanel numCopiesPanel = buildNumCopiesPanel();
152 mainConst.gridwidth = GridBagConstraints.RELATIVE;
153 mainConst.gridx = 0;
154 mainConst.gridy = 4;
155 mainConst.fill = GridBagConstraints.HORIZONTAL;
156 mainConst.weightx = 1.0;
157 mainConst.weighty = 0.1;
158 mainConst.ipady = 4;
159 mainLayout.setConstraints( numCopiesPanel, mainConst );
160 mainPanel.add(numCopiesPanel);
161
162 JPanel pageRangePanel = buildPageRangePanel();
163 mainConst.gridwidth = GridBagConstraints.RELATIVE;
164 mainConst.gridx = 0;
165 mainConst.gridy = 5;
166 mainConst.fill = GridBagConstraints.HORIZONTAL;
167 mainConst.weightx = 1.0;
168 mainConst.weighty = 0.1;
169 mainConst.ipady = 4;
170 mainLayout.setConstraints( pageRangePanel, mainConst );
171 mainPanel.add(pageRangePanel);
172
173 JPanel buttonPanel = buildButtonPanel();
174 mainConst.gridwidth = GridBagConstraints.RELATIVE;
175 mainConst.gridx = 0;
176 mainConst.gridy = 6;
177 mainConst.fill = GridBagConstraints.NONE;
178 mainConst.weightx = 1.0;
179 mainConst.weighty = 0.1;
180 mainConst.ipady = 4;
181 mainLayout.setConstraints( buttonPanel, mainConst );
182 mainPanel.add(buttonPanel);
183
184 }
185
186
187 // --------------------------------------------------------------
188 //
189 // Filename / Browse panel
190 //
191 public JPanel buildFilePanel()
192 {
193 JPanel localPanel = new JPanel();
194 final JFileChooser fc = new JFileChooser();
195
196 localPanel.setBackground(GLPcolors.backgroundColor);
197 localPanel.setLayout(new BorderLayout());
198
199 //Create a regular text field.
200 fileTextField = new JTextField(50);
201 fileTextField.addActionListener(this);
202
203 //Create some labels for the fields.
204 JLabel fileFieldLabel = new JLabel(" File to print: ");
205 fileFieldLabel.setForeground(GLPcolors.foregroundColor);
206 // fileFieldLabel.setLabelFor(fileTextField);
207
208 localPanel.add( fileFieldLabel, BorderLayout.WEST );
209 localPanel.add( fileTextField, BorderLayout.CENTER );
210
211 //Create the open button
212 JButton openButton = new JButton("Browse ..." );
213 openButton.addActionListener(new ActionListener()
214 {
215 public void actionPerformed(ActionEvent e)
216 {
217 int returnVal = fc.showOpenDialog(mainPanel);
218 if (returnVal == JFileChooser.APPROVE_OPTION)
219 {
220 File file = fc.getSelectedFile();
221 // fileTextField.setText(file.getPath() + file.getName());
222 fileTextField.setText(file.getPath());
223 fileName = file.getPath();
224 }
225 }
226 });
227 openButton.setBackground(GLPcolors.buttonBackgroundColor);
228 openButton.setForeground(GLPcolors.buttonForegroundColor);
229 localPanel.add(openButton, BorderLayout.EAST );
230 return(localPanel);
231 }
232
233
234
235 public JPanel buildOrientationComboBox()
236 {
237 JPanel localPanel = new JPanel();
238 localPanel.setLayout(new BorderLayout());
239 localPanel.setBackground(GLPcolors.backgroundColor);
240
241 JLabel localLabel = new JLabel("Page Orientation: ");
242 localLabel.setBackground(GLPcolors.backgroundColor);
243 localLabel.setForeground(GLPcolors.foregroundColor);
244
245 orientationBox = new JComboBox(orientationNames);
246 if (selectedOrientation > 0)
247 orientationBox.setSelectedIndex(selectedOrientation);
248 orientationBox.addActionListener(this);
249 orientationBox.setBackground(GLPcolors.backgroundColor);
250
251 localPanel.add(localLabel,BorderLayout.WEST);
252 localPanel.add(orientationBox,BorderLayout.CENTER);
253
254 return(localPanel);
255 }
256
257
258 public JPanel buildJobSheetsComboBox()
259 {
260 JPanel localPanel = new JPanel();
261 localPanel.setLayout(new BorderLayout());
262 localPanel.setBackground(GLPcolors.backgroundColor);
263
264 JLabel localLabel = new JLabel("Job Sheets: ");
265 localLabel.setBackground(GLPcolors.backgroundColor);
266 localLabel.setForeground(GLPcolors.foregroundColor);
267
268 jobSheetsBox = new JComboBox(jobSheetsNames);
269 if (selectedJobSheets > 0)
270 jobSheetsBox.setSelectedIndex(selectedJobSheets);
271 jobSheetsBox.addActionListener(this);
272 jobSheetsBox.setBackground(GLPcolors.backgroundColor);
273
274 localPanel.add(localLabel,BorderLayout.WEST);
275 localPanel.add(jobSheetsBox,BorderLayout.CENTER);
276
277 return(localPanel);
278 }
279
280 public JPanel buildNumCopiesPanel()
281 {
282 JPanel localPanel = new JPanel();
283 localPanel.setLayout(new FlowLayout());
284 localPanel.setBackground(GLPcolors.backgroundColor);
285
286 JLabel localLabel = new JLabel("Number of copies: ");
287 localLabel.setBackground(GLPcolors.backgroundColor);
288 localLabel.setForeground(GLPcolors.foregroundColor);
289
290 numCopiesField = new JTextField(3);
291 if (numCopiesOption > 0)
292 numCopiesField.setText(new Integer(numCopiesOption).toString());
293 numCopiesField.addActionListener(this);
294 numCopiesField.addFocusListener(textListener);
295 numCopiesField.setBackground(GLPcolors.backgroundColor);
296
297 localPanel.add(localLabel);
298 localPanel.add(numCopiesField);
299
300 return(localPanel);
301 }
302
303
304 public JPanel buildPageRangePanel()
305 {
306 JPanel localPanel = new JPanel();
307 localPanel.setLayout(new FlowLayout());
308 localPanel.setBackground(GLPcolors.backgroundColor);
309
310 printAllCheckBox = new JCheckBox("Print all", pagePrintAll );
311 printAllCheckBox.setBackground(GLPcolors.backgroundColor);
312 printAllCheckBox.setForeground(GLPcolors.foregroundColor);
313 printAllCheckBox.addActionListener(this);
314
315 JLabel localLabel = new JLabel("-or- pages: ");
316 localLabel.setBackground(GLPcolors.backgroundColor);
317 localLabel.setForeground(GLPcolors.foregroundColor);
318
319 JLabel localLabel2 = new JLabel(" to ");
320 localLabel2.setBackground(GLPcolors.backgroundColor);
321 localLabel2.setForeground(GLPcolors.foregroundColor);
322
323 pageLowerField = new JTextField(4);
324 pageUpperField = new JTextField(4);
325
326 pageLowerField.addActionListener(this);
327 pageUpperField.addActionListener(this);
328 pageLowerField.addFocusListener(textListener);
329 pageUpperField.addFocusListener(textListener);
330
331 pageLowerField.setBackground(GLPcolors.backgroundColor);
332 pageUpperField.setBackground(GLPcolors.backgroundColor);
333 pageLowerField.setEnabled(false);
334 pageUpperField.setEnabled(false);
335
336 localPanel.add(printAllCheckBox);
337 localPanel.add(localLabel);
338 localPanel.add(pageLowerField);
339 localPanel.add(localLabel2);
340 localPanel.add(pageUpperField);
341
342 return(localPanel);
343 }
344
345
346
347
348 public JPanel buildTextPanel()
349 {
350 JPanel localPanel = new JPanel();
351 return(localPanel);
352 }
353
354 public JPanel buildButtonPanel()
355 {
356 JPanel localPanel = new JPanel();
357 localPanel.setLayout(new BorderLayout());
358 printButton = new JButton(" Print ");
359 printButton.setBackground(GLPcolors.buttonBackgroundColor);
360 printButton.setForeground(GLPcolors.buttonForegroundColor);
361 printButton.addActionListener( this );
362 localPanel.add(printButton, BorderLayout.WEST );
363 return(localPanel);
364 }
365
366
367
368 public void updateOptions(CupsPrinter cp)
369 {
370 printer = cp;
371 if (printer != null)
372 {
373 load(printer);
374 }
375 else
376 {
377 mainPanel = new JPanel();
378 mainPanel.setLayout(new BorderLayout());
379 mainPanel.setBackground(GLPcolors.backgroundColor);
380 JLabel label = new JLabel("No printer selected");
381 label.setForeground(GLPcolors.foregroundColor);
382 mainPanel.add(label,BorderLayout.CENTER);
383 }
384 }
385
386 public JPanel getPanel()
387 {
388 return(mainPanel);
389 }
390
391
392
393
394 public CupsJob printFile( String filename )
395 {
396 Cups cups;
397 CupsJob job;
398 URL u;
399 IPPAttribute attrs[];
400
401 attrs = buildPrintAttributes();
402
403 // for (int i=0; i < attrs.length; i++)
404 // attrs[i].dump_values();
405
406 try
407 {
408 u = new URL("http://" + GLPvars.getServerName() +
409 ":631/printers/" + printer.getPrinterName() );
410 cups = new Cups(u);
411 cups.setUser(GLPvars.cupsUser);
412 cups.setPasswd(GLPvars.cupsPasswd);
413
414 job = cups.cupsPrintFile(filename,attrs);
415 return(job);
416 }
417 catch (IOException e)
418 {
419 return(null);
420 }
421 }
422
423
424
425
426 private void fillOptionValues()
427 {
428 IPPAttribute a;
429 int i, n;
430
431 //
432 // Job sheets ....
433 //
434 jobSheetsNames = printer.getJobSheetsSupported();
435 if (printer.getJobSheetsDefault() != "none")
436 {
437 for (i=0; i < jobSheetsNames.length; i++)
438 if (jobSheetsNames[i] == printer.getJobSheetsDefault())
439 selectedJobSheets = i;
440 }
441
442 //
443 // Orientation ....
444 //
445 orientationNames = new String[printer.getOrientationSupported().length];
446 orientationValues = printer.getOrientationSupported();
447 for (i=0; i < printer.getOrientationSupported().length; i++)
448 {
449 if (orientationValues[i] == printer.getOrientationDefault())
450 selectedOrientation = i;
451 switch( orientationValues[i] )
452 {
453 case IPPDefs.PORTRAIT:
454 orientationNames[i] = "Portrait";
455 break;
456 case IPPDefs.LANDSCAPE:
457 orientationNames[i] = "Landscape";
458 break;
459 case IPPDefs.REVERSE_LANDSCAPE:
460 orientationNames[i] = "Reverse Landscape";
461 break;
462 case IPPDefs.REVERSE_PORTRAIT:
463 orientationNames[i] = "Reverse Portrait";
464 break;
465 }
466 }
467
468 if (printer.getLowerCopiesSupported() ==
469 printer.getUpperCopiesSupported())
470 {
471 numCopiesOption = printer.getCopiesDefault();
472 }
473 else
474 {
475 numCopiesOption = printer.getLowerCopiesSupported();
476 numLowerCopiesOption = printer.getLowerCopiesSupported();
477 numUpperCopiesOption = printer.getUpperCopiesSupported();
478 }
479 }
480
481
482 private IPPAttribute[] buildPrintAttributes()
483 {
484 IPPAttribute a;
485 IPPAttribute[] attrs;
486 int num_attrs = 0;
487
488 if (orientationOption >= 0)
489 num_attrs++;
490 if (jobSheetsOption.length() > 0)
491 num_attrs++;
492 if (numCopiesOption > 1)
493 num_attrs++;
494 if ((pageLowerOption > 0) && (pageUpperOption > 0) && (!pagePrintAll))
495 num_attrs++;
496
497 if (num_attrs > 0)
498 attrs = new IPPAttribute[num_attrs];
499 else
500 return(null);
501
502 int i = 0;
503 if (jobSheetsOption.length() > 0)
504 {
505 attrs[i] = new IPPAttribute( IPPDefs.TAG_JOB,
506 IPPDefs.TAG_NAME,
507 "job-sheets" );
508 attrs[i].addString( "", jobSheetsOption );
509 i++;
510 }
511 if (orientationOption >= IPPDefs.PORTRAIT)
512 {
513 attrs[i] = new IPPAttribute( IPPDefs.TAG_JOB,
514 IPPDefs.TAG_ENUM,
515 "orientation-requested" );
516 attrs[i].addEnum( orientationOption );
517 i++;
518 }
519 if (numCopiesOption > 1)
520 {
521 attrs[i] = new IPPAttribute( IPPDefs.TAG_JOB,
522 IPPDefs.TAG_INTEGER,
523 "copies" );
524 attrs[i].addInteger( numCopiesOption );
525 i++;
526 }
527 if ((pageLowerOption > 0) && (pageUpperOption > 0) && (!pagePrintAll))
528 {
529 attrs[i] = new IPPAttribute( IPPDefs.TAG_JOB,
530 IPPDefs.TAG_RANGE,
531 "page-ranges" );
532 attrs[i].addRange( pageLowerOption, pageUpperOption );
533 i++;
534 }
535 return(attrs);
536 }
537
538
539
540 // Implementation of ActionListener interface.
541 public void actionPerformed(ActionEvent e)
542 {
543 Object source = e.getSource();
544
545 //
546 // Name typed in
547 //
548 if (source == printAllCheckBox)
549 {
550 JCheckBox cb = (JCheckBox)source;
551 pagePrintAll = cb.isSelected();
552 pageLowerField.setEnabled(!pagePrintAll);
553 pageUpperField.setEnabled(!pagePrintAll);
554 }
555 else if (source == pageLowerField)
556 {
557 String s = pageLowerField.getText();
558 if (s.length() > 1)
559 {
560 pageLowerOption = new Integer(s).intValue();
561 // if (pageLowerOption > 0)
562 // printAllCheckBox.setChecked(false);
563 }
564 }
565 else if (source == pageUpperField)
566 {
567 String s = pageUpperField.getText();
568 if (s.length() > 1)
569 {
570 pageUpperOption = new Integer(s).intValue();
571 // if (pageUpperOption > 0)
572 // printAllCheckBox.setChecked(false);
573 }
574 }
575 else if (source == orientationBox)
576 {
577 JComboBox cb = (JComboBox)source;
578 selectedOrientation = cb.getSelectedIndex();
579 orientationOption = orientationValues[selectedOrientation];
580 }
581 else if (source == jobSheetsBox)
582 {
583 JComboBox cb = (JComboBox)source;
584 selectedJobSheets = cb.getSelectedIndex();
585 jobSheetsOption = jobSheetsNames[selectedJobSheets];
586 }
587 else if (source == numCopiesField)
588 {
589 String s = numCopiesField.getText();
590 if (s.length() >= 1)
591 {
592 numCopiesOption = new Integer(s).intValue();
593 }
594 }
595 else if (source == fileTextField)
596 {
597 String s = fileTextField.getText();
598 if (s.length() > 1)
599 {
600 fileName = s;
601 }
602 }
603 else if (source == printButton)
604 {
605 if (fileName.length() > 1)
606 {
607 job = printFile( fileName );
608 if (job != null)
609 {
610 fileName = "";
611 fileTextField.setText("");
612 JOptionPane.showMessageDialog(mainPanel,
613 "Job " + printer.getPrinterName() + "-" +
614 new Integer(job.job_id).toString() +
615 " queued.");
616 }
617 }
618 }
619 }
620
621
622
623
624
625 public class MyTextListener implements FocusListener
626 {
627
628 public void focusGained(FocusEvent e)
629 {
630 }
631
632 public void focusLost(FocusEvent e)
633 {
634 JTextField txtField = (JTextField)e.getSource();
635 if (txtField == numCopiesField)
636 {
637 String s = numCopiesField.getText();
638 if (s.length() >= 1)
639 {
640 numCopiesOption = new Integer(s).intValue();
641 }
642 }
643 else if (txtField == pageLowerField)
644 {
645 String s = pageLowerField.getText();
646 if (s.length() >= 1)
647 {
648 pageLowerOption = new Integer(s).intValue();
649 }
650 }
651 else if (txtField == pageUpperField)
652 {
653 String s = pageUpperField.getText();
654 if (s.length() >= 1)
655 {
656 pageUpperOption = new Integer(s).intValue();
657 }
658 }
659 }
660 }
661
662}