2839 lines · 102.4 KB · Java
RawDownload
                } else {                    leftMargin.setValue(lmObj);                    rightMargin.setValue(rmObj);                    topMargin.setValue(tmObj);                    bottomMargin.setValue(bmObj);                 }            }        }         /*         * This method either accepts the values and creates a new         * MediaPrintableArea, or does nothing.         * It should not attempt to create a printable area from anything         * other than the exact values passed in.         * But REMIND/TBD: it would be user friendly to replace margins the         * user entered but are out of bounds with the minimum.         * At that point this method will need to take responsibility for         * updating the "stored" values and the UI.         */        private MediaPrintableArea validateMargins(float lm, float rm,                                                   float tm, float bm) {             Class mpaCategory = MediaPrintableArea.class;            MediaPrintableArea mpa;            MediaPrintableArea mpaMax = null;            MediaSize mediaSize = null;             Media media = (Media)asCurrent.get(Media.class);            if (media == null || !(media instanceof MediaSizeName)) {                media = (Media)psCurrent.getDefaultAttributeValue(Media.class);            }            if (media != null && (media instanceof MediaSizeName)) {                MediaSizeName msn = (MediaSizeName)media;                mediaSize = MediaSize.getMediaSizeForName(msn);            }            if (mediaSize == null) {                mediaSize = new MediaSize(8.5f, 11f, Size2DSyntax.INCH);            }             if (media != null) {                PrintRequestAttributeSet tmpASet =                    new HashPrintRequestAttributeSet(asCurrent);                tmpASet.add(media);                 Object values =                    psCurrent.getSupportedAttributeValues(mpaCategory,                                                          docFlavor,                                                          tmpASet);                if (values instanceof MediaPrintableArea[] &&                    ((MediaPrintableArea[])values).length > 0) {                    mpaMax = ((MediaPrintableArea[])values)[0];                 }            }            if (mpaMax == null) {                mpaMax = new MediaPrintableArea(0f, 0f,                                                mediaSize.getX(units),                                                mediaSize.getY(units),                                                units);            }             float wid = mediaSize.getX(units);            float hgt = mediaSize.getY(units);            float pax = lm;            float pay = tm;            float paw = wid - lm - rm;            float pah = hgt - tm - bm;             if (paw <= 0f || pah <= 0f || pax < 0f || pay < 0f ||                pax < mpaMax.getX(units) || paw > mpaMax.getWidth(units) ||                pay < mpaMax.getY(units) || pah > mpaMax.getHeight(units)) {                return null;            } else {                return new MediaPrintableArea(lm, tm, paw, pah, units);            }        }         /* This is complex as a MediaPrintableArea is valid only within         * a particular context of media size.         * So we need a MediaSize as well as a MediaPrintableArea.         * MediaSize can be obtained from MediaSizeName.         * If the application specifies a MediaPrintableArea, we accept it         * to the extent its valid for the Media they specify. If they         * don't specify a Media, then the default is assumed.         *         * If an application doesn't define a MediaPrintableArea, we need to         * create a suitable one, this is created using the specified (or         * default) Media and default 1 inch margins. This is validated         * against the paper in case this is too large for tiny media.         */        public void updateInfo() {             if (isAWT) {                leftMargin.setEnabled(false);                rightMargin.setEnabled(false);                topMargin.setEnabled(false);                bottomMargin.setEnabled(false);                lblLeft.setEnabled(false);                lblRight.setEnabled(false);                lblTop.setEnabled(false);                lblBottom.setEnabled(false);                return;            }             Class mpaCategory = MediaPrintableArea.class;            MediaPrintableArea mpa =                 (MediaPrintableArea)asCurrent.get(mpaCategory);            MediaPrintableArea mpaMax = null;            MediaSize mediaSize = null;             Media media = (Media)asCurrent.get(Media.class);            if (media == null || !(media instanceof MediaSizeName)) {                media = (Media)psCurrent.getDefaultAttributeValue(Media.class);            }            if (media != null && (media instanceof MediaSizeName)) {                MediaSizeName msn = (MediaSizeName)media;                mediaSize = MediaSize.getMediaSizeForName(msn);            }            if (mediaSize == null) {                mediaSize = new MediaSize(8.5f, 11f, Size2DSyntax.INCH);            }             if (media != null) {                PrintRequestAttributeSet tmpASet =                    new HashPrintRequestAttributeSet(asCurrent);                tmpASet.add(media);                 Object values =                    psCurrent.getSupportedAttributeValues(mpaCategory,                                                          docFlavor,                                                          tmpASet);                if (values instanceof MediaPrintableArea[] &&                    ((MediaPrintableArea[])values).length > 0) {                    mpaMax = ((MediaPrintableArea[])values)[0];                 } else if (values instanceof MediaPrintableArea) {                    mpaMax = (MediaPrintableArea)values;                }            }            if (mpaMax == null) {                mpaMax = new MediaPrintableArea(0f, 0f,                                                mediaSize.getX(units),                                                mediaSize.getY(units),                                                units);            }             /*             * At this point we now know as best we can :-             * - the media size             * - the maximum corresponding printable area             * - the media printable area specified by the client, if any.             * The next step is to create a default MPA if none was specified.             * 1" margins are used unless they are disproportionately             * large compared to the size of the media.             */             float wid = mediaSize.getX(MediaPrintableArea.INCH);            float hgt = mediaSize.getY(MediaPrintableArea.INCH);            float maxMarginRatio = 5f;            float xMgn, yMgn;            if (wid > maxMarginRatio) {                xMgn = 1f;            } else {                xMgn = wid / maxMarginRatio;            }            if (hgt > maxMarginRatio) {                yMgn = 1f;            } else {                yMgn = hgt / maxMarginRatio;            }             if (mpa == null) {                mpa = new MediaPrintableArea(xMgn, yMgn,                                             wid-(2*xMgn), hgt-(2*yMgn),                                             MediaPrintableArea.INCH);                asCurrent.add(mpa);            }            float pax = mpa.getX(units);            float pay = mpa.getY(units);            float paw = mpa.getWidth(units);            float pah = mpa.getHeight(units);            float paxMax = mpaMax.getX(units);            float payMax = mpaMax.getY(units);            float pawMax = mpaMax.getWidth(units);            float pahMax = mpaMax.getHeight(units);              boolean invalid = false;             // If the paper is set to something which is too small to            // accommodate a specified printable area, perhaps carried            // over from a larger paper, the adjustment that needs to be            // performed should seem the most natural from a user's viewpoint.            // Since the user is specifying margins, then we are biased            // towards keeping the margins as close to what is specified as            // possible, shrinking or growing the printable area.            // But the API uses printable area, so you need to know the            // media size in which the margins were previously interpreted,            // or at least have a record of the margins.            // In the case that this is the creation of this UI we do not            // have this record, so we are somewhat reliant on the client            // to supply a reasonable default            wid = mediaSize.getX(units);            hgt = mediaSize.getY(units);            if (lmVal >= 0f) {                invalid = true;                 if (lmVal + rmVal > wid) {                    // margins impossible, but maintain P.A if can                    if (paw > pawMax) {                        paw = pawMax;                    }                    // try to centre the printable area.                    pax = (wid - paw)/2f;                } else {                    pax = (lmVal >= paxMax) ? lmVal : paxMax;                    paw = wid - pax - rmVal;                }                if (tmVal + bmVal > hgt) {                    if (pah > pahMax) {                        pah = pahMax;                    }                    pay = (hgt - pah)/2f;                } else {                    pay = (tmVal >= payMax) ? tmVal : payMax;                    pah = hgt - pay - bmVal;                }            }            if (pax < paxMax) {                invalid = true;                pax = paxMax;            }            if (pay < payMax) {                invalid = true;                pay = payMax;            }            if (paw > pawMax) {                invalid = true;                paw = pawMax;            }            if (pah > pahMax) {                invalid = true;                pah = pahMax;            }             if ((pax + paw > paxMax + pawMax) || (paw <= 0f)) {                invalid = true;                pax = paxMax;                paw = pawMax;            }            if ((pay + pah > payMax + pahMax) || (pah <= 0f)) {                invalid = true;                pay = payMax;                pah = pahMax;            }             if (invalid) {                mpa = new MediaPrintableArea(pax, pay, paw, pah, units);                asCurrent.add(mpa);            }             /* We now have a valid printable area.             * Turn it into margins, using the mediaSize             */            lmVal = pax;            tmVal = pay;            rmVal = mediaSize.getX(units) - pax - paw;            bmVal = mediaSize.getY(units) - pay - pah;             lmObj = new Float(lmVal);            rmObj = new Float(rmVal);            tmObj = new Float(tmVal);            bmObj = new Float(bmVal);             /* Now we know the values to use, we need to assign them             * to the fields appropriate for the orientation.             * Note: if orientation changes this method must be called.             */            Class orCategory = OrientationRequested.class;            OrientationRequested or =                (OrientationRequested)asCurrent.get(orCategory);             if (or == null) {                or = (OrientationRequested)                     psCurrent.getDefaultAttributeValue(orCategory);            }             Float tmp;             if (or == OrientationRequested.REVERSE_PORTRAIT) {                tmp = lmObj; lmObj = rmObj; rmObj = tmp;                tmp = tmObj; tmObj = bmObj; bmObj = tmp;            }  else if (or == OrientationRequested.LANDSCAPE) {                tmp = lmObj;                lmObj = bmObj;                bmObj = rmObj;                rmObj = tmObj;                tmObj = tmp;            }  else if (or == OrientationRequested.REVERSE_LANDSCAPE) {                tmp = lmObj;                lmObj = tmObj;                tmObj = rmObj;                rmObj = bmObj;                bmObj = tmp;            }             leftMargin.setValue(lmObj);            rightMargin.setValue(rmObj);            topMargin.setValue(tmObj);            bottomMargin.setValue(bmObj);        }    }     private class MediaPanel extends JPanel implements ItemListener {         private final String strTitle = getMsg("border.media");        private JLabel lblSize, lblSource;        private JComboBox cbSize, cbSource;        private Vector sizes = new Vector();        private Vector sources = new Vector();        private MarginsPanel pnlMargins = null;         public MediaPanel() {            super();             GridBagLayout gridbag = new GridBagLayout();            GridBagConstraints c = new GridBagConstraints();             setLayout(gridbag);            setBorder(BorderFactory.createTitledBorder(strTitle));             cbSize = new JComboBox();            cbSource = new JComboBox();             c.fill = GridBagConstraints.BOTH;            c.insets = compInsets;            c.weighty = 1.0;             c.weightx = 0.0;            lblSize = new JLabel(getMsg("label.size"), JLabel.TRAILING);            lblSize.setDisplayedMnemonic(getMnemonic("label.size"));            lblSize.setLabelFor(cbSize);            addToGB(lblSize, this, gridbag, c);            c.weightx = 1.0;            c.gridwidth = GridBagConstraints.REMAINDER;            addToGB(cbSize, this, gridbag, c);             c.weightx = 0.0;            c.gridwidth = 1;            lblSource = new JLabel(getMsg("label.source"), JLabel.TRAILING);            lblSource.setDisplayedMnemonic(getMnemonic("label.source"));            lblSource.setLabelFor(cbSource);            addToGB(lblSource, this, gridbag, c);            c.gridwidth = GridBagConstraints.REMAINDER;            addToGB(cbSource, this, gridbag, c);        }         private String getMediaName(String key) {            try {                // replace characters that would be invalid in                // a resource key with valid characters                String newkey = key.replace(' ', '-');                newkey = newkey.replace('#', 'n');                 return messageRB.getString(newkey);            } catch (java.util.MissingResourceException e) {                return key;            }        }         public void itemStateChanged(ItemEvent e) {            Object source = e.getSource();             if (e.getStateChange() == ItemEvent.SELECTED) {                if (source == cbSize) {                    int index = cbSize.getSelectedIndex();                     if ((index >= 0) && (index < sizes.size())) {                        if ((cbSource.getItemCount() > 1) &&                            (cbSource.getSelectedIndex() >= 1))                        {                            int src = cbSource.getSelectedIndex() - 1;                            MediaTray mt = (MediaTray)sources.get(src);                            asCurrent.add(new SunAlternateMedia(mt));                        }                        asCurrent.add((MediaSizeName)sizes.get(index));                    }                } else if (source == cbSource) {                    int index = cbSource.getSelectedIndex();                     if ((index >= 1) && (index < (sources.size() + 1))) {                       asCurrent.remove(SunAlternateMedia.class);                       MediaTray newTray = (MediaTray)sources.get(index - 1);                       Media m = (Media)asCurrent.get(Media.class);                       if (m == null || m instanceof MediaTray) {                           asCurrent.add(newTray);                       } else if (m instanceof MediaSizeName) {                           MediaSizeName msn = (MediaSizeName)m;                           Media def = (Media)psCurrent.getDefaultAttributeValue(Media.class);                           if (def instanceof MediaSizeName && def.equals(msn)) {                               asCurrent.add(newTray);                           } else {                               /* Non-default paper size, so need to store tray                                * as SunAlternateMedia                                */                               asCurrent.add(new SunAlternateMedia(newTray));                           }                       }                    } else if (index == 0) {                        asCurrent.remove(SunAlternateMedia.class);                        if (cbSize.getItemCount() > 0) {                            int size = cbSize.getSelectedIndex();                            asCurrent.add((MediaSizeName)sizes.get(size));                        }                    }                }            // orientation affects display of margins.                if (pnlMargins != null) {                    pnlMargins.updateInfo();                }            }        }          /* this is ad hoc to keep things simple */        public void addMediaListener(MarginsPanel pnl) {            pnlMargins = pnl;        }        public void updateInfo() {            Class mdCategory = Media.class;            Class amCategory = SunAlternateMedia.class;            boolean mediaSupported = false;             cbSize.removeItemListener(this);            cbSize.removeAllItems();            cbSource.removeItemListener(this);            cbSource.removeAllItems();            cbSource.addItem(getMediaName("auto-select"));             sizes.clear();            sources.clear();             if (psCurrent.isAttributeCategorySupported(mdCategory)) {                mediaSupported = true;                 Object values =                    psCurrent.getSupportedAttributeValues(mdCategory,                                                          docFlavor,                                                          asCurrent);                 if (values instanceof Media[]) {                    Media[] media = (Media[])values;                     for (int i = 0; i < media.length; i++) {                        Media medium = media[i];                         if (medium instanceof MediaSizeName) {                            sizes.add(medium);                            cbSize.addItem(getMediaName(medium.toString()));                        } else if (medium instanceof MediaTray) {                            sources.add(medium);                            cbSource.addItem(getMediaName(medium.toString()));                        }                    }                }            }             boolean msSupported = (mediaSupported && (sizes.size() > 0));            lblSize.setEnabled(msSupported);            cbSize.setEnabled(msSupported);             if (isAWT) {                cbSource.setEnabled(false);                lblSource.setEnabled(false);            } else {                cbSource.setEnabled(mediaSupported);            }             if (mediaSupported) {                 Media medium = (Media)asCurrent.get(mdCategory);                // initialize size selection to default                Media defMedia = (Media)psCurrent.getDefaultAttributeValue(mdCategory);                if (defMedia instanceof MediaSizeName) {                    cbSize.setSelectedIndex(sizes.size() > 0 ? sizes.indexOf(defMedia) : -1);                }                 if (medium == null ||                    !psCurrent.isAttributeValueSupported(medium,                                                         docFlavor, asCurrent)) {                     medium = defMedia;                     if (medium == null) {                        if (sizes.size() > 0) {                            medium = (Media)sizes.get(0);                        }                    }

Lines 1501–2000 of 2839. Previous lines