1. /*
  2. * @(#)ServiceUI.java 1.11 04/01/13
  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. * AttributeSet attributes = new PrintRequestHashAttributeSet();
  97. * PrintService service = ServiceUI.printDialog(null, 50, 50,
  98. * services, null,
  99. * attributes);
  100. * if (service != null) {
  101. * ... print ...
  102. * }
  103. * </pre>
  104. * <p>
  105. * @param gc used to select screen. null means primary or default screen.
  106. * @param x location of dialog including border in screen coordinates
  107. * @param y location of dialog including border in screen coordinates
  108. * @param services to be browsable, must be non-null.
  109. * @param defaultService - initial PrintService to display.
  110. * @param flavor - the flavor to be printed, or null.
  111. * @param attributes on input is the initial application supplied
  112. * preferences. This cannot be null but may be empty.
  113. * On output the attributes reflect changes made by the user.
  114. * @return print service selected by the user, or null if the user
  115. * cancelled the dialog.
  116. * @throws HeadlessException if GraphicsEnvironment.isHeadless()
  117. * returns true.
  118. * @throws IllegalArgumentException if services is null or empty,
  119. * or attributes is null, or the initial PrintService is not in the
  120. * list of browsable services.
  121. */
  122. public static PrintService printDialog(GraphicsConfiguration gc,
  123. int x, int y,
  124. PrintService[] services,
  125. PrintService defaultService,
  126. DocFlavor flavor,
  127. PrintRequestAttributeSet attributes)
  128. throws HeadlessException
  129. {
  130. int defaultIndex = -1;
  131. if (GraphicsEnvironment.isHeadless()) {
  132. throw new HeadlessException();
  133. } else if ((services == null) || (services.length == 0)) {
  134. throw new IllegalArgumentException("services must be non-null " +
  135. "and non-empty");
  136. } else if (attributes == null) {
  137. throw new IllegalArgumentException("attributes must be non-null");
  138. }
  139. if (defaultService != null) {
  140. for (int i = 0; i < services.length; i++) {
  141. if (services[i].equals(defaultService)) {
  142. defaultIndex = i;
  143. break;
  144. }
  145. }
  146. if (defaultIndex < 0) {
  147. throw new IllegalArgumentException("services must contain " +
  148. "defaultService");
  149. }
  150. } else {
  151. defaultIndex = 0;
  152. }
  153. boolean newFrame = false;
  154. Window owner =
  155. KeyboardFocusManager.getCurrentKeyboardFocusManager().
  156. getActiveWindow();
  157. if (!(owner instanceof Dialog || owner instanceof Frame)) {
  158. owner = new Frame();
  159. newFrame = true;
  160. }
  161. ServiceDialog dialog;
  162. if (owner instanceof Frame) {
  163. dialog = new ServiceDialog(gc, x, y,
  164. services, defaultIndex,
  165. flavor, attributes,
  166. (Frame)owner);
  167. } else {
  168. dialog = new ServiceDialog(gc, x, y,
  169. services, defaultIndex,
  170. flavor, attributes,
  171. (Dialog)owner);
  172. }
  173. dialog.show();
  174. if (dialog.getStatus() == ServiceDialog.APPROVE) {
  175. PrintRequestAttributeSet newas = dialog.getAttributes();
  176. Class dstCategory = Destination.class;
  177. Class amCategory = SunAlternateMedia.class;
  178. Class fdCategory = Fidelity.class;
  179. if (attributes.containsKey(dstCategory) &&
  180. !newas.containsKey(dstCategory))
  181. {
  182. attributes.remove(dstCategory);
  183. }
  184. if (attributes.containsKey(amCategory) &&
  185. !newas.containsKey(amCategory))
  186. {
  187. attributes.remove(amCategory);
  188. }
  189. attributes.addAll(newas);
  190. Fidelity fd = (Fidelity)attributes.get(fdCategory);
  191. if (fd != null) {
  192. if (fd == Fidelity.FIDELITY_TRUE) {
  193. removeUnsupportedAttributes(dialog.getPrintService(),
  194. flavor, attributes);
  195. }
  196. }
  197. }
  198. if (newFrame) {
  199. owner.dispose();
  200. }
  201. return dialog.getPrintService();
  202. }
  203. /**
  204. * POSSIBLE FUTURE API: This method may be used down the road if we
  205. * decide to allow developers to explicitly display a "page setup" dialog.
  206. * Currently we use that functionality internally for the AWT print model.
  207. */
  208. /*
  209. public static void pageDialog(GraphicsConfiguration gc,
  210. int x, int y,
  211. PrintService service,
  212. DocFlavor flavor,
  213. PrintRequestAttributeSet attributes)
  214. throws HeadlessException
  215. {
  216. if (GraphicsEnvironment.isHeadless()) {
  217. throw new HeadlessException();
  218. } else if (service == null) {
  219. throw new IllegalArgumentException("service must be non-null");
  220. } else if (attributes == null) {
  221. throw new IllegalArgumentException("attributes must be non-null");
  222. }
  223. ServiceDialog dialog = new ServiceDialog(gc, x, y, service,
  224. flavor, attributes);
  225. dialog.show();
  226. if (dialog.getStatus() == ServiceDialog.APPROVE) {
  227. PrintRequestAttributeSet newas = dialog.getAttributes();
  228. Class amCategory = SunAlternateMedia.class;
  229. if (attributes.containsKey(amCategory) &&
  230. !newas.containsKey(amCategory))
  231. {
  232. attributes.remove(amCategory);
  233. }
  234. attributes.addAll(newas.values());
  235. }
  236. }
  237. */
  238. /**
  239. * Removes any attributes from the given AttributeSet that are
  240. * unsupported by the given PrintService/DocFlavor combination.
  241. */
  242. private static void removeUnsupportedAttributes(PrintService ps,
  243. DocFlavor flavor,
  244. AttributeSet aset)
  245. {
  246. AttributeSet asUnsupported = ps.getUnsupportedAttributes(flavor,
  247. aset);
  248. if (asUnsupported != null) {
  249. Attribute[] usAttrs = asUnsupported.toArray();
  250. for (int i=0; i<usAttrs.length; i++) {
  251. Class category = usAttrs[i].getCategory();
  252. if (ps.isAttributeCategorySupported(category)) {
  253. Attribute attr =
  254. (Attribute)ps.getDefaultAttributeValue(category);
  255. if (attr != null) {
  256. aset.add(attr);
  257. } else {
  258. aset.remove(category);
  259. }
  260. } else {
  261. aset.remove(category);
  262. }
  263. }
  264. }
  265. }
  266. }