ServiceUI.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* ServiceUI.java --
  2. Copyright (C) 2006 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package javax.print;
  32. import gnu.javax.print.PrinterDialog;
  33. import java.awt.GraphicsConfiguration;
  34. import java.awt.GraphicsEnvironment;
  35. import java.awt.HeadlessException;
  36. import java.util.Arrays;
  37. import javax.print.attribute.PrintRequestAttributeSet;
  38. /**
  39. * <code>ServiceUI</code> provides a method to create a graphical
  40. * print dialog.
  41. * <p>
  42. * The graphical print dialog enables the user to browse the available
  43. * print services on the system. It provides user interfaces to interact
  44. * with the most common printing attributes likes specifying the number of
  45. * copies to print or the page ranges.
  46. * </p><p>
  47. * The initial appearance of the print dialog as shown to the user may be
  48. * specified by providing the default selected print service as well as
  49. * initial values for the printing attributes in the user interface.
  50. * </p>
  51. *
  52. * @author Wolfgang Baer (WBaer@gmx.de)
  53. */
  54. public class ServiceUI
  55. {
  56. /**
  57. * Default constructor.
  58. */
  59. public ServiceUI()
  60. {
  61. // nothing to do here - only one static method
  62. }
  63. /**
  64. * Creates a modal graphical printing dialog at the specified location on
  65. * the screen.
  66. * <p>
  67. * The dialog will return the user selected print service and the given
  68. * attributes set will contain the modified printing attributes. If the
  69. * user cancels the printing dialog <code>null</code> will be returned and
  70. * the printing attributes set will be unmodified.
  71. * </p><p>
  72. * The values of the given attributes set (if not empty) will be displayed
  73. * initially unless the are unsupported by the print service. If a print
  74. * service does not support a particular value it is substituted with the
  75. * default value of the print service.
  76. * </p>
  77. *
  78. * @param gc the screen to use. <code>null</code> is default screen.
  79. * @param x the coordinate of the upper left edge of the dialog in screen
  80. * coordinates (not relative to the parent frame).
  81. * @param y the coordinate of the upper left edge of the dialog in screen
  82. * coordinates (not relative to the parent frame).
  83. * @param services the print services to browse (not null).
  84. * @param defaultService the default service. If <code>null</code>
  85. * the first of the print services in the services array will be used.
  86. * @param flavor the flavours to be printed.
  87. * @param attributes the attributes requested. Will be updated
  88. * by selections done by the user in the dialog.
  89. *
  90. * @return The selected print service or <code>null</code> if user
  91. * has cancelled the printer dialog.
  92. *
  93. * @throws HeadlessException if GraphicsEnvironment is headless
  94. * @throws IllegalArgumentException if services is <code>null</code> or an
  95. * empty array, attributes are <code>null</code> or the given default
  96. * <code>PrintService<code> is not part of the print service array.
  97. */
  98. public static PrintService printDialog(GraphicsConfiguration gc, int x,
  99. int y, PrintService[] services, PrintService defaultService,
  100. DocFlavor flavor, PrintRequestAttributeSet attributes)
  101. throws HeadlessException
  102. {
  103. if (GraphicsEnvironment.isHeadless())
  104. throw new HeadlessException("GraphicsEnvironment is headless.");
  105. if (services == null || services.length == 0 || attributes == null)
  106. throw new IllegalArgumentException("Given print service array / "
  107. + "attributes may not be null");
  108. if (defaultService != null &&
  109. ! Arrays.asList(services).contains(defaultService))
  110. throw new IllegalArgumentException("defaultService is not contained "
  111. + " in the print service array");
  112. PrinterDialog dialog = new PrinterDialog(gc, services, defaultService,
  113. flavor, attributes);
  114. dialog.setLocation(x, y);
  115. dialog.show();
  116. return dialog.getSelectedPrintService();
  117. }
  118. }