home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-20 | 6.9 KB | 249 lines |
- /*
- * @(#)PrinterJob.java 1.4 98/03/18
- *
- * Copyright 1997, 1998 by Sun Microsystems, Inc.,
- * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
- * All rights reserved.
- *
- * This software is the confidential and proprietary information
- * of Sun Microsystems, Inc. ("Confidential Information"). You
- * shall not disclose such Confidential Information and shall use
- * it only in accordance with the terms of the license agreement
- * you entered into with Sun.
- */
-
- package java.awt.print;
-
- import java.io.IOException;
- import java.util.Enumeration;
-
- public abstract class PrinterJob {
-
- /* Instance Variables */
-
- /**
- * The number of book copies to be printed.
- */
- private int mNumCopies = 1;
-
- /**
- * Collation effects the order of the pages printed
- * when multiple copies are requested. For two copies
- * of a three page document the page order is:
- * mCollate true: 1, 2, 3, 1, 2, 3
- * mCollate false: 1, 1, 2, 2, 3, 3
- */
- private boolean mCollate = false;
-
- /**
- * The zero based indices of the first and last
- * pages to be printed. If 'mFirstPage' is
- * UNDEFINED_PAGE_NUM then the first page to
- * be printed is page 0. If 'mLastPage' is
- * UNDEFINED_PAGE_NUM then the last page to
- * be printed is the last one in the book.
- */
- private int mFirstPage = Pageable.UNKNOWN_NUMBER_OF_PAGES;
- private int mLastPage = Pageable.UNKNOWN_NUMBER_OF_PAGES;
-
- /**
- * Prints a set of pages from a Pageable object.
- * The Pageable object is queried for page format
- * and page painter for each page to be drawn. The
- * page painter, an implementor of the Printable
- * interface is then asked to draw the page.
- */
- public void print(Pageable document) throws IOException {
- int numCollatedCopies = getCollatedCopies();
- int numNonCollatedCopies = getNoncollatedCopies();
-
- /* Get the range of pages we are to print. If the
- * last page to print is unknown, then we print to
- * the end of the document. Note that firstPage
- * and lastPage are 0 based page indices.
- */
- int numPages = document.getNumberOfPages();
-
- int firstPage = getFirstPage();
- int lastPage = getLastPage();
- if(lastPage == Pageable.UNKNOWN_NUMBER_OF_PAGES){
- lastPage = document.getNumberOfPages() - 1;
- }
-
- startDoc();
-
- /* Three nested loops iterate over the document. The outer loop
- * counts the number of collated copies while the inner loop
- * counts the number of nonCollated copies. Normally, one of
- * these two loops will only execute once; that is we will
- * either print collated copies or noncollated copies. The
- * middle loop iterates over the pages.
- */
- for(int collated = 0; collated < numCollatedCopies; collated++){
- for(int i = firstPage; i <= lastPage; i++){
- for(int nonCollated = 0; nonCollated < numNonCollatedCopies;
- nonCollated++){
-
- printPage(document.getPage(i), i);
-
- }
- }
- }
-
- endDoc();
- }
-
- /**
- * The user is presented with a dialog through which
- * print time settings can be requested. These settings
- * are stored in the PrinterJob. If the user okays the
- * dialog, then true is returned. If the dialog is canceled
- * then false is returned.
- */
- public abstract boolean setup(Pageable doc);
-
-
- /**
- * The passed in PageFormat will be altered to describe
- * the default page size and orientation of the PrinterJob's
- * current printer.
- */
- public abstract PageFormat defaultPage(PageFormat page);
-
- /**
- * The passed in PageFormat is altered to be usable on
- * the PrinterJob's current printer.
- */
- public abstract PageFormat validatePage(PageFormat page);
-
- /**
- * Return the string value of the PrinterJob property
- * identified by 'key'.
- */
- public abstract String getPropertyString(String key)
- throws PrintPropertyException;
-
- /**
- * Set the String value of the property string identified
- * by 'key'.
- */
- public abstract void setPropertyString(String key, String value)
- throws PrintPropertyException;
-
-
- /**
- * Return an array of the keys for the properties in this
- * PrinterJob.
- */
- public abstract Enumeration getPropertyNames();
-
-
- /**
- * Set the range of pages from a Book to be printed.
- * Both 'firstPage' and 'lastPage' are zero based
- * page indices. If either parameter is less than
- * zero then the page range is set to be from the
- * first page to the last.
- */
- public void setPageRange(int firstPage, int lastPage) {
-
- if(firstPage >= 0 && lastPage >= 0) {
- mFirstPage = firstPage;
- mLastPage = lastPage;
- if(mLastPage < mFirstPage) mLastPage = mFirstPage;
- } else {
- mFirstPage = Pageable.UNKNOWN_NUMBER_OF_PAGES;
- mLastPage = Pageable.UNKNOWN_NUMBER_OF_PAGES;
- }
- }
-
- /**
- * Return the zero based index of the first page to
- * be printed in this job.
- */
- public int getFirstPage() {
- return mFirstPage == Book.UNKNOWN_NUMBER_OF_PAGES ? 0 : mFirstPage;
- }
-
- /**
- * Return the zero based index of the last page to
- * be printed in this job.
- */
- public int getLastPage() {
- return mLastPage;
- }
-
- /**
- * Set the number of copies to be printed.
- */
- public void setCopies(int copies) {
- mNumCopies = copies;
- }
-
- /**
- * Get the number of copies to be printed.
- */
- public int getCopies() {
- return mNumCopies;
- }
-
- /**
- * Set whether copies should be collated or not.
- * Two collated copies of a three page document
- * print in this order: 1, 2, 3, 1, 2, 3 while
- * uncollated copies print in this order:
- * 1, 1, 2, 2, 3, 3.
- */
- public void setCollated(boolean collate) {
- mCollate = collate;
- }
-
- /**
- * Return true if collated copies will be printed.
- */
- public boolean isCollated() {
- return mCollate;
- }
-
- /**
- * Called by the print() method at the start of
- * a print job.
- */
- protected abstract void startDoc() throws IOException;
-
- /**
- * Called by the print() method at the end of
- * a print job.
- */
- protected abstract void endDoc() throws IOException;
-
- /**
- * Images a page to the printer.
- */
- protected abstract void printPage(PageContext page, int pageIndex)
- throws IOException;
-
- /**
- * Returns how many times the entire book should
- * be printed by the PrintJob. If the printer
- * itself supports collation then this method
- * should return 1 indicating that the entire
- * book need only be printed once and the copies
- * will be collated and made in the printer.
- */
- protected int getCollatedCopies() {
- return isCollated() ? getCopies() : 1;
- }
-
- /**
- * Returns how many times each page in the book
- * should be consecutively printed by PrintJob.
- * If the printer makes copies itself then this
- * method should return 1.
- */
- protected int getNoncollatedCopies() {
- return isCollated() ? 1 : getCopies();
- }
-
- }
-