1 /* PrinterDialog.java --
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package gnu
.javax
.print
;
40 import java
.awt
.BorderLayout
;
41 import java
.awt
.Dimension
;
42 import java
.awt
.Frame
;
43 import java
.awt
.GraphicsConfiguration
;
44 import java
.awt
.GridBagConstraints
;
45 import java
.awt
.GridBagLayout
;
46 import java
.awt
.HeadlessException
;
47 import java
.awt
.Insets
;
48 import java
.awt
.event
.ActionEvent
;
49 import java
.awt
.event
.ActionListener
;
50 import java
.awt
.event
.FocusEvent
;
51 import java
.awt
.event
.FocusListener
;
52 import java
.util
.ArrayList
;
53 import java
.util
.ResourceBundle
;
55 import javax
.print
.DocFlavor
;
56 import javax
.print
.PrintService
;
57 import javax
.print
.attribute
.Attribute
;
58 import javax
.print
.attribute
.HashPrintRequestAttributeSet
;
59 import javax
.print
.attribute
.PrintRequestAttributeSet
;
60 import javax
.print
.attribute
.standard
.Chromaticity
;
61 import javax
.print
.attribute
.standard
.Copies
;
62 import javax
.print
.attribute
.standard
.Destination
;
63 import javax
.print
.attribute
.standard
.JobName
;
64 import javax
.print
.attribute
.standard
.JobPriority
;
65 import javax
.print
.attribute
.standard
.JobSheets
;
66 import javax
.print
.attribute
.standard
.Media
;
67 import javax
.print
.attribute
.standard
.MediaPrintableArea
;
68 import javax
.print
.attribute
.standard
.OrientationRequested
;
69 import javax
.print
.attribute
.standard
.PageRanges
;
70 import javax
.print
.attribute
.standard
.PrintQuality
;
71 import javax
.print
.attribute
.standard
.PrinterInfo
;
72 import javax
.print
.attribute
.standard
.PrinterIsAcceptingJobs
;
73 import javax
.print
.attribute
.standard
.PrinterMakeAndModel
;
74 import javax
.print
.attribute
.standard
.PrinterState
;
75 import javax
.print
.attribute
.standard
.RequestingUserName
;
76 import javax
.print
.attribute
.standard
.SheetCollate
;
77 import javax
.print
.attribute
.standard
.Sides
;
78 import javax
.swing
.BorderFactory
;
79 import javax
.swing
.Box
;
80 import javax
.swing
.BoxLayout
;
81 import javax
.swing
.ButtonGroup
;
82 import javax
.swing
.JButton
;
83 import javax
.swing
.JCheckBox
;
84 import javax
.swing
.JComboBox
;
85 import javax
.swing
.JDialog
;
86 import javax
.swing
.JLabel
;
87 import javax
.swing
.JPanel
;
88 import javax
.swing
.JRadioButton
;
89 import javax
.swing
.JSpinner
;
90 import javax
.swing
.JTabbedPane
;
91 import javax
.swing
.JTextField
;
92 import javax
.swing
.SpinnerNumberModel
;
93 import javax
.swing
.border
.TitledBorder
;
94 import javax
.swing
.event
.ChangeEvent
;
95 import javax
.swing
.event
.ChangeListener
;
98 * Implementation of the PrinterDialog used by
99 * {@link javax.print.ServiceUI} for visual selection
100 * of print services and its attributes.
102 * @author Wolfgang Baer (WBaer@gmx.de)
104 public final class PrinterDialog
extends JDialog
implements ActionListener
108 * The General Panel used in the printing dialog.
109 * @author Wolfgang Baer (WBaer@gmx.de)
111 final class GeneralPanel
extends JPanel
114 * Handles the copies attribute.
115 * @author Wolfgang Baer (WBaer@gmx.de)
117 final class CopiesAndSorted
extends JPanel
118 implements ChangeListener
, ActionListener
120 private JCheckBox sort
;
121 private JSpinner copies
;
122 private JLabel copies_lb
;
123 private SpinnerNumberModel copiesModel
;
127 copies_lb
= new JLabel(getLocalizedString("lb.copies"));
128 sort
= new JCheckBox(getLocalizedString("cb.sort"));
129 sort
.addActionListener(this);
131 copiesModel
= new SpinnerNumberModel(1, 1, 9999, 1);
132 copies
= new JSpinner(copiesModel
);
133 copies
.addChangeListener(this);
135 GridBagLayout layout
= new GridBagLayout();
136 GridBagConstraints c
= new GridBagConstraints();
137 c
.fill
= GridBagConstraints
.BOTH
;
138 c
.insets
= new Insets(5, 5, 5, 5);
141 setBorder(new TitledBorder(getLocalizedString("title.copies")));
143 c
.anchor
= GridBagConstraints
.WEST
;
158 // copies jspinner state
159 public void stateChanged(ChangeEvent event
)
161 int value
= ((Integer
) copies
.getValue()).intValue();
162 atts
.add(new Copies(value
));
164 if (value
> 1 && categorySupported(SheetCollate
.class))
165 sort
.setEnabled(true);
167 sort
.setEnabled(false);
170 // sorted checkbox state
171 public void actionPerformed(ActionEvent event
)
173 if (sort
.isSelected())
174 atts
.add(SheetCollate
.COLLATED
);
178 * Called to update for new selected
179 * print service. Tests if currently
180 * selected attributes are supported.
182 void updateForSelectedService()
184 if (categorySupported(Copies
.class))
186 copies
.setEnabled(true);
187 copies_lb
.setEnabled(true);
189 Copies copies
= (Copies
) attribute(Copies
.class);
191 copiesModel
.setValue(new Integer(copies
.getValue()));
193 if (((Integer
)copiesModel
.getValue()).intValue() > 1
194 && categorySupported(SheetCollate
.class))
196 sort
.setEnabled(true);
197 Attribute collate
= attribute(SheetCollate
.class);
198 if (collate
!= null && collate
.equals(SheetCollate
.COLLATED
))
199 sort
.setSelected(true);
202 sort
.setEnabled(false);
206 copies
.setEnabled(false);
207 copies_lb
.setEnabled(false);
213 * Handles the print ranges attribute.
214 * @author Wolfgang Baer (WBaer@gmx.de)
216 final class PrintRange
extends JPanel
217 implements ActionListener
, FocusListener
220 private JRadioButton all_rb
, pages_rb
;
221 private JTextField from_tf
, to_tf
;
225 to
= new JLabel(getLocalizedString("lb.to"));
226 to
.setEnabled(false);
228 all_rb
= new JRadioButton(getLocalizedString("rbt.all"));
229 all_rb
.setSelected(true);
230 all_rb
.setActionCommand("ALL");
231 all_rb
.addActionListener(this);
232 pages_rb
= new JRadioButton(getLocalizedString("rbt.pages"));
233 pages_rb
.setActionCommand("PAGES");
234 pages_rb
.setEnabled(false);
235 pages_rb
.addActionListener(this);
237 ButtonGroup group
= new ButtonGroup();
241 from_tf
= new JTextField("1", 4);
242 from_tf
.setEnabled(false);
243 from_tf
.addFocusListener(this);
244 to_tf
= new JTextField("1", 4);
245 to_tf
.setEnabled(false);
246 to_tf
.addFocusListener(this);
248 GridBagLayout layout
= new GridBagLayout();
249 GridBagConstraints c
= new GridBagConstraints();
250 c
.fill
= GridBagConstraints
.BOTH
;
253 setBorder(new TitledBorder(getLocalizedString("title.printrange")));
255 c
.insets
= new Insets(15, 5, 5, 5);
260 c
.insets
= new Insets(5, 5, 15, 5);
273 c
.insets
= new Insets(5, 5, 15, 15);
280 public void focusGained(FocusEvent event
)
285 public void focusLost(FocusEvent event
)
290 // updates the range after user changed it
291 private void updatePageRanges()
293 int lower
= Integer
.parseInt(from_tf
.getText());
294 int upper
= Integer
.parseInt(to_tf
.getText());
299 to_tf
.setText("" + lower
);
302 PageRanges range
= new PageRanges(lower
, upper
);
307 public void actionPerformed(ActionEvent e
)
309 // if ALL is selected we must use a full-range object
310 if (e
.getActionCommand().equals("ALL"))
312 from_tf
.setEnabled(false);
313 to
.setEnabled(false);
314 to_tf
.setEnabled(false);
316 atts
.add(new PageRanges(1, Integer
.MAX_VALUE
));
320 from_tf
.setEnabled(true);
322 to_tf
.setEnabled(true);
323 all_rb
.setSelected(false);
328 * Called to update for new selected
329 * print service. Tests if currently
330 * selected attributes are supported.
332 void updateForSelectedService()
334 if (categorySupported(PageRanges
.class))
336 pages_rb
.setEnabled(true);
337 PageRanges range
= (PageRanges
) attribute(PageRanges
.class);
340 from_tf
.setEnabled(true);
342 to_tf
.setEnabled(true);
343 all_rb
.setSelected(false);
344 pages_rb
.setSelected(true);
346 int[][] members
= range
.getMembers();
347 // Although passed in attributes may contain more than one
348 // range we only take the first one
349 from_tf
.setText("" + members
[0][0]);
350 to_tf
.setText("" + members
[0][1]);
355 from_tf
.setEnabled(false);
356 to
.setEnabled(false);
357 to_tf
.setEnabled(false);
358 all_rb
.setSelected(true);
364 * Handles the selection of the print services
365 * and its location and description attributes.
366 * @author Wolfgang Baer (WBaer@gmx.de)
368 final class PrintServices
extends JPanel
369 implements ActionListener
371 private JLabel name
, status
, typ
, info
;
372 private JLabel statusValue
, typValue
, infoValue
;
373 private JButton attributes
;
374 private JComboBox services_cob
;
375 private JCheckBox fileRedirection_cb
;
379 name
= new JLabel(getLocalizedString("lb.name"));
380 status
= new JLabel(getLocalizedString("lb.status"));
381 typ
= new JLabel(getLocalizedString("lb.typ"));
382 info
= new JLabel(getLocalizedString("lb.info"));
383 typValue
= new JLabel();
384 infoValue
= new JLabel();
385 statusValue
= new JLabel();
387 attributes
= new JButton(getLocalizedString("bt.attributes"));
388 attributes
.setEnabled(false);
389 attributes
.setActionCommand("ATTRIBUTES");
390 attributes
.addActionListener(this);
392 services_cob
= new JComboBox(getPrintServices());
393 services_cob
.setActionCommand("SERVICE");
394 services_cob
.addActionListener(this);
396 fileRedirection_cb
= new JCheckBox(getLocalizedString("cb.output"));
397 fileRedirection_cb
.setEnabled(false);
399 GridBagLayout layout
= new GridBagLayout();
400 GridBagConstraints c
= new GridBagConstraints();
403 setBorder(new TitledBorder(getLocalizedString("title.printservice")));
405 c
.insets
= new Insets(5, 5, 5, 5);
406 c
.anchor
= GridBagConstraints
.LINE_END
;
426 add(fileRedirection_cb
, c
);
428 c
.anchor
= GridBagConstraints
.LINE_START
;
429 c
.fill
= GridBagConstraints
.HORIZONTAL
;
433 add(services_cob
, c
);
459 public void actionPerformed(ActionEvent e
)
461 if (e
.getActionCommand().equals("SERVICE"))
463 setSelectedPrintService((PrintService
) services_cob
.getSelectedItem());
466 else if (e
.getActionCommand().equals("ATTRIBUTES"))
468 // TODO LowPriority-Enhancement: As tests have shown this button
469 // is even gray and not enabled under Windows - Its a good place
470 // to provide a classpath specific browsing dialog for all
471 // attributes not in the default printing dialog.
476 * Called to update for new selected
477 * print service. Tests if currently
478 * selected attributes are supported.
480 void updateForSelectedService()
482 PrinterMakeAndModel att1
= (PrinterMakeAndModel
)
483 getSelectedPrintService().getAttribute(PrinterMakeAndModel
.class);
484 typValue
.setText(att1
== null ?
"" : att1
.getValue());
486 PrinterInfo att2
= (PrinterInfo
)
487 getSelectedPrintService().getAttribute(PrinterInfo
.class);
488 infoValue
.setText(att2
== null ?
"" : att2
.getValue());
490 PrinterIsAcceptingJobs att3
= (PrinterIsAcceptingJobs
)
491 getSelectedPrintService().getAttribute(PrinterIsAcceptingJobs
.class);
492 PrinterState att4
= (PrinterState
)
493 getSelectedPrintService().getAttribute(PrinterState
.class);
495 String status
= att4
.toString();
496 if (att3
== PrinterIsAcceptingJobs
.ACCEPTING_JOBS
)
497 status
+= " - " + getLocalizedString("lb.acceptingjobs");
498 else if (att3
== PrinterIsAcceptingJobs
.NOT_ACCEPTING_JOBS
)
499 status
+= " - " + getLocalizedString("lb.notacceptingjobs");
501 statusValue
.setText(status
);
503 if (categorySupported(Destination
.class))
505 fileRedirection_cb
.setEnabled(false);
511 private PrintServices printserv_panel
;
512 private PrintRange printrange_panel
;
513 private CopiesAndSorted copies
;
516 * Constructs the General Panel.
518 public GeneralPanel()
520 setBorder(BorderFactory
.createEmptyBorder(5, 5, 5, 5));
522 printserv_panel
= new PrintServices();
523 printrange_panel
= new PrintRange();
524 copies
= new CopiesAndSorted();
526 JPanel layout_panel
= new JPanel();
527 layout_panel
.setLayout(new BoxLayout(layout_panel
, BoxLayout
.LINE_AXIS
));
528 layout_panel
.add(printrange_panel
);
529 layout_panel
.add(Box
.createRigidArea(new Dimension(10, 0)));
530 layout_panel
.add(copies
);
532 setLayout(new BoxLayout(this, BoxLayout
.PAGE_AXIS
));
533 add(printserv_panel
);
534 add(Box
.createRigidArea(new Dimension(0, 12)));
539 * Calls update on all internal panels to adjust
540 * for a new selected print service.
544 printserv_panel
.updateForSelectedService();
545 printrange_panel
.updateForSelectedService();
546 copies
.updateForSelectedService();
551 * The Page setup Panel.
552 * @author Wolfgang Baer (WBaer@gmx.de)
554 final class PageSetupPanel
extends JPanel
557 * Handles the orientation attribute.
558 * @author Wolfgang Baer (WBaer@gmx.de)
560 final class Orientation
extends JPanel
implements ActionListener
562 private JRadioButton portrait
, landscape
, rev_portrait
, rev_landscape
;
566 portrait
= new JRadioButton(getLocalizedString("rbt.portrait"));
567 portrait
.addActionListener(this);
568 landscape
= new JRadioButton(getLocalizedString("rbt.landscape"));
569 landscape
.addActionListener(this);
570 rev_portrait
= new JRadioButton(getLocalizedString("rbt.revportrait"));
571 rev_portrait
.addActionListener(this);
572 rev_landscape
= new JRadioButton(getLocalizedString("rbt.revlandscape"));
573 rev_landscape
.addActionListener(this);
575 ButtonGroup group
= new ButtonGroup();
577 group
.add(landscape
);
578 group
.add(rev_portrait
);
579 group
.add(rev_landscape
);
581 GridBagLayout layout
= new GridBagLayout();
582 GridBagConstraints c
= new GridBagConstraints();
583 c
.fill
= GridBagConstraints
.BOTH
;
586 setBorder(new TitledBorder(getLocalizedString("title.orientation")));
588 c
.insets
= new Insets(5, 5, 5, 5);
599 add(rev_portrait
, c
);
603 add(rev_landscape
, c
);
606 // event handling orientation
607 public void actionPerformed(ActionEvent e
)
609 if (e
.getSource() == portrait
)
610 atts
.add(OrientationRequested
.PORTRAIT
);
611 else if (e
.getSource() == landscape
)
612 atts
.add(OrientationRequested
.LANDSCAPE
);
613 else if (e
.getSource() == rev_portrait
)
614 atts
.add(OrientationRequested
.REVERSE_PORTRAIT
);
616 atts
.add(OrientationRequested
.REVERSE_LANDSCAPE
);
620 * Called to update for new selected
621 * print service. Tests if currently
622 * selected attributes are supported.
624 void updateForSelectedService()
626 if (categorySupported(OrientationRequested
.class))
628 portrait
.setEnabled(true);
629 landscape
.setEnabled(true);
630 rev_landscape
.setEnabled(true);
631 rev_portrait
.setEnabled(true);
633 Attribute orientation
= attribute(OrientationRequested
.class);
634 if (orientation
!= null)
636 if (orientation
.equals(OrientationRequested
.LANDSCAPE
))
637 landscape
.setSelected(true);
638 else if (orientation
.equals(OrientationRequested
.PORTRAIT
))
639 portrait
.setSelected(true);
640 else if (orientation
.equals(OrientationRequested
.REVERSE_PORTRAIT
))
641 rev_portrait
.setSelected(true);
643 rev_landscape
.setSelected(true);
647 Object defaultValue
= defaultValue(OrientationRequested
.class);
648 if (defaultValue
.equals(OrientationRequested
.LANDSCAPE
))
649 landscape
.setSelected(true);
650 else if (defaultValue
.equals(OrientationRequested
.PORTRAIT
))
651 portrait
.setSelected(true);
652 else if (defaultValue
.equals(OrientationRequested
.REVERSE_PORTRAIT
))
653 rev_portrait
.setSelected(true);
655 rev_landscape
.setSelected(true);
660 portrait
.setEnabled(false);
661 landscape
.setEnabled(false);
662 rev_landscape
.setEnabled(false);
663 rev_portrait
.setEnabled(false);
669 * Handles the media attribute.
670 * @author Wolfgang Baer (WBaer@gmx.de)
672 final class MediaTypes
extends JPanel
implements ActionListener
674 private JLabel size_lb
, source_lb
;
675 private JComboBox size
, source
;
679 size_lb
= new JLabel(getLocalizedString("lb.size"));
680 source_lb
= new JLabel(getLocalizedString("lb.source"));
682 size
= new JComboBox();
683 size
.setEditable(false);
684 size
.addActionListener(this);
685 source
= new JComboBox();
686 source
.setEditable(false);
687 size
.addActionListener(this);
689 GridBagLayout layout
= new GridBagLayout();
690 GridBagConstraints c
= new GridBagConstraints();
693 setBorder(new TitledBorder(getLocalizedString("title.medias")));
695 c
.insets
= new Insets(5, 5, 5, 5);
696 c
.anchor
= GridBagConstraints
.LINE_END
;
705 c
.anchor
= GridBagConstraints
.LINE_START
;
706 c
.fill
= GridBagConstraints
.HORIZONTAL
;
718 public void actionPerformed(ActionEvent event
)
720 if (event
.getSource() == size
)
722 Object obj
= size
.getSelectedItem();
723 if (obj
instanceof Media
)
724 atts
.add((Media
) obj
);
727 // we ignore source events currently
728 // as only the automatic selection is used.
732 * Called to update for new selected
733 * print service. Tests if currently
734 * selected attributes are supported.
736 void updateForSelectedService()
738 if (categorySupported(Media
.class))
740 Media
[] medias
= (Media
[]) getSelectedPrintService()
741 .getSupportedAttributeValues(Media
.class, flavor
, null);
743 size
.removeAllItems();
744 if (medias
.length
== 0)
745 size
.addItem(getLocalizedString("lb.automatically"));
747 for (int i
=0; i
< medias
.length
; i
++)
748 size
.addItem(medias
[i
]);
750 Media media
= (Media
) attribute(Media
.class);
752 size
.setSelectedItem(media
);
754 // this is currently ignored
755 source
.removeAllItems();
756 source
.addItem(getLocalizedString("lb.automatically"));
760 size
.removeAllItems();
761 source
.removeAllItems();
763 size
.addItem(getLocalizedString("lb.automatically"));
764 source
.addItem(getLocalizedString("lb.automatically"));
770 * Handles the media printable area attribute.
771 * @author Wolfgang Baer (WBaer@gmx.de)
773 final class Margins
extends JPanel
implements FocusListener
775 private JLabel left
, right
, top
, bottom
;
776 private JTextField left_tf
, right_tf
, top_tf
, bottom_tf
;
780 left
= new JLabel(getLocalizedString("lb.left"));
781 right
= new JLabel(getLocalizedString("lb.right"));
782 top
= new JLabel(getLocalizedString("lb.top"));
783 bottom
= new JLabel(getLocalizedString("lb.bottom"));
785 left_tf
= new JTextField(7);
786 left_tf
.addFocusListener(this);
787 right_tf
= new JTextField(7);
788 right_tf
.addFocusListener(this);
789 top_tf
= new JTextField(7);
790 top_tf
.addFocusListener(this);
791 bottom_tf
= new JTextField(7);
792 bottom_tf
.addFocusListener(this);
794 GridBagLayout layout
= new GridBagLayout();
795 GridBagConstraints c
= new GridBagConstraints();
798 setBorder(new TitledBorder(getLocalizedString("title.margins")));
800 c
.insets
= new Insets(5, 5, 5, 5);
809 c
.insets
= new Insets(5, 5, 5, 5);
818 c
.insets
= new Insets(10, 5, 5, 5);
827 c
.insets
= new Insets(0, 5, 5, 5);
837 public void focusGained(FocusEvent event
)
842 public void focusLost(FocusEvent event
)
847 // updates the margins after user changed it
848 private void updateMargins()
850 // We currently do not support this attribute
851 // as it is not in the IPP spec and therefore not in CUPS
855 * Called to update for new selected
856 * print service. Tests if currently
857 * selected attributes are supported.
859 void updateForSelectedService()
861 if (categorySupported(MediaPrintableArea
.class))
863 left
.setEnabled(true);
864 right
.setEnabled(true);
865 top
.setEnabled(true);
866 bottom
.setEnabled(true);
867 left_tf
.setEnabled(true);
868 right_tf
.setEnabled(true);
869 top_tf
.setEnabled(true);
870 bottom_tf
.setEnabled(true);
874 left
.setEnabled(false);
875 right
.setEnabled(false);
876 top
.setEnabled(false);
877 bottom
.setEnabled(false);
878 left_tf
.setEnabled(false);
879 right_tf
.setEnabled(false);
880 top_tf
.setEnabled(false);
881 bottom_tf
.setEnabled(false);
886 private MediaTypes media_panel
;
887 private Orientation orientation_panel
;
888 private Margins margins_panel
;
891 * Constructs the page setup user interface.
893 public PageSetupPanel()
895 setBorder(BorderFactory
.createEmptyBorder(5, 5, 5, 5));
897 media_panel
= new MediaTypes();
898 orientation_panel
= new Orientation();
899 margins_panel
= new Margins();
901 JPanel layout_panel
= new JPanel();
902 layout_panel
.setLayout(new BoxLayout(layout_panel
, BoxLayout
.LINE_AXIS
));
903 layout_panel
.add(orientation_panel
);
904 layout_panel
.add(Box
.createRigidArea(new Dimension(10, 0)));
905 layout_panel
.add(margins_panel
);
907 setLayout(new BoxLayout(this, BoxLayout
.PAGE_AXIS
));
909 add(Box
.createRigidArea(new Dimension(0, 12)));
914 * Calls update on all internal panels to adjust
915 * for a new selected print service.
919 media_panel
.updateForSelectedService();
920 orientation_panel
.updateForSelectedService();
921 margins_panel
.updateForSelectedService();
926 * The Appearance panel for quality, color etc.
927 * @author Wolfgang Baer (WBaer@gmx.de)
929 final class AppearancePanel
extends JPanel
932 * Handles the print quality attribute.
933 * @author Wolfgang Baer (WBaer@gmx.de)
935 final class Quality
extends JPanel
implements ActionListener
937 private JRadioButton low
, normal
, high
;
938 private ButtonGroup group
;
942 low
= new JRadioButton(getLocalizedString("rbt.low"));
943 low
.addActionListener(this);
944 normal
= new JRadioButton(getLocalizedString("rbt.normal"));
945 normal
.addActionListener(this);
946 high
= new JRadioButton(getLocalizedString("rbt.high"));
947 high
.addActionListener(this);
949 group
= new ButtonGroup();
954 GridBagLayout layout
= new GridBagLayout();
955 GridBagConstraints c
= new GridBagConstraints();
958 setBorder(new TitledBorder(getLocalizedString("title.quality")));
960 c
.fill
= GridBagConstraints
.HORIZONTAL
;
961 c
.insets
= new Insets(5, 5, 5, 5);
975 public void actionPerformed(ActionEvent e
)
977 if (e
.getSource() == low
)
978 atts
.add(PrintQuality
.DRAFT
);
979 else if (e
.getSource() == normal
)
980 atts
.add(PrintQuality
.NORMAL
);
982 atts
.add(PrintQuality
.HIGH
);
986 * Called to update for new selected
987 * print service. Tests if currently
988 * selected attributes are supported.
990 void updateForSelectedService()
992 if (categorySupported(PrintQuality
.class))
994 low
.setEnabled(true);
995 normal
.setEnabled(true);
996 high
.setEnabled(true);
998 Object defaultValue
= defaultValue(PrintQuality
.class);
999 Attribute quality
= attribute(PrintQuality
.class);
1001 if (quality
!= null)
1003 if (quality
.equals(PrintQuality
.DRAFT
))
1004 low
.setSelected(true);
1005 else if (quality
.equals(PrintQuality
.NORMAL
))
1006 normal
.setSelected(true);
1008 high
.setSelected(true);
1012 if (defaultValue
.equals(PrintQuality
.DRAFT
))
1013 low
.setSelected(true);
1014 else if (defaultValue
.equals(PrintQuality
.NORMAL
))
1015 normal
.setSelected(true);
1017 high
.setSelected(true);
1022 low
.setEnabled(false);
1023 normal
.setEnabled(false);
1024 high
.setEnabled(false);
1030 * Handles the job attributes as requesting username, jobname etc.
1031 * @author Wolfgang Baer (WBaer@gmx.de)
1033 final class JobAttributes
extends JPanel
1034 implements ActionListener
, ChangeListener
, FocusListener
1036 private JLabel jobname
, username
, priority_lb
;
1037 private JTextField jobname_tf
, username_tf
;
1038 private JCheckBox cover
;
1039 private JSpinner priority
;
1040 private SpinnerNumberModel model
;
1044 jobname
= new JLabel(getLocalizedString("lb.jobname"));
1045 username
= new JLabel(getLocalizedString("lb.username"));
1046 priority_lb
= new JLabel(getLocalizedString("lb.priority"));
1048 cover
= new JCheckBox(getLocalizedString("cb.cover"));
1049 cover
.addActionListener(this);
1051 model
= new SpinnerNumberModel(1, 1, 100, 1);
1052 priority
= new JSpinner(model
);
1053 priority
.addChangeListener(this);
1055 jobname_tf
= new JTextField();
1056 jobname_tf
.addFocusListener(this);
1057 username_tf
= new JTextField();
1058 username_tf
.addFocusListener(this);
1060 GridBagLayout layout
= new GridBagLayout();
1061 GridBagConstraints c
= new GridBagConstraints();
1064 setBorder(new TitledBorder(getLocalizedString("title.jobattributes")));
1066 c
.insets
= new Insets(10, 5, 10, 5);
1071 c
.anchor
= GridBagConstraints
.LINE_END
;
1075 add(priority_lb
, c
);
1082 c
.anchor
= GridBagConstraints
.LINE_END
;
1091 c
.anchor
= GridBagConstraints
.CENTER
;
1092 c
.fill
= GridBagConstraints
.HORIZONTAL
;
1099 c
.insets
= new Insets(10, 5, 15, 5);
1102 add(username_tf
, c
);
1105 public void actionPerformed(ActionEvent event
)
1107 if (cover
.isSelected())
1108 atts
.add(JobSheets
.STANDARD
);
1110 atts
.add(JobSheets
.NONE
);
1113 public void stateChanged(ChangeEvent event
)
1115 int value
= ((Integer
) priority
.getValue()).intValue();
1116 atts
.add(new JobPriority(value
));
1119 public void focusGained(FocusEvent event
)
1121 updateTextfields(event
);
1124 public void focusLost(FocusEvent event
)
1126 updateTextfields(event
);
1129 private void updateTextfields(FocusEvent event
)
1131 if (event
.getSource() == jobname_tf
)
1132 atts
.add(new JobName(jobname_tf
.getText(), null));
1134 atts
.add(new RequestingUserName(username_tf
.getText(), null));
1138 * Called to update for new selected
1139 * print service. Tests if currently
1140 * selected attributes are supported.
1142 void updateForSelectedService()
1145 if (categorySupported(JobPriority
.class))
1147 JobPriority prio
= (JobPriority
) attribute(JobPriority
.class);
1148 JobPriority value
= (JobPriority
) defaultValue(JobPriority
.class);
1149 priority
.setEnabled(true);
1151 model
.setValue(new Integer(prio
.getValue()));
1153 model
.setValue(new Integer(value
.getValue()));
1156 priority
.setEnabled(false);
1158 // Requesting username
1159 if (categorySupported(RequestingUserName
.class))
1161 Attribute user
= attribute(RequestingUserName
.class);
1162 Object value
= defaultValue(RequestingUserName
.class);
1163 username
.setEnabled(true);
1165 username_tf
.setText(user
.toString());
1167 username_tf
.setText(value
.toString());
1170 username
.setEnabled(false);
1173 if (categorySupported(JobName
.class))
1175 Attribute job
= attribute(JobName
.class);
1176 Object value
= defaultValue(JobName
.class);
1177 jobname
.setEnabled(true);
1179 jobname_tf
.setText(job
.toString());
1181 jobname_tf
.setText(value
.toString());
1184 jobname
.setEnabled(false);
1187 if (categorySupported(JobSheets
.class))
1189 Attribute sheet
= attribute(JobSheets
.class);
1190 Object value
= defaultValue(JobSheets
.class);
1191 cover
.setEnabled(true);
1194 if (sheet
.equals(JobSheets
.NONE
))
1195 cover
.setSelected(false);
1197 cover
.setSelected(true);
1201 if (value
.equals(JobSheets
.NONE
))
1202 cover
.setSelected(false);
1204 cover
.setSelected(true);
1208 cover
.setEnabled(false);
1213 * Handles the sides attributes.
1214 * @author Wolfgang Baer (WBaer@gmx.de)
1216 final class SidesPanel
extends JPanel
implements ActionListener
1218 private JRadioButton oneside
, calendar
, duplex
;
1222 oneside
= new JRadioButton(getLocalizedString("rbt.onesided"));
1223 oneside
.addActionListener(this);
1224 calendar
= new JRadioButton(getLocalizedString("rbt.calendar"));
1225 calendar
.addActionListener(this);
1226 duplex
= new JRadioButton(getLocalizedString("rbt.duplex"));
1227 duplex
.addActionListener(this);
1229 ButtonGroup group
= new ButtonGroup();
1231 group
.add(calendar
);
1234 GridBagLayout layout
= new GridBagLayout();
1235 GridBagConstraints c
= new GridBagConstraints();
1236 c
.fill
= GridBagConstraints
.BOTH
;
1239 setBorder(new TitledBorder(getLocalizedString("title.sides")));
1241 c
.insets
= new Insets(5, 5, 5, 5);
1255 public void actionPerformed(ActionEvent e
)
1257 if (e
.getSource() == calendar
)
1258 atts
.add(Sides
.TWO_SIDED_SHORT_EDGE
);
1259 else if (e
.getSource() == oneside
)
1260 atts
.add(Sides
.ONE_SIDED
);
1262 atts
.add(Sides
.TWO_SIDED_LONG_EDGE
);
1266 * Called to update for new selected
1267 * print service. Tests if currently
1268 * selected attributes are supported.
1270 void updateForSelectedService()
1272 if (categorySupported(Sides
.class))
1274 oneside
.setEnabled(true);
1275 calendar
.setEnabled(true);
1276 duplex
.setEnabled(true);
1278 Object defaultValue
= defaultValue(Sides
.class);
1279 Attribute sides
= attribute(Sides
.class);
1282 if (sides
.equals(Sides
.TWO_SIDED_SHORT_EDGE
))
1283 calendar
.setSelected(true);
1284 else if (sides
.equals(Sides
.ONE_SIDED
))
1285 oneside
.setSelected(true);
1287 duplex
.setSelected(true);
1291 if (defaultValue
.equals(Sides
.TWO_SIDED_SHORT_EDGE
))
1292 calendar
.setSelected(true);
1293 else if (defaultValue
.equals(Sides
.ONE_SIDED
))
1294 oneside
.setSelected(true);
1296 duplex
.setSelected(true);
1301 oneside
.setEnabled(false);
1302 calendar
.setEnabled(false);
1303 duplex
.setEnabled(false);
1309 * Handles the chromaticity attributes.
1310 * @author Wolfgang Baer (WBaer@gmx.de)
1312 final class Color
extends JPanel
implements ActionListener
1314 private JRadioButton bw
, color
;
1318 bw
= new JRadioButton(getLocalizedString("rbt.blackwhite"));
1319 bw
.addActionListener(this);
1320 color
= new JRadioButton(getLocalizedString("rbt.color"));
1321 color
.addActionListener(this);
1323 ButtonGroup group
= new ButtonGroup();
1327 GridBagLayout layout
= new GridBagLayout();
1328 GridBagConstraints c
= new GridBagConstraints();
1331 setBorder(new TitledBorder(getLocalizedString("title.color")));
1333 c
.fill
= GridBagConstraints
.HORIZONTAL
;
1334 c
.insets
= new Insets(5, 5, 5, 5);
1344 public void actionPerformed(ActionEvent e
)
1346 if (e
.getSource() == bw
)
1347 atts
.add(Chromaticity
.MONOCHROME
);
1349 atts
.add(Chromaticity
.COLOR
);
1353 * Called to update for new selected
1354 * print service. Tests if currently
1355 * selected attributes are supported.
1357 void updateForSelectedService()
1359 if (categorySupported(Chromaticity
.class))
1361 bw
.setEnabled(true);
1362 color
.setEnabled(true);
1364 Object defaultValue
= defaultValue(Chromaticity
.class);
1365 Attribute chromaticity
= attribute(Chromaticity
.class);
1366 if (chromaticity
!= null)
1368 if (chromaticity
.equals(Chromaticity
.MONOCHROME
))
1369 bw
.setSelected(true);
1371 color
.setSelected(true);
1375 if (defaultValue
.equals(Chromaticity
.MONOCHROME
))
1376 bw
.setSelected(true);
1378 color
.setSelected(true);
1383 bw
.setEnabled(false);
1384 color
.setEnabled(false);
1389 private Quality quality_panel
;
1390 private JobAttributes jobAttr_panel
;
1391 private SidesPanel sides_panel
;
1392 private Color chromaticy_panel
;
1395 * Creates the panel for appearance attributes.
1397 public AppearancePanel()
1399 setBorder(BorderFactory
.createEmptyBorder(5, 5, 5, 5));
1401 quality_panel
= new Quality();
1402 jobAttr_panel
= new JobAttributes();
1403 sides_panel
= new SidesPanel();
1404 chromaticy_panel
= new Color();
1406 JPanel layout_panel
= new JPanel();
1407 layout_panel
.setLayout(new BoxLayout(layout_panel
, BoxLayout
.LINE_AXIS
));
1408 layout_panel
.add(chromaticy_panel
);
1409 layout_panel
.add(Box
.createRigidArea(new Dimension(10, 0)));
1410 layout_panel
.add(quality_panel
);
1412 JPanel layout2_panel
= new JPanel();
1413 layout2_panel
.setLayout(new BoxLayout(layout2_panel
, BoxLayout
.LINE_AXIS
));
1414 layout2_panel
.add(sides_panel
);
1415 layout2_panel
.add(Box
.createRigidArea(new Dimension(10, 0)));
1416 layout2_panel
.add(jobAttr_panel
);
1418 setLayout(new BoxLayout(this, BoxLayout
.PAGE_AXIS
));
1420 add(Box
.createRigidArea(new Dimension(0, 12)));
1425 * Calls update on all internal panels to adjust
1426 * for a new selected print service.
1430 quality_panel
.updateForSelectedService();
1431 jobAttr_panel
.updateForSelectedService();
1432 sides_panel
.updateForSelectedService();
1433 chromaticy_panel
.updateForSelectedService();
1437 // on main contentpane
1438 private JButton ok_bt
;
1439 private JButton cancel_bt
;
1442 private GeneralPanel general_panel
;
1443 private PageSetupPanel pagesetup_panel
;
1444 private AppearancePanel appearance_panel
;
1446 private PrintService
[] services
;
1447 private PrintService defaultService
;
1448 private PrintService selectedService
;
1449 private DocFlavor flavor
;
1450 private PrintRequestAttributeSet attributes
;
1452 private boolean onlyPageDialog
;
1453 private PrintRequestAttributeSet atts
;
1455 private final static ResourceBundle messages
;
1459 messages
= ResourceBundle
.getBundle("gnu/javax/print/PrinterDialog");
1462 // TODO LowPriority: Include checks so that if a specific value formerly
1463 // selected is no more supported by the new service changes to the default.
1466 * Class private constructs a printer dialog.
1468 * @param gc the screen to use. <code>null</code> is default screen.
1469 * @param services the print services to browse (not null).
1470 * @param defaultService the default service. If <code>null</code>
1471 * the first of the print services in the services array will be used.
1472 * @param flavor the flavours to be printed.
1473 * @param attributes the attributes requested. Will be updated
1474 * by selections done by the user in the dialog.
1475 * @param onlyPageDialog if true a page settings only dialog is constructed.
1477 * @throws HeadlessException if GraphicsEnvironment is headless
1479 private PrinterDialog(GraphicsConfiguration gc
, PrintService
[] services
,
1480 PrintService defaultService
, DocFlavor flavor
,
1481 PrintRequestAttributeSet attributes
, boolean onlyPageDialog
, String title
)
1482 throws HeadlessException
1484 super((Frame
)null, title
, true, gc
);
1486 setResizable(false);
1487 setDefaultCloseOperation(DISPOSE_ON_CLOSE
);
1489 // check and remove service not supporting the flavor
1492 ArrayList list
= new ArrayList(services
.length
);
1493 for(int i
=0; i
< services
.length
; i
++)
1494 if (services
[i
].isDocFlavorSupported(flavor
))
1495 list
.add(services
[i
]);
1497 if (defaultService
!= null
1498 && (! list
.contains(defaultService
)))
1499 defaultService
= (PrintService
) list
.get(0);
1501 PrintService
[] newServices
= new PrintService
[list
.size()];
1502 this.services
= (PrintService
[]) list
.toArray(newServices
);
1505 this.services
= services
;
1507 if (defaultService
== null)
1508 this.defaultService
= services
[0];
1510 this.defaultService
= defaultService
;
1512 this.selectedService
= this.defaultService
;
1513 this.flavor
= flavor
;
1515 // the attributes given by the user
1516 this.attributes
= attributes
;
1517 // the one to work with during browsing
1518 this.atts
= new HashPrintRequestAttributeSet(attributes
);
1520 this.onlyPageDialog
= onlyPageDialog
;
1522 initUI(onlyPageDialog
);
1528 * Constructs a page settings only dialog.
1530 * @param gc the screen to use. <code>null</code> is default screen.
1531 * @param service the print service for the page dialog.
1532 * the first of the print services in the services array will be used.
1533 * @param flavor the flavours to be printed.
1534 * @param attributes the attributes requested. Will be updated
1535 * by selections done by the user in the dialog.
1537 * @throws HeadlessException if GraphicsEnvironment is headless
1539 public PrinterDialog(GraphicsConfiguration gc
, PrintService service
,
1540 DocFlavor flavor
, PrintRequestAttributeSet attributes
)
1541 throws HeadlessException
1543 this(gc
, new PrintService
[] {service
}, service
, flavor
, attributes
,
1544 true, getLocalizedString("title.pagedialog"));
1548 * Constructs a printer dialog.
1550 * @param gc the screen to use. <code>null</code> is default screen.
1551 * @param services the print services to browse (not null).
1552 * @param defaultService the default service. If <code>null</code>
1553 * the first of the print services in the services array will be used.
1554 * @param flavor the flavours to be printed.
1555 * @param attributes the attributes requested. Will be updated
1556 * by selections done by the user in the dialog.
1558 * @throws HeadlessException if GraphicsEnvironment is headless
1560 public PrinterDialog(GraphicsConfiguration gc
, PrintService
[] services
,
1561 PrintService defaultService
, DocFlavor flavor
,
1562 PrintRequestAttributeSet attributes
)
1563 throws HeadlessException
1565 this(gc
, services
, defaultService
, flavor
, attributes
,
1566 false, getLocalizedString("title.printdialog"));
1569 // initializes the gui parts
1570 private void initUI(boolean onlyPageDialog
)
1572 JPanel buttonPane
= new JPanel();
1576 JPanel pane
= new JPanel();
1577 pane
.setLayout(new BorderLayout());
1578 pagesetup_panel
= new PageSetupPanel();
1579 pane
.add(pagesetup_panel
, BorderLayout
.CENTER
);
1581 ok_bt
= new JButton(getLocalizedString("bt.OK"));
1582 ok_bt
.addActionListener(this);
1583 cancel_bt
= new JButton(getLocalizedString("bt.cancel"));
1584 cancel_bt
.addActionListener(this);
1586 getContentPane().add(pane
, BorderLayout
.CENTER
);
1590 general_panel
= new GeneralPanel();
1591 pagesetup_panel
= new PageSetupPanel();
1592 appearance_panel
= new AppearancePanel();
1594 JTabbedPane pane
= new JTabbedPane();
1595 pane
.setBorder(BorderFactory
.createEmptyBorder(5, 5, 5, 5));
1597 ok_bt
= new JButton(getLocalizedString("bt.print"));
1598 ok_bt
.addActionListener(this);
1599 cancel_bt
= new JButton(getLocalizedString("bt.cancel"));
1600 cancel_bt
.addActionListener(this);
1602 // populate jtabbedpane
1603 pane
.addTab(getLocalizedString("tab.general"), general_panel
);
1604 pane
.addTab(getLocalizedString("tab.pagesetup"), pagesetup_panel
);
1605 pane
.addTab(getLocalizedString("tab.appearance"), appearance_panel
);
1607 // Put everything together
1608 getContentPane().add(pane
, BorderLayout
.CENTER
);
1611 buttonPane
.setLayout(new BoxLayout(buttonPane
, BoxLayout
.LINE_AXIS
));
1612 buttonPane
.setBorder(BorderFactory
.createEmptyBorder(5, 5, 5, 5));
1613 buttonPane
.add(Box
.createHorizontalGlue());
1614 buttonPane
.add(ok_bt
);
1615 buttonPane
.add(Box
.createRigidArea(new Dimension(5, 0)));
1616 buttonPane
.add(cancel_bt
);
1618 getContentPane().add(buttonPane
, BorderLayout
.PAGE_END
);
1622 * Returns the modified attributes set.
1623 * @return The attributes.
1625 public PrintRequestAttributeSet
getAttributes()
1631 * Returns the print service selected by the user.
1632 * @return The selected print service.
1634 public PrintService
getSelectedPrintService()
1636 return selectedService
;
1640 * Sets the currently selected print service.
1642 * @param service the service selected.
1644 protected void setSelectedPrintService(PrintService service
)
1646 selectedService
= service
;
1650 * Returns the print service array.
1651 * @return The print services.
1653 protected PrintService
[] getPrintServices()
1659 * Calls update on all panels to adjust
1660 * for a new selected print service.
1664 pagesetup_panel
.update();
1666 if (! onlyPageDialog
)
1668 general_panel
.update();
1669 appearance_panel
.update();
1673 boolean categorySupported(Class category
)
1675 return getSelectedPrintService().
1676 isAttributeCategorySupported(category
);
1679 Object
defaultValue(Class category
)
1681 return getSelectedPrintService().
1682 getDefaultAttributeValue(category
);
1685 Attribute
attribute(Class category
)
1687 return atts
.get(category
);
1691 * Action handler for Print/Cancel buttons.
1692 * If cancel is pressed we reset the attributes
1693 * and the selected service.
1695 * @param e the ActionEvent
1697 public void actionPerformed(ActionEvent e
)
1699 if (e
.getSource() == ok_bt
)
1702 attributes
.addAll(atts
);
1708 selectedService
= null;
1714 * Retrieves localized messages from the resource bundle.
1716 * @param key the key
1717 * @return The localized value for the key.
1719 static final String
getLocalizedString(String key
) {
1720 return messages
.getString(key
);