0N/A/*
2362N/A * Copyright (c) 2000, 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage javax.print;
0N/A
0N/Aimport java.awt.GraphicsConfiguration;
0N/Aimport java.awt.GraphicsDevice;
0N/Aimport java.awt.GraphicsEnvironment;
0N/Aimport java.awt.HeadlessException;
0N/Aimport java.awt.Dialog;
0N/Aimport java.awt.Frame;
0N/Aimport java.awt.Point;
0N/Aimport java.awt.Rectangle;
0N/Aimport java.awt.Window;
0N/Aimport java.awt.KeyboardFocusManager;
0N/Aimport javax.print.attribute.Attribute;
0N/Aimport javax.print.attribute.AttributeSet;
0N/Aimport javax.print.attribute.PrintRequestAttributeSet;
0N/Aimport javax.print.attribute.standard.Destination;
0N/Aimport javax.print.attribute.standard.Fidelity;
0N/A
0N/Aimport sun.print.ServiceDialog;
0N/Aimport sun.print.SunAlternateMedia;
0N/A
0N/A/** This class is a collection of UI convenience methods which provide a
0N/A * graphical user dialog for browsing print services looked up through the Java
0N/A * Print Service API.
0N/A * <p>
0N/A * The dialogs follow a standard pattern of acting as a continue/cancel option
0N/A *for a user as well as allowing the user to select the print service to use
0N/A *and specify choices such as paper size and number of copies.
0N/A * <p>
0N/A * <p>
0N/A * The dialogs are designed to work with pluggable print services though the
0N/A * public APIs of those print services.
0N/A * <p>
0N/A * If a print service provides any vendor extensions these may be made
0N/A * accessible to the user through a vendor supplied tab panel Component.
0N/A * Such a vendor extension is encouraged to use Swing! and to support its
0N/A * accessibility APIs.
0N/A * The vendor extensions should return the settings as part of the
0N/A * AttributeSet.
0N/A * Applications which want to preserve the user settings should use those
0N/A * settings to specify the print job.
0N/A * Note that this class is not referenced by any other part of the Java
0N/A * Print Service and may not be included in profiles which cannot depend
0N/A * on the presence of the AWT packages.
0N/A */
0N/A
0N/Apublic class ServiceUI {
0N/A
0N/A
0N/A /**
0N/A * Presents a dialog to the user for selecting a print service (printer).
0N/A * It is displayed at the location specified by the application and
0N/A * is modal.
0N/A * If the specification is invalid or would make the dialog not visible it
0N/A * will be displayed at a location determined by the implementation.
0N/A * The dialog blocks its calling thread and is application modal.
0N/A * <p>
0N/A * The dialog may include a tab panel with custom UI lazily obtained from
0N/A * the PrintService's ServiceUIFactory when the PrintService is browsed.
0N/A * The dialog will attempt to locate a MAIN_UIROLE first as a JComponent,
0N/A * then as a Panel. If there is no ServiceUIFactory or no matching role
0N/A * the custom tab will be empty or not visible.
0N/A * <p>
0N/A * The dialog returns the print service selected by the user if the user
0N/A * OK's the dialog and null if the user cancels the dialog.
0N/A * <p>
0N/A * An application must pass in an array of print services to browse.
0N/A * The array must be non-null and non-empty.
0N/A * Typically an application will pass in only PrintServices capable
0N/A * of printing a particular document flavor.
0N/A * <p>
0N/A * An application may pass in a PrintService to be initially displayed.
0N/A * A non-null parameter must be included in the array of browsable
0N/A * services.
0N/A * If this parameter is null a service is chosen by the implementation.
0N/A * <p>
0N/A * An application may optionally pass in the flavor to be printed.
0N/A * If this is non-null choices presented to the user can be better
0N/A * validated against those supported by the services.
0N/A * An application must pass in a PrintRequestAttributeSet for returning
0N/A * user choices.
0N/A * On calling the PrintRequestAttributeSet may be empty, or may contain
0N/A * application-specified values.
0N/A * <p>
0N/A * These are used to set the initial settings for the initially
0N/A * displayed print service. Values which are not supported by the print
0N/A * service are ignored. As the user browses print services, attributes
0N/A * and values are copied to the new display. If a user browses a
0N/A * print service which does not support a particular attribute-value, the
0N/A * default for that service is used as the new value to be copied.
0N/A * <p>
0N/A * If the user cancels the dialog, the returned attributes will not reflect
0N/A * any changes made by the user.
0N/A *
0N/A * A typical basic usage of this method may be :
0N/A * <pre>
0N/A * PrintService[] services = PrintServiceLookup.lookupPrintServices(
0N/A * DocFlavor.INPUT_STREAM.JPEG, null);
0N/A * PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
0N/A * if (services.length > 0) {
0N/A * PrintService service = ServiceUI.printDialog(null, 50, 50,
0N/A * services, services[0],
0N/A * null,
0N/A * attributes);
0N/A * if (service != null) {
0N/A * ... print ...
0N/A * }
0N/A * }
0N/A * </pre>
0N/A * <p>
0N/A
0N/A * @param gc used to select screen. null means primary or default screen.
0N/A * @param x location of dialog including border in screen coordinates
0N/A * @param y location of dialog including border in screen coordinates
0N/A * @param services to be browsable, must be non-null.
0N/A * @param defaultService - initial PrintService to display.
0N/A * @param flavor - the flavor to be printed, or null.
0N/A * @param attributes on input is the initial application supplied
0N/A * preferences. This cannot be null but may be empty.
0N/A * On output the attributes reflect changes made by the user.
0N/A * @return print service selected by the user, or null if the user
0N/A * cancelled the dialog.
0N/A * @throws HeadlessException if GraphicsEnvironment.isHeadless()
0N/A * returns true.
0N/A * @throws IllegalArgumentException if services is null or empty,
0N/A * or attributes is null, or the initial PrintService is not in the
0N/A * list of browsable services.
0N/A */
0N/A public static PrintService printDialog(GraphicsConfiguration gc,
0N/A int x, int y,
0N/A PrintService[] services,
0N/A PrintService defaultService,
0N/A DocFlavor flavor,
0N/A PrintRequestAttributeSet attributes)
0N/A throws HeadlessException
0N/A {
0N/A int defaultIndex = -1;
0N/A
0N/A if (GraphicsEnvironment.isHeadless()) {
0N/A throw new HeadlessException();
0N/A } else if ((services == null) || (services.length == 0)) {
0N/A throw new IllegalArgumentException("services must be non-null " +
0N/A "and non-empty");
0N/A } else if (attributes == null) {
0N/A throw new IllegalArgumentException("attributes must be non-null");
0N/A }
0N/A
0N/A if (defaultService != null) {
0N/A for (int i = 0; i < services.length; i++) {
0N/A if (services[i].equals(defaultService)) {
0N/A defaultIndex = i;
0N/A break;
0N/A }
0N/A }
0N/A
0N/A if (defaultIndex < 0) {
0N/A throw new IllegalArgumentException("services must contain " +
0N/A "defaultService");
0N/A }
0N/A } else {
0N/A defaultIndex = 0;
0N/A }
0N/A
0N/A // For now we set owner to null. In the future, it may be passed
0N/A // as an argument.
0N/A Window owner = null;
0N/A
0N/A Rectangle gcBounds = (gc == null) ? GraphicsEnvironment.
0N/A getLocalGraphicsEnvironment().getDefaultScreenDevice().
0N/A getDefaultConfiguration().getBounds() : gc.getBounds();
0N/A
0N/A ServiceDialog dialog;
0N/A if (owner instanceof Frame) {
0N/A dialog = new ServiceDialog(gc,
0N/A x + gcBounds.x,
0N/A y + gcBounds.y,
0N/A services, defaultIndex,
0N/A flavor, attributes,
0N/A (Frame)owner);
0N/A } else {
0N/A dialog = new ServiceDialog(gc,
0N/A x + gcBounds.x,
0N/A y + gcBounds.y,
0N/A services, defaultIndex,
0N/A flavor, attributes,
0N/A (Dialog)owner);
0N/A }
0N/A Rectangle dlgBounds = dialog.getBounds();
0N/A
0N/A // get union of all GC bounds
0N/A GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
0N/A GraphicsDevice[] gs = ge.getScreenDevices();
0N/A for (int j=0; j<gs.length; j++) {
0N/A gcBounds =
0N/A gcBounds.union(gs[j].getDefaultConfiguration().getBounds());
0N/A }
0N/A
0N/A // if portion of dialog is not within the gc boundary
0N/A if (!gcBounds.contains(dlgBounds)) {
0N/A // put in the center relative to parent frame/dialog
0N/A dialog.setLocationRelativeTo(owner);
0N/A }
0N/A dialog.show();
0N/A
0N/A if (dialog.getStatus() == ServiceDialog.APPROVE) {
0N/A PrintRequestAttributeSet newas = dialog.getAttributes();
0N/A Class dstCategory = Destination.class;
0N/A Class amCategory = SunAlternateMedia.class;
0N/A Class fdCategory = Fidelity.class;
0N/A
0N/A if (attributes.containsKey(dstCategory) &&
0N/A !newas.containsKey(dstCategory))
0N/A {
0N/A attributes.remove(dstCategory);
0N/A }
0N/A
0N/A if (attributes.containsKey(amCategory) &&
0N/A !newas.containsKey(amCategory))
0N/A {
0N/A attributes.remove(amCategory);
0N/A }
0N/A
0N/A attributes.addAll(newas);
0N/A
0N/A Fidelity fd = (Fidelity)attributes.get(fdCategory);
0N/A if (fd != null) {
0N/A if (fd == Fidelity.FIDELITY_TRUE) {
0N/A removeUnsupportedAttributes(dialog.getPrintService(),
0N/A flavor, attributes);
0N/A }
0N/A }
0N/A }
0N/A
0N/A return dialog.getPrintService();
0N/A }
0N/A
0N/A /**
0N/A * POSSIBLE FUTURE API: This method may be used down the road if we
0N/A * decide to allow developers to explicitly display a "page setup" dialog.
0N/A * Currently we use that functionality internally for the AWT print model.
0N/A */
0N/A /*
0N/A public static void pageDialog(GraphicsConfiguration gc,
0N/A int x, int y,
0N/A PrintService service,
0N/A DocFlavor flavor,
0N/A PrintRequestAttributeSet attributes)
0N/A throws HeadlessException
0N/A {
0N/A if (GraphicsEnvironment.isHeadless()) {
0N/A throw new HeadlessException();
0N/A } else if (service == null) {
0N/A throw new IllegalArgumentException("service must be non-null");
0N/A } else if (attributes == null) {
0N/A throw new IllegalArgumentException("attributes must be non-null");
0N/A }
0N/A
0N/A ServiceDialog dialog = new ServiceDialog(gc, x, y, service,
0N/A flavor, attributes);
0N/A dialog.show();
0N/A
0N/A if (dialog.getStatus() == ServiceDialog.APPROVE) {
0N/A PrintRequestAttributeSet newas = dialog.getAttributes();
0N/A Class amCategory = SunAlternateMedia.class;
0N/A
0N/A if (attributes.containsKey(amCategory) &&
0N/A !newas.containsKey(amCategory))
0N/A {
0N/A attributes.remove(amCategory);
0N/A }
0N/A
0N/A attributes.addAll(newas.values());
0N/A }
0N/A
0N/A dialog.getOwner().dispose();
0N/A }
0N/A */
0N/A
0N/A /**
0N/A * Removes any attributes from the given AttributeSet that are
0N/A * unsupported by the given PrintService/DocFlavor combination.
0N/A */
0N/A private static void removeUnsupportedAttributes(PrintService ps,
0N/A DocFlavor flavor,
0N/A AttributeSet aset)
0N/A {
0N/A AttributeSet asUnsupported = ps.getUnsupportedAttributes(flavor,
0N/A aset);
0N/A
0N/A if (asUnsupported != null) {
0N/A Attribute[] usAttrs = asUnsupported.toArray();
0N/A
0N/A for (int i=0; i<usAttrs.length; i++) {
0N/A Class category = usAttrs[i].getCategory();
0N/A
0N/A if (ps.isAttributeCategorySupported(category)) {
0N/A Attribute attr =
0N/A (Attribute)ps.getDefaultAttributeValue(category);
0N/A
0N/A if (attr != null) {
0N/A aset.add(attr);
0N/A } else {
0N/A aset.remove(category);
0N/A }
0N/A } else {
0N/A aset.remove(category);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A}