/************************************************************************** /* This class demonstrates the selection of print-services with a GUI. /* /* Copyright (c) 2004 by Bernhard Bablok (mail@bablokb.de) /* /* This program is free software; you can redistribute it and/or modify /* it under the terms of the GNU General Public License as published /* by the Free Software Foundation; either version 2 of the License or /* (at your option) any later version. /* /* This program is distributed in the hope that it will be useful, but /* WITHOUT ANY WARRANTY; without even the implied warranty of /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the /* GNU General Public License for more details. /* /* You should have received a copy of the GNU General Public License /* along with this program; see the file COPYING. If not, write to /* the Free Software Foundation Inc., 59 Temple Place - Suite 330, /* Boston, MA 02111-1307 USA /**************************************************************************/ import java.io.*; import java.net.*; import javax.print.*; import javax.print.attribute.*; import javax.print.attribute.standard.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import de.lohndirekt.print.*; /** This class demonstrates the selection of print-services with a GUI. @version $Revision: 1.2 $ @author $Author: bablokb $ */ public class PrintDemoGUI extends JFrame { private static final String CUPS_URI = "ipp://localhost:631"; private String iFilename; PrintRequestAttributeSet iAttributes; ////////////////////////////////////////////////////////////////////////////// /** Register IPP-Service. */ static { try { PrintServiceLookup psl = new IppPrintServiceLookup(new URI(CUPS_URI),null,null); PrintServiceLookup.registerServiceProvider(psl); } catch (Exception e) { } } ////////////////////////////////////////////////////////////////////////////// /** Ask the user for the desired print-service. */ private PrintService getService() { PrintService[] pservices = PrintServiceLookup.lookupPrintServices(null,null); iAttributes = new HashPrintRequestAttributeSet(); iAttributes.add(MediaSizeName.ISO_A4); return ServiceUI.printDialog(null,50,50,pservices,null,null, iAttributes); } ////////////////////////////////////////////////////////////////////////////// /** Print the document. */ private void printDocument(PrintService ps) { try { if (ps != null) { DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE; DocPrintJob pj = ps.createPrintJob(); FileInputStream stream = new FileInputStream(iFilename); Doc doc = new SimpleDoc(stream,flavor,null); pj.print(doc,iAttributes); } } catch (Exception e) { } } ////////////////////////////////////////////////////////////////////////////// /** Constructor. */ public PrintDemoGUI(String filename) { iFilename = filename; } ////////////////////////////////////////////////////////////////////////////// /** Create GUI-components. */ public void createGUI() { JButton button = new JButton("Print it!"); button.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { PrintService ps = getService(); printDocument(ps); } }); JTextArea text = new JTextArea(20,60); try { FileReader reader = new FileReader(iFilename); text.read(reader,null); reader.close(); } catch (Exception e) { text.setText("Could not read file " + iFilename); } JPanel panel = new JPanel(); panel.setLayout(new BorderLayout()); panel.add(new JScrollPane(text),BorderLayout.NORTH); panel.add(button,BorderLayout.SOUTH); getContentPane().add(panel); } ////////////////////////////////////////////////////////////////////////////// public static void main(String[] args) { if (args.length != 1) usage(); PrintDemoGUI pdg = new PrintDemoGUI(args[0]); pdg.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent event) { System.exit(0); } }); pdg.createGUI(); pdg.pack(); pdg.setLocation(new Point(500,200)); pdg.setTitle("Printer-Demo"); pdg.show(); } ////////////////////////////////////////////////////////////////////////////// /** Print usage info and exit. */ private static void usage() { System.out.println("usage: java " + PrintDemoGUI.class.getName() + " filename"); System.exit(3); } }