OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / lib / classpath / examples / gnu / classpath / examples / print / Demo.java
1 /* Demo.java -- Simple Java Print Service Demo
2    Copyright (C) 2006 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
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)
9 any later version.
10
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.
15
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
19 02110-1301 USA.
20
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
24 combination.
25
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. */
37
38
39 package gnu.classpath.examples.print;
40
41 import java.awt.BorderLayout;
42 import java.awt.GridBagConstraints;
43 import java.awt.GridBagLayout;
44 import java.awt.Insets;
45 import java.awt.event.ActionEvent;
46 import java.awt.event.ActionListener;
47 import java.io.File;
48 import java.io.FileInputStream;
49 import java.io.FileNotFoundException;
50 import java.util.HashSet;
51 import java.util.Iterator;
52
53 import javax.print.DocFlavor;
54 import javax.print.DocPrintJob;
55 import javax.print.PrintException;
56 import javax.print.PrintService;
57 import javax.print.PrintServiceLookup;
58 import javax.print.ServiceUI;
59 import javax.print.SimpleDoc;
60 import javax.print.attribute.Attribute;
61 import javax.print.attribute.HashPrintRequestAttributeSet;
62 import javax.swing.JButton;
63 import javax.swing.JComboBox;
64 import javax.swing.JFileChooser;
65 import javax.swing.JFrame;
66 import javax.swing.JLabel;
67 import javax.swing.JList;
68 import javax.swing.JOptionPane;
69 import javax.swing.JPanel;
70 import javax.swing.JScrollPane;
71 import javax.swing.JTabbedPane;
72 import javax.swing.JTextField;
73
74
75 /**
76  * A simple demo showing the use of the Java Print Service API.
77  * @author Wolfgang Baer (WBaer@gmx.de)
78  */
79 public class Demo extends JFrame 
80   implements ActionListener
81 {
82   // The discovered print services
83   private static PrintService[] services;
84   
85   // variables for the PrintPanel demo
86   private HashPrintRequestAttributeSet atts;
87   private PrintService dialogSelectedService;
88   private JTextField dialogSelectedService_Tf;
89   private JList dialogSelectedServiceAtts;
90   private JComboBox dialogSelectedServicedocFormat;
91   private JTextField selectedFileTf;
92   private File selectedFile;
93   
94   // variables for the PrintServicePanel demo
95   private JComboBox serviceBox;
96   private JList docFormat;
97   private JList attCategories;
98   
99   static
100   {
101     // lookup all services without any constraints
102     services = PrintServiceLookup.lookupPrintServices(null, null);   
103   }
104   
105   /**
106    * Constructs the Print Demo
107    * @param title - the demo title.
108    */
109   public Demo(String title) 
110   {
111     super(title);
112     JPanel content = new JPanel(new BorderLayout());
113     
114     JTabbedPane tabbed = new JTabbedPane();
115     tabbed.addTab("Discover print services", createPrintServicePanel());
116     tabbed.addTab("Print a file", createPrintPanel());
117     
118     JPanel closePanel = new JPanel();
119     JButton closeButton = new JButton("Close");
120     closeButton.setActionCommand("CLOSE");
121     closeButton.addActionListener(this);
122     closePanel.add(closeButton);
123     
124     content.add(tabbed, BorderLayout.CENTER);
125     content.add(closePanel, BorderLayout.SOUTH);
126     getContentPane().add(content);
127   }
128     
129   private JPanel createPrintServicePanel() 
130   {   
131     JPanel panel = new JPanel(new GridBagLayout());
132     GridBagConstraints c = new GridBagConstraints();
133     
134     c.insets = new Insets(5,5,5,5);
135     c.gridx = 0;
136     c.gridy = 0;   
137     c.anchor = GridBagConstraints.WEST;
138     c.fill = GridBagConstraints.HORIZONTAL;
139     JLabel serviceBoxLb = new JLabel("Available print services: ");
140     panel.add(serviceBoxLb, c); 
141     
142     c.gridx = 1;
143     c.gridy = 0;
144     serviceBox = new JComboBox(services);
145     serviceBox.setActionCommand("SERVICE");
146     serviceBox.addActionListener(this);
147     panel.add(serviceBox, c); 
148     
149     c.gridx = 0;
150     c.gridy = 1;
151     JLabel docFormatLb = new JLabel("Supported DocFormat: ");
152     panel.add(docFormatLb, c); 
153     
154     c.gridx = 1;
155     c.gridy = 1;
156     docFormat = new JList(services[0].getSupportedDocFlavors());
157     docFormat.setVisibleRowCount(3);
158     JScrollPane scrollPane = new JScrollPane(docFormat);
159     panel.add(scrollPane, c); 
160     
161     c.gridx = 0;
162     c.gridy = 2;
163     JLabel categoriesLb = new JLabel("Supported Attribute categories: ");
164     panel.add(categoriesLb, c); 
165     
166     c.gridx = 1;
167     c.gridy = 2;
168     attCategories = new JList(services[0].getSupportedAttributeCategories());
169     attCategories.setVisibleRowCount(3);
170     JScrollPane scrollPane2 = new JScrollPane(attCategories);    
171     panel.add(scrollPane2, c); 
172     
173     return panel;
174   }
175   
176   private JPanel createPrintPanel() 
177   {
178     JPanel panel = new JPanel(new GridBagLayout());    
179     GridBagConstraints c = new GridBagConstraints();
180     
181     c.insets = new Insets(5,5,5,5);
182     c.gridx = 0;
183     c.gridy = 0;
184     c.gridwidth = 2;    
185     JButton serviceBtn = new JButton("Show print dialog ...");
186     serviceBtn.addActionListener(this);
187     panel.add(serviceBtn, c); 
188     
189     c.gridx = 0;
190     c.gridy = 1;
191     c.gridwidth = 1;
192     c.anchor = GridBagConstraints.WEST;
193     c.fill = GridBagConstraints.HORIZONTAL;
194     JLabel selectedLb = new JLabel("Selected print service: ");
195     panel.add(selectedLb, c);
196     
197     c.gridx = 1;
198     c.gridy = 1;
199     dialogSelectedService_Tf = new JTextField(25);
200     panel.add(dialogSelectedService_Tf, c);
201     
202     c.gridx = 0;
203     c.gridy = 2;
204     JLabel selectedAttsLb = new JLabel("Selected Attributes: ");
205     panel.add(selectedAttsLb, c);
206     
207     c.gridx = 1;
208     c.gridy = 2;
209     c.weighty = 1.5;
210     c.fill = GridBagConstraints.BOTH;
211     dialogSelectedServiceAtts = new JList();
212     dialogSelectedServiceAtts.setVisibleRowCount(3);    
213     JScrollPane scrollPane = new JScrollPane(dialogSelectedServiceAtts);
214     panel.add(scrollPane, c);
215     
216     c.gridx = 0;
217     c.gridy = 3;
218     c.fill = GridBagConstraints.HORIZONTAL;
219     JLabel fileLb = new JLabel("File to print: ");
220     panel.add(fileLb, c);
221     
222     c.gridx = 1;
223     c.gridy = 3;
224     selectedFileTf = new JTextField(25);
225     panel.add(selectedFileTf, c);
226     
227     c.gridx = 2;
228     c.gridy = 3;
229     c.fill = GridBagConstraints.NONE;
230     JButton fileBt = new JButton("Choose file");
231     fileBt.addActionListener(this);
232     panel.add(fileBt, c);
233     
234     c.gridx = 0;
235     c.gridy = 4;
236     c.fill = GridBagConstraints.HORIZONTAL;
237     JLabel docFormatLb = new JLabel("Document format of file: ");
238     panel.add(docFormatLb, c);
239     
240     c.gridx = 1;
241     c.gridy = 4;
242     dialogSelectedServicedocFormat = new JComboBox();
243     panel.add(dialogSelectedServicedocFormat, c);
244     
245     c.gridx = 0;
246     c.gridy = 5;
247     c.gridwidth = 2;
248     c.anchor = GridBagConstraints.CENTER;
249     c.fill = GridBagConstraints.NONE;
250     JButton printBt = new JButton("Print");
251     printBt.setActionCommand("PRINT");
252     printBt.addActionListener(this);
253     panel.add(printBt, c);
254     
255     return panel;
256   }
257
258   /**
259    * Simple action control - only one listener
260    * @param event
261    */
262   public void actionPerformed(ActionEvent event)
263   {
264     if (event.getActionCommand().equals("CLOSE"))
265       {
266         System.exit(0);
267       } 
268     else if (event.getActionCommand().equals("Choose file"))
269       {
270         JFileChooser chooser = new JFileChooser();
271         chooser.showOpenDialog(this);
272         
273         selectedFile = chooser.getSelectedFile();
274         
275         if (selectedFile != null)
276           selectedFileTf.setText(selectedFile.getName());
277         else
278           selectedFileTf.setText("None selected");
279       }
280     else if (event.getActionCommand().equals("Show print dialog ..."))
281       {
282         atts = new HashPrintRequestAttributeSet();        
283         dialogSelectedService = ServiceUI.printDialog(null, 50, 50, services,
284                                                       null, null, atts);
285         
286         if (dialogSelectedService != null)
287           {
288             dialogSelectedService_Tf.setText(dialogSelectedService.getName());
289             
290             // we do not want to have the class representation in the dialog
291             // as we later always use an InputStream to open the file selected
292             
293             // use set to remove duplicates
294             DocFlavor[] docflavors = dialogSelectedService.getSupportedDocFlavors();
295             HashSet set = new HashSet();
296             for (int i=0; i < docflavors.length; i++)
297               {
298                 String charset = docflavors[i].getParameter("charset");
299                 String mimetype = docflavors[i].getMediaType() + "/" + docflavors[i].getMediaSubtype();
300                 if (charset != null)
301                   mimetype += "; charset=" + charset;
302                 set.add(mimetype);
303               }
304             
305             dialogSelectedServicedocFormat.removeAllItems();       
306             for (Iterator it = set.iterator(); it.hasNext(); )
307               dialogSelectedServicedocFormat.addItem(it.next());
308           }
309         else
310           dialogSelectedService_Tf.setText("None selected");
311
312         Attribute[] attsArray = atts.toArray();
313         String[] attsSTr = new String[attsArray.length];
314         for (int i = 0; i < attsSTr.length; i++)
315           attsSTr[i] = attsArray[i].getName() + " - " + attsArray[i].toString();
316
317         dialogSelectedServiceAtts.setListData(attsSTr);    
318         
319         validate();          
320       } 
321     else if (event.getActionCommand().equals("PRINT"))
322       {  
323         if (selectedFile != null && dialogSelectedService != null) 
324           {
325             DocPrintJob job = dialogSelectedService.createPrintJob();
326             
327             // choose correct docflavor
328             String mimetype = (String) dialogSelectedServicedocFormat.getSelectedItem();
329                         
330             DocFlavor flavor = null;
331             if (mimetype.equals(DocFlavor.INPUT_STREAM.GIF.getMimeType()))
332               flavor = DocFlavor.INPUT_STREAM.GIF;
333             else if (mimetype.equals(DocFlavor.INPUT_STREAM.AUTOSENSE.getMimeType()))
334               flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
335             else if (mimetype.equals(DocFlavor.INPUT_STREAM.JPEG.getMimeType()))
336               flavor = DocFlavor.INPUT_STREAM.JPEG;
337             else if (mimetype.equals(DocFlavor.INPUT_STREAM.PCL.getMimeType()))
338               flavor = DocFlavor.INPUT_STREAM.PCL;
339             else if (mimetype.equals(DocFlavor.INPUT_STREAM.PDF.getMimeType()))
340               flavor = DocFlavor.INPUT_STREAM.PDF;
341             else if (mimetype.equals(DocFlavor.INPUT_STREAM.PNG.getMimeType()))
342               flavor = DocFlavor.INPUT_STREAM.PNG;
343             else if (mimetype.equals(DocFlavor.INPUT_STREAM.POSTSCRIPT.getMimeType()))
344               flavor = DocFlavor.INPUT_STREAM.POSTSCRIPT;
345             else if (mimetype.equals(DocFlavor.INPUT_STREAM.TEXT_HTML_HOST.getMimeType()))
346               flavor = DocFlavor.INPUT_STREAM.TEXT_HTML_HOST;
347             else if (mimetype.equals(DocFlavor.INPUT_STREAM.TEXT_PLAIN_HOST.getMimeType()))
348               flavor = DocFlavor.INPUT_STREAM.TEXT_PLAIN_HOST;
349             else
350               flavor = new DocFlavor(mimetype, "java.io.InputStream");
351             
352             try
353               {
354                 SimpleDoc doc = new SimpleDoc(new FileInputStream(selectedFile), flavor, null);
355                 job.print(doc, atts);
356               }
357             catch (FileNotFoundException e)
358               {
359                 JOptionPane.showMessageDialog(this, "The file was not found.");
360                 e.printStackTrace();
361               }
362             catch (PrintException e)
363               {
364                 JOptionPane.showMessageDialog(this, e, "PrintException", JOptionPane.ERROR_MESSAGE);
365                 e.printStackTrace();
366               }           
367           }
368         else
369           JOptionPane.showMessageDialog(this, "Please select a file to print using the FileChooser", "No file selected", JOptionPane.INFORMATION_MESSAGE);
370       }
371     else if (event.getActionCommand().equals("SERVICE"))
372       {  // A new service was selected
373         PrintService selected = (PrintService) serviceBox.getSelectedItem();
374         
375         DocFlavor[] flavors = selected.getSupportedDocFlavors();
376         docFormat.setListData(flavors);
377         attCategories.setListData(selected.getSupportedAttributeCategories());
378       }
379   }
380
381   /**
382    * Main method.
383    * @param args - nothing defined.
384    */
385   public static void main(String[] args)
386   {
387     Demo app = new Demo("GNU Classpath printing demo");
388     app.pack();
389     app.setVisible(true);
390   }
391 }