2839 lines · 102.4 KB · Java
RawDownload
    /**     * Returns the mnemonic as a KeyEvent.VK constant from the resource.     */    private static int getVKMnemonic(String key) {        String str = getMsg(key + ".vkMnemonic");        if ((str != null) && (str.length() > 0)) {            try {                return Integer.parseInt(str);            } catch (NumberFormatException nfe) {}        }        return 0;    }     /**     * Returns URL for image resource     */    private static URL getImageResource(final String key) {        URL url = (URL)java.security.AccessController.doPrivileged(                       new java.security.PrivilegedAction() {                public Object run() {                    URL url = ServiceDialog.class.getResource(                                                  "resources/" + key);                    return url;                }        });         if (url == null) {            throw new Error("Fatal: Resource for ServiceUI is broken; " +                            "there is no " + key + " key in resource");        }         return url;    }     /**     * Creates a new JButton and sets its text, mnemonic, and ActionListener     */    private static JButton createButton(String key, ActionListener al) {        JButton btn = new JButton(getMsg(key));        btn.setMnemonic(getMnemonic(key));        btn.addActionListener(al);         return btn;    }     /**     * Creates a new JButton and sets its text, and ActionListener     */    private static JButton createExitButton(String key, ActionListener al) {        String str = getMsg(key);        JButton btn = new JButton(str);        btn.addActionListener(al);        btn.getAccessibleContext().setAccessibleDescription(str);        return btn;    }     /**     * Creates a new JCheckBox and sets its text, mnemonic, and ActionListener     */    private static JCheckBox createCheckBox(String key, ActionListener al) {        JCheckBox cb = new JCheckBox(getMsg(key));        cb.setMnemonic(getMnemonic(key));        cb.addActionListener(al);         return cb;    }     /**     * Creates a new JRadioButton and sets its text, mnemonic,     * and ActionListener     */    private static JRadioButton createRadioButton(String key,                                                  ActionListener al)    {        JRadioButton rb = new JRadioButton(getMsg(key));        rb.setMnemonic(getMnemonic(key));        rb.addActionListener(al);         return rb;    }   /**   * Creates a  pop-up dialog for "no print service"   */    public static void showNoPrintService(GraphicsConfiguration gc)    {        Frame dlgFrame = new Frame(gc);        JOptionPane.showMessageDialog(dlgFrame,                                      getMsg("dialog.noprintermsg"));        dlgFrame.dispose();    }     /**     * Sets the constraints for the GridBagLayout and adds the Component     * to the given Container     */    private static void addToGB(Component comp, Container cont,                                GridBagLayout gridbag,                                GridBagConstraints constraints)    {        gridbag.setConstraints(comp, constraints);        cont.add(comp);    }     /**     * Adds the AbstractButton to both the given ButtonGroup and Container     */    private static void addToBG(AbstractButton button, Container cont,                                ButtonGroup bg)    {        bg.add(button);        cont.add(button);    }        /**     * The "General" tab.  Includes the controls for PrintService,     * PageRange, and Copies/Collate.     */    private class GeneralPanel extends JPanel {         private PrintServicePanel pnlPrintService;        private PrintRangePanel pnlPrintRange;        private CopiesPanel pnlCopies;         public GeneralPanel() {            super();             GridBagLayout gridbag = new GridBagLayout();            GridBagConstraints c = new GridBagConstraints();             setLayout(gridbag);             c.fill = GridBagConstraints.BOTH;            c.insets = panelInsets;            c.weightx = 1.0;            c.weighty = 1.0;             c.gridwidth = GridBagConstraints.REMAINDER;            pnlPrintService = new PrintServicePanel();            addToGB(pnlPrintService, this, gridbag, c);             c.gridwidth = GridBagConstraints.RELATIVE;            pnlPrintRange = new PrintRangePanel();            addToGB(pnlPrintRange, this, gridbag, c);             c.gridwidth = GridBagConstraints.REMAINDER;            pnlCopies = new CopiesPanel();            addToGB(pnlCopies, this, gridbag, c);        }         public boolean isPrintToFileRequested() {            return (pnlPrintService.isPrintToFileSelected());        }         public void updateInfo() {            pnlPrintService.updateInfo();            pnlPrintRange.updateInfo();            pnlCopies.updateInfo();        }    }     private class PrintServicePanel extends JPanel        implements ActionListener, ItemListener, PopupMenuListener    {        private final String strTitle = getMsg("border.printservice");        private FilePermission printToFilePermission;        private JButton btnProperties;        private JCheckBox cbPrintToFile;        private JComboBox cbName;        private JLabel lblType, lblStatus, lblInfo;        private ServiceUIFactory uiFactory;        private boolean changedService = false;        private boolean filePermission;         public PrintServicePanel() {            super();             uiFactory = psCurrent.getServiceUIFactory();             GridBagLayout gridbag = new GridBagLayout();            GridBagConstraints c = new GridBagConstraints();             setLayout(gridbag);            setBorder(BorderFactory.createTitledBorder(strTitle));             String[] psnames = new String[services.length];            for (int i = 0; i < psnames.length; i++) {                psnames[i] = services[i].getName();            }            cbName = new JComboBox(psnames);            cbName.setSelectedIndex(defaultServiceIndex);            cbName.addItemListener(this);            cbName.addPopupMenuListener(this);             c.fill = GridBagConstraints.BOTH;            c.insets = compInsets;             c.weightx = 0.0;            JLabel lblName = new JLabel(getMsg("label.psname"), JLabel.TRAILING);            lblName.setDisplayedMnemonic(getMnemonic("label.psname"));            lblName.setLabelFor(cbName);            addToGB(lblName, this, gridbag, c);            c.weightx = 1.0;            c.gridwidth = GridBagConstraints.RELATIVE;            addToGB(cbName, this, gridbag, c);            c.weightx = 0.0;            c.gridwidth = GridBagConstraints.REMAINDER;            btnProperties = createButton("button.properties", this);            addToGB(btnProperties, this, gridbag, c);             c.weighty = 1.0;            lblStatus = addLabel(getMsg("label.status"), gridbag, c);            lblStatus.setLabelFor(null);             lblType = addLabel(getMsg("label.pstype"), gridbag, c);            lblType.setLabelFor(null);             c.gridwidth = 1;            addToGB(new JLabel(getMsg("label.info"), JLabel.TRAILING),                    this, gridbag, c);            c.gridwidth = GridBagConstraints.RELATIVE;            lblInfo = new JLabel();            lblInfo.setLabelFor(null);             addToGB(lblInfo, this, gridbag, c);             c.gridwidth = GridBagConstraints.REMAINDER;            cbPrintToFile = createCheckBox("checkbox.printtofile", this);            addToGB(cbPrintToFile, this, gridbag, c);             filePermission = allowedToPrintToFile();        }         public boolean isPrintToFileSelected() {            return cbPrintToFile.isSelected();        }         private JLabel addLabel(String text,                                GridBagLayout gridbag, GridBagConstraints c)        {            c.gridwidth = 1;            addToGB(new JLabel(text, JLabel.TRAILING), this, gridbag, c);             c.gridwidth = GridBagConstraints.REMAINDER;            JLabel label = new JLabel();            addToGB(label, this, gridbag, c);             return label;        }         public void actionPerformed(ActionEvent e) {            Object source = e.getSource();             if (source == btnProperties) {                if (uiFactory != null) {                    JDialog dialog = (JDialog)uiFactory.getUI(                                               ServiceUIFactory.MAIN_UIROLE,                                               ServiceUIFactory.JDIALOG_UI);                     if (dialog != null) {                        dialog.show();                    } else {                        // REMIND: may want to notify the user why we're                        //         disabling the button                        btnProperties.setEnabled(false);                    }                }            }        }         public void itemStateChanged(ItemEvent e) {            if (e.getStateChange() == ItemEvent.SELECTED) {                int index = cbName.getSelectedIndex();                 if ((index >= 0) && (index < services.length)) {                    if (!services[index].equals(psCurrent)) {                        psCurrent = services[index];                        uiFactory = psCurrent.getServiceUIFactory();                        changedService = true;                         Destination dest =                            (Destination)asOriginal.get(Destination.class);                        // to preserve the state of Print To File                        if ((dest != null || isPrintToFileSelected())                            && psCurrent.isAttributeCategorySupported(                                                        Destination.class)) {                             if (dest != null) {                                asCurrent.add(dest);                            } else {                                dest = (Destination)psCurrent.                                    getDefaultAttributeValue(Destination.class);                                // "dest" should not be null. The following code                                // is only added to safeguard against a possible                                // buggy implementation of a PrintService having a                                // null default Destination.                                if (dest == null) {                                    try {                                        dest =                                            new Destination(new URI("file:out.prn"));                                    } catch (URISyntaxException ue) {                                    }                                }                                 if (dest != null) {                                    asCurrent.add(dest);                                }                            }                        } else {                            asCurrent.remove(Destination.class);                        }                    }                }            }        }         public void popupMenuWillBecomeVisible(PopupMenuEvent e) {            changedService = false;        }         public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {            if (changedService) {                changedService = false;                updatePanels();            }        }         public void popupMenuCanceled(PopupMenuEvent e) {        }         /**         * We disable the "Print To File" checkbox if this returns false         */        private boolean allowedToPrintToFile() {            try {                throwPrintToFile();                return true;            } catch (SecurityException e) {                return false;            }        }         /**         * Break this out as it may be useful when we allow API to         * specify printing to a file. In that case its probably right         * to throw a SecurityException if the permission is not granted.         */        private void throwPrintToFile() {            SecurityManager security = System.getSecurityManager();            if (security != null) {                if (printToFilePermission == null) {                    printToFilePermission =                        new FilePermission("<<ALL FILES>>", "read,write");                }                security.checkPermission(printToFilePermission);            }        }         public void updateInfo() {            Class dstCategory = Destination.class;            boolean dstSupported = false;            boolean dstSelected = false;            boolean dstAllowed = filePermission ?                allowedToPrintToFile() : false;             // setup Destination (print-to-file) widgets            if (psCurrent.isAttributeCategorySupported(dstCategory)) {                dstSupported = true;            }            Destination dst = (Destination)asCurrent.get(dstCategory);            if (dst != null) {                dstSelected = true;            }            cbPrintToFile.setEnabled(dstSupported && dstAllowed);            cbPrintToFile.setSelected(dstSelected && dstAllowed                                      && dstSupported);             // setup PrintService information widgets            Attribute type = psCurrent.getAttribute(PrinterMakeAndModel.class);            if (type != null) {                lblType.setText(type.toString());            }            Attribute status =                psCurrent.getAttribute(PrinterIsAcceptingJobs.class);            if (status != null) {                lblStatus.setText(getMsg(status.toString()));            }            Attribute info = psCurrent.getAttribute(PrinterInfo.class);            if (info != null) {                lblInfo.setText(info.toString());            }            btnProperties.setEnabled(uiFactory != null);        }    }     private class PrintRangePanel extends JPanel        implements ActionListener, FocusListener    {        private final String strTitle = getMsg("border.printrange");        private final PageRanges prAll = new PageRanges(1, Integer.MAX_VALUE);        private JRadioButton rbAll, rbPages, rbSelect;        private JFormattedTextField tfRangeFrom, tfRangeTo;        private JLabel lblRangeTo;        private boolean prSupported;         public PrintRangePanel() {            super();             GridBagLayout gridbag = new GridBagLayout();            GridBagConstraints c = new GridBagConstraints();             setLayout(gridbag);            setBorder(BorderFactory.createTitledBorder(strTitle));             c.fill = GridBagConstraints.BOTH;            c.insets = compInsets;            c.gridwidth = GridBagConstraints.REMAINDER;             ButtonGroup bg = new ButtonGroup();            JPanel pnlTop = new JPanel(new FlowLayout(FlowLayout.LEADING));            rbAll = createRadioButton("radiobutton.rangeall", this);            rbAll.setSelected(true);            bg.add(rbAll);            pnlTop.add(rbAll);            addToGB(pnlTop, this, gridbag, c);             // Selection never seemed to work so I'm commenting this part.            /*            if (isAWT) {                JPanel pnlMiddle  =                    new JPanel(new FlowLayout(FlowLayout.LEADING));                rbSelect =                    createRadioButton("radiobutton.selection", this);                bg.add(rbSelect);                pnlMiddle.add(rbSelect);                addToGB(pnlMiddle, this, gridbag, c);            }            */             JPanel pnlBottom = new JPanel(new FlowLayout(FlowLayout.LEADING));            rbPages = createRadioButton("radiobutton.rangepages", this);            bg.add(rbPages);            pnlBottom.add(rbPages);            DecimalFormat format = new DecimalFormat("####0");            format.setMinimumFractionDigits(0);            format.setMaximumFractionDigits(0);            format.setMinimumIntegerDigits(0);            format.setMaximumIntegerDigits(5);            format.setParseIntegerOnly(true);            format.setDecimalSeparatorAlwaysShown(false);            NumberFormatter nf = new NumberFormatter(format);            nf.setMinimum(new Integer(1));            nf.setMaximum(new Integer(Integer.MAX_VALUE));            nf.setAllowsInvalid(true);            nf.setCommitsOnValidEdit(true);            tfRangeFrom = new JFormattedTextField(nf);            tfRangeFrom.setColumns(4);            tfRangeFrom.setEnabled(false);            tfRangeFrom.addActionListener(this);            tfRangeFrom.addFocusListener(this);            tfRangeFrom.setFocusLostBehavior(                JFormattedTextField.PERSIST);            tfRangeFrom.getAccessibleContext().setAccessibleName(                                          getMsg("radiobutton.rangepages"));            pnlBottom.add(tfRangeFrom);            lblRangeTo = new JLabel(getMsg("label.rangeto"));            lblRangeTo.setEnabled(false);            pnlBottom.add(lblRangeTo);            NumberFormatter nfto;            try {                nfto = (NumberFormatter)nf.clone();            } catch (CloneNotSupportedException e) {                nfto = new NumberFormatter();            }            tfRangeTo = new JFormattedTextField(nfto);            tfRangeTo.setColumns(4);            tfRangeTo.setEnabled(false);            tfRangeTo.addFocusListener(this);            tfRangeTo.getAccessibleContext().setAccessibleName(                                          getMsg("label.rangeto"));            pnlBottom.add(tfRangeTo);            addToGB(pnlBottom, this, gridbag, c);        }         public void actionPerformed(ActionEvent e) {            Object source = e.getSource();            SunPageSelection select = SunPageSelection.ALL;             setupRangeWidgets();             if (source == rbAll) {                asCurrent.add(prAll);            } else if (source == rbSelect) {                select = SunPageSelection.SELECTION;            } else if (source == rbPages ||                       source == tfRangeFrom ||                       source == tfRangeTo) {

Lines 501–1000 of 2839. Previous lines