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