1 /*
   2  * Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.print;
  27 
  28 import java.awt.BorderLayout;
  29 import java.awt.Color;
  30 import java.awt.Component;
  31 import java.awt.Container;
  32 import java.awt.Dialog;
  33 import java.awt.FlowLayout;
  34 import java.awt.Frame;
  35 import java.awt.GraphicsConfiguration;
  36 import java.awt.GridBagLayout;
  37 import java.awt.GridBagConstraints;
  38 import java.awt.GridLayout;
  39 import java.awt.Insets;
  40 import java.awt.Toolkit;
  41 import java.awt.Window;
  42 import java.awt.event.ActionEvent;
  43 import java.awt.event.ActionListener;
  44 import java.awt.event.FocusEvent;
  45 import java.awt.event.FocusListener;
  46 import java.awt.event.ItemEvent;
  47 import java.awt.event.ItemListener;
  48 import java.awt.event.WindowEvent;
  49 import java.awt.event.WindowAdapter;
  50 import java.awt.print.PrinterJob;
  51 import java.io.File;
  52 import java.io.FilePermission;
  53 import java.io.IOException;
  54 import java.net.URI;
  55 import java.net.URL;
  56 import java.text.DecimalFormat;
  57 import java.util.Locale;
  58 import java.util.ResourceBundle;
  59 import java.util.Vector;
  60 import javax.print.*;
  61 import javax.print.attribute.*;
  62 import javax.print.attribute.standard.*;
  63 import javax.swing.*;
  64 import javax.swing.border.Border;
  65 import javax.swing.border.EmptyBorder;
  66 import javax.swing.border.TitledBorder;
  67 import javax.swing.event.ChangeEvent;
  68 import javax.swing.event.ChangeListener;
  69 import javax.swing.event.DocumentEvent;
  70 import javax.swing.event.DocumentListener;
  71 import javax.swing.event.PopupMenuEvent;
  72 import javax.swing.event.PopupMenuListener;
  73 import javax.swing.text.NumberFormatter;
  74 import sun.print.SunPageSelection;
  75 import java.awt.event.KeyEvent;
  76 import java.net.URISyntaxException;
  77 import java.lang.reflect.Field;
  78 import java.net.MalformedURLException;
  79 
  80 /**
  81  * A class which implements a cross-platform print dialog.
  82  *
  83  * @author  Chris Campbell
  84  */
  85 @SuppressWarnings("serial") // Superclass is not serializable across versions
  86 public class ServiceDialog extends JDialog implements ActionListener {
  87 
  88     /**
  89      * Waiting print status (user response pending).
  90      */
  91     public static final int WAITING = 0;
  92 
  93     /**
  94      * Approve print status (user activated "Print" or "OK").
  95      */
  96     public static final int APPROVE = 1;
  97 
  98     /**
  99      * Cancel print status (user activated "Cancel");
 100      */
 101     public static final int CANCEL = 2;
 102 
 103     private static final String strBundle = "sun.print.resources.serviceui";
 104     private static final Insets panelInsets = new Insets(6, 6, 6, 6);
 105     private static final Insets compInsets = new Insets(3, 6, 3, 6);
 106 
 107     private static ResourceBundle messageRB;
 108     private JTabbedPane tpTabs;
 109     private JButton btnCancel, btnApprove;
 110     private PrintService[] services;
 111     private int defaultServiceIndex;
 112     private PrintRequestAttributeSet asOriginal;
 113     private HashPrintRequestAttributeSet asCurrent;
 114     private PrintService psCurrent;
 115     private DocFlavor docFlavor;
 116     private int status;
 117 
 118     private ValidatingFileChooser jfc;
 119 
 120     private GeneralPanel pnlGeneral;
 121     private PageSetupPanel pnlPageSetup;
 122     private AppearancePanel pnlAppearance;
 123 
 124     private boolean isAWT = false;
 125     static {
 126         initResource();
 127     }
 128 
 129 
 130     /**
 131      * Constructor for the "standard" print dialog (containing all relevant
 132      * tabs)
 133      */
 134     public ServiceDialog(GraphicsConfiguration gc,
 135                          int x, int y,
 136                          PrintService[] services,
 137                          int defaultServiceIndex,
 138                          DocFlavor flavor,
 139                          PrintRequestAttributeSet attributes,
 140                          Window window)
 141     {
 142         super(window, getMsg("dialog.printtitle"), Dialog.DEFAULT_MODALITY_TYPE, gc);
 143         initPrintDialog(x, y, services, defaultServiceIndex,
 144                         flavor, attributes);
 145     }
 146 
 147     /**
 148      * Initialize print dialog.
 149      */
 150     void initPrintDialog(int x, int y,
 151                          PrintService[] services,
 152                          int defaultServiceIndex,
 153                          DocFlavor flavor,
 154                          PrintRequestAttributeSet attributes)
 155     {
 156         this.services = services;
 157         this.defaultServiceIndex = defaultServiceIndex;
 158         this.asOriginal = attributes;
 159         this.asCurrent = new HashPrintRequestAttributeSet(attributes);
 160         this.psCurrent = services[defaultServiceIndex];
 161         this.docFlavor = flavor;
 162         SunPageSelection pages =
 163             (SunPageSelection)attributes.get(SunPageSelection.class);
 164         if (pages != null) {
 165             isAWT = true;
 166         }
 167 
 168         if (attributes.get(DialogOwner.class) != null) {
 169             DialogOwner owner = (DialogOwner)attributes.get(DialogOwner.class);
 170             /* When the ServiceDialog is constructed the caller of the
 171              * constructor checks for this attribute and if it specifies a
 172              * window then it will use that in the constructor instead of
 173              * inferring one from keyboard focus.
 174              * In this case the owner of the dialog is the same as that
 175              * specified in the attribute and we do not need to set the
 176              * on top property
 177              */
 178             if ((getOwner() == null) || (owner.getOwner() != getOwner())) {
 179                 try {
 180                     setAlwaysOnTop(true);
 181                 } catch (SecurityException e) {
 182                 }
 183             }
 184         }
 185         Container c = getContentPane();
 186         c.setLayout(new BorderLayout());
 187 
 188         tpTabs = new JTabbedPane();
 189         tpTabs.setBorder(new EmptyBorder(5, 5, 5, 5));
 190 
 191         String gkey = getMsg("tab.general");
 192         int gmnemonic = getVKMnemonic("tab.general");
 193         pnlGeneral = new GeneralPanel();
 194         tpTabs.add(gkey, pnlGeneral);
 195         tpTabs.setMnemonicAt(0, gmnemonic);
 196 
 197         String pkey = getMsg("tab.pagesetup");
 198         int pmnemonic = getVKMnemonic("tab.pagesetup");
 199         pnlPageSetup = new PageSetupPanel();
 200         tpTabs.add(pkey, pnlPageSetup);
 201         tpTabs.setMnemonicAt(1, pmnemonic);
 202 
 203         String akey = getMsg("tab.appearance");
 204         int amnemonic = getVKMnemonic("tab.appearance");
 205         pnlAppearance = new AppearancePanel();
 206         tpTabs.add(akey, pnlAppearance);
 207         tpTabs.setMnemonicAt(2, amnemonic);
 208 
 209         c.add(tpTabs, BorderLayout.CENTER);
 210 
 211         updatePanels();
 212 
 213         JPanel pnlSouth = new JPanel(new FlowLayout(FlowLayout.TRAILING));
 214         btnApprove = createExitButton("button.print", this);
 215         pnlSouth.add(btnApprove);
 216         getRootPane().setDefaultButton(btnApprove);
 217         btnCancel = createExitButton("button.cancel", this);
 218         handleEscKey(btnCancel);
 219         pnlSouth.add(btnCancel);
 220         c.add(pnlSouth, BorderLayout.SOUTH);
 221 
 222         addWindowListener(new WindowAdapter() {
 223             public void windowClosing(WindowEvent event) {
 224                 dispose(CANCEL);
 225             }
 226         });
 227 
 228         getAccessibleContext().setAccessibleDescription(getMsg("dialog.printtitle"));
 229         setResizable(false);
 230         setLocation(x, y);
 231         pack();
 232     }
 233 
 234    /**
 235      * Constructor for the solitary "page setup" dialog
 236      */
 237     public ServiceDialog(GraphicsConfiguration gc,
 238                          int x, int y,
 239                          PrintService ps,
 240                          DocFlavor flavor,
 241                          PrintRequestAttributeSet attributes,
 242                          Window window)
 243     {
 244         super(window, getMsg("dialog.pstitle"), Dialog.DEFAULT_MODALITY_TYPE, gc);
 245         initPageDialog(x, y, ps, flavor, attributes);
 246     }
 247 
 248     /**
 249      * Initialize "page setup" dialog
 250      */
 251     void initPageDialog(int x, int y,
 252                          PrintService ps,
 253                          DocFlavor flavor,
 254                          PrintRequestAttributeSet attributes)
 255     {
 256         this.psCurrent = ps;
 257         this.docFlavor = flavor;
 258         this.asOriginal = attributes;
 259         this.asCurrent = new HashPrintRequestAttributeSet(attributes);
 260 
 261         if (attributes.get(DialogOwner.class) != null) {
 262             /* See comments in same block in initPrintDialog */
 263             DialogOwner owner = (DialogOwner)attributes.get(DialogOwner.class);
 264             if ((getOwner() == null) || (owner.getOwner() != getOwner())) {
 265                 try {
 266                     setAlwaysOnTop(true);
 267                 } catch (SecurityException e) {
 268                 }
 269             }
 270         }
 271 
 272         Container c = getContentPane();
 273         c.setLayout(new BorderLayout());
 274 
 275         pnlPageSetup = new PageSetupPanel();
 276         c.add(pnlPageSetup, BorderLayout.CENTER);
 277 
 278         pnlPageSetup.updateInfo();
 279 
 280         JPanel pnlSouth = new JPanel(new FlowLayout(FlowLayout.TRAILING));
 281         btnApprove = createExitButton("button.ok", this);
 282         pnlSouth.add(btnApprove);
 283         getRootPane().setDefaultButton(btnApprove);
 284         btnCancel = createExitButton("button.cancel", this);
 285         handleEscKey(btnCancel);
 286         pnlSouth.add(btnCancel);
 287         c.add(pnlSouth, BorderLayout.SOUTH);
 288 
 289         addWindowListener(new WindowAdapter() {
 290             public void windowClosing(WindowEvent event) {
 291                 dispose(CANCEL);
 292             }
 293         });
 294 
 295         getAccessibleContext().setAccessibleDescription(getMsg("dialog.pstitle"));
 296         setResizable(false);
 297         setLocation(x, y);
 298         pack();
 299     }
 300 
 301     /**
 302      * Performs Cancel when Esc key is pressed.
 303      */
 304     private void handleEscKey(JButton btnCancel) {
 305         @SuppressWarnings("serial") // anonymous class
 306         Action cancelKeyAction = new AbstractAction() {
 307             public void actionPerformed(ActionEvent e) {
 308                 dispose(CANCEL);
 309             }
 310         };
 311         KeyStroke cancelKeyStroke =
 312             KeyStroke.getKeyStroke((char)KeyEvent.VK_ESCAPE, 0);
 313         InputMap inputMap =
 314             btnCancel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
 315         ActionMap actionMap = btnCancel.getActionMap();
 316 
 317         if (inputMap != null && actionMap != null) {
 318             inputMap.put(cancelKeyStroke, "cancel");
 319             actionMap.put("cancel", cancelKeyAction);
 320         }
 321     }
 322 
 323 
 324     /**
 325      * Returns the current status of the dialog (whether the user has selected
 326      * the "Print" or "Cancel" button)
 327      */
 328     public int getStatus() {
 329         return status;
 330     }
 331 
 332     /**
 333      * Returns an AttributeSet based on whether or not the user cancelled the
 334      * dialog.  If the user selected "Print" we return their new selections,
 335      * otherwise we return the attributes that were passed in initially.
 336      */
 337     public PrintRequestAttributeSet getAttributes() {
 338         if (status == APPROVE) {
 339             return asCurrent;
 340         } else {
 341             return asOriginal;
 342         }
 343     }
 344 
 345     /**
 346      * Returns a PrintService based on whether or not the user cancelled the
 347      * dialog.  If the user selected "Print" we return the user's selection
 348      * for the PrintService, otherwise we return null.
 349      */
 350     public PrintService getPrintService() {
 351         if (status == APPROVE) {
 352             return psCurrent;
 353         } else {
 354             return null;
 355         }
 356     }
 357 
 358     /**
 359      * Sets the current status flag for the dialog and disposes it (thus
 360      * returning control of the parent frame back to the user)
 361      */
 362     public void dispose(int status) {
 363         this.status = status;
 364 
 365         super.dispose();
 366     }
 367 
 368     public void actionPerformed(ActionEvent e) {
 369         Object source = e.getSource();
 370         boolean approved = false;
 371 
 372         if (source == btnApprove) {
 373             approved = true;
 374 
 375             if (pnlGeneral != null) {
 376                 if (pnlGeneral.isPrintToFileRequested()) {
 377                     approved = showFileChooser();
 378                 } else {
 379                     asCurrent.remove(Destination.class);
 380                 }
 381             }
 382         }
 383 
 384         dispose(approved ? APPROVE : CANCEL);
 385     }
 386 
 387     /**
 388      * Displays a JFileChooser that allows the user to select the destination
 389      * for "Print To File"
 390      */
 391     private boolean showFileChooser() {
 392         Class<Destination> dstCategory = Destination.class;
 393 
 394         Destination dst = (Destination)asCurrent.get(dstCategory);
 395         if (dst == null) {
 396             dst = (Destination)asOriginal.get(dstCategory);
 397             if (dst == null) {
 398                 dst = (Destination)psCurrent.getDefaultAttributeValue(dstCategory);
 399                 // "dst" should not be null. The following code
 400                 // is only added to safeguard against a possible
 401                 // buggy implementation of a PrintService having a
 402                 // null default Destination.
 403                 if (dst == null) {
 404                     try {
 405                         dst = new Destination(new URI("file:out.prn"));
 406                     } catch (URISyntaxException e) {
 407                     }
 408                 }
 409             }
 410         }
 411 
 412         File fileDest;
 413         if (dst != null) {
 414             try {
 415                 fileDest = new File(dst.getURI());
 416             } catch (Exception e) {
 417                 // all manner of runtime exceptions possible
 418                 fileDest = new File("out.prn");
 419             }
 420         } else {
 421             fileDest = new File("out.prn");
 422         }
 423 
 424         ValidatingFileChooser jfc = new ValidatingFileChooser();
 425         jfc.setApproveButtonText(getMsg("button.ok"));
 426         jfc.setDialogTitle(getMsg("dialog.printtofile"));
 427         jfc.setDialogType(JFileChooser.SAVE_DIALOG);
 428         jfc.setSelectedFile(fileDest);
 429 
 430         int returnVal = jfc.showDialog(this, null);
 431         if (returnVal == JFileChooser.APPROVE_OPTION) {
 432             fileDest = jfc.getSelectedFile();
 433 
 434             try {
 435                 asCurrent.add(new Destination(fileDest.toURI()));
 436             } catch (Exception e) {
 437                 asCurrent.remove(dstCategory);
 438             }
 439         } else {
 440             asCurrent.remove(dstCategory);
 441         }
 442 
 443         return (returnVal == JFileChooser.APPROVE_OPTION);
 444     }
 445 
 446     /**
 447      * Updates each of the top level panels
 448      */
 449     private void updatePanels() {
 450         pnlGeneral.updateInfo();
 451         pnlPageSetup.updateInfo();
 452         pnlAppearance.updateInfo();
 453     }
 454 
 455     /**
 456      * Initialize ResourceBundle
 457      */
 458     public static void initResource() {
 459         java.security.AccessController.doPrivileged(
 460             new java.security.PrivilegedAction<Object>() {
 461                 public Object run() {
 462                     try {
 463                         messageRB = ResourceBundle.getBundle(strBundle);
 464                         return null;
 465                     } catch (java.util.MissingResourceException e) {
 466                         throw new Error("Fatal: Resource for ServiceUI " +
 467                                         "is missing");
 468                     }
 469                 }
 470             }
 471         );
 472     }
 473 
 474     /**
 475      * Returns message string from resource
 476      */
 477     public static String getMsg(String key) {
 478         try {
 479             return removeMnemonics(messageRB.getString(key));
 480         } catch (java.util.MissingResourceException e) {
 481             throw new Error("Fatal: Resource for ServiceUI is broken; " +
 482                             "there is no " + key + " key in resource");
 483         }
 484     }
 485 
 486     private static String removeMnemonics(String s) {
 487         int i = s.indexOf('&');
 488         int len = s.length();
 489         if (i < 0 || i == (len - 1)) {
 490             return s;
 491         }
 492         int j = s.indexOf('&', i+1);
 493         if (j == i+1) {
 494             if (j+1 == len) {
 495                 return s.substring(0, i+1);  // string ends with &&
 496             } else {
 497                 return s.substring(0, i+1) + removeMnemonics(s.substring(j+1));
 498             }
 499         }
 500         // ok first & not double &&
 501         if (i == 0) {
 502             return removeMnemonics(s.substring(1));
 503         } else {
 504             return (s.substring(0, i) + removeMnemonics(s.substring(i+1)));
 505         }
 506     }
 507 
 508 
 509     /**
 510      * Returns mnemonic character from resource
 511      */
 512     private static char getMnemonic(String key) {
 513         String str = messageRB.getString(key).replace("&&", "");
 514         int index = str.indexOf('&');
 515         if (0 <= index && index < str.length() - 1) {
 516             char c = str.charAt(index + 1);
 517             return Character.toUpperCase(c);
 518         } else {
 519             return (char)0;
 520         }
 521     }
 522 
 523     /**
 524      * Returns the mnemonic as a KeyEvent.VK constant from the resource.
 525      */
 526     static Class<?> _keyEventClazz = null;
 527     private static int getVKMnemonic(String key) {
 528         String s = String.valueOf(getMnemonic(key));
 529         if ( s == null || s.length() != 1) {
 530             return 0;
 531         }
 532         String vkString = "VK_" + s.toUpperCase();
 533 
 534         try {
 535             if (_keyEventClazz == null) {
 536                 _keyEventClazz= Class.forName("java.awt.event.KeyEvent",
 537                                  true, (ServiceDialog.class).getClassLoader());
 538             }
 539             Field field = _keyEventClazz.getDeclaredField(vkString);
 540             int value = field.getInt(null);
 541             return value;
 542         } catch (Exception e) {
 543         }
 544         return 0;
 545     }
 546 
 547     /**
 548      * Returns URL for image resource
 549      */
 550     private static URL getImageResource(final String key) {
 551         URL url = java.security.AccessController.doPrivileged(
 552                        new java.security.PrivilegedAction<URL>() {
 553                 public URL run() {
 554                     URL url = ServiceDialog.class.getResource(
 555                                                   "resources/" + key);
 556                     return url;
 557                 }
 558         });
 559 
 560         if (url == null) {
 561             throw new Error("Fatal: Resource for ServiceUI is broken; " +
 562                             "there is no " + key + " key in resource");
 563         }
 564 
 565         return url;
 566     }
 567 
 568     /**
 569      * Creates a new JButton and sets its text, mnemonic, and ActionListener
 570      */
 571     private static JButton createButton(String key, ActionListener al) {
 572         JButton btn = new JButton(getMsg(key));
 573         btn.setMnemonic(getMnemonic(key));
 574         btn.addActionListener(al);
 575 
 576         return btn;
 577     }
 578 
 579     /**
 580      * Creates a new JButton and sets its text, and ActionListener
 581      */
 582     private static JButton createExitButton(String key, ActionListener al) {
 583         String str = getMsg(key);
 584         JButton btn = new JButton(str);
 585         btn.addActionListener(al);
 586         btn.getAccessibleContext().setAccessibleDescription(str);
 587         return btn;
 588     }
 589 
 590     /**
 591      * Creates a new JCheckBox and sets its text, mnemonic, and ActionListener
 592      */
 593     private static JCheckBox createCheckBox(String key, ActionListener al) {
 594         JCheckBox cb = new JCheckBox(getMsg(key));
 595         cb.setMnemonic(getMnemonic(key));
 596         cb.addActionListener(al);
 597 
 598         return cb;
 599     }
 600 
 601     /**
 602      * Creates a new JRadioButton and sets its text, mnemonic,
 603      * and ActionListener
 604      */
 605     private static JRadioButton createRadioButton(String key,
 606                                                   ActionListener al)
 607     {
 608         JRadioButton rb = new JRadioButton(getMsg(key));
 609         rb.setMnemonic(getMnemonic(key));
 610         rb.addActionListener(al);
 611 
 612         return rb;
 613     }
 614 
 615   /**
 616    * Creates a  pop-up dialog for "no print service"
 617    */
 618     public static void showNoPrintService(GraphicsConfiguration gc)
 619     {
 620         Frame dlgFrame = new Frame(gc);
 621         JOptionPane.showMessageDialog(dlgFrame,
 622                                       getMsg("dialog.noprintermsg"));
 623         dlgFrame.dispose();
 624     }
 625 
 626     /**
 627      * Sets the constraints for the GridBagLayout and adds the Component
 628      * to the given Container
 629      */
 630     private static void addToGB(Component comp, Container cont,
 631                                 GridBagLayout gridbag,
 632                                 GridBagConstraints constraints)
 633     {
 634         gridbag.setConstraints(comp, constraints);
 635         cont.add(comp);
 636     }
 637 
 638     /**
 639      * Adds the AbstractButton to both the given ButtonGroup and Container
 640      */
 641     private static void addToBG(AbstractButton button, Container cont,
 642                                 ButtonGroup bg)
 643     {
 644         bg.add(button);
 645         cont.add(button);
 646     }
 647 
 648 
 649 
 650 
 651     /**
 652      * The "General" tab.  Includes the controls for PrintService,
 653      * PageRange, and Copies/Collate.
 654      */
 655     @SuppressWarnings("serial") // Superclass is not serializable across versions
 656     private class GeneralPanel extends JPanel {
 657 
 658         private PrintServicePanel pnlPrintService;
 659         private PrintRangePanel pnlPrintRange;
 660         private CopiesPanel pnlCopies;
 661 
 662         public GeneralPanel() {
 663             super();
 664 
 665             GridBagLayout gridbag = new GridBagLayout();
 666             GridBagConstraints c = new GridBagConstraints();
 667 
 668             setLayout(gridbag);
 669 
 670             c.fill = GridBagConstraints.BOTH;
 671             c.insets = panelInsets;
 672             c.weightx = 1.0;
 673             c.weighty = 1.0;
 674 
 675             c.gridwidth = GridBagConstraints.REMAINDER;
 676             pnlPrintService = new PrintServicePanel();
 677             addToGB(pnlPrintService, this, gridbag, c);
 678 
 679             c.gridwidth = GridBagConstraints.RELATIVE;
 680             pnlPrintRange = new PrintRangePanel();
 681             addToGB(pnlPrintRange, this, gridbag, c);
 682 
 683             c.gridwidth = GridBagConstraints.REMAINDER;
 684             pnlCopies = new CopiesPanel();
 685             addToGB(pnlCopies, this, gridbag, c);
 686         }
 687 
 688         public boolean isPrintToFileRequested() {
 689             return (pnlPrintService.isPrintToFileSelected());
 690         }
 691 
 692         public void updateInfo() {
 693             pnlPrintService.updateInfo();
 694             pnlPrintRange.updateInfo();
 695             pnlCopies.updateInfo();
 696         }
 697     }
 698 
 699     @SuppressWarnings("serial") // Superclass is not serializable across versions
 700     private class PrintServicePanel extends JPanel
 701         implements ActionListener, ItemListener, PopupMenuListener
 702     {
 703         private final String strTitle = getMsg("border.printservice");
 704         private FilePermission printToFilePermission;
 705         private JButton btnProperties;
 706         private JCheckBox cbPrintToFile;
 707         private JComboBox<String> cbName;
 708         private JLabel lblType, lblStatus, lblInfo;
 709         private ServiceUIFactory uiFactory;
 710         private boolean changedService = false;
 711         private boolean filePermission;
 712 
 713         public PrintServicePanel() {
 714             super();
 715 
 716             uiFactory = psCurrent.getServiceUIFactory();
 717 
 718             GridBagLayout gridbag = new GridBagLayout();
 719             GridBagConstraints c = new GridBagConstraints();
 720 
 721             setLayout(gridbag);
 722             setBorder(BorderFactory.createTitledBorder(strTitle));
 723 
 724             String[] psnames = new String[services.length];
 725             for (int i = 0; i < psnames.length; i++) {
 726                 psnames[i] = services[i].getName();
 727             }
 728             cbName = new JComboBox<>(psnames);
 729             cbName.setSelectedIndex(defaultServiceIndex);
 730             cbName.addItemListener(this);
 731             cbName.addPopupMenuListener(this);
 732 
 733             c.fill = GridBagConstraints.BOTH;
 734             c.insets = compInsets;
 735 
 736             c.weightx = 0.0;
 737             JLabel lblName = new JLabel(getMsg("label.psname"), JLabel.TRAILING);
 738             lblName.setDisplayedMnemonic(getMnemonic("label.psname"));
 739             lblName.setLabelFor(cbName);
 740             addToGB(lblName, this, gridbag, c);
 741             c.weightx = 1.0;
 742             c.gridwidth = GridBagConstraints.RELATIVE;
 743             addToGB(cbName, this, gridbag, c);
 744             c.weightx = 0.0;
 745             c.gridwidth = GridBagConstraints.REMAINDER;
 746             btnProperties = createButton("button.properties", this);
 747             addToGB(btnProperties, this, gridbag, c);
 748 
 749             c.weighty = 1.0;
 750             lblStatus = addLabel(getMsg("label.status"), gridbag, c);
 751             lblStatus.setLabelFor(null);
 752 
 753             lblType = addLabel(getMsg("label.pstype"), gridbag, c);
 754             lblType.setLabelFor(null);
 755 
 756             c.gridwidth = 1;
 757             addToGB(new JLabel(getMsg("label.info"), JLabel.TRAILING),
 758                     this, gridbag, c);
 759             c.gridwidth = GridBagConstraints.RELATIVE;
 760             lblInfo = new JLabel();
 761             lblInfo.setLabelFor(null);
 762 
 763             addToGB(lblInfo, this, gridbag, c);
 764 
 765             c.gridwidth = GridBagConstraints.REMAINDER;
 766             cbPrintToFile = createCheckBox("checkbox.printtofile", this);
 767             addToGB(cbPrintToFile, this, gridbag, c);
 768 
 769             filePermission = allowedToPrintToFile();
 770         }
 771 
 772         public boolean isPrintToFileSelected() {
 773             return cbPrintToFile.isSelected();
 774         }
 775 
 776         private JLabel addLabel(String text,
 777                                 GridBagLayout gridbag, GridBagConstraints c)
 778         {
 779             c.gridwidth = 1;
 780             addToGB(new JLabel(text, JLabel.TRAILING), this, gridbag, c);
 781 
 782             c.gridwidth = GridBagConstraints.REMAINDER;
 783             JLabel label = new JLabel();
 784             addToGB(label, this, gridbag, c);
 785 
 786             return label;
 787         }
 788 
 789         @SuppressWarnings("deprecation")
 790         public void actionPerformed(ActionEvent e) {
 791             Object source = e.getSource();
 792 
 793             if (source == btnProperties) {
 794                 if (uiFactory != null) {
 795                     JDialog dialog = (JDialog)uiFactory.getUI(
 796                                                ServiceUIFactory.MAIN_UIROLE,
 797                                                ServiceUIFactory.JDIALOG_UI);
 798 
 799                     if (dialog != null) {
 800                         dialog.show();
 801                     } else {
 802                         DocumentPropertiesUI docPropertiesUI = null;
 803                         try {
 804                             docPropertiesUI =
 805                                 (DocumentPropertiesUI)uiFactory.getUI
 806                                 (DocumentPropertiesUI.DOCUMENTPROPERTIES_ROLE,
 807                                  DocumentPropertiesUI.DOCPROPERTIESCLASSNAME);
 808                         } catch (Exception ex) {
 809                         }
 810                         if (docPropertiesUI != null) {
 811                             PrinterJobWrapper wrapper = (PrinterJobWrapper)
 812                                 asCurrent.get(PrinterJobWrapper.class);
 813                             if (wrapper == null) {
 814                                 return; // should not happen, defensive only.
 815                             }
 816                             PrinterJob job = wrapper.getPrinterJob();
 817                             if (job == null) {
 818                                 return;  // should not happen, defensive only.
 819                             }
 820                             PrintRequestAttributeSet newAttrs =
 821                                docPropertiesUI.showDocumentProperties
 822                                (job, ServiceDialog.this, psCurrent, asCurrent);
 823                             if (newAttrs != null) {
 824                                 asCurrent.addAll(newAttrs);
 825                                 updatePanels();
 826                             }
 827                         }
 828                     }
 829                 }
 830             }
 831         }
 832 
 833         public void itemStateChanged(ItemEvent e) {
 834             if (e.getStateChange() == ItemEvent.SELECTED) {
 835                 int index = cbName.getSelectedIndex();
 836 
 837                 if ((index >= 0) && (index < services.length)) {
 838                     if (!services[index].equals(psCurrent)) {
 839                         psCurrent = services[index];
 840                         uiFactory = psCurrent.getServiceUIFactory();
 841                         changedService = true;
 842 
 843                         Destination dest =
 844                             (Destination)asOriginal.get(Destination.class);
 845                         // to preserve the state of Print To File
 846                         if ((dest != null || isPrintToFileSelected())
 847                             && psCurrent.isAttributeCategorySupported(
 848                                                         Destination.class)) {
 849 
 850                             if (dest != null) {
 851                                 asCurrent.add(dest);
 852                             } else {
 853                                 dest = (Destination)psCurrent.
 854                                     getDefaultAttributeValue(Destination.class);
 855                                 // "dest" should not be null. The following code
 856                                 // is only added to safeguard against a possible
 857                                 // buggy implementation of a PrintService having a
 858                                 // null default Destination.
 859                                 if (dest == null) {
 860                                     try {
 861                                         dest =
 862                                             new Destination(new URI("file:out.prn"));
 863                                     } catch (URISyntaxException ue) {
 864                                     }
 865                                 }
 866 
 867                                 if (dest != null) {
 868                                     asCurrent.add(dest);
 869                                 }
 870                             }
 871                         } else {
 872                             asCurrent.remove(Destination.class);
 873                         }
 874                     }
 875                 }
 876             }
 877         }
 878 
 879         public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
 880             changedService = false;
 881         }
 882 
 883         public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
 884             if (changedService) {
 885                 changedService = false;
 886                 updatePanels();
 887             }
 888         }
 889 
 890         public void popupMenuCanceled(PopupMenuEvent e) {
 891         }
 892 
 893         /**
 894          * We disable the "Print To File" checkbox if this returns false
 895          */
 896         private boolean allowedToPrintToFile() {
 897             try {
 898                 throwPrintToFile();
 899                 return true;
 900             } catch (SecurityException e) {
 901                 return false;
 902             }
 903         }
 904 
 905         /**
 906          * Break this out as it may be useful when we allow API to
 907          * specify printing to a file. In that case its probably right
 908          * to throw a SecurityException if the permission is not granted.
 909          */
 910         private void throwPrintToFile() {
 911             SecurityManager security = System.getSecurityManager();
 912             if (security != null) {
 913                 if (printToFilePermission == null) {
 914                     printToFilePermission =
 915                         new FilePermission("<<ALL FILES>>", "read,write");
 916                 }
 917                 security.checkPermission(printToFilePermission);
 918             }
 919         }
 920 
 921         public void updateInfo() {
 922             Class<Destination> dstCategory = Destination.class;
 923             boolean dstSupported = false;
 924             boolean dstSelected = false;
 925             boolean dstAllowed = filePermission ?
 926                 allowedToPrintToFile() : false;
 927 
 928             // setup Destination (print-to-file) widgets
 929             Destination dst = (Destination)asCurrent.get(dstCategory);
 930             if (dst != null) {
 931                 try {
 932                      dst.getURI().toURL();
 933                      if (psCurrent.isAttributeValueSupported(dst, docFlavor,
 934                                                              asCurrent)) {
 935                          dstSupported = true;
 936                          dstSelected = true;
 937                      }
 938                  } catch (MalformedURLException ex) {
 939                      dstSupported = true;
 940                  }
 941             } else {
 942                 if (psCurrent.isAttributeCategorySupported(dstCategory)) {
 943                     dstSupported = true;
 944                 }
 945             }
 946             cbPrintToFile.setEnabled(dstSupported && dstAllowed);
 947             cbPrintToFile.setSelected(dstSelected && dstAllowed
 948                                       && dstSupported);
 949 
 950             // setup PrintService information widgets
 951             Attribute type = psCurrent.getAttribute(PrinterMakeAndModel.class);
 952             if (type != null) {
 953                 lblType.setText(type.toString());
 954             }
 955             Attribute status =
 956                 psCurrent.getAttribute(PrinterIsAcceptingJobs.class);
 957             if (status != null) {
 958                 lblStatus.setText(getMsg(status.toString()));
 959             }
 960             Attribute info = psCurrent.getAttribute(PrinterInfo.class);
 961             if (info != null) {
 962                 lblInfo.setText(info.toString());
 963             }
 964             PrinterJob job = null;
 965             PrinterJobWrapper wrapper = (PrinterJobWrapper)
 966                                         asCurrent.get(PrinterJobWrapper.class);
 967             if (wrapper != null) {
 968                 job = wrapper.getPrinterJob();
 969             }
 970             btnProperties.setEnabled(uiFactory != null &&  job != null);
 971         }
 972     }
 973 
 974     @SuppressWarnings("serial") // Superclass is not serializable across versions
 975     private class PrintRangePanel extends JPanel
 976         implements ActionListener, FocusListener
 977     {
 978         private final String strTitle = getMsg("border.printrange");
 979         private final PageRanges prAll = new PageRanges(1, Integer.MAX_VALUE);
 980         private JRadioButton rbAll, rbPages, rbSelect;
 981         private JFormattedTextField tfRangeFrom, tfRangeTo;
 982         private JLabel lblRangeTo;
 983         private boolean prSupported;
 984         private boolean prPgRngSupported;
 985 
 986         public PrintRangePanel() {
 987             super();
 988 
 989             GridBagLayout gridbag = new GridBagLayout();
 990             GridBagConstraints c = new GridBagConstraints();
 991 
 992             setLayout(gridbag);
 993             setBorder(BorderFactory.createTitledBorder(strTitle));
 994 
 995             c.fill = GridBagConstraints.BOTH;
 996             c.insets = compInsets;
 997             c.gridwidth = GridBagConstraints.REMAINDER;
 998 
 999             ButtonGroup bg = new ButtonGroup();
1000             JPanel pnlTop = new JPanel(new FlowLayout(FlowLayout.LEADING));
1001             rbAll = createRadioButton("radiobutton.rangeall", this);
1002             rbAll.setSelected(true);
1003             bg.add(rbAll);
1004             pnlTop.add(rbAll);
1005             addToGB(pnlTop, this, gridbag, c);
1006 
1007             // Selection never seemed to work so I'm commenting this part.
1008             /*
1009             if (isAWT) {
1010                 JPanel pnlMiddle  =
1011                     new JPanel(new FlowLayout(FlowLayout.LEADING));
1012                 rbSelect =
1013                     createRadioButton("radiobutton.selection", this);
1014                 bg.add(rbSelect);
1015                 pnlMiddle.add(rbSelect);
1016                 addToGB(pnlMiddle, this, gridbag, c);
1017             }
1018             */
1019 
1020             JPanel pnlBottom = new JPanel(new FlowLayout(FlowLayout.LEADING));
1021             rbPages = createRadioButton("radiobutton.rangepages", this);
1022             bg.add(rbPages);
1023             pnlBottom.add(rbPages);
1024             DecimalFormat format = new DecimalFormat("####0");
1025             format.setMinimumFractionDigits(0);
1026             format.setMaximumFractionDigits(0);
1027             format.setMinimumIntegerDigits(0);
1028             format.setMaximumIntegerDigits(5);
1029             format.setParseIntegerOnly(true);
1030             format.setDecimalSeparatorAlwaysShown(false);
1031             NumberFormatter nf = new NumberFormatter(format);
1032             nf.setMinimum(1);
1033             nf.setMaximum(Integer.MAX_VALUE);
1034             nf.setAllowsInvalid(true);
1035             nf.setCommitsOnValidEdit(true);
1036             tfRangeFrom = new JFormattedTextField(nf);
1037             tfRangeFrom.setColumns(4);
1038             tfRangeFrom.setEnabled(false);
1039             tfRangeFrom.addActionListener(this);
1040             tfRangeFrom.addFocusListener(this);
1041             tfRangeFrom.setFocusLostBehavior(
1042                 JFormattedTextField.PERSIST);
1043             tfRangeFrom.getAccessibleContext().setAccessibleName(
1044                                           getMsg("radiobutton.rangepages"));
1045             pnlBottom.add(tfRangeFrom);
1046             lblRangeTo = new JLabel(getMsg("label.rangeto"));
1047             lblRangeTo.setEnabled(false);
1048             pnlBottom.add(lblRangeTo);
1049             NumberFormatter nfto;
1050             try {
1051                 nfto = (NumberFormatter)nf.clone();
1052             } catch (CloneNotSupportedException e) {
1053                 nfto = new NumberFormatter();
1054             }
1055             tfRangeTo = new JFormattedTextField(nfto);
1056             tfRangeTo.setColumns(4);
1057             tfRangeTo.setEnabled(false);
1058             tfRangeTo.addFocusListener(this);
1059             tfRangeTo.getAccessibleContext().setAccessibleName(
1060                                           getMsg("label.rangeto"));
1061             pnlBottom.add(tfRangeTo);
1062             addToGB(pnlBottom, this, gridbag, c);
1063         }
1064 
1065         public void actionPerformed(ActionEvent e) {
1066             Object source = e.getSource();
1067             SunPageSelection select = SunPageSelection.ALL;
1068 
1069             setupRangeWidgets();
1070 
1071             if (source == rbAll) {
1072                 asCurrent.add(prAll);
1073             } else if (source == rbSelect) {
1074                 select = SunPageSelection.SELECTION;
1075             } else if (source == rbPages ||
1076                        source == tfRangeFrom ||
1077                        source == tfRangeTo) {
1078                 updateRangeAttribute();
1079                 select = SunPageSelection.RANGE;
1080             }
1081 
1082             if (isAWT) {
1083                 asCurrent.add(select);
1084             }
1085         }
1086 
1087         public void focusLost(FocusEvent e) {
1088             Object source = e.getSource();
1089 
1090             if ((source == tfRangeFrom) || (source == tfRangeTo)) {
1091                 updateRangeAttribute();
1092             }
1093         }
1094 
1095         public void focusGained(FocusEvent e) {}
1096 
1097         private void setupRangeWidgets() {
1098             boolean rangeEnabled = (rbPages.isSelected() && prPgRngSupported);
1099             tfRangeFrom.setEnabled(rangeEnabled);
1100             tfRangeTo.setEnabled(rangeEnabled);
1101             lblRangeTo.setEnabled(rangeEnabled);
1102         }
1103 
1104         private void updateRangeAttribute() {
1105             String strFrom = tfRangeFrom.getText();
1106             String strTo = tfRangeTo.getText();
1107 
1108             int min;
1109             int max;
1110 
1111             try {
1112                 min = Integer.parseInt(strFrom);
1113             } catch (NumberFormatException e) {
1114                 min = 1;
1115             }
1116 
1117             try {
1118                 max = Integer.parseInt(strTo);
1119             } catch (NumberFormatException e) {
1120                 max = min;
1121             }
1122 
1123             if (min < 1) {
1124                 min = 1;
1125                 tfRangeFrom.setValue(1);
1126             }
1127 
1128             if (max < min) {
1129                 max = min;
1130                 tfRangeTo.setValue(min);
1131             }
1132 
1133             PageRanges pr = new PageRanges(min, max);
1134             asCurrent.add(pr);
1135         }
1136 
1137         public void updateInfo() {
1138             Class<PageRanges> prCategory = PageRanges.class;
1139             prSupported = false;
1140 
1141             if (psCurrent.isAttributeCategorySupported(prCategory) ||
1142                    isAWT) {
1143                 prSupported = true;
1144                 prPgRngSupported = psCurrent.isAttributeValueSupported(prAll,
1145                                                                      docFlavor,
1146                                                                      asCurrent);
1147             }
1148 
1149             SunPageSelection select = SunPageSelection.ALL;
1150             int min = 1;
1151             int max = 1;
1152 
1153             PageRanges pr = (PageRanges)asCurrent.get(prCategory);
1154             if (pr != null) {
1155                 if (!pr.equals(prAll)) {
1156                     select = SunPageSelection.RANGE;
1157 
1158                     int[][] members = pr.getMembers();
1159                     if ((members.length > 0) &&
1160                         (members[0].length > 1)) {
1161                         min = members[0][0];
1162                         max = members[0][1];
1163                     }
1164                 }
1165             }
1166 
1167             if (isAWT) {
1168                 select = (SunPageSelection)asCurrent.get(
1169                                                 SunPageSelection.class);
1170             }
1171 
1172             if (select == SunPageSelection.ALL) {
1173                 rbAll.setSelected(true);
1174             } else if (select == SunPageSelection.SELECTION) {
1175                 // Comment this for now -  rbSelect is not initialized
1176                 // because Selection button is not added.
1177                 // See PrintRangePanel above.
1178 
1179                 //rbSelect.setSelected(true);
1180             } else { // RANGE
1181                 rbPages.setSelected(true);
1182             }
1183             tfRangeFrom.setValue(min);
1184             tfRangeTo.setValue(max);
1185             rbAll.setEnabled(prSupported);
1186             rbPages.setEnabled(prPgRngSupported);
1187             setupRangeWidgets();
1188         }
1189     }
1190 
1191     @SuppressWarnings("serial") // Superclass is not serializable across versions
1192     private class CopiesPanel extends JPanel
1193         implements ActionListener, ChangeListener
1194     {
1195         private final String strTitle = getMsg("border.copies");
1196         private SpinnerNumberModel snModel;
1197         private JSpinner spinCopies;
1198         private JLabel lblCopies;
1199         private JCheckBox cbCollate;
1200         private boolean scSupported;
1201 
1202         public CopiesPanel() {
1203             super();
1204 
1205             GridBagLayout gridbag = new GridBagLayout();
1206             GridBagConstraints c = new GridBagConstraints();
1207 
1208             setLayout(gridbag);
1209             setBorder(BorderFactory.createTitledBorder(strTitle));
1210 
1211             c.fill = GridBagConstraints.HORIZONTAL;
1212             c.insets = compInsets;
1213 
1214             lblCopies = new JLabel(getMsg("label.numcopies"), JLabel.TRAILING);
1215             lblCopies.setDisplayedMnemonic(getMnemonic("label.numcopies"));
1216             lblCopies.getAccessibleContext().setAccessibleName(
1217                                              getMsg("label.numcopies"));
1218             addToGB(lblCopies, this, gridbag, c);
1219 
1220             snModel = new SpinnerNumberModel(1, 1, 999, 1);
1221             spinCopies = new JSpinner(snModel);
1222             lblCopies.setLabelFor(spinCopies);
1223             // REMIND
1224             ((JSpinner.NumberEditor)spinCopies.getEditor()).getTextField().setColumns(3);
1225             spinCopies.addChangeListener(this);
1226             c.gridwidth = GridBagConstraints.REMAINDER;
1227             addToGB(spinCopies, this, gridbag, c);
1228 
1229             cbCollate = createCheckBox("checkbox.collate", this);
1230             cbCollate.setEnabled(false);
1231             addToGB(cbCollate, this, gridbag, c);
1232         }
1233 
1234         public void actionPerformed(ActionEvent e) {
1235             if (cbCollate.isSelected()) {
1236                 asCurrent.add(SheetCollate.COLLATED);
1237             } else {
1238                 asCurrent.add(SheetCollate.UNCOLLATED);
1239             }
1240         }
1241 
1242         public void stateChanged(ChangeEvent e) {
1243             updateCollateCB();
1244 
1245             asCurrent.add(new Copies(snModel.getNumber().intValue()));
1246         }
1247 
1248         private void updateCollateCB() {
1249             int num = snModel.getNumber().intValue();
1250             if (isAWT) {
1251                 cbCollate.setEnabled(true);
1252             } else {
1253                 cbCollate.setEnabled((num > 1) && scSupported);
1254             }
1255         }
1256 
1257         public void updateInfo() {
1258             Class<Copies> cpCategory = Copies.class;
1259             Class<SheetCollate> scCategory = SheetCollate.class;
1260             boolean cpSupported = false;
1261             scSupported = false;
1262 
1263             // setup Copies spinner
1264             if (psCurrent.isAttributeCategorySupported(cpCategory)) {
1265                 cpSupported = true;
1266             }
1267             CopiesSupported cs =
1268                 (CopiesSupported)psCurrent.getSupportedAttributeValues(
1269                                                        cpCategory, null, null);
1270             if (cs == null) {
1271                 cs = new CopiesSupported(1, 999);
1272             }
1273             Copies cp = (Copies)asCurrent.get(cpCategory);
1274             if (cp == null) {
1275                 cp = (Copies)psCurrent.getDefaultAttributeValue(cpCategory);
1276                 if (cp == null) {
1277                     cp = new Copies(1);
1278                 }
1279             }
1280             spinCopies.setEnabled(cpSupported);
1281             lblCopies.setEnabled(cpSupported);
1282 
1283             int[][] members = cs.getMembers();
1284             int min, max;
1285             if ((members.length > 0) && (members[0].length > 0)) {
1286                 min = members[0][0];
1287                 max = members[0][1];
1288             } else {
1289                 min = 1;
1290                 max = Integer.MAX_VALUE;
1291             }
1292             snModel.setMinimum(min);
1293             snModel.setMaximum(max);
1294 
1295             int value = cp.getValue();
1296             if ((value < min) || (value > max)) {
1297                 value = min;
1298             }
1299             snModel.setValue(value);
1300 
1301             // setup Collate checkbox
1302             if (psCurrent.isAttributeCategorySupported(scCategory)) {
1303                 scSupported = true;
1304             }
1305             SheetCollate sc = (SheetCollate)asCurrent.get(scCategory);
1306             if (sc == null) {
1307                 sc = (SheetCollate)psCurrent.getDefaultAttributeValue(scCategory);
1308                 if (sc == null) {
1309                     sc = SheetCollate.UNCOLLATED;
1310                 }
1311                 if (sc != null &&
1312                     !psCurrent.isAttributeValueSupported(sc, docFlavor, asCurrent)) {
1313                     scSupported = false;
1314                 }
1315             } else {
1316                 if (!psCurrent.isAttributeValueSupported(sc, docFlavor, asCurrent)) {
1317                     scSupported = false;
1318                 }
1319             }
1320             cbCollate.setSelected(sc == SheetCollate.COLLATED && scSupported);
1321             updateCollateCB();
1322         }
1323     }
1324 
1325 
1326 
1327 
1328     /**
1329      * The "Page Setup" tab.  Includes the controls for MediaSource/MediaTray,
1330      * OrientationRequested, and Sides.
1331      */
1332     @SuppressWarnings("serial") // Superclass is not serializable across versions
1333     private class PageSetupPanel extends JPanel {
1334 
1335         private MediaPanel pnlMedia;
1336         private OrientationPanel pnlOrientation;
1337         private MarginsPanel pnlMargins;
1338 
1339         public PageSetupPanel() {
1340             super();
1341 
1342             GridBagLayout gridbag = new GridBagLayout();
1343             GridBagConstraints c = new GridBagConstraints();
1344 
1345             setLayout(gridbag);
1346 
1347             c.fill = GridBagConstraints.BOTH;
1348             c.insets = panelInsets;
1349             c.weightx = 1.0;
1350             c.weighty = 1.0;
1351 
1352             c.gridwidth = GridBagConstraints.REMAINDER;
1353             pnlMedia = new MediaPanel();
1354             addToGB(pnlMedia, this, gridbag, c);
1355 
1356             pnlOrientation = new OrientationPanel();
1357             c.gridwidth = GridBagConstraints.RELATIVE;
1358             addToGB(pnlOrientation, this, gridbag, c);
1359 
1360             pnlMargins = new MarginsPanel();
1361             pnlOrientation.addOrientationListener(pnlMargins);
1362             pnlMedia.addMediaListener(pnlMargins);
1363             c.gridwidth = GridBagConstraints.REMAINDER;
1364             addToGB(pnlMargins, this, gridbag, c);
1365         }
1366 
1367         public void updateInfo() {
1368             pnlMedia.updateInfo();
1369             pnlOrientation.updateInfo();
1370             pnlMargins.updateInfo();
1371         }
1372     }
1373 
1374     @SuppressWarnings("serial") // Superclass is not serializable across versions
1375     private class MarginsPanel extends JPanel
1376                                implements ActionListener, FocusListener {
1377 
1378         private final String strTitle = getMsg("border.margins");
1379         private JFormattedTextField leftMargin, rightMargin,
1380                                     topMargin, bottomMargin;
1381         private JLabel lblLeft, lblRight, lblTop, lblBottom;
1382         private int units = MediaPrintableArea.MM;
1383         // storage for the last margin values calculated, -ve is uninitialised
1384         private float lmVal = -1f,rmVal = -1f, tmVal = -1f, bmVal = -1f;
1385         // storage for margins as objects mapped into orientation for display
1386         private Float lmObj,rmObj,tmObj,bmObj;
1387 
1388         public MarginsPanel() {
1389             super();
1390 
1391             GridBagLayout gridbag = new GridBagLayout();
1392             GridBagConstraints c = new GridBagConstraints();
1393             c.fill = GridBagConstraints.HORIZONTAL;
1394             c.weightx = 1.0;
1395             c.weighty = 0.0;
1396             c.insets = compInsets;
1397 
1398             setLayout(gridbag);
1399             setBorder(BorderFactory.createTitledBorder(strTitle));
1400 
1401             String unitsKey = "label.millimetres";
1402             String defaultCountry = Locale.getDefault().getCountry();
1403             if (defaultCountry != null &&
1404                 (defaultCountry.isEmpty() ||
1405                  defaultCountry.equals(Locale.US.getCountry()) ||
1406                  defaultCountry.equals(Locale.CANADA.getCountry()))) {
1407                 unitsKey = "label.inches";
1408                 units = MediaPrintableArea.INCH;
1409             }
1410             String unitsMsg = getMsg(unitsKey);
1411 
1412             DecimalFormat format;
1413             if (units == MediaPrintableArea.MM) {
1414                 format = new DecimalFormat("###.##");
1415                 format.setMaximumIntegerDigits(3);
1416             } else {
1417                 format = new DecimalFormat("##.##");
1418                 format.setMaximumIntegerDigits(2);
1419             }
1420 
1421             format.setMinimumFractionDigits(1);
1422             format.setMaximumFractionDigits(2);
1423             format.setMinimumIntegerDigits(1);
1424             format.setParseIntegerOnly(false);
1425             format.setDecimalSeparatorAlwaysShown(true);
1426             NumberFormatter nf = new NumberFormatter(format);
1427             nf.setMinimum(Float.valueOf(0.0f));
1428             nf.setMaximum(Float.valueOf(999.0f));
1429             nf.setAllowsInvalid(true);
1430             nf.setCommitsOnValidEdit(true);
1431 
1432             leftMargin = new JFormattedTextField(nf);
1433             leftMargin.addFocusListener(this);
1434             leftMargin.addActionListener(this);
1435             leftMargin.getAccessibleContext().setAccessibleName(
1436                                               getMsg("label.leftmargin"));
1437             rightMargin = new JFormattedTextField(nf);
1438             rightMargin.addFocusListener(this);
1439             rightMargin.addActionListener(this);
1440             rightMargin.getAccessibleContext().setAccessibleName(
1441                                               getMsg("label.rightmargin"));
1442             topMargin = new JFormattedTextField(nf);
1443             topMargin.addFocusListener(this);
1444             topMargin.addActionListener(this);
1445             topMargin.getAccessibleContext().setAccessibleName(
1446                                               getMsg("label.topmargin"));
1447 
1448             bottomMargin = new JFormattedTextField(nf);
1449             bottomMargin.addFocusListener(this);
1450             bottomMargin.addActionListener(this);
1451             bottomMargin.getAccessibleContext().setAccessibleName(
1452                                               getMsg("label.bottommargin"));
1453 
1454             c.gridwidth = GridBagConstraints.RELATIVE;
1455             lblLeft = new JLabel(getMsg("label.leftmargin") + " " + unitsMsg,
1456                                  JLabel.LEADING);
1457             lblLeft.setDisplayedMnemonic(getMnemonic("label.leftmargin"));
1458             lblLeft.setLabelFor(leftMargin);
1459             addToGB(lblLeft, this, gridbag, c);
1460 
1461             c.gridwidth = GridBagConstraints.REMAINDER;
1462             lblRight = new JLabel(getMsg("label.rightmargin") + " " + unitsMsg,
1463                                   JLabel.LEADING);
1464             lblRight.setDisplayedMnemonic(getMnemonic("label.rightmargin"));
1465             lblRight.setLabelFor(rightMargin);
1466             addToGB(lblRight, this, gridbag, c);
1467 
1468             c.gridwidth = GridBagConstraints.RELATIVE;
1469             addToGB(leftMargin, this, gridbag, c);
1470 
1471             c.gridwidth = GridBagConstraints.REMAINDER;
1472             addToGB(rightMargin, this, gridbag, c);
1473 
1474             // add an invisible spacing component.
1475             addToGB(new JPanel(), this, gridbag, c);
1476 
1477             c.gridwidth = GridBagConstraints.RELATIVE;
1478             lblTop = new JLabel(getMsg("label.topmargin") + " " + unitsMsg,
1479                                 JLabel.LEADING);
1480             lblTop.setDisplayedMnemonic(getMnemonic("label.topmargin"));
1481             lblTop.setLabelFor(topMargin);
1482             addToGB(lblTop, this, gridbag, c);
1483 
1484             c.gridwidth = GridBagConstraints.REMAINDER;
1485             lblBottom = new JLabel(getMsg("label.bottommargin") +
1486                                    " " + unitsMsg, JLabel.LEADING);
1487             lblBottom.setDisplayedMnemonic(getMnemonic("label.bottommargin"));
1488             lblBottom.setLabelFor(bottomMargin);
1489             addToGB(lblBottom, this, gridbag, c);
1490 
1491             c.gridwidth = GridBagConstraints.RELATIVE;
1492             addToGB(topMargin, this, gridbag, c);
1493 
1494             c.gridwidth = GridBagConstraints.REMAINDER;
1495             addToGB(bottomMargin, this, gridbag, c);
1496 
1497         }
1498 
1499         public void actionPerformed(ActionEvent e) {
1500             Object source = e.getSource();
1501             updateMargins(source);
1502         }
1503 
1504         public void focusLost(FocusEvent e) {
1505             Object source = e.getSource();
1506             updateMargins(source);
1507         }
1508 
1509         public void focusGained(FocusEvent e) {}
1510 
1511         /* Get the numbers, use to create a MPA.
1512          * If its valid, accept it and update the attribute set.
1513          * If its not valid, then reject it and call updateInfo()
1514          * to re-establish the previous entries.
1515          */
1516         public void updateMargins(Object source) {
1517             if (!(source instanceof JFormattedTextField)) {
1518                 return;
1519             } else {
1520                 JFormattedTextField tf = (JFormattedTextField)source;
1521                 Float val = (Float)tf.getValue();
1522                 if (val == null) {
1523                     return;
1524                 }
1525                 if (tf == leftMargin && val.equals(lmObj)) {
1526                     return;
1527                 }
1528                 if (tf == rightMargin && val.equals(rmObj)) {
1529                     return;
1530                 }
1531                 if (tf == topMargin && val.equals(tmObj)) {
1532                     return;
1533                 }
1534                 if (tf == bottomMargin && val.equals(bmObj)) {
1535                     return;
1536                 }
1537             }
1538 
1539             Float lmTmpObj = (Float)leftMargin.getValue();
1540             Float rmTmpObj = (Float)rightMargin.getValue();
1541             Float tmTmpObj = (Float)topMargin.getValue();
1542             Float bmTmpObj = (Float)bottomMargin.getValue();
1543 
1544             float lm = lmTmpObj.floatValue();
1545             float rm = rmTmpObj.floatValue();
1546             float tm = tmTmpObj.floatValue();
1547             float bm = bmTmpObj.floatValue();
1548 
1549             /* adjust for orientation */
1550             Class<OrientationRequested> orCategory = OrientationRequested.class;
1551             OrientationRequested or =
1552                 (OrientationRequested)asCurrent.get(orCategory);
1553 
1554             if (or == null) {
1555                 or = (OrientationRequested)
1556                      psCurrent.getDefaultAttributeValue(orCategory);
1557             }
1558 
1559             float tmp;
1560             if (or == OrientationRequested.REVERSE_PORTRAIT) {
1561                 tmp = lm; lm = rm; rm = tmp;
1562                 tmp = tm; tm = bm; bm = tmp;
1563             } else if (or == OrientationRequested.LANDSCAPE) {
1564                 tmp = lm;
1565                 lm = tm;
1566                 tm = rm;
1567                 rm = bm;
1568                 bm = tmp;
1569             } else if (or == OrientationRequested.REVERSE_LANDSCAPE) {
1570                 tmp = lm;
1571                 lm = bm;
1572                 bm = rm;
1573                 rm = tm;
1574                 tm = tmp;
1575             }
1576             MediaPrintableArea mpa;
1577             if ((mpa = validateMargins(lm, rm, tm, bm)) != null) {
1578                 asCurrent.add(mpa);
1579                 lmVal = lm;
1580                 rmVal = rm;
1581                 tmVal = tm;
1582                 bmVal = bm;
1583                 lmObj = lmTmpObj;
1584                 rmObj = rmTmpObj;
1585                 tmObj = tmTmpObj;
1586                 bmObj = bmTmpObj;
1587             } else {
1588                 if (lmObj == null || rmObj == null ||
1589                     tmObj == null || bmObj == null) {
1590                     return;
1591                 } else {
1592                     leftMargin.setValue(lmObj);
1593                     rightMargin.setValue(rmObj);
1594                     topMargin.setValue(tmObj);
1595                     bottomMargin.setValue(bmObj);
1596 
1597                 }
1598             }
1599         }
1600 
1601         /*
1602          * This method either accepts the values and creates a new
1603          * MediaPrintableArea, or does nothing.
1604          * It should not attempt to create a printable area from anything
1605          * other than the exact values passed in.
1606          * But REMIND/TBD: it would be user friendly to replace margins the
1607          * user entered but are out of bounds with the minimum.
1608          * At that point this method will need to take responsibility for
1609          * updating the "stored" values and the UI.
1610          */
1611         private MediaPrintableArea validateMargins(float lm, float rm,
1612                                                    float tm, float bm) {
1613 
1614             Class<MediaPrintableArea> mpaCategory = MediaPrintableArea.class;
1615             MediaPrintableArea mpa;
1616             MediaPrintableArea mpaMax = null;
1617             MediaSize mediaSize = null;
1618 
1619             Media media = (Media)asCurrent.get(Media.class);
1620             if (media == null || !(media instanceof MediaSizeName)) {
1621                 media = (Media)psCurrent.getDefaultAttributeValue(Media.class);
1622             }
1623             if (media != null && (media instanceof MediaSizeName)) {
1624                 MediaSizeName msn = (MediaSizeName)media;
1625                 mediaSize = MediaSize.getMediaSizeForName(msn);
1626             }
1627             if (mediaSize == null) {
1628                 mediaSize = new MediaSize(8.5f, 11f, Size2DSyntax.INCH);
1629             }
1630 
1631             if (media != null) {
1632                 PrintRequestAttributeSet tmpASet =
1633                     new HashPrintRequestAttributeSet(asCurrent);
1634                 tmpASet.add(media);
1635 
1636                 Object values =
1637                     psCurrent.getSupportedAttributeValues(mpaCategory,
1638                                                           docFlavor,
1639                                                           tmpASet);
1640                 if (values instanceof MediaPrintableArea[] &&
1641                     ((MediaPrintableArea[])values).length > 0) {
1642                     mpaMax = ((MediaPrintableArea[])values)[0];
1643 
1644                 }
1645             }
1646             if (mpaMax == null) {
1647                 mpaMax = new MediaPrintableArea(0f, 0f,
1648                                                 mediaSize.getX(units),
1649                                                 mediaSize.getY(units),
1650                                                 units);
1651             }
1652 
1653             float wid = mediaSize.getX(units);
1654             float hgt = mediaSize.getY(units);
1655             float pax = lm;
1656             float pay = tm;
1657             float par = rm;
1658             float pab = bm;
1659             float paw = wid - lm - rm;
1660             float pah = hgt - tm - bm;
1661 
1662             if (paw <= 0f || pah <= 0f || pax < 0f || pay < 0f ||
1663                 par <= 0f || pab <= 0f ||
1664                 pax < mpaMax.getX(units) || paw > mpaMax.getWidth(units) ||
1665                 pay < mpaMax.getY(units) || pah > mpaMax.getHeight(units)) {
1666                 return null;
1667             } else {
1668                 return new MediaPrintableArea(lm, tm, paw, pah, units);
1669             }
1670         }
1671 
1672         /* This is complex as a MediaPrintableArea is valid only within
1673          * a particular context of media size.
1674          * So we need a MediaSize as well as a MediaPrintableArea.
1675          * MediaSize can be obtained from MediaSizeName.
1676          * If the application specifies a MediaPrintableArea, we accept it
1677          * to the extent its valid for the Media they specify. If they
1678          * don't specify a Media, then the default is assumed.
1679          *
1680          * If an application doesn't define a MediaPrintableArea, we need to
1681          * create a suitable one, this is created using the specified (or
1682          * default) Media and default 1 inch margins. This is validated
1683          * against the paper in case this is too large for tiny media.
1684          */
1685         public void updateInfo() {
1686 
1687             if (isAWT) {
1688                 leftMargin.setEnabled(false);
1689                 rightMargin.setEnabled(false);
1690                 topMargin.setEnabled(false);
1691                 bottomMargin.setEnabled(false);
1692                 lblLeft.setEnabled(false);
1693                 lblRight.setEnabled(false);
1694                 lblTop.setEnabled(false);
1695                 lblBottom.setEnabled(false);
1696                 return;
1697             }
1698 
1699             Class<MediaPrintableArea> mpaCategory = MediaPrintableArea.class;
1700             MediaPrintableArea mpa =
1701                  (MediaPrintableArea)asCurrent.get(mpaCategory);
1702             MediaPrintableArea mpaMax = null;
1703             MediaSize mediaSize = null;
1704 
1705             Media media = (Media)asCurrent.get(Media.class);
1706             if (media == null || !(media instanceof MediaSizeName)) {
1707                 media = (Media)psCurrent.getDefaultAttributeValue(Media.class);
1708             }
1709             if (media != null && (media instanceof MediaSizeName)) {
1710                 MediaSizeName msn = (MediaSizeName)media;
1711                 mediaSize = MediaSize.getMediaSizeForName(msn);
1712             }
1713             if (mediaSize == null) {
1714                 mediaSize = new MediaSize(8.5f, 11f, Size2DSyntax.INCH);
1715             }
1716 
1717             if (media != null) {
1718                 PrintRequestAttributeSet tmpASet =
1719                     new HashPrintRequestAttributeSet(asCurrent);
1720                 tmpASet.add(media);
1721 
1722                 Object values =
1723                     psCurrent.getSupportedAttributeValues(mpaCategory,
1724                                                           docFlavor,
1725                                                           tmpASet);
1726                 if (values instanceof MediaPrintableArea[] &&
1727                     ((MediaPrintableArea[])values).length > 0) {
1728                     mpaMax = ((MediaPrintableArea[])values)[0];
1729 
1730                 } else if (values instanceof MediaPrintableArea) {
1731                     mpaMax = (MediaPrintableArea)values;
1732                 }
1733             }
1734             if (mpaMax == null) {
1735                 mpaMax = new MediaPrintableArea(0f, 0f,
1736                                                 mediaSize.getX(units),
1737                                                 mediaSize.getY(units),
1738                                                 units);
1739             }
1740 
1741             /*
1742              * At this point we now know as best we can :-
1743              * - the media size
1744              * - the maximum corresponding printable area
1745              * - the media printable area specified by the client, if any.
1746              * The next step is to create a default MPA if none was specified.
1747              * 1" margins are used unless they are disproportionately
1748              * large compared to the size of the media.
1749              */
1750 
1751             float wid = mediaSize.getX(MediaPrintableArea.INCH);
1752             float hgt = mediaSize.getY(MediaPrintableArea.INCH);
1753             float maxMarginRatio = 5f;
1754             float xMgn, yMgn;
1755             if (wid > maxMarginRatio) {
1756                 xMgn = 1f;
1757             } else {
1758                 xMgn = wid / maxMarginRatio;
1759             }
1760             if (hgt > maxMarginRatio) {
1761                 yMgn = 1f;
1762             } else {
1763                 yMgn = hgt / maxMarginRatio;
1764             }
1765 
1766             if (mpa == null) {
1767                 mpa = new MediaPrintableArea(xMgn, yMgn,
1768                                              wid-(2*xMgn), hgt-(2*yMgn),
1769                                              MediaPrintableArea.INCH);
1770                 asCurrent.add(mpa);
1771             }
1772             float pax = mpa.getX(units);
1773             float pay = mpa.getY(units);
1774             float paw = mpa.getWidth(units);
1775             float pah = mpa.getHeight(units);
1776             float paxMax = mpaMax.getX(units);
1777             float payMax = mpaMax.getY(units);
1778             float pawMax = mpaMax.getWidth(units);
1779             float pahMax = mpaMax.getHeight(units);
1780 
1781 
1782             boolean invalid = false;
1783 
1784             // If the paper is set to something which is too small to
1785             // accommodate a specified printable area, perhaps carried
1786             // over from a larger paper, the adjustment that needs to be
1787             // performed should seem the most natural from a user's viewpoint.
1788             // Since the user is specifying margins, then we are biased
1789             // towards keeping the margins as close to what is specified as
1790             // possible, shrinking or growing the printable area.
1791             // But the API uses printable area, so you need to know the
1792             // media size in which the margins were previously interpreted,
1793             // or at least have a record of the margins.
1794             // In the case that this is the creation of this UI we do not
1795             // have this record, so we are somewhat reliant on the client
1796             // to supply a reasonable default
1797             wid = mediaSize.getX(units);
1798             hgt = mediaSize.getY(units);
1799             if (lmVal >= 0f) {
1800                 invalid = true;
1801 
1802                 if (lmVal + rmVal > wid) {
1803                     // margins impossible, but maintain P.A if can
1804                     if (paw > pawMax) {
1805                         paw = pawMax;
1806                     }
1807                     // try to centre the printable area.
1808                     pax = (wid - paw)/2f;
1809                 } else {
1810                     pax = (lmVal >= paxMax) ? lmVal : paxMax;
1811                     paw = wid - pax - rmVal;
1812                 }
1813                 if (tmVal + bmVal > hgt) {
1814                     if (pah > pahMax) {
1815                         pah = pahMax;
1816                     }
1817                     pay = (hgt - pah)/2f;
1818                 } else {
1819                     pay = (tmVal >= payMax) ? tmVal : payMax;
1820                     pah = hgt - pay - bmVal;
1821                 }
1822             }
1823             if (pax < paxMax) {
1824                 invalid = true;
1825                 pax = paxMax;
1826             }
1827             if (pay < payMax) {
1828                 invalid = true;
1829                 pay = payMax;
1830             }
1831             if (paw > pawMax) {
1832                 invalid = true;
1833                 paw = pawMax;
1834             }
1835             if (pah > pahMax) {
1836                 invalid = true;
1837                 pah = pahMax;
1838             }
1839 
1840             if ((pax + paw > paxMax + pawMax) || (paw <= 0f)) {
1841                 invalid = true;
1842                 pax = paxMax;
1843                 paw = pawMax;
1844             }
1845             if ((pay + pah > payMax + pahMax) || (pah <= 0f)) {
1846                 invalid = true;
1847                 pay = payMax;
1848                 pah = pahMax;
1849             }
1850 
1851             if (invalid) {
1852                 mpa = new MediaPrintableArea(pax, pay, paw, pah, units);
1853                 asCurrent.add(mpa);
1854             }
1855 
1856             /* We now have a valid printable area.
1857              * Turn it into margins, using the mediaSize
1858              */
1859             lmVal = pax;
1860             tmVal = pay;
1861             rmVal = mediaSize.getX(units) - pax - paw;
1862             bmVal = mediaSize.getY(units) - pay - pah;
1863 
1864             lmObj = lmVal;
1865             rmObj = rmVal;
1866             tmObj = tmVal;
1867             bmObj = bmVal;
1868 
1869             /* Now we know the values to use, we need to assign them
1870              * to the fields appropriate for the orientation.
1871              * Note: if orientation changes this method must be called.
1872              */
1873             Class<OrientationRequested> orCategory = OrientationRequested.class;
1874             OrientationRequested or =
1875                 (OrientationRequested)asCurrent.get(orCategory);
1876 
1877             if (or == null) {
1878                 or = (OrientationRequested)
1879                      psCurrent.getDefaultAttributeValue(orCategory);
1880             }
1881 
1882             Float tmp;
1883 
1884             if (or == OrientationRequested.REVERSE_PORTRAIT) {
1885                 tmp = lmObj; lmObj = rmObj; rmObj = tmp;
1886                 tmp = tmObj; tmObj = bmObj; bmObj = tmp;
1887             }  else if (or == OrientationRequested.LANDSCAPE) {
1888                 tmp = lmObj;
1889                 lmObj = bmObj;
1890                 bmObj = rmObj;
1891                 rmObj = tmObj;
1892                 tmObj = tmp;
1893             }  else if (or == OrientationRequested.REVERSE_LANDSCAPE) {
1894                 tmp = lmObj;
1895                 lmObj = tmObj;
1896                 tmObj = rmObj;
1897                 rmObj = bmObj;
1898                 bmObj = tmp;
1899             }
1900 
1901             leftMargin.setValue(lmObj);
1902             rightMargin.setValue(rmObj);
1903             topMargin.setValue(tmObj);
1904             bottomMargin.setValue(bmObj);
1905         }
1906     }
1907 
1908     @SuppressWarnings("serial") // Superclass is not serializable across versions
1909     private class MediaPanel extends JPanel implements ItemListener {
1910 
1911         private final String strTitle = getMsg("border.media");
1912         private JLabel lblSize, lblSource;
1913         private JComboBox<Object> cbSize, cbSource;
1914         private Vector<MediaSizeName> sizes = new Vector<>();
1915         private Vector<MediaTray> sources = new Vector<>();
1916         private MarginsPanel pnlMargins = null;
1917 
1918         public MediaPanel() {
1919             super();
1920 
1921             GridBagLayout gridbag = new GridBagLayout();
1922             GridBagConstraints c = new GridBagConstraints();
1923 
1924             setLayout(gridbag);
1925             setBorder(BorderFactory.createTitledBorder(strTitle));
1926 
1927             cbSize = new JComboBox<>();
1928             cbSource = new JComboBox<>();
1929 
1930             c.fill = GridBagConstraints.BOTH;
1931             c.insets = compInsets;
1932             c.weighty = 1.0;
1933 
1934             c.weightx = 0.0;
1935             lblSize = new JLabel(getMsg("label.size"), JLabel.TRAILING);
1936             lblSize.setDisplayedMnemonic(getMnemonic("label.size"));
1937             lblSize.setLabelFor(cbSize);
1938             addToGB(lblSize, this, gridbag, c);
1939             c.weightx = 1.0;
1940             c.gridwidth = GridBagConstraints.REMAINDER;
1941             addToGB(cbSize, this, gridbag, c);
1942 
1943             c.weightx = 0.0;
1944             c.gridwidth = 1;
1945             lblSource = new JLabel(getMsg("label.source"), JLabel.TRAILING);
1946             lblSource.setDisplayedMnemonic(getMnemonic("label.source"));
1947             lblSource.setLabelFor(cbSource);
1948             addToGB(lblSource, this, gridbag, c);
1949             c.gridwidth = GridBagConstraints.REMAINDER;
1950             addToGB(cbSource, this, gridbag, c);
1951         }
1952 
1953         private String getMediaName(String key) {
1954             try {
1955                 // replace characters that would be invalid in
1956                 // a resource key with valid characters
1957                 String newkey = key.replace(' ', '-');
1958                 newkey = newkey.replace('#', 'n');
1959 
1960                 return messageRB.getString(newkey);
1961             } catch (java.util.MissingResourceException e) {
1962                 return key;
1963             }
1964         }
1965 
1966         public void itemStateChanged(ItemEvent e) {
1967             Object source = e.getSource();
1968 
1969             if (e.getStateChange() == ItemEvent.SELECTED) {
1970                 if (source == cbSize) {
1971                     int index = cbSize.getSelectedIndex();
1972 
1973                     if ((index >= 0) && (index < sizes.size())) {
1974                         if ((cbSource.getItemCount() > 1) &&
1975                             (cbSource.getSelectedIndex() >= 1))
1976                         {
1977                             int src = cbSource.getSelectedIndex() - 1;
1978                             MediaTray mt = sources.get(src);
1979                             asCurrent.add(new SunAlternateMedia(mt));
1980                         }
1981                         asCurrent.add(sizes.get(index));
1982                     }
1983                 } else if (source == cbSource) {
1984                     int index = cbSource.getSelectedIndex();
1985 
1986                     if ((index >= 1) && (index < (sources.size() + 1))) {
1987                        asCurrent.remove(SunAlternateMedia.class);
1988                        MediaTray newTray = sources.get(index - 1);
1989                        Media m = (Media)asCurrent.get(Media.class);
1990                        if (m == null || m instanceof MediaTray) {
1991                            asCurrent.add(newTray);
1992                        } else if (m instanceof MediaSizeName) {
1993                            MediaSizeName msn = (MediaSizeName)m;
1994                            Media def = (Media)psCurrent.getDefaultAttributeValue(Media.class);
1995                            if (def instanceof MediaSizeName && def.equals(msn)) {
1996                                asCurrent.add(newTray);
1997                            } else {
1998                                /* Non-default paper size, so need to store tray
1999                                 * as SunAlternateMedia
2000                                 */
2001                                asCurrent.add(new SunAlternateMedia(newTray));
2002                            }
2003                        }
2004                     } else if (index == 0) {
2005                         asCurrent.remove(SunAlternateMedia.class);
2006                         if (cbSize.getItemCount() > 0) {
2007                             int size = cbSize.getSelectedIndex();
2008                             asCurrent.add(sizes.get(size));
2009                         }
2010                     }
2011                 }
2012             // orientation affects display of margins.
2013                 if (pnlMargins != null) {
2014                     pnlMargins.updateInfo();
2015                 }
2016             }
2017         }
2018 
2019 
2020         /* this is ad hoc to keep things simple */
2021         public void addMediaListener(MarginsPanel pnl) {
2022             pnlMargins = pnl;
2023         }
2024         public void updateInfo() {
2025             Class<Media> mdCategory = Media.class;
2026             Class<SunAlternateMedia> amCategory = SunAlternateMedia.class;
2027             boolean mediaSupported = false;
2028 
2029             cbSize.removeItemListener(this);
2030             cbSize.removeAllItems();
2031             cbSource.removeItemListener(this);
2032             cbSource.removeAllItems();
2033             cbSource.addItem(getMediaName("auto-select"));
2034 
2035             sizes.clear();
2036             sources.clear();
2037 
2038             if (psCurrent.isAttributeCategorySupported(mdCategory)) {
2039                 mediaSupported = true;
2040 
2041                 Object values =
2042                     psCurrent.getSupportedAttributeValues(mdCategory,
2043                                                           docFlavor,
2044                                                           asCurrent);
2045 
2046                 if (values instanceof Media[]) {
2047                     Media[] media = (Media[])values;
2048 
2049                     for (int i = 0; i < media.length; i++) {
2050                         Media medium = media[i];
2051 
2052                         if (medium instanceof MediaSizeName) {
2053                             sizes.add((MediaSizeName)medium);
2054                             cbSize.addItem(getMediaName(medium.toString()));
2055                         } else if (medium instanceof MediaTray) {
2056                             sources.add((MediaTray)medium);
2057                             cbSource.addItem(getMediaName(medium.toString()));
2058                         }
2059                     }
2060                 }
2061             }
2062 
2063             boolean msSupported = (mediaSupported && (sizes.size() > 0));
2064             lblSize.setEnabled(msSupported);
2065             cbSize.setEnabled(msSupported);
2066 
2067             if (isAWT) {
2068                 cbSource.setEnabled(false);
2069                 lblSource.setEnabled(false);
2070             } else {
2071                 cbSource.setEnabled(mediaSupported);
2072             }
2073 
2074             if (mediaSupported) {
2075 
2076                 Media medium = (Media)asCurrent.get(mdCategory);
2077 
2078                // initialize size selection to default
2079                 Media defMedia = (Media)psCurrent.getDefaultAttributeValue(mdCategory);
2080                 if (defMedia instanceof MediaSizeName) {
2081                     cbSize.setSelectedIndex(sizes.size() > 0 ? sizes.indexOf(defMedia) : -1);
2082                 }
2083 
2084                 if (medium == null ||
2085                     !psCurrent.isAttributeValueSupported(medium,
2086                                                          docFlavor, asCurrent)) {
2087 
2088                     medium = defMedia;
2089 
2090                     if (medium == null) {
2091                         if (sizes.size() > 0) {
2092                             medium = (Media)sizes.get(0);
2093                         }
2094                     }
2095                     if (medium != null) {
2096                         asCurrent.add(medium);
2097                     }
2098                 }
2099                 if (medium != null) {
2100                     if (medium instanceof MediaSizeName) {
2101                         MediaSizeName ms = (MediaSizeName)medium;
2102                         cbSize.setSelectedIndex(sizes.indexOf(ms));
2103                     } else if (medium instanceof MediaTray) {
2104                         MediaTray mt = (MediaTray)medium;
2105                         cbSource.setSelectedIndex(sources.indexOf(mt) + 1);
2106                     }
2107                 } else {
2108                     cbSize.setSelectedIndex(sizes.size() > 0 ? 0 : -1);
2109                     cbSource.setSelectedIndex(0);
2110                 }
2111 
2112                 SunAlternateMedia alt = (SunAlternateMedia)asCurrent.get(amCategory);
2113                 if (alt != null) {
2114                     Media md = alt.getMedia();
2115                     if (md instanceof MediaTray) {
2116                         MediaTray mt = (MediaTray)md;
2117                         cbSource.setSelectedIndex(sources.indexOf(mt) + 1);
2118                     }
2119                 }
2120 
2121                 int selIndex = cbSize.getSelectedIndex();
2122                 if ((selIndex >= 0) && (selIndex < sizes.size())) {
2123                   asCurrent.add(sizes.get(selIndex));
2124                 }
2125 
2126                 selIndex = cbSource.getSelectedIndex();
2127                 if ((selIndex >= 1) && (selIndex < (sources.size()+1))) {
2128                     MediaTray mt = sources.get(selIndex-1);
2129                     if (medium instanceof MediaTray) {
2130                         asCurrent.add(mt);
2131                     } else {
2132                         asCurrent.add(new SunAlternateMedia(mt));
2133                     }
2134                 }
2135 
2136 
2137             }
2138             cbSize.addItemListener(this);
2139             cbSource.addItemListener(this);
2140         }
2141     }
2142 
2143     @SuppressWarnings("serial") // Superclass is not serializable across versions
2144     private class OrientationPanel extends JPanel
2145         implements ActionListener
2146     {
2147         private final String strTitle = getMsg("border.orientation");
2148         private IconRadioButton rbPortrait, rbLandscape,
2149                                 rbRevPortrait, rbRevLandscape;
2150         private MarginsPanel pnlMargins = null;
2151 
2152         public OrientationPanel() {
2153             super();
2154 
2155             GridBagLayout gridbag = new GridBagLayout();
2156             GridBagConstraints c = new GridBagConstraints();
2157 
2158             setLayout(gridbag);
2159             setBorder(BorderFactory.createTitledBorder(strTitle));
2160 
2161             c.fill = GridBagConstraints.BOTH;
2162             c.insets = compInsets;
2163             c.weighty = 1.0;
2164             c.gridwidth = GridBagConstraints.REMAINDER;
2165 
2166             ButtonGroup bg = new ButtonGroup();
2167             rbPortrait = new IconRadioButton("radiobutton.portrait",
2168                                              "orientPortrait.png", true,
2169                                              bg, this);
2170             rbPortrait.addActionListener(this);
2171             addToGB(rbPortrait, this, gridbag, c);
2172             rbLandscape = new IconRadioButton("radiobutton.landscape",
2173                                               "orientLandscape.png", false,
2174                                               bg, this);
2175             rbLandscape.addActionListener(this);
2176             addToGB(rbLandscape, this, gridbag, c);
2177             rbRevPortrait = new IconRadioButton("radiobutton.revportrait",
2178                                                 "orientRevPortrait.png", false,
2179                                                 bg, this);
2180             rbRevPortrait.addActionListener(this);
2181             addToGB(rbRevPortrait, this, gridbag, c);
2182             rbRevLandscape = new IconRadioButton("radiobutton.revlandscape",
2183                                                  "orientRevLandscape.png", false,
2184                                                  bg, this);
2185             rbRevLandscape.addActionListener(this);
2186             addToGB(rbRevLandscape, this, gridbag, c);
2187         }
2188 
2189         public void actionPerformed(ActionEvent e) {
2190             Object source = e.getSource();
2191 
2192             if (rbPortrait.isSameAs(source)) {
2193                 asCurrent.add(OrientationRequested.PORTRAIT);
2194             } else if (rbLandscape.isSameAs(source)) {
2195                 asCurrent.add(OrientationRequested.LANDSCAPE);
2196             } else if (rbRevPortrait.isSameAs(source)) {
2197                 asCurrent.add(OrientationRequested.REVERSE_PORTRAIT);
2198             } else if (rbRevLandscape.isSameAs(source)) {
2199                 asCurrent.add(OrientationRequested.REVERSE_LANDSCAPE);
2200             }
2201             // orientation affects display of margins.
2202             if (pnlMargins != null) {
2203                 pnlMargins.updateInfo();
2204             }
2205         }
2206 
2207         /* This is ad hoc to keep things simple */
2208         void addOrientationListener(MarginsPanel pnl) {
2209             pnlMargins = pnl;
2210         }
2211 
2212         public void updateInfo() {
2213             Class<OrientationRequested> orCategory = OrientationRequested.class;
2214             boolean pSupported = false;
2215             boolean lSupported = false;
2216             boolean rpSupported = false;
2217             boolean rlSupported = false;
2218 
2219             if (isAWT) {
2220                 pSupported = true;
2221                 lSupported = true;
2222             } else
2223             if (psCurrent.isAttributeCategorySupported(orCategory)) {
2224                 Object values =
2225                     psCurrent.getSupportedAttributeValues(orCategory,
2226                                                           docFlavor,
2227                                                           asCurrent);
2228 
2229                 if (values instanceof OrientationRequested[]) {
2230                     OrientationRequested[] ovalues =
2231                         (OrientationRequested[])values;
2232 
2233                     for (int i = 0; i < ovalues.length; i++) {
2234                         OrientationRequested value = ovalues[i];
2235 
2236                         if (value == OrientationRequested.PORTRAIT) {
2237                             pSupported = true;
2238                         } else if (value == OrientationRequested.LANDSCAPE) {
2239                             lSupported = true;
2240                         } else if (value == OrientationRequested.REVERSE_PORTRAIT) {
2241                             rpSupported = true;
2242                         } else if (value == OrientationRequested.REVERSE_LANDSCAPE) {
2243                             rlSupported = true;
2244                         }
2245                     }
2246                 }
2247             }
2248 
2249 
2250             rbPortrait.setEnabled(pSupported);
2251             rbLandscape.setEnabled(lSupported);
2252             rbRevPortrait.setEnabled(rpSupported);
2253             rbRevLandscape.setEnabled(rlSupported);
2254 
2255             OrientationRequested or = (OrientationRequested)asCurrent.get(orCategory);
2256             if (or == null ||
2257                 !psCurrent.isAttributeValueSupported(or, docFlavor, asCurrent)) {
2258 
2259                 or = (OrientationRequested)psCurrent.getDefaultAttributeValue(orCategory);
2260                 // need to validate if default is not supported
2261                 if ((or != null) &&
2262                    !psCurrent.isAttributeValueSupported(or, docFlavor, asCurrent)) {
2263                     or = null;
2264                     Object values =
2265                         psCurrent.getSupportedAttributeValues(orCategory,
2266                                                               docFlavor,
2267                                                               asCurrent);
2268                     if (values instanceof OrientationRequested[]) {
2269                         OrientationRequested[] orValues =
2270                                             (OrientationRequested[])values;
2271                         if (orValues.length > 1) {
2272                             // get the first in the list
2273                             or = orValues[0];
2274                         }
2275                     }
2276                 }
2277 
2278                 if (or == null) {
2279                     or = OrientationRequested.PORTRAIT;
2280                 }
2281                 asCurrent.add(or);
2282             }
2283 
2284             if (or == OrientationRequested.PORTRAIT) {
2285                 rbPortrait.setSelected(true);
2286             } else if (or == OrientationRequested.LANDSCAPE) {
2287                 rbLandscape.setSelected(true);
2288             } else if (or == OrientationRequested.REVERSE_PORTRAIT) {
2289                 rbRevPortrait.setSelected(true);
2290             } else { // if (or == OrientationRequested.REVERSE_LANDSCAPE)
2291                 rbRevLandscape.setSelected(true);
2292             }
2293         }
2294     }
2295 
2296 
2297 
2298     /**
2299      * The "Appearance" tab.  Includes the controls for Chromaticity,
2300      * PrintQuality, JobPriority, JobName, and other related job attributes.
2301      */
2302     @SuppressWarnings("serial") // Superclass is not serializable across versions
2303     private class AppearancePanel extends JPanel {
2304 
2305         private ChromaticityPanel pnlChromaticity;
2306         private QualityPanel pnlQuality;
2307         private JobAttributesPanel pnlJobAttributes;
2308         private SidesPanel pnlSides;
2309 
2310         public AppearancePanel() {
2311             super();
2312 
2313             GridBagLayout gridbag = new GridBagLayout();
2314             GridBagConstraints c = new GridBagConstraints();
2315 
2316             setLayout(gridbag);
2317 
2318             c.fill = GridBagConstraints.BOTH;
2319             c.insets = panelInsets;
2320             c.weightx = 1.0;
2321             c.weighty = 1.0;
2322 
2323             c.gridwidth = GridBagConstraints.RELATIVE;
2324             pnlChromaticity = new ChromaticityPanel();
2325             addToGB(pnlChromaticity, this, gridbag, c);
2326 
2327             c.gridwidth = GridBagConstraints.REMAINDER;
2328             pnlQuality = new QualityPanel();
2329             addToGB(pnlQuality, this, gridbag, c);
2330 
2331             c.gridwidth = 1;
2332             pnlSides = new SidesPanel();
2333             addToGB(pnlSides, this, gridbag, c);
2334 
2335             c.gridwidth = GridBagConstraints.REMAINDER;
2336             pnlJobAttributes = new JobAttributesPanel();
2337             addToGB(pnlJobAttributes, this, gridbag, c);
2338 
2339         }
2340 
2341         public void updateInfo() {
2342             pnlChromaticity.updateInfo();
2343             pnlQuality.updateInfo();
2344             pnlSides.updateInfo();
2345             pnlJobAttributes.updateInfo();
2346         }
2347     }
2348 
2349     @SuppressWarnings("serial") // Superclass is not serializable across versions
2350     private class ChromaticityPanel extends JPanel
2351         implements ActionListener
2352     {
2353         private final String strTitle = getMsg("border.chromaticity");
2354         private JRadioButton rbMonochrome, rbColor;
2355 
2356         public ChromaticityPanel() {
2357             super();
2358 
2359             GridBagLayout gridbag = new GridBagLayout();
2360             GridBagConstraints c = new GridBagConstraints();
2361 
2362             setLayout(gridbag);
2363             setBorder(BorderFactory.createTitledBorder(strTitle));
2364 
2365             c.fill = GridBagConstraints.BOTH;
2366             c.gridwidth = GridBagConstraints.REMAINDER;
2367             c.weighty = 1.0;
2368 
2369             ButtonGroup bg = new ButtonGroup();
2370             rbMonochrome = createRadioButton("radiobutton.monochrome", this);
2371             rbMonochrome.setSelected(true);
2372             bg.add(rbMonochrome);
2373             addToGB(rbMonochrome, this, gridbag, c);
2374             rbColor = createRadioButton("radiobutton.color", this);
2375             bg.add(rbColor);
2376             addToGB(rbColor, this, gridbag, c);
2377         }
2378 
2379         public void actionPerformed(ActionEvent e) {
2380             Object source = e.getSource();
2381 
2382             // REMIND: use isSameAs if we move to a IconRB in the future
2383             if (source == rbMonochrome) {
2384                 asCurrent.add(Chromaticity.MONOCHROME);
2385             } else if (source == rbColor) {
2386                 asCurrent.add(Chromaticity.COLOR);
2387             }
2388         }
2389 
2390         public void updateInfo() {
2391             Class<Chromaticity> chCategory = Chromaticity.class;
2392             boolean monoSupported = false;
2393             boolean colorSupported = false;
2394 
2395             if (isAWT) {
2396                 monoSupported = true;
2397                 colorSupported = true;
2398             } else
2399             if (psCurrent.isAttributeCategorySupported(chCategory)) {
2400                 Object values =
2401                     psCurrent.getSupportedAttributeValues(chCategory,
2402                                                           docFlavor,
2403                                                           asCurrent);
2404 
2405                 if (values instanceof Chromaticity[]) {
2406                     Chromaticity[] cvalues = (Chromaticity[])values;
2407 
2408                     for (int i = 0; i < cvalues.length; i++) {
2409                         Chromaticity value = cvalues[i];
2410 
2411                         if (value == Chromaticity.MONOCHROME) {
2412                             monoSupported = true;
2413                         } else if (value == Chromaticity.COLOR) {
2414                             colorSupported = true;
2415                         }
2416                     }
2417                 }
2418             }
2419 
2420 
2421             rbMonochrome.setEnabled(monoSupported);
2422             rbColor.setEnabled(colorSupported);
2423 
2424             Chromaticity ch = (Chromaticity)asCurrent.get(chCategory);
2425             if (ch == null) {
2426                 ch = (Chromaticity)psCurrent.getDefaultAttributeValue(chCategory);
2427                 if (ch == null) {
2428                     ch = Chromaticity.MONOCHROME;
2429                 }
2430             }
2431 
2432             if (ch == Chromaticity.MONOCHROME) {
2433                 rbMonochrome.setSelected(true);
2434             } else { // if (ch == Chromaticity.COLOR)
2435                 rbColor.setSelected(true);
2436             }
2437         }
2438     }
2439 
2440     @SuppressWarnings("serial") // Superclass is not serializable across versions
2441     private class QualityPanel extends JPanel
2442         implements ActionListener
2443     {
2444         private final String strTitle = getMsg("border.quality");
2445         private JRadioButton rbDraft, rbNormal, rbHigh;
2446 
2447         public QualityPanel() {
2448             super();
2449 
2450             GridBagLayout gridbag = new GridBagLayout();
2451             GridBagConstraints c = new GridBagConstraints();
2452 
2453             setLayout(gridbag);
2454             setBorder(BorderFactory.createTitledBorder(strTitle));
2455 
2456             c.fill = GridBagConstraints.BOTH;
2457             c.gridwidth = GridBagConstraints.REMAINDER;
2458             c.weighty = 1.0;
2459 
2460             ButtonGroup bg = new ButtonGroup();
2461             rbDraft = createRadioButton("radiobutton.draftq", this);
2462             bg.add(rbDraft);
2463             addToGB(rbDraft, this, gridbag, c);
2464             rbNormal = createRadioButton("radiobutton.normalq", this);
2465             rbNormal.setSelected(true);
2466             bg.add(rbNormal);
2467             addToGB(rbNormal, this, gridbag, c);
2468             rbHigh = createRadioButton("radiobutton.highq", this);
2469             bg.add(rbHigh);
2470             addToGB(rbHigh, this, gridbag, c);
2471         }
2472 
2473         public void actionPerformed(ActionEvent e) {
2474             Object source = e.getSource();
2475 
2476             if (source == rbDraft) {
2477                 asCurrent.add(PrintQuality.DRAFT);
2478             } else if (source == rbNormal) {
2479                 asCurrent.add(PrintQuality.NORMAL);
2480             } else if (source == rbHigh) {
2481                 asCurrent.add(PrintQuality.HIGH);
2482             }
2483         }
2484 
2485         public void updateInfo() {
2486             Class<PrintQuality> pqCategory = PrintQuality.class;
2487             boolean draftSupported = false;
2488             boolean normalSupported = false;
2489             boolean highSupported = false;
2490 
2491             if (isAWT) {
2492                 draftSupported = true;
2493                 normalSupported = true;
2494                 highSupported = true;
2495             } else
2496             if (psCurrent.isAttributeCategorySupported(pqCategory)) {
2497                 Object values =
2498                     psCurrent.getSupportedAttributeValues(pqCategory,
2499                                                           docFlavor,
2500                                                           asCurrent);
2501 
2502                 if (values instanceof PrintQuality[]) {
2503                     PrintQuality[] qvalues = (PrintQuality[])values;
2504 
2505                     for (int i = 0; i < qvalues.length; i++) {
2506                         PrintQuality value = qvalues[i];
2507 
2508                         if (value == PrintQuality.DRAFT) {
2509                             draftSupported = true;
2510                         } else if (value == PrintQuality.NORMAL) {
2511                             normalSupported = true;
2512                         } else if (value == PrintQuality.HIGH) {
2513                             highSupported = true;
2514                         }
2515                     }
2516                 }
2517             }
2518 
2519             rbDraft.setEnabled(draftSupported);
2520             rbNormal.setEnabled(normalSupported);
2521             rbHigh.setEnabled(highSupported);
2522 
2523             PrintQuality pq = (PrintQuality)asCurrent.get(pqCategory);
2524             if (pq == null) {
2525                 pq = (PrintQuality)psCurrent.getDefaultAttributeValue(pqCategory);
2526                 if (pq == null) {
2527                     pq = PrintQuality.NORMAL;
2528                 }
2529             }
2530 
2531             if (pq == PrintQuality.DRAFT) {
2532                 rbDraft.setSelected(true);
2533             } else if (pq == PrintQuality.NORMAL) {
2534                 rbNormal.setSelected(true);
2535             } else { // if (pq == PrintQuality.HIGH)
2536                 rbHigh.setSelected(true);
2537             }
2538         }
2539 
2540 
2541     }
2542 
2543     @SuppressWarnings("serial") // Superclass is not serializable across versions
2544     private class SidesPanel extends JPanel
2545         implements ActionListener
2546     {
2547         private final String strTitle = getMsg("border.sides");
2548         private IconRadioButton rbOneSide, rbTumble, rbDuplex;
2549 
2550         public SidesPanel() {
2551             super();
2552 
2553             GridBagLayout gridbag = new GridBagLayout();
2554             GridBagConstraints c = new GridBagConstraints();
2555 
2556             setLayout(gridbag);
2557             setBorder(BorderFactory.createTitledBorder(strTitle));
2558 
2559             c.fill = GridBagConstraints.BOTH;
2560             c.insets = compInsets;
2561             c.weighty = 1.0;
2562             c.gridwidth = GridBagConstraints.REMAINDER;
2563 
2564             ButtonGroup bg = new ButtonGroup();
2565             rbOneSide = new IconRadioButton("radiobutton.oneside",
2566                                             "oneside.png", true,
2567                                             bg, this);
2568             rbOneSide.addActionListener(this);
2569             addToGB(rbOneSide, this, gridbag, c);
2570             rbTumble = new IconRadioButton("radiobutton.tumble",
2571                                            "tumble.png", false,
2572                                            bg, this);
2573             rbTumble.addActionListener(this);
2574             addToGB(rbTumble, this, gridbag, c);
2575             rbDuplex = new IconRadioButton("radiobutton.duplex",
2576                                            "duplex.png", false,
2577                                            bg, this);
2578             rbDuplex.addActionListener(this);
2579             c.gridwidth = GridBagConstraints.REMAINDER;
2580             addToGB(rbDuplex, this, gridbag, c);
2581         }
2582 
2583         public void actionPerformed(ActionEvent e) {
2584             Object source = e.getSource();
2585 
2586             if (rbOneSide.isSameAs(source)) {
2587                 asCurrent.add(Sides.ONE_SIDED);
2588             } else if (rbTumble.isSameAs(source)) {
2589                 asCurrent.add(Sides.TUMBLE);
2590             } else if (rbDuplex.isSameAs(source)) {
2591                 asCurrent.add(Sides.DUPLEX);
2592             }
2593         }
2594 
2595         public void updateInfo() {
2596             Class<Sides> sdCategory = Sides.class;
2597             boolean osSupported = false;
2598             boolean tSupported = false;
2599             boolean dSupported = false;
2600 
2601             if (psCurrent.isAttributeCategorySupported(sdCategory)) {
2602                 Object values =
2603                     psCurrent.getSupportedAttributeValues(sdCategory,
2604                                                           docFlavor,
2605                                                           asCurrent);
2606 
2607                 if (values instanceof Sides[]) {
2608                     Sides[] svalues = (Sides[])values;
2609 
2610                     for (int i = 0; i < svalues.length; i++) {
2611                         Sides value = svalues[i];
2612 
2613                         if (value == Sides.ONE_SIDED) {
2614                             osSupported = true;
2615                         } else if (value == Sides.TUMBLE) {
2616                             tSupported = true;
2617                         } else if (value == Sides.DUPLEX) {
2618                             dSupported = true;
2619                         }
2620                     }
2621                 }
2622             }
2623             rbOneSide.setEnabled(osSupported);
2624             rbTumble.setEnabled(tSupported);
2625             rbDuplex.setEnabled(dSupported);
2626 
2627             Sides sd = (Sides)asCurrent.get(sdCategory);
2628             if (sd == null) {
2629                 sd = (Sides)psCurrent.getDefaultAttributeValue(sdCategory);
2630                 if (sd == null) {
2631                     sd = Sides.ONE_SIDED;
2632                 }
2633             }
2634 
2635             if (sd == Sides.ONE_SIDED) {
2636                 rbOneSide.setSelected(true);
2637             } else if (sd == Sides.TUMBLE) {
2638                 rbTumble.setSelected(true);
2639             } else { // if (sd == Sides.DUPLEX)
2640                 rbDuplex.setSelected(true);
2641             }
2642         }
2643     }
2644 
2645 
2646     @SuppressWarnings("serial") // Superclass is not serializable across versions
2647     private class JobAttributesPanel extends JPanel
2648         implements ActionListener, ChangeListener, FocusListener
2649     {
2650         private final String strTitle = getMsg("border.jobattributes");
2651         private JLabel lblPriority, lblJobName, lblUserName;
2652         private JSpinner spinPriority;
2653         private SpinnerNumberModel snModel;
2654         private JCheckBox cbJobSheets;
2655         private JTextField tfJobName, tfUserName;
2656 
2657         public JobAttributesPanel() {
2658             super();
2659 
2660             GridBagLayout gridbag = new GridBagLayout();
2661             GridBagConstraints c = new GridBagConstraints();
2662 
2663             setLayout(gridbag);
2664             setBorder(BorderFactory.createTitledBorder(strTitle));
2665 
2666             c.fill = GridBagConstraints.NONE;
2667             c.insets = compInsets;
2668             c.weighty = 1.0;
2669 
2670             cbJobSheets = createCheckBox("checkbox.jobsheets", this);
2671             c.anchor = GridBagConstraints.LINE_START;
2672             addToGB(cbJobSheets, this, gridbag, c);
2673 
2674             JPanel pnlTop = new JPanel();
2675             lblPriority = new JLabel(getMsg("label.priority"), JLabel.TRAILING);
2676             lblPriority.setDisplayedMnemonic(getMnemonic("label.priority"));
2677 
2678             pnlTop.add(lblPriority);
2679             snModel = new SpinnerNumberModel(1, 1, 100, 1);
2680             spinPriority = new JSpinner(snModel);
2681             lblPriority.setLabelFor(spinPriority);
2682             // REMIND
2683             ((JSpinner.NumberEditor)spinPriority.getEditor()).getTextField().setColumns(3);
2684             spinPriority.addChangeListener(this);
2685             pnlTop.add(spinPriority);
2686             c.anchor = GridBagConstraints.LINE_END;
2687             c.gridwidth = GridBagConstraints.REMAINDER;
2688             pnlTop.getAccessibleContext().setAccessibleName(
2689                                        getMsg("label.priority"));
2690             addToGB(pnlTop, this, gridbag, c);
2691 
2692             c.fill = GridBagConstraints.HORIZONTAL;
2693             c.anchor = GridBagConstraints.CENTER;
2694             c.weightx = 0.0;
2695             c.gridwidth = 1;
2696             char jmnemonic = getMnemonic("label.jobname");
2697             lblJobName = new JLabel(getMsg("label.jobname"), JLabel.TRAILING);
2698             lblJobName.setDisplayedMnemonic(jmnemonic);
2699             addToGB(lblJobName, this, gridbag, c);
2700             c.weightx = 1.0;
2701             c.gridwidth = GridBagConstraints.REMAINDER;
2702             tfJobName = new JTextField();
2703             lblJobName.setLabelFor(tfJobName);
2704             tfJobName.addFocusListener(this);
2705             tfJobName.setFocusAccelerator(jmnemonic);
2706             tfJobName.getAccessibleContext().setAccessibleName(
2707                                              getMsg("label.jobname"));
2708             addToGB(tfJobName, this, gridbag, c);
2709 
2710             c.weightx = 0.0;
2711             c.gridwidth = 1;
2712             char umnemonic = getMnemonic("label.username");
2713             lblUserName = new JLabel(getMsg("label.username"), JLabel.TRAILING);
2714             lblUserName.setDisplayedMnemonic(umnemonic);
2715             addToGB(lblUserName, this, gridbag, c);
2716             c.gridwidth = GridBagConstraints.REMAINDER;
2717             tfUserName = new JTextField();
2718             lblUserName.setLabelFor(tfUserName);
2719             tfUserName.addFocusListener(this);
2720             tfUserName.setFocusAccelerator(umnemonic);
2721             tfUserName.getAccessibleContext().setAccessibleName(
2722                                              getMsg("label.username"));
2723             addToGB(tfUserName, this, gridbag, c);
2724         }
2725 
2726         public void actionPerformed(ActionEvent e) {
2727             if (cbJobSheets.isSelected()) {
2728                 asCurrent.add(JobSheets.STANDARD);
2729             } else {
2730                 asCurrent.add(JobSheets.NONE);
2731             }
2732         }
2733 
2734         public void stateChanged(ChangeEvent e) {
2735             asCurrent.add(new JobPriority(snModel.getNumber().intValue()));
2736         }
2737 
2738         public void focusLost(FocusEvent e) {
2739             Object source = e.getSource();
2740 
2741             if (source == tfJobName) {
2742                 asCurrent.add(new JobName(tfJobName.getText(),
2743                                           Locale.getDefault()));
2744             } else if (source == tfUserName) {
2745                 asCurrent.add(new RequestingUserName(tfUserName.getText(),
2746                                                      Locale.getDefault()));
2747             }
2748         }
2749 
2750         public void focusGained(FocusEvent e) {}
2751 
2752         public void updateInfo() {
2753             Class<JobSheets>          jsCategory = JobSheets.class;
2754             Class<JobPriority>        jpCategory = JobPriority.class;
2755             Class<JobName>            jnCategory = JobName.class;
2756             Class<RequestingUserName> unCategory = RequestingUserName.class;
2757             boolean jsSupported = false;
2758             boolean jpSupported = false;
2759             boolean jnSupported = false;
2760             boolean unSupported = false;
2761 
2762             // setup JobSheets checkbox
2763             if (psCurrent.isAttributeCategorySupported(jsCategory)) {
2764                 jsSupported = true;
2765             }
2766             JobSheets js = (JobSheets)asCurrent.get(jsCategory);
2767             if (js == null) {
2768                 js = (JobSheets)psCurrent.getDefaultAttributeValue(jsCategory);
2769                 if (js == null) {
2770                     js = JobSheets.STANDARD;
2771                 }
2772             }
2773             cbJobSheets.setSelected(js != JobSheets.NONE && jsSupported);
2774             cbJobSheets.setEnabled(jsSupported);
2775 
2776             // setup JobPriority spinner
2777             if (!isAWT && psCurrent.isAttributeCategorySupported(jpCategory)) {
2778                 jpSupported = true;
2779             }
2780             JobPriority jp = (JobPriority)asCurrent.get(jpCategory);
2781             if (jp == null) {
2782                 jp = (JobPriority)psCurrent.getDefaultAttributeValue(jpCategory);
2783                 if (jp == null) {
2784                     jp = new JobPriority(1);
2785                 }
2786             }
2787             int value = jp.getValue();
2788             if ((value < 1) || (value > 100)) {
2789                 value = 1;
2790             }
2791             snModel.setValue(value);
2792             lblPriority.setEnabled(jpSupported);
2793             spinPriority.setEnabled(jpSupported);
2794 
2795             // setup JobName text field
2796             if (psCurrent.isAttributeCategorySupported(jnCategory)) {
2797                 jnSupported = true;
2798             }
2799             JobName jn = (JobName)asCurrent.get(jnCategory);
2800             if (jn == null) {
2801                 jn = (JobName)psCurrent.getDefaultAttributeValue(jnCategory);
2802                 if (jn == null) {
2803                     jn = new JobName("", Locale.getDefault());
2804                 }
2805             }
2806             tfJobName.setText(jn.getValue());
2807             tfJobName.setEnabled(jnSupported);
2808             lblJobName.setEnabled(jnSupported);
2809 
2810             // setup RequestingUserName text field
2811             if (!isAWT && psCurrent.isAttributeCategorySupported(unCategory)) {
2812                 unSupported = true;
2813             }
2814             RequestingUserName un = (RequestingUserName)asCurrent.get(unCategory);
2815             if (un == null) {
2816                 un = (RequestingUserName)psCurrent.getDefaultAttributeValue(unCategory);
2817                 if (un == null) {
2818                     un = new RequestingUserName("", Locale.getDefault());
2819                 }
2820             }
2821             tfUserName.setText(un.getValue());
2822             tfUserName.setEnabled(unSupported);
2823             lblUserName.setEnabled(unSupported);
2824         }
2825     }
2826 
2827 
2828 
2829 
2830     /**
2831      * A special widget that groups a JRadioButton with an associated icon,
2832      * placed to the left of the radio button.
2833      */
2834     @SuppressWarnings("serial") // Superclass is not serializable across versions
2835     private class IconRadioButton extends JPanel {
2836 
2837         private JRadioButton rb;
2838         private JLabel lbl;
2839 
2840         public IconRadioButton(String key, String img, boolean selected,
2841                                ButtonGroup bg, ActionListener al)
2842         {
2843             super(new FlowLayout(FlowLayout.LEADING));
2844             final URL imgURL = getImageResource(img);
2845             Icon icon = java.security.AccessController.doPrivileged(
2846                                  new java.security.PrivilegedAction<Icon>() {
2847                 public Icon run() {
2848                     Icon icon = new ImageIcon(imgURL);
2849                     return icon;
2850                 }
2851             });
2852             lbl = new JLabel(icon);
2853             add(lbl);
2854 
2855             rb = createRadioButton(key, al);
2856             rb.setSelected(selected);
2857             addToBG(rb, this, bg);
2858         }
2859 
2860         public void addActionListener(ActionListener al) {
2861             rb.addActionListener(al);
2862         }
2863 
2864         public boolean isSameAs(Object source) {
2865             return (rb == source);
2866         }
2867 
2868         public void setEnabled(boolean enabled) {
2869             rb.setEnabled(enabled);
2870             lbl.setEnabled(enabled);
2871         }
2872 
2873         public boolean isSelected() {
2874             return rb.isSelected();
2875         }
2876 
2877         public void setSelected(boolean selected) {
2878             rb.setSelected(selected);
2879         }
2880     }
2881 
2882     /**
2883      * Similar in functionality to the default JFileChooser, except this
2884      * chooser will pop up a "Do you want to overwrite..." dialog if the
2885      * user selects a file that already exists.
2886      */
2887     @SuppressWarnings("serial") // JDK implementation class
2888     private class ValidatingFileChooser extends JFileChooser {
2889         public void approveSelection() {
2890             File selected = getSelectedFile();
2891             boolean exists;
2892 
2893             try {
2894                 exists = selected.exists();
2895             } catch (SecurityException e) {
2896                 exists = false;
2897             }
2898 
2899             if (exists) {
2900                 int val;
2901                 val = JOptionPane.showConfirmDialog(this,
2902                                                     getMsg("dialog.overwrite"),
2903                                                     getMsg("dialog.owtitle"),
2904                                                     JOptionPane.YES_NO_OPTION);
2905                 if (val != JOptionPane.YES_OPTION) {
2906                     return;
2907                 }
2908             }
2909 
2910             try {
2911                 if (selected.createNewFile()) {
2912                     selected.delete();
2913                 }
2914             }  catch (IOException ioe) {
2915                 JOptionPane.showMessageDialog(this,
2916                                    getMsg("dialog.writeerror")+" "+selected,
2917                                    getMsg("dialog.owtitle"),
2918                                    JOptionPane.WARNING_MESSAGE);
2919                 return;
2920             } catch (SecurityException se) {
2921                 //There is already file read/write access so at this point
2922                 // only delete access is denied.  Just ignore it because in
2923                 // most cases the file created in createNewFile gets
2924                 // overwritten anyway.
2925             }
2926             File pFile = selected.getParentFile();
2927             if ((selected.exists() &&
2928                       (!selected.isFile() || !selected.canWrite())) ||
2929                      ((pFile != null) &&
2930                       (!pFile.exists() || (pFile.exists() && !pFile.canWrite())))) {
2931                 JOptionPane.showMessageDialog(this,
2932                                    getMsg("dialog.writeerror")+" "+selected,
2933                                    getMsg("dialog.owtitle"),
2934                                    JOptionPane.WARNING_MESSAGE);
2935                 return;
2936             }
2937 
2938             super.approveSelection();
2939         }
2940     }
2941 }