jdk/src/java.desktop/windows/classes/sun/print/Win32PrintJob.java
author prr
Sat, 19 Sep 2015 15:45:59 -0700
changeset 32865 f9cb6e427f9e
parent 25859 3317bb8137f4
permissions -rw-r--r--
8136783: Run blessed-modifier-order script on java.desktop Reviewed-by: martin, serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 19167
diff changeset
     2
 * Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
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.net.URI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.BufferedInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.BufferedOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.FileOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.FileNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.io.Reader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.print.CancelablePrintJob;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.print.Doc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.print.DocFlavor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.print.DocPrintJob;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.print.PrintService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.print.PrintException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import javax.print.event.PrintJobEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import javax.print.event.PrintJobListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import javax.print.event.PrintJobAttributeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import javax.print.attribute.Attribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import javax.print.attribute.AttributeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import javax.print.attribute.AttributeSetUtilities;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import javax.print.attribute.DocAttributeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import javax.print.attribute.HashPrintJobAttributeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import javax.print.attribute.HashPrintRequestAttributeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import javax.print.attribute.PrintJobAttribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
import javax.print.attribute.PrintJobAttributeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import javax.print.attribute.PrintRequestAttribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
import javax.print.attribute.PrintRequestAttributeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
import javax.print.attribute.standard.Copies;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
import javax.print.attribute.standard.DocumentName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
import javax.print.attribute.standard.Fidelity;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
import javax.print.attribute.standard.JobName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
import javax.print.attribute.standard.JobOriginatingUserName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
import javax.print.attribute.standard.Media;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
import javax.print.attribute.standard.MediaSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
import javax.print.attribute.standard.MediaSizeName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
import javax.print.attribute.standard.OrientationRequested;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
import javax.print.attribute.standard.RequestingUserName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
import javax.print.attribute.standard.Destination;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
import javax.print.attribute.standard.PrinterIsAcceptingJobs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
import javax.print.attribute.standard.PrinterState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
import javax.print.attribute.standard.PrinterStateReason;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
import javax.print.attribute.standard.PrinterStateReasons;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
import java.awt.print.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
public class Win32PrintJob implements CancelablePrintJob {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 25859
diff changeset
    82
    private transient Vector<PrintJobListener> jobListeners;
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 25859
diff changeset
    83
    private transient Vector<PrintJobAttributeListener> attrListeners;
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 25859
diff changeset
    84
    private transient Vector<PrintJobAttributeSet> listenedAttributeSets;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private Win32PrintService service;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private boolean fidelity;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private boolean printing = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private boolean printReturned = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private PrintRequestAttributeSet reqAttrSet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private PrintJobAttributeSet jobAttrSet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private PrinterJob job;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    private Doc doc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private String mDestination = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    /* these variables used globally to store reference to the print
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * data retrieved as a stream. On completion these are always closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * if non-null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private InputStream instream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private Reader reader = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /* default values overridden by those extracted from the attributes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private String jobName = "Java Printing";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private int copies = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private MediaSizeName mediaName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    private MediaSize     mediaSize = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private OrientationRequested orient = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /* print job handle used by native code */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    private long hPrintJob;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /* buffer length for printing raw data */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private static final int PRINTBUFFERLEN = 8192;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    Win32PrintJob(Win32PrintService service) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        this.service = service;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public PrintService getPrintService() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        return service;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public PrintJobAttributeSet getAttributes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            if (jobAttrSet == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                /* just return an empty set until the job is submitted */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                PrintJobAttributeSet jobSet = new HashPrintJobAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                return AttributeSetUtilities.unmodifiableView(jobSet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
              return jobAttrSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public void addPrintJobListener(PrintJobListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            if (listener == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            if (jobListeners == null) {
25140
80e863984492 8042870: Fix raw and unchecked warnings in sun.print
darcy
parents: 22584
diff changeset
   142
                jobListeners = new Vector<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            jobListeners.add(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public void removePrintJobListener(PrintJobListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            if (listener == null || jobListeners == null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            jobListeners.remove(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            if (jobListeners.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                jobListeners = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
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
    /* Closes any stream already retrieved for the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * We want to avoid unnecessarily asking the Doc to create a stream only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * to get a reference in order to close it because the job failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * If the representation class is itself a "stream", this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * closes that stream too.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    private void closeDataStreams() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if (doc == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        Object data = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            data = doc.getPrintData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        if (instream != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                instream.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                instream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        else if (reader != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                reader.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                reader = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        else if (data instanceof InputStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                ((InputStream)data).close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        else if (data instanceof Reader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                ((Reader)data).close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    private void notifyEvent(int reason) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        /* since this method should always get called, here's where
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
         * we will perform the clean up of any data stream supplied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        switch (reason) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            case PrintJobEvent.DATA_TRANSFER_COMPLETE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            case PrintJobEvent.JOB_CANCELED :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            case PrintJobEvent.JOB_FAILED :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            case PrintJobEvent.NO_MORE_EVENTS :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            case PrintJobEvent.JOB_COMPLETE :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                closeDataStreams();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            if (jobListeners != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                PrintJobListener listener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                PrintJobEvent event = new PrintJobEvent(this, reason);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                for (int i = 0; i < jobListeners.size(); i++) {
25140
80e863984492 8042870: Fix raw and unchecked warnings in sun.print
darcy
parents: 22584
diff changeset
   230
                    listener = jobListeners.elementAt(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                    switch (reason) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                        case PrintJobEvent.JOB_COMPLETE :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                            listener.printJobCompleted(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                        case PrintJobEvent.JOB_CANCELED :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                            listener.printJobCanceled(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                        case PrintJobEvent.JOB_FAILED :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                            listener.printJobFailed(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                        case PrintJobEvent.DATA_TRANSFER_COMPLETE :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                            listener.printDataTransferCompleted(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                        case PrintJobEvent.NO_MORE_EVENTS :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                            listener.printJobNoMoreEvents(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                    }
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    public void addPrintJobAttributeListener(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                                  PrintJobAttributeListener listener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                                  PrintJobAttributeSet attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            if (listener == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            if (attrListeners == null) {
25140
80e863984492 8042870: Fix raw and unchecked warnings in sun.print
darcy
parents: 22584
diff changeset
   269
                attrListeners = new Vector<>();
80e863984492 8042870: Fix raw and unchecked warnings in sun.print
darcy
parents: 22584
diff changeset
   270
                listenedAttributeSets = new Vector<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            attrListeners.add(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            if (attributes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                attributes = new HashPrintJobAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            listenedAttributeSets.add(attributes);
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
    public void removePrintJobAttributeListener(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                                        PrintJobAttributeListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            if (listener == null || attrListeners == null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            int index = attrListeners.indexOf(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            if (index == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                attrListeners.remove(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                listenedAttributeSets.remove(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                if (attrListeners.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                    attrListeners = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    listenedAttributeSets = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    public void print(Doc doc, PrintRequestAttributeSet attributes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        throws PrintException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            if (printing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                throw new PrintException("already printing");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                printing = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 19167
diff changeset
   311
        PrinterState prnState = service.getAttribute(PrinterState.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        if (prnState == PrinterState.STOPPED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            PrinterStateReasons prnStateReasons =
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 19167
diff changeset
   314
                service.getAttribute(PrinterStateReasons.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                if ((prnStateReasons != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                    (prnStateReasons.containsKey(PrinterStateReason.SHUTDOWN)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                    throw new PrintException("PrintService is no longer available.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 19167
diff changeset
   322
        if (service.getAttribute(PrinterIsAcceptingJobs.class) ==
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 19167
diff changeset
   323
            PrinterIsAcceptingJobs.NOT_ACCEPTING_JOBS) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            throw new PrintException("Printer is not accepting job.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        this.doc = doc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        /* check if the parameters are valid before doing much processing */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        DocFlavor flavor = doc.getDocFlavor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        Object data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            data = doc.getPrintData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            throw new PrintException("can't get print data: " + e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
19167
c575eb772ac8 8021583: test/javax/print/autosense/PrintAutoSenseData.java throwing NPE
jgodinez
parents: 5506
diff changeset
   340
        if (data == null) {
c575eb772ac8 8021583: test/javax/print/autosense/PrintAutoSenseData.java throwing NPE
jgodinez
parents: 5506
diff changeset
   341
            throw new PrintException("Null print data.");
c575eb772ac8 8021583: test/javax/print/autosense/PrintAutoSenseData.java throwing NPE
jgodinez
parents: 5506
diff changeset
   342
        }
c575eb772ac8 8021583: test/javax/print/autosense/PrintAutoSenseData.java throwing NPE
jgodinez
parents: 5506
diff changeset
   343
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        if (flavor == null || (!service.isDocFlavorSupported(flavor))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            throw new PrintJobFlavorException("invalid flavor", flavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        initializeAttributeSets(doc, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        getAttributeValues(flavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        String repClassName = flavor.getRepresentationClassName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        if (flavor.equals(DocFlavor.INPUT_STREAM.GIF) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            flavor.equals(DocFlavor.INPUT_STREAM.JPEG) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            flavor.equals(DocFlavor.INPUT_STREAM.PNG) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            flavor.equals(DocFlavor.BYTE_ARRAY.GIF) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            flavor.equals(DocFlavor.BYTE_ARRAY.JPEG) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            flavor.equals(DocFlavor.BYTE_ARRAY.PNG)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                instream = doc.getStreamForBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                if (instream == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                    notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                    throw new PrintException("No stream for data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                printableJob(new ImagePrinter(instream));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                service.wakeNotifier();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            } catch (ClassCastException cce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                throw new PrintException(cce);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                throw new PrintException(ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        } else if (flavor.equals(DocFlavor.URL.GIF) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                   flavor.equals(DocFlavor.URL.JPEG) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                   flavor.equals(DocFlavor.URL.PNG)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                printableJob(new ImagePrinter((URL)data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                service.wakeNotifier();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            } catch (ClassCastException cce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                throw new PrintException(cce);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        } else if (repClassName.equals("java.awt.print.Pageable")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                pageableJob((Pageable)doc.getPrintData());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                service.wakeNotifier();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            } catch (ClassCastException cce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                throw new PrintException(cce);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                throw new PrintException(ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        } else if (repClassName.equals("java.awt.print.Printable")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                printableJob((Printable)doc.getPrintData());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                service.wakeNotifier();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            } catch (ClassCastException cce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                throw new PrintException(cce);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                throw new PrintException(ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        } else if (repClassName.equals("[B") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                   repClassName.equals("java.io.InputStream") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                   repClassName.equals("java.net.URL")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            if (repClassName.equals("java.net.URL")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                URL url = (URL)data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                    instream = url.openStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                    notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    throw new PrintException(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                    instream = doc.getStreamForBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                    notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                    throw new PrintException(ioe.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            if (instream == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                throw new PrintException("No stream for data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            if (mDestination != null) { // if destination attribute is set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                    FileOutputStream fos = new FileOutputStream(mDestination);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                    byte []buffer = new byte[1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                    int cread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                    while ((cread = instream.read(buffer, 0, buffer.length)) >=0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                        fos.write(buffer, 0, cread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                    fos.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                    fos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                } catch (FileNotFoundException fnfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                    notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                    throw new PrintException(fnfe.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                    notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                    throw new PrintException(ioe.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                notifyEvent(PrintJobEvent.DATA_TRANSFER_COMPLETE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                notifyEvent(PrintJobEvent.JOB_COMPLETE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                service.wakeNotifier();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            if (!startPrintRawData(service.getName(), jobName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                throw new PrintException("Print job failed to start.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            BufferedInputStream  bin = new BufferedInputStream(instream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            int bread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                byte[] buffer = new byte[PRINTBUFFERLEN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                while ((bread = bin.read(buffer, 0, PRINTBUFFERLEN)) >=0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                    if (!printRawData(buffer, bread)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                        bin.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                        notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                        throw new PrintException ("Problem while spooling data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                bin.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                if (!endPrintRawData()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                    notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                    throw new PrintException("Print job failed to close properly.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                notifyEvent(PrintJobEvent.DATA_TRANSFER_COMPLETE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                throw new PrintException (e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                notifyEvent(PrintJobEvent.NO_MORE_EVENTS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            throw new PrintException("unrecognized class: "+repClassName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        service.wakeNotifier();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    public void printableJob(Printable printable) throws PrintException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                if (job != null) { // shouldn't happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                    throw new PrintException("already printing");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                    job = new sun.awt.windows.WPrinterJob();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            PrintService svc = getPrintService();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            job.setPrintService(svc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            if (copies == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                Copies c = (Copies)svc.getDefaultAttributeValue(Copies.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                copies = c.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            if (mediaName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                Object media = svc.getDefaultAttributeValue(Media.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                if (media instanceof MediaSizeName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                    mediaName = (MediaSizeName) media;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                    mediaSize = MediaSize.getMediaSizeForName(mediaName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            if (orient == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                orient =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                    (OrientationRequested)svc.getDefaultAttributeValue(OrientationRequested.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            job.setCopies(copies);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            job.setJobName(jobName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            PageFormat pf = new PageFormat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            if (mediaSize != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                Paper p = new Paper();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                p.setSize(mediaSize.getX(MediaSize.INCH)*72.0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                          mediaSize.getY(MediaSize.INCH)*72.0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                p.setImageableArea(72.0, 72.0, p.getWidth()-144.0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                                   p.getHeight()-144.0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                pf.setPaper(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            if (orient == OrientationRequested.REVERSE_LANDSCAPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                pf.setOrientation(PageFormat.REVERSE_LANDSCAPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            } else if (orient == OrientationRequested.LANDSCAPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                pf.setOrientation(PageFormat.LANDSCAPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            job.setPrintable(printable, pf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            job.print(reqAttrSet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            notifyEvent(PrintJobEvent.DATA_TRANSFER_COMPLETE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        } catch (PrinterException pe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            throw new PrintException(pe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            printReturned = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            notifyEvent(PrintJobEvent.NO_MORE_EVENTS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    public void pageableJob(Pageable pageable) throws PrintException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                if (job != null) { // shouldn't happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                    throw new PrintException("already printing");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                    job = new sun.awt.windows.WPrinterJob();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            PrintService svc = getPrintService();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            job.setPrintService(svc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            if (copies == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                Copies c = (Copies)svc.getDefaultAttributeValue(Copies.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                copies = c.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            job.setCopies(copies);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            job.setJobName(jobName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            job.setPageable(pageable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            job.print(reqAttrSet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            notifyEvent(PrintJobEvent.DATA_TRANSFER_COMPLETE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        } catch (PrinterException pe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            throw new PrintException(pe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            printReturned = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            notifyEvent(PrintJobEvent.NO_MORE_EVENTS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    /* There's some inefficiency here as the job set is created even though
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * it may never be requested.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    private synchronized void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        initializeAttributeSets(Doc doc, PrintRequestAttributeSet reqSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        reqAttrSet = new HashPrintRequestAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        jobAttrSet = new HashPrintJobAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        Attribute[] attrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        if (reqSet != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            reqAttrSet.addAll(reqSet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            attrs = reqSet.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            for (int i=0; i<attrs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                if (attrs[i] instanceof PrintJobAttribute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                    jobAttrSet.add(attrs[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        DocAttributeSet docSet = doc.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        if (docSet != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            attrs = docSet.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            for (int i=0; i<attrs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                if (attrs[i] instanceof PrintRequestAttribute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                    reqAttrSet.add(attrs[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                if (attrs[i] instanceof PrintJobAttribute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                    jobAttrSet.add(attrs[i]);
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        /* add the user name to the job */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        String userName = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
          userName = System.getProperty("user.name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        if (userName == null || userName.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            RequestingUserName ruName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                (RequestingUserName)reqSet.get(RequestingUserName.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            if (ruName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                jobAttrSet.add(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                    new JobOriginatingUserName(ruName.getValue(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                                               ruName.getLocale()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                jobAttrSet.add(new JobOriginatingUserName("", null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            jobAttrSet.add(new JobOriginatingUserName(userName, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        /* if no job name supplied use doc name (if supplied), if none and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
         * its a URL use that, else finally anything .. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        if (jobAttrSet.get(JobName.class) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            JobName jobName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            if (docSet != null && docSet.get(DocumentName.class) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                DocumentName docName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                    (DocumentName)docSet.get(DocumentName.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                jobName = new JobName(docName.getValue(), docName.getLocale());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                jobAttrSet.add(jobName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                String str = "JPS Job:" + doc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                    Object printData = doc.getPrintData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                    if (printData instanceof URL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                        str = ((URL)(doc.getPrintData())).toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                jobName = new JobName(str, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                jobAttrSet.add(jobName);
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
        jobAttrSet = AttributeSetUtilities.unmodifiableView(jobAttrSet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    private void getAttributeValues(DocFlavor flavor) throws PrintException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        if (reqAttrSet.get(Fidelity.class) == Fidelity.FIDELITY_TRUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            fidelity = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            fidelity = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
25140
80e863984492 8042870: Fix raw and unchecked warnings in sun.print
darcy
parents: 22584
diff changeset
   673
        Class<? extends Attribute> category;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        Attribute [] attrs = reqAttrSet.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        for (int i=0; i<attrs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            Attribute attr = attrs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            category = attr.getCategory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            if (fidelity == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                if (!service.isAttributeCategorySupported(category)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                    notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                    throw new PrintJobAttributeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                        "unsupported category: " + category, category, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                } else if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                    (!service.isAttributeValueSupported(attr, flavor, null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                    notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                    throw new PrintJobAttributeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                        "unsupported attribute: " + attr, null, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            if (category == Destination.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
              URI uri = ((Destination)attr).getURI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
              if (!"file".equals(uri.getScheme())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                throw new PrintException("Not a file: URI");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
              } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                  mDestination = (new File(uri)).getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                  throw new PrintException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                // check write access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                  try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                    security.checkWrite(mDestination);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                  } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                    notifyEvent(PrintJobEvent.JOB_FAILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                    throw new PrintException(se);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            } else if (category == JobName.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                jobName = ((JobName)attr).getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            } else if (category == Copies.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                copies = ((Copies)attr).getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            } else if (category == Media.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
              if (attr instanceof MediaSizeName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                    mediaName = (MediaSizeName)attr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                    // If requested MediaSizeName is not supported,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                    // get the corresponding media size - this will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                    // be used to create a new PageFormat.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                    if (!service.isAttributeValueSupported(attr, null, null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                        mediaSize = MediaSize.getMediaSizeForName(mediaName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            } else if (category == OrientationRequested.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                orient = (OrientationRequested)attr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    private native boolean startPrintRawData(String printerName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                                             String jobName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    private native boolean printRawData(byte[] data, int count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    private native boolean endPrintRawData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    /* Cancel PrinterJob jobs that haven't yet completed. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
   public void cancel() throws PrintException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            if (!printing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                throw new PrintException("Job is not yet submitted.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            } else if (job != null && !printReturned) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                job.cancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                notifyEvent(PrintJobEvent.JOB_CANCELED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                throw new PrintException("Job could not be cancelled.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
}