jdk/src/windows/classes/sun/print/Win32PrintService.java
author dav
Mon, 07 Apr 2008 14:53:51 +0400
changeset 438 2ae294e4518c
parent 2 90ce3da70b43
child 715 f16baef3a20e
permissions -rw-r--r--
6613529: Avoid duplicate object creation within JDK packages Summary: avoid using constructors when unique values are not necessary Reviewed-by: volk, igor, peterz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2000-2006 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.print;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.URI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.URISyntaxException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.print.DocFlavor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.print.DocPrintJob;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.print.PrintService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.print.ServiceUIFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.print.attribute.Attribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.print.attribute.AttributeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.print.attribute.AttributeSetUtilities;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.print.attribute.EnumSyntax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.print.attribute.HashAttributeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.print.attribute.PrintServiceAttribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.print.attribute.PrintServiceAttributeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.print.attribute.HashPrintServiceAttributeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.print.attribute.standard.PrinterName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import javax.print.attribute.standard.PrinterIsAcceptingJobs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import javax.print.attribute.standard.QueuedJobCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import javax.print.attribute.standard.JobName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import javax.print.attribute.standard.RequestingUserName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import javax.print.attribute.standard.Chromaticity;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import javax.print.attribute.standard.Copies;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import javax.print.attribute.standard.CopiesSupported;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import javax.print.attribute.standard.Destination;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import javax.print.attribute.standard.Fidelity;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import javax.print.attribute.standard.Media;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import javax.print.attribute.standard.MediaSizeName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
import javax.print.attribute.standard.MediaSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import javax.print.attribute.standard.MediaTray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
import javax.print.attribute.standard.MediaPrintableArea;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
import javax.print.attribute.standard.OrientationRequested;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
import javax.print.attribute.standard.PageRanges;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
import javax.print.attribute.standard.PrinterState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
import javax.print.attribute.standard.PrinterStateReason;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
import javax.print.attribute.standard.PrinterStateReasons;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
import javax.print.attribute.standard.Severity;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
import javax.print.attribute.standard.Sides;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
import javax.print.attribute.standard.ColorSupported;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
import javax.print.attribute.standard.PrintQuality;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
import javax.print.attribute.ResolutionSyntax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
import javax.print.attribute.standard.PrinterResolution;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
import javax.print.attribute.standard.SheetCollate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
import javax.print.event.PrintServiceAttributeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
import sun.print.SunPrinterJobService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
public class Win32PrintService implements PrintService, AttributeUpdater,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                                          SunPrinterJobService {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public static MediaSize[] predefMedia;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        Class c = Win32MediaSize.class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private static final DocFlavor[] supportedFlavors = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        DocFlavor.BYTE_ARRAY.GIF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        DocFlavor.INPUT_STREAM.GIF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        DocFlavor.URL.GIF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        DocFlavor.BYTE_ARRAY.JPEG,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        DocFlavor.INPUT_STREAM.JPEG,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        DocFlavor.URL.JPEG,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        DocFlavor.BYTE_ARRAY.PNG,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        DocFlavor.INPUT_STREAM.PNG,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        DocFlavor.URL.PNG,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        DocFlavor.SERVICE_FORMATTED.PAGEABLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        DocFlavor.SERVICE_FORMATTED.PRINTABLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        DocFlavor.BYTE_ARRAY.AUTOSENSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        DocFlavor.URL.AUTOSENSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        DocFlavor.INPUT_STREAM.AUTOSENSE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /* let's try to support a few of these */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private static final Class[] serviceAttrCats = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        PrinterName.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        PrinterIsAcceptingJobs.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        QueuedJobCount.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        ColorSupported.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /*  it turns out to be inconvenient to store the other categories
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *  separately because many attributes are in multiple categories.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    private static Class[] otherAttrCats = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        JobName.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        RequestingUserName.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        Copies.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        Destination.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        OrientationRequested.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        PageRanges.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        Media.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        MediaPrintableArea.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        Fidelity.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        // We support collation on 2D printer jobs, even if the driver can't.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        SheetCollate.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        SunAlternateMedia.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        Chromaticity.class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * This table together with methods findWin32Media and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * findMatchingMediaSizeNameMM are declared public as these are also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * used in WPrinterJob.java.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public static final MediaSizeName[] dmPaperToPrintService = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
      MediaSizeName.NA_LETTER, MediaSizeName.NA_LETTER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
      MediaSizeName.TABLOID, MediaSizeName.LEDGER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
      MediaSizeName.NA_LEGAL, MediaSizeName.INVOICE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
      MediaSizeName.EXECUTIVE, MediaSizeName.ISO_A3,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
      MediaSizeName.ISO_A4, MediaSizeName.ISO_A4,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
      MediaSizeName.ISO_A5, MediaSizeName.JIS_B4,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
      MediaSizeName.JIS_B5, MediaSizeName.FOLIO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
      MediaSizeName.QUARTO, MediaSizeName.NA_10X14_ENVELOPE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
      MediaSizeName.B, MediaSizeName.NA_LETTER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
      MediaSizeName.NA_NUMBER_9_ENVELOPE, MediaSizeName.NA_NUMBER_10_ENVELOPE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
      MediaSizeName.NA_NUMBER_11_ENVELOPE, MediaSizeName.NA_NUMBER_12_ENVELOPE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
      MediaSizeName.NA_NUMBER_14_ENVELOPE, MediaSizeName.C,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
      MediaSizeName.D, MediaSizeName.E,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
      MediaSizeName.ISO_DESIGNATED_LONG, MediaSizeName.ISO_C5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
      MediaSizeName.ISO_C3, MediaSizeName.ISO_C4,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
      MediaSizeName.ISO_C6, MediaSizeName.ITALY_ENVELOPE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
      MediaSizeName.ISO_B4, MediaSizeName.ISO_B5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
      MediaSizeName.ISO_B6, MediaSizeName.ITALY_ENVELOPE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
      MediaSizeName.MONARCH_ENVELOPE, MediaSizeName.PERSONAL_ENVELOPE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
      MediaSizeName.NA_10X15_ENVELOPE, MediaSizeName.NA_9X12_ENVELOPE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
      MediaSizeName.FOLIO, MediaSizeName.ISO_B4,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
      MediaSizeName.JAPANESE_POSTCARD, MediaSizeName.NA_9X11_ENVELOPE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    private static final MediaTray[] dmPaperBinToPrintService = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
      MediaTray.TOP, MediaTray.BOTTOM, MediaTray.MIDDLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
      MediaTray.MANUAL, MediaTray.ENVELOPE, Win32MediaTray.ENVELOPE_MANUAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
      Win32MediaTray.AUTO, Win32MediaTray.TRACTOR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
      Win32MediaTray.SMALL_FORMAT, Win32MediaTray.LARGE_FORMAT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
      MediaTray.LARGE_CAPACITY, null, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
      MediaTray.MAIN, Win32MediaTray.FORMSOURCE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    // from wingdi.h
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    private static int DM_PAPERSIZE = 0x2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    private static int DM_PRINTQUALITY = 0x400;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    private static int DM_YRESOLUTION = 0x2000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    private static final int DMRES_MEDIUM = -3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    private static final int DMRES_HIGH = -4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    private static final int DMORIENT_LANDSCAPE = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    private static final int DMDUP_VERTICAL = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    private static final int DMDUP_HORIZONTAL = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    private static final int DMCOLLATE_TRUE = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    // media sizes with indices above dmPaperToPrintService' length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    private static final int DMPAPER_A2 = 66;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    private static final int DMPAPER_A6 = 70;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    private static final int DMPAPER_B6_JIS = 88;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    // Bit settings for getPrinterCapabilities which matches that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    // of native getCapabilities in WPrinterJob.cpp
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    private static final int DEVCAP_COLOR = 0x0001;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    private static final int DEVCAP_DUPLEX = 0x0002;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    private static final int DEVCAP_COLLATE = 0x0004;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    private static final int DEVCAP_QUALITY = 0x0008;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    private static final int DEVCAP_POSTSCRIPT = 0x0010;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    private String printer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    private PrinterName name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    private String port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    transient private PrintServiceAttributeSet lastSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    transient private ServiceNotifier notifier = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    private MediaSizeName[] mediaSizeNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    private MediaPrintableArea[] mediaPrintables;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    private MediaTray[] mediaTrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    private PrinterResolution[] printRes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    private int nCopies;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    private int prnCaps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    private int[] defaultSettings;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    private boolean gotTrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    private boolean gotCopies;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    private boolean mediaInitialized;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    private ArrayList idList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    private MediaSize[] mediaSizes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    private boolean isInvalid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    Win32PrintService(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            throw new IllegalArgumentException("null printer name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        printer = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        // initialize flags
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        mediaInitialized = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        gotTrays = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        gotCopies = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        isInvalid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        printRes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        prnCaps = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        defaultSettings = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        port = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    public void invalidateService() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        isInvalid = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        return printer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    private PrinterName getPrinterName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            name = new PrinterName(printer, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    public int findPaperID(MediaSizeName msn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        if (msn instanceof Win32MediaSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            Win32MediaSize winMedia = (Win32MediaSize)msn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            return winMedia.getDMPaper();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            for (int id=0; id<dmPaperToPrintService.length;id++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                if (dmPaperToPrintService[id].equals(msn)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                    return id+1; // DMPAPER_LETTER == 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            if (msn.equals(MediaSizeName.ISO_A2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                return DMPAPER_A2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            else if (msn.equals(MediaSizeName.ISO_A6)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                return DMPAPER_A6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            else if (msn.equals(MediaSizeName.JIS_B6)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                return DMPAPER_B6_JIS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    public MediaTray findMediaTray(int dmBin) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        if (dmBin >= 1 && dmBin <= dmPaperBinToPrintService.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            return dmPaperBinToPrintService[dmBin-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        MediaTray[] trays = getMediaTrays();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        if (trays != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            for (int i=0;i<trays.length;i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                if(trays[i] instanceof Win32MediaTray) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                    Win32MediaTray win32Tray = (Win32MediaTray)trays[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                    if (win32Tray.winID == dmBin) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                        return win32Tray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        return Win32MediaTray.AUTO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    public MediaSizeName findWin32Media(int dmIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        if (dmIndex >= 1 && dmIndex <= dmPaperToPrintService.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
           switch(dmIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            /* matching media sizes with indices beyond
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
               dmPaperToPrintService's length */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            case DMPAPER_A2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                return MediaSizeName.ISO_A2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            case DMPAPER_A6:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                return MediaSizeName.ISO_A6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            case DMPAPER_B6_JIS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                return MediaSizeName.JIS_B6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                return dmPaperToPrintService[dmIndex - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    private boolean addToUniqueList(ArrayList msnList, MediaSizeName mediaName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        MediaSizeName msn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        for (int i=0; i< msnList.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            msn = (MediaSizeName)msnList.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            if (msn == mediaName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        msnList.add(mediaName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    private synchronized void initMedia() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        if (mediaInitialized == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        mediaInitialized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        int[] media = getAllMediaIDs(printer, getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        if (media == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        ArrayList msnList = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        ArrayList printableList = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        MediaSizeName mediaName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        boolean added;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        boolean queryFailure = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        float[] prnArea;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        // Get all mediaSizes supported by the printer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        // We convert media to ArrayList idList and pass this to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        // function for getting mediaSizes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        // This is to ensure that mediaSizes and media IDs have 1-1 correspondence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        // We remove from ID list any invalid mediaSize.  Though this is rare,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        // it happens in HP 4050 German driver.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        idList = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        for (int i=0; i < media.length; i++) {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   351
            idList.add(Integer.valueOf(media[i]));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        mediaSizes = getMediaSizes(idList, media);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        for (int i = 0; i < idList.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            // match Win ID with our predefined ID using table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            mediaName = findWin32Media(((Integer)idList.get(i)).intValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            // Verify that this standard size is the same size as that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            // reported by the driver. This should be the case except when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            // the driver is mis-using a standard windows paper ID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            if (mediaName != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                idList.size() == mediaSizes.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                MediaSize win32Size = MediaSize.getMediaSizeForName(mediaName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                MediaSize driverSize = mediaSizes[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                int error = 2540; // == 1/10"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                if (Math.abs(win32Size.getX(1)-driverSize.getX(1)) > error ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                    Math.abs(win32Size.getY(1)-driverSize.getY(1)) > error)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                   mediaName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            // No match found, then we get the MediaSizeName out of the MediaSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            // This requires 1-1 correspondence, lengths must be checked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            if ((mediaName == null) && (idList.size() == mediaSizes.length)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                mediaName = mediaSizes[i].getMediaSizeName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            // Add mediaName to the msnList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            if (mediaName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                added = addToUniqueList(msnList, mediaName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                // get MediaPrintableArea only for supported MediaSizeName ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                if (added && !queryFailure) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                    prnArea=getMediaPrintableArea(printer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                                                  ((Integer)idList.get(i)).intValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                    if (prnArea != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                            MediaPrintableArea mpa =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                                new MediaPrintableArea(prnArea[0],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                                                       prnArea[1],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                                                       prnArea[2],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                                                       prnArea[3],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                                                       MediaPrintableArea.INCH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                            printableList.add(mpa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                        } catch (IllegalArgumentException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                        // Calling getMediaPrintableArea causes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                        // much overhead so if first attempt failed, we should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                        // just bail out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                        if (i==0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                            queryFailure = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        // init mediaSizeNames
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        mediaSizeNames = new MediaSizeName[msnList.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        msnList.toArray(mediaSizeNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        // init mediaPrintables
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        mediaPrintables = new MediaPrintableArea[printableList.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        printableList.toArray(mediaPrintables);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    private synchronized MediaTray[] getMediaTrays() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        if (gotTrays == true && mediaTrays != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            return mediaTrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        String prnPort = getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        int[] mediaTr = getAllMediaTrays(printer, prnPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        String[] winMediaTrayNames = getAllMediaTrayNames(printer, prnPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        if ((mediaTr == null) || (winMediaTrayNames == null)){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        /* first count how many valid bins there are so we can allocate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
         * an array of the correct size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        int nTray = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        for (int i=0; i < mediaTr.length ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            if (mediaTr[i] > 0) nTray++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        MediaTray[] arr = new MediaTray[nTray];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        int dmBin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        for (int i = 0, j=0; i < mediaTr.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            dmBin = mediaTr[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            if (dmBin > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                // check for unsupported DMBINs and create new Win32MediaTray
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                if ((dmBin > dmPaperBinToPrintService.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                    || (dmPaperBinToPrintService[dmBin-1] == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                    arr[j++] = new Win32MediaTray(dmBin, winMediaTrayNames[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                    arr[j++] = dmPaperBinToPrintService[dmBin-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            // no else - For invalid ids, just ignore it because assigning a "default"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            // value may result in duplicate trays.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        mediaTrays = arr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        gotTrays = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        return mediaTrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    private boolean isSameSize(float w1, float h1, float w2, float h2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        float diffX = w1 - w2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        float diffY = h1 - h2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        // Get diff of reverse dimensions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        // EPSON Stylus COLOR 860 reverses envelope's width & height
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        float diffXrev = w1 - h2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        float diffYrev = h1 - w2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        if (((Math.abs(diffX)<=1) && (Math.abs(diffY)<=1)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            ((Math.abs(diffXrev)<=1) && (Math.abs(diffYrev)<=1))){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
          return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
          return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    public MediaSizeName findMatchingMediaSizeNameMM (float w, float h){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        if (predefMedia != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            for (int k=0; k<predefMedia.length;k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                if (predefMedia[k] == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                if (isSameSize(predefMedia[k].getX(MediaSize.MM),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                               predefMedia[k].getY(MediaSize.MM),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                               w, h)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                  return predefMedia[k].getMediaSizeName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    private MediaSize[] getMediaSizes(ArrayList idList, int[] media) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        String prnPort = getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        int[] mediaSz = getAllMediaSizes(printer, prnPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        String[] winMediaNames = getAllMediaNames(printer, prnPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        MediaSizeName msn = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        MediaSize ms = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        float wid, ht;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        if ((mediaSz == null) || (winMediaNames == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        int nMedia = mediaSz.length/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        ArrayList msList = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        for (int i = 0; i < nMedia; i++, ms=null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            wid = mediaSz[i*2]/10f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            ht = mediaSz[i*2+1]/10f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
          // Make sure to validate wid & ht.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
          // HP LJ 4050 (german) causes IAE in Sonderformat paper, wid & ht
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
          // returned is not constant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
          if ((wid <= 0) || (ht <= 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            //Remove corresponding ID from list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            if (nMedia == media.length) {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   520
                Integer remObj = Integer.valueOf(media[i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
              idList.remove(idList.indexOf(remObj));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
          // Find matching media using dimensions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
          // This call matches only with our own predefined sizes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
          msn = findMatchingMediaSizeNameMM(wid, ht);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
          if (msn != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            ms = MediaSize.getMediaSizeForName(msn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
          if (ms != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            msList.add(ms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
          } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
              Win32MediaSize wms =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                new Win32MediaSize(winMediaNames[i], media[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
              ms = new MediaSize(wid, ht, MediaSize.MM, wms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
              msList.add(ms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            } catch(IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
              if (nMedia == media.length) {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   542
                  Integer remObj = Integer.valueOf(media[i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                idList.remove(idList.indexOf(remObj));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        MediaSize[] arr2 = new MediaSize[msList.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        msList.toArray(arr2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        return arr2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    private PrinterIsAcceptingJobs getPrinterIsAcceptingJobs() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        if (getJobStatus(printer, 2) != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            return PrinterIsAcceptingJobs.NOT_ACCEPTING_JOBS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            return PrinterIsAcceptingJobs.ACCEPTING_JOBS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    private PrinterState getPrinterState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        if (isInvalid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            return PrinterState.STOPPED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    private PrinterStateReasons getPrinterStateReasons() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        if (isInvalid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            PrinterStateReasons psr = new PrinterStateReasons();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            psr.put(PrinterStateReason.SHUTDOWN, Severity.ERROR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            return psr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    private QueuedJobCount getQueuedJobCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        int count = getJobStatus(printer, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        if (count != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            return new QueuedJobCount(count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            return new QueuedJobCount(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    private boolean isSupportedCopies(Copies copies) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            if (gotCopies == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                nCopies = getCopiesSupported(printer, getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                gotCopies = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        int numCopies = copies.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        return (numCopies > 0 && numCopies <= nCopies);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    private boolean isSupportedMedia(MediaSizeName msn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        initMedia();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        if (mediaSizeNames != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            for (int i=0; i<mediaSizeNames.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                if (msn.equals(mediaSizeNames[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    private boolean isSupportedMediaPrintableArea(MediaPrintableArea mpa) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        initMedia();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        if (mediaPrintables != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            for (int i=0; i<mediaPrintables.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                if (mpa.equals(mediaPrintables[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    private boolean isSupportedMediaTray(MediaTray msn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        MediaTray[] trays = getMediaTrays();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        if (trays != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            for (int i=0; i<trays.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                if (msn.equals(trays[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    private int getPrinterCapabilities() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        if (prnCaps == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            prnCaps = getCapabilities(printer, getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        return prnCaps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    private String getPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        if (port == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            port = getPrinterPort(printer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        return port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
   /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    * NOTE: defaults indices must match those in WPrinterJob.cpp
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    private int[] getDefaultPrinterSettings() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        if (defaultSettings == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            defaultSettings = getDefaultSettings(printer, getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        return defaultSettings;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    private PrinterResolution[] getPrintResolutions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        if (printRes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            int[] prnRes = getAllResolutions(printer, getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            if (prnRes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                printRes = new PrinterResolution[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                int nRes = prnRes.length/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                ArrayList arrList = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                PrinterResolution pr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                for (int i=0; i<nRes; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                  try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                        pr = new PrinterResolution(prnRes[i*2],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                                       prnRes[i*2+1], PrinterResolution.DPI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                        arrList.add(pr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                    } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                printRes = (PrinterResolution[])arrList.toArray(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                                        new PrinterResolution[arrList.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        return printRes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
    private boolean isSupportedResolution(PrinterResolution res) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        PrinterResolution[] supportedRes = getPrintResolutions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        if (supportedRes != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
            for (int i=0; i<supportedRes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                if (res.equals(supportedRes[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
    public DocPrintJob createPrintJob() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
      SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
      if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        security.checkPrintJobAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        return new Win32PrintJob(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    private PrintServiceAttributeSet getDynamicAttributes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        PrintServiceAttributeSet attrs = new HashPrintServiceAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        attrs.add(getPrinterIsAcceptingJobs());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        attrs.add(getQueuedJobCount());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        return attrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
    public PrintServiceAttributeSet getUpdatedAttributes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        PrintServiceAttributeSet currSet = getDynamicAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        if (lastSet == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            lastSet = currSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            return AttributeSetUtilities.unmodifiableView(currSet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            PrintServiceAttributeSet updates =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                new HashPrintServiceAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            Attribute []attrs =  currSet.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            for (int i=0; i<attrs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                Attribute attr = attrs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                if (!lastSet.containsValue(attr)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                    updates.add(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            lastSet = currSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            return AttributeSetUtilities.unmodifiableView(updates);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
    public void wakeNotifier() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            if (notifier != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                notifier.wake();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    public void addPrintServiceAttributeListener(PrintServiceAttributeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                                                 listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            if (listener == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            if (notifier == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                notifier = new ServiceNotifier(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            notifier.addListener(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
    public void removePrintServiceAttributeListener(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                                      PrintServiceAttributeListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            if (listener == null || notifier == null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            notifier.removeListener(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            if (notifier.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                notifier.stopNotifier();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                notifier = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
    public <T extends PrintServiceAttribute> T
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        getAttribute(Class<T> category)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        if (category == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            throw new NullPointerException("category");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        if (!(PrintServiceAttribute.class.isAssignableFrom(category))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
            throw new IllegalArgumentException("Not a PrintServiceAttribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        if (category == ColorSupported.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            int caps = getPrinterCapabilities();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            if ((caps & DEVCAP_COLOR) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                return (T)ColorSupported.SUPPORTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                return (T)ColorSupported.NOT_SUPPORTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        } else if (category == PrinterName.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            return (T)getPrinterName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        } else if (category == PrinterState.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            return (T)getPrinterState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        } else if (category == PrinterStateReasons.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            return (T)getPrinterStateReasons();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        } else if (category == QueuedJobCount.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            return (T)getQueuedJobCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        } else if (category == PrinterIsAcceptingJobs.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            return (T)getPrinterIsAcceptingJobs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
    public PrintServiceAttributeSet getAttributes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        PrintServiceAttributeSet attrs = new  HashPrintServiceAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        attrs.add(getPrinterName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        attrs.add(getPrinterIsAcceptingJobs());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        PrinterState prnState = getPrinterState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        if (prnState != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
            attrs.add(prnState);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        PrinterStateReasons prnStateReasons = getPrinterStateReasons();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        if (prnStateReasons != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
            attrs.add(prnStateReasons);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        attrs.add(getQueuedJobCount());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        int caps = getPrinterCapabilities();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        if ((caps & DEVCAP_COLOR) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            attrs.add(ColorSupported.SUPPORTED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
            attrs.add(ColorSupported.NOT_SUPPORTED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        return AttributeSetUtilities.unmodifiableView(attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
    public DocFlavor[] getSupportedDocFlavors() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        int len = supportedFlavors.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        DocFlavor[] supportedDocFlavors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        int caps = getPrinterCapabilities();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        // doc flavors supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        // if PostScript is supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        if ((caps & DEVCAP_POSTSCRIPT) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
            supportedDocFlavors = new DocFlavor[len+3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            System.arraycopy(supportedFlavors, 0, supportedDocFlavors, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
            supportedDocFlavors[len] = DocFlavor.BYTE_ARRAY.POSTSCRIPT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            supportedDocFlavors[len+1] = DocFlavor.INPUT_STREAM.POSTSCRIPT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
            supportedDocFlavors[len+2] = DocFlavor.URL.POSTSCRIPT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
            supportedDocFlavors = new DocFlavor[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            System.arraycopy(supportedFlavors, 0, supportedDocFlavors, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        return supportedDocFlavors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
    public boolean isDocFlavorSupported(DocFlavor flavor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        /* To avoid a native query which may be time-consuming
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
         * do not invoke native unless postscript support is being queried.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
         * Instead just check the ones we 'always' support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        DocFlavor[] supportedDocFlavors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        if (isPostScriptFlavor(flavor)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
            supportedDocFlavors = getSupportedDocFlavors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            supportedDocFlavors = supportedFlavors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        for (int f=0; f<supportedDocFlavors.length; f++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
            if (flavor.equals(supportedDocFlavors[f])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
    public Class<?>[] getSupportedAttributeCategories() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        ArrayList categList = new ArrayList(otherAttrCats.length+3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        for (int i=0; i < otherAttrCats.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
            categList.add(otherAttrCats[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        int caps = getPrinterCapabilities();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        if ((caps & DEVCAP_DUPLEX) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
            categList.add(Sides.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        if ((caps & DEVCAP_QUALITY) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
            int[] defaults = getDefaultPrinterSettings();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
            // Added check: if supported, we should be able to get the default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
            if ((defaults[3] >= DMRES_HIGH) && (defaults[3] < 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
                categList.add(PrintQuality.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        PrinterResolution[] supportedRes = getPrintResolutions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        if ((supportedRes!=null) && (supportedRes.length>0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
            categList.add(PrinterResolution.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        return (Class[])categList.toArray(new Class[categList.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
    public boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        isAttributeCategorySupported(Class<? extends Attribute> category)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        if (category == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
            throw new NullPointerException("null category");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        if (!(Attribute.class.isAssignableFrom(category))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
            throw new IllegalArgumentException(category +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                                               " is not an Attribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        Class[] classList = getSupportedAttributeCategories();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        for (int i = 0; i < classList.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            if (category.equals(classList[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
    public Object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        getDefaultAttributeValue(Class<? extends Attribute> category)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        if (category == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
            throw new NullPointerException("null category");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        if (!Attribute.class.isAssignableFrom(category)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
            throw new IllegalArgumentException(category +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                                               " is not an Attribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        if (!isAttributeCategorySupported(category)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
        int[] defaults = getDefaultPrinterSettings();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        // indices must match those in WPrinterJob.cpp
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        int defPaper = defaults[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        int defYRes = defaults[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        int defQuality = defaults[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
        int defCopies = defaults[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
        int defOrient = defaults[5];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
        int defSides = defaults[6];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
        int defCollate = defaults[7];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        if (category == Copies.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
            if (defCopies > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                return new Copies(defCopies);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                return new Copies(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        } else if (category == Chromaticity.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
            int caps = getPrinterCapabilities();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
            if ((caps & DEVCAP_COLOR) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                return Chromaticity.MONOCHROME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
                return Chromaticity.COLOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        } else if (category == JobName.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
            return new JobName("Java Printing", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        } else if (category == OrientationRequested.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
            if (defOrient == DMORIENT_LANDSCAPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                return OrientationRequested.LANDSCAPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
                return OrientationRequested.PORTRAIT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        } else if (category == PageRanges.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
            return new PageRanges(1, Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
        } else if (category == Media.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
            MediaSizeName msn = findWin32Media(defPaper);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
            if (msn != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                if (!isSupportedMedia(msn) && mediaSizeNames != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
                    msn = mediaSizeNames[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
                    defPaper = findPaperID(msn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                return msn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
             } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
                 initMedia();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                 if ((mediaSizeNames != null) && (mediaSizeNames.length > 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
                     // if 'mediaSizeNames' is not null, idList and mediaSizes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
                     // cannot be null but to be safe, add a check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
                     if ((idList != null) && (mediaSizes != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
                         (idList.size() == mediaSizes.length)) {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   987
                         Integer defIdObj = Integer.valueOf(defPaper);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
                         int index = idList.indexOf(defIdObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
                         if (index>=0 && index<mediaSizes.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
                             return mediaSizes[index].getMediaSizeName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
                         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
                     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
                     return mediaSizeNames[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                 }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        } else if (category == MediaPrintableArea.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
            /* Verify defPaper */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
            MediaSizeName msn = findWin32Media(defPaper);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
            if (msn != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
                !isSupportedMedia(msn) && mediaSizeNames != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
                defPaper = findPaperID(mediaSizeNames[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
            float[] prnArea = getMediaPrintableArea(printer, defPaper);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
            if (prnArea != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
                MediaPrintableArea printableArea = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
                    printableArea = new MediaPrintableArea(prnArea[0],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
                                                           prnArea[1],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
                                                           prnArea[2],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
                                                           prnArea[3],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
                                                           MediaPrintableArea.INCH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
                } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
                return printableArea;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        } else if (category == SunAlternateMedia.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        } else if (category == Destination.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
                return new Destination((new File("out.prn")).toURI());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
            } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
                    return new Destination(new URI("file:out.prn"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
                } catch (URISyntaxException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        } else if (category == Sides.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
            switch(defSides) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            case DMDUP_VERTICAL :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
                return Sides.TWO_SIDED_LONG_EDGE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
            case DMDUP_HORIZONTAL :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
                return Sides.TWO_SIDED_SHORT_EDGE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
            default :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
                return Sides.ONE_SIDED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
        } else if (category == PrinterResolution.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            int yRes = defYRes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
            int xRes = defQuality;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
            if ((xRes < 0) || (yRes < 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
                int res = (yRes > xRes) ? yRes : xRes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                if (res > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
                 return new PrinterResolution(res, res, PrinterResolution.DPI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
               return new PrinterResolution(xRes, yRes, PrinterResolution.DPI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        } else if (category == ColorSupported.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
            int caps = getPrinterCapabilities();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
            if ((caps & DEVCAP_COLOR) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
                return ColorSupported.SUPPORTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
                return ColorSupported.NOT_SUPPORTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        } else if (category == PrintQuality.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
            if ((defQuality < 0) && (defQuality >= DMRES_HIGH)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
                switch (defQuality) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
                case DMRES_HIGH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
                    return PrintQuality.HIGH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
                case DMRES_MEDIUM:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
                    return PrintQuality.NORMAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
                    return PrintQuality.DRAFT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
        } else if (category == RequestingUserName.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
            String userName = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
              userName = System.getProperty("user.name", "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
            } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
            return new RequestingUserName(userName, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
        } else if (category == SheetCollate.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
            if (defCollate == DMCOLLATE_TRUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
                return SheetCollate.COLLATED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
                return SheetCollate.UNCOLLATED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        } else if (category == Fidelity.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
            return Fidelity.FIDELITY_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
    private boolean isPostScriptFlavor(DocFlavor flavor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        if (flavor.equals(DocFlavor.BYTE_ARRAY.POSTSCRIPT) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
            flavor.equals(DocFlavor.INPUT_STREAM.POSTSCRIPT) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
            flavor.equals(DocFlavor.URL.POSTSCRIPT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
    private boolean isPSDocAttr(Class category) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        if (category == OrientationRequested.class || category == Copies.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
    private boolean isAutoSense(DocFlavor flavor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
        if (flavor.equals(DocFlavor.BYTE_ARRAY.AUTOSENSE) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
            flavor.equals(DocFlavor.INPUT_STREAM.AUTOSENSE) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
            flavor.equals(DocFlavor.URL.AUTOSENSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
    public Object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        getSupportedAttributeValues(Class<? extends Attribute> category,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
                                    DocFlavor flavor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
                                    AttributeSet attributes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
        if (category == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
            throw new NullPointerException("null category");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
        if (!Attribute.class.isAssignableFrom(category)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
            throw new IllegalArgumentException(category +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
                                             " does not implement Attribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        if (flavor != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
            if (!isDocFlavorSupported(flavor)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
                throw new IllegalArgumentException(flavor +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
                                                  " is an unsupported flavor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
                // if postscript & category is already specified within the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
                //  PostScript data we return null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
            } else if (isAutoSense(flavor) ||(isPostScriptFlavor(flavor) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
                       (isPSDocAttr(category)))){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
        if (!isAttributeCategorySupported(category)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
        if (category == JobName.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
            return new JobName("Java Printing", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
        } else if (category == RequestingUserName.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
          String userName = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
          try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
            userName = System.getProperty("user.name", "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
          } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
            return new RequestingUserName(userName, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
        } else if (category == ColorSupported.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
            int caps = getPrinterCapabilities();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
            if ((caps & DEVCAP_COLOR) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
                return ColorSupported.SUPPORTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
                return ColorSupported.NOT_SUPPORTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
        } else if (category == Chromaticity.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
            if (flavor == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
                flavor.equals(DocFlavor.SERVICE_FORMATTED.PAGEABLE) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
                flavor.equals(DocFlavor.SERVICE_FORMATTED.PRINTABLE) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
                flavor.equals(DocFlavor.BYTE_ARRAY.GIF) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
                flavor.equals(DocFlavor.INPUT_STREAM.GIF) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
                flavor.equals(DocFlavor.URL.GIF) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
                flavor.equals(DocFlavor.BYTE_ARRAY.JPEG) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
                flavor.equals(DocFlavor.INPUT_STREAM.JPEG) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
                flavor.equals(DocFlavor.URL.JPEG) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
                flavor.equals(DocFlavor.BYTE_ARRAY.PNG) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
                flavor.equals(DocFlavor.INPUT_STREAM.PNG) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
                flavor.equals(DocFlavor.URL.PNG)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
                int caps = getPrinterCapabilities();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
                if ((caps & DEVCAP_COLOR) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
                    Chromaticity []arr = new Chromaticity[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
                    arr[0] = Chromaticity.MONOCHROME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
                    return (arr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
                    Chromaticity []arr = new Chromaticity[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
                    arr[0] = Chromaticity.MONOCHROME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
                    arr[1] = Chromaticity.COLOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
                    return (arr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        } else if (category == Destination.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
                return new Destination((new File("out.prn")).toURI());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
            } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
                    return new Destination(new URI("file:out.prn"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
                } catch (URISyntaxException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
        } else if (category == OrientationRequested.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
            if (flavor == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
                flavor.equals(DocFlavor.SERVICE_FORMATTED.PAGEABLE) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
                flavor.equals(DocFlavor.SERVICE_FORMATTED.PRINTABLE) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
                flavor.equals(DocFlavor.INPUT_STREAM.GIF) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
                flavor.equals(DocFlavor.INPUT_STREAM.JPEG) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
                flavor.equals(DocFlavor.INPUT_STREAM.PNG) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
                flavor.equals(DocFlavor.BYTE_ARRAY.GIF) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
                flavor.equals(DocFlavor.BYTE_ARRAY.JPEG) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
                flavor.equals(DocFlavor.BYTE_ARRAY.PNG) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
                flavor.equals(DocFlavor.URL.GIF) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
                flavor.equals(DocFlavor.URL.JPEG) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
                flavor.equals(DocFlavor.URL.PNG)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
                OrientationRequested []arr = new OrientationRequested[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
                arr[0] = OrientationRequested.PORTRAIT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
                arr[1] = OrientationRequested.LANDSCAPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
                arr[2] = OrientationRequested.REVERSE_LANDSCAPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
                return arr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
        } else if ((category == Copies.class) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
                   (category == CopiesSupported.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
                if (gotCopies == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
                    nCopies = getCopiesSupported(printer, getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
                    gotCopies = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
            return new CopiesSupported(1, nCopies);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
        } else if (category == Media.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
            initMedia();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
            int len = (mediaSizeNames == null) ? 0 : mediaSizeNames.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
            MediaTray[] trays = getMediaTrays();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
            len += (trays == null) ? 0 : trays.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
            Media []arr = new Media[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
            if (mediaSizeNames != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
                System.arraycopy(mediaSizeNames, 0, arr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
                                 0, mediaSizeNames.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
            if (trays != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
                System.arraycopy(trays, 0, arr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
                                 mediaSizeNames.length, trays.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
            return arr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
        } else if (category == MediaPrintableArea.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
            initMedia();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
            if (mediaPrintables == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
            // if getting printable area for a specific media size
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
            Media mediaName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
            if ((attributes != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
                ((mediaName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
                  (Media)attributes.get(Media.class)) != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
                if (mediaName instanceof MediaSizeName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
                    MediaPrintableArea []arr = new MediaPrintableArea[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
                    if (mediaSizeNames.length == mediaPrintables.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
                        for (int j=0; j < mediaSizeNames.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
                            if (mediaName.equals(mediaSizeNames[j])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
                                arr[0] = mediaPrintables[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
                                return arr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
                    MediaSize ms =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
                      MediaSize.getMediaSizeForName((MediaSizeName)mediaName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
                    if (ms != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
                        arr[0] = new MediaPrintableArea(0, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
                                                        ms.getX(MediaSize.INCH),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
                                                        ms.getY(MediaSize.INCH),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
                                                        MediaPrintableArea.INCH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
                        return arr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
                // else an instance of MediaTray, fall thru returning
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
                // all MediaPrintableAreas
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
            MediaPrintableArea []arr =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
                new MediaPrintableArea[mediaPrintables.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
            System.arraycopy(mediaPrintables, 0, arr, 0, mediaPrintables.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
            return arr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
        } else if (category == SunAlternateMedia.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
            return new SunAlternateMedia(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
                              (Media)getDefaultAttributeValue(Media.class));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
        } else if (category == PageRanges.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
            if (flavor == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
                flavor.equals(DocFlavor.SERVICE_FORMATTED.PAGEABLE) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
                flavor.equals(DocFlavor.SERVICE_FORMATTED.PRINTABLE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
                PageRanges []arr = new PageRanges[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
                arr[0] = new PageRanges(1, Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
                return arr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
        } else if (category == PrinterResolution.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
            PrinterResolution[] supportedRes = getPrintResolutions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
            if (supportedRes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
            PrinterResolution []arr =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
                new PrinterResolution[supportedRes.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
            System.arraycopy(supportedRes, 0, arr, 0, supportedRes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
            return arr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
        } else if (category == Sides.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
            if (flavor == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
                flavor.equals(DocFlavor.SERVICE_FORMATTED.PAGEABLE) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
                flavor.equals(DocFlavor.SERVICE_FORMATTED.PRINTABLE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
                Sides []arr = new Sides[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
                arr[0] = Sides.ONE_SIDED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
                arr[1] = Sides.TWO_SIDED_LONG_EDGE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
                arr[2] = Sides.TWO_SIDED_SHORT_EDGE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
                return arr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
        } else if (category == PrintQuality.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
            PrintQuality []arr = new PrintQuality[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
            arr[0] = PrintQuality.DRAFT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
            arr[1] = PrintQuality.HIGH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
            arr[2] = PrintQuality.NORMAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
            return arr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
        } else if (category == SheetCollate.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
            if (flavor == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
                (flavor.equals(DocFlavor.SERVICE_FORMATTED.PAGEABLE) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
                 flavor.equals(DocFlavor.SERVICE_FORMATTED.PRINTABLE))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
                SheetCollate []arr = new SheetCollate[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
                arr[0] = SheetCollate.COLLATED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
                arr[1] = SheetCollate.UNCOLLATED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
                return arr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
        } else if (category == Fidelity.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
            Fidelity []arr = new Fidelity[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
            arr[0] = Fidelity.FIDELITY_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
            arr[1] = Fidelity.FIDELITY_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
            return arr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
    public boolean isAttributeValueSupported(Attribute attr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
                                             DocFlavor flavor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
                                             AttributeSet attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
        if (attr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
            throw new NullPointerException("null attribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
        Class category = attr.getCategory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
        if (flavor != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
            if (!isDocFlavorSupported(flavor)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
                throw new IllegalArgumentException(flavor +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
                                                   " is an unsupported flavor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
                // if postscript & category is already specified within the PostScript data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
                // we return false
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
            } else if (isAutoSense(flavor) || (isPostScriptFlavor(flavor) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
                       (isPSDocAttr(category)))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
        if (!isAttributeCategorySupported(category)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
        else if (category == Chromaticity.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
            if ((flavor == null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
                flavor.equals(DocFlavor.SERVICE_FORMATTED.PAGEABLE) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
                flavor.equals(DocFlavor.SERVICE_FORMATTED.PRINTABLE) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
                flavor.equals(DocFlavor.BYTE_ARRAY.GIF) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
                flavor.equals(DocFlavor.INPUT_STREAM.GIF) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
                flavor.equals(DocFlavor.URL.GIF) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
                flavor.equals(DocFlavor.BYTE_ARRAY.JPEG) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
                flavor.equals(DocFlavor.INPUT_STREAM.JPEG) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
                flavor.equals(DocFlavor.URL.JPEG) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
                flavor.equals(DocFlavor.BYTE_ARRAY.PNG) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
                flavor.equals(DocFlavor.INPUT_STREAM.PNG) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
                flavor.equals(DocFlavor.URL.PNG)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
                int caps = getPrinterCapabilities();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
                if ((caps & DEVCAP_COLOR) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
                    return attr == Chromaticity.MONOCHROME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
        } else if (category == Copies.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
            return isSupportedCopies((Copies)attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
        } else if (category == Destination.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
            URI uri = ((Destination)attr).getURI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
            if ("file".equals(uri.getScheme()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
                !(uri.getSchemeSpecificPart().equals(""))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
        } else if (category == Media.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
            if (attr instanceof MediaSizeName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
                return isSupportedMedia((MediaSizeName)attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
            if (attr instanceof MediaTray) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
                return isSupportedMediaTray((MediaTray)attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
        } else if (category == MediaPrintableArea.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
            return isSupportedMediaPrintableArea((MediaPrintableArea)attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
        } else if (category == SunAlternateMedia.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
            Media media = ((SunAlternateMedia)attr).getMedia();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
            return isAttributeValueSupported(media, flavor, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
        } else if (category == PageRanges.class ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
                   category == SheetCollate.class ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
                   category == Sides.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
            if (flavor != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
                !(flavor.equals(DocFlavor.SERVICE_FORMATTED.PAGEABLE) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
                flavor.equals(DocFlavor.SERVICE_FORMATTED.PRINTABLE))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
        } else if (category == PrinterResolution.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
            if (attr instanceof PrinterResolution) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
                return isSupportedResolution((PrinterResolution)attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
        } else if (category == OrientationRequested.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
            if (attr == OrientationRequested.REVERSE_PORTRAIT ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
                (flavor != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
                !(flavor.equals(DocFlavor.SERVICE_FORMATTED.PAGEABLE) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
                flavor.equals(DocFlavor.SERVICE_FORMATTED.PRINTABLE) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
                flavor.equals(DocFlavor.INPUT_STREAM.GIF) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
                flavor.equals(DocFlavor.INPUT_STREAM.JPEG) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
                flavor.equals(DocFlavor.INPUT_STREAM.PNG) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
                flavor.equals(DocFlavor.BYTE_ARRAY.GIF) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
                flavor.equals(DocFlavor.BYTE_ARRAY.JPEG) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
                flavor.equals(DocFlavor.BYTE_ARRAY.PNG) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
                flavor.equals(DocFlavor.URL.GIF) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
                flavor.equals(DocFlavor.URL.JPEG) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
                flavor.equals(DocFlavor.URL.PNG))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
        } else if (category == ColorSupported.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
            int caps = getPrinterCapabilities();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
            boolean isColorSup = ((caps & DEVCAP_COLOR) != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
            if  ((!isColorSup && (attr == ColorSupported.SUPPORTED)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
                (isColorSup && (attr == ColorSupported.NOT_SUPPORTED))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
    public AttributeSet getUnsupportedAttributes(DocFlavor flavor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
                                                 AttributeSet attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
        if (flavor != null && !isDocFlavorSupported(flavor)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
            throw new IllegalArgumentException("flavor " + flavor +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
                                               "is not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
        if (attributes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
        Attribute attr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
        AttributeSet unsupp = new HashAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
        Attribute []attrs = attributes.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
        for (int i=0; i<attrs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
                attr = attrs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
                if (!isAttributeCategorySupported(attr.getCategory())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
                    unsupp.add(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
                else if (!isAttributeValueSupported(attr, flavor, attributes)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
                    unsupp.add(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
            } catch (ClassCastException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
        if (unsupp.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
            return unsupp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
    public ServiceUIFactory getServiceUIFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
        return "Win32 Printer : " + getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
        return  (obj == this ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
                 (obj instanceof Win32PrintService &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
                  ((Win32PrintService)obj).getName().equals(getName())));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
   public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
        return this.getClass().hashCode()+getName().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
    public boolean usesClass(Class c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
        return (c == sun.awt.windows.WPrinterJob.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
    private native int[] getAllMediaIDs(String printerName, String port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
    private native int[] getAllMediaSizes(String printerName, String port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
    private native int[] getAllMediaTrays(String printerName, String port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
    private native float[] getMediaPrintableArea(String printerName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
                                                 int paperSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
    private native String[] getAllMediaNames(String printerName, String port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
    private native String[] getAllMediaTrayNames(String printerName, String port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
    private native int getCopiesSupported(String printerName, String port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
    private native int[] getAllResolutions(String printerName, String port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
    private native int getCapabilities(String printerName, String port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
    private native int[] getDefaultSettings(String printerName, String port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
    private native int getJobStatus(String printerName, int type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
    private native String getPrinterPort(String printerName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
class Win32MediaSize extends MediaSizeName {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
    private static ArrayList winStringTable = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
    private static ArrayList winEnumTable = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
    private int dmPaperID; // driver ID for this paper.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
    private Win32MediaSize(int x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
        super(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
    private synchronized static int nextValue(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
      winStringTable.add(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
      return (winStringTable.size()-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
    public Win32MediaSize(String name, int dmPaper) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
        super(nextValue(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
        dmPaperID = dmPaper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
        winEnumTable.add(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
    private MediaSizeName[] getSuperEnumTable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
      return (MediaSizeName[])super.getEnumValueTable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
         /* initialize Win32PrintService.predefMedia */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
            Win32MediaSize winMedia = new Win32MediaSize(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
            // cannot call getSuperEnumTable directly because of static context
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
            MediaSizeName[] enumMedia = winMedia.getSuperEnumTable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
            if (enumMedia != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
                Win32PrintService.predefMedia = new MediaSize[enumMedia.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
                for (int i=0; i<enumMedia.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
                    Win32PrintService.predefMedia[i] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
                        MediaSize.getMediaSizeForName(enumMedia[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
    int getDMPaper() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
        return dmPaperID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
    protected String[] getStringTable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
      String[] nameTable = new String[winStringTable.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
      return (String[])winStringTable.toArray(nameTable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
    protected EnumSyntax[] getEnumValueTable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
      MediaSizeName[] enumTable = new MediaSizeName[winEnumTable.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
      return (MediaSizeName[])winEnumTable.toArray(enumTable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
}