1. /*
  2. * @(#)Printable.java 1.17 04/01/28
  3. *
  4. * Copyright 2004 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.awt.Graphics;
  9. /**
  10. * The <code>Printable</code> interface is implemented
  11. * by the <code>print</code> methods of the current
  12. * page painter, which is called by the printing
  13. * system to render a page. When building a
  14. * {@link Pageable}, pairs of {@link PageFormat}
  15. * instances and instances that implement
  16. * this interface are used to describe each page. The
  17. * instance implementing <code>Printable</code> is called to
  18. * print the page's graphics.
  19. * <p>
  20. * A <code>Printable(..)</code> may be set on a <code>PrinterJob</code>.
  21. * When the client subsequently initiates printing by calling
  22. * <code>PrinterJob.print(..)</code> control
  23. * <p>
  24. * is handed to the printing system until all pages have been printed.
  25. * It does this by calling <code>Printable.print(..)</code> until
  26. * all pages in the document have been printed.
  27. * In using the <code>Printable</code> interface the printing
  28. * commits to image the contents of a page whenever
  29. * requested by the printing system.
  30. * <p>
  31. * The parameters to <code>Printable.print(..)</code> include a
  32. * <code>PageFormat</code> which describes the printable area of
  33. * the page, needed for calculating the contents that will fit the
  34. * page, and the page index, which specifies the zero-based print
  35. * stream index of the requested page.
  36. * <p>
  37. * For correct printing behaviour, the following points should be
  38. * observed:
  39. * <ul>
  40. * <li> The printing system may request a page index more than once.
  41. * On each occasion equal PageFormat parameters will be supplied.
  42. *
  43. * <li>The printing system will call <code>Printable.print(..)</code>
  44. * with page indexes which increase monotonically, although as noted above,
  45. * the <code>Printable</code> should expect multiple calls for a page index
  46. * and that page indexes may be skipped, when page ranges are specified
  47. * by the client, or by a user through a print dialog.
  48. *
  49. * <li>If multiple collated copies of a document are requested, and the
  50. * printer cannot natively support this, then the document may be imaged
  51. * multiple times. Printing will start each copy from the lowest print
  52. * stream page index page.
  53. *
  54. * <li>With the exception of re-imaging an entire document for multiple
  55. * collated copies, the increasing page index order means that when
  56. * page N is requested if a client needs to calculate page break position,
  57. * it may safely discard any state related to pages < N, and make current
  58. * that for page N. "State" usually is just the calculated position in the
  59. * document that corresponds to the start of the page.
  60. *
  61. * <li>When called by the printing system the <code>Printable</code> must
  62. * inspect and honour the supplied PageFormat parameter as well as the
  63. * page index.
  64. * This is key to correct printing behaviour, and it has the
  65. * implication that the client has the responsibility of tracking
  66. * what content belongs on the specified page.
  67. *
  68. * <li>When the <code>Printable</code> is obtained from a client-supplied
  69. * <code>Pageable</code> then the client may provide different PageFormats
  70. * for each page index. Calculations of page breaks must account for this.
  71. * </ul>
  72. * <p>
  73. * @see java.awt.print.Pageable
  74. * @see java.awt.print.PageFormat
  75. * @see java.awt.print.PrinterJob
  76. */
  77. public interface Printable {
  78. /**
  79. * Returned from {@link #print(Graphics, PageFormat, int)}
  80. * to signify that the requested page was rendered.
  81. */
  82. int PAGE_EXISTS = 0;
  83. /**
  84. * Returned from <code>print</code> to signify that the
  85. * <code>pageIndex</code> is too large and that the requested page
  86. * does not exist.
  87. */
  88. int NO_SUCH_PAGE = 1;
  89. /**
  90. * Prints the page at the specified index into the specified
  91. * {@link Graphics} context in the specified
  92. * format. A <code>PrinterJob</code> calls the
  93. * <code>Printable</code> interface to request that a page be
  94. * rendered into the context specified by
  95. * <code>graphics</code>. The format of the page to be drawn is
  96. * specified by <code>pageFormat</code>. The zero based index
  97. * of the requested page is specified by <code>pageIndex</code>.
  98. * If the requested page does not exist then this method returns
  99. * NO_SUCH_PAGE; otherwise PAGE_EXISTS is returned.
  100. * The <code>Graphics</code> class or subclass implements the
  101. * {@link PrinterGraphics} interface to provide additional
  102. * information. If the <code>Printable</code> object
  103. * aborts the print job then it throws a {@link PrinterException}.
  104. * @param graphics the context into which the page is drawn
  105. * @param pageFormat the size and orientation of the page being drawn
  106. * @param pageIndex the zero based index of the page to be drawn
  107. * @return PAGE_EXISTS if the page is rendered successfully
  108. * or NO_SUCH_PAGE if <code>pageIndex</code> specifies a
  109. * non-existent page.
  110. * @exception java.awt.print.PrinterException
  111. * thrown when the print job is terminated.
  112. */
  113. int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
  114. throws PrinterException;
  115. }