1 /* 2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.awt.print; 27 28 import java.util.Vector; 29 30 /** 31 * The {@code Book} 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<BookPage> mPages; 51 52 /* Instance Methods */ 53 54 /** 55 * Creates a new, empty {@code Book}. 56 */ Book()57 public Book() { 58 mPages = new Vector<>(); 59 } 60 61 /** 62 * Returns the number of pages in this {@code Book}. 63 * @return the number of pages this {@code Book} contains. 64 */ getNumberOfPages()65 public int getNumberOfPages(){ 66 return mPages.size(); 67 } 68 69 /** 70 * Returns the {@link PageFormat} of the page specified by 71 * {@code pageIndex}. 72 * @param pageIndex the zero based index of the page whose 73 * {@code PageFormat} is being requested 74 * @return the {@code PageFormat} describing the size and 75 * orientation of the page. 76 * @throws IndexOutOfBoundsException if the {@code Pageable} 77 * does not contain the requested page 78 */ getPageFormat(int pageIndex)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}. 88 * @param pageIndex the zero based index of the page whose 89 * {@code Printable} is being requested 90 * @return the {@code Printable} that renders the page. 91 * @throws IndexOutOfBoundsException if the {@code Pageable} 92 * does not contain the requested page 93 */ getPrintable(int pageIndex)94 public Printable getPrintable(int pageIndex) 95 throws IndexOutOfBoundsException 96 { 97 return getPage(pageIndex).getPrintable(); 98 } 99 100 /** 101 * Sets the {@code PageFormat} and the {@code Painter} 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} 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} 110 * @throws NullPointerException if the {@code painter} or 111 * {@code page} argument is {@code null} 112 */ setPage(int pageIndex, Printable painter, PageFormat page)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}. 129 * @param painter the {@code Printable} instance that 130 * renders the page 131 * @param page the size and orientation of the page 132 * @throws NullPointerException 133 * If the {@code painter} or {@code page} 134 * argument is {@code null} 135 */ append(Printable painter, PageFormat page)136 public void append(Printable painter, PageFormat page) { 137 mPages.addElement(new BookPage(painter, page)); 138 } 139 140 /** 141 * Appends {@code numPages} pages to the end of this 142 * {@code Book}. Each of the pages is associated with 143 * {@code page}. 144 * @param painter the {@code Printable} 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}. 149 * @throws NullPointerException 150 * If the {@code painter} or {@code page} 151 * argument is {@code null} 152 */ append(Printable painter, PageFormat page, int numPages)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 */ getPage(int pageIndex)167 private BookPage getPage(int pageIndex) 168 throws ArrayIndexOutOfBoundsException 169 { 170 return 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} or {@code format} 194 * argument is {@code null} 195 */ BookPage(Printable painter, PageFormat format)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 */ getPrintable()210 Printable getPrintable() { 211 return mPainter; 212 } 213 214 /** 215 * Return the format of the page. 216 */ getPageFormat()217 PageFormat getPageFormat() { 218 return mFormat; 219 } 220 } 221 } 222