1 /*
2 * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26 package java.awt.print;
27
28 import java.util.Vector;
29
30 /**
31 * The <code>Book</code> class provides a representation of a document in
32 * which pages may have different page formats and page painters. This
33 * class uses the {@link Pageable} interface to interact with a
34 * {@link PrinterJob}.
35 * @see Pageable
36 * @see PrinterJob
37 */
38
39 public class Book implements Pageable {
40
41 /* Class Constants */
42
43 /* Class Variables */
44
45 /* Instance Variables */
46
47 /**
48 * The set of pages that make up the Book.
49 */
50 private Vector mPages;
51
52 /* Instance Methods */
53
54 /**
55 * Creates a new, empty <code>Book</code>.
56 */
57 public Book() {
58 mPages = new Vector();
59 }
60
61 /**
62 * Returns the number of pages in this <code>Book</code>.
63 * @return the number of pages this <code>Book</code> contains.
64 */
65 public int getNumberOfPages(){
66 return mPages.size();
67 }
68
69 /**
70 * Returns the {@link PageFormat} of the page specified by
71 * <code>pageIndex</code>.
72 * @param pageIndex the zero based index of the page whose
73 * <code>PageFormat</code> is being requested
74 * @return the <code>PageFormat</code> describing the size and
75 * orientation of the page.
76 * @throws IndexOutOfBoundsException if the <code>Pageable</code>
77 * does not contain the requested page
78 */
79 public PageFormat getPageFormat(int pageIndex)
80 throws IndexOutOfBoundsException
81 {
82 return getPage(pageIndex).getPageFormat();
83 }
84
85 /**
86 * Returns the {@link Printable} instance responsible for rendering
87 * the page specified by <code>pageIndex</code>.
88 * @param pageIndex the zero based index of the page whose
89 * <code>Printable</code> is being requested
90 * @return the <code>Printable</code> that renders the page.
91 * @throws IndexOutOfBoundsException if the <code>Pageable</code>
92 * does not contain the requested page
93 */
94 public Printable getPrintable(int pageIndex)
95 throws IndexOutOfBoundsException
96 {
97 return getPage(pageIndex).getPrintable();
98 }
99
100 /**
101 * Sets the <code>PageFormat</code> and the <code>Painter</code> for a
102 * specified page number.
103 * @param pageIndex the zero based index of the page whose
104 * painter and format is altered
105 * @param painter the <code>Printable</code> instance that
106 * renders the page
107 * @param page the size and orientation of the page
108 * @throws IndexOutOfBoundsException if the specified
109 * page is not already in this <code>Book</code>
110 * @throws NullPointerException if the <code>painter</code> or
111 * <code>page</code> argument is <code>null</code>
112 */
113 public void setPage(int pageIndex, Printable painter, PageFormat page)
114 throws IndexOutOfBoundsException
115 {
116 if (painter == null) {
117 throw new NullPointerException("painter is null");
118 }
119
120 if (page == null) {
121 throw new NullPointerException("page is null");
122 }
123
124 mPages.setElementAt(new BookPage(painter, page), pageIndex);
125 }
126
127 /**
128 * Appends a single page to the end of this <code>Book</code>.
129 * @param painter the <code>Printable</code> instance that
130 * renders the page
131 * @param page the size and orientation of the page
132 * @throws <code>NullPointerException</code>
133 * If the <code>painter</code> or <code>page</code>
134 * argument is <code>null</code>
135 */
136 public void append(Printable painter, PageFormat page) {
137 mPages.addElement(new BookPage(painter, page));
138 }
139
140 /**
141 * Appends <code>numPages</code> pages to the end of this
142 * <code>Book</code>. Each of the pages is associated with
143 * <code>page</code>.
144 * @param painter the <code>Printable</code> instance that renders
145 * the page
146 * @param page the size and orientation of the page
147 * @param numPages the number of pages to be added to the
148 * this <code>Book</code>.
149 * @throws <code>NullPointerException</code>
150 * If the <code>painter</code> or <code>page</code>
151 * argument is <code>null</code>
152 */
153 public void append(Printable painter, PageFormat page, int numPages) {
154 BookPage bookPage = new BookPage(painter, page);
155 int pageIndex = mPages.size();
156 int newSize = pageIndex + numPages;
157
158 mPages.setSize(newSize);
159 for(int i = pageIndex; i < newSize; i++){
160 mPages.setElementAt(bookPage, i);
161 }
162 }
163
164 /**
165 * Return the BookPage for the page specified by 'pageIndex'.
166 */
167 private BookPage getPage(int pageIndex)
168 throws ArrayIndexOutOfBoundsException
169 {
170 return (BookPage) mPages.elementAt(pageIndex);
171 }
172
173 /**
174 * The BookPage inner class describes an individual
175 * page in a Book through a PageFormat-Printable pair.
176 */
177 private class BookPage {
178 /**
179 * The size and orientation of the page.
180 */
181 private PageFormat mFormat;
182
183 /**
184 * The instance that will draw the page.
185 */
186 private Printable mPainter;
187
188 /**
189 * A new instance where 'format' describes the page's
190 * size and orientation and 'painter' is the instance
191 * that will draw the page's graphics.
192 * @throws NullPointerException
193 * If the <code>painter</code> or <code>format</code>
194 * argument is <code>null</code>
195 */
196 BookPage(Printable painter, PageFormat format) {
197
198 if (painter == null || format == null) {
199 throw new NullPointerException();
200 }
201
202 mFormat = format;
203 mPainter = painter;
204 }
205
206 /**
207 * Return the instance that paints the
208 * page.
209 */
210 Printable getPrintable() {
211 return mPainter;
212 }
213
214 /**
215 * Return the format of the page.
216 */
217 PageFormat getPageFormat() {
218 return mFormat;
219 }
220 }
221 }