001 /*
002 * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.awt.print;
027
028 import java.util.Vector;
029
030 /**
031 * The <code>Book</code> class provides a representation of a document in
032 * which pages may have different page formats and page painters. This
033 * class uses the {@link Pageable} interface to interact with a
034 * {@link PrinterJob}.
035 * @see Pageable
036 * @see PrinterJob
037 */
038
039 public class Book implements Pageable {
040
041 /* Class Constants */
042
043 /* Class Variables */
044
045 /* Instance Variables */
046
047 /**
048 * The set of pages that make up the Book.
049 */
050 private Vector mPages;
051
052 /* Instance Methods */
053
054 /**
055 * Creates a new, empty <code>Book</code>.
056 */
057 public Book() {
058 mPages = new Vector();
059 }
060
061 /**
062 * Returns the number of pages in this <code>Book</code>.
063 * @return the number of pages this <code>Book</code> contains.
064 */
065 public int getNumberOfPages() {
066 return mPages.size();
067 }
068
069 /**
070 * Returns the {@link PageFormat} of the page specified by
071 * <code>pageIndex</code>.
072 * @param pageIndex the zero based index of the page whose
073 * <code>PageFormat</code> is being requested
074 * @return the <code>PageFormat</code> describing the size and
075 * orientation of the page.
076 * @throws IndexOutOfBoundsException if the <code>Pageable</code>
077 * does not contain the requested page
078 */
079 public PageFormat getPageFormat(int pageIndex)
080 throws IndexOutOfBoundsException {
081 return getPage(pageIndex).getPageFormat();
082 }
083
084 /**
085 * Returns the {@link Printable} instance responsible for rendering
086 * the page specified by <code>pageIndex</code>.
087 * @param pageIndex the zero based index of the page whose
088 * <code>Printable</code> is being requested
089 * @return the <code>Printable</code> that renders the page.
090 * @throws IndexOutOfBoundsException if the <code>Pageable</code>
091 * does not contain the requested page
092 */
093 public Printable getPrintable(int pageIndex)
094 throws IndexOutOfBoundsException {
095 return getPage(pageIndex).getPrintable();
096 }
097
098 /**
099 * Sets the <code>PageFormat</code> and the <code>Painter</code> for a
100 * specified page number.
101 * @param pageIndex the zero based index of the page whose
102 * painter and format is altered
103 * @param painter the <code>Printable</code> instance that
104 * renders the page
105 * @param page the size and orientation of the page
106 * @throws IndexOutOfBoundsException if the specified
107 * page is not already in this <code>Book</code>
108 * @throws NullPointerException if the <code>painter</code> or
109 * <code>page</code> argument is <code>null</code>
110 */
111 public void setPage(int pageIndex, Printable painter,
112 PageFormat page) throws IndexOutOfBoundsException {
113 if (painter == null) {
114 throw new NullPointerException("painter is null");
115 }
116
117 if (page == null) {
118 throw new NullPointerException("page is null");
119 }
120
121 mPages.setElementAt(new BookPage(painter, page), pageIndex);
122 }
123
124 /**
125 * Appends a single page to the end of this <code>Book</code>.
126 * @param painter the <code>Printable</code> instance that
127 * renders the page
128 * @param page the size and orientation of the page
129 * @throws <code>NullPointerException</code>
130 * If the <code>painter</code> or <code>page</code>
131 * argument is <code>null</code>
132 */
133 public void append(Printable painter, PageFormat page) {
134 mPages.addElement(new BookPage(painter, page));
135 }
136
137 /**
138 * Appends <code>numPages</code> pages to the end of this
139 * <code>Book</code>. Each of the pages is associated with
140 * <code>page</code>.
141 * @param painter the <code>Printable</code> instance that renders
142 * the page
143 * @param page the size and orientation of the page
144 * @param numPages the number of pages to be added to the
145 * this <code>Book</code>.
146 * @throws <code>NullPointerException</code>
147 * If the <code>painter</code> or <code>page</code>
148 * argument is <code>null</code>
149 */
150 public void append(Printable painter, PageFormat page, int numPages) {
151 BookPage bookPage = new BookPage(painter, page);
152 int pageIndex = mPages.size();
153 int newSize = pageIndex + numPages;
154
155 mPages.setSize(newSize);
156 for (int i = pageIndex; i < newSize; i++) {
157 mPages.setElementAt(bookPage, i);
158 }
159 }
160
161 /**
162 * Return the BookPage for the page specified by 'pageIndex'.
163 */
164 private BookPage getPage(int pageIndex)
165 throws ArrayIndexOutOfBoundsException {
166 return (BookPage) mPages.elementAt(pageIndex);
167 }
168
169 /**
170 * The BookPage inner class describes an individual
171 * page in a Book through a PageFormat-Printable pair.
172 */
173 private class BookPage {
174 /**
175 * The size and orientation of the page.
176 */
177 private PageFormat mFormat;
178
179 /**
180 * The instance that will draw the page.
181 */
182 private Printable mPainter;
183
184 /**
185 * A new instance where 'format' describes the page's
186 * size and orientation and 'painter' is the instance
187 * that will draw the page's graphics.
188 * @throws NullPointerException
189 * If the <code>painter</code> or <code>format</code>
190 * argument is <code>null</code>
191 */
192 BookPage(Printable painter, PageFormat format) {
193
194 if (painter == null || format == null) {
195 throw new NullPointerException();
196 }
197
198 mFormat = format;
199 mPainter = painter;
200 }
201
202 /**
203 * Return the instance that paints the
204 * page.
205 */
206 Printable getPrintable() {
207 return mPainter;
208 }
209
210 /**
211 * Return the format of the page.
212 */
213 PageFormat getPageFormat() {
214 return mFormat;
215 }
216 }
217 }
|