1. /*
  2. * @(#)ServiceUI.java 1.12 04/01/06
  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.awt.GraphicsConfiguration;
  9. import java.awt.GraphicsEnvironment;
  10. import java.awt.HeadlessException;
  11. import java.awt.Dialog;
  12. import java.awt.Frame;
  13. import java.awt.Window;
  14. import java.awt.KeyboardFocusManager;
  15. import javax.print.attribute.Attribute;
  16. import javax.print.attribute.AttributeSet;
  17. import javax.print.attribute.PrintRequestAttributeSet;
  18. import javax.print.attribute.standard.Destination;
  19. import javax.print.attribute.standard.Fidelity;
  20. import sun.print.ServiceDialog;
  21. import sun.print.SunAlternateMedia;
  22. /** This class is a collection of UI convenience methods which provide a
  23. * graphical user dialog for browsing print services looked up through the Java
  24. * Print Service API.
  25. * <p>
  26. * The dialogs follow a standard pattern of acting as a continue/cancel option
  27. *for a user as well as allowing the user to select the print service to use
  28. *and specify choices such as paper size and number of copies.
  29. * <p>
  30. * <p>
  31. * The dialogs are designed to work with pluggable print services though the
  32. * public APIs of those print services.
  33. * <p>
  34. * If a print service provides any vendor extensions these may be made
  35. * accessible to the user through a vendor supplied tab panel Component.
  36. * Such a vendor extension is encouraged to use Swing! and to support its
  37. * accessibility APIs.
  38. * The vendor extensions should return the settings as part of the
  39. * AttributeSet.
  40. * Applications which want to preserve the user settings should use those
  41. * settings to specify the print job.
  42. * Note that this class is not referenced by any other part of the Java
  43. * Print Service and may not be included in profiles which cannot depend
  44. * on the presence of the AWT packages.
  45. */
  46. public class ServiceUI {
  47. /**
  48. * Presents a dialog to the user for selecting a print service (printer).
  49. * It is displayed at the location specified by the application and
  50. * is modal.
  51. * If the specification is invalid or would make the dialog not visible it
  52. * will be displayed at a location determined by the implementation.
  53. * The dialog blocks its calling thread and is application modal.
  54. * <p>
  55. * The dialog may include a tab panel with custom UI lazily obtained from
  56. * the PrintService's ServiceUIFactory when the PrintService is browsed.
  57. * The dialog will attempt to locate a MAIN_UIROLE first as a JComponent,
  58. * then as a Panel. If there is no ServiceUIFactory or no matching role
  59. * the custom tab will be empty or not visible.
  60. * <p>
  61. * The dialog returns the print service selected by the user if the user
  62. * OK's the dialog and null if the user cancels the dialog.
  63. * <p>
  64. * An application must pass in an array of print services to browse.
  65. * The array must be non-null and non-empty.
  66. * Typically an application will pass in only PrintServices capable
  67. * of printing a particular document flavor.
  68. * <p>
  69. * An application may pass in a PrintService to be initially displayed.
  70. * A non-null parameter must be included in the array of browsable
  71. * services.
  72. * If this parameter is null a service is chosen by the implementation.
  73. * <p>
  74. * An application may optionally pass in the flavor to be printed.
  75. * If this is non-null choices presented to the user can be better
  76. * validated against those supported by the services.
  77. * An application must pass in a PrintRequestAttributeSet for returning
  78. * user choices.
  79. * On calling the PrintRequestAttributeSet may be empty, or may contain
  80. * application-specified values.
  81. * <p>
  82. * These are used to set the initial settings for the initially
  83. * displayed print service. Values which are not supported by the print
  84. * service are ignored. As the user browses print services, attributes
  85. * and values are copied to the new display. If a user browses a
  86. * print service which does not support a particular attribute-value, the
  87. * default for that service is used as the new value to be copied.
  88. * <p>
  89. * If the user cancels the dialog, the returned attributes will not reflect
  90. * any changes made by the user.
  91. *
  92. * A typical basic usage of this method may be :
  93. * <pre>
  94. * PrintService[] services = PrintServiceLookup.lookupPrintServices(
  95. * DocFlavor.INPUT_STREAM.JPEG, null);
  96. * PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
  97. * if (services.length > 0) {
  98. * PrintService service = ServiceUI.printDialog(null, 50, 50,
  99. * services, services[0],
  100. * null,
  101. * attributes);
  102. * if (service != null) {
  103. * ... print ...
  104. * }
  105. * }
  106. * </pre>
  107. * <p>
  108. * @param gc used to select screen. null means primary or default screen.
  109. * @param x location of dialog including border in screen coordinates
  110. * @param y location of dialog including border in screen coordinates
  111. * @param services to be browsable, must be non-null.
  112. * @param defaultService - initial PrintService to display.
  113. * @param flavor - the flavor to be printed, or null.
  114. * @param attributes on input is the initial application supplied
  115. * preferences. This cannot be null but may be empty.
  116. * On output the attributes reflect changes made by the user.
  117. * @return print service selected by the user, or null if the user
  118. * cancelled the dialog.
  119. * @throws HeadlessException if GraphicsEnvironment.isHeadless()
  120. * returns true.
  121. * @throws IllegalArgumentException if services is null or empty,
  122. * or attributes is null, or the initial PrintService is not in the
  123. * list of browsable services.
  124. */
  125. public static PrintService printDialog(GraphicsConfiguration gc,
  126. int x, int y,
  127. PrintService[] services,
  128. PrintService defaultService,
  129. DocFlavor flavor,
  130. PrintRequestAttributeSet attributes)
  131. throws HeadlessException
  132. {
  133. int defaultIndex = -1;
  134. if (GraphicsEnvironment.isHeadless()) {
  135. throw new HeadlessException();
  136. } else if ((services == null) || (services.length == 0)) {
  137. throw new IllegalArgumentException("services must be non-null " +
  138. "and non-empty");
  139. } else if (attributes == null) {
  140. throw new IllegalArgumentException("attributes must be non-null");
  141. }
  142. if (defaultService != null) {
  143. for (int i = 0; i < services.length; i++) {
  144. if (services[i].equals(defaultService)) {
  145. defaultIndex = i;
  146. break;
  147. }
  148. }
  149. if (defaultIndex < 0) {
  150. throw new IllegalArgumentException("services must contain " +
  151. "defaultService");
  152. }
  153. } else {
  154. defaultIndex = 0;
  155. }
  156. boolean newFrame = false;
  157. Window owner =
  158. KeyboardFocusManager.getCurrentKeyboardFocusManager().
  159. getActiveWindow();
  160. if (!(owner instanceof Dialog || owner instanceof Frame)) {
  161. owner = new Frame();
  162. newFrame = true;
  163. }
  164. ServiceDialog dialog;
  165. if (owner instanceof Frame) {
  166. dialog = new ServiceDialog(gc, x, y,
  167. services, defaultIndex,
  168. flavor, attributes,
  169. (Frame)owner);
  170. } else {
  171. dialog = new ServiceDialog(gc, x, y,
  172. services, defaultIndex,
  173. flavor, attributes,
  174. (Dialog)owner);
  175. }
  176. dialog.show();
  177. if (dialog.getStatus() == ServiceDialog.APPROVE) {
  178. PrintRequestAttributeSet newas = dialog.getAttributes();
  179. Class dstCategory = Destination.class;
  180. Class amCategory = SunAlternateMedia.class;
  181. Class fdCategory = Fidelity.class;
  182. if (attributes.containsKey(dstCategory) &&
  183. !newas.containsKey(dstCategory))
  184. {
  185. attributes.remove(dstCategory);
  186. }
  187. if (attributes.containsKey(amCategory) &&
  188. !newas.containsKey(amCategory))
  189. {
  190. attributes.remove(amCategory);
  191. }
  192. attributes.addAll(newas);
  193. Fidelity fd = (Fidelity)attributes.get(fdCategory);
  194. if (fd != null) {
  195. if (fd == Fidelity.FIDELITY_TRUE) {
  196. removeUnsupportedAttributes(dialog.getPrintService(),
  197. flavor, attributes);
  198. }
  199. }
  200. }
  201. if (newFrame) {
  202. owner.dispose();
  203. }
  204. return dialog.getPrintService();
  205. }
  206. /**
  207. * POSSIBLE FUTURE API: This method may be used down the road if we
  208. * decide to allow developers to explicitly display a "page setup" dialog.
  209. * Currently we use that functionality internally for the AWT print model.
  210. */
  211. /*
  212. public static void pageDialog(GraphicsConfiguration gc,
  213. int x, int y,
  214. PrintService service,
  215. DocFlavor flavor,
  216. PrintRequestAttributeSet attributes)
  217. throws HeadlessException
  218. {
  219. if (GraphicsEnvironment.isHeadless()) {
  220. throw new HeadlessException();
  221. } else if (service == null) {
  222. throw new IllegalArgumentException("service must be non-null");
  223. } else if (attributes == null) {
  224. throw new IllegalArgumentException("attributes must be non-null");
  225. }
  226. ServiceDialog dialog = new ServiceDialog(gc, x, y, service,
  227. flavor, attributes);
  228. dialog.show();
  229. if (dialog.getStatus() == ServiceDialog.APPROVE) {
  230. PrintRequestAttributeSet newas = dialog.getAttributes();
  231. Class amCategory = SunAlternateMedia.class;
  232. if (attributes.containsKey(amCategory) &&
  233. !newas.containsKey(amCategory))
  234. {
  235. attributes.remove(amCategory);
  236. }
  237. attributes.addAll(newas.values());
  238. }
  239. dialog.getOwner().dispose();
  240. }
  241. */
  242. /**
  243. * Removes any attributes from the given AttributeSet that are
  244. * unsupported by the given PrintService/DocFlavor combination.
  245. */
  246. private static void removeUnsupportedAttributes(PrintService ps,
  247. DocFlavor flavor,
  248. AttributeSet aset)
  249. {
  250. AttributeSet asUnsupported = ps.getUnsupportedAttributes(flavor,
  251. aset);
  252. if (asUnsupported != null) {
  253. Attribute[] usAttrs = asUnsupported.toArray();
  254. for (int i=0; i<usAttrs.length; i++) {
  255. Class category = usAttrs[i].getCategory();
  256. if (ps.isAttributeCategorySupported(category)) {
  257. Attribute attr =
  258. (Attribute)ps.getDefaultAttributeValue(category);
  259. if (attr != null) {
  260. aset.add(attr);
  261. } else {
  262. aset.remove(category);
  263. }
  264. } else {
  265. aset.remove(category);
  266. }
  267. }
  268. }
  269. }
  270. }