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