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