1 /* Demo.java -- Simple Java Print Service Demo
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. */
39 package gnu.classpath.examples.print;
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;
48 import java.io.FileInputStream;
49 import java.io.FileNotFoundException;
50 import java.util.HashSet;
51 import java.util.Iterator;
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;
76 * A simple demo showing the use of the Java Print Service API.
77 * @author Wolfgang Baer (WBaer@gmx.de)
79 public class Demo extends JFrame
80 implements ActionListener
82 // The discovered print services
83 private static PrintService[] services;
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;
94 // variables for the PrintServicePanel demo
95 private JComboBox serviceBox;
96 private JList docFormat;
97 private JList attCategories;
101 // lookup all services without any constraints
102 services = PrintServiceLookup.lookupPrintServices(null, null);
106 * Constructs the Print Demo
107 * @param title - the demo title.
109 public Demo(String title)
112 JPanel content = new JPanel(new BorderLayout());
114 JTabbedPane tabbed = new JTabbedPane();
115 tabbed.addTab("Discover print services", createPrintServicePanel());
116 tabbed.addTab("Print a file", createPrintPanel());
118 JPanel closePanel = new JPanel();
119 JButton closeButton = new JButton("Close");
120 closeButton.setActionCommand("CLOSE");
121 closeButton.addActionListener(this);
122 closePanel.add(closeButton);
124 content.add(tabbed, BorderLayout.CENTER);
125 content.add(closePanel, BorderLayout.SOUTH);
126 getContentPane().add(content);
129 private JPanel createPrintServicePanel()
131 JPanel panel = new JPanel(new GridBagLayout());
132 GridBagConstraints c = new GridBagConstraints();
134 c.insets = new Insets(5,5,5,5);
137 c.anchor = GridBagConstraints.WEST;
138 c.fill = GridBagConstraints.HORIZONTAL;
139 JLabel serviceBoxLb = new JLabel("Available print services: ");
140 panel.add(serviceBoxLb, c);
144 serviceBox = new JComboBox(services);
145 serviceBox.setActionCommand("SERVICE");
146 serviceBox.addActionListener(this);
147 panel.add(serviceBox, c);
151 JLabel docFormatLb = new JLabel("Supported DocFormat: ");
152 panel.add(docFormatLb, c);
156 docFormat = new JList(services[0].getSupportedDocFlavors());
157 docFormat.setVisibleRowCount(3);
158 JScrollPane scrollPane = new JScrollPane(docFormat);
159 panel.add(scrollPane, c);
163 JLabel categoriesLb = new JLabel("Supported Attribute categories: ");
164 panel.add(categoriesLb, c);
168 attCategories = new JList(services[0].getSupportedAttributeCategories());
169 attCategories.setVisibleRowCount(3);
170 JScrollPane scrollPane2 = new JScrollPane(attCategories);
171 panel.add(scrollPane2, c);
176 private JPanel createPrintPanel()
178 JPanel panel = new JPanel(new GridBagLayout());
179 GridBagConstraints c = new GridBagConstraints();
181 c.insets = new Insets(5,5,5,5);
185 JButton serviceBtn = new JButton("Show print dialog ...");
186 serviceBtn.addActionListener(this);
187 panel.add(serviceBtn, c);
192 c.anchor = GridBagConstraints.WEST;
193 c.fill = GridBagConstraints.HORIZONTAL;
194 JLabel selectedLb = new JLabel("Selected print service: ");
195 panel.add(selectedLb, c);
199 dialogSelectedService_Tf = new JTextField(25);
200 panel.add(dialogSelectedService_Tf, c);
204 JLabel selectedAttsLb = new JLabel("Selected Attributes: ");
205 panel.add(selectedAttsLb, c);
210 c.fill = GridBagConstraints.BOTH;
211 dialogSelectedServiceAtts = new JList();
212 dialogSelectedServiceAtts.setVisibleRowCount(3);
213 JScrollPane scrollPane = new JScrollPane(dialogSelectedServiceAtts);
214 panel.add(scrollPane, c);
218 c.fill = GridBagConstraints.HORIZONTAL;
219 JLabel fileLb = new JLabel("File to print: ");
220 panel.add(fileLb, c);
224 selectedFileTf = new JTextField(25);
225 panel.add(selectedFileTf, c);
229 c.fill = GridBagConstraints.NONE;
230 JButton fileBt = new JButton("Choose file");
231 fileBt.addActionListener(this);
232 panel.add(fileBt, c);
236 c.fill = GridBagConstraints.HORIZONTAL;
237 JLabel docFormatLb = new JLabel("Document format of file: ");
238 panel.add(docFormatLb, c);
242 dialogSelectedServicedocFormat = new JComboBox();
243 panel.add(dialogSelectedServicedocFormat, c);
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);
259 * Simple action control - only one listener
262 public void actionPerformed(ActionEvent event)
264 if (event.getActionCommand().equals("CLOSE"))
268 else if (event.getActionCommand().equals("Choose file"))
270 JFileChooser chooser = new JFileChooser();
271 chooser.showOpenDialog(this);
273 selectedFile = chooser.getSelectedFile();
275 if (selectedFile != null)
276 selectedFileTf.setText(selectedFile.getName());
278 selectedFileTf.setText("None selected");
280 else if (event.getActionCommand().equals("Show print dialog ..."))
282 atts = new HashPrintRequestAttributeSet();
283 dialogSelectedService = ServiceUI.printDialog(null, 50, 50, services,
286 if (dialogSelectedService != null)
288 dialogSelectedService_Tf.setText(dialogSelectedService.getName());
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
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++)
298 String charset = docflavors[i].getParameter("charset");
299 String mimetype = docflavors[i].getMediaType() + "/" + docflavors[i].getMediaSubtype();
301 mimetype += "; charset=" + charset;
305 dialogSelectedServicedocFormat.removeAllItems();
306 for (Iterator it = set.iterator(); it.hasNext(); )
307 dialogSelectedServicedocFormat.addItem(it.next());
310 dialogSelectedService_Tf.setText("None selected");
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();
317 dialogSelectedServiceAtts.setListData(attsSTr);
321 else if (event.getActionCommand().equals("PRINT"))
323 if (selectedFile != null && dialogSelectedService != null)
325 DocPrintJob job = dialogSelectedService.createPrintJob();
327 // choose correct docflavor
328 String mimetype = (String) dialogSelectedServicedocFormat.getSelectedItem();
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;
350 flavor = new DocFlavor(mimetype, "java.io.InputStream");
354 SimpleDoc doc = new SimpleDoc(new FileInputStream(selectedFile), flavor, null);
355 job.print(doc, atts);
357 catch (FileNotFoundException e)
359 JOptionPane.showMessageDialog(this, "The file was not found.");
362 catch (PrintException e)
364 JOptionPane.showMessageDialog(this, e, "PrintException", JOptionPane.ERROR_MESSAGE);
369 JOptionPane.showMessageDialog(this, "Please select a file to print using the FileChooser", "No file selected", JOptionPane.INFORMATION_MESSAGE);
371 else if (event.getActionCommand().equals("SERVICE"))
372 { // A new service was selected
373 PrintService selected = (PrintService) serviceBox.getSelectedItem();
375 DocFlavor[] flavors = selected.getSupportedDocFlavors();
376 docFormat.setListData(flavors);
377 attCategories.setListData(selected.getSupportedAttributeCategories());
383 * @param args - nothing defined.
385 public static void main(String[] args)
387 Demo app = new Demo("GNU Classpath printing demo");
389 app.setVisible(true);