feat(jdk8): move files to new folder to avoid resources compiled.

This commit is contained in:
2025-09-07 15:25:52 +08:00
parent 3f0047bf6f
commit 8c35cfb1c0
17415 changed files with 217 additions and 213 deletions

View File

@@ -0,0 +1,221 @@
/*
* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.awt.print;
import java.util.Vector;
/**
* The <code>Book</code> class provides a representation of a document in
* which pages may have different page formats and page painters. This
* class uses the {@link Pageable} interface to interact with a
* {@link PrinterJob}.
* @see Pageable
* @see PrinterJob
*/
public class Book implements Pageable {
/* Class Constants */
/* Class Variables */
/* Instance Variables */
/**
* The set of pages that make up the Book.
*/
private Vector mPages;
/* Instance Methods */
/**
* Creates a new, empty <code>Book</code>.
*/
public Book() {
mPages = new Vector();
}
/**
* Returns the number of pages in this <code>Book</code>.
* @return the number of pages this <code>Book</code> contains.
*/
public int getNumberOfPages(){
return mPages.size();
}
/**
* Returns the {@link PageFormat} of the page specified by
* <code>pageIndex</code>.
* @param pageIndex the zero based index of the page whose
* <code>PageFormat</code> is being requested
* @return the <code>PageFormat</code> describing the size and
* orientation of the page.
* @throws IndexOutOfBoundsException if the <code>Pageable</code>
* does not contain the requested page
*/
public PageFormat getPageFormat(int pageIndex)
throws IndexOutOfBoundsException
{
return getPage(pageIndex).getPageFormat();
}
/**
* Returns the {@link Printable} instance responsible for rendering
* the page specified by <code>pageIndex</code>.
* @param pageIndex the zero based index of the page whose
* <code>Printable</code> is being requested
* @return the <code>Printable</code> that renders the page.
* @throws IndexOutOfBoundsException if the <code>Pageable</code>
* does not contain the requested page
*/
public Printable getPrintable(int pageIndex)
throws IndexOutOfBoundsException
{
return getPage(pageIndex).getPrintable();
}
/**
* Sets the <code>PageFormat</code> and the <code>Painter</code> for a
* specified page number.
* @param pageIndex the zero based index of the page whose
* painter and format is altered
* @param painter the <code>Printable</code> instance that
* renders the page
* @param page the size and orientation of the page
* @throws IndexOutOfBoundsException if the specified
* page is not already in this <code>Book</code>
* @throws NullPointerException if the <code>painter</code> or
* <code>page</code> argument is <code>null</code>
*/
public void setPage(int pageIndex, Printable painter, PageFormat page)
throws IndexOutOfBoundsException
{
if (painter == null) {
throw new NullPointerException("painter is null");
}
if (page == null) {
throw new NullPointerException("page is null");
}
mPages.setElementAt(new BookPage(painter, page), pageIndex);
}
/**
* Appends a single page to the end of this <code>Book</code>.
* @param painter the <code>Printable</code> instance that
* renders the page
* @param page the size and orientation of the page
* @throws NullPointerException
* If the <code>painter</code> or <code>page</code>
* argument is <code>null</code>
*/
public void append(Printable painter, PageFormat page) {
mPages.addElement(new BookPage(painter, page));
}
/**
* Appends <code>numPages</code> pages to the end of this
* <code>Book</code>. Each of the pages is associated with
* <code>page</code>.
* @param painter the <code>Printable</code> instance that renders
* the page
* @param page the size and orientation of the page
* @param numPages the number of pages to be added to the
* this <code>Book</code>.
* @throws NullPointerException
* If the <code>painter</code> or <code>page</code>
* argument is <code>null</code>
*/
public void append(Printable painter, PageFormat page, int numPages) {
BookPage bookPage = new BookPage(painter, page);
int pageIndex = mPages.size();
int newSize = pageIndex + numPages;
mPages.setSize(newSize);
for(int i = pageIndex; i < newSize; i++){
mPages.setElementAt(bookPage, i);
}
}
/**
* Return the BookPage for the page specified by 'pageIndex'.
*/
private BookPage getPage(int pageIndex)
throws ArrayIndexOutOfBoundsException
{
return (BookPage) mPages.elementAt(pageIndex);
}
/**
* The BookPage inner class describes an individual
* page in a Book through a PageFormat-Printable pair.
*/
private class BookPage {
/**
* The size and orientation of the page.
*/
private PageFormat mFormat;
/**
* The instance that will draw the page.
*/
private Printable mPainter;
/**
* A new instance where 'format' describes the page's
* size and orientation and 'painter' is the instance
* that will draw the page's graphics.
* @throws NullPointerException
* If the <code>painter</code> or <code>format</code>
* argument is <code>null</code>
*/
BookPage(Printable painter, PageFormat format) {
if (painter == null || format == null) {
throw new NullPointerException();
}
mFormat = format;
mPainter = painter;
}
/**
* Return the instance that paints the
* page.
*/
Printable getPrintable() {
return mPainter;
}
/**
* Return the format of the page.
*/
PageFormat getPageFormat() {
return mFormat;
}
}
}

View File

@@ -0,0 +1,365 @@
/*
* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.awt.print;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.lang.annotation.Native;
/**
* The <code>PageFormat</code> class describes the size and
* orientation of a page to be printed.
*/
public class PageFormat implements Cloneable
{
/* Class Constants */
/**
* The origin is at the bottom left of the paper with
* x running bottom to top and y running left to right.
* Note that this is not the Macintosh landscape but
* is the Window's and PostScript landscape.
*/
@Native public static final int LANDSCAPE = 0;
/**
* The origin is at the top left of the paper with
* x running to the right and y running down the
* paper.
*/
@Native public static final int PORTRAIT = 1;
/**
* The origin is at the top right of the paper with x
* running top to bottom and y running right to left.
* Note that this is the Macintosh landscape.
*/
@Native public static final int REVERSE_LANDSCAPE = 2;
/* Instance Variables */
/**
* A description of the physical piece of paper.
*/
private Paper mPaper;
/**
* The orientation of the current page. This will be
* one of the constants: PORTRIAT, LANDSCAPE, or
* REVERSE_LANDSCAPE,
*/
private int mOrientation = PORTRAIT;
/* Constructors */
/**
* Creates a default, portrait-oriented
* <code>PageFormat</code>.
*/
public PageFormat()
{
mPaper = new Paper();
}
/* Instance Methods */
/**
* Makes a copy of this <code>PageFormat</code> with the same
* contents as this <code>PageFormat</code>.
* @return a copy of this <code>PageFormat</code>.
*/
public Object clone() {
PageFormat newPage;
try {
newPage = (PageFormat) super.clone();
newPage.mPaper = (Paper)mPaper.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
newPage = null; // should never happen.
}
return newPage;
}
/**
* Returns the width, in 1/72nds of an inch, of the page.
* This method takes into account the orientation of the
* page when determining the width.
* @return the width of the page.
*/
public double getWidth() {
double width;
int orientation = getOrientation();
if (orientation == PORTRAIT) {
width = mPaper.getWidth();
} else {
width = mPaper.getHeight();
}
return width;
}
/**
* Returns the height, in 1/72nds of an inch, of the page.
* This method takes into account the orientation of the
* page when determining the height.
* @return the height of the page.
*/
public double getHeight() {
double height;
int orientation = getOrientation();
if (orientation == PORTRAIT) {
height = mPaper.getHeight();
} else {
height = mPaper.getWidth();
}
return height;
}
/**
* Returns the x coordinate of the upper left point of the
* imageable area of the <code>Paper</code> object
* associated with this <code>PageFormat</code>.
* This method takes into account the
* orientation of the page.
* @return the x coordinate of the upper left point of the
* imageable area of the <code>Paper</code> object
* associated with this <code>PageFormat</code>.
*/
public double getImageableX() {
double x;
switch (getOrientation()) {
case LANDSCAPE:
x = mPaper.getHeight()
- (mPaper.getImageableY() + mPaper.getImageableHeight());
break;
case PORTRAIT:
x = mPaper.getImageableX();
break;
case REVERSE_LANDSCAPE:
x = mPaper.getImageableY();
break;
default:
/* This should never happen since it signifies that the
* PageFormat is in an invalid orientation.
*/
throw new InternalError("unrecognized orientation");
}
return x;
}
/**
* Returns the y coordinate of the upper left point of the
* imageable area of the <code>Paper</code> object
* associated with this <code>PageFormat</code>.
* This method takes into account the
* orientation of the page.
* @return the y coordinate of the upper left point of the
* imageable area of the <code>Paper</code> object
* associated with this <code>PageFormat</code>.
*/
public double getImageableY() {
double y;
switch (getOrientation()) {
case LANDSCAPE:
y = mPaper.getImageableX();
break;
case PORTRAIT:
y = mPaper.getImageableY();
break;
case REVERSE_LANDSCAPE:
y = mPaper.getWidth()
- (mPaper.getImageableX() + mPaper.getImageableWidth());
break;
default:
/* This should never happen since it signifies that the
* PageFormat is in an invalid orientation.
*/
throw new InternalError("unrecognized orientation");
}
return y;
}
/**
* Returns the width, in 1/72nds of an inch, of the imageable
* area of the page. This method takes into account the orientation
* of the page.
* @return the width of the page.
*/
public double getImageableWidth() {
double width;
if (getOrientation() == PORTRAIT) {
width = mPaper.getImageableWidth();
} else {
width = mPaper.getImageableHeight();
}
return width;
}
/**
* Return the height, in 1/72nds of an inch, of the imageable
* area of the page. This method takes into account the orientation
* of the page.
* @return the height of the page.
*/
public double getImageableHeight() {
double height;
if (getOrientation() == PORTRAIT) {
height = mPaper.getImageableHeight();
} else {
height = mPaper.getImageableWidth();
}
return height;
}
/**
* Returns a copy of the {@link Paper} object associated
* with this <code>PageFormat</code>. Changes made to the
* <code>Paper</code> object returned from this method do not
* affect the <code>Paper</code> object of this
* <code>PageFormat</code>. To update the <code>Paper</code>
* object of this <code>PageFormat</code>, create a new
* <code>Paper</code> object and set it into this
* <code>PageFormat</code> by using the {@link #setPaper(Paper)}
* method.
* @return a copy of the <code>Paper</code> object associated
* with this <code>PageFormat</code>.
* @see #setPaper
*/
public Paper getPaper() {
return (Paper)mPaper.clone();
}
/**
* Sets the <code>Paper</code> object for this
* <code>PageFormat</code>.
* @param paper the <code>Paper</code> object to which to set
* the <code>Paper</code> object for this <code>PageFormat</code>.
* @exception NullPointerException
* a null paper instance was passed as a parameter.
* @see #getPaper
*/
public void setPaper(Paper paper) {
mPaper = (Paper)paper.clone();
}
/**
* Sets the page orientation. <code>orientation</code> must be
* one of the constants: PORTRAIT, LANDSCAPE,
* or REVERSE_LANDSCAPE.
* @param orientation the new orientation for the page
* @throws IllegalArgumentException if
* an unknown orientation was requested
* @see #getOrientation
*/
public void setOrientation(int orientation) throws IllegalArgumentException
{
if (0 <= orientation && orientation <= REVERSE_LANDSCAPE) {
mOrientation = orientation;
} else {
throw new IllegalArgumentException();
}
}
/**
* Returns the orientation of this <code>PageFormat</code>.
* @return this <code>PageFormat</code> object's orientation.
* @see #setOrientation
*/
public int getOrientation() {
return mOrientation;
}
/**
* Returns a transformation matrix that translates user
* space rendering to the requested orientation
* of the page. The values are placed into the
* array as
* {&nbsp;m00,&nbsp;m10,&nbsp;m01,&nbsp;m11,&nbsp;m02,&nbsp;m12} in
* the form required by the {@link AffineTransform}
* constructor.
* @return the matrix used to translate user space rendering
* to the orientation of the page.
* @see java.awt.geom.AffineTransform
*/
public double[] getMatrix() {
double[] matrix = new double[6];
switch (mOrientation) {
case LANDSCAPE:
matrix[0] = 0; matrix[1] = -1;
matrix[2] = 1; matrix[3] = 0;
matrix[4] = 0; matrix[5] = mPaper.getHeight();
break;
case PORTRAIT:
matrix[0] = 1; matrix[1] = 0;
matrix[2] = 0; matrix[3] = 1;
matrix[4] = 0; matrix[5] = 0;
break;
case REVERSE_LANDSCAPE:
matrix[0] = 0; matrix[1] = 1;
matrix[2] = -1; matrix[3] = 0;
matrix[4] = mPaper.getWidth(); matrix[5] = 0;
break;
default:
throw new IllegalArgumentException();
}
return matrix;
}
}

View File

@@ -0,0 +1,85 @@
/*
* Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.awt.print;
import java.lang.annotation.Native;
/**
* The <code>Pageable</code> implementation represents a set of
* pages to be printed. The <code>Pageable</code> object returns
* the total number of pages in the set as well as the
* {@link PageFormat} and {@link Printable} for a specified page.
* @see java.awt.print.PageFormat
* @see java.awt.print.Printable
*/
public interface Pageable {
/**
* This constant is returned from the
* {@link #getNumberOfPages() getNumberOfPages}
* method if a <code>Pageable</code> implementation does not know
* the number of pages in its set.
*/
@Native int UNKNOWN_NUMBER_OF_PAGES = -1;
/**
* Returns the number of pages in the set.
* To enable advanced printing features,
* it is recommended that <code>Pageable</code>
* implementations return the true number of pages
* rather than the
* UNKNOWN_NUMBER_OF_PAGES constant.
* @return the number of pages in this <code>Pageable</code>.
*/
int getNumberOfPages();
/**
* Returns the <code>PageFormat</code> of the page specified by
* <code>pageIndex</code>.
* @param pageIndex the zero based index of the page whose
* <code>PageFormat</code> is being requested
* @return the <code>PageFormat</code> describing the size and
* orientation.
* @throws IndexOutOfBoundsException if
* the <code>Pageable</code> does not contain the requested
* page.
*/
PageFormat getPageFormat(int pageIndex)
throws IndexOutOfBoundsException;
/**
* Returns the <code>Printable</code> instance responsible for
* rendering the page specified by <code>pageIndex</code>.
* @param pageIndex the zero based index of the page whose
* <code>Printable</code> is being requested
* @return the <code>Printable</code> that renders the page.
* @throws IndexOutOfBoundsException if
* the <code>Pageable</code> does not contain the requested
* page.
*/
Printable getPrintable(int pageIndex)
throws IndexOutOfBoundsException;
}

View File

@@ -0,0 +1,209 @@
/*
* Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.awt.print;
import java.awt.geom.Rectangle2D;
/**
* The <code>Paper</code> class describes the physical characteristics of
* a piece of paper.
* <p>
* When creating a <code>Paper</code> object, it is the application's
* responsibility to ensure that the paper size and the imageable area
* are compatible. For example, if the paper size is changed from
* 11 x 17 to 8.5 x 11, the application might need to reduce the
* imageable area so that whatever is printed fits on the page.
* <p>
* @see #setSize(double, double)
* @see #setImageableArea(double, double, double, double)
*/
public class Paper implements Cloneable {
/* Private Class Variables */
private static final int INCH = 72;
private static final double LETTER_WIDTH = 8.5 * INCH;
private static final double LETTER_HEIGHT = 11 * INCH;
/* Instance Variables */
/**
* The height of the physical page in 1/72nds
* of an inch. The number is stored as a floating
* point value rather than as an integer
* to facilitate the conversion from metric
* units to 1/72nds of an inch and then back.
* (This may or may not be a good enough reason
* for a float).
*/
private double mHeight;
/**
* The width of the physical page in 1/72nds
* of an inch.
*/
private double mWidth;
/**
* The area of the page on which drawing will
* be visable. The area outside of this
* rectangle but on the Page generally
* reflects the printer's hardware margins.
* The origin of the physical page is
* at (0, 0) with this rectangle provided
* in that coordinate system.
*/
private Rectangle2D mImageableArea;
/* Constructors */
/**
* Creates a letter sized piece of paper
* with one inch margins.
*/
public Paper() {
mHeight = LETTER_HEIGHT;
mWidth = LETTER_WIDTH;
mImageableArea = new Rectangle2D.Double(INCH, INCH,
mWidth - 2 * INCH,
mHeight - 2 * INCH);
}
/* Instance Methods */
/**
* Creates a copy of this <code>Paper</code> with the same contents
* as this <code>Paper</code>.
* @return a copy of this <code>Paper</code>.
*/
public Object clone() {
Paper newPaper;
try {
/* It's okay to copy the reference to the imageable
* area into the clone since we always return a copy
* of the imageable area when asked for it.
*/
newPaper = (Paper) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
newPaper = null; // should never happen.
}
return newPaper;
}
/**
* Returns the height of the page in 1/72nds of an inch.
* @return the height of the page described by this
* <code>Paper</code>.
*/
public double getHeight() {
return mHeight;
}
/**
* Sets the width and height of this <code>Paper</code>
* object, which represents the properties of the page onto
* which printing occurs.
* The dimensions are supplied in 1/72nds of
* an inch.
* @param width the value to which to set this <code>Paper</code>
* object's width
* @param height the value to which to set this <code>Paper</code>
* object's height
*/
public void setSize(double width, double height) {
mWidth = width;
mHeight = height;
}
/**
* Returns the width of the page in 1/72nds
* of an inch.
* @return the width of the page described by this
* <code>Paper</code>.
*/
public double getWidth() {
return mWidth;
}
/**
* Sets the imageable area of this <code>Paper</code>. The
* imageable area is the area on the page in which printing
* occurs.
* @param x the X coordinate to which to set the
* upper-left corner of the imageable area of this <code>Paper</code>
* @param y the Y coordinate to which to set the
* upper-left corner of the imageable area of this <code>Paper</code>
* @param width the value to which to set the width of the
* imageable area of this <code>Paper</code>
* @param height the value to which to set the height of the
* imageable area of this <code>Paper</code>
*/
public void setImageableArea(double x, double y,
double width, double height) {
mImageableArea = new Rectangle2D.Double(x, y, width,height);
}
/**
* Returns the x coordinate of the upper-left corner of this
* <code>Paper</code> object's imageable area.
* @return the x coordinate of the imageable area.
*/
public double getImageableX() {
return mImageableArea.getX();
}
/**
* Returns the y coordinate of the upper-left corner of this
* <code>Paper</code> object's imageable area.
* @return the y coordinate of the imageable area.
*/
public double getImageableY() {
return mImageableArea.getY();
}
/**
* Returns the width of this <code>Paper</code> object's imageable
* area.
* @return the width of the imageable area.
*/
public double getImageableWidth() {
return mImageableArea.getWidth();
}
/**
* Returns the height of this <code>Paper</code> object's imageable
* area.
* @return the height of the imageable area.
*/
public double getImageableHeight() {
return mImageableArea.getHeight();
}
}

View File

@@ -0,0 +1,144 @@
/*
* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.awt.print;
import java.awt.Graphics;
/**
* The <code>Printable</code> interface is implemented
* by the <code>print</code> methods of the current
* page painter, which is called by the printing
* system to render a page. When building a
* {@link Pageable}, pairs of {@link PageFormat}
* instances and instances that implement
* this interface are used to describe each page. The
* instance implementing <code>Printable</code> is called to
* print the page's graphics.
* <p>
* A <code>Printable(..)</code> may be set on a <code>PrinterJob</code>.
* When the client subsequently initiates printing by calling
* <code>PrinterJob.print(..)</code> control
* <p>
* is handed to the printing system until all pages have been printed.
* It does this by calling <code>Printable.print(..)</code> until
* all pages in the document have been printed.
* In using the <code>Printable</code> interface the printing
* commits to image the contents of a page whenever
* requested by the printing system.
* <p>
* The parameters to <code>Printable.print(..)</code> include a
* <code>PageFormat</code> which describes the printable area of
* the page, needed for calculating the contents that will fit the
* page, and the page index, which specifies the zero-based print
* stream index of the requested page.
* <p>
* For correct printing behaviour, the following points should be
* observed:
* <ul>
* <li> The printing system may request a page index more than once.
* On each occasion equal PageFormat parameters will be supplied.
*
* <li>The printing system will call <code>Printable.print(..)</code>
* with page indexes which increase monotonically, although as noted above,
* the <code>Printable</code> should expect multiple calls for a page index
* and that page indexes may be skipped, when page ranges are specified
* by the client, or by a user through a print dialog.
*
* <li>If multiple collated copies of a document are requested, and the
* printer cannot natively support this, then the document may be imaged
* multiple times. Printing will start each copy from the lowest print
* stream page index page.
*
* <li>With the exception of re-imaging an entire document for multiple
* collated copies, the increasing page index order means that when
* page N is requested if a client needs to calculate page break position,
* it may safely discard any state related to pages &lt; N, and make current
* that for page N. "State" usually is just the calculated position in the
* document that corresponds to the start of the page.
*
* <li>When called by the printing system the <code>Printable</code> must
* inspect and honour the supplied PageFormat parameter as well as the
* page index. The format of the page to be drawn is specified by the
* supplied PageFormat. The size, orientation and imageable area of the page
* is therefore already determined and rendering must be within this
* imageable area.
* This is key to correct printing behaviour, and it has the
* implication that the client has the responsibility of tracking
* what content belongs on the specified page.
*
* <li>When the <code>Printable</code> is obtained from a client-supplied
* <code>Pageable</code> then the client may provide different PageFormats
* for each page index. Calculations of page breaks must account for this.
* </ul>
* <p>
* @see java.awt.print.Pageable
* @see java.awt.print.PageFormat
* @see java.awt.print.PrinterJob
*/
public interface Printable {
/**
* Returned from {@link #print(Graphics, PageFormat, int)}
* to signify that the requested page was rendered.
*/
int PAGE_EXISTS = 0;
/**
* Returned from <code>print</code> to signify that the
* <code>pageIndex</code> is too large and that the requested page
* does not exist.
*/
int NO_SUCH_PAGE = 1;
/**
* Prints the page at the specified index into the specified
* {@link Graphics} context in the specified
* format. A <code>PrinterJob</code> calls the
* <code>Printable</code> interface to request that a page be
* rendered into the context specified by
* <code>graphics</code>. The format of the page to be drawn is
* specified by <code>pageFormat</code>. The zero based index
* of the requested page is specified by <code>pageIndex</code>.
* If the requested page does not exist then this method returns
* NO_SUCH_PAGE; otherwise PAGE_EXISTS is returned.
* The <code>Graphics</code> class or subclass implements the
* {@link PrinterGraphics} interface to provide additional
* information. If the <code>Printable</code> object
* aborts the print job then it throws a {@link PrinterException}.
* @param graphics the context into which the page is drawn
* @param pageFormat the size and orientation of the page being drawn
* @param pageIndex the zero based index of the page to be drawn
* @return PAGE_EXISTS if the page is rendered successfully
* or NO_SUCH_PAGE if <code>pageIndex</code> specifies a
* non-existent page.
* @exception java.awt.print.PrinterException
* thrown when the print job is terminated.
*/
int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
throws PrinterException;
}

View File

@@ -0,0 +1,55 @@
/*
* Copyright (c) 1998, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.awt.print;
/**
* The <code>PrinterAbortException</code> class is a subclass of
* {@link PrinterException} and is used to indicate that a user
* or application has terminated the print job while it was in
* the process of printing.
*/
public class PrinterAbortException extends PrinterException {
/**
* Constructs a new <code>PrinterAbortException</code> with no
* detail message.
*/
public PrinterAbortException() {
super();
}
/**
* Constructs a new <code>PrinterAbortException</code> with
* the specified detail message.
* @param msg the message to be generated when a
* <code>PrinterAbortException</code> is thrown
*/
public PrinterAbortException(String msg) {
super(msg);
}
}

View File

@@ -0,0 +1,53 @@
/*
* Copyright (c) 1998, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.awt.print;
/**
* The <code>PrinterException</code> class and its subclasses are used
* to indicate that an exceptional condition has occurred in the print
* system.
*/
public class PrinterException extends Exception {
/**
* Constructs a new <code>PrinterException</code> object
* without a detail message.
*/
public PrinterException() {
}
/**
* Constructs a new <code>PrinterException</code> object
* with the specified detail message.
* @param msg the message to generate when a
* <code>PrinterException</code> is thrown
*/
public PrinterException(String msg) {
super(msg);
}
}

View File

@@ -0,0 +1,47 @@
/*
* Copyright (c) 1998, 1999, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.awt.print;
/**
* The <code>PrinterGraphics</code> interface is implemented by
* {@link java.awt.Graphics} objects that are passed to
* {@link Printable} objects to render a page. It allows an
* application to find the {@link PrinterJob} object that is
* controlling the printing.
*/
public interface PrinterGraphics {
/**
* Returns the <code>PrinterJob</code> that is controlling the
* current rendering request.
* @return the <code>PrinterJob</code> controlling the current
* rendering request.
* @see java.awt.print.Printable
*/
PrinterJob getPrinterJob();
}

View File

@@ -0,0 +1,88 @@
/*
* Copyright (c) 1998, 2000, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.awt.print;
import java.io.IOException;
/**
* The <code>PrinterIOException</code> class is a subclass of
* {@link PrinterException} and is used to indicate that an IO error
* of some sort has occurred while printing.
*
* <p>As of release 1.4, this exception has been retrofitted to conform to
* the general purpose exception-chaining mechanism. The
* "<code>IOException</code> that terminated the print job"
* that is provided at construction time and accessed via the
* {@link #getIOException()} method is now known as the <i>cause</i>,
* and may be accessed via the {@link Throwable#getCause()} method,
* as well as the aforementioned "legacy method."
*/
public class PrinterIOException extends PrinterException {
static final long serialVersionUID = 5850870712125932846L;
/**
* The IO error that terminated the print job.
* @serial
*/
private IOException mException;
/**
* Constructs a new <code>PrinterIOException</code>
* with the string representation of the specified
* {@link IOException}.
* @param exception the specified <code>IOException</code>
*/
public PrinterIOException(IOException exception) {
initCause(null); // Disallow subsequent initCause
mException = exception;
}
/**
* Returns the <code>IOException</code> that terminated
* the print job.
*
* <p>This method predates the general-purpose exception chaining facility.
* The {@link Throwable#getCause()} method is now the preferred means of
* obtaining this information.
*
* @return the <code>IOException</code> that terminated
* the print job.
* @see IOException
*/
public IOException getIOException() {
return mException;
}
/**
* Returns the the cause of this exception (the <code>IOException</code>
* that terminated the print job).
*
* @return the cause of this exception.
* @since 1.4
*/
public Throwable getCause() {
return mException;
}
}

View File

@@ -0,0 +1,619 @@
/*
* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.awt.print;
import java.awt.AWTError;
import java.awt.HeadlessException;
import java.util.Enumeration;
import javax.print.DocFlavor;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.StreamPrintServiceFactory;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Media;
import javax.print.attribute.standard.MediaPrintableArea;
import javax.print.attribute.standard.MediaSize;
import javax.print.attribute.standard.MediaSizeName;
import javax.print.attribute.standard.OrientationRequested;
import sun.security.action.GetPropertyAction;
/**
* The <code>PrinterJob</code> class is the principal class that controls
* printing. An application calls methods in this class to set up a job,
* optionally to invoke a print dialog with the user, and then to print
* the pages of the job.
*/
public abstract class PrinterJob {
/* Public Class Methods */
/**
* Creates and returns a <code>PrinterJob</code> which is initially
* associated with the default printer.
* If no printers are available on the system, a PrinterJob will still
* be returned from this method, but <code>getPrintService()</code>
* will return <code>null</code>, and calling
* {@link #print() print} with this <code>PrinterJob</code> might
* generate an exception. Applications that need to determine if
* there are suitable printers before creating a <code>PrinterJob</code>
* should ensure that the array returned from
* {@link #lookupPrintServices() lookupPrintServices} is not empty.
* @return a new <code>PrinterJob</code>.
*
* @throws SecurityException if a security manager exists and its
* {@link java.lang.SecurityManager#checkPrintJobAccess}
* method disallows this thread from creating a print job request
*/
public static PrinterJob getPrinterJob() {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkPrintJobAccess();
}
return (PrinterJob) java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction() {
public Object run() {
String nm = System.getProperty("java.awt.printerjob", null);
try {
return (PrinterJob)Class.forName(nm).newInstance();
} catch (ClassNotFoundException e) {
throw new AWTError("PrinterJob not found: " + nm);
} catch (InstantiationException e) {
throw new AWTError("Could not instantiate PrinterJob: " + nm);
} catch (IllegalAccessException e) {
throw new AWTError("Could not access PrinterJob: " + nm);
}
}
});
}
/**
* A convenience method which looks up 2D print services.
* Services returned from this method may be installed on
* <code>PrinterJob</code>s which support print services.
* Calling this method is equivalent to calling
* {@link javax.print.PrintServiceLookup#lookupPrintServices(
* DocFlavor, AttributeSet)
* PrintServiceLookup.lookupPrintServices()}
* and specifying a Pageable DocFlavor.
* @return a possibly empty array of 2D print services.
* @since 1.4
*/
public static PrintService[] lookupPrintServices() {
return PrintServiceLookup.
lookupPrintServices(DocFlavor.SERVICE_FORMATTED.PAGEABLE, null);
}
/**
* A convenience method which locates factories for stream print
* services which can image 2D graphics.
* Sample usage :
* <pre>{@code
* FileOutputStream outstream;
* StreamPrintService psPrinter;
* String psMimeType = "application/postscript";
* PrinterJob pj = PrinterJob.getPrinterJob();
*
* StreamPrintServiceFactory[] factories =
* PrinterJob.lookupStreamPrintServices(psMimeType);
* if (factories.length > 0) {
* try {
* outstream = new File("out.ps");
* psPrinter = factories[0].getPrintService(outstream);
* // psPrinter can now be set as the service on a PrinterJob
* pj.setPrintService(psPrinter)
* } catch (Exception e) {
* e.printStackTrace();
* }
* }
* }</pre>
* Services returned from this method may be installed on
* <code>PrinterJob</code> instances which support print services.
* Calling this method is equivalent to calling
* {@link javax.print.StreamPrintServiceFactory#lookupStreamPrintServiceFactories(DocFlavor, String)
* StreamPrintServiceFactory.lookupStreamPrintServiceFactories()
* } and specifying a Pageable DocFlavor.
*
* @param mimeType the required output format, or null to mean any format.
* @return a possibly empty array of 2D stream print service factories.
* @since 1.4
*/
public static StreamPrintServiceFactory[]
lookupStreamPrintServices(String mimeType) {
return StreamPrintServiceFactory.lookupStreamPrintServiceFactories(
DocFlavor.SERVICE_FORMATTED.PAGEABLE,
mimeType);
}
/* Public Methods */
/**
* A <code>PrinterJob</code> object should be created using the
* static {@link #getPrinterJob() getPrinterJob} method.
*/
public PrinterJob() {
}
/**
* Returns the service (printer) for this printer job.
* Implementations of this class which do not support print services
* may return null. null will also be returned if no printers are
* available.
* @return the service for this printer job.
* @see #setPrintService(PrintService)
* @see #getPrinterJob()
* @since 1.4
*/
public PrintService getPrintService() {
return null;
}
/**
* Associate this PrinterJob with a new PrintService.
* This method is overridden by subclasses which support
* specifying a Print Service.
*
* Throws <code>PrinterException</code> if the specified service
* cannot support the <code>Pageable</code> and
* <code>Printable</code> interfaces necessary to support 2D printing.
* @param service a print service that supports 2D printing
* @exception PrinterException if the specified service does not support
* 2D printing, or this PrinterJob class does not support
* setting a 2D print service, or the specified service is
* otherwise not a valid print service.
* @see #getPrintService
* @since 1.4
*/
public void setPrintService(PrintService service)
throws PrinterException {
throw new PrinterException(
"Setting a service is not supported on this class");
}
/**
* Calls <code>painter</code> to render the pages. The pages in the
* document to be printed by this
* <code>PrinterJob</code> are rendered by the {@link Printable}
* object, <code>painter</code>. The {@link PageFormat} for each page
* is the default page format.
* @param painter the <code>Printable</code> that renders each page of
* the document.
*/
public abstract void setPrintable(Printable painter);
/**
* Calls <code>painter</code> to render the pages in the specified
* <code>format</code>. The pages in the document to be printed by
* this <code>PrinterJob</code> are rendered by the
* <code>Printable</code> object, <code>painter</code>. The
* <code>PageFormat</code> of each page is <code>format</code>.
* @param painter the <code>Printable</code> called to render
* each page of the document
* @param format the size and orientation of each page to
* be printed
*/
public abstract void setPrintable(Printable painter, PageFormat format);
/**
* Queries <code>document</code> for the number of pages and
* the <code>PageFormat</code> and <code>Printable</code> for each
* page held in the <code>Pageable</code> instance,
* <code>document</code>.
* @param document the pages to be printed. It can not be
* <code>null</code>.
* @exception NullPointerException the <code>Pageable</code> passed in
* was <code>null</code>.
* @see PageFormat
* @see Printable
*/
public abstract void setPageable(Pageable document)
throws NullPointerException;
/**
* Presents a dialog to the user for changing the properties of
* the print job.
* This method will display a native dialog if a native print
* service is selected, and user choice of printers will be restricted
* to these native print services.
* To present the cross platform print dialog for all services,
* including native ones instead use
* <code>printDialog(PrintRequestAttributeSet)</code>.
* <p>
* PrinterJob implementations which can use PrintService's will update
* the PrintService for this PrinterJob to reflect the new service
* selected by the user.
* @return <code>true</code> if the user does not cancel the dialog;
* <code>false</code> otherwise.
* @exception HeadlessException if GraphicsEnvironment.isHeadless()
* returns true.
* @see java.awt.GraphicsEnvironment#isHeadless
*/
public abstract boolean printDialog() throws HeadlessException;
/**
* A convenience method which displays a cross-platform print dialog
* for all services which are capable of printing 2D graphics using the
* <code>Pageable</code> interface. The selected printer when the
* dialog is initially displayed will reflect the print service currently
* attached to this print job.
* If the user changes the print service, the PrinterJob will be
* updated to reflect this, unless the user cancels the dialog.
* As well as allowing the user to select the destination printer,
* the user can also select values of various print request attributes.
* <p>
* The attributes parameter on input will reflect the applications
* required initial selections in the user dialog. Attributes not
* specified display using the default for the service. On return it
* will reflect the user's choices. Selections may be updated by
* the implementation to be consistent with the supported values
* for the currently selected print service.
* <p>
* As the user scrolls to a new print service selection, the values
* copied are based on the settings for the previous service, together
* with any user changes. The values are not based on the original
* settings supplied by the client.
* <p>
* With the exception of selected printer, the PrinterJob state is
* not updated to reflect the user's changes.
* For the selections to affect a printer job, the attributes must
* be specified in the call to the
* <code>print(PrintRequestAttributeSet)</code> method. If using
* the Pageable interface, clients which intend to use media selected
* by the user must create a PageFormat derived from the user's
* selections.
* If the user cancels the dialog, the attributes will not reflect
* any changes made by the user.
* @param attributes on input is application supplied attributes,
* on output the contents are updated to reflect user choices.
* This parameter may not be null.
* @return <code>true</code> if the user does not cancel the dialog;
* <code>false</code> otherwise.
* @exception HeadlessException if GraphicsEnvironment.isHeadless()
* returns true.
* @exception NullPointerException if <code>attributes</code> parameter
* is null.
* @see java.awt.GraphicsEnvironment#isHeadless
* @since 1.4
*
*/
public boolean printDialog(PrintRequestAttributeSet attributes)
throws HeadlessException {
if (attributes == null) {
throw new NullPointerException("attributes");
}
return printDialog();
}
/**
* Displays a dialog that allows modification of a
* <code>PageFormat</code> instance.
* The <code>page</code> argument is used to initialize controls
* in the page setup dialog.
* If the user cancels the dialog then this method returns the
* original <code>page</code> object unmodified.
* If the user okays the dialog then this method returns a new
* <code>PageFormat</code> object with the indicated changes.
* In either case, the original <code>page</code> object is
* not modified.
* @param page the default <code>PageFormat</code> presented to the
* user for modification
* @return the original <code>page</code> object if the dialog
* is cancelled; a new <code>PageFormat</code> object
* containing the format indicated by the user if the
* dialog is acknowledged.
* @exception HeadlessException if GraphicsEnvironment.isHeadless()
* returns true.
* @see java.awt.GraphicsEnvironment#isHeadless
* @since 1.2
*/
public abstract PageFormat pageDialog(PageFormat page)
throws HeadlessException;
/**
* A convenience method which displays a cross-platform page setup dialog.
* The choices available will reflect the print service currently
* set on this PrinterJob.
* <p>
* The attributes parameter on input will reflect the client's
* required initial selections in the user dialog. Attributes which are
* not specified display using the default for the service. On return it
* will reflect the user's choices. Selections may be updated by
* the implementation to be consistent with the supported values
* for the currently selected print service.
* <p>
* The return value will be a PageFormat equivalent to the
* selections in the PrintRequestAttributeSet.
* If the user cancels the dialog, the attributes will not reflect
* any changes made by the user, and the return value will be null.
* @param attributes on input is application supplied attributes,
* on output the contents are updated to reflect user choices.
* This parameter may not be null.
* @return a page format if the user does not cancel the dialog;
* <code>null</code> otherwise.
* @exception HeadlessException if GraphicsEnvironment.isHeadless()
* returns true.
* @exception NullPointerException if <code>attributes</code> parameter
* is null.
* @see java.awt.GraphicsEnvironment#isHeadless
* @since 1.4
*
*/
public PageFormat pageDialog(PrintRequestAttributeSet attributes)
throws HeadlessException {
if (attributes == null) {
throw new NullPointerException("attributes");
}
return pageDialog(defaultPage());
}
/**
* Clones the <code>PageFormat</code> argument and alters the
* clone to describe a default page size and orientation.
* @param page the <code>PageFormat</code> to be cloned and altered
* @return clone of <code>page</code>, altered to describe a default
* <code>PageFormat</code>.
*/
public abstract PageFormat defaultPage(PageFormat page);
/**
* Creates a new <code>PageFormat</code> instance and
* sets it to a default size and orientation.
* @return a <code>PageFormat</code> set to a default size and
* orientation.
*/
public PageFormat defaultPage() {
return defaultPage(new PageFormat());
}
/**
* Calculates a <code>PageFormat</code> with values consistent with those
* supported by the current <code>PrintService</code> for this job
* (ie the value returned by <code>getPrintService()</code>) and media,
* printable area and orientation contained in <code>attributes</code>.
* <p>
* Calling this method does not update the job.
* It is useful for clients that have a set of attributes obtained from
* <code>printDialog(PrintRequestAttributeSet attributes)</code>
* and need a PageFormat to print a Pageable object.
* @param attributes a set of printing attributes, for example obtained
* from calling printDialog. If <code>attributes</code> is null a default
* PageFormat is returned.
* @return a <code>PageFormat</code> whose settings conform with
* those of the current service and the specified attributes.
* @since 1.6
*/
public PageFormat getPageFormat(PrintRequestAttributeSet attributes) {
PrintService service = getPrintService();
PageFormat pf = defaultPage();
if (service == null || attributes == null) {
return pf;
}
Media media = (Media)attributes.get(Media.class);
MediaPrintableArea mpa =
(MediaPrintableArea)attributes.get(MediaPrintableArea.class);
OrientationRequested orientReq =
(OrientationRequested)attributes.get(OrientationRequested.class);
if (media == null && mpa == null && orientReq == null) {
return pf;
}
Paper paper = pf.getPaper();
/* If there's a media but no media printable area, we can try
* to retrieve the default value for mpa and use that.
*/
if (mpa == null && media != null &&
service.isAttributeCategorySupported(MediaPrintableArea.class)) {
Object mpaVals =
service.getSupportedAttributeValues(MediaPrintableArea.class,
null, attributes);
if (mpaVals instanceof MediaPrintableArea[] &&
((MediaPrintableArea[])mpaVals).length > 0) {
mpa = ((MediaPrintableArea[])mpaVals)[0];
}
}
if (media != null &&
service.isAttributeValueSupported(media, null, attributes)) {
if (media instanceof MediaSizeName) {
MediaSizeName msn = (MediaSizeName)media;
MediaSize msz = MediaSize.getMediaSizeForName(msn);
if (msz != null) {
double inch = 72.0;
double paperWid = msz.getX(MediaSize.INCH) * inch;
double paperHgt = msz.getY(MediaSize.INCH) * inch;
paper.setSize(paperWid, paperHgt);
if (mpa == null) {
paper.setImageableArea(inch, inch,
paperWid-2*inch,
paperHgt-2*inch);
}
}
}
}
if (mpa != null &&
service.isAttributeValueSupported(mpa, null, attributes)) {
float [] printableArea =
mpa.getPrintableArea(MediaPrintableArea.INCH);
for (int i=0; i < printableArea.length; i++) {
printableArea[i] = printableArea[i]*72.0f;
}
paper.setImageableArea(printableArea[0], printableArea[1],
printableArea[2], printableArea[3]);
}
if (orientReq != null &&
service.isAttributeValueSupported(orientReq, null, attributes)) {
int orient;
if (orientReq.equals(OrientationRequested.REVERSE_LANDSCAPE)) {
orient = PageFormat.REVERSE_LANDSCAPE;
} else if (orientReq.equals(OrientationRequested.LANDSCAPE)) {
orient = PageFormat.LANDSCAPE;
} else {
orient = PageFormat.PORTRAIT;
}
pf.setOrientation(orient);
}
pf.setPaper(paper);
pf = validatePage(pf);
return pf;
}
/**
* Returns the clone of <code>page</code> with its settings
* adjusted to be compatible with the current printer of this
* <code>PrinterJob</code>. For example, the returned
* <code>PageFormat</code> could have its imageable area
* adjusted to fit within the physical area of the paper that
* is used by the current printer.
* @param page the <code>PageFormat</code> that is cloned and
* whose settings are changed to be compatible with
* the current printer
* @return a <code>PageFormat</code> that is cloned from
* <code>page</code> and whose settings are changed
* to conform with this <code>PrinterJob</code>.
*/
public abstract PageFormat validatePage(PageFormat page);
/**
* Prints a set of pages.
* @exception PrinterException an error in the print system
* caused the job to be aborted.
* @see Book
* @see Pageable
* @see Printable
*/
public abstract void print() throws PrinterException;
/**
* Prints a set of pages using the settings in the attribute
* set. The default implementation ignores the attribute set.
* <p>
* Note that some attributes may be set directly on the PrinterJob
* by equivalent method calls, (for example), copies:
* <code>setcopies(int)</code>, job name: <code>setJobName(String)</code>
* and specifying media size and orientation though the
* <code>PageFormat</code> object.
* <p>
* If a supported attribute-value is specified in this attribute set,
* it will take precedence over the API settings for this print()
* operation only.
* The following behaviour is specified for PageFormat:
* If a client uses the Printable interface, then the
* <code>attributes</code> parameter to this method is examined
* for attributes which specify media (by size), orientation, and
* imageable area, and those are used to construct a new PageFormat
* which is passed to the Printable object's print() method.
* See {@link Printable} for an explanation of the required
* behaviour of a Printable to ensure optimal printing via PrinterJob.
* For clients of the Pageable interface, the PageFormat will always
* be as supplied by that interface, on a per page basis.
* <p>
* These behaviours allow an application to directly pass the
* user settings returned from
* <code>printDialog(PrintRequestAttributeSet attributes</code> to
* this print() method.
* <p>
*
* @param attributes a set of attributes for the job
* @exception PrinterException an error in the print system
* caused the job to be aborted.
* @see Book
* @see Pageable
* @see Printable
* @since 1.4
*/
public void print(PrintRequestAttributeSet attributes)
throws PrinterException {
print();
}
/**
* Sets the number of copies to be printed.
* @param copies the number of copies to be printed
* @see #getCopies
*/
public abstract void setCopies(int copies);
/**
* Gets the number of copies to be printed.
* @return the number of copies to be printed.
* @see #setCopies
*/
public abstract int getCopies();
/**
* Gets the name of the printing user.
* @return the name of the printing user
*/
public abstract String getUserName();
/**
* Sets the name of the document to be printed.
* The document name can not be <code>null</code>.
* @param jobName the name of the document to be printed
* @see #getJobName
*/
public abstract void setJobName(String jobName);
/**
* Gets the name of the document to be printed.
* @return the name of the document to be printed.
* @see #setJobName
*/
public abstract String getJobName();
/**
* Cancels a print job that is in progress. If
* {@link #print() print} has been called but has not
* returned then this method signals
* that the job should be cancelled at the next
* chance. If there is no print job in progress then
* this call does nothing.
*/
public abstract void cancel();
/**
* Returns <code>true</code> if a print job is
* in progress, but is going to be cancelled
* at the next opportunity; otherwise returns
* <code>false</code>.
* @return <code>true</code> if the job in progress
* is going to be cancelled; <code>false</code> otherwise.
*/
public abstract boolean isCancelled();
}