3 import java
.awt
.event
.*;
5 import javax
.swing
.filechooser
.*;
9 import com
.easysw
.cups
.*;
11 public class GLPoptions
implements ActionListener
15 CupsPrinter printer
= null;
20 JTextField fileTextField
;
22 GridBagLayout mainLayout
;
23 GridBagConstraints mainConst
;
28 String
[] jobSheetsNames
;
29 String
[] orientationNames
;
30 int[] orientationValues
;
31 String
[] qualityNames
;
34 String jobSheetsOption
= "";
35 int orientationOption
= -1;
36 int qualityOption
= -1;
38 int pageLowerOption
= 0;
39 int pageUpperOption
= 0;
40 boolean pagePrintAll
= true;
42 int numCopiesOption
= 1;
43 int numLowerCopiesOption
= -1;
44 int numUpperCopiesOption
= -1;
46 int selectedJobSheets
= 0;
47 int selectedOrientation
= 0;
48 int selectedQuality
= 0;
50 JComboBox orientationBox
;
51 JComboBox jobSheetsBox
;
52 JTextField numCopiesField
;
53 JCheckBox printAllCheckBox
;
54 JTextField pageLowerField
;
55 JTextField pageUpperField
;
56 MyTextListener textListener
= new MyTextListener();
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
);
71 public GLPoptions(CupsPrinter cp
)
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
);
90 private void load( CupsPrinter cp
)
95 // Create the main panel to contain the two sub panels.
96 mainPanel
= new JPanel();
97 mainLayout
= new GridBagLayout();
98 mainConst
= new GridBagConstraints();
100 mainPanel
.setLayout(mainLayout
);
101 mainPanel
.setBorder(BorderFactory
.createEmptyBorder(5,5,5,5));
102 mainPanel
.setBackground(GLPcolors
.backgroundColor
);
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
;
111 mainConst
.fill
= GridBagConstraints
.NONE
;
112 mainConst
.weightx
= 0.0;
113 mainConst
.weighty
= 0.0;
115 mainLayout
.setConstraints( printerNameText
, mainConst
);
116 mainPanel
.add(printerNameText
);
118 JPanel filePanel
= buildFilePanel();
119 mainConst
.gridwidth
= GridBagConstraints
.RELATIVE
;
122 mainConst
.fill
= GridBagConstraints
.HORIZONTAL
;
123 mainConst
.weightx
= 1.0;
124 mainConst
.weighty
= 0.1;
126 mainLayout
.setConstraints( filePanel
, mainConst
);
127 mainPanel
.add(filePanel
);
129 JPanel orientationPanel
= buildOrientationComboBox();
130 mainConst
.gridwidth
= GridBagConstraints
.RELATIVE
;
133 mainConst
.fill
= GridBagConstraints
.NONE
;
134 mainConst
.weightx
= 0.8;
135 mainConst
.weighty
= 0.1;
137 mainLayout
.setConstraints( orientationPanel
, mainConst
);
138 mainPanel
.add(orientationPanel
);
140 JPanel jobSheetsPanel
= buildJobSheetsComboBox();
141 mainConst
.gridwidth
= GridBagConstraints
.RELATIVE
;
144 mainConst
.fill
= GridBagConstraints
.NONE
;
145 mainConst
.weightx
= 0.8;
146 mainConst
.weighty
= 0.1;
148 mainLayout
.setConstraints( jobSheetsPanel
, mainConst
);
149 mainPanel
.add(jobSheetsPanel
);
151 JPanel numCopiesPanel
= buildNumCopiesPanel();
152 mainConst
.gridwidth
= GridBagConstraints
.RELATIVE
;
155 mainConst
.fill
= GridBagConstraints
.HORIZONTAL
;
156 mainConst
.weightx
= 1.0;
157 mainConst
.weighty
= 0.1;
159 mainLayout
.setConstraints( numCopiesPanel
, mainConst
);
160 mainPanel
.add(numCopiesPanel
);
162 JPanel pageRangePanel
= buildPageRangePanel();
163 mainConst
.gridwidth
= GridBagConstraints
.RELATIVE
;
166 mainConst
.fill
= GridBagConstraints
.HORIZONTAL
;
167 mainConst
.weightx
= 1.0;
168 mainConst
.weighty
= 0.1;
170 mainLayout
.setConstraints( pageRangePanel
, mainConst
);
171 mainPanel
.add(pageRangePanel
);
173 JPanel buttonPanel
= buildButtonPanel();
174 mainConst
.gridwidth
= GridBagConstraints
.RELATIVE
;
177 mainConst
.fill
= GridBagConstraints
.NONE
;
178 mainConst
.weightx
= 1.0;
179 mainConst
.weighty
= 0.1;
181 mainLayout
.setConstraints( buttonPanel
, mainConst
);
182 mainPanel
.add(buttonPanel
);
187 // --------------------------------------------------------------
189 // Filename / Browse panel
191 public JPanel
buildFilePanel()
193 JPanel localPanel
= new JPanel();
194 final JFileChooser fc
= new JFileChooser();
196 localPanel
.setBackground(GLPcolors
.backgroundColor
);
197 localPanel
.setLayout(new BorderLayout());
199 //Create a regular text field.
200 fileTextField
= new JTextField(50);
201 fileTextField
.addActionListener(this);
203 //Create some labels for the fields.
204 JLabel fileFieldLabel
= new JLabel(" File to print: ");
205 fileFieldLabel
.setForeground(GLPcolors
.foregroundColor
);
206 // fileFieldLabel.setLabelFor(fileTextField);
208 localPanel
.add( fileFieldLabel
, BorderLayout
.WEST
);
209 localPanel
.add( fileTextField
, BorderLayout
.CENTER
);
211 //Create the open button
212 JButton openButton
= new JButton("Browse ..." );
213 openButton
.addActionListener(new ActionListener()
215 public void actionPerformed(ActionEvent e
)
217 int returnVal
= fc
.showOpenDialog(mainPanel
);
218 if (returnVal
== JFileChooser
.APPROVE_OPTION
)
220 File file
= fc
.getSelectedFile();
221 // fileTextField.setText(file.getPath() + file.getName());
222 fileTextField
.setText(file
.getPath());
223 fileName
= file
.getPath();
227 openButton
.setBackground(GLPcolors
.buttonBackgroundColor
);
228 openButton
.setForeground(GLPcolors
.buttonForegroundColor
);
229 localPanel
.add(openButton
, BorderLayout
.EAST
);
235 public JPanel
buildOrientationComboBox()
237 JPanel localPanel
= new JPanel();
238 localPanel
.setLayout(new BorderLayout());
239 localPanel
.setBackground(GLPcolors
.backgroundColor
);
241 JLabel localLabel
= new JLabel("Page Orientation: ");
242 localLabel
.setBackground(GLPcolors
.backgroundColor
);
243 localLabel
.setForeground(GLPcolors
.foregroundColor
);
245 orientationBox
= new JComboBox(orientationNames
);
246 if (selectedOrientation
> 0)
247 orientationBox
.setSelectedIndex(selectedOrientation
);
248 orientationBox
.addActionListener(this);
249 orientationBox
.setBackground(GLPcolors
.backgroundColor
);
251 localPanel
.add(localLabel
,BorderLayout
.WEST
);
252 localPanel
.add(orientationBox
,BorderLayout
.CENTER
);
258 public JPanel
buildJobSheetsComboBox()
260 JPanel localPanel
= new JPanel();
261 localPanel
.setLayout(new BorderLayout());
262 localPanel
.setBackground(GLPcolors
.backgroundColor
);
264 JLabel localLabel
= new JLabel("Job Sheets: ");
265 localLabel
.setBackground(GLPcolors
.backgroundColor
);
266 localLabel
.setForeground(GLPcolors
.foregroundColor
);
268 jobSheetsBox
= new JComboBox(jobSheetsNames
);
269 if (selectedJobSheets
> 0)
270 jobSheetsBox
.setSelectedIndex(selectedJobSheets
);
271 jobSheetsBox
.addActionListener(this);
272 jobSheetsBox
.setBackground(GLPcolors
.backgroundColor
);
274 localPanel
.add(localLabel
,BorderLayout
.WEST
);
275 localPanel
.add(jobSheetsBox
,BorderLayout
.CENTER
);
280 public JPanel
buildNumCopiesPanel()
282 JPanel localPanel
= new JPanel();
283 localPanel
.setLayout(new FlowLayout());
284 localPanel
.setBackground(GLPcolors
.backgroundColor
);
286 JLabel localLabel
= new JLabel("Number of copies: ");
287 localLabel
.setBackground(GLPcolors
.backgroundColor
);
288 localLabel
.setForeground(GLPcolors
.foregroundColor
);
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
);
297 localPanel
.add(localLabel
);
298 localPanel
.add(numCopiesField
);
304 public JPanel
buildPageRangePanel()
306 JPanel localPanel
= new JPanel();
307 localPanel
.setLayout(new FlowLayout());
308 localPanel
.setBackground(GLPcolors
.backgroundColor
);
310 printAllCheckBox
= new JCheckBox("Print all", pagePrintAll
);
311 printAllCheckBox
.setBackground(GLPcolors
.backgroundColor
);
312 printAllCheckBox
.setForeground(GLPcolors
.foregroundColor
);
313 printAllCheckBox
.addActionListener(this);
315 JLabel localLabel
= new JLabel("-or- pages: ");
316 localLabel
.setBackground(GLPcolors
.backgroundColor
);
317 localLabel
.setForeground(GLPcolors
.foregroundColor
);
319 JLabel localLabel2
= new JLabel(" to ");
320 localLabel2
.setBackground(GLPcolors
.backgroundColor
);
321 localLabel2
.setForeground(GLPcolors
.foregroundColor
);
323 pageLowerField
= new JTextField(4);
324 pageUpperField
= new JTextField(4);
326 pageLowerField
.addActionListener(this);
327 pageUpperField
.addActionListener(this);
328 pageLowerField
.addFocusListener(textListener
);
329 pageUpperField
.addFocusListener(textListener
);
331 pageLowerField
.setBackground(GLPcolors
.backgroundColor
);
332 pageUpperField
.setBackground(GLPcolors
.backgroundColor
);
333 pageLowerField
.setEnabled(false);
334 pageUpperField
.setEnabled(false);
336 localPanel
.add(printAllCheckBox
);
337 localPanel
.add(localLabel
);
338 localPanel
.add(pageLowerField
);
339 localPanel
.add(localLabel2
);
340 localPanel
.add(pageUpperField
);
348 public JPanel
buildTextPanel()
350 JPanel localPanel
= new JPanel();
354 public JPanel
buildButtonPanel()
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
);
368 public void updateOptions(CupsPrinter cp
)
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
);
386 public JPanel
getPanel()
394 public CupsJob
printFile( String filename
)
399 IPPAttribute attrs
[];
401 attrs
= buildPrintAttributes();
403 // for (int i=0; i < attrs.length; i++)
404 // attrs[i].dump_values();
408 u
= new URL("http://" + GLPvars
.getServerName() +
409 ":631/printers/" + printer
.getPrinterName() );
411 cups
.setUser(GLPvars
.cupsUser
);
412 cups
.setPasswd(GLPvars
.cupsPasswd
);
414 job
= cups
.cupsPrintFile(filename
,attrs
);
417 catch (IOException e
)
426 private void fillOptionValues()
434 jobSheetsNames
= printer
.getJobSheetsSupported();
435 if (printer
.getJobSheetsDefault() != "none")
437 for (i
=0; i
< jobSheetsNames
.length
; i
++)
438 if (jobSheetsNames
[i
] == printer
.getJobSheetsDefault())
439 selectedJobSheets
= i
;
445 orientationNames
= new String
[printer
.getOrientationSupported().length
];
446 orientationValues
= printer
.getOrientationSupported();
447 for (i
=0; i
< printer
.getOrientationSupported().length
; i
++)
449 if (orientationValues
[i
] == printer
.getOrientationDefault())
450 selectedOrientation
= i
;
451 switch( orientationValues
[i
] )
453 case IPPDefs
.PORTRAIT
:
454 orientationNames
[i
] = "Portrait";
456 case IPPDefs
.LANDSCAPE
:
457 orientationNames
[i
] = "Landscape";
459 case IPPDefs
.REVERSE_LANDSCAPE
:
460 orientationNames
[i
] = "Reverse Landscape";
462 case IPPDefs
.REVERSE_PORTRAIT
:
463 orientationNames
[i
] = "Reverse Portrait";
468 if (printer
.getLowerCopiesSupported() ==
469 printer
.getUpperCopiesSupported())
471 numCopiesOption
= printer
.getCopiesDefault();
475 numCopiesOption
= printer
.getLowerCopiesSupported();
476 numLowerCopiesOption
= printer
.getLowerCopiesSupported();
477 numUpperCopiesOption
= printer
.getUpperCopiesSupported();
482 private IPPAttribute
[] buildPrintAttributes()
485 IPPAttribute
[] attrs
;
488 if (orientationOption
>= 0)
490 if (jobSheetsOption
.length() > 0)
492 if (numCopiesOption
> 1)
494 if ((pageLowerOption
> 0) && (pageUpperOption
> 0) && (!pagePrintAll
))
498 attrs
= new IPPAttribute
[num_attrs
];
503 if (jobSheetsOption
.length() > 0)
505 attrs
[i
] = new IPPAttribute( IPPDefs
.TAG_JOB
,
508 attrs
[i
].addString( "", jobSheetsOption
);
511 if (orientationOption
>= IPPDefs
.PORTRAIT
)
513 attrs
[i
] = new IPPAttribute( IPPDefs
.TAG_JOB
,
515 "orientation-requested" );
516 attrs
[i
].addEnum( orientationOption
);
519 if (numCopiesOption
> 1)
521 attrs
[i
] = new IPPAttribute( IPPDefs
.TAG_JOB
,
524 attrs
[i
].addInteger( numCopiesOption
);
527 if ((pageLowerOption
> 0) && (pageUpperOption
> 0) && (!pagePrintAll
))
529 attrs
[i
] = new IPPAttribute( IPPDefs
.TAG_JOB
,
532 attrs
[i
].addRange( pageLowerOption
, pageUpperOption
);
540 // Implementation of ActionListener interface.
541 public void actionPerformed(ActionEvent e
)
543 Object source
= e
.getSource();
548 if (source
== printAllCheckBox
)
550 JCheckBox cb
= (JCheckBox
)source
;
551 pagePrintAll
= cb
.isSelected();
552 pageLowerField
.setEnabled(!pagePrintAll
);
553 pageUpperField
.setEnabled(!pagePrintAll
);
555 else if (source
== pageLowerField
)
557 String s
= pageLowerField
.getText();
560 pageLowerOption
= new Integer(s
).intValue();
561 // if (pageLowerOption > 0)
562 // printAllCheckBox.setChecked(false);
565 else if (source
== pageUpperField
)
567 String s
= pageUpperField
.getText();
570 pageUpperOption
= new Integer(s
).intValue();
571 // if (pageUpperOption > 0)
572 // printAllCheckBox.setChecked(false);
575 else if (source
== orientationBox
)
577 JComboBox cb
= (JComboBox
)source
;
578 selectedOrientation
= cb
.getSelectedIndex();
579 orientationOption
= orientationValues
[selectedOrientation
];
581 else if (source
== jobSheetsBox
)
583 JComboBox cb
= (JComboBox
)source
;
584 selectedJobSheets
= cb
.getSelectedIndex();
585 jobSheetsOption
= jobSheetsNames
[selectedJobSheets
];
587 else if (source
== numCopiesField
)
589 String s
= numCopiesField
.getText();
592 numCopiesOption
= new Integer(s
).intValue();
595 else if (source
== fileTextField
)
597 String s
= fileTextField
.getText();
603 else if (source
== printButton
)
605 if (fileName
.length() > 1)
607 job
= printFile( fileName
);
611 fileTextField
.setText("");
612 JOptionPane
.showMessageDialog(mainPanel
,
613 "Job " + printer
.getPrinterName() + "-" +
614 new Integer(job
.job_id
).toString() +
625 public class MyTextListener
implements FocusListener
628 public void focusGained(FocusEvent e
)
632 public void focusLost(FocusEvent e
)
634 JTextField txtField
= (JTextField
)e
.getSource();
635 if (txtField
== numCopiesField
)
637 String s
= numCopiesField
.getText();
640 numCopiesOption
= new Integer(s
).intValue();
643 else if (txtField
== pageLowerField
)
645 String s
= pageLowerField
.getText();
648 pageLowerOption
= new Integer(s
).intValue();
651 else if (txtField
== pageUpperField
)
653 String s
= pageUpperField
.getText();
656 pageUpperOption
= new Integer(s
).intValue();