KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > JobAttributes


1 /*
2  * @(#)JobAttributes.java 1.9 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.awt;
9
10 /**
11  * A set of attributes which control a print job.
12  * <p>
13  * Instances of this class control the number of copies, default selection,
14  * destination, print dialog, file and printer names, page ranges, multiple
15  * document handling (including collation), and multi-page imposition (such
16  * as duplex) of every print job which uses the instance. Attribute names are
17  * compliant with the Internet Printing Protocol (IPP) 1.1 where possible.
18  * Attribute values are partially compliant where possible.
19  * <p>
20  * To use a method which takes an inner class type, pass a reference to
21  * one of the constant fields of the inner class. Client code cannot create
22  * new instances of the inner class types because none of those classes
23  * has a public constructor. For example, to set the print dialog type to
24  * the cross-platform, pure Java print dialog, use the following code:
25  * <pre>
26  * import java.awt.JobAttributes;
27  *
28  * public class PureJavaPrintDialogExample {
29  * public void setPureJavaPrintDialog(JobAttributes jobAttributes) {
30  * jobAttributes.setDialog(JobAttributes.DialogType.COMMON);
31  * }
32  * }
33  * </pre>
34  * <p>
35  * Every IPP attribute which supports an <i>attributeName</i>-default value
36  * has a corresponding <code>set<i>attributeName</i>ToDefault</code> method.
37  * Default value fields are not provided.
38  *
39  * @version 1.9, 12/19/03
40  * @author David Mendenhall
41  */

42 public final class JobAttributes implements Cloneable JavaDoc {
43     /**
44      * A type-safe enumeration of possible default selection states.
45      */

46     public static final class DefaultSelectionType extends AttributeValue JavaDoc {
47         private static final int I_ALL = 0;
48         private static final int I_RANGE = 1;
49         private static final int I_SELECTION = 2;
50
51         private static final String JavaDoc NAMES[] = {
52         "all", "range", "selection"
53     };
54
55         /**
56      * The <code>DefaultSelectionType</code> instance to use for
57          * specifying that all pages of the job should be printed.
58      */

59         public static final DefaultSelectionType ALL =
60            new DefaultSelectionType(I_ALL);
61         /**
62      * The <code>DefaultSelectionType</code> instance to use for
63          * specifying that a range of pages of the job should be printed.
64      */

65         public static final DefaultSelectionType RANGE =
66            new DefaultSelectionType(I_RANGE);
67         /**
68      * The <code>DefaultSelectionType</code> instance to use for
69          * specifying that the current selection should be printed.
70      */

71         public static final DefaultSelectionType SELECTION =
72            new DefaultSelectionType(I_SELECTION);
73
74         private DefaultSelectionType(int type) {
75         super(type, NAMES);
76     }
77     }
78
79     /**
80      * A type-safe enumeration of possible job destinations.
81      */

82     public static final class DestinationType extends AttributeValue JavaDoc {
83         private static final int I_FILE = 0;
84         private static final int I_PRINTER = 1;
85
86         private static final String JavaDoc NAMES[] = {
87         "file", "printer"
88     };
89
90         /**
91      * The <code>DestinationType</code> instance to use for
92          * specifying print to file.
93      */

94         public static final DestinationType FILE =
95             new DestinationType(I_FILE);
96         /**
97      * The <code>DestinationType</code> instance to use for
98          * specifying print to printer.
99      */

100         public static final DestinationType PRINTER =
101             new DestinationType(I_PRINTER);
102
103         private DestinationType(int type) {
104         super(type, NAMES);
105     }
106     }
107
108     /**
109      * A type-safe enumeration of possible dialogs to display to the user.
110      */

111     public static final class DialogType extends AttributeValue JavaDoc {
112         private static final int I_COMMON = 0;
113         private static final int I_NATIVE = 1;
114         private static final int I_NONE = 2;
115
116         private static final String JavaDoc NAMES[] = {
117         "common", "native", "none"
118     };
119
120         /**
121      * The <code>DialogType</code> instance to use for
122          * specifying the cross-platform, pure Java print dialog.
123      */

124         public static final DialogType COMMON = new DialogType(I_COMMON);
125         /**
126      * The <code>DialogType</code> instance to use for
127          * specifying the platform's native print dialog.
128      */

129         public static final DialogType NATIVE = new DialogType(I_NATIVE);
130         /**
131      * The <code>DialogType</code> instance to use for
132          * specifying no print dialog.
133      */

134         public static final DialogType NONE = new DialogType(I_NONE);
135
136         private DialogType(int type) {
137         super(type, NAMES);
138     }
139     }
140
141     /**
142      * A type-safe enumeration of possible multiple copy handling states.
143      * It is used to control how the sheets of multiple copies of a single
144      * document are collated.
145      */

146     public static final class MultipleDocumentHandlingType extends
147                                                                AttributeValue JavaDoc {
148         private static final int I_SEPARATE_DOCUMENTS_COLLATED_COPIES = 0;
149         private static final int I_SEPARATE_DOCUMENTS_UNCOLLATED_COPIES = 1;
150
151         private static final String JavaDoc NAMES[] = {
152         "separate-documents-collated-copies",
153         "separate-documents-uncollated-copies"
154     };
155
156         /**
157      * The <code>MultipleDocumentHandlingType</code> instance to use for specifying
158      * that the job should be divided into separate, collated copies.
159      */

160         public static final MultipleDocumentHandlingType
161             SEPARATE_DOCUMENTS_COLLATED_COPIES =
162                 new MultipleDocumentHandlingType(
163                     I_SEPARATE_DOCUMENTS_COLLATED_COPIES);
164         /**
165      * The <code>MultipleDocumentHandlingType</code> instance to use for specifying
166      * that the job should be divided into separate, uncollated copies.
167      */

168         public static final MultipleDocumentHandlingType
169             SEPARATE_DOCUMENTS_UNCOLLATED_COPIES =
170                 new MultipleDocumentHandlingType(
171                     I_SEPARATE_DOCUMENTS_UNCOLLATED_COPIES);
172
173         private MultipleDocumentHandlingType(int type) {
174         super(type, NAMES);
175     }
176     }
177
178     /**
179      * A type-safe enumeration of possible multi-page impositions. These
180      * impositions are in compliance with IPP 1.1.
181      */

182     public static final class SidesType extends AttributeValue JavaDoc {
183         private static final int I_ONE_SIDED = 0;
184         private static final int I_TWO_SIDED_LONG_EDGE = 1;
185         private static final int I_TWO_SIDED_SHORT_EDGE = 2;
186
187         private static final String JavaDoc NAMES[] = {
188         "one-sided", "two-sided-long-edge", "two-sided-short-edge"
189     };
190
191         /**
192      * The <code>SidesType</code> instance to use for specifying that
193      * consecutive job pages should be printed upon the same side of
194          * consecutive media sheets.
195      */

196         public static final SidesType ONE_SIDED = new SidesType(I_ONE_SIDED);
197         /**
198      * The <code>SidesType</code> instance to use for specifying that
199          * consecutive job pages should be printed upon front and back sides
200          * of consecutive media sheets, such that the orientation of each pair
201          * of pages on the medium would be correct for the reader as if for
202          * binding on the long edge.
203      */

204         public static final SidesType TWO_SIDED_LONG_EDGE =
205             new SidesType(I_TWO_SIDED_LONG_EDGE);
206         /**
207      * The <code>SidesType</code> instance to use for specifying that
208          * consecutive job pages should be printed upon front and back sides
209          * of consecutive media sheets, such that the orientation of each pair
210          * of pages on the medium would be correct for the reader as if for
211          * binding on the short edge.
212      */

213         public static final SidesType TWO_SIDED_SHORT_EDGE =
214             new SidesType(I_TWO_SIDED_SHORT_EDGE);
215
216         private SidesType(int type) {
217         super(type, NAMES);
218     }
219     }
220
221     private int copies;
222     private DefaultSelectionType defaultSelection;
223     private DestinationType destination;
224     private DialogType dialog;
225     private String JavaDoc fileName;
226     private int fromPage;
227     private int maxPage;
228     private int minPage;
229     private MultipleDocumentHandlingType multipleDocumentHandling;
230     private int[][] pageRanges;
231     private int prFirst;
232     private int prLast;
233     private String JavaDoc printer;
234     private SidesType sides;
235     private int toPage;
236
237     /**
238      * Constructs a <code>JobAttributes</code> instance with default
239      * values for every attribute. The dialog defaults to
240      * <code>DialogType.NATIVE</code>. Min page defaults to
241      * <code>1</code>. Max page defaults to <code>Integer.MAX_VALUE</code>.
242      * Destination defaults to <code>DestinationType.PRINTER</code>.
243      * Selection defaults to <code>DefaultSelectionType.ALL</code>.
244      * Number of copies defaults to <code>1</code>. Multiple document handling defaults
245      * to <code>MultipleDocumentHandlingType.SEPARATE_DOCUMENTS_UNCOLLATED_COPIES</code>.
246      * Sides defaults to <code>SidesType.ONE_SIDED</code>. File name defaults
247      * to <code>null</code>.
248      */

249     public JobAttributes() {
250         setCopiesToDefault();
251     setDefaultSelection(DefaultSelectionType.ALL);
252     setDestination(DestinationType.PRINTER);
253     setDialog(DialogType.NATIVE);
254     setMaxPage(Integer.MAX_VALUE);
255     setMinPage(1);
256     setMultipleDocumentHandlingToDefault();
257     setSidesToDefault();
258     }
259
260     /**
261      * Constructs a <code>JobAttributes</code> instance which is a copy
262      * of the supplied <code>JobAttributes</code>.
263      *
264      * @param obj the <code>JobAttributes</code> to copy
265      */

266     public JobAttributes(JobAttributes JavaDoc obj) {
267         set(obj);
268     }
269
270     /**
271      * Constructs a <code>JobAttributes</code> instance with the
272      * specified values for every attribute.
273      *
274      * @param copies an integer greater than 0
275      * @param defaultSelection <code>DefaultSelectionType.ALL</code>,
276      * <code>DefaultSelectionType.RANGE</code>, or
277      * <code>DefaultSelectionType.SELECTION</code>
278      * @param destination <code>DesintationType.FILE</code> or
279      * <code>DesintationType.PRINTER</code>
280      * @param dialog <code>DialogType.COMMON</code>,
281      * <code>DialogType.NATIVE</code>, or
282      * <code>DialogType.NONE</code>
283      * @param fileName the possibly <code>null</code> file name
284      * @param maxPage an integer greater than zero and greater than or equal
285      * to <i>minPage</i>
286      * @param minPage an integer greater than zero and less than or equal
287      * to <i>maxPage</i>
288      * @param multipleDocumentHandling
289      * <code>MultipleDocumentHandlingType.SEPARATE_DOCUMENTS_COLLATED_COPIES</code> or
290      * <code>MultipleDocumentHandlingType.SEPARATE_DOCUMENTS_UNCOLLATED_COPIES</code>
291      * @param pageRanges an array of integer arrays of two elements; an array
292      * is interpreted as a range spanning all pages including and
293      * between the specified pages; ranges must be in ascending
294      * order and must not overlap; specified page numbers cannot be
295      * less than <i>minPage</i> nor greater than <i>maxPage</i>;
296      * for example:
297      * <pre>
298      * (new int[][] { new int[] { 1, 3 }, new int[] { 5, 5 },
299      * new int[] { 15, 19 } }),
300      * </pre>
301      * specifies pages 1, 2, 3, 5, 15, 16, 17, 18, and 19. Note that
302      * (<code>new int[][] { new int[] { 1, 1 }, new int[] { 1, 2 } }</code>),
303      * is an invalid set of page ranges because the two ranges
304      * overlap
305      * @param printer the possibly <code>null</code> printer name
306      * @param sides <code>SidesType.ONE_SIDED</code>,
307      * <code>SidesType.TWO_SIDED_LONG_EDGE</code>, or
308      * <code>SidesType.TWO_SIDED_SHORT_EDGE</code>
309      * @throws IllegalArgumentException if one or more of the above
310      * conditions is violated
311      */

312     public JobAttributes(int copies, DefaultSelectionType defaultSelection,
313              DestinationType destination, DialogType dialog,
314              String JavaDoc fileName, int maxPage, int minPage,
315              MultipleDocumentHandlingType multipleDocumentHandling,
316              int[][] pageRanges, String JavaDoc printer, SidesType sides) {
317         setCopies(copies);
318     setDefaultSelection(defaultSelection);
319     setDestination(destination);
320     setDialog(dialog);
321     setFileName(fileName);
322     setMaxPage(maxPage);
323     setMinPage(minPage);
324     setMultipleDocumentHandling(multipleDocumentHandling);
325     setPageRanges(pageRanges);
326     setPrinter(printer);
327     setSides(sides);
328     }
329
330     /**
331      * Creates and returns a copy of this <code>JobAttributes</code>.
332      *
333      * @return the newly created copy; it is safe to cast this Object into
334      * a <code>JobAttributes</code>
335      */

336     public Object JavaDoc clone() {
337         try {
338         return super.clone();
339     } catch (CloneNotSupportedException JavaDoc e) {
340         // Since we implement Cloneable, this should never happen
341
throw new InternalError JavaDoc();
342     }
343     }
344
345     /**
346      * Sets all of the attributes of this <code>JobAttributes</code> to
347      * the same values as the attributes of obj.
348      *
349      * @param obj the <code>JobAttributes</code> to copy
350      */

351     public void set(JobAttributes JavaDoc obj) {
352         copies = obj.copies;
353     defaultSelection = obj.defaultSelection;
354     destination = obj.destination;
355     dialog = obj.dialog;
356     fileName = obj.fileName;
357     fromPage = obj.fromPage;
358     maxPage = obj.maxPage;
359     minPage = obj.minPage;
360     multipleDocumentHandling = obj.multipleDocumentHandling;
361     // okay because we never modify the contents of pageRanges
362
pageRanges = obj.pageRanges;
363     prFirst = obj.prFirst;
364     prLast = obj.prLast;
365     printer = obj.printer;
366     sides = obj.sides;
367     toPage = obj.toPage;
368     }
369
370     /**
371      * Returns the number of copies the application should render for jobs
372      * using these attributes. This attribute is updated to the value chosen
373      * by the user.
374      *
375      * @return an integer greater than 0.
376      */

377     public int getCopies() {
378         return copies;
379     }
380
381     /**
382      * Specifies the number of copies the application should render for jobs
383      * using these attributes. Not specifying this attribute is equivalent to
384      * specifying <code>1</code>.
385      *
386      * @param copies an integer greater than 0
387      * @throws IllegalArgumentException if <code>copies</code> is less than
388      * or equal to 0
389      */

390     public void setCopies(int copies) {
391         if (copies <= 0) {
392         throw new IllegalArgumentException JavaDoc("Invalid value for attribute "+
393                            "copies");
394     }
395     this.copies = copies;
396     }
397
398     /**
399      * Sets the number of copies the application should render for jobs using
400      * these attributes to the default. The default number of copies is 1.
401      */

402     public void setCopiesToDefault() {
403         setCopies(1);
404     }
405
406     /**
407      * Specifies whether, for jobs using these attributes, the application
408      * should print all pages, the range specified by the return value of
409      * <code>getPageRanges</code>, or the current selection. This attribute
410      * is updated to the value chosen by the user.
411      *
412      * @return DefaultSelectionType.ALL, DefaultSelectionType.RANGE, or
413      * DefaultSelectionType.SELECTION
414      */

415     public DefaultSelectionType getDefaultSelection() {
416         return defaultSelection;
417     }
418
419     /**
420      * Specifies whether, for jobs using these attributes, the application
421      * should print all pages, the range specified by the return value of
422      * <code>getPageRanges</code>, or the current selection. Not specifying
423      * this attribute is equivalent to specifying DefaultSelectionType.ALL.
424      *
425      * @param defaultSelection DefaultSelectionType.ALL,
426      * DefaultSelectionType.RANGE, or DefaultSelectionType.SELECTION.
427      * @throws IllegalArgumentException if defaultSelection is <code>null</code>
428      */

429     public void setDefaultSelection(DefaultSelectionType defaultSelection) {
430         if (defaultSelection == null) {
431         throw new IllegalArgumentException JavaDoc("Invalid value for attribute "+
432                            "defaultSelection");
433     }
434         this.defaultSelection = defaultSelection;
435     }
436
437     /**
438      * Specifies whether output will be to a printer or a file for jobs using
439      * these attributes. This attribute is updated to the value chosen by the
440      * user.
441      *
442      * @return DesintationType.FILE or DesintationType.PRINTER
443      */

444     public DestinationType getDestination() {
445         return destination;
446     }
447
448     /**
449      * Specifies whether output will be to a printer or a file for jobs using
450      * these attributes. Not specifying this attribute is equivalent to
451      * specifying DesintationType.PRINTER.
452      *
453      * @param destination DesintationType.FILE or DesintationType.PRINTER.
454      * @throws IllegalArgumentException if destination is null.
455      */

456     public void setDestination(DestinationType destination) {
457         if (destination == null) {
458         throw new IllegalArgumentException JavaDoc("Invalid value for attribute "+
459                            "destination");
460     }
461         this.destination = destination;
462     }
463
464     /**
465      * Returns whether, for jobs using these attributes, the user should see
466      * a print dialog in which to modify the print settings, and which type of
467      * print dialog should be displayed. DialogType.COMMON denotes a cross-
468      * platform, pure Java print dialog. DialogType.NATIVE denotes the
469      * platform's native print dialog. If a platform does not support a native
470      * print dialog, the pure Java print dialog is displayed instead.
471      * DialogType.NONE specifies no print dialog (i.e., background printing).
472      * This attribute cannot be modified by, and is not subject to any
473      * limitations of, the implementation or the target printer.
474      *
475      * @return <code>DialogType.COMMON</code>, <code>DialogType.NATIVE</code>, or
476      * <code>DialogType.NONE</code>
477      */

478     public DialogType getDialog() {
479         return dialog;
480     }
481
482     /**
483      * Specifies whether, for jobs using these attributes, the user should see
484      * a print dialog in which to modify the print settings, and which type of
485      * print dialog should be displayed. DialogType.COMMON denotes a cross-
486      * platform, pure Java print dialog. DialogType.NATIVE denotes the
487      * platform's native print dialog. If a platform does not support a native
488      * print dialog, the pure Java print dialog is displayed instead.
489      * DialogType.NONE specifies no print dialog (i.e., background printing).
490      * Not specifying this attribute is equivalent to specifying
491      * DialogType.NATIVE.
492      *
493      * @param dialog DialogType.COMMON, DialogType.NATIVE, or
494      * DialogType.NONE.
495      * @throws IllegalArgumentException if dialog is null.
496      */

497     public void setDialog(DialogType dialog) {
498         if (dialog == null) {
499         throw new IllegalArgumentException JavaDoc("Invalid value for attribute "+
500                            "dialog");
501     }
502         this.dialog = dialog;
503     }
504
505     /**
506      * Specifies the file name for the output file for jobs using these
507      * attributes. This attribute is updated to the value chosen by the user.
508      *
509      * @return the possibly <code>null</code> file name
510      */

511     public String JavaDoc getFileName() {
512         return fileName;
513     }
514
515     /**
516      * Specifies the file name for the output file for jobs using these
517      * attributes. Default is platform-dependent and implementation-defined.
518      *
519      * @param fileName the possibly null file name.
520      */

521     public void setFileName(String JavaDoc fileName) {
522         this.fileName = fileName;
523     }
524
525     /**
526      * Returns, for jobs using these attributes, the first page to be
527      * printed, if a range of pages is to be printed. This attribute is
528      * updated to the value chosen by the user. An application should ignore
529      * this attribute on output, unless the return value of the <code>
530      * getDefaultSelection</code> method is DefaultSelectionType.RANGE. An
531      * application should honor the return value of <code>getPageRanges</code>
532      * over the return value of this method, if possible.
533      *
534      * @return an integer greater than zero and less than or equal to
535      * <i>toPage</i> and greater than or equal to <i>minPage</i> and
536      * less than or equal to <i>maxPage</i>.
537      */

538     public int getFromPage() {
539         if (fromPage != 0) {
540         return fromPage;
541     } else if (toPage != 0) {
542         return getMinPage();
543     } else if (pageRanges != null) {
544         return prFirst;
545     } else {
546         return getMinPage();
547     }
548     }
549
550     /**
551      * Specifies, for jobs using these attributes, the first page to be
552      * printed, if a range of pages is to be printed. If this attribute is not
553      * specified, then the values from the pageRanges attribute are used. If
554      * pageRanges and either or both of fromPage and toPage are specified,
555      * pageRanges takes precedence. Specifying none of pageRanges, fromPage,
556      * or toPage is equivalent to calling
557      * setPageRanges(new int[][] { new int[] { <i>minPage</i> } });
558      *
559      * @param fromPage an integer greater than zero and less than or equal to
560      * <i>toPage</i> and greater than or equal to <i>minPage</i> and
561      * less than or equal to <i>maxPage</i>.
562      * @throws IllegalArgumentException if one or more of the above
563      * conditions is violated.
564      */

565     public void setFromPage(int fromPage) {
566         if (fromPage <= 0 ||
567         (toPage != 0 && fromPage > toPage) ||
568         fromPage < minPage ||
569         fromPage > maxPage) {
570         throw new IllegalArgumentException JavaDoc("Invalid value for attribute "+
571                            "fromPage");
572     }
573     this.fromPage = fromPage;
574     }
575
576     /**
577      * Specifies the maximum value the user can specify as the last page to
578      * be printed for jobs using these attributes. This attribute cannot be
579      * modified by, and is not subject to any limitations of, the
580      * implementation or the target printer.
581      *
582      * @return an integer greater than zero and greater than or equal
583      * to <i>minPage</i>.
584      */

585     public int getMaxPage() {
586         return maxPage;
587     }
588
589     /**
590      * Specifies the maximum value the user can specify as the last page to
591      * be printed for jobs using these attributes. Not specifying this
592      * attribute is equivalent to specifying <code>Integer.MAX_VALUE</code>.
593      *
594      * @param maxPage an integer greater than zero and greater than or equal
595      * to <i>minPage</i>
596      * @throws IllegalArgumentException if one or more of the above
597      * conditions is violated
598      */

599     public void setMaxPage(int maxPage) {
600         if (maxPage <= 0 || maxPage < minPage) {
601         throw new IllegalArgumentException JavaDoc("Invalid value for attribute "+
602                            "maxPage");
603     }
604     this.maxPage = maxPage;
605     }
606
607     /**
608      * Specifies the minimum value the user can specify as the first page to
609      * be printed for jobs using these attributes. This attribute cannot be
610      * modified by, and is not subject to any limitations of, the
611      * implementation or the target printer.
612      *
613      * @return an integer greater than zero and less than or equal
614      * to <i>maxPage</i>.
615      */

616     public int getMinPage() {
617         return minPage;
618     }
619
620     /**
621      * Specifies the minimum value the user can specify as the first page to
622      * be printed for jobs using these attributes. Not specifying this
623      * attribute is equivalent to specifying <code>1</code>.
624      *
625      * @param minPage an integer greater than zero and less than or equal
626      * to <i>maxPage</i>.
627      * @throws IllegalArgumentException if one or more of the above
628      * conditions is violated.
629      */

630     public void setMinPage(int minPage) {
631         if (minPage <= 0 || minPage > maxPage) {
632         throw new IllegalArgumentException JavaDoc("Invalid value for attribute "+
633                            "minPage");
634     }
635     this.minPage = minPage;
636     }
637
638     /**
639      * Specifies the handling of multiple copies, including collation, for
640      * jobs using these attributes. This attribute is updated to the value
641      * chosen by the user.
642      *
643      * @return
644      * MultipleDocumentHandlingType.SEPARATE_DOCUMENTS_COLLATED_COPIES or
645      * MultipleDocumentHandlingType.SEPARATE_DOCUMENTS_UNCOLLATED_COPIES.
646      */

647     public MultipleDocumentHandlingType getMultipleDocumentHandling() {
648         return multipleDocumentHandling;
649     }
650
651     /**
652      * Specifies the handling of multiple copies, including collation, for
653      * jobs using these attributes. Not specifying this attribute is equivalent
654      * to specifying
655      * MultipleDocumentHandlingType.SEPARATE_DOCUMENTS_UNCOLLATED_COPIES.
656      *
657      * @param multipleDocumentHandling
658      * MultipleDocumentHandlingType.SEPARATE_DOCUMENTS_COLLATED_COPIES or
659      * MultipleDocumentHandlingType.SEPARATE_DOCUMENTS_UNCOLLATED_COPIES.
660      * @throws IllegalArgumentException if multipleDocumentHandling is null.
661      */

662     public void setMultipleDocumentHandling(MultipleDocumentHandlingType
663                         multipleDocumentHandling) {
664         if (multipleDocumentHandling == null) {
665         throw new IllegalArgumentException JavaDoc("Invalid value for attribute "+
666                            "multipleDocumentHandling");
667     }
668         this.multipleDocumentHandling = multipleDocumentHandling;
669     }
670
671     /**
672      * Sets the handling of multiple copies, including collation, for jobs
673      * using these attributes to the default. The default handling is
674      * MultipleDocumentHandlingType.SEPARATE_DOCUMENTS_UNCOLLATED_COPIES.
675      */

676     public void setMultipleDocumentHandlingToDefault() {
677         setMultipleDocumentHandling(
678             MultipleDocumentHandlingType.SEPARATE_DOCUMENTS_UNCOLLATED_COPIES);
679     }
680
681     /**
682      * Specifies, for jobs using these attributes, the ranges of pages to be
683      * printed, if a range of pages is to be printed. All range numbers are
684      * inclusive. This attribute is updated to the value chosen by the user.
685      * An application should ignore this attribute on output, unless the
686      * return value of the <code>getDefaultSelection</code> method is
687      * DefaultSelectionType.RANGE.
688      *
689      * @return an array of integer arrays of 2 elements. An array
690      * is interpreted as a range spanning all pages including and
691      * between the specified pages. Ranges must be in ascending
692      * order and must not overlap. Specified page numbers cannot be
693      * less than <i>minPage</i> nor greater than <i>maxPage</i>.
694      * For example:
695      * (new int[][] { new int[] { 1, 3 }, new int[] { 5, 5 },
696      * new int[] { 15, 19 } }),
697      * specifies pages 1, 2, 3, 5, 15, 16, 17, 18, and 19.
698      */

699     public int[][] getPageRanges() {
700         if (pageRanges != null) {
701         // Return a copy because otherwise client code could circumvent the
702
// the checks made in setPageRanges by modifying the returned
703
// array.
704
int[][] copy = new int[pageRanges.length][2];
705         for (int i = 0; i < pageRanges.length; i++) {
706         copy[i][0] = pageRanges[i][0];
707         copy[i][1] = pageRanges[i][1];
708         }
709         return copy;
710     } else if (fromPage != 0 || toPage != 0) {
711         int fromPage = getFromPage();
712         int toPage = getToPage();
713         return new int[][] { new int[] { fromPage, toPage } };
714     } else {
715         int minPage = getMinPage();
716         return new int[][] { new int[] { minPage, minPage } };
717     }
718     }
719
720     /**
721      * Specifies, for jobs using these attributes, the ranges of pages to be
722      * printed, if a range of pages is to be printed. All range numbers are
723      * inclusive. If this attribute is not specified, then the values from the
724      * fromPage and toPages attributes are used. If pageRanges and either or
725      * both of fromPage and toPage are specified, pageRanges takes precedence.
726      * Specifying none of pageRanges, fromPage, or toPage is equivalent to
727      * calling setPageRanges(new int[][] { new int[] { <i>minPage</i>,
728      * <i>minPage</i> } });
729      *
730      * @param pageRanges an array of integer arrays of 2 elements. An array
731      * is interpreted as a range spanning all pages including and
732      * between the specified pages. Ranges must be in ascending
733      * order and must not overlap. Specified page numbers cannot be
734      * less than <i>minPage</i> nor greater than <i>maxPage</i>.
735      * For example:
736      * (new int[][] { new int[] { 1, 3 }, new int[] { 5, 5 },
737      * new int[] { 15, 19 } }),
738      * specifies pages 1, 2, 3, 5, 15, 16, 17, 18, and 19. Note that
739      * (new int[][] { new int[] { 1, 1 }, new int[] { 1, 2 } }),
740      * is an invalid set of page ranges because the two ranges
741      * overlap.
742      * @throws IllegalArgumentException if one or more of the above
743      * conditions is violated.
744      */

745     public void setPageRanges(int[][] pageRanges) {
746         String JavaDoc xcp = "Invalid value for attribute pageRanges";
747     int first = 0;
748         int last = 0;
749
750     if (pageRanges == null) {
751         throw new IllegalArgumentException JavaDoc(xcp);
752     }
753
754         for (int i = 0; i < pageRanges.length; i++) {
755         if (pageRanges[i] == null ||
756         pageRanges[i].length != 2 ||
757         pageRanges[i][0] <= last ||
758         pageRanges[i][1] < pageRanges[i][0]) {
759                 throw new IllegalArgumentException JavaDoc(xcp);
760         }
761         last = pageRanges[i][1];
762         if (first == 0) {
763             first = pageRanges[i][0];
764         }
765     }
766
767     if (first < minPage || last > maxPage) {
768         throw new IllegalArgumentException JavaDoc(xcp);
769     }
770
771         // Store a copy because otherwise client code could circumvent the
772
// the checks made above by holding a reference to the array and
773
// modifying it after calling setPageRanges.
774
int[][] copy = new int[pageRanges.length][2];
775     for (int i = 0; i < pageRanges.length; i++) {
776         copy[i][0] = pageRanges[i][0];
777         copy[i][1] = pageRanges[i][1];
778     }
779     this.pageRanges = copy;
780     this.prFirst = first;
781     this.prLast = last;
782     }
783
784     /**
785      * Returns the destination printer for jobs using these attributes. This
786      * attribute is updated to the value chosen by the user.
787      *
788      * @return the possibly null printer name.
789      */

790     public String JavaDoc getPrinter() {
791         return printer;
792     }
793
794     /**
795      * Specifies the destination printer for jobs using these attributes.
796      * Default is platform-dependent and implementation-defined.
797      *
798      * @param printer the possibly null printer name.
799      */

800     public void setPrinter(String JavaDoc printer) {
801         this.printer = printer;
802     }
803
804     /**
805      * Returns how consecutive pages should be imposed upon the sides of the
806      * print medium for jobs using these attributes. SidesType.ONE_SIDED
807      * imposes each consecutive page upon the same side of consecutive media
808      * sheets. This imposition is sometimes called <i>simplex</i>.
809      * SidesType.TWO_SIDED_LONG_EDGE imposes each consecutive pair of pages
810      * upon front and back sides of consecutive media sheets, such that the
811      * orientation of each pair of pages on the medium would be correct for
812      * the reader as if for binding on the long edge. This imposition is
813      * sometimes called <i>duplex</i>. SidesType.TWO_SIDED_SHORT_EDGE imposes
814      * each consecutive pair of pages upon front and back sides of consecutive
815      * media sheets, such that the orientation of each pair of pages on the
816      * medium would be correct for the reader as if for binding on the short
817      * edge. This imposition is sometimes called <i>tumble</i>. This attribute
818      * is updated to the value chosen by the user.
819      *
820      * @return SidesType.ONE_SIDED, SidesType.TWO_SIDED_LONG_EDGE, or
821      * SidesType.TWO_SIDED_SHORT_EDGE.
822      */

823     public SidesType getSides() {
824         return sides;
825     }
826
827     /**
828      * Specifies how consecutive pages should be imposed upon the sides of the
829      * print medium for jobs using these attributes. SidesType.ONE_SIDED
830      * imposes each consecutive page upon the same side of consecutive media
831      * sheets. This imposition is sometimes called <i>simplex</i>.
832      * SidesType.TWO_SIDED_LONG_EDGE imposes each consecutive pair of pages
833      * upon front and back sides of consecutive media sheets, such that the
834      * orientation of each pair of pages on the medium would be correct for
835      * the reader as if for binding on the long edge. This imposition is
836      * sometimes called <i>duplex</i>. SidesType.TWO_SIDED_SHORT_EDGE imposes
837      * each consecutive pair of pages upon front and back sides of consecutive
838      * media sheets, such that the orientation of each pair of pages on the
839      * medium would be correct for the reader as if for binding on the short
840      * edge. This imposition is sometimes called <i>tumble</i>. Not specifying
841      * this attribute is equivalent to specifying SidesType.ONE_SIDED.
842      *
843      * @param sides SidesType.ONE_SIDED, SidesType.TWO_SIDED_LONG_EDGE, or
844      * SidesType.TWO_SIDED_SHORT_EDGE.
845      * @throws IllegalArgumentException if sides is null.
846      */

847     public void setSides(SidesType sides) {
848         if (sides == null) {
849         throw new IllegalArgumentException JavaDoc("Invalid value for attribute "+
850                            "sides");
851     }
852         this.sides = sides;
853     }
854
855     /**
856      * Sets how consecutive pages should be imposed upon the sides of the
857      * print medium for jobs using these attributes to the default. The
858      * default imposition is SidesType.ONE_SIDED.
859      */

860     public void setSidesToDefault() {
861         setSides(SidesType.ONE_SIDED);
862     }
863
864     /**
865      * Returns, for jobs using these attributes, the last page (inclusive)
866      * to be printed, if a range of pages is to be printed. This attribute is
867      * updated to the value chosen by the user. An application should ignore
868      * this attribute on output, unless the return value of the <code>
869      * getDefaultSelection</code> method is DefaultSelectionType.RANGE. An
870      * application should honor the return value of <code>getPageRanges</code>
871      * over the return value of this method, if possible.
872      *
873      * @return an integer greater than zero and greater than or equal
874      * to <i>toPage</i> and greater than or equal to <i>minPage</i>
875      * and less than or equal to <i>maxPage</i>.
876      */

877     public int getToPage() {
878         if (toPage != 0) {
879         return toPage;
880     } else if (fromPage != 0) {
881         return fromPage;
882     } else if (pageRanges != null) {
883         return prLast;
884     } else {
885         return getMinPage();
886     }
887     }
888
889     /**
890      * Specifies, for jobs using these attributes, the last page (inclusive)
891      * to be printed, if a range of pages is to be printed.
892      * If this attribute is not specified, then the values from the pageRanges
893      * attribute are used. If pageRanges and either or both of fromPage and
894      * toPage are specified, pageRanges takes precedence. Specifying none of
895      * pageRanges, fromPage, or toPage is equivalent to calling
896      * setPageRanges(new int[][] { new int[] { <i>minPage</i> } });
897      *
898      * @param toPage an integer greater than zero and greater than or equal
899      * to <i>fromPage</i> and greater than or equal to <i>minPage</i>
900      * and less than or equal to <i>maxPage</i>.
901      * @throws IllegalArgumentException if one or more of the above
902      * conditions is violated.
903      */

904     public void setToPage(int toPage) {
905         if (toPage <= 0 ||
906         (fromPage != 0 && toPage < fromPage) ||
907         toPage < minPage ||
908         toPage > maxPage) {
909         throw new IllegalArgumentException JavaDoc("Invalid value for attribute "+
910                            "toPage");
911     }
912     this.toPage = toPage;
913     }
914
915     /**
916      * Determines whether two JobAttributes are equal to each other.
917      * <p>
918      * Two JobAttributes are equal if and only if each of their attributes are
919      * equal. Attributes of enumeration type are equal if and only if the
920      * fields refer to the same unique enumeration object. A set of page
921      * ranges is equal if and only if the sets are of equal length, each range
922      * enumerates the same pages, and the ranges are in the same order.
923      *
924      * @param obj the object whose equality will be checked.
925      * @return whether obj is equal to this JobAttribute according to the
926      * above criteria.
927      */

928     public boolean equals(Object JavaDoc obj) {
929         if (!(obj instanceof JobAttributes JavaDoc)) {
930         return false;
931     }
932     JobAttributes JavaDoc rhs = (JobAttributes JavaDoc)obj;
933
934     if (fileName == null) {
935         if (rhs.fileName != null) {
936             return false;
937         }
938     } else {
939         if (!fileName.equals(rhs.fileName)) {
940             return false;
941         }
942     }
943
944     if (pageRanges == null) {
945         if (rhs.pageRanges != null) {
946             return false;
947         }
948     } else {
949         if (rhs.pageRanges == null ||
950             pageRanges.length != rhs.pageRanges.length) {
951             return false;
952         }
953         for (int i = 0; i < pageRanges.length; i++) {
954             if (pageRanges[i][0] != rhs.pageRanges[i][0] ||
955             pageRanges[i][1] != rhs.pageRanges[i][1]) {
956             return false;
957         }
958         }
959     }
960
961     if (printer == null) {
962         if (rhs.printer != null) {
963             return false;
964         }
965     } else {
966         if (!printer.equals(rhs.printer)) {
967             return false;
968         }
969     }
970
971     return (copies == rhs.copies &&
972         defaultSelection == rhs.defaultSelection &&
973         destination == rhs.destination &&
974         dialog == rhs.dialog &&
975         fromPage == rhs.fromPage &&
976         maxPage == rhs.maxPage &&
977         minPage == rhs.minPage &&
978         multipleDocumentHandling == rhs.multipleDocumentHandling &&
979         prFirst == rhs.prFirst &&
980         prLast == rhs.prLast &&
981         sides == rhs.sides &&
982         toPage == rhs.toPage);
983     }
984
985     /**
986      * Returns a hash code value for this JobAttributes.
987      *
988      * @return the hash code.
989      */

990     public int hashCode() {
991     int rest = ((copies + fromPage + maxPage + minPage + prFirst + prLast +
992              toPage) * 31) << 21;
993     if (pageRanges != null) {
994         int sum = 0;
995         for (int i = 0; i < pageRanges.length; i++) {
996             sum += pageRanges[i][0] + pageRanges[i][1];
997         }
998         rest ^= (sum * 31) << 11;
999     }
1000    if (fileName != null) {
1001        rest ^= fileName.hashCode();
1002    }
1003    if (printer != null) {
1004        rest ^= printer.hashCode();
1005    }
1006    return (defaultSelection.hashCode() << 6 ^
1007        destination.hashCode() << 5 ^
1008        dialog.hashCode() << 3 ^
1009        multipleDocumentHandling.hashCode() << 2 ^
1010        sides.hashCode() ^
1011        rest);
1012    }
1013
1014    /**
1015     * Returns a string representation of this JobAttributes.
1016     *
1017     * @return the string representation.
1018     */

1019    public String JavaDoc toString() {
1020        int[][] pageRanges = getPageRanges();
1021    String JavaDoc prStr = "[";
1022    boolean first = true;
1023    for (int i = 0; i < pageRanges.length; i++) {
1024        if (first) {
1025            first = false;
1026        } else {
1027            prStr += ",";
1028        }
1029        prStr += pageRanges[i][0] + ":" + pageRanges[i][1];
1030    }
1031    prStr += "]";
1032
1033        return "copies=" + getCopies() + ",defaultSelection=" +
1034        getDefaultSelection() + ",destination=" + getDestination() +
1035        ",dialog=" + getDialog() + ",fileName=" + getFileName() +
1036        ",fromPage=" + getFromPage() + ",maxPage=" + getMaxPage() +
1037        ",minPage=" + getMinPage() + ",multiple-document-handling=" +
1038        getMultipleDocumentHandling() + ",page-ranges=" + prStr +
1039        ",printer=" + getPrinter() + ",sides=" + getSides() + ",toPage=" +
1040        getToPage();
1041    }
1042}
1043
Popular Tags