jdk/src/share/classes/sun/print/RasterPrinterJob.java
author malenkov
Tue, 29 Oct 2013 17:01:06 +0400
changeset 21278 ef8a3a2a72f2
parent 19781 91b06f016821
child 21283 b24d2a6e59b8
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
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 12047
diff changeset
     2
 * Copyright (c) 1998, 2012, 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: 715
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: 715
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: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
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.io.FilePermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.Color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.Dialog;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.Frame;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.Graphics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.Graphics2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.awt.GraphicsConfiguration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.awt.GraphicsEnvironment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.awt.HeadlessException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.awt.KeyboardFocusManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.awt.Rectangle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.awt.Shape;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.awt.geom.AffineTransform;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.awt.geom.Area;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.awt.geom.Point2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.awt.geom.Rectangle2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.awt.image.BufferedImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.awt.print.Book;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.awt.print.Pageable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.awt.print.PageFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.awt.print.Paper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.awt.print.Printable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import java.awt.print.PrinterAbortException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import java.awt.print.PrinterException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import java.awt.print.PrinterJob;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import java.awt.Window;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
import java.util.Locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import sun.awt.image.ByteInterleavedRaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
import javax.print.Doc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
import javax.print.DocFlavor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
import javax.print.DocPrintJob;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
import javax.print.PrintException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
import javax.print.PrintService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
import javax.print.PrintServiceLookup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
import javax.print.ServiceUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
import javax.print.StreamPrintService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
import javax.print.StreamPrintServiceFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
import javax.print.attribute.Attribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
import javax.print.attribute.AttributeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
import javax.print.attribute.HashPrintRequestAttributeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
import javax.print.attribute.PrintRequestAttributeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
import javax.print.attribute.Size2DSyntax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
import javax.print.attribute.standard.Chromaticity;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
import javax.print.attribute.standard.Copies;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
import javax.print.attribute.standard.Destination;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
import javax.print.attribute.standard.DialogTypeSelection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
import javax.print.attribute.standard.Fidelity;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
import javax.print.attribute.standard.JobName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
import javax.print.attribute.standard.JobSheets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
import javax.print.attribute.standard.Media;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
import javax.print.attribute.standard.MediaPrintableArea;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
import javax.print.attribute.standard.MediaSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
import javax.print.attribute.standard.MediaSizeName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
import javax.print.attribute.standard.OrientationRequested;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
import javax.print.attribute.standard.PageRanges;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
import javax.print.attribute.standard.PrinterState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
import javax.print.attribute.standard.PrinterStateReason;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
import javax.print.attribute.standard.PrinterStateReasons;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
import javax.print.attribute.standard.PrinterIsAcceptingJobs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
import javax.print.attribute.standard.RequestingUserName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
import javax.print.attribute.standard.SheetCollate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
import javax.print.attribute.standard.Sides;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
import sun.print.PageableDoc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
import sun.print.ServiceDialog;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
import sun.print.SunPrinterJobService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
import sun.print.SunPageSelection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * A class which rasterizes a printer job.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * @author Richard Blanchard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
public abstract class RasterPrinterJob extends PrinterJob {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 /* Class Constants */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     /* Printer destination type. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    protected static final int PRINTER = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     /* File destination type.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    protected static final int FILE = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /* Stream destination type.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    protected static final int STREAM = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * Maximum amount of memory in bytes to use for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * buffered image "band". 4Mb is a compromise between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * limiting the number of bands on hi-res printers and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * not using too much of the Java heap or causing paging
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * on systems with little RAM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    private static final int MAX_BAND_SIZE = (1024 * 1024 * 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /* Dots Per Inch */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    private static final float DPI = 72.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * Useful mainly for debugging, this system property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * can be used to force the printing code to print
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * using a particular pipeline. The two currently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * supported values are FORCE_RASTER and FORCE_PDL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    private static final String FORCE_PIPE_PROP = "sun.java2d.print.pipeline";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * When the system property FORCE_PIPE_PROP has this value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * then each page of a print job will be rendered through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * the raster pipeline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    private static final String FORCE_RASTER = "raster";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * When the system property FORCE_PIPE_PROP has this value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * then each page of a print job will be rendered through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * the PDL pipeline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    private static final String FORCE_PDL = "pdl";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * When the system property SHAPE_TEXT_PROP has this value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * then text is always rendered as a shape, and no attempt is made
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * to match the font through GDI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    private static final String SHAPE_TEXT_PROP = "sun.java2d.print.shapetext";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * values obtained from System properties in static initialiser block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public static boolean forcePDL = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public static boolean forceRaster = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    public static boolean shapeTextProp = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        /* The system property FORCE_PIPE_PROP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
         * can be used to force the printing code to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
         * use a particular pipeline. Either the raster
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
         * pipeline or the pdl pipeline can be forced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        String forceStr =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
           (String)java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                   new sun.security.action.GetPropertyAction(FORCE_PIPE_PROP));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        if (forceStr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            if (forceStr.equalsIgnoreCase(FORCE_PDL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                forcePDL = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            } else if (forceStr.equalsIgnoreCase(FORCE_RASTER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                forceRaster = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        String shapeTextStr =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
           (String)java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                   new sun.security.action.GetPropertyAction(SHAPE_TEXT_PROP));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        if (shapeTextStr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            shapeTextProp = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    /* Instance Variables */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    /**
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 19781
diff changeset
   198
     * Used to minimize GC & reallocation of band when printing
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    private int cachedBandWidth = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    private int cachedBandHeight = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    private BufferedImage cachedBand = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * The number of book copies to be printed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    private int mNumCopies = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * Collation effects the order of the pages printed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * when multiple copies are requested. For two copies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * of a three page document the page order is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *  mCollate true: 1, 2, 3, 1, 2, 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *  mCollate false: 1, 1, 2, 2, 3, 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    private boolean mCollate = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * The zero based indices of the first and last
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * pages to be printed. If 'mFirstPage' is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * UNDEFINED_PAGE_NUM then the first page to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * be printed is page 0. If 'mLastPage' is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * UNDEFINED_PAGE_NUM then the last page to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * be printed is the last one in the book.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    private int mFirstPage = Pageable.UNKNOWN_NUMBER_OF_PAGES;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    private int mLastPage = Pageable.UNKNOWN_NUMBER_OF_PAGES;
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 previous print stream Paper
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * Used to check if the paper size has changed such that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * implementation needs to emit the new paper size information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * into the print stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * Since we do our own rotation, and the margins aren't relevant,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * Its strictly the dimensions of the paper that we will check.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    private Paper previousPaper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * The document to be printed. It is initialized to an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * empty (zero pages) book.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     */
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   243
// MacOSX - made protected so subclasses can reference it.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   244
    protected Pageable mDocument = new Book();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * The name of the job being printed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     */
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   249
    private String mDocName = "Java Printing";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * Printing cancellation flags
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     */
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   255
 // MacOSX - made protected so subclasses can reference it.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   256
    protected boolean performingPrinting = false;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   257
 // MacOSX - made protected so subclasses can reference it.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   258
    protected boolean userCancelled = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    * Print to file permission variables.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    private FilePermission printToFilePermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * List of areas & the graphics state for redrawing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    private ArrayList redrawList = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    /* variables representing values extracted from an attribute set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * These take precedence over values set on a printer job
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    private int copiesAttr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    private String jobNameAttr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    private String userNameAttr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    private PageRanges pageRangesAttr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    protected Sides sidesAttr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    protected String destinationAttr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    protected boolean noJobSheet = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    protected int mDestType = RasterPrinterJob.FILE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    protected String mDestination = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    protected boolean collateAttReq = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * Device rotation flag, if it support 270, this is set to true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    protected boolean landscapeRotates270 = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * attributes used by no-args page and print dialog and print method to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * communicate state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    protected PrintRequestAttributeSet attributes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * Class to keep state information for redrawing areas
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * "region" is an area at as a high a resolution as possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * The redrawing code needs to look at sx, sy to calculate the scale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * to device resolution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    private class GraphicsState {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        Rectangle2D region;  // Area of page to repaint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        Shape theClip;       // image drawing clip.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        AffineTransform theTransform; // to transform clip to dev coords.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        double sx;           // X scale from region to device resolution
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        double sy;           // Y scale from region to device resolution
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * Service for this job
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    protected PrintService myService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
 /* Constructors */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    public RasterPrinterJob()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
/* Abstract Methods */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * Returns the resolution in dots per inch across the width
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * of the page.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    abstract protected double getXRes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * Returns the resolution in dots per inch down the height
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * of the page.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    abstract protected double getYRes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * Must be obtained from the current printer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * Value is in device pixels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * Not adjusted for orientation of the paper.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    abstract protected double getPhysicalPrintableX(Paper p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * Must be obtained from the current printer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * Value is in device pixels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * Not adjusted for orientation of the paper.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    abstract protected double getPhysicalPrintableY(Paper p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * Must be obtained from the current printer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * Value is in device pixels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * Not adjusted for orientation of the paper.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    abstract protected double getPhysicalPrintableWidth(Paper p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * Must be obtained from the current printer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * Value is in device pixels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * Not adjusted for orientation of the paper.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    abstract protected double getPhysicalPrintableHeight(Paper p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * Must be obtained from the current printer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * Value is in device pixels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * Not adjusted for orientation of the paper.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    abstract protected double getPhysicalPageWidth(Paper p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * Must be obtained from the current printer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * Value is in device pixels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * Not adjusted for orientation of the paper.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    abstract protected double getPhysicalPageHeight(Paper p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * Begin a new page.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    abstract protected void startPage(PageFormat format, Printable painter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                                      int index, boolean paperChanged)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        throws PrinterException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * End a page.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    abstract protected void endPage(PageFormat format, Printable painter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                                    int index)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        throws PrinterException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * Prints the contents of the array of ints, 'data'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * to the current page. The band is placed at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * location (x, y) in device coordinates on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * page. The width and height of the band is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * specified by the caller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    abstract protected void printBand(byte[] data, int x, int y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                                      int width, int height)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        throws PrinterException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
/* Instance Methods */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
      * save graphics state of a PathGraphics for later redrawing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
      * of part of page represented by the region in that state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    public void saveState(AffineTransform at, Shape clip,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                          Rectangle2D region, double sx, double sy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        GraphicsState gstate = new GraphicsState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        gstate.theTransform = at;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        gstate.theClip = clip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        gstate.region = region;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        gstate.sx = sx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        gstate.sy = sy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        redrawList.add(gstate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * A convenience method which returns the default service
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * for 2D <code>PrinterJob</code>s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * May return null if there is no suitable default (although there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * may still be 2D services available).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * @return default 2D print service, or null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * @since     1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    protected static PrintService lookupDefaultPrintService() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        PrintService service = PrintServiceLookup.lookupDefaultPrintService();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        /* Pageable implies Printable so checking both isn't strictly needed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        if (service != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            service.isDocFlavorSupported(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                                DocFlavor.SERVICE_FORMATTED.PAGEABLE) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            service.isDocFlavorSupported(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                                DocFlavor.SERVICE_FORMATTED.PRINTABLE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            return service;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
           PrintService []services =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
             PrintServiceLookup.lookupPrintServices(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                                DocFlavor.SERVICE_FORMATTED.PAGEABLE, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
           if (services.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
               return services[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * Returns the service (printer) for this printer job.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * Implementations of this class which do not support print services
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * may return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * @return the service for this printer job.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    public PrintService getPrintService() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        if (myService == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            PrintService svc = PrintServiceLookup.lookupDefaultPrintService();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            if (svc != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                svc.isDocFlavorSupported(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                     DocFlavor.SERVICE_FORMATTED.PAGEABLE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                    setPrintService(svc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                    myService = svc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                } catch (PrinterException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            if (myService == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                PrintService[] svcs = PrintServiceLookup.lookupPrintServices(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                    DocFlavor.SERVICE_FORMATTED.PAGEABLE, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                if (svcs.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                        setPrintService(svcs[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                        myService = svcs[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                    } catch (PrinterException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        return myService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * Associate this PrinterJob with a new PrintService.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * Throws <code>PrinterException</code> if the specified service
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * cannot support the <code>Pageable</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * <code>Printable</code> interfaces necessary to support 2D printing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * @param a print service which supports 2D printing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * @throws PrinterException if the specified service does not support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * 2D printing or no longer available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    public void setPrintService(PrintService service)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        throws PrinterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        if (service == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            throw new PrinterException("Service cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        } else if (!(service instanceof StreamPrintService) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                   service.getName() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            throw new PrinterException("Null PrintService name.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            // Check the list of services.  This service may have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            // deleted already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            PrinterState prnState = (PrinterState)service.getAttribute(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                                                  PrinterState.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            if (prnState == PrinterState.STOPPED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                PrinterStateReasons prnStateReasons =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                    (PrinterStateReasons)service.getAttribute(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                                                 PrinterStateReasons.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                if ((prnStateReasons != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                    (prnStateReasons.containsKey(PrinterStateReason.SHUTDOWN)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                    throw new PrinterException("PrintService is no longer available.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            if (service.isDocFlavorSupported(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                                             DocFlavor.SERVICE_FORMATTED.PAGEABLE) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                service.isDocFlavorSupported(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                                             DocFlavor.SERVICE_FORMATTED.PRINTABLE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                myService = service;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                throw new PrinterException("Not a 2D print service: " + service);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
15969
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   530
    private PageFormat attributeToPageFormat(PrintService service,
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   531
                                               PrintRequestAttributeSet attSet) {
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   532
        PageFormat page = defaultPage();
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   533
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   534
        if (service == null) {
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   535
            return page;
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   536
        }
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   537
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   538
        OrientationRequested orient = (OrientationRequested)
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   539
                                      attSet.get(OrientationRequested.class);
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   540
        if (orient == null) {
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   541
            orient = (OrientationRequested)
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   542
                    service.getDefaultAttributeValue(OrientationRequested.class);
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   543
        }
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   544
        if (orient == OrientationRequested.REVERSE_LANDSCAPE) {
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   545
            page.setOrientation(PageFormat.REVERSE_LANDSCAPE);
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   546
        } else if (orient == OrientationRequested.LANDSCAPE) {
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   547
            page.setOrientation(PageFormat.LANDSCAPE);
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   548
        } else {
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   549
            page.setOrientation(PageFormat.PORTRAIT);
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   550
        }
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   551
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   552
        Media media = (Media)attSet.get(Media.class);
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   553
        if (media == null) {
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   554
            media =
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   555
                (Media)service.getDefaultAttributeValue(Media.class);
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   556
        }
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   557
        if (!(media instanceof MediaSizeName)) {
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   558
            media = MediaSizeName.NA_LETTER;
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   559
        }
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   560
        MediaSize size =
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   561
            MediaSize.getMediaSizeForName((MediaSizeName)media);
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   562
        if (size == null) {
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   563
            size = MediaSize.NA.LETTER;
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   564
        }
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   565
        Paper paper = new Paper();
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   566
        float dim[] = size.getSize(1); //units == 1 to avoid FP error
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   567
        double w = Math.rint((dim[0]*72.0)/Size2DSyntax.INCH);
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   568
        double h = Math.rint((dim[1]*72.0)/Size2DSyntax.INCH);
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   569
        paper.setSize(w, h);
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   570
        MediaPrintableArea area =
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   571
             (MediaPrintableArea)
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   572
             attSet.get(MediaPrintableArea.class);
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   573
        double ix, iw, iy, ih;
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   574
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   575
        if (area != null) {
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   576
            // Should pass in same unit as updatePageAttributes
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   577
            // to avoid rounding off errors.
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   578
            ix = Math.rint(
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   579
                           area.getX(MediaPrintableArea.INCH) * DPI);
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   580
            iy = Math.rint(
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   581
                           area.getY(MediaPrintableArea.INCH) * DPI);
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   582
            iw = Math.rint(
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   583
                           area.getWidth(MediaPrintableArea.INCH) * DPI);
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   584
            ih = Math.rint(
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   585
                           area.getHeight(MediaPrintableArea.INCH) * DPI);
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   586
        }
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   587
        else {
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   588
            if (w >= 72.0 * 6.0) {
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   589
                ix = 72.0;
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   590
                iw = w - 2 * 72.0;
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   591
            } else {
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   592
                ix = w / 6.0;
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   593
                iw = w * 0.75;
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   594
            }
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   595
            if (h >= 72.0 * 6.0) {
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   596
                iy = 72.0;
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   597
                ih = h - 2 * 72.0;
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   598
            } else {
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   599
                iy = h / 6.0;
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   600
                ih = h * 0.75;
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   601
            }
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   602
        }
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   603
        paper.setImageableArea(ix, iy, iw, ih);
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   604
        page.setPaper(paper);
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   605
        return page;
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   606
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    protected void updatePageAttributes(PrintService service,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                                        PageFormat page) {
17405
9071dd38156b 8011069: Printing: NullPointerException since jdk8 b82 showing native Page Setup Dialog.
jgodinez
parents: 15969
diff changeset
   610
        if (this.attributes == null) {
9071dd38156b 8011069: Printing: NullPointerException since jdk8 b82 showing native Page Setup Dialog.
jgodinez
parents: 15969
diff changeset
   611
            this.attributes = new HashPrintRequestAttributeSet();
9071dd38156b 8011069: Printing: NullPointerException since jdk8 b82 showing native Page Setup Dialog.
jgodinez
parents: 15969
diff changeset
   612
        }
9071dd38156b 8011069: Printing: NullPointerException since jdk8 b82 showing native Page Setup Dialog.
jgodinez
parents: 15969
diff changeset
   613
15969
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   614
        updateAttributesWithPageFormat(service, page, this.attributes);
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   615
    }
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   616
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   617
    protected void updateAttributesWithPageFormat(PrintService service,
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   618
                                        PageFormat page,
17405
9071dd38156b 8011069: Printing: NullPointerException since jdk8 b82 showing native Page Setup Dialog.
jgodinez
parents: 15969
diff changeset
   619
                                        PrintRequestAttributeSet pageAttributes) {
9071dd38156b 8011069: Printing: NullPointerException since jdk8 b82 showing native Page Setup Dialog.
jgodinez
parents: 15969
diff changeset
   620
        if (service == null || page == null || pageAttributes == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        float x = (float)Math.rint(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                         (page.getPaper().getWidth()*Size2DSyntax.INCH)/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                         (72.0))/(float)Size2DSyntax.INCH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        float y = (float)Math.rint(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                         (page.getPaper().getHeight()*Size2DSyntax.INCH)/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                         (72.0))/(float)Size2DSyntax.INCH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        // We should limit the list where we search the matching
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        // media, this will prevent mapping to wrong media ex. Ledger
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        // can be mapped to B.  Especially useful when creating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        // custom MediaSize.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        Media[] mediaList = (Media[])service.getSupportedAttributeValues(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                                      Media.class, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        Media media = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            media = CustomMediaSizeName.findMedia(mediaList, x, y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                                   Size2DSyntax.INCH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        } catch (IllegalArgumentException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        if ((media == null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
             !(service.isAttributeValueSupported(media, null, null))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            media = (Media)service.getDefaultAttributeValue(Media.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        OrientationRequested orient;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        switch (page.getOrientation()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        case PageFormat.LANDSCAPE :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            orient = OrientationRequested.LANDSCAPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        case PageFormat.REVERSE_LANDSCAPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            orient = OrientationRequested.REVERSE_LANDSCAPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            orient = OrientationRequested.PORTRAIT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
17405
9071dd38156b 8011069: Printing: NullPointerException since jdk8 b82 showing native Page Setup Dialog.
jgodinez
parents: 15969
diff changeset
   660
        if (media != null) {
9071dd38156b 8011069: Printing: NullPointerException since jdk8 b82 showing native Page Setup Dialog.
jgodinez
parents: 15969
diff changeset
   661
            pageAttributes.add(media);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        }
17405
9071dd38156b 8011069: Printing: NullPointerException since jdk8 b82 showing native Page Setup Dialog.
jgodinez
parents: 15969
diff changeset
   663
        pageAttributes.add(orient);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        float ix = (float)(page.getPaper().getImageableX()/DPI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        float iw = (float)(page.getPaper().getImageableWidth()/DPI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        float iy = (float)(page.getPaper().getImageableY()/DPI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        float ih = (float)(page.getPaper().getImageableHeight()/DPI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        if (ix < 0) ix = 0f; if (iy < 0) iy = 0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        try {
17405
9071dd38156b 8011069: Printing: NullPointerException since jdk8 b82 showing native Page Setup Dialog.
jgodinez
parents: 15969
diff changeset
   671
            pageAttributes.add(new MediaPrintableArea(ix, iy, iw, ih,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                                                  MediaPrintableArea.INCH));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        } catch (IllegalArgumentException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * Display a dialog to the user allowing the modification of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * PageFormat instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * The <code>page</code> argument is used to initialize controls
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * in the page setup dialog.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * If the user cancels the dialog, then the method returns the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * original <code>page</code> object unmodified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * If the user okays the dialog then the method returns a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * PageFormat object with the indicated changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * In either case the original <code>page</code> object will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * not be modified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * @param     page    the default PageFormat presented to the user
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     *                    for modification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * @return    the original <code>page</code> object if the dialog
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     *            is cancelled, or a new PageFormat object containing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     *            the format indicated by the user if the dialog is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     *            acknowledged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * @exception HeadlessException if GraphicsEnvironment.isHeadless()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * returns true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * @see java.awt.GraphicsEnvironment#isHeadless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * @since     1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    public PageFormat pageDialog(PageFormat page)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        throws HeadlessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        if (GraphicsEnvironment.isHeadless()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            throw new HeadlessException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        final GraphicsConfiguration gc =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
          GraphicsEnvironment.getLocalGraphicsEnvironment().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
          getDefaultScreenDevice().getDefaultConfiguration();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        PrintService service =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            (PrintService)java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                                        new java.security.PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                    PrintService service = getPrintService();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                    if (service == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                        ServiceDialog.showNoPrintService(gc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                    return service;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        if (service == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
            return page;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        updatePageAttributes(service, page);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        PageFormat newPage = pageDialog(attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        if (newPage == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            return page;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            return newPage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * return a PageFormat corresponding to the updated attributes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * or null if the user cancelled the dialog.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    public PageFormat pageDialog(final PrintRequestAttributeSet attributes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        throws HeadlessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        if (GraphicsEnvironment.isHeadless()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            throw new HeadlessException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
15969
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   746
        DialogTypeSelection dlg =
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   747
            (DialogTypeSelection)attributes.get(DialogTypeSelection.class);
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   748
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   749
        // Check for native, note that default dialog is COMMON.
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   750
        if (dlg == DialogTypeSelection.NATIVE) {
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   751
            PrintService pservice = getPrintService();
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   752
            PageFormat page = pageDialog(attributeToPageFormat(pservice,
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   753
                                                               attributes));
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   754
            updateAttributesWithPageFormat(pservice, page, attributes);
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   755
            return page;
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   756
        }
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   757
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        final GraphicsConfiguration gc =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            GraphicsEnvironment.getLocalGraphicsEnvironment().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            getDefaultScreenDevice().getDefaultConfiguration();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        Rectangle bounds = gc.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        int x = bounds.x+bounds.width/3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        int y = bounds.y+bounds.height/3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        PrintService service =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            (PrintService)java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                                        new java.security.PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                    PrintService service = getPrintService();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                    if (service == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                        ServiceDialog.showNoPrintService(gc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                    return service;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        if (service == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        ServiceDialog pageDialog = new ServiceDialog(gc, x, y, service,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                                       DocFlavor.SERVICE_FORMATTED.PAGEABLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                                       attributes, (Frame)null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        pageDialog.show();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        if (pageDialog.getStatus() == ServiceDialog.APPROVE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            PrintRequestAttributeSet newas =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                pageDialog.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            Class amCategory = SunAlternateMedia.class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            if (attributes.containsKey(amCategory) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                !newas.containsKey(amCategory)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                attributes.remove(amCategory);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            attributes.addAll(newas);
15969
c7ed36e13460 8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents: 14342
diff changeset
   797
            return attributeToPageFormat(service, attributes);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * Presents the user a dialog for changing properties of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * print job interactively.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * The services browsable here are determined by the type of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * service currently installed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * If the application installed a StreamPrintService on this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * PrinterJob, only the available StreamPrintService (factories) are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * browsable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     * @param attributes to store changed properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     * @return false if the user cancels the dialog and true otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     * @exception HeadlessException if GraphicsEnvironment.isHeadless()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     * returns true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     * @see java.awt.GraphicsEnvironment#isHeadless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
    public boolean printDialog(final PrintRequestAttributeSet attributes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        throws HeadlessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        if (GraphicsEnvironment.isHeadless()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
            throw new HeadlessException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        DialogTypeSelection dlg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            (DialogTypeSelection)attributes.get(DialogTypeSelection.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        // Check for native, note that default dialog is COMMON.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        if (dlg == DialogTypeSelection.NATIVE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
            this.attributes = attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                debug_println("calling setAttributes in printDialog");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                setAttributes(attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
            } catch (PrinterException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
            boolean ret = printDialog();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            this.attributes = attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
            return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        /* A security check has already been performed in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
         * java.awt.print.printerJob.getPrinterJob method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
         * So by the time we get here, it is OK for the current thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
         * to print either to a file (from a Dialog we control!) or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
         * to a chosen printer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
         * We raise privilege when we put up the dialog, to avoid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
         * the "warning applet window" banner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        final GraphicsConfiguration gc =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            GraphicsEnvironment.getLocalGraphicsEnvironment().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
            getDefaultScreenDevice().getDefaultConfiguration();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        PrintService service =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
            (PrintService)java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                       new java.security.PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                    PrintService service = getPrintService();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
                    if (service == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
                        ServiceDialog.showNoPrintService(gc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                    return service;
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
        if (service == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        PrintService[] services;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        StreamPrintServiceFactory[] spsFactories = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        if (service instanceof StreamPrintService) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
            spsFactories = lookupStreamPrintServices(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
            services = new StreamPrintService[spsFactories.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
            for (int i=0; i<spsFactories.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
                services[i] = spsFactories[i].getPrintService(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
            services =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
            (PrintService[])java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                       new java.security.PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
                public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                    PrintService[] services = PrinterJob.lookupPrintServices();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                    return services;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
            if ((services == null) || (services.length == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                 * No services but default PrintService exists?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                 * Create services using defaultService.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                services = new PrintService[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                services[0] = service;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        Rectangle bounds = gc.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        int x = bounds.x+bounds.width/3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        int y = bounds.y+bounds.height/3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        PrintService newService;
19781
91b06f016821 4673406: RFE: Java Printing: Provide a way to display win32 printer driver's dialog
prr
parents: 17405
diff changeset
   906
        // temporarily add an attribute pointing back to this job.
91b06f016821 4673406: RFE: Java Printing: Provide a way to display win32 printer driver's dialog
prr
parents: 17405
diff changeset
   907
        PrinterJobWrapper jobWrapper = new PrinterJobWrapper(this);
91b06f016821 4673406: RFE: Java Printing: Provide a way to display win32 printer driver's dialog
prr
parents: 17405
diff changeset
   908
        attributes.add(jobWrapper);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
            newService =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
            ServiceUI.printDialog(gc, x, y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                                  services, service,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
                                  DocFlavor.SERVICE_FORMATTED.PAGEABLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
                                  attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        } catch (IllegalArgumentException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            newService = ServiceUI.printDialog(gc, x, y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
                                  services, services[0],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
                                  DocFlavor.SERVICE_FORMATTED.PAGEABLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
                                  attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        }
19781
91b06f016821 4673406: RFE: Java Printing: Provide a way to display win32 printer driver's dialog
prr
parents: 17405
diff changeset
   921
        attributes.remove(PrinterJobWrapper.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        if (newService == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        if (!service.equals(newService)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                setPrintService(newService);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
            } catch (PrinterException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                 * The only time it would throw an exception is when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
                 * newService is no longer available but we should still
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                 * select this printer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
                myService = newService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     * Presents the user a dialog for changing properties of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * print job interactively.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * @returns false if the user cancels the dialog and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     *          true otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     * @exception HeadlessException if GraphicsEnvironment.isHeadless()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     * returns true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     * @see java.awt.GraphicsEnvironment#isHeadless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
    public boolean printDialog() throws HeadlessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        if (GraphicsEnvironment.isHeadless()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
            throw new HeadlessException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        PrintRequestAttributeSet attributes =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
          new HashPrintRequestAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        attributes.add(new Copies(getCopies()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        attributes.add(new JobName(getJobName(), null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        boolean doPrint = printDialog(attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        if (doPrint) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
            JobName jobName = (JobName)attributes.get(JobName.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
            if (jobName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                setJobName(jobName.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
            Copies copies = (Copies)attributes.get(Copies.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
            if (copies != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                setCopies(copies.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
            Destination dest = (Destination)attributes.get(Destination.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
            if (dest != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
                    mDestType = RasterPrinterJob.FILE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
                    mDestination = (new File(dest.getURI())).getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                    mDestination = "out.prn";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
                    PrintService ps = getPrintService();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
                    if (ps != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                        Destination defaultDest = (Destination)ps.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
                            getDefaultAttributeValue(Destination.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
                        if (defaultDest != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
                            mDestination = (new File(defaultDest.getURI())).getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
                mDestType = RasterPrinterJob.PRINTER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
                PrintService ps = getPrintService();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
                if (ps != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
                    mDestination = ps.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
        return doPrint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     * The pages in the document to be printed by this PrinterJob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     * are drawn by the Printable object 'painter'. The PageFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     * for each page is the default page format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     * @param Printable Called to render each page of the document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
    public void setPrintable(Printable painter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        setPageable(new OpenBook(defaultPage(new PageFormat()), painter));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     * The pages in the document to be printed by this PrinterJob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     * are drawn by the Printable object 'painter'. The PageFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     * of each page is 'format'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     * @param Printable Called to render each page of the document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     * @param PageFormat The size and orientation of each page to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     *                   be printed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
    public void setPrintable(Printable painter, PageFormat format) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        setPageable(new OpenBook(format, painter));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        updatePageAttributes(getPrintService(), format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     * The pages in the document to be printed are held by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     * Pageable instance 'document'. 'document' will be queried
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     * for the number of pages as well as the PageFormat and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     * Printable for each page.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     * @param Pageable The document to be printed. It may not be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     * @exception NullPointerException the Pageable passed in was null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     * @see PageFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     * @see Printable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
    public void setPageable(Pageable document) throws NullPointerException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        if (document != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
            mDocument = document;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
    protected void initPrinter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
    protected boolean isSupportedValue(Attribute attrval,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
                                     PrintRequestAttributeSet attrset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        PrintService ps = getPrintService();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
            (attrval != null && ps != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
             ps.isAttributeValueSupported(attrval,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
                                          DocFlavor.SERVICE_FORMATTED.PAGEABLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
                                          attrset));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
    /* subclasses may need to pull extra information out of the attribute set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     * They can override this method & call super.setAttributes()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
    protected  void setAttributes(PrintRequestAttributeSet attributes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        throws PrinterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        /*  reset all values to defaults */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
        setCollated(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        sidesAttr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        pageRangesAttr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
        copiesAttr = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        jobNameAttr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
        userNameAttr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
        destinationAttr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
        collateAttReq = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
        PrintService service = getPrintService();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
        if (attributes == null  || service == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
        boolean fidelity = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
        Fidelity attrFidelity = (Fidelity)attributes.get(Fidelity.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
        if (attrFidelity != null && attrFidelity == Fidelity.FIDELITY_TRUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
            fidelity = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
        if (fidelity == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
           AttributeSet unsupported =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
               service.getUnsupportedAttributes(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
                                         DocFlavor.SERVICE_FORMATTED.PAGEABLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
                                         attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
           if (unsupported != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
               throw new PrinterException("Fidelity cannot be satisfied");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
         * Since we have verified supported values if fidelity is true,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
         * we can either ignore unsupported values, or substitute a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
         * reasonable alternative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
        SheetCollate collateAttr =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
            (SheetCollate)attributes.get(SheetCollate.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        if (isSupportedValue(collateAttr,  attributes)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
            setCollated(collateAttr == SheetCollate.COLLATED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        sidesAttr = (Sides)attributes.get(Sides.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        if (!isSupportedValue(sidesAttr,  attributes)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
            sidesAttr = Sides.ONE_SIDED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        pageRangesAttr =  (PageRanges)attributes.get(PageRanges.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
        if (!isSupportedValue(pageRangesAttr, attributes)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
            pageRangesAttr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
            if ((SunPageSelection)attributes.get(SunPageSelection.class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
                     == SunPageSelection.RANGE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
                // get to, from, min, max page ranges
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
                int[][] range = pageRangesAttr.getMembers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
                // setPageRanges uses 0-based indexing so we subtract 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
                setPageRange(range[0][0] - 1, range[0][1] - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
               setPageRange(-1, - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
        Copies copies = (Copies)attributes.get(Copies.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
        if (isSupportedValue(copies,  attributes) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
            (!fidelity && copies != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
            copiesAttr = copies.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
            setCopies(copiesAttr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
            copiesAttr = getCopies();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        Destination destination =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
            (Destination)attributes.get(Destination.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
        if (isSupportedValue(destination,  attributes)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
                // Old code (new File(destination.getURI())).getPath()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
                // would generate a "URI is not hierarchical" IAE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
                // for "file:out.prn" so we use getSchemeSpecificPart instead
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
                destinationAttr = "" + new File(destination.getURI().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
                                                getSchemeSpecificPart());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
            } catch (Exception e) { // paranoid exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
                Destination defaultDest = (Destination)service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
                    getDefaultAttributeValue(Destination.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
                if (defaultDest != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
                    destinationAttr = "" + new File(defaultDest.getURI().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
                                                getSchemeSpecificPart());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        JobSheets jobSheets = (JobSheets)attributes.get(JobSheets.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
        if (jobSheets != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
            noJobSheet = jobSheets == JobSheets.NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
        JobName jobName = (JobName)attributes.get(JobName.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
        if (isSupportedValue(jobName,  attributes) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
            (!fidelity && jobName != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
            jobNameAttr = jobName.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
            setJobName(jobNameAttr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
            jobNameAttr = getJobName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
        RequestingUserName userName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
            (RequestingUserName)attributes.get(RequestingUserName.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        if (isSupportedValue(userName,  attributes) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
            (!fidelity && userName != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
            userNameAttr = userName.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
                userNameAttr = getUserName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
            } catch (SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
                userNameAttr = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
        /* OpenBook is used internally only when app uses Printable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
         * This is the case when we use the values from the attribute set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
        Media media = (Media)attributes.get(Media.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        OrientationRequested orientReq =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
           (OrientationRequested)attributes.get(OrientationRequested.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        MediaPrintableArea mpa =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
            (MediaPrintableArea)attributes.get(MediaPrintableArea.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
        if ((orientReq != null || media != null || mpa != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
            getPageable() instanceof OpenBook) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
            /* We could almost(!) use PrinterJob.getPageFormat() except
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
             * here we need to start with the PageFormat from the OpenBook :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
            Pageable pageable = getPageable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
            Printable printable = pageable.getPrintable(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
            PageFormat pf = (PageFormat)pageable.getPageFormat(0).clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
            Paper paper = pf.getPaper();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
            /* If there's a media but no media printable area, we can try
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
             * to retrieve the default value for mpa and use that.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
            if (mpa == null && media != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
                service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
                isAttributeCategorySupported(MediaPrintableArea.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
                Object mpaVals = service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
                    getSupportedAttributeValues(MediaPrintableArea.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
                                                null, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
                if (mpaVals instanceof MediaPrintableArea[] &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
                    ((MediaPrintableArea[])mpaVals).length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
                    mpa = ((MediaPrintableArea[])mpaVals)[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
            if (isSupportedValue(orientReq, attributes) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
                (!fidelity && orientReq != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
                int orient;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
                if (orientReq.equals(OrientationRequested.REVERSE_LANDSCAPE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
                    orient = PageFormat.REVERSE_LANDSCAPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
                } else if (orientReq.equals(OrientationRequested.LANDSCAPE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
                    orient = PageFormat.LANDSCAPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
                    orient = PageFormat.PORTRAIT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
                pf.setOrientation(orient);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
            if (isSupportedValue(media, attributes) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
                (!fidelity && media != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
                if (media instanceof MediaSizeName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
                    MediaSizeName msn = (MediaSizeName)media;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
                    MediaSize msz = MediaSize.getMediaSizeForName(msn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
                    if (msz != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
                        float paperWid =  msz.getX(MediaSize.INCH) * 72.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
                        float paperHgt =  msz.getY(MediaSize.INCH) * 72.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
                        paper.setSize(paperWid, paperHgt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
                        if (mpa == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
                            paper.setImageableArea(72.0, 72.0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
                                                   paperWid-144.0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
                                                   paperHgt-144.0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
            if (isSupportedValue(mpa, attributes) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
                (!fidelity && mpa != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
                float [] printableArea =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
                    mpa.getPrintableArea(MediaPrintableArea.INCH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
                for (int i=0; i < printableArea.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
                    printableArea[i] = printableArea[i]*72.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
                paper.setImageableArea(printableArea[0], printableArea[1],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
                                       printableArea[2], printableArea[3]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
            pf.setPaper(paper);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
            pf = validatePage(pf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
            setPrintable(printable, pf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
            // for AWT where pageable is not an instance of OpenBook,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
            // we need to save paper info
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
            this.attributes = attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
     * Services we don't recognize as built-in services can't be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
     * implemented as subclasses of PrinterJob, therefore we create
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
     * a DocPrintJob from their service and pass a Doc representing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
     * the application's printjob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
     */
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
  1275
// MacOSX - made protected so subclasses can reference it.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
  1276
    protected void spoolToService(PrintService psvc,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
                                PrintRequestAttributeSet attributes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
        throws PrinterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
        if (psvc == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
            throw new PrinterException("No print service found.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
        DocPrintJob job = psvc.createPrintJob();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
        Doc doc = new PageableDoc(getPageable());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
        if (attributes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
            attributes = new HashPrintRequestAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
            job.print(doc, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
        } catch (PrintException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
            throw new PrinterException(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
     * Prints a set of pages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
     * @exception java.awt.print.PrinterException an error in the print system
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
     *                                          caused the job to be aborted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
     * @see java.awt.print.Book
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
     * @see java.awt.print.Pageable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
     * @see java.awt.print.Printable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
    public void print() throws PrinterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
        print(attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
    public static boolean debugPrint = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
    protected void debug_println(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
        if (debugPrint) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
            System.out.println("RasterPrinterJob "+str+" "+this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
    public void print(PrintRequestAttributeSet attributes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
        throws PrinterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
         * In the future PrinterJob will probably always dispatch
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
         * the print job to the PrintService.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
         * This is how third party 2D Print Services will be invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
         * when applications use the PrinterJob API.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
         * However the JRE's concrete PrinterJob implementations have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
         * not yet been re-worked to be implemented as standalone
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
         * services, and are implemented only as subclasses of PrinterJob.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
         * So here we dispatch only those services we do not recognize
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
         * as implemented through platform subclasses of PrinterJob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
         * (and this class).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
        PrintService psvc = getPrintService();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
        debug_println("psvc = "+psvc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
        if (psvc == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
            throw new PrinterException("No print service found.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
        // Check the list of services.  This service may have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
        // deleted already
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
        PrinterState prnState = (PrinterState)psvc.getAttribute(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
                                                  PrinterState.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
        if (prnState == PrinterState.STOPPED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
            PrinterStateReasons prnStateReasons =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
                    (PrinterStateReasons)psvc.getAttribute(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
                                                 PrinterStateReasons.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
                if ((prnStateReasons != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
                    (prnStateReasons.containsKey(PrinterStateReason.SHUTDOWN)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
                    throw new PrinterException("PrintService is no longer available.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
        if ((PrinterIsAcceptingJobs)(psvc.getAttribute(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
                         PrinterIsAcceptingJobs.class)) ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
                         PrinterIsAcceptingJobs.NOT_ACCEPTING_JOBS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
            throw new PrinterException("Printer is not accepting job.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
        if ((psvc instanceof SunPrinterJobService) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
            ((SunPrinterJobService)psvc).usesClass(getClass())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
            setAttributes(attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
            // throw exception for invalid destination
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
            if (destinationAttr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
                // destinationAttr is null for Destination(new URI(""))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
                // because isAttributeValueSupported returns false in setAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
                // Destination(new URI(" ")) throws URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
                File f = new File(destinationAttr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
                    // check if this is a new file and if filename chars are valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
                    if (f.createNewFile()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
                        f.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
                } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
                    throw new PrinterException("Cannot write to file:"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
                                               destinationAttr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
                } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
                    //There is already file read/write access so at this point
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
                    // only delete access is denied.  Just ignore it because in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
                    // most cases the file created in createNewFile gets overwritten
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
                    // anyway.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
                File pFile = f.getParentFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
                if ((f.exists() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
                     (!f.isFile() || !f.canWrite())) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
                    ((pFile != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
                     (!pFile.exists() || (pFile.exists() && !pFile.canWrite())))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
                    throw new PrinterException("Cannot write to file:"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
                                               destinationAttr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
            spoolToService(psvc, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
        /* We need to make sure that the collation and copies
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
         * settings are initialised */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
        initPrinter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
        int numCollatedCopies = getCollatedCopies();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
        int numNonCollatedCopies = getNoncollatedCopies();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
        debug_println("getCollatedCopies()  "+numCollatedCopies
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
              + " getNoncollatedCopies() "+ numNonCollatedCopies);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
        /* Get the range of pages we are to print. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
         * last page to print is unknown, then we print to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
         * the end of the document. Note that firstPage
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
         * and lastPage are 0 based page indices.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
        int numPages = mDocument.getNumberOfPages();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
        if (numPages == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
        int firstPage = getFirstPage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
        int lastPage = getLastPage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
        if(lastPage == Pageable.UNKNOWN_NUMBER_OF_PAGES){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
            int totalPages = mDocument.getNumberOfPages();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
            if (totalPages != Pageable.UNKNOWN_NUMBER_OF_PAGES) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
                lastPage = mDocument.getNumberOfPages() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
                performingPrinting = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
                userCancelled = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
            startDoc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
            if (isCancelled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
                cancelDoc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
            // PageRanges can be set even if RANGE is not selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
            // so we need to check if it is selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
            boolean rangeIsSelected = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
            if (attributes != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
                SunPageSelection pages =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
                    (SunPageSelection)attributes.get(SunPageSelection.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
                if ((pages != null) && (pages != SunPageSelection.RANGE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
                    rangeIsSelected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
            debug_println("after startDoc rangeSelected? "+rangeIsSelected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
                      + " numNonCollatedCopies "+ numNonCollatedCopies);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
            /* Three nested loops iterate over the document. The outer loop
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
             * counts the number of collated copies while the inner loop
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
             * counts the number of nonCollated copies. Normally, one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
             * these two loops will only execute once; that is we will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
             * either print collated copies or noncollated copies. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
             * middle loop iterates over the pages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
             * If a PageRanges attribute is used, it constrains the pages
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
             * that are imaged. If a platform subclass (though a user dialog)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
             * requests a page range via setPageRange(). it too can
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
             * constrain the page ranges that are imaged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
             * It is expected that only one of these will be used in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
             * job but both should be able to co-exist.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
            for(int collated = 0; collated < numCollatedCopies; collated++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
                for(int i = firstPage, pageResult = Printable.PAGE_EXISTS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
                    (i <= lastPage ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
                     lastPage == Pageable.UNKNOWN_NUMBER_OF_PAGES)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
                    && pageResult == Printable.PAGE_EXISTS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
                    i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
                    if ((pageRangesAttr != null) && rangeIsSelected ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
                        int nexti = pageRangesAttr.next(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
                        if (nexti == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
                        } else if (nexti != i+1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
                            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
                    for(int nonCollated = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
                        nonCollated < numNonCollatedCopies
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
                        && pageResult == Printable.PAGE_EXISTS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
                        nonCollated++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
                        if (isCancelled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
                            cancelDoc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
                        debug_println("printPage "+i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
                        pageResult = printPage(mDocument, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
            if (isCancelled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
                cancelDoc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
            // reset previousPaper in case this job is invoked again.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
            previousPaper = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
                if (performingPrinting) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
                    endDoc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
                performingPrinting = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
                notify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
     * updates a Paper object to reflect the current printer's selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
     * paper size and imageable area for that paper size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
     * Default implementation copies settings from the original, applies
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
     * applies some validity checks, changes them only if they are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
     * clearly unreasonable, then sets them into the new Paper.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
     * Subclasses are expected to override this method to make more
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
     * informed decisons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
    protected void validatePaper(Paper origPaper, Paper newPaper) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
        if (origPaper == null || newPaper == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
            double wid = origPaper.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
            double hgt = origPaper.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
            double ix = origPaper.getImageableX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
            double iy = origPaper.getImageableY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
            double iw = origPaper.getImageableWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
            double ih = origPaper.getImageableHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
            /* Assume any +ve values are legal. Overall paper dimensions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
             * take precedence. Make sure imageable area fits on the paper.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
            Paper defaultPaper = new Paper();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
            wid = ((wid > 0.0) ? wid : defaultPaper.getWidth());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
            hgt = ((hgt > 0.0) ? hgt : defaultPaper.getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
            ix = ((ix > 0.0) ? ix : defaultPaper.getImageableX());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
            iy = ((iy > 0.0) ? iy : defaultPaper.getImageableY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
            iw = ((iw > 0.0) ? iw : defaultPaper.getImageableWidth());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
            ih = ((ih > 0.0) ? ih : defaultPaper.getImageableHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
            /* full width/height is not likely to be imageable, but since we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
             * don't know the limits we have to allow it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
            if (iw > wid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
                iw = wid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
            if (ih > hgt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
                ih = hgt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
            if ((ix + iw) > wid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
                ix = wid - iw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
            if ((iy + ih) > hgt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
                iy = hgt - ih;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
            newPaper.setSize(wid, hgt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
            newPaper.setImageableArea(ix, iy, iw, ih);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
     * The passed in PageFormat will be copied and altered to describe
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
     * the default page size and orientation of the PrinterJob's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
     * current printer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
     * Platform subclasses which can access the actual default paper size
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
     * for a printer may override this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
    public PageFormat defaultPage(PageFormat page) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
        PageFormat newPage = (PageFormat)page.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
        newPage.setOrientation(PageFormat.PORTRAIT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
        Paper newPaper = new Paper();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
        double ptsPerInch = 72.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
        double w, h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
        Media media = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
        PrintService service = getPrintService();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
        if (service != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
            MediaSize size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
            media =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
                (Media)service.getDefaultAttributeValue(Media.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
            if (media instanceof MediaSizeName &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
               ((size = MediaSize.getMediaSizeForName((MediaSizeName)media)) !=
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
                null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
                w =  size.getX(MediaSize.INCH) * ptsPerInch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
                h =  size.getY(MediaSize.INCH) * ptsPerInch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
                newPaper.setSize(w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
                newPaper.setImageableArea(ptsPerInch, ptsPerInch,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
                                          w - 2.0*ptsPerInch,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
                                          h - 2.0*ptsPerInch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
                newPage.setPaper(newPaper);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
                return newPage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
        /* Default to A4 paper outside North America.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
        String defaultCountry = Locale.getDefault().getCountry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
        if (!Locale.getDefault().equals(Locale.ENGLISH) && // ie "C"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
            defaultCountry != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
            !defaultCountry.equals(Locale.US.getCountry()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
            !defaultCountry.equals(Locale.CANADA.getCountry())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
            double mmPerInch = 25.4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
            w = Math.rint((210.0*ptsPerInch)/mmPerInch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
            h = Math.rint((297.0*ptsPerInch)/mmPerInch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
            newPaper.setSize(w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
            newPaper.setImageableArea(ptsPerInch, ptsPerInch,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
                                      w - 2.0*ptsPerInch,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
                                      h - 2.0*ptsPerInch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
        newPage.setPaper(newPaper);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
        return newPage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
     * The passed in PageFormat is cloned and altered to be usable on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
     * the PrinterJob's current printer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
    public PageFormat validatePage(PageFormat page) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
        PageFormat newPage = (PageFormat)page.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
        Paper newPaper = new Paper();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
        validatePaper(newPage.getPaper(), newPaper);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
        newPage.setPaper(newPaper);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
        return newPage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
     * Set the number of copies to be printed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
    public void setCopies(int copies) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
        mNumCopies = copies;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
     * Get the number of copies to be printed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
    public int getCopies() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
        return mNumCopies;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
   /* Used when executing a print job where an attribute set may
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
     * over ride API values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
    protected int getCopiesInt() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
        return (copiesAttr > 0) ? copiesAttr : getCopies();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
     * Get the name of the printing user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
     * The caller must have security permission to read system properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
    public String getUserName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
        return System.getProperty("user.name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
   /* Used when executing a print job where an attribute set may
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
     * over ride API values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
    protected String getUserNameInt() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
        if  (userNameAttr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
            return userNameAttr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
                return  getUserName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
            } catch (SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
                return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
     * Set the name of the document to be printed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
     * The document name can not be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
    public void setJobName(String jobName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
        if (jobName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
            mDocName = jobName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
     * Get the name of the document to be printed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
    public String getJobName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
        return mDocName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
    /* Used when executing a print job where an attribute set may
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
     * over ride API values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
    protected String getJobNameInt() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
        return (jobNameAttr != null) ? jobNameAttr : getJobName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
     * Set the range of pages from a Book to be printed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
     * Both 'firstPage' and 'lastPage' are zero based
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
     * page indices. If either parameter is less than
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
     * zero then the page range is set to be from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
     * first page to the last.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
    protected void setPageRange(int firstPage, int lastPage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
        if(firstPage >= 0 && lastPage >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
            mFirstPage = firstPage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
            mLastPage = lastPage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
            if(mLastPage < mFirstPage) mLastPage = mFirstPage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
            mFirstPage = Pageable.UNKNOWN_NUMBER_OF_PAGES;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
            mLastPage = Pageable.UNKNOWN_NUMBER_OF_PAGES;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
     * Return the zero based index of the first page to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
     * be printed in this job.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
    protected int getFirstPage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
        return mFirstPage == Book.UNKNOWN_NUMBER_OF_PAGES ? 0 : mFirstPage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
     * Return the zero based index of the last page to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
     * be printed in this job.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
    protected int getLastPage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
        return mLastPage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
     * Set whether copies should be collated or not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
     * Two collated copies of a three page document
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
     * print in this order: 1, 2, 3, 1, 2, 3 while
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
     * uncollated copies print in this order:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
     * 1, 1, 2, 2, 3, 3.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
     * This is set when request is using an attribute set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
    protected void setCollated(boolean collate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
        mCollate = collate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
        collateAttReq = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
     * Return true if collated copies will be printed as determined
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
     * in an attribute set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
    protected boolean isCollated() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
            return mCollate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
     * Called by the print() method at the start of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
     * a print job.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
    protected abstract void startDoc() throws PrinterException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
     * Called by the print() method at the end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
     * a print job.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
    protected abstract void endDoc() throws PrinterException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
    /* Called by cancelDoc */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
    protected abstract void abortDoc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
  1773
// MacOSX - made protected so subclasses can reference it.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
  1774
    protected void cancelDoc() throws PrinterAbortException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
        abortDoc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
            userCancelled = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
            performingPrinting = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
            notify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
        throw new PrinterAbortException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
     * Returns how many times the entire book should
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
     * be printed by the PrintJob. If the printer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
     * itself supports collation then this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
     * should return 1 indicating that the entire
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
     * book need only be printed once and the copies
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
     * will be collated and made in the printer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
    protected int getCollatedCopies() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
        return isCollated() ? getCopiesInt() : 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
     * Returns how many times each page in the book
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
     * should be consecutively printed by PrintJob.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
     * If the printer makes copies itself then this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
     * method should return 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
    protected int getNoncollatedCopies() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
        return isCollated() ? 1 : getCopiesInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
    /* The printer graphics config is cached on the job, so that it can
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
     * be created once, and updated only as needed (for now only to change
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
     * the bounds if when using a Pageable the page sizes changes).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
    private int deviceWidth, deviceHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
    private AffineTransform defaultDeviceTransform;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
    private PrinterGraphicsConfig pgConfig;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
    synchronized void setGraphicsConfigInfo(AffineTransform at,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
                                            double pw, double ph) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
        Point2D.Double pt = new Point2D.Double(pw, ph);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
        at.transform(pt, pt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
        if (pgConfig == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
            defaultDeviceTransform == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
            !at.equals(defaultDeviceTransform) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
            deviceWidth != (int)pt.getX() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
            deviceHeight != (int)pt.getY()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
                deviceWidth = (int)pt.getX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
                deviceHeight = (int)pt.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
                defaultDeviceTransform = at;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
                pgConfig = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
    synchronized PrinterGraphicsConfig getPrinterGraphicsConfig() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
        if (pgConfig != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
            return pgConfig;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
        String deviceID = "Printer Device";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
        PrintService service = getPrintService();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
        if (service != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
            deviceID = service.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
        pgConfig = new PrinterGraphicsConfig(deviceID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
                                             defaultDeviceTransform,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
                                             deviceWidth, deviceHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
        return pgConfig;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
     * Print a page from the provided document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
     * @return int Printable.PAGE_EXISTS if the page existed and was drawn and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
     *             Printable.NO_SUCH_PAGE if the page did not exist.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
     * @see java.awt.print.Printable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
    protected int printPage(Pageable document, int pageIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
        throws PrinterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
        PageFormat page;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
        PageFormat origPage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
        Printable painter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
            origPage = document.getPageFormat(pageIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
            page = (PageFormat)origPage.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
            painter = document.getPrintable(pageIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
            PrinterException pe =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
                    new PrinterException("Error getting page or printable.[ " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
                                          e +" ]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
            pe.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
            throw pe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
        /* Get the imageable area from Paper instead of PageFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
         * because we do not want it adjusted by the page orientation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
        Paper paper = page.getPaper();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
        // if non-portrait and 270 degree landscape rotation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
        if (page.getOrientation() != PageFormat.PORTRAIT &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
            landscapeRotates270) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
            double left = paper.getImageableX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
            double top = paper.getImageableY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
            double width = paper.getImageableWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
            double height = paper.getImageableHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
            paper.setImageableArea(paper.getWidth()-left-width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
                                   paper.getHeight()-top-height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
                                   width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
            page.setPaper(paper);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
            if (page.getOrientation() == PageFormat.LANDSCAPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
                page.setOrientation(PageFormat.REVERSE_LANDSCAPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
                page.setOrientation(PageFormat.LANDSCAPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
        double xScale = getXRes() / 72.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
        double yScale = getYRes() / 72.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
        /* The deviceArea is the imageable area in the printer's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
         * resolution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
        Rectangle2D deviceArea =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
            new Rectangle2D.Double(paper.getImageableX() * xScale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
                                   paper.getImageableY() * yScale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
                                   paper.getImageableWidth() * xScale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
                                   paper.getImageableHeight() * yScale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
        /* Build and hold on to a uniform transform so that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
         * we can get back to device space at the beginning
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
         * of each band.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
        AffineTransform uniformTransform = new AffineTransform();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
        /* The scale transform is used to switch from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
         * device space to the user's 72 dpi space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
        AffineTransform scaleTransform = new AffineTransform();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
        scaleTransform.scale(xScale, yScale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
        /* bandwidth is multiple of 4 as the data is used in a win32 DIB and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
         * some drivers behave badly if scanlines aren't multiples of 4 bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
        int bandWidth = (int) deviceArea.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
        if (bandWidth % 4 != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
            bandWidth += (4 - (bandWidth % 4));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
        if (bandWidth <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
            throw new PrinterException("Paper's imageable width is too small.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
        int deviceAreaHeight = (int)deviceArea.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
        if (deviceAreaHeight <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
            throw new PrinterException("Paper's imageable height is too small.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
        /* Figure out the number of lines that will fit into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
         * our maximum band size. The hard coded 3 reflects the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
         * fact that we can only create 24 bit per pixel 3 byte BGR
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
         * BufferedImages. FIX.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
        int bandHeight = (int)(MAX_BAND_SIZE / bandWidth / 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
        int deviceLeft = (int)Math.rint(paper.getImageableX() * xScale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
        int deviceTop  = (int)Math.rint(paper.getImageableY() * yScale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
        /* The device transform is used to move the band down
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
         * the page using translates. Normally this is all it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
         * would do, but since, when printing, the Window's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
         * DIB format wants the last line to be first (lowest) in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
         * memory, the deviceTransform moves the origin to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
         * bottom of the band and flips the origin. This way the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
         * app prints upside down into the band which is the DIB
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
         * format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
        AffineTransform deviceTransform = new AffineTransform();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
        deviceTransform.translate(-deviceLeft, deviceTop);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
        deviceTransform.translate(0, bandHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
        deviceTransform.scale(1, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
        /* Create a BufferedImage to hold the band. We set the clip
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
         * of the band to be tight around the bits so that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
         * application can use it to figure what part of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
         * page needs to be drawn. The clip is never altered in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
         * this method, but we do translate the band's coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
         * system so that the app will see the clip moving down the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
         * page though it s always around the same set of pixels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
        BufferedImage pBand = new BufferedImage(1, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
                                                BufferedImage.TYPE_3BYTE_BGR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
        /* Have the app draw into a PeekGraphics object so we can
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
         * learn something about the needs of the print job.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
        PeekGraphics peekGraphics = createPeekGraphics(pBand.createGraphics(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
                                                       this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
        Rectangle2D.Double pageFormatArea =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
            new Rectangle2D.Double(page.getImageableX(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
                                   page.getImageableY(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
                                   page.getImageableWidth(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
                                   page.getImageableHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
        peekGraphics.transform(scaleTransform);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
        peekGraphics.translate(-getPhysicalPrintableX(paper) / xScale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
                               -getPhysicalPrintableY(paper) / yScale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
        peekGraphics.transform(new AffineTransform(page.getMatrix()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
        initPrinterGraphics(peekGraphics, pageFormatArea);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
        AffineTransform pgAt = peekGraphics.getTransform();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
        /* Update the information used to return a GraphicsConfiguration
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
         * for this printer device. It needs to be updated per page as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
         * not all pages in a job may be the same size (different bounds)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
         * The transform is the scaling transform as this corresponds to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
         * the default transform for the device. The width and height are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
         * those of the paper, not the page format, as we want to describe
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
         * the bounds of the device in its natural coordinate system of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
         * device coordinate whereas a page format may be in a rotated context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
        setGraphicsConfigInfo(scaleTransform,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
                              paper.getWidth(), paper.getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
        int pageResult = painter.print(peekGraphics, origPage, pageIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
        debug_println("pageResult "+pageResult);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
        if (pageResult == Printable.PAGE_EXISTS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
            debug_println("startPage "+pageIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
            /* We need to check if the paper size is changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
             * Note that it is not sufficient to ask for the pageformat
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
             * of "pageIndex-1", since PageRanges mean that pages can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
             * skipped. So we have to look at the actual last paper size used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
            Paper thisPaper = page.getPaper();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
            boolean paperChanged =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
                previousPaper == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
                thisPaper.getWidth() != previousPaper.getWidth() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
                thisPaper.getHeight() != previousPaper.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
            previousPaper = thisPaper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
            startPage(page, painter, pageIndex, paperChanged);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
            Graphics2D pathGraphics = createPathGraphics(peekGraphics, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
                                                         painter, page,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
                                                         pageIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
            /* If we can convert the page directly to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
             * underlying graphics system then we do not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
             * need to rasterize. We also may not need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
             * create the 'band' if all the pages can take
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
             * this path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
            if (pathGraphics != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
                pathGraphics.transform(scaleTransform);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
                // user (0,0) should be origin of page, not imageable area
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
                pathGraphics.translate(-getPhysicalPrintableX(paper) / xScale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
                                       -getPhysicalPrintableY(paper) / yScale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
                pathGraphics.transform(new AffineTransform(page.getMatrix()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
                initPrinterGraphics(pathGraphics, pageFormatArea);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
                redrawList.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
                AffineTransform initialTx = pathGraphics.getTransform();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
                painter.print(pathGraphics, origPage, pageIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
                for (int i=0;i<redrawList.size();i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
                   GraphicsState gstate = (GraphicsState)redrawList.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
                   pathGraphics.setTransform(initialTx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
                   ((PathGraphics)pathGraphics).redrawRegion(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
                                                         gstate.region,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
                                                         gstate.sx,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
                                                         gstate.sy,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
                                                         gstate.theClip,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
                                                         gstate.theTransform);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
            /* This is the banded-raster printing loop.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
             * It should be moved into its own method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
                BufferedImage band = cachedBand;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
                if (cachedBand == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
                    bandWidth != cachedBandWidth ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
                    bandHeight != cachedBandHeight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
                    band = new BufferedImage(bandWidth, bandHeight,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
                                             BufferedImage.TYPE_3BYTE_BGR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
                    cachedBand = band;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
                    cachedBandWidth = bandWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
                    cachedBandHeight = bandHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
                Graphics2D bandGraphics = band.createGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
                Rectangle2D.Double clipArea =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
                    new Rectangle2D.Double(0, 0, bandWidth, bandHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
                initPrinterGraphics(bandGraphics, clipArea);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
                ProxyGraphics2D painterGraphics =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
                    new ProxyGraphics2D(bandGraphics, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
                Graphics2D clearGraphics = band.createGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
                clearGraphics.setColor(Color.white);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
                /* We need the actual bits of the BufferedImage to send to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
                 * the native Window's code. 'data' points to the actual
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
                 * pixels. Right now these are in ARGB format with 8 bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
                 * per component. We need to use a monochrome BufferedImage
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
                 * for monochrome printers when this is supported by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
                 * BufferedImage. FIX
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
                ByteInterleavedRaster tile = (ByteInterleavedRaster)band.getRaster();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
                byte[] data = tile.getDataStorage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
                /* Loop over the page moving our band down the page,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
                 * calling the app to render the band, and then send the band
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
                 * to the printer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
                int deviceBottom = deviceTop + deviceAreaHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
                /* device's printable x,y is really addressable origin
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
                 * we address relative to media origin so when we print a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
                 * band we need to adjust for the different methods of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
                 * addressing it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
                int deviceAddressableX = (int)getPhysicalPrintableX(paper);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
                int deviceAddressableY = (int)getPhysicalPrintableY(paper);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
                for (int bandTop = 0; bandTop <= deviceAreaHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
                     bandTop += bandHeight)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
                    /* Put the band back into device space and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
                     * erase the contents of the band.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
                    clearGraphics.fillRect(0, 0, bandWidth, bandHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
                    /* Put the band into the correct location on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
                     * page. Once the band is moved we translate the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
                     * device transform so that the band will move down
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
                     * the page on the next iteration of the loop.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
                    bandGraphics.setTransform(uniformTransform);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
                    bandGraphics.transform(deviceTransform);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
                    deviceTransform.translate(0, -bandHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
                    /* Switch the band from device space to user,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
                     * 72 dpi, space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
                    bandGraphics.transform(scaleTransform);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
                    bandGraphics.transform(new AffineTransform(page.getMatrix()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
                    Rectangle clip = bandGraphics.getClipBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
                    clip = pgAt.createTransformedShape(clip).getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
                    if ((clip == null) || peekGraphics.hitsDrawingArea(clip) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
                        (bandWidth > 0 && bandHeight > 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
                        /* if the client has specified an imageable X or Y
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
                         * which is off than the physically addressable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
                         * area of the page, then we need to adjust for that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
                         * here so that we pass only non -ve band coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
                         * We also need to translate by the adjusted amount
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
                         * so that printing appears in the correct place.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
                        int bandX = deviceLeft - deviceAddressableX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
                        if (bandX < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
                            bandGraphics.translate(bandX/xScale,0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
                            bandX = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
                        int bandY = deviceTop + bandTop - deviceAddressableY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
                        if (bandY < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
                            bandGraphics.translate(0,bandY/yScale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
                            bandY = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
                        /* Have the app's painter image into the band
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
                         * and then send the band to the printer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
                        painterGraphics.setDelegate((Graphics2D) bandGraphics.create());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
                        painter.print(painterGraphics, origPage, pageIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
                        painterGraphics.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
                        printBand(data, bandX, bandY, bandWidth, bandHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
                clearGraphics.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
                bandGraphics.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
            debug_println("calling endPage "+pageIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
            endPage(page, painter, pageIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
        return pageResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
     * If a print job is in progress, print() has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
     * called but has not returned, then this signals
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
     * that the job should be cancelled and the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
     * chance. If there is no print job in progress then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
     * this call does nothing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
    public void cancel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
            if (performingPrinting) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
                userCancelled = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
            notify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
     * Returns true is a print job is ongoing but will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
     * be cancelled and the next opportunity. false is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
     * returned otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
    public boolean isCancelled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
        boolean cancelled = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
            cancelled = (performingPrinting && userCancelled);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
            notify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
        return cancelled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
     * Return the Pageable describing the pages to be printed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
    protected Pageable getPageable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
        return mDocument;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
     * Examine the metrics captured by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
     * <code>PeekGraphics</code> instance and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
     * if capable of directly converting this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
     * print job to the printer's control language
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
     * or the native OS's graphics primitives, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
     * return a <code>PathGraphics</code> to perform
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
     * that conversion. If there is not an object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
     * capable of the conversion then return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
     * <code>null</code>. Returning <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
     * causes the print job to be rasterized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
    protected Graphics2D createPathGraphics(PeekGraphics graphics,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
                                            PrinterJob printerJob,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
                                            Printable painter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
                                            PageFormat pageFormat,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
                                            int pageIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
     * Create and return an object that will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
     * gather and hold metrics about the print
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
     * job. This method is passed a <code>Graphics2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
     * object that can be used as a proxy for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
     * object gathering the print job matrics. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
     * method is also supplied with the instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
     * controlling the print job, <code>printerJob</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
    protected PeekGraphics createPeekGraphics(Graphics2D graphics,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
                                              PrinterJob printerJob) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
        return new PeekGraphics(graphics, printerJob);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
     * Configure the passed in Graphics2D so that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
     * is contains the defined initial settings
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
     * for a print job. These settings are:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
     *      color:  black.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
     *      clip:   <as passed in>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
     */
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
  2256
// MacOSX - made protected so subclasses can reference it.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
  2257
    protected void initPrinterGraphics(Graphics2D g, Rectangle2D clip) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
        g.setClip(clip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
        g.setPaint(Color.black);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
    * User dialogs should disable "File" buttons if this returns false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
    public boolean checkAllowedToPrintToFile() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
            throwPrintToFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
        } catch (SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
     * Break this out as it may be useful when we allow API to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
     * specify printing to a file. In that case its probably right
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
     * to throw a SecurityException if the permission is not granted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
    private void throwPrintToFile() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
            if (printToFilePermission == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
                printToFilePermission =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
                    new FilePermission("<<ALL FILES>>", "read,write");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
            security.checkPermission(printToFilePermission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
    /* On-screen drawString renders most control chars as the missing glyph
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
     * and have the non-zero advance of that glyph.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
     * Exceptions are \t, \n and \r which are considered zero-width.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
     * This is a utility method used by subclasses to remove them so we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
     * don't have to worry about platform or font specific handling of them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
    protected String removeControlChars(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
        char[] in_chars = s.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
        int len = in_chars.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
        char[] out_chars = new char[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
        int pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
        for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
            char c = in_chars[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
            if (c > '\r' || c < '\t' || c == '\u000b' || c == '\u000c')  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
               out_chars[pos++] = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
        if (pos == len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
            return s; // no need to make a new String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
            return new String(out_chars, 0, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
}