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