KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > print > Book


1 /*
2  * @(#)Book.java 1.17 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.awt.print;
9
10 import java.util.Vector JavaDoc;
11
12 /**
13  * The <code>Book</code> class provides a representation of a document in
14  * which pages may have different page formats and page painters. This
15  * class uses the {@link Pageable} interface to interact with a
16  * {@link PrinterJob}.
17  * @see Pageable
18  * @see PrinterJob
19 */

20
21 public class Book implements Pageable JavaDoc {
22
23  /* Class Constants */
24
25  /* Class Variables */
26
27  /* Instance Variables */
28
29     /**
30      * The set of pages that make up the Book.
31      */

32     private Vector JavaDoc mPages;
33
34  /* Instance Methods */
35
36     /**
37      * Creates a new, empty <code>Book</code>.
38      */

39     public Book() {
40         mPages = new Vector JavaDoc();
41     }
42
43     /**
44      * Returns the number of pages in this <code>Book</code>.
45      * @return the number of pages this <code>Book</code> contains.
46      */

47     public int getNumberOfPages(){
48     return mPages.size();
49     }
50
51     /**
52      * Returns the {@link PageFormat} of the page specified by
53      * <code>pageIndex</code>.
54      * @param pageIndex the zero based index of the page whose
55      * <code>PageFormat</code> is being requested
56      * @return the <code>PageFormat</code> describing the size and
57      * orientation of the page.
58      * @throws IndexOutOfBoundsException if the <code>Pageable</code>
59      * does not contain the requested page
60      */

61     public PageFormat JavaDoc getPageFormat(int pageIndex)
62     throws IndexOutOfBoundsException JavaDoc
63     {
64     return getPage(pageIndex).getPageFormat();
65     }
66
67     /**
68      * Returns the {@link Printable} instance responsible for rendering
69      * the page specified by <code>pageIndex</code>.
70      * @param pageIndex the zero based index of the page whose
71      * <code>Printable</code> is being requested
72      * @return the <code>Printable</code> that renders the page.
73      * @throws IndexOutOfBoundsException if the <code>Pageable</code>
74      * does not contain the requested page
75      */

76     public Printable JavaDoc getPrintable(int pageIndex)
77     throws IndexOutOfBoundsException JavaDoc
78     {
79     return getPage(pageIndex).getPrintable();
80     }
81
82     /**
83      * Sets the <code>PageFormat</code> and the <code>Painter</code> for a
84      * specified page number.
85      * @param pageIndex the zero based index of the page whose
86      * painter and format is altered
87      * @param painter the <code>Printable</code> instance that
88      * renders the page
89      * @param page the size and orientation of the page
90      * @throws IndexOutOfBoundsException if the specified
91      * page is not already in this <code>Book</code>
92      * @throws NullPointerException if the <code>painter</code> or
93      * <code>page</code> argument is <code>null</code>
94      */

95     public void setPage(int pageIndex, Printable JavaDoc painter, PageFormat JavaDoc page)
96     throws IndexOutOfBoundsException JavaDoc
97     {
98     if (painter == null) {
99         throw new NullPointerException JavaDoc("painter is null");
100     }
101
102     if (page == null) {
103         throw new NullPointerException JavaDoc("page is null");
104     }
105
106     mPages.setElementAt(new BookPage(painter, page), pageIndex);
107     }
108
109     /**
110      * Appends a single page to the end of this <code>Book</code>.
111      * @param painter the <code>Printable</code> instance that
112      * renders the page
113      * @param page the size and orientation of the page
114      * @throws <code>NullPointerException</code>
115      * If the <code>painter</code> or <code>page</code>
116      * argument is <code>null</code>
117      */

118     public void append(Printable JavaDoc painter, PageFormat JavaDoc page) {
119     mPages.addElement(new BookPage(painter, page));
120     }
121
122     /**
123      * Appends <code>numPages</code> pages to the end of this
124      * <code>Book</code>. Each of the pages is associated with
125      * <code>page</code>.
126      * @param painter the <code>Printable</code> instance that renders
127      * the page
128      * @param page the size and orientation of the page
129      * @param numPages the number of pages to be added to the
130      * this <code>Book</code>.
131      * @throws <code>NullPointerException</code>
132      * If the <code>painter</code> or <code>page</code>
133      * argument is <code>null</code>
134      */

135     public void append(Printable JavaDoc painter, PageFormat JavaDoc page, int numPages) {
136     BookPage bookPage = new BookPage(painter, page);
137     int pageIndex = mPages.size();
138     int newSize = pageIndex + numPages;
139
140     mPages.setSize(newSize);
141     for(int i = pageIndex; i < newSize; i++){
142         mPages.setElementAt(bookPage, i);
143     }
144     }
145
146     /**
147      * Return the BookPage for the page specified by 'pageIndex'.
148      */

149     private BookPage getPage(int pageIndex)
150     throws ArrayIndexOutOfBoundsException JavaDoc
151     {
152     return (BookPage) mPages.elementAt(pageIndex);
153     }
154
155     /**
156      * The BookPage inner class describes an individual
157      * page in a Book through a PageFormat-Printable pair.
158      */

159     private class BookPage {
160     /**
161      * The size and orientation of the page.
162      */

163     private PageFormat JavaDoc mFormat;
164
165     /**
166      * The instance that will draw the page.
167      */

168     private Printable JavaDoc mPainter;
169
170     /**
171      * A new instance where 'format' describes the page's
172      * size and orientation and 'painter' is the instance
173      * that will draw the page's graphics.
174      * @throws NullPointerException
175      * If the <code>painter</code> or <code>format</code>
176      * argument is <code>null</code>
177      */

178     BookPage(Printable JavaDoc painter, PageFormat JavaDoc format) {
179
180         if (painter == null || format == null) {
181         throw new NullPointerException JavaDoc();
182         }
183
184         mFormat = format;
185         mPainter = painter;
186     }
187
188     /**
189      * Return the instance that paints the
190      * page.
191      */

192     Printable JavaDoc getPrintable() {
193         return mPainter;
194     }
195
196     /**
197      * Return the format of the page.
198      */

199     PageFormat JavaDoc getPageFormat() {
200         return mFormat;
201     }
202     }
203 }
204
Popular Tags