jdk/src/share/classes/sun/print/PrintJob2D.java
author malenkov
Tue, 29 Oct 2013 17:01:06 +0400
changeset 21278 ef8a3a2a72f2
parent 5506 202f599c92aa
child 23010 6dadb192ad81
permissions -rw-r--r--
8022746: List of spelling errors in API doc Reviewed-by: alexsch, smarks
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2000, 2007, 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.awt.Dimension;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.Frame;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.Graphics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.Graphics2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.PrintJob;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.JobAttributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.JobAttributes.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.awt.PageAttributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.awt.PageAttributes.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.awt.print.PageFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.awt.print.Paper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.awt.print.Printable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.awt.print.PrinterException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.awt.print.PrinterJob;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.io.FilePermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.net.URI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.net.URISyntaxException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import java.util.Properties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import javax.print.PrintService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import javax.print.attribute.HashPrintRequestAttributeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import javax.print.attribute.PrintRequestAttributeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import javax.print.attribute.ResolutionSyntax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import javax.print.attribute.Size2DSyntax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
import javax.print.attribute.standard.Chromaticity;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import javax.print.attribute.standard.Copies;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
import javax.print.attribute.standard.Destination;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
import javax.print.attribute.standard.DialogTypeSelection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
import javax.print.attribute.standard.JobName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
import javax.print.attribute.standard.MediaSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
import javax.print.attribute.standard.PrintQuality;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
import javax.print.attribute.standard.PrinterResolution;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
import javax.print.attribute.standard.SheetCollate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
import javax.print.attribute.standard.Sides;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
import javax.print.attribute.standard.Media;
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.MediaSizeName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
import javax.print.attribute.standard.PageRanges;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
import sun.print.SunPageSelection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
import sun.print.SunMinMaxPage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * A class which initiates and executes a print job using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * the underlying PrinterJob graphics conversions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * @see Toolkit#getPrintJob
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
public class PrintJob2D extends PrintJob implements Printable, Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private static final MediaType SIZES[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        MediaType.ISO_4A0, MediaType.ISO_2A0, MediaType.ISO_A0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        MediaType.ISO_A1, MediaType.ISO_A2, MediaType.ISO_A3,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        MediaType.ISO_A4, MediaType.ISO_A5, MediaType.ISO_A6,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        MediaType.ISO_A7, MediaType.ISO_A8, MediaType.ISO_A9,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        MediaType.ISO_A10, MediaType.ISO_B0, MediaType.ISO_B1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        MediaType.ISO_B2, MediaType.ISO_B3, MediaType.ISO_B4,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        MediaType.ISO_B5, MediaType.ISO_B6, MediaType.ISO_B7,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        MediaType.ISO_B8, MediaType.ISO_B9, MediaType.ISO_B10,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        MediaType.JIS_B0, MediaType.JIS_B1, MediaType.JIS_B2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        MediaType.JIS_B3, MediaType.JIS_B4, MediaType.JIS_B5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        MediaType.JIS_B6, MediaType.JIS_B7, MediaType.JIS_B8,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        MediaType.JIS_B9, MediaType.JIS_B10, MediaType.ISO_C0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        MediaType.ISO_C1, MediaType.ISO_C2, MediaType.ISO_C3,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        MediaType.ISO_C4, MediaType.ISO_C5, MediaType.ISO_C6,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        MediaType.ISO_C7, MediaType.ISO_C8, MediaType.ISO_C9,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        MediaType.ISO_C10, MediaType.ISO_DESIGNATED_LONG,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        MediaType.EXECUTIVE, MediaType.FOLIO, MediaType.INVOICE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        MediaType.LEDGER, MediaType.NA_LETTER, MediaType.NA_LEGAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        MediaType.QUARTO, MediaType.A, MediaType.B,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        MediaType.C, MediaType.D, MediaType.E,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        MediaType.NA_10X15_ENVELOPE, MediaType.NA_10X14_ENVELOPE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        MediaType.NA_10X13_ENVELOPE, MediaType.NA_9X12_ENVELOPE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        MediaType.NA_9X11_ENVELOPE, MediaType.NA_7X9_ENVELOPE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        MediaType.NA_6X9_ENVELOPE, MediaType.NA_NUMBER_9_ENVELOPE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        MediaType.NA_NUMBER_10_ENVELOPE, MediaType.NA_NUMBER_11_ENVELOPE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        MediaType.NA_NUMBER_12_ENVELOPE, MediaType.NA_NUMBER_14_ENVELOPE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        MediaType.INVITE_ENVELOPE, MediaType.ITALY_ENVELOPE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        MediaType.MONARCH_ENVELOPE, MediaType.PERSONAL_ENVELOPE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /* This array maps the above array to the objects used by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * javax.print APIs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    private static final MediaSizeName JAVAXSIZES[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        null, null, MediaSizeName.ISO_A0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        MediaSizeName.ISO_A1, MediaSizeName.ISO_A2, MediaSizeName.ISO_A3,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        MediaSizeName.ISO_A4, MediaSizeName.ISO_A5, MediaSizeName.ISO_A6,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        MediaSizeName.ISO_A7, MediaSizeName.ISO_A8, MediaSizeName.ISO_A9,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        MediaSizeName.ISO_A10, MediaSizeName.ISO_B0, MediaSizeName.ISO_B1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        MediaSizeName.ISO_B2, MediaSizeName.ISO_B3, MediaSizeName.ISO_B4,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        MediaSizeName.ISO_B5,  MediaSizeName.ISO_B6, MediaSizeName.ISO_B7,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        MediaSizeName.ISO_B8, MediaSizeName.ISO_B9, MediaSizeName.ISO_B10,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        MediaSizeName.JIS_B0, MediaSizeName.JIS_B1, MediaSizeName.JIS_B2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        MediaSizeName.JIS_B3, MediaSizeName.JIS_B4, MediaSizeName.JIS_B5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        MediaSizeName.JIS_B6, MediaSizeName.JIS_B7, MediaSizeName.JIS_B8,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        MediaSizeName.JIS_B9, MediaSizeName.JIS_B10, MediaSizeName.ISO_C0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        MediaSizeName.ISO_C1, MediaSizeName.ISO_C2, MediaSizeName.ISO_C3,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        MediaSizeName.ISO_C4, MediaSizeName.ISO_C5, MediaSizeName.ISO_C6,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        null, null, null, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        MediaSizeName.ISO_DESIGNATED_LONG, MediaSizeName.EXECUTIVE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        MediaSizeName.FOLIO, MediaSizeName.INVOICE, MediaSizeName.LEDGER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        MediaSizeName.NA_LETTER, MediaSizeName.NA_LEGAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        MediaSizeName.QUARTO, MediaSizeName.A, MediaSizeName.B,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        MediaSizeName.C, MediaSizeName.D, MediaSizeName.E,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        MediaSizeName.NA_10X15_ENVELOPE, MediaSizeName.NA_10X14_ENVELOPE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        MediaSizeName.NA_10X13_ENVELOPE, MediaSizeName.NA_9X12_ENVELOPE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        MediaSizeName.NA_9X11_ENVELOPE, MediaSizeName.NA_7X9_ENVELOPE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        MediaSizeName.NA_6X9_ENVELOPE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        MediaSizeName.NA_NUMBER_9_ENVELOPE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        MediaSizeName.NA_NUMBER_10_ENVELOPE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        MediaSizeName.NA_NUMBER_11_ENVELOPE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        MediaSizeName.NA_NUMBER_12_ENVELOPE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        MediaSizeName.NA_NUMBER_14_ENVELOPE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        null, MediaSizeName.ITALY_ENVELOPE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        MediaSizeName.MONARCH_ENVELOPE, MediaSizeName.PERSONAL_ENVELOPE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    // widths and lengths in PostScript points (1/72 in.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    private static final int WIDTHS[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        /*iso-4a0*/ 4768, /*iso-2a0*/ 3370, /*iso-a0*/ 2384, /*iso-a1*/ 1684,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        /*iso-a2*/ 1191, /*iso-a3*/ 842, /*iso-a4*/ 595, /*iso-a5*/ 420,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        /*iso-a6*/ 298, /*iso-a7*/ 210, /*iso-a8*/ 147, /*iso-a9*/ 105,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        /*iso-a10*/ 74, /*iso-b0*/ 2835, /*iso-b1*/ 2004, /*iso-b2*/ 1417,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        /*iso-b3*/ 1001, /*iso-b4*/ 709, /*iso-b5*/ 499, /*iso-b6*/ 354,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        /*iso-b7*/ 249, /*iso-b8*/ 176, /*iso-b9*/ 125, /*iso-b10*/ 88,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        /*jis-b0*/ 2920, /*jis-b1*/ 2064, /*jis-b2*/ 1460, /*jis-b3*/ 1032,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        /*jis-b4*/ 729, /*jis-b5*/ 516, /*jis-b6*/ 363, /*jis-b7*/ 258,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        /*jis-b8*/ 181, /*jis-b9*/ 128, /*jis-b10*/ 91, /*iso-c0*/ 2599,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        /*iso-c1*/ 1837, /*iso-c2*/ 1298, /*iso-c3*/ 918, /*iso-c4*/ 649,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        /*iso-c5*/ 459, /*iso-c6*/ 323, /*iso-c7*/ 230, /*iso-c8*/ 162,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        /*iso-c9*/ 113, /*iso-c10*/ 79, /*iso-designated-long*/ 312,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        /*executive*/ 522, /*folio*/ 612, /*invoice*/ 396, /*ledger*/ 792,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        /*na-letter*/ 612, /*na-legal*/ 612, /*quarto*/ 609, /*a*/ 612,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        /*b*/ 792, /*c*/ 1224, /*d*/ 1584, /*e*/ 2448,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        /*na-10x15-envelope*/ 720, /*na-10x14-envelope*/ 720,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        /*na-10x13-envelope*/ 720, /*na-9x12-envelope*/ 648,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        /*na-9x11-envelope*/ 648, /*na-7x9-envelope*/ 504,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        /*na-6x9-envelope*/ 432, /*na-number-9-envelope*/ 279,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        /*na-number-10-envelope*/ 297, /*na-number-11-envelope*/ 324,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        /*na-number-12-envelope*/ 342, /*na-number-14-envelope*/ 360,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        /*invite-envelope*/ 624, /*italy-envelope*/ 312,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        /*monarch-envelope*/ 279, /*personal-envelope*/ 261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    private static final int LENGTHS[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        /*iso-4a0*/ 6741, /*iso-2a0*/ 4768, /*iso-a0*/ 3370, /*iso-a1*/ 2384,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        /*iso-a2*/ 1684, /*iso-a3*/ 1191, /*iso-a4*/ 842, /*iso-a5*/ 595,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        /*iso-a6*/ 420, /*iso-a7*/ 298, /*iso-a8*/ 210, /*iso-a9*/ 147,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        /*iso-a10*/ 105, /*iso-b0*/ 4008, /*iso-b1*/ 2835, /*iso-b2*/ 2004,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        /*iso-b3*/ 1417, /*iso-b4*/ 1001, /*iso-b5*/ 729, /*iso-b6*/ 499,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        /*iso-b7*/ 354, /*iso-b8*/ 249, /*iso-b9*/ 176, /*iso-b10*/ 125,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        /*jis-b0*/ 4127, /*jis-b1*/ 2920, /*jis-b2*/ 2064, /*jis-b3*/ 1460,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        /*jis-b4*/ 1032, /*jis-b5*/ 729, /*jis-b6*/ 516, /*jis-b7*/ 363,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        /*jis-b8*/ 258, /*jis-b9*/ 181, /*jis-b10*/ 128, /*iso-c0*/ 3677,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        /*iso-c1*/ 2599, /*iso-c2*/ 1837, /*iso-c3*/ 1298, /*iso-c4*/ 918,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        /*iso-c5*/ 649, /*iso-c6*/ 459, /*iso-c7*/ 323, /*iso-c8*/ 230,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        /*iso-c9*/ 162, /*iso-c10*/ 113, /*iso-designated-long*/ 624,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        /*executive*/ 756, /*folio*/ 936, /*invoice*/ 612, /*ledger*/ 1224,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        /*na-letter*/ 792, /*na-legal*/ 1008, /*quarto*/ 780, /*a*/ 792,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        /*b*/ 1224, /*c*/ 1584, /*d*/ 2448, /*e*/ 3168,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        /*na-10x15-envelope*/ 1080, /*na-10x14-envelope*/ 1008,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        /*na-10x13-envelope*/ 936, /*na-9x12-envelope*/ 864,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        /*na-9x11-envelope*/ 792, /*na-7x9-envelope*/ 648,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        /*na-6x9-envelope*/ 648, /*na-number-9-envelope*/ 639,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        /*na-number-10-envelope*/ 684, /*na-number-11-envelope*/ 747,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        /*na-number-12-envelope*/ 792, /*na-number-14-envelope*/ 828,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        /*invite-envelope*/ 624, /*italy-envelope*/ 652,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        /*monarch-envelope*/ 540, /*personal-envelope*/ 468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    private Frame frame;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    private String docTitle = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    private JobAttributes jobAttributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    private PageAttributes pageAttributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    private PrintRequestAttributeSet attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * Displays the native or cross-platform dialog and allows the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * user to update job & page attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * The PrinterJob being uses to implement the PrintJob.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    private PrinterJob printerJob;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * The size of the page being used for the PrintJob.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    private PageFormat pageFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * The PrinterJob and the application run on different
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * threads and communicate through a pair of message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * queues. This queue is the list of Graphics that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * the PrinterJob has requested rendering for, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * for which the application has not yet called getGraphics().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * In practice the length of this message queue is always
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * 0 or 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    private MessageQ graphicsToBeDrawn = new MessageQ("tobedrawn");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * Used to communicate between the application's thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * and the PrinterJob's thread this message queue holds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * the list of Graphics into which the application has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * finished drawing, but that have not yet been returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * to the PrinterJob thread. Again, in practice, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * length of this message queue is always 0 or 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    private MessageQ graphicsDrawn = new MessageQ("drawn");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * The last Graphics returned to the application via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * getGraphics. This is the Graphics into which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * application is currently drawing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    private Graphics2D currentGraphics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * The zero based index of the page currently being rendered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * by the application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    private int pageIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    // The following Strings are maintained for backward-compatibility with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    // Properties based print control.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    private final static String DEST_PROP = "awt.print.destination";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    private final static String PRINTER = "printer";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    private final static String FILE = "file";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    private final static String PRINTER_PROP = "awt.print.printer";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    private final static String FILENAME_PROP = "awt.print.fileName";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    private final static String NUMCOPIES_PROP = "awt.print.numCopies";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    private final static String OPTIONS_PROP = "awt.print.options";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    private final static String ORIENT_PROP = "awt.print.orientation";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    private final static String PORTRAIT = "portrait";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    private final static String LANDSCAPE = "landscape";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    private final static String PAPERSIZE_PROP = "awt.print.paperSize";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    private final static String LETTER = "letter";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    private final static String LEGAL = "legal";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    private final static String EXECUTIVE = "executive";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    private final static String A4 = "a4";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    private Properties props;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    private String options = ""; // REMIND: needs implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * The thread on which PrinterJob is running.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * This is different than the applications thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    private Thread printerJobThread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    public PrintJob2D(Frame frame,  String doctitle,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                      final Properties props) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        this.props = props;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        this.jobAttributes = new JobAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        this.pageAttributes = new PageAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        translateInputProps();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        initPrintJob2D(frame, doctitle,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                       this.jobAttributes, this.pageAttributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    public PrintJob2D(Frame frame,  String doctitle,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                      JobAttributes jobAttributes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                      PageAttributes pageAttributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        initPrintJob2D(frame, doctitle, jobAttributes, pageAttributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    private void initPrintJob2D(Frame frame,  String doctitle,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                                JobAttributes jobAttributes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                                PageAttributes pageAttributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            security.checkPrintJobAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        if (frame == null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            (jobAttributes == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
             jobAttributes.getDialog() == DialogType.NATIVE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            throw new NullPointerException("Frame must not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        this.frame = frame;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        this.docTitle = (doctitle == null) ? "" : doctitle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        this.jobAttributes = (jobAttributes != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            ? jobAttributes : new JobAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        this.pageAttributes = (pageAttributes != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            ? pageAttributes : new PageAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        // Currently, we always reduce page ranges to xxx or xxx-xxx
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        int[][] pageRanges = this.jobAttributes.getPageRanges();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        int first = pageRanges[0][0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        int last = pageRanges[pageRanges.length - 1][1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        this.jobAttributes.setPageRanges(new int[][] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            new int[] { first, last }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        this.jobAttributes.setToPage(last);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        this.jobAttributes.setFromPage(first);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        // Verify that the cross feed and feed resolutions are the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        int[] res = this.pageAttributes.getPrinterResolution();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        if (res[0] != res[1]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            throw new IllegalArgumentException("Differing cross feed and feed"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                                               " resolutions not supported.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        // Verify that the app has access to the file system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        DestinationType dest= this.jobAttributes.getDestination();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        if (dest == DestinationType.FILE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            throwPrintToFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            // check if given filename is valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            String destStr = jobAttributes.getFileName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            if ((destStr != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                (jobAttributes.getDialog() == JobAttributes.DialogType.NONE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                File f = new File(destStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                    // check if this is a new file and if filename chars are valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                    // createNewFile returns false if file exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                    if (f.createNewFile()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                        f.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                    throw new IllegalArgumentException("Cannot write to file:"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                                                       destStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                    //There is already file read/write access so at this point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                    // only delete access is denied.  Just ignore it because in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                    // most cases the file created in createNewFile gets overwritten
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                    // anyway.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                 File pFile = f.getParentFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                 if ((f.exists() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                      (!f.isFile() || !f.canWrite())) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                     ((pFile != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                      (!pFile.exists() || (pFile.exists() && !pFile.canWrite())))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                     throw new IllegalArgumentException("Cannot write to file:"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                                                        destStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                 }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    public boolean printDialog() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        boolean proceedWithPrint = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        printerJob = PrinterJob.getPrinterJob();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        if (printerJob == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        DialogType d = this.jobAttributes.getDialog();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        PrintService pServ = printerJob.getPrintService();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        if ((pServ == null) &&  (d == DialogType.NONE)){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        copyAttributes(pServ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        DefaultSelectionType select =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            this.jobAttributes.getDefaultSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        if (select == DefaultSelectionType.RANGE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            attributes.add(SunPageSelection.RANGE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        } else if (select == DefaultSelectionType.SELECTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            attributes.add(SunPageSelection.SELECTION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            attributes.add(SunPageSelection.ALL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        if (frame != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
             attributes.add(new DialogOwner(frame));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        if ( d == DialogType.NONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            proceedWithPrint = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            if (d == DialogType.NATIVE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                attributes.add(DialogTypeSelection.NATIVE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            }  else { //  (d == DialogType.COMMON)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                attributes.add(DialogTypeSelection.COMMON);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            if (proceedWithPrint = printerJob.printDialog(attributes)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                if (pServ == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                    // Windows gives an option to install a service
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                    // when it detects there are no printers so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                    // we make sure we get the updated print service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                    pServ = printerJob.getPrintService();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                    if (pServ == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                updateAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                translateOutputProps();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        if (proceedWithPrint) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            JobName jname = (JobName)attributes.get(JobName.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            if (jname != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                printerJob.setJobName(jname.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            pageFormat = new PageFormat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            Media media = (Media)attributes.get(Media.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            MediaSize mediaSize =  null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            if (media != null  && media instanceof MediaSizeName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                mediaSize = MediaSize.getMediaSizeForName((MediaSizeName)media);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            Paper p = pageFormat.getPaper();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            if (mediaSize != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                p.setSize(mediaSize.getX(MediaSize.INCH)*72.0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                          mediaSize.getY(MediaSize.INCH)*72.0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            if (pageAttributes.getOrigin()==OriginType.PRINTABLE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                // AWT uses 1/4" borders by default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                p.setImageableArea(18.0, 18.0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                                   p.getWidth()-36.0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                                   p.getHeight()-36.0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                p.setImageableArea(0.0,0.0,p.getWidth(),p.getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            pageFormat.setPaper(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            OrientationRequested orient =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
               (OrientationRequested)attributes.get(OrientationRequested.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            if (orient!= null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                orient == OrientationRequested.REVERSE_LANDSCAPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                pageFormat.setOrientation(PageFormat.REVERSE_LANDSCAPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            } else if (orient == OrientationRequested.LANDSCAPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                pageFormat.setOrientation(PageFormat.LANDSCAPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                pageFormat.setOrientation(PageFormat.PORTRAIT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            printerJob.setPrintable(this, pageFormat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        return proceedWithPrint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    private void updateAttributes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        Copies c = (Copies)attributes.get(Copies.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        jobAttributes.setCopies(c.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        SunPageSelection sel =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            (SunPageSelection)attributes.get(SunPageSelection.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        if (sel == SunPageSelection.RANGE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            jobAttributes.setDefaultSelection(DefaultSelectionType.RANGE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        } else if (sel == SunPageSelection.SELECTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            jobAttributes.setDefaultSelection(DefaultSelectionType.SELECTION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            jobAttributes.setDefaultSelection(DefaultSelectionType.ALL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        Destination dest = (Destination)attributes.get(Destination.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        if (dest != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            jobAttributes.setDestination(DestinationType.FILE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            jobAttributes.setFileName(dest.getURI().getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            jobAttributes.setDestination(DestinationType.PRINTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        PrintService serv = printerJob.getPrintService();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        if (serv != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            jobAttributes.setPrinter(serv.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        PageRanges range = (PageRanges)attributes.get(PageRanges.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        int[][] members = range.getMembers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        jobAttributes.setPageRanges(members);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        SheetCollate collation =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            (SheetCollate)attributes.get(SheetCollate.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        if (collation == SheetCollate.COLLATED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            jobAttributes.setMultipleDocumentHandling(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            MultipleDocumentHandlingType.SEPARATE_DOCUMENTS_COLLATED_COPIES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            jobAttributes.setMultipleDocumentHandling(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            MultipleDocumentHandlingType.SEPARATE_DOCUMENTS_UNCOLLATED_COPIES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        Sides sides = (Sides)attributes.get(Sides.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        if (sides == Sides.TWO_SIDED_LONG_EDGE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            jobAttributes.setSides(SidesType.TWO_SIDED_LONG_EDGE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        } else if (sides == Sides.TWO_SIDED_SHORT_EDGE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            jobAttributes.setSides(SidesType.TWO_SIDED_SHORT_EDGE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            jobAttributes.setSides(SidesType.ONE_SIDED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        // PageAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        Chromaticity color =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            (Chromaticity)attributes.get(Chromaticity.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        if (color == Chromaticity.COLOR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            pageAttributes.setColor(ColorType.COLOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            pageAttributes.setColor(ColorType.MONOCHROME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        OrientationRequested orient =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            (OrientationRequested)attributes.get(OrientationRequested.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        if (orient == OrientationRequested.LANDSCAPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            pageAttributes.setOrientationRequested(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                                       OrientationRequestedType.LANDSCAPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            pageAttributes.setOrientationRequested(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                                       OrientationRequestedType.PORTRAIT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        PrintQuality qual = (PrintQuality)attributes.get(PrintQuality.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        if (qual == PrintQuality.DRAFT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            pageAttributes.setPrintQuality(PrintQualityType.DRAFT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        } else if (qual == PrintQuality.HIGH) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            pageAttributes.setPrintQuality(PrintQualityType.HIGH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        } else { // NORMAL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            pageAttributes.setPrintQuality(PrintQualityType.NORMAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        Media msn = (Media)attributes.get(Media.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        if (msn != null && msn instanceof MediaSizeName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            MediaType mType = unMapMedia((MediaSizeName)msn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            if (mType != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                pageAttributes.setMedia(mType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        debugPrintAttributes(false, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    private void debugPrintAttributes(boolean ja, boolean pa ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        if (ja) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            System.out.println("new Attributes\ncopies = "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                               jobAttributes.getCopies()+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                               "\nselection = "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                               jobAttributes.getDefaultSelection()+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                               "\ndest "+jobAttributes.getDestination()+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                               "\nfile "+jobAttributes.getFileName()+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                               "\nfromPage "+jobAttributes.getFromPage()+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                               "\ntoPage "+jobAttributes.getToPage()+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                               "\ncollation "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                               jobAttributes.getMultipleDocumentHandling()+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                               "\nPrinter "+jobAttributes.getPrinter()+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                               "\nSides2 "+jobAttributes.getSides()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                               );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        if (pa) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            System.out.println("new Attributes\ncolor = "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                               pageAttributes.getColor()+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                               "\norientation = "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                               pageAttributes.getOrientationRequested()+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                               "\nquality "+pageAttributes.getPrintQuality()+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                               "\nMedia2 "+pageAttributes.getMedia()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                               );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    /* From JobAttributes we will copy job name and duplex printing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * and destination.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * The majority of the rest of the attributes are reflected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * From PageAttributes we copy color, media size, orientation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * origin type, resolution and print quality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * We use the media, orientation in creating the page format, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * the origin type to set its imageable area.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * REMIND: Interpretation of resolution, additional media sizes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    private void copyAttributes(PrintService printServ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        attributes = new HashPrintRequestAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        attributes.add(new JobName(docTitle, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        PrintService pServ = printServ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        String printerName = jobAttributes.getPrinter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        if (printerName != null && printerName != ""
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            && !printerName.equals(pServ.getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            // Search for the given printerName in the list of PrintServices
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            PrintService []services = PrinterJob.lookupPrintServices();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                for (int i=0; i<services.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                    if (printerName.equals(services[i].getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                        printerJob.setPrintService(services[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                        pServ = services[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            } catch (PrinterException pe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        DestinationType dest = jobAttributes.getDestination();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        if (dest == DestinationType.FILE &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            pServ.isAttributeCategorySupported(Destination.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            String fileName = jobAttributes.getFileName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            Destination defaultDest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            if (fileName == null && (defaultDest = (Destination)pServ.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                    getDefaultAttributeValue(Destination.class)) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                attributes.add(defaultDest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                URI uri = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                    if (fileName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                        if (fileName.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                            fileName = ".";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                        // defaultDest should not be null.  The following code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                        // is only added to safeguard against a possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                        // buggy implementation of a PrintService having a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                        // null default Destination.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                        fileName = "out.prn";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                    uri = (new File(fileName)).toURI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                        // '\\' file separator is illegal character in opaque
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                        // part and causes URISyntaxException, so we replace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                        // it with '/'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                        fileName = fileName.replace('\\', '/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                        uri = new URI("file:"+fileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                    } catch (URISyntaxException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                if (uri != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                    attributes.add(new Destination(uri));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        attributes.add(new SunMinMaxPage(jobAttributes.getMinPage(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                                         jobAttributes.getMaxPage()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        SidesType sType = jobAttributes.getSides();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        if (sType == SidesType.TWO_SIDED_LONG_EDGE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
            attributes.add(Sides.TWO_SIDED_LONG_EDGE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        } else if (sType == SidesType.TWO_SIDED_SHORT_EDGE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            attributes.add(Sides.TWO_SIDED_SHORT_EDGE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        } else if (sType == SidesType.ONE_SIDED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            attributes.add(Sides.ONE_SIDED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        MultipleDocumentHandlingType hType =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
          jobAttributes.getMultipleDocumentHandling();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        if (hType ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            MultipleDocumentHandlingType.SEPARATE_DOCUMENTS_COLLATED_COPIES) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
          attributes.add(SheetCollate.COLLATED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
          attributes.add(SheetCollate.UNCOLLATED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        attributes.add(new Copies(jobAttributes.getCopies()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        attributes.add(new PageRanges(jobAttributes.getFromPage(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                                      jobAttributes.getToPage()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        if (pageAttributes.getColor() == ColorType.COLOR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            attributes.add(Chromaticity.COLOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            attributes.add(Chromaticity.MONOCHROME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        pageFormat = printerJob.defaultPage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        if (pageAttributes.getOrientationRequested() ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            OrientationRequestedType.LANDSCAPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
            pageFormat.setOrientation(PageFormat.LANDSCAPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                attributes.add(OrientationRequested.LANDSCAPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                pageFormat.setOrientation(PageFormat.PORTRAIT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                attributes.add(OrientationRequested.PORTRAIT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        MediaType media = pageAttributes.getMedia();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        MediaSizeName msn = mapMedia(media);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        if (msn != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            attributes.add(msn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        PrintQualityType qType =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            pageAttributes.getPrintQuality();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        if (qType == PrintQualityType.DRAFT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            attributes.add(PrintQuality.DRAFT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        } else if (qType == PrintQualityType.NORMAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            attributes.add(PrintQuality.NORMAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        } else if (qType == PrintQualityType.HIGH) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            attributes.add(PrintQuality.HIGH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     * Gets a Graphics object that will draw to the next page.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     * The page is sent to the printer when the graphics
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * object is disposed.  This graphics object will also implement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * the PrintGraphics interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * @see PrintGraphics
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
    public Graphics getGraphics() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        Graphics printGraphics = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            ++pageIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            // Thread should not be created after end has been called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            // One way to detect this is if any of the graphics queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            //  has been closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            if (pageIndex == 0 && !graphicsToBeDrawn.isClosed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            /* We start a thread on which the PrinterJob will run.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
             * The PrinterJob will ask for pages on that thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
             * and will use a message queue to fulfill the application's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
             * requests for a Graphics on the application's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
             * thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                startPrinterJobThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            notify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        /* If the application has already been handed back
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
         * a graphics then we need to put that graphics into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
         * the drawn queue so that the PrinterJob thread can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
         * return to the print system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        if (currentGraphics != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            graphicsDrawn.append(currentGraphics);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            currentGraphics = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        /* We'll block here until a new graphics becomes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
         * available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        currentGraphics = graphicsToBeDrawn.pop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        if (currentGraphics instanceof PeekGraphics) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            ( (PeekGraphics) currentGraphics).setAWTDrawingOnly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            graphicsDrawn.append(currentGraphics);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            currentGraphics = graphicsToBeDrawn.pop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        if (currentGraphics != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            /* In the PrintJob API, the origin is at the upper-
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
             * left of the imageable area when using the new "printable"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
             * origin attribute, otherwise its the physical origin (for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
             * backwards compatibility. We emulate this by createing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
             * a PageFormat which matches and then performing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
             * translate to the origin. This is a no-op if physical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
             * origin is specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            currentGraphics.translate(pageFormat.getImageableX(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                                      pageFormat.getImageableY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 5506
diff changeset
   815
            /* Scale to accommodate AWT's notion of printer resolution */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            double awtScale = 72.0/getPageResolutionInternal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            currentGraphics.scale(awtScale, awtScale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            /* The caller wants a Graphics instance but we do
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
             * not want them to make 2D calls. We can't hand
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
             * back a Graphics2D. The returned Graphics also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
             * needs to implement PrintGraphics, so we wrap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
             * the Graphics2D instance. The PrintJob API has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
             * the application dispose of the Graphics so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
             * we create a copy of the one returned by PrinterJob.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            printGraphics = new ProxyPrintGraphics(currentGraphics.create(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                                                   this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        return printGraphics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     * Returns the dimensions of the page in pixels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     * The resolution of the page is chosen so that it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     * is similar to the screen resolution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     * Except (since 1.3) when the application specifies a resolution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     * In that case it it scaled accordingly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
    public Dimension getPageDimension() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        double wid, hgt, scale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        if (pageAttributes != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            pageAttributes.getOrigin()==OriginType.PRINTABLE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
            wid = pageFormat.getImageableWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            hgt = pageFormat.getImageableHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            wid = pageFormat.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
            hgt = pageFormat.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        scale = getPageResolutionInternal() / 72.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        return new Dimension((int)(wid * scale), (int)(hgt * scale));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     private double getPageResolutionInternal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        if (pageAttributes != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
            int []res = pageAttributes.getPrinterResolution();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
            if (res[2] == 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                return res[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
            } else /* if (res[2] == 4) */ {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
                return (res[0] * 2.54);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
            return 72.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * Returns the resolution of the page in pixels per inch.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     * Note that this doesn't have to correspond to the physical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * resolution of the printer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
    public int getPageResolution() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        return (int)getPageResolutionInternal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     * Returns true if the last page will be printed first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
    public boolean lastPageFirst() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * Ends the print job and does any necessary cleanup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
    public synchronized void end() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        /* Prevent the PrinterJob thread from appending any more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
         * graphics to the to-be-drawn queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        graphicsToBeDrawn.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        /* If we have a currentGraphics it was the last one returned to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
         * PrintJob client. Append it to the drawn queue so that print()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
         * will return allowing the page to be flushed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
         * This really ought to happen in dispose() but for whatever reason
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
         * that isn't how the old PrintJob worked even though its spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
         * said dispose() flushed the page.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        if (currentGraphics != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
            graphicsDrawn.append(currentGraphics);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        graphicsDrawn.closeWhenEmpty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        /* Wait for the PrinterJob.print() thread to terminate, ensuring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
         * that RasterPrinterJob has made its end doc call, and resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
         * are released, files closed etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        if( printerJobThread != null && printerJobThread.isAlive() ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
                printerJobThread.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
            } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     * Ends this print job once it is no longer referenced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     * @see #end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
    public void finalize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     * Prints the page at the specified index into the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * {@link Graphics} context in the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     * format.  A <code>PrinterJob</code> calls the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     * <code>Printable</code> interface to request that a page be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     * rendered into the context specified by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     * <code>graphics</code>.  The format of the page to be drawn is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     * specified by <code>pageFormat</code>.  The zero based index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     * of the requested page is specified by <code>pageIndex</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     * If the requested page does not exist then this method returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     * NO_SUCH_PAGE; otherwise PAGE_EXISTS is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     * The <code>Graphics</code> class or subclass implements the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     * {@link PrinterGraphics} interface to provide additional
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     * information.  If the <code>Printable</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     * aborts the print job then it throws a {@link PrinterException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     * @param graphics the context into which the page is drawn
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     * @param pageFormat the size and orientation of the page being drawn
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * @param pageIndex the zero based index of the page to be drawn
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * @return PAGE_EXISTS if the page is rendered successfully
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     *         or NO_SUCH_PAGE if <code>pageIndex</code> specifies a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     *         non-existent page.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     * @exception java.awt.print.PrinterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     *         thrown when the print job is terminated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                 throws PrinterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        int result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        /* This method will be called by the PrinterJob on a thread other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
         * that the application's thread. We hold on to the graphics
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
         * until we can rendevous with the application's thread and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
         * hand over the graphics. The application then does all the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
         * drawing. When the application is done drawing we rendevous
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
         * again with the PrinterJob thread and release the Graphics
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
         * so that it knows we are done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        /* Add the graphics to the message queue of graphics to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
         * be rendered. This is really a one slot queue. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
         * application's thread will come along and remove the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
         * graphics from the queue when the app asks for a graphics.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        graphicsToBeDrawn.append( (Graphics2D) graphics);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
        /* We now wait for the app's thread to finish drawing on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
         * the Graphics. This thread will sleep until the application
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
         * release the graphics by placing it in the graphics drawn
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
         * message queue. If the application signals that it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
         * finished drawing the entire document then we'll get null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
         * returned when we try and pop a finished graphic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        if (graphicsDrawn.pop() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
            result = PAGE_EXISTS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
            result = NO_SUCH_PAGE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
    private void startPrinterJobThread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
        printerJobThread = new Thread(this, "printerJobThread");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        printerJobThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
            printerJob.print(attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        } catch (PrinterException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
            //REMIND: need to store this away and not rethrow it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        /* Close the message queues so that nobody is stuck
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
         * waiting for one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        graphicsToBeDrawn.closeWhenEmpty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        graphicsDrawn.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
    private class MessageQ {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        private String qid="noname";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        private ArrayList queue = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
        MessageQ(String id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
          qid = id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        synchronized void closeWhenEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            while (queue != null && queue.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
                    wait(1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
                } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
                    // do nothing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
            queue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
            notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        synchronized void close() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
            queue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
            notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
        synchronized boolean append(Graphics2D g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
            boolean queued = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
            if (queue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                queue.add(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
                queued = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                notify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
            return queued;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
        synchronized Graphics2D pop() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
            Graphics2D g = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
            while (g == null && queue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                if (queue.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
                    g = (Graphics2D) queue.remove(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
                    notify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
                        wait(2000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
                    } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
                        // do nothing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
            return g;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
        synchronized boolean isClosed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
            return queue == null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
    private static int[] getSize(MediaType mType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        int []dim = new int[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        dim[0] = 612;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
        dim[1] = 792;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        for (int i=0; i < SIZES.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
            if (SIZES[i] == mType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
                dim[0] = WIDTHS[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
                dim[1] = LENGTHS[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
        return dim;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
    public static MediaSizeName mapMedia(MediaType mType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        MediaSizeName media = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
        // JAVAXSIZES.length and SIZES.length must be equal!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
        // Attempt to recover by getting the smaller size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        int length = Math.min(SIZES.length, JAVAXSIZES.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
        for (int i=0; i < length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
            if (SIZES[i] == mType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
                if ((JAVAXSIZES[i] != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
                    MediaSize.getMediaSizeForName(JAVAXSIZES[i]) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
                    media = JAVAXSIZES[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
                    /* create Custom Media */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
                    media = new CustomMediaSizeName(SIZES[i].toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
                    float w = (float)Math.rint(WIDTHS[i]  / 72.0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
                    float h = (float)Math.rint(LENGTHS[i] / 72.0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
                    if (w > 0.0 && h > 0.0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
                        // add new created MediaSize to our static map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
                        // so it will be found when we call findMedia
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
                        new MediaSize(w, h, Size2DSyntax.INCH, media);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
        return media;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
    public static MediaType unMapMedia(MediaSizeName mSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        MediaType media = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        // JAVAXSIZES.length and SIZES.length must be equal!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
        // Attempt to recover by getting the smaller size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        int length = Math.min(SIZES.length, JAVAXSIZES.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
        for (int i=0; i < length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
            if (JAVAXSIZES[i] == mSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
                if (SIZES[i] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
                    media = SIZES[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
        return media;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
    private void translateInputProps() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
        if (props == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        String str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
        str = props.getProperty(DEST_PROP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        if (str != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
            if (str.equals(PRINTER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
                jobAttributes.setDestination(DestinationType.PRINTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
            } else if (str.equals(FILE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
                jobAttributes.setDestination(DestinationType.FILE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
        str = props.getProperty(PRINTER_PROP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
        if (str != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
            jobAttributes.setPrinter(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
        str = props.getProperty(FILENAME_PROP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
        if (str != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
            jobAttributes.setFileName(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
        str = props.getProperty(NUMCOPIES_PROP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        if (str != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
            jobAttributes.setCopies(Integer.parseInt(str));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
        this.options = props.getProperty(OPTIONS_PROP, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        str = props.getProperty(ORIENT_PROP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        if (str != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
            if (str.equals(PORTRAIT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
                pageAttributes.setOrientationRequested(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
                                        OrientationRequestedType.PORTRAIT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
            } else if (str.equals(LANDSCAPE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
                pageAttributes.setOrientationRequested(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
                                        OrientationRequestedType.LANDSCAPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
        str = props.getProperty(PAPERSIZE_PROP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        if (str != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
            if (str.equals(LETTER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
                pageAttributes.setMedia(SIZES[MediaType.LETTER.hashCode()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
            } else if (str.equals(LEGAL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
                pageAttributes.setMedia(SIZES[MediaType.LEGAL.hashCode()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
            } else if (str.equals(EXECUTIVE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
                pageAttributes.setMedia(SIZES[MediaType.EXECUTIVE.hashCode()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
            } else if (str.equals(A4)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
                pageAttributes.setMedia(SIZES[MediaType.A4.hashCode()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
    private void translateOutputProps() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
        if (props == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
        String str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
        props.setProperty(DEST_PROP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
            (jobAttributes.getDestination() == DestinationType.PRINTER) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
                          PRINTER : FILE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
        str = jobAttributes.getPrinter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
        if (str != null && !str.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
            props.setProperty(PRINTER_PROP, str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
        str = jobAttributes.getFileName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
        if (str != null && !str.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
            props.setProperty(FILENAME_PROP, str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
        int copies = jobAttributes.getCopies();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
        if (copies > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
            props.setProperty(NUMCOPIES_PROP, "" + copies);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
        str = this.options;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
        if (str != null && !str.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
            props.setProperty(OPTIONS_PROP, str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
        props.setProperty(ORIENT_PROP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
            (pageAttributes.getOrientationRequested() ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
             OrientationRequestedType.PORTRAIT)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
                          ? PORTRAIT : LANDSCAPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
        MediaType media = SIZES[pageAttributes.getMedia().hashCode()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
        if (media == MediaType.LETTER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
            str = LETTER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
        } else if (media == MediaType.LEGAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
            str = LEGAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
        } else if (media == MediaType.EXECUTIVE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
            str = EXECUTIVE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
        } else if (media == MediaType.A4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
            str = A4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
            str = media.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
        props.setProperty(PAPERSIZE_PROP, str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
    private void throwPrintToFile() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
        FilePermission printToFilePermission = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
            if (printToFilePermission == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
                printToFilePermission =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
                    new FilePermission("<<ALL FILES>>", "read,write");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
            security.checkPermission(printToFilePermission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
}