View Javadoc
1   /*
2    * Copyright (c) 2004 by Cosylab d.o.o.
3    *
4    * The full license specifying the redistribution, modification, usage and other
5    * rights and obligations is included with the distribution of this project in
6    * the file license.html. If the license is not included you may find a copy at
7    * http://www.cosylab.com/legal/abeans_license.htm or may write to Cosylab, d.o.o.
8    *
9    * THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND, NOT EVEN THE
10   * IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR OF THIS SOFTWARE, ASSUMES
11   * _NO_ RESPONSIBILITY FOR ANY CONSEQUENCE RESULTING FROM THE USE, MODIFICATION,
12   * OR REDISTRIBUTION OF THIS SOFTWARE.
13   */
14  package com.cosylab.vdct;
15  
16  import java.awt.Component;
17  import java.awt.GridBagConstraints;
18  import java.awt.GridBagLayout;
19  import java.awt.Insets;
20  import java.awt.event.ActionEvent;
21  import java.awt.event.ActionListener;
22  import java.awt.print.PrinterJob;
23  
24  import javax.print.PrintService;
25  import javax.swing.JButton;
26  import javax.swing.JComboBox;
27  import javax.swing.JDialog;
28  import javax.swing.JLabel;
29  import javax.swing.JPanel;
30  
31  /**
32   * <code>PrinterSelector</code> is a dialog window that displays all available PrintServices.
33   * It enables selection of a certain PrintService and its distribution to its requestor.
34   *
35   * @author <a href="mailto:jaka.bobnar@cosylab.com">Jaka Bobnar</a>
36   * @version $Id$
37   * 
38   * @since VERSION
39   */
40  public class PrinterSelector extends JDialog implements ActionListener {
41  
42      private JPanel canvas;
43      private JComboBox servicesCombo;
44      private JLabel servicesLabel;
45      private JButton okButton;
46      private JButton cancelButton;
47      private static final String OK_COMMAND = "Ok";
48      private static final String CANCEL_COMMAND = "Cancel";
49      
50      private PrintService service = null;
51      
52      private static PrinterSelector selector = new PrinterSelector();
53      
54      /**
55       * 
56       * Return the instance of the PrinterSelector.
57       * 
58       * @param parent the parent component of this dialog
59       * @return selector
60       */
61      public static PrinterSelector getPrinterSelector(Component parent) {
62          selector.setLocationRelativeTo(parent);
63          return selector;
64      }
65      
66      private PrinterSelector() {
67          this.setTitle("Select printer");
68          this.setSize(430,120);
69          this.setModal(true);
70          this.setResizable(false);
71          this.setContentPane(getCanvas());
72      }
73      
74      private JPanel getCanvas() {
75          if (canvas == null) {
76              canvas = new JPanel();
77              canvas.setLayout(new GridBagLayout());
78              
79              servicesLabel = new JLabel("Available printers: ");
80              canvas.add(servicesLabel, new GridBagConstraints(0,0,1,1,1,0,GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(5,5,5,5), 1,1));
81              servicesCombo = new JComboBox();
82              canvas.add(servicesCombo, new GridBagConstraints(1,0,3,1,0.8,0,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5,5,5,5), 1,1));
83              
84              okButton = new JButton("OK");
85              canvas.add(okButton, new GridBagConstraints(2,1,1,1,1,0,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5,5,5,5), 1,1));
86              okButton.addActionListener(this);
87              okButton.setActionCommand(OK_COMMAND);
88              cancelButton = new JButton("Cancel");
89              canvas.add(cancelButton, new GridBagConstraints(3,1,1,1,1,0,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5,5,5,5), 1,1));
90              cancelButton.addActionListener(this);
91              cancelButton.setActionCommand(CANCEL_COMMAND);
92              
93          }
94          return canvas;
95      }
96      
97      /**
98       * 
99       * Shows the PrinterSelector and return the selected PrintService if SAVE button is pressed.
100      * If the SKIP button is pressed method returns null.
101      * @param previousService previousService
102      * @return selected PrintService.
103      */
104     public PrintService getPrintService(PrintService previousService) {
105         refresh(previousService);
106         this.setVisible(true);
107         return service;
108     }
109     
110     /*
111      *  (non-Javadoc)
112      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
113      */
114     public void actionPerformed(ActionEvent e) {
115         String command = e.getActionCommand();
116         if (command.equals(CANCEL_COMMAND)) {
117             service = null;
118             this.dispose();
119         } else if (command.equals(OK_COMMAND)) {
120             service = (PrintService) servicesCombo.getSelectedItem();
121             this.dispose();
122         }
123     }
124     
125     private void refresh(PrintService previousService) {
126         service = null;
127         servicesCombo.removeAllItems();
128         PrintService[] ps = PrinterJob.lookupPrintServices();
129         for (int i = 0; i < ps.length; i++) {
130             servicesCombo.addItem(ps[i]);
131         }
132         if (previousService != null) {
133             servicesCombo.setSelectedItem(previousService);
134         }
135         
136     }
137 }