1. /*
  2. * @(#)Book.java 1.16 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.awt.print;
  8. import java.util.Vector;
  9. /**
  10. * The <code>Book</code> class provides a representation of a document in
  11. * which pages may have different page formats and page painters. This
  12. * class uses the {@link Pageable} interface to interact with a
  13. * {@link PrinterJob}.
  14. * @see Pageable
  15. * @see PrinterJob
  16. */
  17. public class Book implements Pageable {
  18. /* Class Constants */
  19. /* Class Variables */
  20. /* Instance Variables */
  21. /**
  22. * The set of pages that make up the Book.
  23. */
  24. private Vector mPages;
  25. /* Instance Methods */
  26. /**
  27. * Creates a new, empty <code>Book</code>.
  28. */
  29. public Book() {
  30. mPages = new Vector();
  31. }
  32. /**
  33. * Returns the number of pages in this <code>Book</code>.
  34. * @return the number of pages this <code>Book</code> contains.
  35. */
  36. public int getNumberOfPages(){
  37. return mPages.size();
  38. }
  39. /**
  40. * Returns the {@link PageFormat} of the page specified by
  41. * <code>pageIndex</code>.
  42. * @param pageIndex the zero based index of the page whose
  43. * <code>PageFormat</code> is being requested
  44. * @return the <code>PageFormat</code> describing the size and
  45. * orientation of the page.
  46. * @throws IndexOutOfBoundsException if the <code>Pageable</code>
  47. * does not contain the requested page
  48. */
  49. public PageFormat getPageFormat(int pageIndex)
  50. throws IndexOutOfBoundsException
  51. {
  52. return getPage(pageIndex).getPageFormat();
  53. }
  54. /**
  55. * Returns the {@link Printable} instance responsible for rendering
  56. * the page specified by <code>pageIndex</code>.
  57. * @param pageIndex the zero based index of the page whose
  58. * <code>Printable</code> is being requested
  59. * @return the <code>Printable</code> that renders the page.
  60. * @throws IndexOutOfBoundsException if the <code>Pageable</code>
  61. * does not contain the requested page
  62. */
  63. public Printable getPrintable(int pageIndex)
  64. throws IndexOutOfBoundsException
  65. {
  66. return getPage(pageIndex).getPrintable();
  67. }
  68. /**
  69. * Sets the <code>PageFormat</code> and the <code>Painter</code> for a
  70. * specified page number.
  71. * @param pageIndex the zero based index of the page whose
  72. * painter and format is altered
  73. * @param painter the <code>Printable</code> instance that
  74. * renders the page
  75. * @param page the size and orientation of the page
  76. * @throws IndexOutOfBoundsException if the specified
  77. * page is not already in this <code>Book</code>
  78. * @throws NullPointerException if the <code>painter</code> or
  79. * <code>page</code> argument is <code>null</code>
  80. */
  81. public void setPage(int pageIndex, Printable painter, PageFormat page)
  82. throws IndexOutOfBoundsException
  83. {
  84. if (painter == null) {
  85. throw new NullPointerException("painter is null");
  86. }
  87. if (page == null) {
  88. throw new NullPointerException("page is null");
  89. }
  90. mPages.setElementAt(new BookPage(painter, page), pageIndex);
  91. }
  92. /**
  93. * Appends a single page to the end of this <code>Book</code>.
  94. * @param painter the <code>Printable</code> instance that
  95. * renders the page
  96. * @param page the size and orientation of the page
  97. * @throws <code>NullPointerException</code>
  98. * If the <code>painter</code> or <code>page</code>
  99. * argument is <code>null</code>
  100. */
  101. public void append(Printable painter, PageFormat page) {
  102. mPages.addElement(new BookPage(painter, page));
  103. }
  104. /**
  105. * Appends <code>numPages</code> pages to the end of this
  106. * <code>Book</code>. Each of the pages is associated with
  107. * <code>page</code>.
  108. * @param painter the <code>Printable</code> instance that renders
  109. * the page
  110. * @param page the size and orientation of the page
  111. * @param numPages the number of pages to be added to the
  112. * this <code>Book</code>.
  113. * @throws <code>NullPointerException</code>
  114. * If the <code>painter</code> or <code>page</code>
  115. * argument is <code>null</code>
  116. */
  117. public void append(Printable painter, PageFormat page, int numPages) {
  118. BookPage bookPage = new BookPage(painter, page);
  119. int pageIndex = mPages.size();
  120. int newSize = pageIndex + numPages;
  121. mPages.setSize(newSize);
  122. for(int i = pageIndex; i < newSize; i++){
  123. mPages.setElementAt(bookPage, i);
  124. }
  125. }
  126. /**
  127. * Return the BookPage for the page specified by 'pageIndex'.
  128. */
  129. private BookPage getPage(int pageIndex)
  130. throws ArrayIndexOutOfBoundsException
  131. {
  132. return (BookPage) mPages.elementAt(pageIndex);
  133. }
  134. /**
  135. * The BookPage inner class describes an individual
  136. * page in a Book through a PageFormat-Printable pair.
  137. */
  138. private class BookPage {
  139. /**
  140. * The size and orientation of the page.
  141. */
  142. private PageFormat mFormat;
  143. /**
  144. * The instance that will draw the page.
  145. */
  146. private Printable mPainter;
  147. /**
  148. * A new instance where 'format' describes the page's
  149. * size and orientation and 'painter' is the instance
  150. * that will draw the page's graphics.
  151. * @throws NullPointerException
  152. * If the <code>painter</code> or <code>format</code>
  153. * argument is <code>null</code>
  154. */
  155. BookPage(Printable painter, PageFormat format) {
  156. if (painter == null || format == null) {
  157. throw new NullPointerException();
  158. }
  159. mFormat = format;
  160. mPainter = painter;
  161. }
  162. /**
  163. * Return the instance that paints the
  164. * page.
  165. */
  166. Printable getPrintable() {
  167. return mPainter;
  168. }
  169. /**
  170. * Return the format of the page.
  171. */
  172. PageFormat getPageFormat() {
  173. return mFormat;
  174. }
  175. }
  176. }