home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / awt / print / PrinterJob.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  6.9 KB  |  249 lines

  1. /*
  2.  * @(#)PrinterJob.java    1.4 98/03/18
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.awt.print;
  16.  
  17. import java.io.IOException;
  18. import java.util.Enumeration;
  19.  
  20. public abstract class PrinterJob {
  21.  
  22. /* Instance Variables */
  23.  
  24.     /**
  25.      * The number of book copies to be printed.
  26.      */
  27.     private int mNumCopies = 1;
  28.     
  29.     /**
  30.      * Collation effects the order of the pages printed
  31.      * when multiple copies are requested. For two copies
  32.      * of a three page document the page order is:
  33.      *    mCollate true: 1, 2, 3, 1, 2, 3
  34.      *    mCollate false: 1, 1, 2, 2, 3, 3
  35.      */
  36.     private boolean mCollate = false;
  37.  
  38.     /**
  39.      * The zero based indices of the first and last
  40.      * pages to be printed. If 'mFirstPage' is 
  41.      * UNDEFINED_PAGE_NUM then the first page to
  42.      * be printed is page 0. If 'mLastPage' is
  43.      * UNDEFINED_PAGE_NUM then the last page to
  44.      * be printed is the last one in the book.
  45.      */
  46.     private int mFirstPage = Pageable.UNKNOWN_NUMBER_OF_PAGES;
  47.     private int mLastPage = Pageable.UNKNOWN_NUMBER_OF_PAGES;
  48.  
  49.     /**
  50.      * Prints a set of pages from a Pageable object.
  51.      * The Pageable object is queried for page format
  52.      * and page painter for each page to be drawn. The
  53.      * page painter, an implementor of the Printable
  54.      * interface is then asked to draw the page.
  55.      */
  56.     public void print(Pageable document) throws IOException {
  57.     int numCollatedCopies = getCollatedCopies();
  58.     int numNonCollatedCopies = getNoncollatedCopies();
  59.  
  60.     /* Get the range of pages we are to print. If the
  61.      * last page to print is unknown, then we print to
  62.      * the end of the document. Note that firstPage
  63.      * and lastPage are 0 based page indices.
  64.      */
  65.     int numPages = document.getNumberOfPages();
  66.  
  67.     int firstPage = getFirstPage();
  68.     int lastPage = getLastPage();
  69.     if(lastPage == Pageable.UNKNOWN_NUMBER_OF_PAGES){
  70.         lastPage = document.getNumberOfPages() - 1;
  71.     }
  72.  
  73.     startDoc();
  74.  
  75.     /* Three nested loops iterate over the document. The outer loop
  76.      * counts the number of collated copies while the inner loop
  77.      * counts the number of nonCollated copies. Normally, one of
  78.      * these two loops will only execute once; that is we will
  79.      * either print collated copies or noncollated copies. The
  80.      * middle loop iterates over the pages.
  81.     */
  82.     for(int collated = 0; collated < numCollatedCopies; collated++){
  83.         for(int i = firstPage; i <= lastPage; i++){
  84.         for(int nonCollated = 0; nonCollated < numNonCollatedCopies;
  85.                      nonCollated++){
  86.  
  87.             printPage(document.getPage(i), i);
  88.  
  89.         }
  90.         }
  91.     }
  92.  
  93.     endDoc();
  94.     }
  95.  
  96.     /**
  97.      * The user is presented with a dialog through which
  98.      * print time settings can be requested. These settings
  99.      * are stored in the PrinterJob. If the user okays the
  100.      * dialog, then true is returned. If the dialog is canceled
  101.      * then false is returned.
  102.      */
  103.     public abstract boolean setup(Pageable doc);
  104.  
  105.  
  106.     /**
  107.      * The passed in PageFormat will be altered to describe
  108.      * the default page size and orientation of the PrinterJob's
  109.      * current printer.
  110.      */
  111.     public abstract PageFormat defaultPage(PageFormat page);
  112.  
  113.     /**
  114.      * The passed in PageFormat is altered to be usable on
  115.      * the PrinterJob's current printer.
  116.      */
  117.     public abstract PageFormat validatePage(PageFormat page);
  118.  
  119.     /**
  120.      * Return the string value of the PrinterJob property
  121.      * identified by 'key'.
  122.      */
  123.     public abstract String getPropertyString(String key)
  124.                throws PrintPropertyException;
  125.  
  126.     /**
  127.      * Set the String value of the property string identified
  128.      * by 'key'.
  129.      */
  130.     public abstract void setPropertyString(String key, String value)
  131.              throws PrintPropertyException;
  132.  
  133.  
  134.     /**
  135.      * Return an array of the keys for the properties in this
  136.      * PrinterJob.
  137.      */
  138.     public abstract Enumeration getPropertyNames();
  139.  
  140.  
  141.     /**
  142.      * Set the range of pages from a Book to be printed.
  143.      * Both 'firstPage' and 'lastPage' are zero based
  144.      * page indices. If either parameter is less than
  145.      * zero then the page range is set to be from the
  146.      * first page to the last.
  147.      */
  148.     public void setPageRange(int firstPage, int lastPage) {
  149.  
  150.     if(firstPage >= 0 && lastPage >= 0) {
  151.         mFirstPage = firstPage;
  152.         mLastPage = lastPage;
  153.         if(mLastPage < mFirstPage) mLastPage = mFirstPage;
  154.     } else {
  155.         mFirstPage = Pageable.UNKNOWN_NUMBER_OF_PAGES;
  156.         mLastPage = Pageable.UNKNOWN_NUMBER_OF_PAGES;
  157.     }
  158.     }
  159.  
  160.     /**
  161.      * Return the zero based index of the first page to
  162.      * be printed in this job.
  163.      */
  164.     public int getFirstPage() {
  165.     return mFirstPage == Book.UNKNOWN_NUMBER_OF_PAGES ? 0 : mFirstPage;
  166.     }
  167.  
  168.     /**
  169.      * Return the zero based index of the last page to
  170.      * be printed in this job.
  171.      */
  172.     public int getLastPage() {
  173.     return mLastPage;
  174.     }
  175.  
  176.     /**
  177.      * Set the number of copies to be printed.
  178.      */
  179.     public void setCopies(int copies) {
  180.     mNumCopies = copies;
  181.     }
  182.  
  183.     /**
  184.      * Get the number of copies to be printed.
  185.      */
  186.     public int getCopies() {
  187.     return mNumCopies;
  188.     }
  189.  
  190.     /**
  191.      * Set whether copies should be collated or not.
  192.      * Two collated copies of a three page document
  193.      * print in this order: 1, 2, 3, 1, 2, 3 while
  194.      * uncollated copies print in this order:
  195.      * 1, 1, 2, 2, 3, 3.
  196.      */
  197.     public void setCollated(boolean collate) {
  198.     mCollate = collate;
  199.     }
  200.  
  201.     /**
  202.      * Return true if collated copies will be printed.
  203.      */
  204.     public boolean isCollated() {
  205.     return mCollate;
  206.     }
  207.  
  208.     /**
  209.      * Called by the print() method at the start of
  210.      * a print job.
  211.      */
  212.     protected abstract void startDoc() throws IOException;
  213.  
  214.     /**
  215.      * Called by the print() method at the end of
  216.      * a print job.
  217.      */
  218.     protected abstract void endDoc() throws IOException;
  219.  
  220.     /**
  221.      * Images a page to the printer.
  222.      */
  223.     protected abstract void printPage(PageContext page, int pageIndex)
  224.                       throws IOException;
  225.  
  226.     /**
  227.      * Returns how many times the entire book should
  228.      * be printed by the PrintJob. If the printer
  229.      * itself supports collation then this method
  230.      * should return 1 indicating that the entire
  231.      * book need only be printed once and the copies
  232.      * will be collated and made in the printer.
  233.      */
  234.     protected int getCollatedCopies() {
  235.         return isCollated() ? getCopies() : 1;
  236.     }
  237.  
  238.     /**
  239.      * Returns how many times each page in the book
  240.      * should be consecutively printed by PrintJob.
  241.      * If the printer makes copies itself then this
  242.      * method should return 1.
  243.      */
  244.     protected int getNoncollatedCopies() {
  245.     return isCollated() ? 1 : getCopies();
  246.     }
  247.  
  248. }
  249.