1. /*
  2. * @(#)Book.java 1.11 01/11/29
  3. *
  4. * Copyright 2002 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. * @exception <code>IndexOutOfBoundsException</code>
  47. * The <code>Pageable</code> does not contain the requested
  48. * page
  49. */
  50. public PageFormat getPageFormat(int pageIndex)
  51. throws IndexOutOfBoundsException
  52. {
  53. return getPage(pageIndex).getPageFormat();
  54. }
  55. /**
  56. * Returns the {@link Printable} instance responsible for rendering
  57. * the page specified by <code>pageIndex</code>.
  58. * @param pageIndex the zero based index of the page whose
  59. * <code>Printable</code> is being requested
  60. * @return the <code>Printable</code> that renders the page.
  61. * @exception <code>IndexOutOfBoundsException</code>
  62. * the <code>Pageable</code> does not contain the requested
  63. * page.
  64. */
  65. public Printable getPrintable(int pageIndex)
  66. throws IndexOutOfBoundsException
  67. {
  68. return getPage(pageIndex).getPrintable();
  69. }
  70. /**
  71. * Sets the <code>PageFormat</code> and the <code>Painter</code> for a
  72. * specified page number.
  73. * @param pageIndex the zero based index of the page whose
  74. * painter and format is altered
  75. * @param painter the <code>Printable</code> instance that
  76. * renders the page
  77. * @param page the size and orientation of the page
  78. * @exception <code>IndexOutOfBoundsException</code> If the specified
  79. * page is not already in this <code>Book</code>
  80. * @throws <code>NullPointerException</code>
  81. * If the <code>painter</code> or <code>page</code>
  82. * argument is <code>null</code>
  83. */
  84. public void setPage(int pageIndex, Printable painter, PageFormat page)
  85. throws IndexOutOfBoundsException
  86. {
  87. if (painter == null) {
  88. throw new NullPointerException("painter is null");
  89. }
  90. if (page == null) {
  91. throw new NullPointerException("page is null");
  92. }
  93. mPages.setElementAt(new BookPage(painter, page), pageIndex);
  94. }
  95. /**
  96. * Appends a single page to the end of this <code>Book</code>.
  97. * @param painter the <code>Printable</code> instance that
  98. * renders the page
  99. * @param page the size and orientation of the page
  100. * @throws <code>NullPointerException</code>
  101. * If the <code>painter</code> or <code>page</code>
  102. * argument is <code>null</code>
  103. */
  104. public void append(Printable painter, PageFormat page) {
  105. mPages.addElement(new BookPage(painter, page));
  106. }
  107. /**
  108. * Appends <code>numPages</code> pages to the end of this
  109. * <code>Book</code>. Each of the pages is associated with
  110. * <code>page</code>.
  111. * @param painter the <code>Printable</code> instance that renders
  112. * the page
  113. * @param page the size and orientation of the page
  114. * @param numPages the number of pages to be added to the
  115. * this <code>Book</code>.
  116. * @throws <code>NullPointerException</code>
  117. * If the <code>painter</code> or <code>page</code>
  118. * argument is <code>null</code>
  119. */
  120. public void append(Printable painter, PageFormat page, int numPages) {
  121. BookPage bookPage = new BookPage(painter, page);
  122. int pageIndex = mPages.size();
  123. int newSize = pageIndex + numPages;
  124. mPages.setSize(newSize);
  125. for(int i = pageIndex; i < newSize; i++){
  126. mPages.setElementAt(bookPage, i);
  127. }
  128. }
  129. /**
  130. * Return the BookPage for the page specified by 'pageIndex'.
  131. */
  132. private BookPage getPage(int pageIndex)
  133. throws ArrayIndexOutOfBoundsException
  134. {
  135. return (BookPage) mPages.elementAt(pageIndex);
  136. }
  137. /**
  138. * The BookPage inner class describes an individual
  139. * page in a Book through a PageFormat-Printable pair.
  140. */
  141. private class BookPage {
  142. /**
  143. * The size and orientation of the page.
  144. */
  145. private PageFormat mFormat;
  146. /**
  147. * The instance that will draw the page.
  148. */
  149. private Printable mPainter;
  150. /**
  151. * A new instance where 'format' describes the page's
  152. * size and orientation and 'painter' is the instance
  153. * that will draw the page's graphics.
  154. * @throws NullPointerException
  155. * If the <code>painter</code> or <code>format</code>
  156. * argument is <code>null</code>
  157. */
  158. BookPage(Printable painter, PageFormat format) {
  159. if (painter == null || format == null) {
  160. throw new NullPointerException();
  161. }
  162. mFormat = format;
  163. mPainter = painter;
  164. }
  165. /**
  166. * Return the instance that paints the
  167. * page.
  168. */
  169. Printable getPrintable() {
  170. return mPainter;
  171. }
  172. /**
  173. * Return the format of the page.
  174. */
  175. PageFormat getPageFormat() {
  176. return mFormat;
  177. }
  178. }
  179. }