Source for javax.print.ServiceUI

   1: /* ServiceUI.java -- 
   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 javax.print;
  40: 
  41: import gnu.javax.print.PrinterDialog;
  42: 
  43: import java.awt.GraphicsConfiguration;
  44: import java.awt.GraphicsEnvironment;
  45: import java.awt.HeadlessException;
  46: import java.util.Arrays;
  47: 
  48: import javax.print.attribute.PrintRequestAttributeSet;
  49: 
  50: /**
  51:  * <code>ServiceUI</code> provides a method to create a graphical 
  52:  * print dialog.
  53:  * <p>
  54:  * The graphical print dialog enables the user to browse the available
  55:  * print services on the system. It provides user interfaces to interact 
  56:  * with the most common printing attributes likes specifying the number of 
  57:  * copies to print or the page ranges.
  58:  * </p><p>
  59:  * The initial appearance of the print dialog as shown to the user may be 
  60:  * specified by providing the default selected print service as well as 
  61:  * initial values for the printing attributes in the user interface.
  62:  * </p>
  63:  * 
  64:  * @author Wolfgang Baer (WBaer@gmx.de)
  65:  */
  66: public class ServiceUI
  67: {
  68: 
  69:   /**
  70:    * Default constructor.
  71:    */
  72:   public ServiceUI()
  73:   {
  74:     // nothing to do here - only one static method
  75:   }
  76:   
  77:   /**
  78:    * Creates a modal graphical printing dialog at the specified location on 
  79:    * the screen.
  80:    * <p>
  81:    * The dialog will return the user selected print service and the given 
  82:    * attributes set will contain the modified printing attributes. If the 
  83:    * user cancels the printing dialog <code>null</code> will be returned and 
  84:    * the printing attributes set will be unmodified.
  85:    * </p><p>
  86:    * The values of the given attributes set (if not empty) will be displayed
  87:    * initially unless the are unsupported by the print service. If a print 
  88:    * service does not support a particular value it is substituted with the
  89:    * default value of the print service.
  90:    * </p> 
  91:    * 
  92:    * @param gc the screen to use. <code>null</code> is default screen.
  93:    * @param x the coordinate of the upper left edge of the dialog in screen 
  94:    *   coordinates (not relative to the parent frame).
  95:    * @param y the coordinate of the upper left edge of the dialog in screen 
  96:    *   coordinates (not relative to the parent frame).
  97:    * @param services the print services to browse (not null).
  98:    * @param defaultService the default service. If <code>null</code>
  99:    * the first of the print services in the services array will be used.
 100:    * @param flavor the flavours to be printed.
 101:    * @param attributes the attributes requested. Will be updated 
 102:    * by selections done by the user in the dialog. 
 103:    * 
 104:    * @return The selected print service or <code>null</code> if user
 105:    * has cancelled the printer dialog.
 106:    * 
 107:    * @throws HeadlessException if GraphicsEnvironment is headless
 108:    * @throws IllegalArgumentException if services is <code>null</code> or an 
 109:    * empty array, attributes are <code>null</code> or the given default 
 110:    * <code>PrintService<code> is not part of the print service array.
 111:    */
 112:   public static PrintService printDialog(GraphicsConfiguration gc, int x, 
 113:       int y, PrintService[] services, PrintService defaultService,
 114:       DocFlavor flavor, PrintRequestAttributeSet attributes)
 115:     throws HeadlessException
 116:     {       
 117:       if (GraphicsEnvironment.isHeadless())
 118:         throw new HeadlessException("GraphicsEnvironment is headless.");
 119:       
 120:       if (services == null || services.length == 0 || attributes == null)
 121:         throw new IllegalArgumentException("Given print service array / " 
 122:                                            + "attributes may not be null");
 123:       
 124:       if (defaultService != null && 
 125:           ! Arrays.asList(services).contains(defaultService))
 126:         throw new IllegalArgumentException("defaultService is not contained " 
 127:                                            + " in the print service array");
 128:       
 129:       PrinterDialog dialog = new PrinterDialog(gc, services, defaultService, 
 130:                                                flavor, attributes);
 131:       
 132:       dialog.setLocation(x, y);
 133:       dialog.show();
 134:       
 135:       return dialog.getSelectedPrintService();
 136:     }
 137: }