1. /*
  2. * @(#)PrintService.java 1.11 04/05/05
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.print;
  8. import java.util.Locale;
  9. import javax.print.attribute.Attribute;
  10. import javax.print.attribute.AttributeSet;
  11. import javax.print.attribute.PrintServiceAttribute;
  12. import javax.print.attribute.PrintServiceAttributeSet;
  13. import javax.print.event.PrintServiceAttributeListener;
  14. /**
  15. * Interface PrintService is the factory for a DocPrintJob. A PrintService
  16. * describes the capabilities of a Printer and can be queried regarding
  17. * a printer's supported attributes.
  18. * <P>
  19. * Example:
  20. * <PRE>
  21. * DocFlavor flavor = DocFlavor.INPUT_STREAM.POSTSCRIPT;
  22. * PrintRequestAttributeSet aset = new HashPrintRequestHashAttributeSet();
  23. * aset.add(MediaSizeName.ISO_A4);
  24. * PrintService[] pservices =
  25. * PrintServiceLookup.lookupPrintServices(flavor, aset);
  26. * if (pservices.length > 0) {
  27. * DocPrintJob pj = pservices[0].createPrintJob();
  28. * // InputStreamDoc is an implementation of the Doc interface //
  29. * Doc doc = new InputStreamDoc("test.ps", flavor);
  30. * try {
  31. * pj.print(doc, aset);
  32. * } catch (PrintException e) {
  33. * }
  34. * }
  35. * </PRE>
  36. */
  37. public interface PrintService {
  38. /** Returns a String name for this print service which may be used
  39. * by applications to request a particular print service.
  40. * In a suitable context, such as a name service, this name must be
  41. * unique.
  42. * In some environments this unique name may be the same as the user
  43. * friendly printer name defined as the
  44. * {@link javax.print.attribute.standard.PrinterName PrinterName}
  45. * attribute.
  46. * @return name of the service.
  47. */
  48. public String getName();
  49. /**
  50. * Creates and returns a PrintJob capable of handling data from
  51. * any of the supported document flavors.
  52. * @return a DocPrintJob object
  53. */
  54. public DocPrintJob createPrintJob();
  55. /**
  56. * Registers a listener for events on this PrintService.
  57. * @param listener a PrintServiceAttributeListener, which
  58. * monitors the status of a print service
  59. * @see #removePrintServiceAttributeListener
  60. */
  61. public void addPrintServiceAttributeListener(
  62. PrintServiceAttributeListener listener);
  63. /**
  64. * Removes the print-service listener from this print service.
  65. * This means the listener is no longer interested in
  66. * <code>PrintService</code> events.
  67. * @param listener a PrintServiceAttributeListener object
  68. * @see #addPrintServiceAttributeListener
  69. */
  70. public void removePrintServiceAttributeListener(
  71. PrintServiceAttributeListener listener);
  72. /**
  73. * Obtains this print service's set of printer description attributes
  74. * giving this Print Service's status. The returned attribute set object
  75. * is unmodifiable. The returned attribute set object is a "snapshot" of
  76. * this Print Service's attribute set at the time of the
  77. * <CODE>getAttributes()</CODE> method call: that is, the returned
  78. * attribute set's contents will <I>not</I> be updated if this print
  79. * service's attribute set's contents change in the future. To detect
  80. * changes in attribute values, call <CODE>getAttributes()</CODE> again
  81. * and compare the new attribute set to the previous attribute set;
  82. * alternatively, register a listener for print service events.
  83. *
  84. * @return Unmodifiable snapshot of this Print Service's attribute set.
  85. * May be empty, but not null.
  86. */
  87. public PrintServiceAttributeSet getAttributes();
  88. /**
  89. * Gets the value of the single specified service attribute.
  90. * This may be useful to clients which only need the value of one
  91. * attribute and want to minimise overhead.
  92. * @param category the category of a PrintServiceAttribute supported
  93. * by this service - may not be null.
  94. * @return the value of the supported attribute or null if the
  95. * attribute is not supported by this service.
  96. * @exception NullPointerException if the category is null.
  97. * @exception IllegalArgumentException
  98. * (unchecked exception) if <CODE>category</CODE> is not a
  99. * <code>Class</code> that implements interface
  100. *{@link javax.print.attribute.PrintServiceAttribute PrintServiceAttribute}.
  101. */
  102. public <T extends PrintServiceAttribute>
  103. T getAttribute(Class<T> category);
  104. /**
  105. * Determines the print data formats a client can specify when setting
  106. * up a job for this <code>PrintService</code>. A print data format is
  107. * designated by a "doc
  108. * flavor" (class {@link javax.print.DocFlavor DocFlavor})
  109. * consisting of a MIME type plus a print data representation class.
  110. * <P>
  111. * Note that some doc flavors may not be supported in combination
  112. * with all attributes. Use <code>getUnsupportedAttributes(..)</code>
  113. * to validate specific combinations.
  114. *
  115. * @return Array of supported doc flavors, should have at least
  116. * one element.
  117. *
  118. */
  119. public DocFlavor[] getSupportedDocFlavors();
  120. /**
  121. * Determines if this print service supports a specific
  122. * <code>DocFlavor</code>. This is a convenience method to determine
  123. * if the <code>DocFlavor</code> would be a member of the result of
  124. * <code>getSupportedDocFlavors()</code>.
  125. * <p>
  126. * Note that some doc flavors may not be supported in combination
  127. * with all attributes. Use <code>getUnsupportedAttributes(..)</code>
  128. * to validate specific combinations.
  129. *
  130. * @param flavor the <code>DocFlavor</code>to query for support.
  131. * @return <code>true</code> if this print service supports the
  132. * specified <code>DocFlavor</code> <code>false</code> otherwise.
  133. * @exception NullPointerException
  134. * (unchecked exception) Thrown if <CODE>flavor</CODE> is null.
  135. */
  136. public boolean isDocFlavorSupported(DocFlavor flavor);
  137. /**
  138. * Determines the printing attribute categories a client can specify
  139. * when setting up a job for this print service.
  140. * A printing attribute category is
  141. * designated by a <code>Class</code> that implements interface
  142. * {@link javax.print.attribute.Attribute Attribute}. This method returns
  143. * just the attribute <I>categories</I> that are supported; it does not
  144. * return the particular attribute <I>values</I> that are supported.
  145. * <P>
  146. * This method returns all the printing attribute
  147. * categories this print service supports for any possible job.
  148. * Some categories may not be supported in a particular context (ie
  149. * for a particular <code>DocFlavor</code>).
  150. * Use one of the methods that include a <code>DocFlavor</code> to
  151. * validate the request before submitting it, such as
  152. * <code>getSupportedAttributeValues(..)</code>.
  153. *
  154. * @return Array of printing attribute categories that the client can
  155. * specify as a doc-level or job-level attribute in a Print
  156. * Request. Each element in the array is a {@link java.lang.Class
  157. * Class} that implements interface {@link
  158. * javax.print.attribute.Attribute Attribute}.
  159. * The array is empty if no categories are supported.
  160. */
  161. public Class<?>[] getSupportedAttributeCategories();
  162. /**
  163. * Determines whether a client can specify the given printing
  164. * attribute category when setting up a job for this print service. A
  165. * printing attribute category is designated by a <code>Class</code>
  166. * that implements interface {@link javax.print.attribute.Attribute
  167. * Attribute}. This method tells whether the attribute <I>category</I> is
  168. * supported; it does not tell whether a particular attribute <I>value</I>
  169. * is supported.
  170. * <p>
  171. * Some categories may not be supported in a particular context (ie
  172. * for a particular <code>DocFlavor</code>).
  173. * Use one of the methods which include a <code>DocFlavor</code> to
  174. * validate the request before submitting it, such as
  175. * <code>getSupportedAttributeValues(..)</code>.
  176. * <P>
  177. * This is a convenience method to determine if the category
  178. * would be a member of the result of
  179. * <code>getSupportedAttributeCategories()</code>.
  180. *
  181. * @param category Printing attribute category to test. It must be a
  182. * <code>Class</code> that implements
  183. * interface
  184. * {@link javax.print.attribute.Attribute Attribute}.
  185. *
  186. * @return <code>true</code> if this print service supports
  187. * specifying a doc-level or
  188. * job-level attribute in <CODE>category</CODE> in a Print
  189. * Request; <code>false</code> if it doesn't.
  190. *
  191. * @exception NullPointerException
  192. * (unchecked exception) Thrown if <CODE>category</CODE> is null.
  193. * @exception IllegalArgumentException
  194. * (unchecked exception) Thrown if <CODE>category</CODE> is not a
  195. * <code>Class</code> that implements interface
  196. * {@link javax.print.attribute.Attribute Attribute}.
  197. */
  198. public boolean
  199. isAttributeCategorySupported(Class<? extends Attribute> category);
  200. /**
  201. * Determines this print service's default printing attribute value in
  202. * the given category. A printing attribute value is an instance of
  203. * a class that implements interface
  204. * {@link javax.print.attribute.Attribute Attribute}. If a client sets
  205. * up a print job and does not specify any attribute value in the
  206. * given category, this Print Service will use the
  207. * default attribute value instead.
  208. * <p>
  209. * Some attributes may not be supported in a particular context (ie
  210. * for a particular <code>DocFlavor</code>).
  211. * Use one of the methods that include a <code>DocFlavor</code> to
  212. * validate the request before submitting it, such as
  213. * <code>getSupportedAttributeValues(..)</code>.
  214. * <P>
  215. * Not all attributes have a default value. For example the
  216. * service will not have a defaultvalue for <code>RequestingUser</code>
  217. * i.e. a null return for a supported category means there is no
  218. * service default value for that category. Use the
  219. * <code>isAttributeCategorySupported(Class)</code> method to
  220. * distinguish these cases.
  221. *
  222. * @param category Printing attribute category for which the default
  223. * attribute value is requested. It must be a {@link
  224. * java.lang.Class Class} that implements interface
  225. * {@link javax.print.attribute.Attribute
  226. * Attribute}.
  227. *
  228. * @return Default attribute value for <CODE>category</CODE>, or null
  229. * if this Print Service does not support specifying a doc-level or
  230. * job-level attribute in <CODE>category</CODE> in a Print
  231. * Request, or the service does not have a default value
  232. * for this attribute.
  233. *
  234. * @exception NullPointerException
  235. * (unchecked exception) Thrown if <CODE>category</CODE> is null.
  236. * @exception IllegalArgumentException
  237. * (unchecked exception) Thrown if <CODE>category</CODE> is not a
  238. * {@link java.lang.Class Class} that implements interface {@link
  239. * javax.print.attribute.Attribute Attribute}.
  240. */
  241. public Object
  242. getDefaultAttributeValue(Class<? extends Attribute> category);
  243. /**
  244. * Determines the printing attribute values a client can specify in
  245. * the given category when setting up a job for this print service. A
  246. * printing
  247. * attribute value is an instance of a class that implements interface
  248. * {@link javax.print.attribute.Attribute Attribute}.
  249. * <P>
  250. * If <CODE>flavor</CODE> is null and <CODE>attributes</CODE> is null
  251. * or is an empty set, this method returns all the printing attribute
  252. * values this Print Service supports for any possible job. If
  253. * <CODE>flavor</CODE> is not null or <CODE>attributes</CODE> is not
  254. * an empty set, this method returns just the printing attribute values
  255. * that are compatible with the given doc flavor and/or set of attributes.
  256. * That is, a null return value may indicate that specifying this attribute
  257. * is incompatible with the specified DocFlavor.
  258. * Also if DocFlavor is not null it must be a flavor supported by this
  259. * PrintService, else IllegalArgumentException will be thrown.
  260. * <P>
  261. * If the <code>attributes</code> parameter contains an Attribute whose
  262. * category is the same as the <code>category</code> parameter, the service
  263. * must ignore this attribute in the AttributeSet.
  264. * <p>
  265. * <code>DocAttribute</code>s which are to be specified on the
  266. * <code>Doc</code> must be included in this set to accurately
  267. * represent the context.
  268. * <p>
  269. * This method returns an Object because different printing attribute
  270. * categories indicate the supported attribute values in different ways.
  271. * The documentation for each printing attribute in package {@link
  272. * javax.print.attribute.standard javax.print.attribute.standard}
  273. * describes how each attribute indicates its supported values. Possible
  274. * ways of indicating support include:
  275. * <UL>
  276. * <LI>
  277. * Return a single instance of the attribute category to indicate that any
  278. * value is legal -- used, for example, by an attribute whose value is an
  279. * arbitrary text string. (The value of the returned attribute object is
  280. * irrelevant.)
  281. * <LI>
  282. * Return an array of one or more instances of the attribute category,
  283. * containing the legal values -- used, for example, by an attribute with
  284. * a list of enumerated values. The type of the array is an array of the
  285. * specified attribute category type as returned by its
  286. * <code>getCategory(Class)</code>.
  287. * <LI>
  288. * Return a single object (of some class other than the attribute category)
  289. * that indicates bounds on the legal values -- used, for example, by an
  290. * integer-valued attribute that must lie within a certain range.
  291. * </UL>
  292. * <P>
  293. *
  294. * @param category Printing attribute category to test. It must be a
  295. * {@link java.lang.Class Class} that implements
  296. * interface {@link
  297. * javax.print.attribute.Attribute Attribute}.
  298. * @param flavor Doc flavor for a supposed job, or null.
  299. * @param attributes Set of printing attributes for a supposed job
  300. * (both job-level attributes and document-level
  301. * attributes), or null.
  302. *
  303. * @return Object indicating supported values for <CODE>category</CODE>,
  304. * or null if this Print Service does not support specifying a
  305. * doc-level or job-level attribute in <CODE>category</CODE> in
  306. * a Print Request.
  307. *
  308. * @exception NullPointerException
  309. * (unchecked exception) Thrown if <CODE>category</CODE> is null.
  310. * @exception IllegalArgumentException
  311. * (unchecked exception) Thrown if <CODE>category</CODE> is not a
  312. * {@link java.lang.Class Class} that implements interface {@link
  313. * javax.print.attribute.Attribute Attribute}, or
  314. * <code>DocFlavor</code> is not supported by this service.
  315. */
  316. public Object
  317. getSupportedAttributeValues(Class<? extends Attribute> category,
  318. DocFlavor flavor,
  319. AttributeSet attributes);
  320. /**
  321. * Determines whether a client can specify the given printing
  322. * attribute
  323. * value when setting up a job for this Print Service. A printing
  324. * attribute value is an instance of a class that implements interface
  325. * {@link javax.print.attribute.Attribute Attribute}.
  326. * <P>
  327. * If <CODE>flavor</CODE> is null and <CODE>attributes</CODE> is null or
  328. * is an empty set, this method tells whether this Print Service supports
  329. * the given printing attribute value for some possible combination of doc
  330. * flavor and set of attributes. If <CODE>flavor</CODE> is not null or
  331. * <CODE>attributes</CODE> is not an empty set, this method tells whether
  332. * this Print Service supports the given printing attribute value in
  333. * combination with the given doc flavor and/or set of attributes.
  334. * <p>
  335. * Also if DocFlavor is not null it must be a flavor supported by this
  336. * PrintService, else IllegalArgumentException will be thrown.
  337. * <p>
  338. * <code>DocAttribute</code>s which are to be specified on the
  339. * <code>Doc</code> must be included in this set to accurately
  340. * represent the context.
  341. * <p>
  342. * This is a convenience method to determine if the value
  343. * would be a member of the result of
  344. * <code>getSupportedAttributeValues(...)</code>.
  345. *
  346. * @param attrval Printing attribute value to test.
  347. * @param flavor Doc flavor for a supposed job, or null.
  348. * @param attributes Set of printing attributes for a supposed job
  349. * (both job-level attributes and document-level
  350. * attributes), or null.
  351. *
  352. * @return True if this Print Service supports specifying
  353. * <CODE>attrval</CODE> as a doc-level or job-level attribute in a
  354. * Print Request, false if it doesn't.
  355. *
  356. * @exception NullPointerException
  357. * (unchecked exception) if <CODE>attrval</CODE> is null.
  358. * @exception IllegalArgumentException if flavor is not supported by
  359. * this PrintService.
  360. */
  361. public boolean isAttributeValueSupported(Attribute attrval,
  362. DocFlavor flavor,
  363. AttributeSet attributes);
  364. /**
  365. * Identifies the attributes that are unsupported for a print request
  366. * in the context of a particular DocFlavor.
  367. * This method is useful for validating a potential print job and
  368. * identifying the specific attributes which cannot be supported.
  369. * It is important to supply only a supported DocFlavor or an
  370. * IllegalArgumentException will be thrown. If the
  371. * return value from this method is null, all attributes are supported.
  372. * <p>
  373. * <code>DocAttribute</code>s which are to be specified on the
  374. * <code>Doc</code> must be included in this set to accurately
  375. * represent the context.
  376. * <p>
  377. * If the return value is non-null, all attributes in the returned
  378. * set are unsupported with this DocFlavor. The returned set does not
  379. * distinguish attribute categories that are unsupported from
  380. * unsupported attribute values.
  381. * <p>
  382. * A supported print request can then be created by removing
  383. * all unsupported attributes from the original attribute set,
  384. * except in the case that the DocFlavor is unsupported.
  385. * <p>
  386. * If any attributes are unsupported only because they are in conflict
  387. * with other attributes then it is at the discretion of the service
  388. * to select the attribute(s) to be identified as the cause of the
  389. * conflict.
  390. * <p>
  391. * Use <code>isDocFlavorSupported()</code> to verify that a DocFlavor
  392. * is supported before calling this method.
  393. *
  394. * @param flavor Doc flavor to test, or null
  395. * @param attributes Set of printing attributes for a supposed job
  396. * (both job-level attributes and document-level
  397. * attributes), or null.
  398. *
  399. * @return null if this Print Service supports the print request
  400. * specification, else the unsupported attributes.
  401. *
  402. * @exception IllegalArgumentException if<CODE>flavor</CODE> is
  403. * not supported by this PrintService.
  404. */
  405. public AttributeSet getUnsupportedAttributes(DocFlavor flavor,
  406. AttributeSet attributes);
  407. /**
  408. * Returns a factory for UI components which allow users to interact
  409. * with the service in various roles.
  410. * Services which do not provide any UI should return null.
  411. * Print Services which do provide UI but want to be supported in
  412. * an environment with no UI support should ensure that the factory
  413. * is not initialised unless the application calls this method to
  414. * obtain the factory.
  415. * See <code>ServiceUIFactory</code> for more information.
  416. * @return null or a factory for UI components.
  417. */
  418. public ServiceUIFactory getServiceUIFactory();
  419. /**
  420. * Determines if two services are referring to the same underlying
  421. * service. Objects encapsulating a print service may not exhibit
  422. * equality of reference even though they refer to the same underlying
  423. * service.
  424. * <p>
  425. * Clients should call this method to determine if two services are
  426. * referring to the same underlying service.
  427. * <p>
  428. * Services must implement this method and return true only if the
  429. * service objects being compared may be used interchangeably by the
  430. * client.
  431. * Services are free to return the same object reference to an underlying
  432. * service if that, but clients must not depend on equality of reference.
  433. * @param obj the reference object with which to compare.
  434. * @return true if this service is the same as the obj argument,
  435. * false otherwise.
  436. */
  437. public boolean equals(Object obj);
  438. /**
  439. * This method should be implemented consistently with
  440. * <code>equals(Object)</code>.
  441. * @return hash code of this object.
  442. */
  443. public int hashCode();
  444. }