2839 lines · 102.4 KB · Java
RawDownload
/* * Copyright (c) 2000, 2008, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation.  Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code 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 * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package sun.print; import java.awt.BorderLayout;import java.awt.Color;import java.awt.Component;import java.awt.Container;import java.awt.Dialog;import java.awt.FlowLayout;import java.awt.Frame;import java.awt.GraphicsConfiguration;import java.awt.GridBagLayout;import java.awt.GridBagConstraints;import java.awt.GridLayout;import java.awt.Insets;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.FocusEvent;import java.awt.event.FocusListener;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;import java.awt.event.WindowEvent;import java.awt.event.WindowAdapter;import java.io.File;import java.io.FilePermission;import java.io.IOException;import java.net.URI;import java.net.URL;import java.text.DecimalFormat;import java.util.Locale;import java.util.ResourceBundle;import java.util.Vector;import javax.print.*;import javax.print.attribute.*;import javax.print.attribute.standard.*;import javax.swing.*;import javax.swing.border.Border;import javax.swing.border.EmptyBorder;import javax.swing.border.TitledBorder;import javax.swing.event.ChangeEvent;import javax.swing.event.ChangeListener;import javax.swing.event.DocumentEvent;import javax.swing.event.DocumentListener;import javax.swing.event.PopupMenuEvent;import javax.swing.event.PopupMenuListener;import javax.swing.text.NumberFormatter;import sun.print.SunPageSelection;import java.awt.event.KeyEvent;import java.net.URISyntaxException;  /** * A class which implements a cross-platform print dialog. * * @author  Chris Campbell */public class ServiceDialog extends JDialog implements ActionListener {     /**     * Waiting print status (user response pending).     */    public final static int WAITING = 0;     /**     * Approve print status (user activated "Print" or "OK").     */    public final static int APPROVE = 1;     /**     * Cancel print status (user activated "Cancel");     */    public final static int CANCEL = 2;     private static final String strBundle = "sun.print.resources.serviceui";    private static final Insets panelInsets = new Insets(6, 6, 6, 6);    private static final Insets compInsets = new Insets(3, 6, 3, 6);     private static ResourceBundle messageRB;    private JTabbedPane tpTabs;    private JButton btnCancel, btnApprove;    private PrintService[] services;    private int defaultServiceIndex;    private PrintRequestAttributeSet asOriginal;    private HashPrintRequestAttributeSet asCurrent;    private PrintService psCurrent;    private DocFlavor docFlavor;    private int status;     private ValidatingFileChooser jfc;     private GeneralPanel pnlGeneral;    private PageSetupPanel pnlPageSetup;    private AppearancePanel pnlAppearance;     private boolean isAWT = false;      static {        initResource();    }      /**     * Constructor for the "standard" print dialog (containing all relevant     * tabs)     */    public ServiceDialog(GraphicsConfiguration gc,                         int x, int y,                         PrintService[] services,                         int defaultServiceIndex,                         DocFlavor flavor,                         PrintRequestAttributeSet attributes,                         Dialog dialog)    {        super(dialog, getMsg("dialog.printtitle"), true, gc);        initPrintDialog(x, y, services, defaultServiceIndex,                        flavor, attributes);    }       /**     * Constructor for the "standard" print dialog (containing all relevant     * tabs)     */    public ServiceDialog(GraphicsConfiguration gc,                         int x, int y,                         PrintService[] services,                         int defaultServiceIndex,                         DocFlavor flavor,                         PrintRequestAttributeSet attributes,                         Frame frame)    {        super(frame, getMsg("dialog.printtitle"), true, gc);        initPrintDialog(x, y, services, defaultServiceIndex,                        flavor, attributes);    }      /**     * Initialize print dialog.     */    void initPrintDialog(int x, int y,                         PrintService[] services,                         int defaultServiceIndex,                         DocFlavor flavor,                         PrintRequestAttributeSet attributes)    {        this.services = services;        this.defaultServiceIndex = defaultServiceIndex;        this.asOriginal = attributes;        this.asCurrent = new HashPrintRequestAttributeSet(attributes);        this.psCurrent = services[defaultServiceIndex];        this.docFlavor = flavor;        SunPageSelection pages =            (SunPageSelection)attributes.get(SunPageSelection.class);        if (pages != null) {            isAWT = true;        }         Container c = getContentPane();        c.setLayout(new BorderLayout());         tpTabs = new JTabbedPane();        tpTabs.setBorder(new EmptyBorder(5, 5, 5, 5));         String gkey = getMsg("tab.general");        int gmnemonic = getVKMnemonic("tab.general");        pnlGeneral = new GeneralPanel();        tpTabs.add(gkey, pnlGeneral);        tpTabs.setMnemonicAt(0, gmnemonic);         String pkey = getMsg("tab.pagesetup");        int pmnemonic = getVKMnemonic("tab.pagesetup");        pnlPageSetup = new PageSetupPanel();        tpTabs.add(pkey, pnlPageSetup);        tpTabs.setMnemonicAt(1, pmnemonic);         String akey = getMsg("tab.appearance");        int amnemonic = getVKMnemonic("tab.appearance");        pnlAppearance = new AppearancePanel();        tpTabs.add(akey, pnlAppearance);        tpTabs.setMnemonicAt(2, amnemonic);         c.add(tpTabs, BorderLayout.CENTER);         updatePanels();         JPanel pnlSouth = new JPanel(new FlowLayout(FlowLayout.TRAILING));        btnApprove = createExitButton("button.print", this);        pnlSouth.add(btnApprove);        getRootPane().setDefaultButton(btnApprove);        btnCancel = createExitButton("button.cancel", this);        handleEscKey(btnCancel);        pnlSouth.add(btnCancel);        c.add(pnlSouth, BorderLayout.SOUTH);         addWindowListener(new WindowAdapter() {            public void windowClosing(WindowEvent event) {                dispose(CANCEL);            }        });         getAccessibleContext().setAccessibleDescription(getMsg("dialog.printtitle"));        setResizable(false);        setLocation(x, y);        pack();    }    /**     * Constructor for the solitary "page setup" dialog     */    public ServiceDialog(GraphicsConfiguration gc,                         int x, int y,                         PrintService ps,                         DocFlavor flavor,                         PrintRequestAttributeSet attributes,                         Dialog dialog)    {        super(dialog, getMsg("dialog.pstitle"), true, gc);        initPageDialog(x, y, ps, flavor, attributes);    }     /**     * Constructor for the solitary "page setup" dialog     */    public ServiceDialog(GraphicsConfiguration gc,                         int x, int y,                         PrintService ps,                         DocFlavor flavor,                         PrintRequestAttributeSet attributes,                         Frame frame)    {        super(frame, getMsg("dialog.pstitle"), true, gc);        initPageDialog(x, y, ps, flavor, attributes);    }      /**     * Initialize "page setup" dialog     */    void initPageDialog(int x, int y,                         PrintService ps,                         DocFlavor flavor,                         PrintRequestAttributeSet attributes)    {        this.psCurrent = ps;        this.docFlavor = flavor;        this.asOriginal = attributes;        this.asCurrent = new HashPrintRequestAttributeSet(attributes);         Container c = getContentPane();        c.setLayout(new BorderLayout());         pnlPageSetup = new PageSetupPanel();        c.add(pnlPageSetup, BorderLayout.CENTER);         pnlPageSetup.updateInfo();         JPanel pnlSouth = new JPanel(new FlowLayout(FlowLayout.TRAILING));        btnApprove = createExitButton("button.ok", this);        pnlSouth.add(btnApprove);        getRootPane().setDefaultButton(btnApprove);        btnCancel = createExitButton("button.cancel", this);        handleEscKey(btnCancel);        pnlSouth.add(btnCancel);        c.add(pnlSouth, BorderLayout.SOUTH);         addWindowListener(new WindowAdapter() {            public void windowClosing(WindowEvent event) {                dispose(CANCEL);            }        });         getAccessibleContext().setAccessibleDescription(getMsg("dialog.pstitle"));        setResizable(false);        setLocation(x, y);        pack();    }     /**     * Performs Cancel when Esc key is pressed.     */    private void handleEscKey(JButton btnCancel) {        Action cancelKeyAction = new AbstractAction() {            public void actionPerformed(ActionEvent e) {                dispose(CANCEL);            }        };        KeyStroke cancelKeyStroke =            KeyStroke.getKeyStroke((char)KeyEvent.VK_ESCAPE, 0);        InputMap inputMap =            btnCancel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);        ActionMap actionMap = btnCancel.getActionMap();         if (inputMap != null && actionMap != null) {            inputMap.put(cancelKeyStroke, "cancel");            actionMap.put("cancel", cancelKeyAction);        }    }      /**     * Returns the current status of the dialog (whether the user has selected     * the "Print" or "Cancel" button)     */    public int getStatus() {        return status;    }     /**     * Returns an AttributeSet based on whether or not the user cancelled the     * dialog.  If the user selected "Print" we return their new selections,     * otherwise we return the attributes that were passed in initially.     */    public PrintRequestAttributeSet getAttributes() {        if (status == APPROVE) {            return asCurrent;        } else {            return asOriginal;        }    }     /**     * Returns a PrintService based on whether or not the user cancelled the     * dialog.  If the user selected "Print" we return the user's selection     * for the PrintService, otherwise we return null.     */    public PrintService getPrintService() {        if (status == APPROVE) {            return psCurrent;        } else {            return null;        }    }     /**     * Sets the current status flag for the dialog and disposes it (thus     * returning control of the parent frame back to the user)     */    public void dispose(int status) {        this.status = status;         super.dispose();    }     public void actionPerformed(ActionEvent e) {        Object source = e.getSource();        boolean approved = false;         if (source == btnApprove) {            approved = true;             if (pnlGeneral != null) {                if (pnlGeneral.isPrintToFileRequested()) {                    approved = showFileChooser();                } else {                    asCurrent.remove(Destination.class);                }            }        }         dispose(approved ? APPROVE : CANCEL);    }     /**     * Displays a JFileChooser that allows the user to select the destination     * for "Print To File"     */    private boolean showFileChooser() {        Class dstCategory = Destination.class;         Destination dst = (Destination)asCurrent.get(dstCategory);        if (dst == null) {            dst = (Destination)asOriginal.get(dstCategory);            if (dst == null) {                dst = (Destination)psCurrent.getDefaultAttributeValue(dstCategory);                // "dst" should not be null. The following code                // is only added to safeguard against a possible                // buggy implementation of a PrintService having a                // null default Destination.                if (dst == null) {                    try {                        dst = new Destination(new URI("file:out.prn"));                    } catch (URISyntaxException e) {                    }                }            }        }         File fileDest;        if (dst != null) {            try {                fileDest = new File(dst.getURI());            } catch (Exception e) {                // all manner of runtime exceptions possible                fileDest = new File("out.prn");            }        } else {            fileDest = new File("out.prn");        }         ValidatingFileChooser jfc = new ValidatingFileChooser();        jfc.setApproveButtonText(getMsg("button.ok"));        jfc.setDialogTitle(getMsg("dialog.printtofile"));        jfc.setSelectedFile(fileDest);         int returnVal = jfc.showDialog(this, null);        if (returnVal == JFileChooser.APPROVE_OPTION) {            fileDest = jfc.getSelectedFile();             try {                asCurrent.add(new Destination(fileDest.toURI()));            } catch (Exception e) {                asCurrent.remove(dstCategory);            }        } else {            asCurrent.remove(dstCategory);        }         return (returnVal == JFileChooser.APPROVE_OPTION);    }     /**     * Updates each of the top level panels     */    private void updatePanels() {        pnlGeneral.updateInfo();        pnlPageSetup.updateInfo();        pnlAppearance.updateInfo();    }     /**     * Initialize ResourceBundle     */    public static void initResource() {        java.security.AccessController.doPrivileged(            new java.security.PrivilegedAction() {                public Object run() {                    try {                        messageRB = ResourceBundle.getBundle(strBundle);                        return null;                    } catch (java.util.MissingResourceException e) {                        throw new Error("Fatal: Resource for ServiceUI " +                                        "is missing");                    }                }            }        );    }     /**     * Returns message string from resource     */    public static String getMsg(String key) {        try {            return messageRB.getString(key);        } catch (java.util.MissingResourceException e) {            throw new Error("Fatal: Resource for ServiceUI is broken; " +                            "there is no " + key + " key in resource");        }    }     /**     * Returns mnemonic character from resource     */    private static char getMnemonic(String key) {        String str = getMsg(key + ".mnemonic");        if ((str != null) && (str.length() > 0)) {            return str.charAt(0);        } else {            return (char)0;        }    } 

Lines 1–500 of 2839.