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