0N/A/*
2362N/A * Copyright (c) 2000, 2008, 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 sun.print;
0N/A
0N/Aimport java.awt.BorderLayout;
0N/Aimport java.awt.Color;
0N/Aimport java.awt.Component;
0N/Aimport java.awt.Container;
0N/Aimport java.awt.Dialog;
0N/Aimport java.awt.FlowLayout;
0N/Aimport java.awt.Frame;
0N/Aimport java.awt.GraphicsConfiguration;
0N/Aimport java.awt.GridBagLayout;
0N/Aimport java.awt.GridBagConstraints;
0N/Aimport java.awt.GridLayout;
0N/Aimport java.awt.Insets;
0N/Aimport java.awt.Toolkit;
0N/Aimport java.awt.event.ActionEvent;
0N/Aimport java.awt.event.ActionListener;
0N/Aimport java.awt.event.FocusEvent;
0N/Aimport java.awt.event.FocusListener;
0N/Aimport java.awt.event.ItemEvent;
0N/Aimport java.awt.event.ItemListener;
0N/Aimport java.awt.event.WindowEvent;
0N/Aimport java.awt.event.WindowAdapter;
0N/Aimport java.io.File;
0N/Aimport java.io.FilePermission;
0N/Aimport java.io.IOException;
0N/Aimport java.net.URI;
0N/Aimport java.net.URL;
0N/Aimport java.text.DecimalFormat;
0N/Aimport java.util.Locale;
0N/Aimport java.util.ResourceBundle;
0N/Aimport java.util.Vector;
0N/Aimport javax.print.*;
0N/Aimport javax.print.attribute.*;
0N/Aimport javax.print.attribute.standard.*;
0N/Aimport javax.swing.*;
0N/Aimport javax.swing.border.Border;
0N/Aimport javax.swing.border.EmptyBorder;
0N/Aimport javax.swing.border.TitledBorder;
0N/Aimport javax.swing.event.ChangeEvent;
0N/Aimport javax.swing.event.ChangeListener;
0N/Aimport javax.swing.event.DocumentEvent;
0N/Aimport javax.swing.event.DocumentListener;
0N/Aimport javax.swing.event.PopupMenuEvent;
0N/Aimport javax.swing.event.PopupMenuListener;
0N/Aimport javax.swing.text.NumberFormatter;
0N/Aimport sun.print.SunPageSelection;
0N/Aimport java.awt.event.KeyEvent;
0N/Aimport java.net.URISyntaxException;
4986N/Aimport java.lang.reflect.Field;
0N/A
0N/A
0N/A/**
0N/A * A class which implements a cross-platform print dialog.
0N/A *
0N/A * @author Chris Campbell
0N/A */
0N/Apublic class ServiceDialog extends JDialog implements ActionListener {
0N/A
0N/A /**
0N/A * Waiting print status (user response pending).
0N/A */
0N/A public final static int WAITING = 0;
0N/A
0N/A /**
0N/A * Approve print status (user activated "Print" or "OK").
0N/A */
0N/A public final static int APPROVE = 1;
0N/A
0N/A /**
0N/A * Cancel print status (user activated "Cancel");
0N/A */
0N/A public final static int CANCEL = 2;
0N/A
0N/A private static final String strBundle = "sun.print.resources.serviceui";
0N/A private static final Insets panelInsets = new Insets(6, 6, 6, 6);
0N/A private static final Insets compInsets = new Insets(3, 6, 3, 6);
0N/A
0N/A private static ResourceBundle messageRB;
0N/A private JTabbedPane tpTabs;
0N/A private JButton btnCancel, btnApprove;
0N/A private PrintService[] services;
0N/A private int defaultServiceIndex;
0N/A private PrintRequestAttributeSet asOriginal;
0N/A private HashPrintRequestAttributeSet asCurrent;
0N/A private PrintService psCurrent;
0N/A private DocFlavor docFlavor;
0N/A private int status;
0N/A
0N/A private ValidatingFileChooser jfc;
0N/A
0N/A private GeneralPanel pnlGeneral;
0N/A private PageSetupPanel pnlPageSetup;
0N/A private AppearancePanel pnlAppearance;
0N/A
0N/A private boolean isAWT = false;
0N/A
0N/A
0N/A static {
0N/A initResource();
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Constructor for the "standard" print dialog (containing all relevant
0N/A * tabs)
0N/A */
0N/A public ServiceDialog(GraphicsConfiguration gc,
0N/A int x, int y,
0N/A PrintService[] services,
0N/A int defaultServiceIndex,
0N/A DocFlavor flavor,
0N/A PrintRequestAttributeSet attributes,
0N/A Dialog dialog)
0N/A {
0N/A super(dialog, getMsg("dialog.printtitle"), true, gc);
0N/A initPrintDialog(x, y, services, defaultServiceIndex,
0N/A flavor, attributes);
0N/A }
0N/A
0N/A
0N/A
0N/A /**
0N/A * Constructor for the "standard" print dialog (containing all relevant
0N/A * tabs)
0N/A */
0N/A public ServiceDialog(GraphicsConfiguration gc,
0N/A int x, int y,
0N/A PrintService[] services,
0N/A int defaultServiceIndex,
0N/A DocFlavor flavor,
0N/A PrintRequestAttributeSet attributes,
0N/A Frame frame)
0N/A {
0N/A super(frame, getMsg("dialog.printtitle"), true, gc);
0N/A initPrintDialog(x, y, services, defaultServiceIndex,
0N/A flavor, attributes);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Initialize print dialog.
0N/A */
0N/A void initPrintDialog(int x, int y,
0N/A PrintService[] services,
0N/A int defaultServiceIndex,
0N/A DocFlavor flavor,
0N/A PrintRequestAttributeSet attributes)
0N/A {
0N/A this.services = services;
0N/A this.defaultServiceIndex = defaultServiceIndex;
0N/A this.asOriginal = attributes;
0N/A this.asCurrent = new HashPrintRequestAttributeSet(attributes);
0N/A this.psCurrent = services[defaultServiceIndex];
0N/A this.docFlavor = flavor;
0N/A SunPageSelection pages =
0N/A (SunPageSelection)attributes.get(SunPageSelection.class);
0N/A if (pages != null) {
0N/A isAWT = true;
0N/A }
0N/A
0N/A Container c = getContentPane();
0N/A c.setLayout(new BorderLayout());
0N/A
0N/A tpTabs = new JTabbedPane();
0N/A tpTabs.setBorder(new EmptyBorder(5, 5, 5, 5));
0N/A
0N/A String gkey = getMsg("tab.general");
0N/A int gmnemonic = getVKMnemonic("tab.general");
0N/A pnlGeneral = new GeneralPanel();
0N/A tpTabs.add(gkey, pnlGeneral);
0N/A tpTabs.setMnemonicAt(0, gmnemonic);
0N/A
0N/A String pkey = getMsg("tab.pagesetup");
0N/A int pmnemonic = getVKMnemonic("tab.pagesetup");
0N/A pnlPageSetup = new PageSetupPanel();
0N/A tpTabs.add(pkey, pnlPageSetup);
0N/A tpTabs.setMnemonicAt(1, pmnemonic);
0N/A
0N/A String akey = getMsg("tab.appearance");
0N/A int amnemonic = getVKMnemonic("tab.appearance");
0N/A pnlAppearance = new AppearancePanel();
0N/A tpTabs.add(akey, pnlAppearance);
0N/A tpTabs.setMnemonicAt(2, amnemonic);
0N/A
0N/A c.add(tpTabs, BorderLayout.CENTER);
0N/A
0N/A updatePanels();
0N/A
0N/A JPanel pnlSouth = new JPanel(new FlowLayout(FlowLayout.TRAILING));
0N/A btnApprove = createExitButton("button.print", this);
0N/A pnlSouth.add(btnApprove);
0N/A getRootPane().setDefaultButton(btnApprove);
0N/A btnCancel = createExitButton("button.cancel", this);
0N/A handleEscKey(btnCancel);
0N/A pnlSouth.add(btnCancel);
0N/A c.add(pnlSouth, BorderLayout.SOUTH);
0N/A
0N/A addWindowListener(new WindowAdapter() {
0N/A public void windowClosing(WindowEvent event) {
0N/A dispose(CANCEL);
0N/A }
0N/A });
0N/A
0N/A getAccessibleContext().setAccessibleDescription(getMsg("dialog.printtitle"));
0N/A setResizable(false);
0N/A setLocation(x, y);
0N/A pack();
0N/A }
0N/A
0N/A /**
0N/A * Constructor for the solitary "page setup" dialog
0N/A */
0N/A public ServiceDialog(GraphicsConfiguration gc,
0N/A int x, int y,
0N/A PrintService ps,
0N/A DocFlavor flavor,
0N/A PrintRequestAttributeSet attributes,
0N/A Dialog dialog)
0N/A {
0N/A super(dialog, getMsg("dialog.pstitle"), true, gc);
0N/A initPageDialog(x, y, ps, flavor, attributes);
0N/A }
0N/A
0N/A /**
0N/A * Constructor for the solitary "page setup" dialog
0N/A */
0N/A public ServiceDialog(GraphicsConfiguration gc,
0N/A int x, int y,
0N/A PrintService ps,
0N/A DocFlavor flavor,
0N/A PrintRequestAttributeSet attributes,
0N/A Frame frame)
0N/A {
0N/A super(frame, getMsg("dialog.pstitle"), true, gc);
0N/A initPageDialog(x, y, ps, flavor, attributes);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Initialize "page setup" dialog
0N/A */
0N/A void initPageDialog(int x, int y,
0N/A PrintService ps,
0N/A DocFlavor flavor,
0N/A PrintRequestAttributeSet attributes)
0N/A {
0N/A this.psCurrent = ps;
0N/A this.docFlavor = flavor;
0N/A this.asOriginal = attributes;
0N/A this.asCurrent = new HashPrintRequestAttributeSet(attributes);
0N/A
0N/A Container c = getContentPane();
0N/A c.setLayout(new BorderLayout());
0N/A
0N/A pnlPageSetup = new PageSetupPanel();
0N/A c.add(pnlPageSetup, BorderLayout.CENTER);
0N/A
0N/A pnlPageSetup.updateInfo();
0N/A
0N/A JPanel pnlSouth = new JPanel(new FlowLayout(FlowLayout.TRAILING));
0N/A btnApprove = createExitButton("button.ok", this);
0N/A pnlSouth.add(btnApprove);
0N/A getRootPane().setDefaultButton(btnApprove);
0N/A btnCancel = createExitButton("button.cancel", this);
0N/A handleEscKey(btnCancel);
0N/A pnlSouth.add(btnCancel);
0N/A c.add(pnlSouth, BorderLayout.SOUTH);
0N/A
0N/A addWindowListener(new WindowAdapter() {
0N/A public void windowClosing(WindowEvent event) {
0N/A dispose(CANCEL);
0N/A }
0N/A });
0N/A
0N/A getAccessibleContext().setAccessibleDescription(getMsg("dialog.pstitle"));
0N/A setResizable(false);
0N/A setLocation(x, y);
0N/A pack();
0N/A }
0N/A
0N/A /**
0N/A * Performs Cancel when Esc key is pressed.
0N/A */
0N/A private void handleEscKey(JButton btnCancel) {
0N/A Action cancelKeyAction = new AbstractAction() {
0N/A public void actionPerformed(ActionEvent e) {
0N/A dispose(CANCEL);
0N/A }
0N/A };
0N/A KeyStroke cancelKeyStroke =
0N/A KeyStroke.getKeyStroke((char)KeyEvent.VK_ESCAPE, 0);
0N/A InputMap inputMap =
0N/A btnCancel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
0N/A ActionMap actionMap = btnCancel.getActionMap();
0N/A
0N/A if (inputMap != null && actionMap != null) {
0N/A inputMap.put(cancelKeyStroke, "cancel");
0N/A actionMap.put("cancel", cancelKeyAction);
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns the current status of the dialog (whether the user has selected
0N/A * the "Print" or "Cancel" button)
0N/A */
0N/A public int getStatus() {
0N/A return status;
0N/A }
0N/A
0N/A /**
0N/A * Returns an AttributeSet based on whether or not the user cancelled the
0N/A * dialog. If the user selected "Print" we return their new selections,
0N/A * otherwise we return the attributes that were passed in initially.
0N/A */
0N/A public PrintRequestAttributeSet getAttributes() {
0N/A if (status == APPROVE) {
0N/A return asCurrent;
0N/A } else {
0N/A return asOriginal;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns a PrintService based on whether or not the user cancelled the
0N/A * dialog. If the user selected "Print" we return the user's selection
0N/A * for the PrintService, otherwise we return null.
0N/A */
0N/A public PrintService getPrintService() {
0N/A if (status == APPROVE) {
0N/A return psCurrent;
0N/A } else {
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Sets the current status flag for the dialog and disposes it (thus
0N/A * returning control of the parent frame back to the user)
0N/A */
0N/A public void dispose(int status) {
0N/A this.status = status;
0N/A
0N/A super.dispose();
0N/A }
0N/A
0N/A public void actionPerformed(ActionEvent e) {
0N/A Object source = e.getSource();
0N/A boolean approved = false;
0N/A
0N/A if (source == btnApprove) {
0N/A approved = true;
0N/A
0N/A if (pnlGeneral != null) {
0N/A if (pnlGeneral.isPrintToFileRequested()) {
0N/A approved = showFileChooser();
0N/A } else {
0N/A asCurrent.remove(Destination.class);
0N/A }
0N/A }
0N/A }
0N/A
0N/A dispose(approved ? APPROVE : CANCEL);
0N/A }
0N/A
0N/A /**
0N/A * Displays a JFileChooser that allows the user to select the destination
0N/A * for "Print To File"
0N/A */
0N/A private boolean showFileChooser() {
0N/A Class dstCategory = Destination.class;
0N/A
0N/A Destination dst = (Destination)asCurrent.get(dstCategory);
0N/A if (dst == null) {
0N/A dst = (Destination)asOriginal.get(dstCategory);
0N/A if (dst == null) {
0N/A dst = (Destination)psCurrent.getDefaultAttributeValue(dstCategory);
0N/A // "dst" should not be null. The following code
0N/A // is only added to safeguard against a possible
0N/A // buggy implementation of a PrintService having a
0N/A // null default Destination.
0N/A if (dst == null) {
0N/A try {
0N/A dst = new Destination(new URI("file:out.prn"));
0N/A } catch (URISyntaxException e) {
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A File fileDest;
0N/A if (dst != null) {
0N/A try {
0N/A fileDest = new File(dst.getURI());
0N/A } catch (Exception e) {
0N/A // all manner of runtime exceptions possible
0N/A fileDest = new File("out.prn");
0N/A }
0N/A } else {
0N/A fileDest = new File("out.prn");
0N/A }
0N/A
0N/A ValidatingFileChooser jfc = new ValidatingFileChooser();
0N/A jfc.setApproveButtonText(getMsg("button.ok"));
0N/A jfc.setDialogTitle(getMsg("dialog.printtofile"));
5128N/A jfc.setDialogType(JFileChooser.SAVE_DIALOG);
0N/A jfc.setSelectedFile(fileDest);
0N/A
0N/A int returnVal = jfc.showDialog(this, null);
0N/A if (returnVal == JFileChooser.APPROVE_OPTION) {
0N/A fileDest = jfc.getSelectedFile();
0N/A
0N/A try {
0N/A asCurrent.add(new Destination(fileDest.toURI()));
0N/A } catch (Exception e) {
0N/A asCurrent.remove(dstCategory);
0N/A }
0N/A } else {
0N/A asCurrent.remove(dstCategory);
0N/A }
0N/A
0N/A return (returnVal == JFileChooser.APPROVE_OPTION);
0N/A }
0N/A
0N/A /**
0N/A * Updates each of the top level panels
0N/A */
0N/A private void updatePanels() {
0N/A pnlGeneral.updateInfo();
0N/A pnlPageSetup.updateInfo();
0N/A pnlAppearance.updateInfo();
0N/A }
0N/A
0N/A /**
0N/A * Initialize ResourceBundle
0N/A */
0N/A public static void initResource() {
0N/A java.security.AccessController.doPrivileged(
0N/A new java.security.PrivilegedAction() {
0N/A public Object run() {
0N/A try {
0N/A messageRB = ResourceBundle.getBundle(strBundle);
0N/A return null;
0N/A } catch (java.util.MissingResourceException e) {
0N/A throw new Error("Fatal: Resource for ServiceUI " +
0N/A "is missing");
0N/A }
0N/A }
0N/A }
0N/A );
0N/A }
0N/A
0N/A /**
0N/A * Returns message string from resource
0N/A */
0N/A public static String getMsg(String key) {
0N/A try {
4986N/A return removeMnemonics(messageRB.getString(key));
0N/A } catch (java.util.MissingResourceException e) {
0N/A throw new Error("Fatal: Resource for ServiceUI is broken; " +
0N/A "there is no " + key + " key in resource");
0N/A }
0N/A }
0N/A
4986N/A private static String removeMnemonics(String s) {
4986N/A int i = s.indexOf('&');
4986N/A int len = s.length();
4986N/A if (i < 0 || i == (len - 1)) {
4986N/A return s;
4986N/A }
4986N/A int j = s.indexOf('&', i+1);
4986N/A if (j == i+1) {
4986N/A if (j+1 == len) {
4986N/A return s.substring(0, i+1); // string ends with &&
4986N/A } else {
4986N/A return s.substring(0, i+1) + removeMnemonics(s.substring(j+1));
4986N/A }
4986N/A }
4986N/A // ok first & not double &&
4986N/A if (i == 0) {
4986N/A return removeMnemonics(s.substring(1));
4986N/A } else {
4986N/A return (s.substring(0, i) + removeMnemonics(s.substring(i+1)));
4986N/A }
4986N/A }
4986N/A
4986N/A
0N/A /**
0N/A * Returns mnemonic character from resource
0N/A */
0N/A private static char getMnemonic(String key) {
4986N/A String str = messageRB.getString(key).replace("&&", "");
4986N/A int index = str.indexOf('&');
4986N/A if (0 <= index && index < str.length() - 1) {
4986N/A char c = str.charAt(index + 1);
4986N/A return Character.toUpperCase(c);
0N/A } else {
0N/A return (char)0;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the mnemonic as a KeyEvent.VK constant from the resource.
0N/A */
4986N/A static Class _keyEventClazz = null;
0N/A private static int getVKMnemonic(String key) {
4986N/A String s = String.valueOf(getMnemonic(key));
4986N/A if ( s == null || s.length() != 1) {
4986N/A return 0;
4986N/A }
4986N/A String vkString = "VK_" + s.toUpperCase();
4986N/A
4986N/A try {
4986N/A if (_keyEventClazz == null) {
4986N/A _keyEventClazz= Class.forName("java.awt.event.KeyEvent",
4986N/A true, (ServiceDialog.class).getClassLoader());
4986N/A }
4986N/A Field field = _keyEventClazz.getDeclaredField(vkString);
4986N/A int value = field.getInt(null);
4986N/A return value;
4986N/A } catch (Exception e) {
0N/A }
0N/A return 0;
0N/A }
0N/A
0N/A /**
0N/A * Returns URL for image resource
0N/A */
0N/A private static URL getImageResource(final String key) {
0N/A URL url = (URL)java.security.AccessController.doPrivileged(
0N/A new java.security.PrivilegedAction() {
0N/A public Object run() {
0N/A URL url = ServiceDialog.class.getResource(
0N/A "resources/" + key);
0N/A return url;
0N/A }
0N/A });
0N/A
0N/A if (url == null) {
0N/A throw new Error("Fatal: Resource for ServiceUI is broken; " +
0N/A "there is no " + key + " key in resource");
0N/A }
0N/A
0N/A return url;
0N/A }
0N/A
0N/A /**
0N/A * Creates a new JButton and sets its text, mnemonic, and ActionListener
0N/A */
0N/A private static JButton createButton(String key, ActionListener al) {
0N/A JButton btn = new JButton(getMsg(key));
0N/A btn.setMnemonic(getMnemonic(key));
0N/A btn.addActionListener(al);
0N/A
0N/A return btn;
0N/A }
0N/A
0N/A /**
0N/A * Creates a new JButton and sets its text, and ActionListener
0N/A */
0N/A private static JButton createExitButton(String key, ActionListener al) {
0N/A String str = getMsg(key);
0N/A JButton btn = new JButton(str);
0N/A btn.addActionListener(al);
0N/A btn.getAccessibleContext().setAccessibleDescription(str);
0N/A return btn;
0N/A }
0N/A
0N/A /**
0N/A * Creates a new JCheckBox and sets its text, mnemonic, and ActionListener
0N/A */
0N/A private static JCheckBox createCheckBox(String key, ActionListener al) {
0N/A JCheckBox cb = new JCheckBox(getMsg(key));
0N/A cb.setMnemonic(getMnemonic(key));
0N/A cb.addActionListener(al);
0N/A
0N/A return cb;
0N/A }
0N/A
0N/A /**
0N/A * Creates a new JRadioButton and sets its text, mnemonic,
0N/A * and ActionListener
0N/A */
0N/A private static JRadioButton createRadioButton(String key,
0N/A ActionListener al)
0N/A {
0N/A JRadioButton rb = new JRadioButton(getMsg(key));
0N/A rb.setMnemonic(getMnemonic(key));
0N/A rb.addActionListener(al);
0N/A
0N/A return rb;
0N/A }
0N/A
0N/A /**
0N/A * Creates a pop-up dialog for "no print service"
0N/A */
0N/A public static void showNoPrintService(GraphicsConfiguration gc)
0N/A {
0N/A Frame dlgFrame = new Frame(gc);
0N/A JOptionPane.showMessageDialog(dlgFrame,
0N/A getMsg("dialog.noprintermsg"));
0N/A dlgFrame.dispose();
0N/A }
0N/A
0N/A /**
0N/A * Sets the constraints for the GridBagLayout and adds the Component
0N/A * to the given Container
0N/A */
0N/A private static void addToGB(Component comp, Container cont,
0N/A GridBagLayout gridbag,
0N/A GridBagConstraints constraints)
0N/A {
0N/A gridbag.setConstraints(comp, constraints);
0N/A cont.add(comp);
0N/A }
0N/A
0N/A /**
0N/A * Adds the AbstractButton to both the given ButtonGroup and Container
0N/A */
0N/A private static void addToBG(AbstractButton button, Container cont,
0N/A ButtonGroup bg)
0N/A {
0N/A bg.add(button);
0N/A cont.add(button);
0N/A }
0N/A
0N/A
0N/A
0N/A
0N/A /**
0N/A * The "General" tab. Includes the controls for PrintService,
0N/A * PageRange, and Copies/Collate.
0N/A */
0N/A private class GeneralPanel extends JPanel {
0N/A
0N/A private PrintServicePanel pnlPrintService;
0N/A private PrintRangePanel pnlPrintRange;
0N/A private CopiesPanel pnlCopies;
0N/A
0N/A public GeneralPanel() {
0N/A super();
0N/A
0N/A GridBagLayout gridbag = new GridBagLayout();
0N/A GridBagConstraints c = new GridBagConstraints();
0N/A
0N/A setLayout(gridbag);
0N/A
0N/A c.fill = GridBagConstraints.BOTH;
0N/A c.insets = panelInsets;
0N/A c.weightx = 1.0;
0N/A c.weighty = 1.0;
0N/A
0N/A c.gridwidth = GridBagConstraints.REMAINDER;
0N/A pnlPrintService = new PrintServicePanel();
0N/A addToGB(pnlPrintService, this, gridbag, c);
0N/A
0N/A c.gridwidth = GridBagConstraints.RELATIVE;
0N/A pnlPrintRange = new PrintRangePanel();
0N/A addToGB(pnlPrintRange, this, gridbag, c);
0N/A
0N/A c.gridwidth = GridBagConstraints.REMAINDER;
0N/A pnlCopies = new CopiesPanel();
0N/A addToGB(pnlCopies, this, gridbag, c);
0N/A }
0N/A
0N/A public boolean isPrintToFileRequested() {
0N/A return (pnlPrintService.isPrintToFileSelected());
0N/A }
0N/A
0N/A public void updateInfo() {
0N/A pnlPrintService.updateInfo();
0N/A pnlPrintRange.updateInfo();
0N/A pnlCopies.updateInfo();
0N/A }
0N/A }
0N/A
0N/A private class PrintServicePanel extends JPanel
0N/A implements ActionListener, ItemListener, PopupMenuListener
0N/A {
0N/A private final String strTitle = getMsg("border.printservice");
0N/A private FilePermission printToFilePermission;
0N/A private JButton btnProperties;
0N/A private JCheckBox cbPrintToFile;
0N/A private JComboBox cbName;
0N/A private JLabel lblType, lblStatus, lblInfo;
0N/A private ServiceUIFactory uiFactory;
0N/A private boolean changedService = false;
0N/A private boolean filePermission;
0N/A
0N/A public PrintServicePanel() {
0N/A super();
0N/A
0N/A uiFactory = psCurrent.getServiceUIFactory();
0N/A
0N/A GridBagLayout gridbag = new GridBagLayout();
0N/A GridBagConstraints c = new GridBagConstraints();
0N/A
0N/A setLayout(gridbag);
0N/A setBorder(BorderFactory.createTitledBorder(strTitle));
0N/A
0N/A String[] psnames = new String[services.length];
0N/A for (int i = 0; i < psnames.length; i++) {
0N/A psnames[i] = services[i].getName();
0N/A }
0N/A cbName = new JComboBox(psnames);
0N/A cbName.setSelectedIndex(defaultServiceIndex);
0N/A cbName.addItemListener(this);
0N/A cbName.addPopupMenuListener(this);
0N/A
0N/A c.fill = GridBagConstraints.BOTH;
0N/A c.insets = compInsets;
0N/A
0N/A c.weightx = 0.0;
0N/A JLabel lblName = new JLabel(getMsg("label.psname"), JLabel.TRAILING);
0N/A lblName.setDisplayedMnemonic(getMnemonic("label.psname"));
0N/A lblName.setLabelFor(cbName);
0N/A addToGB(lblName, this, gridbag, c);
0N/A c.weightx = 1.0;
0N/A c.gridwidth = GridBagConstraints.RELATIVE;
0N/A addToGB(cbName, this, gridbag, c);
0N/A c.weightx = 0.0;
0N/A c.gridwidth = GridBagConstraints.REMAINDER;
0N/A btnProperties = createButton("button.properties", this);
0N/A addToGB(btnProperties, this, gridbag, c);
0N/A
0N/A c.weighty = 1.0;
0N/A lblStatus = addLabel(getMsg("label.status"), gridbag, c);
0N/A lblStatus.setLabelFor(null);
0N/A
0N/A lblType = addLabel(getMsg("label.pstype"), gridbag, c);
0N/A lblType.setLabelFor(null);
0N/A
0N/A c.gridwidth = 1;
0N/A addToGB(new JLabel(getMsg("label.info"), JLabel.TRAILING),
0N/A this, gridbag, c);
0N/A c.gridwidth = GridBagConstraints.RELATIVE;
0N/A lblInfo = new JLabel();
0N/A lblInfo.setLabelFor(null);
0N/A
0N/A addToGB(lblInfo, this, gridbag, c);
0N/A
0N/A c.gridwidth = GridBagConstraints.REMAINDER;
0N/A cbPrintToFile = createCheckBox("checkbox.printtofile", this);
0N/A addToGB(cbPrintToFile, this, gridbag, c);
0N/A
0N/A filePermission = allowedToPrintToFile();
0N/A }
0N/A
0N/A public boolean isPrintToFileSelected() {
0N/A return cbPrintToFile.isSelected();
0N/A }
0N/A
0N/A private JLabel addLabel(String text,
0N/A GridBagLayout gridbag, GridBagConstraints c)
0N/A {
0N/A c.gridwidth = 1;
0N/A addToGB(new JLabel(text, JLabel.TRAILING), this, gridbag, c);
0N/A
0N/A c.gridwidth = GridBagConstraints.REMAINDER;
0N/A JLabel label = new JLabel();
0N/A addToGB(label, this, gridbag, c);
0N/A
0N/A return label;
0N/A }
0N/A
0N/A public void actionPerformed(ActionEvent e) {
0N/A Object source = e.getSource();
0N/A
0N/A if (source == btnProperties) {
0N/A if (uiFactory != null) {
0N/A JDialog dialog = (JDialog)uiFactory.getUI(
0N/A ServiceUIFactory.MAIN_UIROLE,
0N/A ServiceUIFactory.JDIALOG_UI);
0N/A
0N/A if (dialog != null) {
0N/A dialog.show();
0N/A } else {
0N/A // REMIND: may want to notify the user why we're
0N/A // disabling the button
0N/A btnProperties.setEnabled(false);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A public void itemStateChanged(ItemEvent e) {
0N/A if (e.getStateChange() == ItemEvent.SELECTED) {
0N/A int index = cbName.getSelectedIndex();
0N/A
0N/A if ((index >= 0) && (index < services.length)) {
0N/A if (!services[index].equals(psCurrent)) {
0N/A psCurrent = services[index];
0N/A uiFactory = psCurrent.getServiceUIFactory();
0N/A changedService = true;
0N/A
0N/A Destination dest =
0N/A (Destination)asOriginal.get(Destination.class);
0N/A // to preserve the state of Print To File
0N/A if ((dest != null || isPrintToFileSelected())
0N/A && psCurrent.isAttributeCategorySupported(
0N/A Destination.class)) {
0N/A
0N/A if (dest != null) {
0N/A asCurrent.add(dest);
0N/A } else {
0N/A dest = (Destination)psCurrent.
0N/A getDefaultAttributeValue(Destination.class);
0N/A // "dest" should not be null. The following code
0N/A // is only added to safeguard against a possible
0N/A // buggy implementation of a PrintService having a
0N/A // null default Destination.
0N/A if (dest == null) {
0N/A try {
0N/A dest =
0N/A new Destination(new URI("file:out.prn"));
0N/A } catch (URISyntaxException ue) {
0N/A }
0N/A }
0N/A
0N/A if (dest != null) {
0N/A asCurrent.add(dest);
0N/A }
0N/A }
0N/A } else {
0N/A asCurrent.remove(Destination.class);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
0N/A changedService = false;
0N/A }
0N/A
0N/A public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
0N/A if (changedService) {
0N/A changedService = false;
0N/A updatePanels();
0N/A }
0N/A }
0N/A
0N/A public void popupMenuCanceled(PopupMenuEvent e) {
0N/A }
0N/A
0N/A /**
0N/A * We disable the "Print To File" checkbox if this returns false
0N/A */
0N/A private boolean allowedToPrintToFile() {
0N/A try {
0N/A throwPrintToFile();
0N/A return true;
0N/A } catch (SecurityException e) {
0N/A return false;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Break this out as it may be useful when we allow API to
0N/A * specify printing to a file. In that case its probably right
0N/A * to throw a SecurityException if the permission is not granted.
0N/A */
0N/A private void throwPrintToFile() {
0N/A SecurityManager security = System.getSecurityManager();
0N/A if (security != null) {
0N/A if (printToFilePermission == null) {
0N/A printToFilePermission =
0N/A new FilePermission("<<ALL FILES>>", "read,write");
0N/A }
0N/A security.checkPermission(printToFilePermission);
0N/A }
0N/A }
0N/A
0N/A public void updateInfo() {
0N/A Class dstCategory = Destination.class;
0N/A boolean dstSupported = false;
0N/A boolean dstSelected = false;
0N/A boolean dstAllowed = filePermission ?
0N/A allowedToPrintToFile() : false;
0N/A
0N/A // setup Destination (print-to-file) widgets
0N/A if (psCurrent.isAttributeCategorySupported(dstCategory)) {
0N/A dstSupported = true;
0N/A }
0N/A Destination dst = (Destination)asCurrent.get(dstCategory);
0N/A if (dst != null) {
0N/A dstSelected = true;
0N/A }
0N/A cbPrintToFile.setEnabled(dstSupported && dstAllowed);
0N/A cbPrintToFile.setSelected(dstSelected && dstAllowed
0N/A && dstSupported);
0N/A
0N/A // setup PrintService information widgets
0N/A Attribute type = psCurrent.getAttribute(PrinterMakeAndModel.class);
0N/A if (type != null) {
0N/A lblType.setText(type.toString());
0N/A }
0N/A Attribute status =
0N/A psCurrent.getAttribute(PrinterIsAcceptingJobs.class);
0N/A if (status != null) {
0N/A lblStatus.setText(getMsg(status.toString()));
0N/A }
0N/A Attribute info = psCurrent.getAttribute(PrinterInfo.class);
0N/A if (info != null) {
0N/A lblInfo.setText(info.toString());
0N/A }
0N/A btnProperties.setEnabled(uiFactory != null);
0N/A }
0N/A }
0N/A
0N/A private class PrintRangePanel extends JPanel
0N/A implements ActionListener, FocusListener
0N/A {
0N/A private final String strTitle = getMsg("border.printrange");
0N/A private final PageRanges prAll = new PageRanges(1, Integer.MAX_VALUE);
0N/A private JRadioButton rbAll, rbPages, rbSelect;
0N/A private JFormattedTextField tfRangeFrom, tfRangeTo;
0N/A private JLabel lblRangeTo;
0N/A private boolean prSupported;
0N/A
0N/A public PrintRangePanel() {
0N/A super();
0N/A
0N/A GridBagLayout gridbag = new GridBagLayout();
0N/A GridBagConstraints c = new GridBagConstraints();
0N/A
0N/A setLayout(gridbag);
0N/A setBorder(BorderFactory.createTitledBorder(strTitle));
0N/A
0N/A c.fill = GridBagConstraints.BOTH;
0N/A c.insets = compInsets;
0N/A c.gridwidth = GridBagConstraints.REMAINDER;
0N/A
0N/A ButtonGroup bg = new ButtonGroup();
0N/A JPanel pnlTop = new JPanel(new FlowLayout(FlowLayout.LEADING));
0N/A rbAll = createRadioButton("radiobutton.rangeall", this);
0N/A rbAll.setSelected(true);
0N/A bg.add(rbAll);
0N/A pnlTop.add(rbAll);
0N/A addToGB(pnlTop, this, gridbag, c);
0N/A
0N/A // Selection never seemed to work so I'm commenting this part.
0N/A /*
0N/A if (isAWT) {
0N/A JPanel pnlMiddle =
0N/A new JPanel(new FlowLayout(FlowLayout.LEADING));
0N/A rbSelect =
0N/A createRadioButton("radiobutton.selection", this);
0N/A bg.add(rbSelect);
0N/A pnlMiddle.add(rbSelect);
0N/A addToGB(pnlMiddle, this, gridbag, c);
0N/A }
0N/A */
0N/A
0N/A JPanel pnlBottom = new JPanel(new FlowLayout(FlowLayout.LEADING));
0N/A rbPages = createRadioButton("radiobutton.rangepages", this);
0N/A bg.add(rbPages);
0N/A pnlBottom.add(rbPages);
0N/A DecimalFormat format = new DecimalFormat("####0");
0N/A format.setMinimumFractionDigits(0);
0N/A format.setMaximumFractionDigits(0);
0N/A format.setMinimumIntegerDigits(0);
0N/A format.setMaximumIntegerDigits(5);
0N/A format.setParseIntegerOnly(true);
0N/A format.setDecimalSeparatorAlwaysShown(false);
0N/A NumberFormatter nf = new NumberFormatter(format);
0N/A nf.setMinimum(new Integer(1));
0N/A nf.setMaximum(new Integer(Integer.MAX_VALUE));
0N/A nf.setAllowsInvalid(true);
0N/A nf.setCommitsOnValidEdit(true);
0N/A tfRangeFrom = new JFormattedTextField(nf);
0N/A tfRangeFrom.setColumns(4);
0N/A tfRangeFrom.setEnabled(false);
0N/A tfRangeFrom.addActionListener(this);
0N/A tfRangeFrom.addFocusListener(this);
0N/A tfRangeFrom.setFocusLostBehavior(
0N/A JFormattedTextField.PERSIST);
0N/A tfRangeFrom.getAccessibleContext().setAccessibleName(
0N/A getMsg("radiobutton.rangepages"));
0N/A pnlBottom.add(tfRangeFrom);
0N/A lblRangeTo = new JLabel(getMsg("label.rangeto"));
0N/A lblRangeTo.setEnabled(false);
0N/A pnlBottom.add(lblRangeTo);
0N/A NumberFormatter nfto;
0N/A try {
0N/A nfto = (NumberFormatter)nf.clone();
0N/A } catch (CloneNotSupportedException e) {
0N/A nfto = new NumberFormatter();
0N/A }
0N/A tfRangeTo = new JFormattedTextField(nfto);
0N/A tfRangeTo.setColumns(4);
0N/A tfRangeTo.setEnabled(false);
0N/A tfRangeTo.addFocusListener(this);
0N/A tfRangeTo.getAccessibleContext().setAccessibleName(
0N/A getMsg("label.rangeto"));
0N/A pnlBottom.add(tfRangeTo);
0N/A addToGB(pnlBottom, this, gridbag, c);
0N/A }
0N/A
0N/A public void actionPerformed(ActionEvent e) {
0N/A Object source = e.getSource();
0N/A SunPageSelection select = SunPageSelection.ALL;
0N/A
0N/A setupRangeWidgets();
0N/A
0N/A if (source == rbAll) {
0N/A asCurrent.add(prAll);
0N/A } else if (source == rbSelect) {
0N/A select = SunPageSelection.SELECTION;
0N/A } else if (source == rbPages ||
0N/A source == tfRangeFrom ||
0N/A source == tfRangeTo) {
0N/A updateRangeAttribute();
0N/A select = SunPageSelection.RANGE;
0N/A }
0N/A
0N/A if (isAWT) {
0N/A asCurrent.add(select);
0N/A }
0N/A }
0N/A
0N/A public void focusLost(FocusEvent e) {
0N/A Object source = e.getSource();
0N/A
0N/A if ((source == tfRangeFrom) || (source == tfRangeTo)) {
0N/A updateRangeAttribute();
0N/A }
0N/A }
0N/A
0N/A public void focusGained(FocusEvent e) {}
0N/A
0N/A private void setupRangeWidgets() {
0N/A boolean rangeEnabled = (rbPages.isSelected() && prSupported);
0N/A tfRangeFrom.setEnabled(rangeEnabled);
0N/A tfRangeTo.setEnabled(rangeEnabled);
0N/A lblRangeTo.setEnabled(rangeEnabled);
0N/A }
0N/A
0N/A private void updateRangeAttribute() {
0N/A String strFrom = tfRangeFrom.getText();
0N/A String strTo = tfRangeTo.getText();
0N/A
0N/A int min;
0N/A int max;
0N/A
0N/A try {
0N/A min = Integer.parseInt(strFrom);
0N/A } catch (NumberFormatException e) {
0N/A min = 1;
0N/A }
0N/A
0N/A try {
0N/A max = Integer.parseInt(strTo);
0N/A } catch (NumberFormatException e) {
0N/A max = min;
0N/A }
0N/A
0N/A if (min < 1) {
0N/A min = 1;
0N/A tfRangeFrom.setValue(new Integer(1));
0N/A }
0N/A
0N/A if (max < min) {
0N/A max = min;
0N/A tfRangeTo.setValue(new Integer(min));
0N/A }
0N/A
0N/A PageRanges pr = new PageRanges(min, max);
0N/A asCurrent.add(pr);
0N/A }
0N/A
0N/A public void updateInfo() {
0N/A Class prCategory = PageRanges.class;
0N/A prSupported = false;
0N/A
0N/A if (psCurrent.isAttributeCategorySupported(prCategory) ||
0N/A isAWT) {
0N/A prSupported = true;
0N/A }
0N/A
0N/A SunPageSelection select = SunPageSelection.ALL;
0N/A int min = 1;
0N/A int max = 1;
0N/A
0N/A PageRanges pr = (PageRanges)asCurrent.get(prCategory);
0N/A if (pr != null) {
0N/A if (!pr.equals(prAll)) {
0N/A select = SunPageSelection.RANGE;
0N/A
0N/A int[][] members = pr.getMembers();
0N/A if ((members.length > 0) &&
0N/A (members[0].length > 1)) {
0N/A min = members[0][0];
0N/A max = members[0][1];
0N/A }
0N/A }
0N/A }
0N/A
0N/A if (isAWT) {
0N/A select = (SunPageSelection)asCurrent.get(
0N/A SunPageSelection.class);
0N/A }
0N/A
0N/A if (select == SunPageSelection.ALL) {
0N/A rbAll.setSelected(true);
0N/A } else if (select == SunPageSelection.SELECTION) {
0N/A // Comment this for now - rbSelect is not initialized
0N/A // because Selection button is not added.
0N/A // See PrintRangePanel above.
0N/A
0N/A //rbSelect.setSelected(true);
0N/A } else { // RANGE
0N/A rbPages.setSelected(true);
0N/A }
0N/A tfRangeFrom.setValue(new Integer(min));
0N/A tfRangeTo.setValue(new Integer(max));
0N/A rbAll.setEnabled(prSupported);
0N/A rbPages.setEnabled(prSupported);
0N/A setupRangeWidgets();
0N/A }
0N/A }
0N/A
0N/A private class CopiesPanel extends JPanel
0N/A implements ActionListener, ChangeListener
0N/A {
0N/A private final String strTitle = getMsg("border.copies");
0N/A private SpinnerNumberModel snModel;
0N/A private JSpinner spinCopies;
0N/A private JLabel lblCopies;
0N/A private JCheckBox cbCollate;
0N/A private boolean scSupported;
0N/A
0N/A public CopiesPanel() {
0N/A super();
0N/A
0N/A GridBagLayout gridbag = new GridBagLayout();
0N/A GridBagConstraints c = new GridBagConstraints();
0N/A
0N/A setLayout(gridbag);
0N/A setBorder(BorderFactory.createTitledBorder(strTitle));
0N/A
0N/A c.fill = GridBagConstraints.HORIZONTAL;
0N/A c.insets = compInsets;
0N/A
0N/A lblCopies = new JLabel(getMsg("label.numcopies"), JLabel.TRAILING);
0N/A lblCopies.setDisplayedMnemonic(getMnemonic("label.numcopies"));
0N/A lblCopies.getAccessibleContext().setAccessibleName(
0N/A getMsg("label.numcopies"));
0N/A addToGB(lblCopies, this, gridbag, c);
0N/A
0N/A snModel = new SpinnerNumberModel(1, 1, 999, 1);
0N/A spinCopies = new JSpinner(snModel);
0N/A lblCopies.setLabelFor(spinCopies);
0N/A // REMIND
0N/A ((JSpinner.NumberEditor)spinCopies.getEditor()).getTextField().setColumns(3);
0N/A spinCopies.addChangeListener(this);
0N/A c.gridwidth = GridBagConstraints.REMAINDER;
0N/A addToGB(spinCopies, this, gridbag, c);
0N/A
0N/A cbCollate = createCheckBox("checkbox.collate", this);
0N/A cbCollate.setEnabled(false);
0N/A addToGB(cbCollate, this, gridbag, c);
0N/A }
0N/A
0N/A public void actionPerformed(ActionEvent e) {
0N/A if (cbCollate.isSelected()) {
0N/A asCurrent.add(SheetCollate.COLLATED);
0N/A } else {
0N/A asCurrent.add(SheetCollate.UNCOLLATED);
0N/A }
0N/A }
0N/A
0N/A public void stateChanged(ChangeEvent e) {
0N/A updateCollateCB();
0N/A
0N/A asCurrent.add(new Copies(snModel.getNumber().intValue()));
0N/A }
0N/A
0N/A private void updateCollateCB() {
0N/A int num = snModel.getNumber().intValue();
0N/A if (isAWT) {
0N/A cbCollate.setEnabled(true);
0N/A } else {
0N/A cbCollate.setEnabled((num > 1) && scSupported);
0N/A }
0N/A }
0N/A
0N/A public void updateInfo() {
0N/A Class cpCategory = Copies.class;
0N/A Class csCategory = CopiesSupported.class;
0N/A Class scCategory = SheetCollate.class;
0N/A boolean cpSupported = false;
0N/A scSupported = false;
0N/A
0N/A // setup Copies spinner
0N/A if (psCurrent.isAttributeCategorySupported(cpCategory)) {
0N/A cpSupported = true;
0N/A }
0N/A CopiesSupported cs =
0N/A (CopiesSupported)psCurrent.getSupportedAttributeValues(
0N/A cpCategory, null, null);
0N/A if (cs == null) {
0N/A cs = new CopiesSupported(1, 999);
0N/A }
0N/A Copies cp = (Copies)asCurrent.get(cpCategory);
0N/A if (cp == null) {
0N/A cp = (Copies)psCurrent.getDefaultAttributeValue(cpCategory);
0N/A if (cp == null) {
0N/A cp = new Copies(1);
0N/A }
0N/A }
0N/A spinCopies.setEnabled(cpSupported);
0N/A lblCopies.setEnabled(cpSupported);
0N/A
0N/A int[][] members = cs.getMembers();
0N/A int min, max;
0N/A if ((members.length > 0) && (members[0].length > 0)) {
0N/A min = members[0][0];
0N/A max = members[0][1];
0N/A } else {
0N/A min = 1;
0N/A max = Integer.MAX_VALUE;
0N/A }
0N/A snModel.setMinimum(new Integer(min));
0N/A snModel.setMaximum(new Integer(max));
0N/A
0N/A int value = cp.getValue();
0N/A if ((value < min) || (value > max)) {
0N/A value = min;
0N/A }
0N/A snModel.setValue(new Integer(value));
0N/A
0N/A // setup Collate checkbox
0N/A if (psCurrent.isAttributeCategorySupported(scCategory)) {
0N/A scSupported = true;
0N/A }
0N/A SheetCollate sc = (SheetCollate)asCurrent.get(scCategory);
0N/A if (sc == null) {
0N/A sc = (SheetCollate)psCurrent.getDefaultAttributeValue(scCategory);
0N/A if (sc == null) {
0N/A sc = SheetCollate.UNCOLLATED;
0N/A }
0N/A }
0N/A cbCollate.setSelected(sc == SheetCollate.COLLATED);
0N/A updateCollateCB();
0N/A }
0N/A }
0N/A
0N/A
0N/A
0N/A
0N/A /**
0N/A * The "Page Setup" tab. Includes the controls for MediaSource/MediaTray,
0N/A * OrientationRequested, and Sides.
0N/A */
0N/A private class PageSetupPanel extends JPanel {
0N/A
0N/A private MediaPanel pnlMedia;
0N/A private OrientationPanel pnlOrientation;
0N/A private MarginsPanel pnlMargins;
0N/A
0N/A public PageSetupPanel() {
0N/A super();
0N/A
0N/A GridBagLayout gridbag = new GridBagLayout();
0N/A GridBagConstraints c = new GridBagConstraints();
0N/A
0N/A setLayout(gridbag);
0N/A
0N/A c.fill = GridBagConstraints.BOTH;
0N/A c.insets = panelInsets;
0N/A c.weightx = 1.0;
0N/A c.weighty = 1.0;
0N/A
0N/A c.gridwidth = GridBagConstraints.REMAINDER;
0N/A pnlMedia = new MediaPanel();
0N/A addToGB(pnlMedia, this, gridbag, c);
0N/A
0N/A pnlOrientation = new OrientationPanel();
0N/A c.gridwidth = GridBagConstraints.RELATIVE;
0N/A addToGB(pnlOrientation, this, gridbag, c);
0N/A
0N/A pnlMargins = new MarginsPanel();
0N/A pnlOrientation.addOrientationListener(pnlMargins);
0N/A pnlMedia.addMediaListener(pnlMargins);
0N/A c.gridwidth = GridBagConstraints.REMAINDER;
0N/A addToGB(pnlMargins, this, gridbag, c);
0N/A }
0N/A
0N/A public void updateInfo() {
0N/A pnlMedia.updateInfo();
0N/A pnlOrientation.updateInfo();
0N/A pnlMargins.updateInfo();
0N/A }
0N/A }
0N/A
0N/A private class MarginsPanel extends JPanel
0N/A implements ActionListener, FocusListener {
0N/A
0N/A private final String strTitle = getMsg("border.margins");
0N/A private JFormattedTextField leftMargin, rightMargin,
0N/A topMargin, bottomMargin;
0N/A private JLabel lblLeft, lblRight, lblTop, lblBottom;
0N/A private int units = MediaPrintableArea.MM;
0N/A // storage for the last margin values calculated, -ve is uninitialised
0N/A private float lmVal = -1f,rmVal = -1f, tmVal = -1f, bmVal = -1f;
0N/A // storage for margins as objects mapped into orientation for display
0N/A private Float lmObj,rmObj,tmObj,bmObj;
0N/A
0N/A public MarginsPanel() {
0N/A super();
0N/A
0N/A GridBagLayout gridbag = new GridBagLayout();
0N/A GridBagConstraints c = new GridBagConstraints();
0N/A c.fill = GridBagConstraints.HORIZONTAL;
0N/A c.weightx = 1.0;
0N/A c.weighty = 0.0;
0N/A c.insets = compInsets;
0N/A
0N/A setLayout(gridbag);
0N/A setBorder(BorderFactory.createTitledBorder(strTitle));
0N/A
0N/A String unitsKey = "label.millimetres";
0N/A String defaultCountry = Locale.getDefault().getCountry();
0N/A if (defaultCountry != null &&
0N/A (defaultCountry.equals("") ||
0N/A defaultCountry.equals(Locale.US.getCountry()) ||
0N/A defaultCountry.equals(Locale.CANADA.getCountry()))) {
0N/A unitsKey = "label.inches";
0N/A units = MediaPrintableArea.INCH;
0N/A }
0N/A String unitsMsg = getMsg(unitsKey);
0N/A
0N/A DecimalFormat format;
0N/A if (units == MediaPrintableArea.MM) {
0N/A format = new DecimalFormat("###.##");
0N/A format.setMaximumIntegerDigits(3);
0N/A } else {
0N/A format = new DecimalFormat("##.##");
0N/A format.setMaximumIntegerDigits(2);
0N/A }
0N/A
0N/A format.setMinimumFractionDigits(1);
0N/A format.setMaximumFractionDigits(2);
0N/A format.setMinimumIntegerDigits(1);
0N/A format.setParseIntegerOnly(false);
0N/A format.setDecimalSeparatorAlwaysShown(true);
0N/A NumberFormatter nf = new NumberFormatter(format);
0N/A nf.setMinimum(new Float(0.0f));
0N/A nf.setMaximum(new Float(999.0f));
0N/A nf.setAllowsInvalid(true);
0N/A nf.setCommitsOnValidEdit(true);
0N/A
0N/A leftMargin = new JFormattedTextField(nf);
0N/A leftMargin.addFocusListener(this);
0N/A leftMargin.addActionListener(this);
0N/A leftMargin.getAccessibleContext().setAccessibleName(
0N/A getMsg("label.leftmargin"));
0N/A rightMargin = new JFormattedTextField(nf);
0N/A rightMargin.addFocusListener(this);
0N/A rightMargin.addActionListener(this);
0N/A rightMargin.getAccessibleContext().setAccessibleName(
0N/A getMsg("label.rightmargin"));
0N/A topMargin = new JFormattedTextField(nf);
0N/A topMargin.addFocusListener(this);
0N/A topMargin.addActionListener(this);
0N/A topMargin.getAccessibleContext().setAccessibleName(
0N/A getMsg("label.topmargin"));
0N/A topMargin = new JFormattedTextField(nf);
0N/A bottomMargin = new JFormattedTextField(nf);
0N/A bottomMargin.addFocusListener(this);
0N/A bottomMargin.addActionListener(this);
0N/A bottomMargin.getAccessibleContext().setAccessibleName(
0N/A getMsg("label.bottommargin"));
0N/A topMargin = new JFormattedTextField(nf);
0N/A c.gridwidth = GridBagConstraints.RELATIVE;
0N/A lblLeft = new JLabel(getMsg("label.leftmargin") + " " + unitsMsg,
0N/A JLabel.LEADING);
0N/A lblLeft.setDisplayedMnemonic(getMnemonic("label.leftmargin"));
0N/A lblLeft.setLabelFor(leftMargin);
0N/A addToGB(lblLeft, this, gridbag, c);
0N/A
0N/A c.gridwidth = GridBagConstraints.REMAINDER;
0N/A lblRight = new JLabel(getMsg("label.rightmargin") + " " + unitsMsg,
0N/A JLabel.LEADING);
0N/A lblRight.setDisplayedMnemonic(getMnemonic("label.rightmargin"));
0N/A lblRight.setLabelFor(rightMargin);
0N/A addToGB(lblRight, this, gridbag, c);
0N/A
0N/A c.gridwidth = GridBagConstraints.RELATIVE;
0N/A addToGB(leftMargin, this, gridbag, c);
0N/A
0N/A c.gridwidth = GridBagConstraints.REMAINDER;
0N/A addToGB(rightMargin, this, gridbag, c);
0N/A
0N/A // add an invisible spacing component.
0N/A addToGB(new JPanel(), this, gridbag, c);
0N/A
0N/A c.gridwidth = GridBagConstraints.RELATIVE;
0N/A lblTop = new JLabel(getMsg("label.topmargin") + " " + unitsMsg,
0N/A JLabel.LEADING);
0N/A lblTop.setDisplayedMnemonic(getMnemonic("label.topmargin"));
0N/A lblTop.setLabelFor(topMargin);
0N/A addToGB(lblTop, this, gridbag, c);
0N/A
0N/A c.gridwidth = GridBagConstraints.REMAINDER;
0N/A lblBottom = new JLabel(getMsg("label.bottommargin") +
0N/A " " + unitsMsg, JLabel.LEADING);
0N/A lblBottom.setDisplayedMnemonic(getMnemonic("label.bottommargin"));
0N/A lblBottom.setLabelFor(bottomMargin);
0N/A addToGB(lblBottom, this, gridbag, c);
0N/A
0N/A c.gridwidth = GridBagConstraints.RELATIVE;
0N/A addToGB(topMargin, this, gridbag, c);
0N/A
0N/A c.gridwidth = GridBagConstraints.REMAINDER;
0N/A addToGB(bottomMargin, this, gridbag, c);
0N/A
0N/A }
0N/A
0N/A public void actionPerformed(ActionEvent e) {
0N/A Object source = e.getSource();
0N/A updateMargins(source);
0N/A }
0N/A
0N/A public void focusLost(FocusEvent e) {
0N/A Object source = e.getSource();
0N/A updateMargins(source);
0N/A }
0N/A
0N/A public void focusGained(FocusEvent e) {}
0N/A
0N/A /* Get the numbers, use to create a MPA.
0N/A * If its valid, accept it and update the attribute set.
0N/A * If its not valid, then reject it and call updateInfo()
0N/A * to re-establish the previous entries.
0N/A */
0N/A public void updateMargins(Object source) {
0N/A if (!(source instanceof JFormattedTextField)) {
0N/A return;
0N/A } else {
0N/A JFormattedTextField tf = (JFormattedTextField)source;
0N/A Float val = (Float)tf.getValue();
0N/A if (val == null) {
0N/A return;
0N/A }
0N/A if (tf == leftMargin && val.equals(lmObj)) {
0N/A return;
0N/A }
0N/A if (tf == rightMargin && val.equals(rmObj)) {
0N/A return;
0N/A }
0N/A if (tf == topMargin && val.equals(tmObj)) {
0N/A return;
0N/A }
0N/A if (tf == bottomMargin && val.equals(bmObj)) {
0N/A return;
0N/A }
0N/A }
0N/A
0N/A Float lmTmpObj = (Float)leftMargin.getValue();
0N/A Float rmTmpObj = (Float)rightMargin.getValue();
0N/A Float tmTmpObj = (Float)topMargin.getValue();
0N/A Float bmTmpObj = (Float)bottomMargin.getValue();
0N/A
0N/A float lm = lmTmpObj.floatValue();
0N/A float rm = rmTmpObj.floatValue();
0N/A float tm = tmTmpObj.floatValue();
0N/A float bm = bmTmpObj.floatValue();
0N/A
0N/A /* adjust for orientation */
0N/A Class orCategory = OrientationRequested.class;
0N/A OrientationRequested or =
0N/A (OrientationRequested)asCurrent.get(orCategory);
0N/A
0N/A if (or == null) {
0N/A or = (OrientationRequested)
0N/A psCurrent.getDefaultAttributeValue(orCategory);
0N/A }
0N/A
0N/A float tmp;
0N/A if (or == OrientationRequested.REVERSE_PORTRAIT) {
0N/A tmp = lm; lm = rm; rm = tmp;
0N/A tmp = tm; tm = bm; bm = tmp;
0N/A } else if (or == OrientationRequested.LANDSCAPE) {
0N/A tmp = lm;
0N/A lm = tm;
0N/A tm = rm;
0N/A rm = bm;
0N/A bm = tmp;
0N/A } else if (or == OrientationRequested.REVERSE_LANDSCAPE) {
0N/A tmp = lm;
0N/A lm = bm;
0N/A bm = rm;
0N/A rm = tm;
0N/A tm = tmp;
0N/A }
0N/A MediaPrintableArea mpa;
0N/A if ((mpa = validateMargins(lm, rm, tm, bm)) != null) {
0N/A asCurrent.add(mpa);
0N/A lmVal = lm;
0N/A rmVal = rm;
0N/A tmVal = tm;
0N/A bmVal = bm;
0N/A lmObj = lmTmpObj;
0N/A rmObj = rmTmpObj;
0N/A tmObj = tmTmpObj;
0N/A bmObj = bmTmpObj;
0N/A } else {
0N/A if (lmObj == null || rmObj == null ||
0N/A tmObj == null || rmObj == null) {
0N/A return;
0N/A } else {
0N/A leftMargin.setValue(lmObj);
0N/A rightMargin.setValue(rmObj);
0N/A topMargin.setValue(tmObj);
0N/A bottomMargin.setValue(bmObj);
0N/A
0N/A }
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * This method either accepts the values and creates a new
0N/A * MediaPrintableArea, or does nothing.
0N/A * It should not attempt to create a printable area from anything
0N/A * other than the exact values passed in.
0N/A * But REMIND/TBD: it would be user friendly to replace margins the
0N/A * user entered but are out of bounds with the minimum.
0N/A * At that point this method will need to take responsibility for
0N/A * updating the "stored" values and the UI.
0N/A */
0N/A private MediaPrintableArea validateMargins(float lm, float rm,
0N/A float tm, float bm) {
0N/A
0N/A Class mpaCategory = MediaPrintableArea.class;
0N/A MediaPrintableArea mpa;
0N/A MediaPrintableArea mpaMax = null;
0N/A MediaSize mediaSize = null;
0N/A
0N/A Media media = (Media)asCurrent.get(Media.class);
0N/A if (media == null || !(media instanceof MediaSizeName)) {
0N/A media = (Media)psCurrent.getDefaultAttributeValue(Media.class);
0N/A }
0N/A if (media != null && (media instanceof MediaSizeName)) {
0N/A MediaSizeName msn = (MediaSizeName)media;
0N/A mediaSize = MediaSize.getMediaSizeForName(msn);
0N/A }
0N/A if (mediaSize == null) {
0N/A mediaSize = new MediaSize(8.5f, 11f, Size2DSyntax.INCH);
0N/A }
0N/A
0N/A if (media != null) {
0N/A PrintRequestAttributeSet tmpASet =
0N/A new HashPrintRequestAttributeSet(asCurrent);
0N/A tmpASet.add(media);
0N/A
0N/A Object values =
0N/A psCurrent.getSupportedAttributeValues(mpaCategory,
0N/A docFlavor,
0N/A tmpASet);
0N/A if (values instanceof MediaPrintableArea[] &&
0N/A ((MediaPrintableArea[])values).length > 0) {
0N/A mpaMax = ((MediaPrintableArea[])values)[0];
0N/A
0N/A }
0N/A }
0N/A if (mpaMax == null) {
0N/A mpaMax = new MediaPrintableArea(0f, 0f,
0N/A mediaSize.getX(units),
0N/A mediaSize.getY(units),
0N/A units);
0N/A }
0N/A
0N/A float wid = mediaSize.getX(units);
0N/A float hgt = mediaSize.getY(units);
0N/A float pax = lm;
0N/A float pay = tm;
0N/A float paw = wid - lm - rm;
0N/A float pah = hgt - tm - bm;
0N/A
0N/A if (paw <= 0f || pah <= 0f || pax < 0f || pay < 0f ||
0N/A pax < mpaMax.getX(units) || paw > mpaMax.getWidth(units) ||
0N/A pay < mpaMax.getY(units) || pah > mpaMax.getHeight(units)) {
0N/A return null;
0N/A } else {
0N/A return new MediaPrintableArea(lm, tm, paw, pah, units);
0N/A }
0N/A }
0N/A
0N/A /* This is complex as a MediaPrintableArea is valid only within
0N/A * a particular context of media size.
0N/A * So we need a MediaSize as well as a MediaPrintableArea.
0N/A * MediaSize can be obtained from MediaSizeName.
0N/A * If the application specifies a MediaPrintableArea, we accept it
0N/A * to the extent its valid for the Media they specify. If they
0N/A * don't specify a Media, then the default is assumed.
0N/A *
0N/A * If an application doesn't define a MediaPrintableArea, we need to
0N/A * create a suitable one, this is created using the specified (or
0N/A * default) Media and default 1 inch margins. This is validated
0N/A * against the paper in case this is too large for tiny media.
0N/A */
0N/A public void updateInfo() {
0N/A
0N/A if (isAWT) {
0N/A leftMargin.setEnabled(false);
0N/A rightMargin.setEnabled(false);
0N/A topMargin.setEnabled(false);
0N/A bottomMargin.setEnabled(false);
0N/A lblLeft.setEnabled(false);
0N/A lblRight.setEnabled(false);
0N/A lblTop.setEnabled(false);
0N/A lblBottom.setEnabled(false);
0N/A return;
0N/A }
0N/A
0N/A Class mpaCategory = MediaPrintableArea.class;
0N/A MediaPrintableArea mpa =
0N/A (MediaPrintableArea)asCurrent.get(mpaCategory);
0N/A MediaPrintableArea mpaMax = null;
0N/A MediaSize mediaSize = null;
0N/A
0N/A Media media = (Media)asCurrent.get(Media.class);
0N/A if (media == null || !(media instanceof MediaSizeName)) {
0N/A media = (Media)psCurrent.getDefaultAttributeValue(Media.class);
0N/A }
0N/A if (media != null && (media instanceof MediaSizeName)) {
0N/A MediaSizeName msn = (MediaSizeName)media;
0N/A mediaSize = MediaSize.getMediaSizeForName(msn);
0N/A }
0N/A if (mediaSize == null) {
0N/A mediaSize = new MediaSize(8.5f, 11f, Size2DSyntax.INCH);
0N/A }
0N/A
0N/A if (media != null) {
0N/A PrintRequestAttributeSet tmpASet =
0N/A new HashPrintRequestAttributeSet(asCurrent);
0N/A tmpASet.add(media);
0N/A
0N/A Object values =
0N/A psCurrent.getSupportedAttributeValues(mpaCategory,
0N/A docFlavor,
0N/A tmpASet);
0N/A if (values instanceof MediaPrintableArea[] &&
0N/A ((MediaPrintableArea[])values).length > 0) {
0N/A mpaMax = ((MediaPrintableArea[])values)[0];
0N/A
0N/A } else if (values instanceof MediaPrintableArea) {
0N/A mpaMax = (MediaPrintableArea)values;
0N/A }
0N/A }
0N/A if (mpaMax == null) {
0N/A mpaMax = new MediaPrintableArea(0f, 0f,
0N/A mediaSize.getX(units),
0N/A mediaSize.getY(units),
0N/A units);
0N/A }
0N/A
0N/A /*
0N/A * At this point we now know as best we can :-
0N/A * - the media size
0N/A * - the maximum corresponding printable area
0N/A * - the media printable area specified by the client, if any.
0N/A * The next step is to create a default MPA if none was specified.
0N/A * 1" margins are used unless they are disproportionately
0N/A * large compared to the size of the media.
0N/A */
0N/A
0N/A float wid = mediaSize.getX(MediaPrintableArea.INCH);
0N/A float hgt = mediaSize.getY(MediaPrintableArea.INCH);
0N/A float maxMarginRatio = 5f;
0N/A float xMgn, yMgn;
0N/A if (wid > maxMarginRatio) {
0N/A xMgn = 1f;
0N/A } else {
0N/A xMgn = wid / maxMarginRatio;
0N/A }
0N/A if (hgt > maxMarginRatio) {
0N/A yMgn = 1f;
0N/A } else {
0N/A yMgn = hgt / maxMarginRatio;
0N/A }
0N/A
0N/A if (mpa == null) {
0N/A mpa = new MediaPrintableArea(xMgn, yMgn,
0N/A wid-(2*xMgn), hgt-(2*yMgn),
0N/A MediaPrintableArea.INCH);
0N/A asCurrent.add(mpa);
0N/A }
0N/A float pax = mpa.getX(units);
0N/A float pay = mpa.getY(units);
0N/A float paw = mpa.getWidth(units);
0N/A float pah = mpa.getHeight(units);
0N/A float paxMax = mpaMax.getX(units);
0N/A float payMax = mpaMax.getY(units);
0N/A float pawMax = mpaMax.getWidth(units);
0N/A float pahMax = mpaMax.getHeight(units);
0N/A
0N/A
0N/A boolean invalid = false;
0N/A
0N/A // If the paper is set to something which is too small to
0N/A // accommodate a specified printable area, perhaps carried
0N/A // over from a larger paper, the adjustment that needs to be
0N/A // performed should seem the most natural from a user's viewpoint.
0N/A // Since the user is specifying margins, then we are biased
0N/A // towards keeping the margins as close to what is specified as
0N/A // possible, shrinking or growing the printable area.
0N/A // But the API uses printable area, so you need to know the
0N/A // media size in which the margins were previously interpreted,
0N/A // or at least have a record of the margins.
0N/A // In the case that this is the creation of this UI we do not
0N/A // have this record, so we are somewhat reliant on the client
0N/A // to supply a reasonable default
0N/A wid = mediaSize.getX(units);
0N/A hgt = mediaSize.getY(units);
0N/A if (lmVal >= 0f) {
0N/A invalid = true;
0N/A
0N/A if (lmVal + rmVal > wid) {
0N/A // margins impossible, but maintain P.A if can
0N/A if (paw > pawMax) {
0N/A paw = pawMax;
0N/A }
0N/A // try to centre the printable area.
0N/A pax = (wid - paw)/2f;
0N/A } else {
0N/A pax = (lmVal >= paxMax) ? lmVal : paxMax;
0N/A paw = wid - pax - rmVal;
0N/A }
0N/A if (tmVal + bmVal > hgt) {
0N/A if (pah > pahMax) {
0N/A pah = pahMax;
0N/A }
0N/A pay = (hgt - pah)/2f;
0N/A } else {
0N/A pay = (tmVal >= payMax) ? tmVal : payMax;
0N/A pah = hgt - pay - bmVal;
0N/A }
0N/A }
0N/A if (pax < paxMax) {
0N/A invalid = true;
0N/A pax = paxMax;
0N/A }
0N/A if (pay < payMax) {
0N/A invalid = true;
0N/A pay = payMax;
0N/A }
0N/A if (paw > pawMax) {
0N/A invalid = true;
0N/A paw = pawMax;
0N/A }
0N/A if (pah > pahMax) {
0N/A invalid = true;
0N/A pah = pahMax;
0N/A }
0N/A
0N/A if ((pax + paw > paxMax + pawMax) || (paw <= 0f)) {
0N/A invalid = true;
0N/A pax = paxMax;
0N/A paw = pawMax;
0N/A }
0N/A if ((pay + pah > payMax + pahMax) || (pah <= 0f)) {
0N/A invalid = true;
0N/A pay = payMax;
0N/A pah = pahMax;
0N/A }
0N/A
0N/A if (invalid) {
0N/A mpa = new MediaPrintableArea(pax, pay, paw, pah, units);
0N/A asCurrent.add(mpa);
0N/A }
0N/A
0N/A /* We now have a valid printable area.
0N/A * Turn it into margins, using the mediaSize
0N/A */
0N/A lmVal = pax;
0N/A tmVal = pay;
0N/A rmVal = mediaSize.getX(units) - pax - paw;
0N/A bmVal = mediaSize.getY(units) - pay - pah;
0N/A
0N/A lmObj = new Float(lmVal);
0N/A rmObj = new Float(rmVal);
0N/A tmObj = new Float(tmVal);
0N/A bmObj = new Float(bmVal);
0N/A
0N/A /* Now we know the values to use, we need to assign them
0N/A * to the fields appropriate for the orientation.
0N/A * Note: if orientation changes this method must be called.
0N/A */
0N/A Class orCategory = OrientationRequested.class;
0N/A OrientationRequested or =
0N/A (OrientationRequested)asCurrent.get(orCategory);
0N/A
0N/A if (or == null) {
0N/A or = (OrientationRequested)
0N/A psCurrent.getDefaultAttributeValue(orCategory);
0N/A }
0N/A
0N/A Float tmp;
0N/A
0N/A if (or == OrientationRequested.REVERSE_PORTRAIT) {
0N/A tmp = lmObj; lmObj = rmObj; rmObj = tmp;
0N/A tmp = tmObj; tmObj = bmObj; bmObj = tmp;
0N/A } else if (or == OrientationRequested.LANDSCAPE) {
0N/A tmp = lmObj;
0N/A lmObj = bmObj;
0N/A bmObj = rmObj;
0N/A rmObj = tmObj;
0N/A tmObj = tmp;
0N/A } else if (or == OrientationRequested.REVERSE_LANDSCAPE) {
0N/A tmp = lmObj;
0N/A lmObj = tmObj;
0N/A tmObj = rmObj;
0N/A rmObj = bmObj;
0N/A bmObj = tmp;
0N/A }
0N/A
0N/A leftMargin.setValue(lmObj);
0N/A rightMargin.setValue(rmObj);
0N/A topMargin.setValue(tmObj);
0N/A bottomMargin.setValue(bmObj);
0N/A }
0N/A }
0N/A
0N/A private class MediaPanel extends JPanel implements ItemListener {
0N/A
0N/A private final String strTitle = getMsg("border.media");
0N/A private JLabel lblSize, lblSource;
0N/A private JComboBox cbSize, cbSource;
0N/A private Vector sizes = new Vector();
0N/A private Vector sources = new Vector();
0N/A private MarginsPanel pnlMargins = null;
0N/A
0N/A public MediaPanel() {
0N/A super();
0N/A
0N/A GridBagLayout gridbag = new GridBagLayout();
0N/A GridBagConstraints c = new GridBagConstraints();
0N/A
0N/A setLayout(gridbag);
0N/A setBorder(BorderFactory.createTitledBorder(strTitle));
0N/A
0N/A cbSize = new JComboBox();
0N/A cbSource = new JComboBox();
0N/A
0N/A c.fill = GridBagConstraints.BOTH;
0N/A c.insets = compInsets;
0N/A c.weighty = 1.0;
0N/A
0N/A c.weightx = 0.0;
0N/A lblSize = new JLabel(getMsg("label.size"), JLabel.TRAILING);
0N/A lblSize.setDisplayedMnemonic(getMnemonic("label.size"));
0N/A lblSize.setLabelFor(cbSize);
0N/A addToGB(lblSize, this, gridbag, c);
0N/A c.weightx = 1.0;
0N/A c.gridwidth = GridBagConstraints.REMAINDER;
0N/A addToGB(cbSize, this, gridbag, c);
0N/A
0N/A c.weightx = 0.0;
0N/A c.gridwidth = 1;
0N/A lblSource = new JLabel(getMsg("label.source"), JLabel.TRAILING);
0N/A lblSource.setDisplayedMnemonic(getMnemonic("label.source"));
0N/A lblSource.setLabelFor(cbSource);
0N/A addToGB(lblSource, this, gridbag, c);
0N/A c.gridwidth = GridBagConstraints.REMAINDER;
0N/A addToGB(cbSource, this, gridbag, c);
0N/A }
0N/A
0N/A private String getMediaName(String key) {
0N/A try {
0N/A // replace characters that would be invalid in
0N/A // a resource key with valid characters
0N/A String newkey = key.replace(' ', '-');
0N/A newkey = newkey.replace('#', 'n');
0N/A
0N/A return messageRB.getString(newkey);
0N/A } catch (java.util.MissingResourceException e) {
0N/A return key;
0N/A }
0N/A }
0N/A
0N/A public void itemStateChanged(ItemEvent e) {
0N/A Object source = e.getSource();
0N/A
0N/A if (e.getStateChange() == ItemEvent.SELECTED) {
0N/A if (source == cbSize) {
0N/A int index = cbSize.getSelectedIndex();
0N/A
0N/A if ((index >= 0) && (index < sizes.size())) {
0N/A if ((cbSource.getItemCount() > 1) &&
0N/A (cbSource.getSelectedIndex() >= 1))
0N/A {
0N/A int src = cbSource.getSelectedIndex() - 1;
0N/A MediaTray mt = (MediaTray)sources.get(src);
0N/A asCurrent.add(new SunAlternateMedia(mt));
0N/A }
0N/A asCurrent.add((MediaSizeName)sizes.get(index));
0N/A }
0N/A } else if (source == cbSource) {
0N/A int index = cbSource.getSelectedIndex();
0N/A
0N/A if ((index >= 1) && (index < (sources.size() + 1))) {
0N/A asCurrent.remove(SunAlternateMedia.class);
0N/A MediaTray newTray = (MediaTray)sources.get(index - 1);
0N/A Media m = (Media)asCurrent.get(Media.class);
0N/A if (m == null || m instanceof MediaTray) {
0N/A asCurrent.add(newTray);
0N/A } else if (m instanceof MediaSizeName) {
0N/A MediaSizeName msn = (MediaSizeName)m;
0N/A Media def = (Media)psCurrent.getDefaultAttributeValue(Media.class);
0N/A if (def instanceof MediaSizeName && def.equals(msn)) {
0N/A asCurrent.add(newTray);
0N/A } else {
0N/A /* Non-default paper size, so need to store tray
0N/A * as SunAlternateMedia
0N/A */
0N/A asCurrent.add(new SunAlternateMedia(newTray));
0N/A }
0N/A }
0N/A } else if (index == 0) {
0N/A asCurrent.remove(SunAlternateMedia.class);
0N/A if (cbSize.getItemCount() > 0) {
0N/A int size = cbSize.getSelectedIndex();
0N/A asCurrent.add((MediaSizeName)sizes.get(size));
0N/A }
0N/A }
0N/A }
0N/A // orientation affects display of margins.
0N/A if (pnlMargins != null) {
0N/A pnlMargins.updateInfo();
0N/A }
0N/A }
0N/A }
0N/A
0N/A
0N/A /* this is ad hoc to keep things simple */
0N/A public void addMediaListener(MarginsPanel pnl) {
0N/A pnlMargins = pnl;
0N/A }
0N/A public void updateInfo() {
0N/A Class mdCategory = Media.class;
0N/A Class amCategory = SunAlternateMedia.class;
0N/A boolean mediaSupported = false;
0N/A
0N/A cbSize.removeItemListener(this);
0N/A cbSize.removeAllItems();
0N/A cbSource.removeItemListener(this);
0N/A cbSource.removeAllItems();
0N/A cbSource.addItem(getMediaName("auto-select"));
0N/A
0N/A sizes.clear();
0N/A sources.clear();
0N/A
0N/A if (psCurrent.isAttributeCategorySupported(mdCategory)) {
0N/A mediaSupported = true;
0N/A
0N/A Object values =
0N/A psCurrent.getSupportedAttributeValues(mdCategory,
0N/A docFlavor,
0N/A asCurrent);
0N/A
0N/A if (values instanceof Media[]) {
0N/A Media[] media = (Media[])values;
0N/A
0N/A for (int i = 0; i < media.length; i++) {
0N/A Media medium = media[i];
0N/A
0N/A if (medium instanceof MediaSizeName) {
0N/A sizes.add(medium);
0N/A cbSize.addItem(getMediaName(medium.toString()));
0N/A } else if (medium instanceof MediaTray) {
0N/A sources.add(medium);
0N/A cbSource.addItem(getMediaName(medium.toString()));
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A boolean msSupported = (mediaSupported && (sizes.size() > 0));
0N/A lblSize.setEnabled(msSupported);
0N/A cbSize.setEnabled(msSupported);
0N/A
0N/A if (isAWT) {
0N/A cbSource.setEnabled(false);
0N/A lblSource.setEnabled(false);
0N/A } else {
0N/A cbSource.setEnabled(mediaSupported);
0N/A }
0N/A
0N/A if (mediaSupported) {
0N/A
0N/A Media medium = (Media)asCurrent.get(mdCategory);
0N/A
0N/A // initialize size selection to default
0N/A Media defMedia = (Media)psCurrent.getDefaultAttributeValue(mdCategory);
0N/A if (defMedia instanceof MediaSizeName) {
0N/A cbSize.setSelectedIndex(sizes.size() > 0 ? sizes.indexOf(defMedia) : -1);
0N/A }
0N/A
0N/A if (medium == null ||
0N/A !psCurrent.isAttributeValueSupported(medium,
0N/A docFlavor, asCurrent)) {
0N/A
0N/A medium = defMedia;
0N/A
0N/A if (medium == null) {
0N/A if (sizes.size() > 0) {
0N/A medium = (Media)sizes.get(0);
0N/A }
0N/A }
0N/A if (medium != null) {
0N/A asCurrent.add(medium);
0N/A }
0N/A }
0N/A if (medium != null) {
0N/A if (medium instanceof MediaSizeName) {
0N/A MediaSizeName ms = (MediaSizeName)medium;
0N/A cbSize.setSelectedIndex(sizes.indexOf(ms));
0N/A } else if (medium instanceof MediaTray) {
0N/A MediaTray mt = (MediaTray)medium;
0N/A cbSource.setSelectedIndex(sources.indexOf(mt) + 1);
0N/A }
0N/A } else {
0N/A cbSize.setSelectedIndex(sizes.size() > 0 ? 0 : -1);
0N/A cbSource.setSelectedIndex(0);
0N/A }
0N/A
0N/A SunAlternateMedia alt = (SunAlternateMedia)asCurrent.get(amCategory);
0N/A if (alt != null) {
0N/A Media md = alt.getMedia();
0N/A if (md instanceof MediaTray) {
0N/A MediaTray mt = (MediaTray)md;
0N/A cbSource.setSelectedIndex(sources.indexOf(mt) + 1);
0N/A }
0N/A }
0N/A
0N/A int selIndex = cbSize.getSelectedIndex();
0N/A if ((selIndex >= 0) && (selIndex < sizes.size())) {
0N/A asCurrent.add((MediaSizeName)sizes.get(selIndex));
0N/A }
0N/A
0N/A selIndex = cbSource.getSelectedIndex();
0N/A if ((selIndex >= 1) && (selIndex < (sources.size()+1))) {
0N/A MediaTray mt = (MediaTray)sources.get(selIndex-1);
0N/A if (medium instanceof MediaTray) {
0N/A asCurrent.add(mt);
0N/A } else {
0N/A asCurrent.add(new SunAlternateMedia(mt));
0N/A }
0N/A }
0N/A
0N/A
0N/A }
0N/A cbSize.addItemListener(this);
0N/A cbSource.addItemListener(this);
0N/A }
0N/A }
0N/A
0N/A private class OrientationPanel extends JPanel
0N/A implements ActionListener
0N/A {
0N/A private final String strTitle = getMsg("border.orientation");
0N/A private IconRadioButton rbPortrait, rbLandscape,
0N/A rbRevPortrait, rbRevLandscape;
0N/A private MarginsPanel pnlMargins = null;
0N/A
0N/A public OrientationPanel() {
0N/A super();
0N/A
0N/A GridBagLayout gridbag = new GridBagLayout();
0N/A GridBagConstraints c = new GridBagConstraints();
0N/A
0N/A setLayout(gridbag);
0N/A setBorder(BorderFactory.createTitledBorder(strTitle));
0N/A
0N/A c.fill = GridBagConstraints.BOTH;
0N/A c.insets = compInsets;
0N/A c.weighty = 1.0;
0N/A c.gridwidth = GridBagConstraints.REMAINDER;
0N/A
0N/A ButtonGroup bg = new ButtonGroup();
0N/A rbPortrait = new IconRadioButton("radiobutton.portrait",
0N/A "orientPortrait.png", true,
0N/A bg, this);
0N/A rbPortrait.addActionListener(this);
0N/A addToGB(rbPortrait, this, gridbag, c);
0N/A rbLandscape = new IconRadioButton("radiobutton.landscape",
0N/A "orientLandscape.png", false,
0N/A bg, this);
0N/A rbLandscape.addActionListener(this);
0N/A addToGB(rbLandscape, this, gridbag, c);
0N/A rbRevPortrait = new IconRadioButton("radiobutton.revportrait",
0N/A "orientRevPortrait.png", false,
0N/A bg, this);
0N/A rbRevPortrait.addActionListener(this);
0N/A addToGB(rbRevPortrait, this, gridbag, c);
0N/A rbRevLandscape = new IconRadioButton("radiobutton.revlandscape",
0N/A "orientRevLandscape.png", false,
0N/A bg, this);
0N/A rbRevLandscape.addActionListener(this);
0N/A addToGB(rbRevLandscape, this, gridbag, c);
0N/A }
0N/A
0N/A public void actionPerformed(ActionEvent e) {
0N/A Object source = e.getSource();
0N/A
0N/A if (rbPortrait.isSameAs(source)) {
0N/A asCurrent.add(OrientationRequested.PORTRAIT);
0N/A } else if (rbLandscape.isSameAs(source)) {
0N/A asCurrent.add(OrientationRequested.LANDSCAPE);
0N/A } else if (rbRevPortrait.isSameAs(source)) {
0N/A asCurrent.add(OrientationRequested.REVERSE_PORTRAIT);
0N/A } else if (rbRevLandscape.isSameAs(source)) {
0N/A asCurrent.add(OrientationRequested.REVERSE_LANDSCAPE);
0N/A }
0N/A // orientation affects display of margins.
0N/A if (pnlMargins != null) {
0N/A pnlMargins.updateInfo();
0N/A }
0N/A }
0N/A
0N/A /* This is ad hoc to keep things simple */
0N/A void addOrientationListener(MarginsPanel pnl) {
0N/A pnlMargins = pnl;
0N/A }
0N/A
0N/A public void updateInfo() {
0N/A Class orCategory = OrientationRequested.class;
0N/A boolean pSupported = false;
0N/A boolean lSupported = false;
0N/A boolean rpSupported = false;
0N/A boolean rlSupported = false;
0N/A
0N/A if (isAWT) {
0N/A pSupported = true;
0N/A lSupported = true;
0N/A } else
0N/A if (psCurrent.isAttributeCategorySupported(orCategory)) {
0N/A Object values =
0N/A psCurrent.getSupportedAttributeValues(orCategory,
0N/A docFlavor,
0N/A asCurrent);
0N/A
0N/A if (values instanceof OrientationRequested[]) {
0N/A OrientationRequested[] ovalues =
0N/A (OrientationRequested[])values;
0N/A
0N/A for (int i = 0; i < ovalues.length; i++) {
0N/A OrientationRequested value = ovalues[i];
0N/A
0N/A if (value == OrientationRequested.PORTRAIT) {
0N/A pSupported = true;
0N/A } else if (value == OrientationRequested.LANDSCAPE) {
0N/A lSupported = true;
0N/A } else if (value == OrientationRequested.REVERSE_PORTRAIT) {
0N/A rpSupported = true;
0N/A } else if (value == OrientationRequested.REVERSE_LANDSCAPE) {
0N/A rlSupported = true;
0N/A }
0N/A }
0N/A }
997N/A }
997N/A
997N/A
997N/A rbPortrait.setEnabled(pSupported);
997N/A rbLandscape.setEnabled(lSupported);
997N/A rbRevPortrait.setEnabled(rpSupported);
997N/A rbRevLandscape.setEnabled(rlSupported);
997N/A
997N/A OrientationRequested or = (OrientationRequested)asCurrent.get(orCategory);
997N/A if (or == null ||
997N/A !psCurrent.isAttributeValueSupported(or, docFlavor, asCurrent)) {
997N/A
997N/A or = (OrientationRequested)psCurrent.getDefaultAttributeValue(orCategory);
997N/A // need to validate if default is not supported
997N/A if ((or != null) &&
997N/A !psCurrent.isAttributeValueSupported(or, docFlavor, asCurrent)) {
997N/A or = null;
997N/A Object values =
997N/A psCurrent.getSupportedAttributeValues(orCategory,
997N/A docFlavor,
997N/A asCurrent);
997N/A if (values instanceof OrientationRequested[]) {
997N/A OrientationRequested[] orValues =
997N/A (OrientationRequested[])values;
997N/A if (orValues.length > 1) {
997N/A // get the first in the list
997N/A or = orValues[0];
0N/A }
0N/A }
997N/A }
997N/A
997N/A if (or == null) {
997N/A or = OrientationRequested.PORTRAIT;
0N/A }
997N/A asCurrent.add(or);
997N/A }
997N/A
997N/A if (or == OrientationRequested.PORTRAIT) {
997N/A rbPortrait.setSelected(true);
997N/A } else if (or == OrientationRequested.LANDSCAPE) {
997N/A rbLandscape.setSelected(true);
997N/A } else if (or == OrientationRequested.REVERSE_PORTRAIT) {
997N/A rbRevPortrait.setSelected(true);
997N/A } else { // if (or == OrientationRequested.REVERSE_LANDSCAPE)
997N/A rbRevLandscape.setSelected(true);
0N/A }
0N/A }
0N/A }
0N/A
0N/A
0N/A
0N/A /**
0N/A * The "Appearance" tab. Includes the controls for Chromaticity,
0N/A * PrintQuality, JobPriority, JobName, and other related job attributes.
0N/A */
0N/A private class AppearancePanel extends JPanel {
0N/A
0N/A private ChromaticityPanel pnlChromaticity;
0N/A private QualityPanel pnlQuality;
0N/A private JobAttributesPanel pnlJobAttributes;
0N/A private SidesPanel pnlSides;
0N/A
0N/A public AppearancePanel() {
0N/A super();
0N/A
0N/A GridBagLayout gridbag = new GridBagLayout();
0N/A GridBagConstraints c = new GridBagConstraints();
0N/A
0N/A setLayout(gridbag);
0N/A
0N/A c.fill = GridBagConstraints.BOTH;
0N/A c.insets = panelInsets;
0N/A c.weightx = 1.0;
0N/A c.weighty = 1.0;
0N/A
0N/A c.gridwidth = GridBagConstraints.RELATIVE;
0N/A pnlChromaticity = new ChromaticityPanel();
0N/A addToGB(pnlChromaticity, this, gridbag, c);
0N/A
0N/A c.gridwidth = GridBagConstraints.REMAINDER;
0N/A pnlQuality = new QualityPanel();
0N/A addToGB(pnlQuality, this, gridbag, c);
0N/A
0N/A c.gridwidth = 1;
0N/A pnlSides = new SidesPanel();
0N/A addToGB(pnlSides, this, gridbag, c);
0N/A
0N/A c.gridwidth = GridBagConstraints.REMAINDER;
0N/A pnlJobAttributes = new JobAttributesPanel();
0N/A addToGB(pnlJobAttributes, this, gridbag, c);
0N/A
0N/A }
0N/A
0N/A public void updateInfo() {
0N/A pnlChromaticity.updateInfo();
0N/A pnlQuality.updateInfo();
0N/A pnlSides.updateInfo();
0N/A pnlJobAttributes.updateInfo();
0N/A }
0N/A }
0N/A
0N/A private class ChromaticityPanel extends JPanel
0N/A implements ActionListener
0N/A {
0N/A private final String strTitle = getMsg("border.chromaticity");
0N/A private JRadioButton rbMonochrome, rbColor;
0N/A
0N/A public ChromaticityPanel() {
0N/A super();
0N/A
0N/A GridBagLayout gridbag = new GridBagLayout();
0N/A GridBagConstraints c = new GridBagConstraints();
0N/A
0N/A setLayout(gridbag);
0N/A setBorder(BorderFactory.createTitledBorder(strTitle));
0N/A
0N/A c.fill = GridBagConstraints.BOTH;
0N/A c.gridwidth = GridBagConstraints.REMAINDER;
0N/A c.weighty = 1.0;
0N/A
0N/A ButtonGroup bg = new ButtonGroup();
0N/A rbMonochrome = createRadioButton("radiobutton.monochrome", this);
0N/A rbMonochrome.setSelected(true);
0N/A bg.add(rbMonochrome);
0N/A addToGB(rbMonochrome, this, gridbag, c);
0N/A rbColor = createRadioButton("radiobutton.color", this);
0N/A bg.add(rbColor);
0N/A addToGB(rbColor, this, gridbag, c);
0N/A }
0N/A
0N/A public void actionPerformed(ActionEvent e) {
0N/A Object source = e.getSource();
0N/A
0N/A // REMIND: use isSameAs if we move to a IconRB in the future
0N/A if (source == rbMonochrome) {
0N/A asCurrent.add(Chromaticity.MONOCHROME);
0N/A } else if (source == rbColor) {
0N/A asCurrent.add(Chromaticity.COLOR);
0N/A }
0N/A }
0N/A
0N/A public void updateInfo() {
0N/A Class chCategory = Chromaticity.class;
0N/A boolean monoSupported = false;
0N/A boolean colorSupported = false;
0N/A
0N/A if (isAWT) {
0N/A monoSupported = true;
0N/A colorSupported = true;
0N/A } else
0N/A if (psCurrent.isAttributeCategorySupported(chCategory)) {
0N/A Object values =
0N/A psCurrent.getSupportedAttributeValues(chCategory,
0N/A docFlavor,
0N/A asCurrent);
0N/A
0N/A if (values instanceof Chromaticity[]) {
0N/A Chromaticity[] cvalues = (Chromaticity[])values;
0N/A
0N/A for (int i = 0; i < cvalues.length; i++) {
0N/A Chromaticity value = cvalues[i];
0N/A
0N/A if (value == Chromaticity.MONOCHROME) {
0N/A monoSupported = true;
0N/A } else if (value == Chromaticity.COLOR) {
0N/A colorSupported = true;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A
0N/A rbMonochrome.setEnabled(monoSupported);
0N/A rbColor.setEnabled(colorSupported);
0N/A
0N/A Chromaticity ch = (Chromaticity)asCurrent.get(chCategory);
0N/A if (ch == null) {
0N/A ch = (Chromaticity)psCurrent.getDefaultAttributeValue(chCategory);
0N/A if (ch == null) {
0N/A ch = Chromaticity.MONOCHROME;
0N/A }
0N/A }
0N/A
0N/A if (ch == Chromaticity.MONOCHROME) {
0N/A rbMonochrome.setSelected(true);
0N/A } else { // if (ch == Chromaticity.COLOR)
0N/A rbColor.setSelected(true);
0N/A }
0N/A }
0N/A }
0N/A
0N/A private class QualityPanel extends JPanel
0N/A implements ActionListener
0N/A {
0N/A private final String strTitle = getMsg("border.quality");
0N/A private JRadioButton rbDraft, rbNormal, rbHigh;
0N/A
0N/A public QualityPanel() {
0N/A super();
0N/A
0N/A GridBagLayout gridbag = new GridBagLayout();
0N/A GridBagConstraints c = new GridBagConstraints();
0N/A
0N/A setLayout(gridbag);
0N/A setBorder(BorderFactory.createTitledBorder(strTitle));
0N/A
0N/A c.fill = GridBagConstraints.BOTH;
0N/A c.gridwidth = GridBagConstraints.REMAINDER;
0N/A c.weighty = 1.0;
0N/A
0N/A ButtonGroup bg = new ButtonGroup();
0N/A rbDraft = createRadioButton("radiobutton.draftq", this);
0N/A bg.add(rbDraft);
0N/A addToGB(rbDraft, this, gridbag, c);
0N/A rbNormal = createRadioButton("radiobutton.normalq", this);
0N/A rbNormal.setSelected(true);
0N/A bg.add(rbNormal);
0N/A addToGB(rbNormal, this, gridbag, c);
0N/A rbHigh = createRadioButton("radiobutton.highq", this);
0N/A bg.add(rbHigh);
0N/A addToGB(rbHigh, this, gridbag, c);
0N/A }
0N/A
0N/A public void actionPerformed(ActionEvent e) {
0N/A Object source = e.getSource();
0N/A
0N/A if (source == rbDraft) {
0N/A asCurrent.add(PrintQuality.DRAFT);
0N/A } else if (source == rbNormal) {
0N/A asCurrent.add(PrintQuality.NORMAL);
0N/A } else if (source == rbHigh) {
0N/A asCurrent.add(PrintQuality.HIGH);
0N/A }
0N/A }
0N/A
0N/A public void updateInfo() {
0N/A Class pqCategory = PrintQuality.class;
0N/A boolean draftSupported = false;
0N/A boolean normalSupported = false;
0N/A boolean highSupported = false;
0N/A
0N/A if (isAWT) {
0N/A draftSupported = true;
0N/A normalSupported = true;
0N/A highSupported = true;
0N/A } else
0N/A if (psCurrent.isAttributeCategorySupported(pqCategory)) {
0N/A Object values =
0N/A psCurrent.getSupportedAttributeValues(pqCategory,
0N/A docFlavor,
0N/A asCurrent);
0N/A
0N/A if (values instanceof PrintQuality[]) {
0N/A PrintQuality[] qvalues = (PrintQuality[])values;
0N/A
0N/A for (int i = 0; i < qvalues.length; i++) {
0N/A PrintQuality value = qvalues[i];
0N/A
0N/A if (value == PrintQuality.DRAFT) {
0N/A draftSupported = true;
0N/A } else if (value == PrintQuality.NORMAL) {
0N/A normalSupported = true;
0N/A } else if (value == PrintQuality.HIGH) {
0N/A highSupported = true;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A rbDraft.setEnabled(draftSupported);
0N/A rbNormal.setEnabled(normalSupported);
0N/A rbHigh.setEnabled(highSupported);
0N/A
0N/A PrintQuality pq = (PrintQuality)asCurrent.get(pqCategory);
0N/A if (pq == null) {
0N/A pq = (PrintQuality)psCurrent.getDefaultAttributeValue(pqCategory);
0N/A if (pq == null) {
0N/A pq = PrintQuality.NORMAL;
0N/A }
0N/A }
0N/A
0N/A if (pq == PrintQuality.DRAFT) {
0N/A rbDraft.setSelected(true);
0N/A } else if (pq == PrintQuality.NORMAL) {
0N/A rbNormal.setSelected(true);
0N/A } else { // if (pq == PrintQuality.HIGH)
0N/A rbHigh.setSelected(true);
0N/A }
0N/A }
0N/A
0N/A
0N/A }
0N/A private class SidesPanel extends JPanel
0N/A implements ActionListener
0N/A {
0N/A private final String strTitle = getMsg("border.sides");
0N/A private IconRadioButton rbOneSide, rbTumble, rbDuplex;
0N/A
0N/A public SidesPanel() {
0N/A super();
0N/A
0N/A GridBagLayout gridbag = new GridBagLayout();
0N/A GridBagConstraints c = new GridBagConstraints();
0N/A
0N/A setLayout(gridbag);
0N/A setBorder(BorderFactory.createTitledBorder(strTitle));
0N/A
0N/A c.fill = GridBagConstraints.BOTH;
0N/A c.insets = compInsets;
0N/A c.weighty = 1.0;
0N/A c.gridwidth = GridBagConstraints.REMAINDER;
0N/A
0N/A ButtonGroup bg = new ButtonGroup();
0N/A rbOneSide = new IconRadioButton("radiobutton.oneside",
0N/A "oneside.png", true,
0N/A bg, this);
0N/A rbOneSide.addActionListener(this);
0N/A addToGB(rbOneSide, this, gridbag, c);
0N/A rbTumble = new IconRadioButton("radiobutton.tumble",
0N/A "tumble.png", false,
0N/A bg, this);
0N/A rbTumble.addActionListener(this);
0N/A addToGB(rbTumble, this, gridbag, c);
0N/A rbDuplex = new IconRadioButton("radiobutton.duplex",
0N/A "duplex.png", false,
0N/A bg, this);
0N/A rbDuplex.addActionListener(this);
0N/A c.gridwidth = GridBagConstraints.REMAINDER;
0N/A addToGB(rbDuplex, this, gridbag, c);
0N/A }
0N/A
0N/A public void actionPerformed(ActionEvent e) {
0N/A Object source = e.getSource();
0N/A
0N/A if (rbOneSide.isSameAs(source)) {
0N/A asCurrent.add(Sides.ONE_SIDED);
0N/A } else if (rbTumble.isSameAs(source)) {
0N/A asCurrent.add(Sides.TUMBLE);
0N/A } else if (rbDuplex.isSameAs(source)) {
0N/A asCurrent.add(Sides.DUPLEX);
0N/A }
0N/A }
0N/A
0N/A public void updateInfo() {
0N/A Class sdCategory = Sides.class;
0N/A boolean osSupported = false;
0N/A boolean tSupported = false;
0N/A boolean dSupported = false;
0N/A
0N/A if (psCurrent.isAttributeCategorySupported(sdCategory)) {
0N/A Object values =
0N/A psCurrent.getSupportedAttributeValues(sdCategory,
0N/A docFlavor,
0N/A asCurrent);
0N/A
0N/A if (values instanceof Sides[]) {
0N/A Sides[] svalues = (Sides[])values;
0N/A
0N/A for (int i = 0; i < svalues.length; i++) {
0N/A Sides value = svalues[i];
0N/A
0N/A if (value == Sides.ONE_SIDED) {
0N/A osSupported = true;
0N/A } else if (value == Sides.TUMBLE) {
0N/A tSupported = true;
0N/A } else if (value == Sides.DUPLEX) {
0N/A dSupported = true;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A rbOneSide.setEnabled(osSupported);
0N/A rbTumble.setEnabled(tSupported);
0N/A rbDuplex.setEnabled(dSupported);
0N/A
0N/A Sides sd = (Sides)asCurrent.get(sdCategory);
0N/A if (sd == null) {
0N/A sd = (Sides)psCurrent.getDefaultAttributeValue(sdCategory);
0N/A if (sd == null) {
0N/A sd = Sides.ONE_SIDED;
0N/A }
0N/A }
0N/A
0N/A if (sd == Sides.ONE_SIDED) {
0N/A rbOneSide.setSelected(true);
0N/A } else if (sd == Sides.TUMBLE) {
0N/A rbTumble.setSelected(true);
0N/A } else { // if (sd == Sides.DUPLEX)
0N/A rbDuplex.setSelected(true);
0N/A }
0N/A }
0N/A }
0N/A
0N/A
0N/A
0N/A private class JobAttributesPanel extends JPanel
0N/A implements ActionListener, ChangeListener, FocusListener
0N/A {
0N/A private final String strTitle = getMsg("border.jobattributes");
0N/A private JLabel lblPriority, lblJobName, lblUserName;
0N/A private JSpinner spinPriority;
0N/A private SpinnerNumberModel snModel;
0N/A private JCheckBox cbJobSheets;
0N/A private JTextField tfJobName, tfUserName;
0N/A
0N/A public JobAttributesPanel() {
0N/A super();
0N/A
0N/A GridBagLayout gridbag = new GridBagLayout();
0N/A GridBagConstraints c = new GridBagConstraints();
0N/A
0N/A setLayout(gridbag);
0N/A setBorder(BorderFactory.createTitledBorder(strTitle));
0N/A
0N/A c.fill = GridBagConstraints.NONE;
0N/A c.insets = compInsets;
0N/A c.weighty = 1.0;
0N/A
0N/A cbJobSheets = createCheckBox("checkbox.jobsheets", this);
0N/A c.anchor = GridBagConstraints.LINE_START;
0N/A addToGB(cbJobSheets, this, gridbag, c);
0N/A
0N/A JPanel pnlTop = new JPanel();
0N/A lblPriority = new JLabel(getMsg("label.priority"), JLabel.TRAILING);
0N/A lblPriority.setDisplayedMnemonic(getMnemonic("label.priority"));
0N/A
0N/A pnlTop.add(lblPriority);
0N/A snModel = new SpinnerNumberModel(1, 1, 100, 1);
0N/A spinPriority = new JSpinner(snModel);
0N/A lblPriority.setLabelFor(spinPriority);
0N/A // REMIND
0N/A ((JSpinner.NumberEditor)spinPriority.getEditor()).getTextField().setColumns(3);
0N/A spinPriority.addChangeListener(this);
0N/A pnlTop.add(spinPriority);
0N/A c.anchor = GridBagConstraints.LINE_END;
0N/A c.gridwidth = GridBagConstraints.REMAINDER;
0N/A pnlTop.getAccessibleContext().setAccessibleName(
0N/A getMsg("label.priority"));
0N/A addToGB(pnlTop, this, gridbag, c);
0N/A
0N/A c.fill = GridBagConstraints.HORIZONTAL;
0N/A c.anchor = GridBagConstraints.CENTER;
0N/A c.weightx = 0.0;
0N/A c.gridwidth = 1;
0N/A char jmnemonic = getMnemonic("label.jobname");
0N/A lblJobName = new JLabel(getMsg("label.jobname"), JLabel.TRAILING);
0N/A lblJobName.setDisplayedMnemonic(jmnemonic);
0N/A addToGB(lblJobName, this, gridbag, c);
0N/A c.weightx = 1.0;
0N/A c.gridwidth = GridBagConstraints.REMAINDER;
0N/A tfJobName = new JTextField();
0N/A lblJobName.setLabelFor(tfJobName);
0N/A tfJobName.addFocusListener(this);
0N/A tfJobName.setFocusAccelerator(jmnemonic);
0N/A tfJobName.getAccessibleContext().setAccessibleName(
0N/A getMsg("label.jobname"));
0N/A addToGB(tfJobName, this, gridbag, c);
0N/A
0N/A c.weightx = 0.0;
0N/A c.gridwidth = 1;
0N/A char umnemonic = getMnemonic("label.username");
0N/A lblUserName = new JLabel(getMsg("label.username"), JLabel.TRAILING);
0N/A lblUserName.setDisplayedMnemonic(umnemonic);
0N/A addToGB(lblUserName, this, gridbag, c);
0N/A c.gridwidth = GridBagConstraints.REMAINDER;
0N/A tfUserName = new JTextField();
0N/A lblUserName.setLabelFor(tfUserName);
0N/A tfUserName.addFocusListener(this);
0N/A tfUserName.setFocusAccelerator(umnemonic);
0N/A tfUserName.getAccessibleContext().setAccessibleName(
0N/A getMsg("label.username"));
0N/A addToGB(tfUserName, this, gridbag, c);
0N/A }
0N/A
0N/A public void actionPerformed(ActionEvent e) {
0N/A if (cbJobSheets.isSelected()) {
0N/A asCurrent.add(JobSheets.STANDARD);
0N/A } else {
0N/A asCurrent.add(JobSheets.NONE);
0N/A }
0N/A }
0N/A
0N/A public void stateChanged(ChangeEvent e) {
0N/A asCurrent.add(new JobPriority(snModel.getNumber().intValue()));
0N/A }
0N/A
0N/A public void focusLost(FocusEvent e) {
0N/A Object source = e.getSource();
0N/A
0N/A if (source == tfJobName) {
0N/A asCurrent.add(new JobName(tfJobName.getText(),
0N/A Locale.getDefault()));
0N/A } else if (source == tfUserName) {
0N/A asCurrent.add(new RequestingUserName(tfUserName.getText(),
0N/A Locale.getDefault()));
0N/A }
0N/A }
0N/A
0N/A public void focusGained(FocusEvent e) {}
0N/A
0N/A public void updateInfo() {
0N/A Class jsCategory = JobSheets.class;
0N/A Class jpCategory = JobPriority.class;
0N/A Class jnCategory = JobName.class;
0N/A Class unCategory = RequestingUserName.class;
0N/A boolean jsSupported = false;
0N/A boolean jpSupported = false;
0N/A boolean jnSupported = false;
0N/A boolean unSupported = false;
0N/A
0N/A // setup JobSheets checkbox
0N/A if (psCurrent.isAttributeCategorySupported(jsCategory)) {
0N/A jsSupported = true;
0N/A }
0N/A JobSheets js = (JobSheets)asCurrent.get(jsCategory);
0N/A if (js == null) {
0N/A js = (JobSheets)psCurrent.getDefaultAttributeValue(jsCategory);
0N/A if (js == null) {
0N/A js = JobSheets.NONE;
0N/A }
0N/A }
0N/A cbJobSheets.setSelected(js != JobSheets.NONE);
0N/A cbJobSheets.setEnabled(jsSupported);
0N/A
0N/A // setup JobPriority spinner
0N/A if (!isAWT && psCurrent.isAttributeCategorySupported(jpCategory)) {
0N/A jpSupported = true;
0N/A }
0N/A JobPriority jp = (JobPriority)asCurrent.get(jpCategory);
0N/A if (jp == null) {
0N/A jp = (JobPriority)psCurrent.getDefaultAttributeValue(jpCategory);
0N/A if (jp == null) {
0N/A jp = new JobPriority(1);
0N/A }
0N/A }
0N/A int value = jp.getValue();
0N/A if ((value < 1) || (value > 100)) {
0N/A value = 1;
0N/A }
0N/A snModel.setValue(new Integer(value));
0N/A lblPriority.setEnabled(jpSupported);
0N/A spinPriority.setEnabled(jpSupported);
0N/A
0N/A // setup JobName text field
0N/A if (psCurrent.isAttributeCategorySupported(jnCategory)) {
0N/A jnSupported = true;
0N/A }
0N/A JobName jn = (JobName)asCurrent.get(jnCategory);
0N/A if (jn == null) {
0N/A jn = (JobName)psCurrent.getDefaultAttributeValue(jnCategory);
0N/A if (jn == null) {
0N/A jn = new JobName("", Locale.getDefault());
0N/A }
0N/A }
0N/A tfJobName.setText(jn.getValue());
0N/A tfJobName.setEnabled(jnSupported);
0N/A lblJobName.setEnabled(jnSupported);
0N/A
0N/A // setup RequestingUserName text field
0N/A if (!isAWT && psCurrent.isAttributeCategorySupported(unCategory)) {
0N/A unSupported = true;
0N/A }
0N/A RequestingUserName un = (RequestingUserName)asCurrent.get(unCategory);
0N/A if (un == null) {
0N/A un = (RequestingUserName)psCurrent.getDefaultAttributeValue(unCategory);
0N/A if (un == null) {
0N/A un = new RequestingUserName("", Locale.getDefault());
0N/A }
0N/A }
0N/A tfUserName.setText(un.getValue());
0N/A tfUserName.setEnabled(unSupported);
0N/A lblUserName.setEnabled(unSupported);
0N/A }
0N/A }
0N/A
0N/A
0N/A
0N/A
0N/A /**
0N/A * A special widget that groups a JRadioButton with an associated icon,
0N/A * placed to the left of the radio button.
0N/A */
0N/A private class IconRadioButton extends JPanel {
0N/A
0N/A private JRadioButton rb;
0N/A private JLabel lbl;
0N/A
0N/A public IconRadioButton(String key, String img, boolean selected,
0N/A ButtonGroup bg, ActionListener al)
0N/A {
0N/A super(new FlowLayout(FlowLayout.LEADING));
0N/A final URL imgURL = getImageResource(img);
0N/A Icon icon = (Icon)java.security.AccessController.doPrivileged(
0N/A new java.security.PrivilegedAction() {
0N/A public Object run() {
0N/A Icon icon = new ImageIcon(imgURL);
0N/A return icon;
0N/A }
0N/A });
0N/A lbl = new JLabel(icon);
0N/A add(lbl);
0N/A
0N/A rb = createRadioButton(key, al);
0N/A rb.setSelected(selected);
0N/A addToBG(rb, this, bg);
0N/A }
0N/A
0N/A public void addActionListener(ActionListener al) {
0N/A rb.addActionListener(al);
0N/A }
0N/A
0N/A public boolean isSameAs(Object source) {
0N/A return (rb == source);
0N/A }
0N/A
0N/A public void setEnabled(boolean enabled) {
0N/A rb.setEnabled(enabled);
0N/A lbl.setEnabled(enabled);
0N/A }
0N/A
0N/A public boolean isSelected() {
0N/A return rb.isSelected();
0N/A }
0N/A
0N/A public void setSelected(boolean selected) {
0N/A rb.setSelected(selected);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Similar in functionality to the default JFileChooser, except this
0N/A * chooser will pop up a "Do you want to overwrite..." dialog if the
0N/A * user selects a file that already exists.
0N/A */
0N/A private class ValidatingFileChooser extends JFileChooser {
0N/A public void approveSelection() {
0N/A File selected = getSelectedFile();
0N/A boolean exists;
0N/A
0N/A try {
0N/A exists = selected.exists();
0N/A } catch (SecurityException e) {
0N/A exists = false;
0N/A }
0N/A
0N/A if (exists) {
0N/A int val;
0N/A val = JOptionPane.showConfirmDialog(this,
0N/A getMsg("dialog.overwrite"),
0N/A getMsg("dialog.owtitle"),
0N/A JOptionPane.YES_NO_OPTION);
0N/A if (val != JOptionPane.YES_OPTION) {
0N/A return;
0N/A }
0N/A }
0N/A
0N/A try {
0N/A if (selected.createNewFile()) {
0N/A selected.delete();
0N/A }
0N/A } catch (IOException ioe) {
0N/A JOptionPane.showMessageDialog(this,
0N/A getMsg("dialog.writeerror")+" "+selected,
0N/A getMsg("dialog.owtitle"),
0N/A JOptionPane.WARNING_MESSAGE);
0N/A return;
0N/A } catch (SecurityException se) {
0N/A //There is already file read/write access so at this point
0N/A // only delete access is denied. Just ignore it because in
0N/A // most cases the file created in createNewFile gets
0N/A // overwritten anyway.
0N/A }
0N/A File pFile = selected.getParentFile();
0N/A if ((selected.exists() &&
0N/A (!selected.isFile() || !selected.canWrite())) ||
0N/A ((pFile != null) &&
0N/A (!pFile.exists() || (pFile.exists() && !pFile.canWrite())))) {
0N/A JOptionPane.showMessageDialog(this,
0N/A getMsg("dialog.writeerror")+" "+selected,
0N/A getMsg("dialog.owtitle"),
0N/A JOptionPane.WARNING_MESSAGE);
0N/A return;
0N/A }
0N/A
0N/A super.approveSelection();
0N/A }
0N/A }
0N/A}