jdk/src/windows/classes/sun/print/Win32PrintServiceLookup.java
author dcubed
Wed, 26 May 2010 14:16:55 -0700
changeset 5684 f6538e83cf3b
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Merge
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-2007 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.BufferedReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.InputStreamReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.PrivilegedActionException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.PrivilegedExceptionAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.print.DocFlavor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.print.MultiDocPrintService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.print.PrintService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.print.PrintServiceLookup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.print.attribute.Attribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.print.attribute.AttributeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.print.attribute.HashPrintRequestAttributeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.print.attribute.HashPrintServiceAttributeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.print.attribute.PrintRequestAttribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.print.attribute.PrintRequestAttributeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.print.attribute.PrintServiceAttribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.print.attribute.PrintServiceAttributeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import javax.print.attribute.standard.PrinterName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
public class Win32PrintServiceLookup extends PrintServiceLookup {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private String defaultPrinter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private PrintService defaultPrintService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private String[] printers; /* excludes the default printer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private PrintService[] printServices; /* includes the default printer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                    new sun.security.action.LoadLibraryAction("awt"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /* The singleton win32 print lookup service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * Code that is aware of this field and wants to use it must first
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * see if its null, and if so instantiate it by calling a method such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * javax.print.PrintServiceLookup.defaultPrintService() so that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * same instance is stored there.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private static Win32PrintServiceLookup win32PrintLUS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /* Think carefully before calling this. Preferably don't call it. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public static Win32PrintServiceLookup getWin32PrintLUS() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        if (win32PrintLUS == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            /* This call is internally synchronized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
             * When it returns an instance of this class will have
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
             * been instantiated - else there's a JDK internal error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            PrintServiceLookup.lookupDefaultPrintService();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        return win32PrintLUS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public Win32PrintServiceLookup() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        if (win32PrintLUS == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            win32PrintLUS = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            String osName = AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                new sun.security.action.GetPropertyAction("os.name"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            // There's no capability for Win98 to refresh printers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            // See "OpenPrinter" for more info.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            if (osName != null && osName.startsWith("Windows 98")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            // start the printer listener thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            PrinterChangeListener thr = new PrinterChangeListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            thr.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            thr.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        } /* else condition ought to never happen! */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /* Want the PrintService which is default print service to have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * equality of reference with the equivalent in list of print services
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * This isn't required by the API and there's a risk doing this will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * lead people to assume its guaranteed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public synchronized PrintService[] getPrintServices() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            security.checkPrintJobAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (printServices == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            refreshServices();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        return printServices;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    private synchronized void refreshServices() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        printers = getAllPrinterNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        if (printers == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            // In Windows it is safe to assume no default if printers == null so we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            // don't get the default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            printServices = new PrintService[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        PrintService[] newServices = new PrintService[printers.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        PrintService defService = getDefaultPrintService();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        for (int p = 0; p < printers.length; p++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            if (defService != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                printers[p].equals(defService.getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                newServices[p] = defService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                if (printServices == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                    newServices[p] = new Win32PrintService(printers[p]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                    int j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    for (j = 0; j < printServices.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                        if ((printServices[j]!= null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                            (printers[p].equals(printServices[j].getName()))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                            newServices[p] = printServices[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                            printServices[j] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                    if (j == printServices.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                        newServices[p] = new Win32PrintService(printers[p]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        // Look for deleted services and invalidate these
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        if (printServices != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            for (int j=0; j < printServices.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                if ((printServices[j] instanceof Win32PrintService) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    (!printServices[j].equals(defaultPrintService))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    ((Win32PrintService)printServices[j]).invalidateService();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        printServices = newServices;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public synchronized PrintService getPrintServiceByName(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        if (name == null || name.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            /* getPrintServices() is now very fast. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            PrintService[] printServices = getPrintServices();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            for (int i=0; i<printServices.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                if (printServices[i].getName().equals(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                    return printServices[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    boolean matchingService(PrintService service,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                            PrintServiceAttributeSet serviceSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        if (serviceSet != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            Attribute [] attrs =  serviceSet.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            Attribute serviceAttr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            for (int i=0; i<attrs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                serviceAttr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    = service.getAttribute((Class<PrintServiceAttribute>)attrs[i].getCategory());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                if (serviceAttr == null || !serviceAttr.equals(attrs[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    public PrintService[] getPrintServices(DocFlavor flavor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                                           AttributeSet attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
          security.checkPrintJobAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        PrintRequestAttributeSet requestSet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        PrintServiceAttributeSet serviceSet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        if (attributes != null && !attributes.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            requestSet = new HashPrintRequestAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            serviceSet = new HashPrintServiceAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            Attribute[] attrs = attributes.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            for (int i=0; i<attrs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                if (attrs[i] instanceof PrintRequestAttribute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                    requestSet.add(attrs[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                } else if (attrs[i] instanceof PrintServiceAttribute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                    serviceSet.add(attrs[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
         * Special case: If client is asking for a particular printer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
         * (by name) then we can save time by getting just that service
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
         * to check against the rest of the specified attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        PrintService[] services = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        if (serviceSet != null && serviceSet.get(PrinterName.class) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            PrinterName name = (PrinterName)serviceSet.get(PrinterName.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            PrintService service = getPrintServiceByName(name.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            if (service == null || !matchingService(service, serviceSet)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                services = new PrintService[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                services = new PrintService[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                services[0] = service;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            services = getPrintServices();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        if (services.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            return services;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            ArrayList matchingServices = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            for (int i=0; i<services.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                    if (services[i].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                        getUnsupportedAttributes(flavor, requestSet) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                        matchingServices.add(services[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            services = new PrintService[matchingServices.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            return (PrintService[])matchingServices.toArray(services);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * return empty array as don't support multi docs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    public MultiDocPrintService[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        getMultiDocPrintServices(DocFlavor[] flavors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                                 AttributeSet attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
          security.checkPrintJobAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        return new MultiDocPrintService[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    public synchronized PrintService getDefaultPrintService() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
          security.checkPrintJobAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        // Windows does not have notification for a change in default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        // so we always get the latest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        defaultPrinter = getDefaultPrinterName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        if (defaultPrinter == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        if ((defaultPrintService != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            defaultPrintService.getName().equals(defaultPrinter)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            return defaultPrintService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
         // Not the same as default so proceed to get new PrintService.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        // clear defaultPrintService
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        defaultPrintService = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        if (printServices != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            for (int j=0; j<printServices.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                if (defaultPrinter.equals(printServices[j].getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                    defaultPrintService = printServices[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        if (defaultPrintService == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            defaultPrintService = new Win32PrintService(defaultPrinter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        return defaultPrintService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    class PrinterChangeListener extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        long chgObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        PrinterChangeListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            chgObj = notifyFirstPrinterChange(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            if (chgObj != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                    // wait for configuration to change
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                    if (notifyPrinterChange(chgObj) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                            refreshServices();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                        } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                        notifyClosePrinterChange(chgObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    private native String getDefaultPrinterName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    private native String[] getAllPrinterNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    private native long notifyFirstPrinterChange(String printer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    private native void notifyClosePrinterChange(long chgObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    private native int notifyPrinterChange(long chgObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
}