1. /*
  2. * @(#)PrinterJob.java 1.18 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.awt.AWTError;
  9. import java.util.Enumeration;
  10. import sun.security.action.GetPropertyAction;
  11. /**
  12. * The <code>PrinterJob</code> class is the principal class that controls
  13. * printing. An application calls methods in this class to set up a job,
  14. * optionally to invoke a print dialog with the user, and then to print
  15. * the pages of the job.
  16. */
  17. public abstract class PrinterJob {
  18. /* Public Class Methods */
  19. /**
  20. * Creates and returns a <code>PrinterJob</code>.
  21. * @return a new <code>PrinterJob</code>.
  22. */
  23. public static PrinterJob getPrinterJob() {
  24. SecurityManager security = System.getSecurityManager();
  25. if (security != null) {
  26. security.checkPrintJobAccess();
  27. }
  28. return (PrinterJob) java.security.AccessController.doPrivileged(
  29. new java.security.PrivilegedAction() {
  30. public Object run() {
  31. String nm = System.getProperty("java.awt.printerjob", null);
  32. try {
  33. return (PrinterJob)Class.forName(nm).newInstance();
  34. } catch (ClassNotFoundException e) {
  35. throw new AWTError("PrinterJob not found: " + nm);
  36. } catch (InstantiationException e) {
  37. throw new AWTError("Could not instantiate PrinterJob: " + nm);
  38. } catch (IllegalAccessException e) {
  39. throw new AWTError("Could not access PrinterJob: " + nm);
  40. }
  41. }
  42. });
  43. }
  44. /* Public Methods */
  45. /**
  46. * A <code>PrinterJob</code> object should be created using the
  47. * static {@link #getPrinterJob() <code>getPrinterJob</code>} method.
  48. */
  49. public PrinterJob() {
  50. }
  51. /**
  52. * Calls <code>painter</code> to render the pages. The pages in the
  53. * document to be printed by this
  54. * <code>PrinterJob</code> are rendered by the {@link Printable}
  55. * object, <code>painter</code>. The {@link PageFormat} for each page
  56. * is the default page format.
  57. * @param painter the <code>Printable</code> that renders each page of
  58. * the document.
  59. */
  60. public abstract void setPrintable(Printable painter);
  61. /**
  62. * Calls <code>painter</code> to render the pages in the specified
  63. * <code>format</code>. The pages in the document to be printed by
  64. * this <code>PrinterJob</code> are rendered by the
  65. * <code>Printable</code> object, <code>painter</code>. The
  66. * <code>PageFormat</code> of each page is <code>format</code>.
  67. * @param painter the <code>Printable</code> called to render
  68. * each page of the document
  69. * @param format the size and orientation of each page to
  70. * be printed
  71. */
  72. public abstract void setPrintable(Printable painter, PageFormat format);
  73. /**
  74. * Queries <code>document</code> for the number of pages and
  75. * the <code>PageFormat</code> and <code>Printable</code> for each
  76. * page held in the <code>Pageable</code> instance,
  77. * <code>document</code>.
  78. * @param document the pages to be printed. It can not be
  79. * <code>null</code>.
  80. * @exception NullPointerException the <code>Pageable</code> passed in
  81. * was <code>null</code>.
  82. * @see PageFormat
  83. * @see Printable
  84. */
  85. public abstract void setPageable(Pageable document)
  86. throws NullPointerException;
  87. /**
  88. * Presents a dialog to the user for changing the properties of
  89. * the print job.
  90. * @return <code>true</code> if the user does not cancel the dialog;
  91. * <code>false</code> otherwise.
  92. */
  93. public abstract boolean printDialog();
  94. /**
  95. * Displays a dialog that allows modification of a
  96. * <code>PageFormat</code> instance.
  97. * The <code>page</code> argument is used to initialize controls
  98. * in the page setup dialog.
  99. * If the user cancels the dialog then this method returns the
  100. * original <code>page</code> object unmodified.
  101. * If the user okays the dialog then this method returns a new
  102. * <code>PageFormat</code> object with the indicated changes.
  103. * In either case, the original <code>page</code> object is
  104. * not modified.
  105. * @param page the default <code>PageFormat</code> presented to the
  106. * user for modification
  107. * @return the original <code>page</code> object if the dialog
  108. * is cancelled; a new <code>PageFormat</code> object
  109. * containing the format indicated by the user if the
  110. * dialog is acknowledged.
  111. * @since JDK1.2
  112. */
  113. public abstract PageFormat pageDialog(PageFormat page);
  114. /**
  115. * Clones the <code>PageFormat</code> argument and alters the
  116. * clone to describe a default page size and orientation.
  117. * @param page the <code>PageFormat</code> to be cloned and altered
  118. * @return clone of <code>page</code>, altered to describe a default
  119. * <code>PageFormat</code>.
  120. */
  121. public abstract PageFormat defaultPage(PageFormat page);
  122. /**
  123. * Creates a new <code>PageFormat</code> instance and
  124. * sets it to a default size and orientation.
  125. * @return a <code>PageFormat</code> set to a default size and
  126. * orientation.
  127. */
  128. public PageFormat defaultPage() {
  129. return defaultPage(new PageFormat());
  130. }
  131. /**
  132. * Alters the <code>PageFormat</code> argument to be usable on
  133. * this <code>PrinterJob</code> object's current printer.
  134. * @param page this page description is cloned and then its settings
  135. * are altered to be usuable for this
  136. * <code>PrinterJob</code>
  137. * @return a <code>PageFormat</code> that is cloned from
  138. * the <code>PageFormat</code> parameter and altered
  139. * to conform with this <code>PrinterJob</code>.
  140. */
  141. public abstract PageFormat validatePage(PageFormat page);
  142. /**
  143. * Prints a set of pages.
  144. * @exception PrinterException an error in the print system
  145. * caused the job to be aborted.
  146. * @see Book
  147. * @see Pageable
  148. * @see Printable
  149. */
  150. public abstract void print() throws PrinterException;
  151. /**
  152. * Sets the number of copies to be printed.
  153. * @param copies the number of copies to be printed
  154. */
  155. public abstract void setCopies(int copies);
  156. /**
  157. * Gets the number of copies to be printed.
  158. * @return the number of copies to be printed.
  159. */
  160. public abstract int getCopies();
  161. /**
  162. * Gets the name of the printing user.
  163. * @return the name of the printing user
  164. */
  165. public abstract String getUserName();
  166. /**
  167. * Sets the name of the document to be printed.
  168. * The document name can not be <code>null</code>.
  169. * @param jobName the name of the document to be printed
  170. */
  171. public abstract void setJobName(String jobName);
  172. /**
  173. * Gets the name of the document to be printed.
  174. * @return the name of the document to be printed.
  175. */
  176. public abstract String getJobName();
  177. /**
  178. * Cancels a print job that is in progress. If
  179. * {@link #print() print} has been called but has not
  180. * returned then this method signals
  181. * that the job should be cancelled at the next
  182. * chance. If there is no print job in progress then
  183. * this call does nothing.
  184. */
  185. public abstract void cancel();
  186. /**
  187. * Returns <code>true</code> if a print job is
  188. * in progress, but is going to be cancelled
  189. * at the next opportunity; otherwise returns
  190. * <code>false</code>.
  191. * @return <code>true</code> if the job in progress
  192. * is going to be cancelled; <code>false</code> otherwise.
  193. */
  194. public abstract boolean isCancelled();
  195. }