001    /* ServiceUI.java -- 
002       Copyright (C) 2006 Free Software Foundation, Inc.
003    
004    This file is part of GNU Classpath.
005    
006    GNU Classpath is free software; you can redistribute it and/or modify
007    it under the terms of the GNU General Public License as published by
008    the Free Software Foundation; either version 2, or (at your option)
009    any later version.
010    
011    GNU Classpath is distributed in the hope that it will be useful, but
012    WITHOUT ANY WARRANTY; without even the implied warranty of
013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014    General Public License for more details.
015    
016    You should have received a copy of the GNU General Public License
017    along with GNU Classpath; see the file COPYING.  If not, write to the
018    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019    02110-1301 USA.
020    
021    Linking this library statically or dynamically with other modules is
022    making a combined work based on this library.  Thus, the terms and
023    conditions of the GNU General Public License cover the whole
024    combination.
025    
026    As a special exception, the copyright holders of this library give you
027    permission to link this library with independent modules to produce an
028    executable, regardless of the license terms of these independent
029    modules, and to copy and distribute the resulting executable under
030    terms of your choice, provided that you also meet, for each linked
031    independent module, the terms and conditions of the license of that
032    module.  An independent module is a module which is not derived from
033    or based on this library.  If you modify this library, you may extend
034    this exception to your version of the library, but you are not
035    obligated to do so.  If you do not wish to do so, delete this
036    exception statement from your version. */
037    
038    
039    package javax.print;
040    
041    import gnu.javax.print.PrinterDialog;
042    
043    import java.awt.GraphicsConfiguration;
044    import java.awt.GraphicsEnvironment;
045    import java.awt.HeadlessException;
046    import java.util.Arrays;
047    
048    import javax.print.attribute.PrintRequestAttributeSet;
049    
050    /**
051     * <code>ServiceUI</code> provides a method to create a graphical 
052     * print dialog.
053     * <p>
054     * The graphical print dialog enables the user to browse the available
055     * print services on the system. It provides user interfaces to interact 
056     * with the most common printing attributes likes specifying the number of 
057     * copies to print or the page ranges.
058     * </p><p>
059     * The initial appearance of the print dialog as shown to the user may be 
060     * specified by providing the default selected print service as well as 
061     * initial values for the printing attributes in the user interface.
062     * </p>
063     * 
064     * @author Wolfgang Baer (WBaer@gmx.de)
065     */
066    public class ServiceUI
067    {
068    
069      /**
070       * Default constructor.
071       */
072      public ServiceUI()
073      {
074        // nothing to do here - only one static method
075      }
076      
077      /**
078       * Creates a modal graphical printing dialog at the specified location on 
079       * the screen.
080       * <p>
081       * The dialog will return the user selected print service and the given 
082       * attributes set will contain the modified printing attributes. If the 
083       * user cancels the printing dialog <code>null</code> will be returned and 
084       * the printing attributes set will be unmodified.
085       * </p><p>
086       * The values of the given attributes set (if not empty) will be displayed
087       * initially unless the are unsupported by the print service. If a print 
088       * service does not support a particular value it is substituted with the
089       * default value of the print service.
090       * </p> 
091       * 
092       * @param gc the screen to use. <code>null</code> is default screen.
093       * @param x the coordinate of the upper left edge of the dialog in screen 
094       *   coordinates (not relative to the parent frame).
095       * @param y the coordinate of the upper left edge of the dialog in screen 
096       *   coordinates (not relative to the parent frame).
097       * @param services the print services to browse (not null).
098       * @param defaultService the default service. If <code>null</code>
099       * 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    }