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 / Book.java next >
Encoding:
Java Source  |  1998-03-20  |  3.7 KB  |  172 lines

  1. /*
  2.  * @(#)Book.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.util.Vector;
  18.  
  19. /**
  20.  * A Book maintains a list of pages to be printed.
  21.  */
  22.  
  23. public class Book implements Pageable {
  24.  
  25.  /* Class Constants */
  26.      
  27.  /* Class Variables */
  28.  
  29.  /* Instance Variables */
  30.  
  31.     /**
  32.      * The set of pages that make up the Book.
  33.      */
  34.     private Vector mPages;
  35.     
  36.  /* Instance Methods */
  37.  
  38.     /**
  39.      *    Create a new, empty book.
  40.      */
  41.     public Book() {
  42.         mPages = new Vector();
  43.     }
  44.      
  45.     /**
  46.      * Return the number of pages in this document.
  47.      */
  48.     public int getNumberOfPages(){
  49.     return mPages.size();
  50.     }
  51.  
  52.     public PageContext getPage(int pageIndex)
  53.                throws IndexOutOfBoundsException {
  54.     return (BookPageContext) mPages.elementAt(pageIndex);
  55.     }
  56.      
  57.     /**
  58.      * Set the page format and the painter for a
  59.      * given page number. If the page specified is not
  60.      * already in the book, then an IndexOutOfBoundsException
  61.      * excpetion is thrown.
  62.      */
  63.     public void setPage(int pageNumber, PageFormat page, Printable painter)
  64.             throws IndexOutOfBoundsException
  65.     {
  66.     mPages.setElementAt(new BookPageContext(page, painter, pageNumber),
  67.                 pageNumber);
  68.     }
  69.     
  70.     /**
  71.      * Append a single page to the end of this
  72.      * document.
  73.      */
  74.     public void append(PageFormat page, Printable painter)
  75.     {
  76.     int pageIndex = mPages.size();
  77.     mPages.addElement(new BookPageContext(page, painter, pageIndex));
  78.     }
  79.  
  80.     /**
  81.      * Append 'numPages' pages to the end of this document.
  82.      * Each of the pages is associated with 'page'.
  83.      */     
  84.     public void append(PageFormat page, Printable painter, int numPages)
  85.     {
  86.     int pageIndex = mPages.size();
  87.     int newSize = pageIndex + numPages;
  88.  
  89.     mPages.setSize(newSize);
  90.  
  91.     for(int i = pageIndex; i < newSize; i++){
  92.         setPage(i, page, painter);
  93.     }
  94.     }
  95.  
  96.     /**
  97.      * The BookPageContext inner class describes an individual
  98.      * page in a Book.
  99.      */
  100.     protected class BookPageContext implements PageContext
  101.     {
  102.     /**
  103.      *  The size and orientation of the page.
  104.      */
  105.     private PageFormat mFormat;
  106.  
  107.     /**
  108.      * The instance that will draw the page.
  109.      */
  110.     private Printable mPainter;
  111.  
  112.     /**
  113.      * The zero based index of this page inthe Book,
  114.      */
  115.     private int mIndex;
  116.  
  117.     /**
  118.      * A new instance where 'format' describes the page's
  119.      * size and orientation and 'painter' is the instance
  120.      * that will draw the page's graphics.
  121.      */
  122.     protected BookPageContext(PageFormat format, Printable painter,
  123.                   int pageIndex) {
  124.         set(format, painter);
  125.         mIndex = pageIndex;
  126.     }
  127.  
  128.     /**
  129.      * Set the paper size and orientation, 'format', as
  130.      * well as the instance that will draw the page,
  131.      * 'painter'.
  132.      */
  133.     protected void set(PageFormat format, Printable painter) {
  134.         mFormat = format;
  135.         mPainter = painter;
  136.     }
  137.  
  138.     /**
  139.      * Return the instance that paints the
  140.      * page.
  141.      */
  142.     public Printable getPagePainter() {
  143.         return mPainter;
  144.     }
  145.  
  146.     /**
  147.      * Return the format of the page.
  148.      */
  149.     public PageFormat getPageFormat() {
  150.         return mFormat;
  151.     }
  152.     
  153.     /**
  154.      * Return the name of the given page. This is
  155.      * simply the page index plus 1 converted to
  156.      * a String.
  157.      */
  158.     public String getPageName() {
  159.         return Integer.toString(mIndex + 1);
  160.     }
  161.  
  162.     /**
  163.      * Return this page's index.
  164.      */
  165.     public int getPageIndex() {
  166.         return mIndex;
  167.     }
  168.  
  169.     }    
  170. }
  171.  
  172.