1. /*
  2. * @(#)JobAttributes.java 1.4 00/02/02
  3. *
  4. * Copyright 1999, 2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package java.awt;
  11. /**
  12. * A set of attributes which control a print job.
  13. * <p>
  14. * Instances of this class control the number of copies, default selection,
  15. * destination, print dialog, file and printer names, page ranges, multiple
  16. * document handling (including collation), and multi-page imposition (such
  17. * as duplex) of every print job which uses the instance. Attribute names are
  18. * compliant with the Internet Printing Protocol (IPP) 1.1 where possible.
  19. * Attribute values are partially compliant where possible.
  20. * <p>
  21. * To use a method which takes an inner class type, pass a reference to
  22. * one of the constant fields of the inner class. Client code cannot create
  23. * new instances of the inner class types because none of those classes
  24. * has a public constructor. For example, to set the print dialog type to
  25. * the cross-platform, pure Java print dialog, use the following code:
  26. * <pre>
  27. * import java.awt.JobAttributes;
  28. *
  29. * public class PureJavaPrintDialogExample {
  30. * public void setPureJavaPrintDialog(JobAttributes jobAttributes) {
  31. * jobAttributes.setDialog(JobAttributes.DialogType.COMMON);
  32. * }
  33. * }
  34. * </pre>
  35. * <p>
  36. * Every IPP attribute which supports an <i>attributeName</i>-default value
  37. * has a corresponding <code>set<i>attributeName</i>ToDefault</code> method.
  38. * Default value fields are not provided.
  39. *
  40. * @version 1.4, 02/02/00
  41. * @author David Mendenhall
  42. */
  43. public final class JobAttributes implements Cloneable {
  44. /**
  45. * A type-safe enumeration of possible default selection states.
  46. */
  47. public static final class DefaultSelectionType extends AttributeValue {
  48. private static final int I_ALL = 0;
  49. private static final int I_RANGE = 1;
  50. private static final int I_SELECTION = 2;
  51. private static final String NAMES[] = {
  52. "all", "range", "selection"
  53. };
  54. /**
  55. * The DefaultSelectionType instance to use for specifying that all
  56. * pages of the job should be printed.
  57. */
  58. public static final DefaultSelectionType ALL =
  59. new DefaultSelectionType(I_ALL);
  60. /**
  61. * The DefaultSelectionType instance to use for specifying that a
  62. * range of pages of the job should be printed.
  63. */
  64. public static final DefaultSelectionType RANGE =
  65. new DefaultSelectionType(I_RANGE);
  66. /**
  67. * The DefaultSelectionType instance to use for specifying that the
  68. * current selection should be printed.
  69. */
  70. public static final DefaultSelectionType SELECTION =
  71. new DefaultSelectionType(I_SELECTION);
  72. private DefaultSelectionType(int type) {
  73. super(type, NAMES);
  74. }
  75. }
  76. /**
  77. * A type-safe enumeration of possible job destinations.
  78. */
  79. public static final class DestinationType extends AttributeValue {
  80. private static final int I_FILE = 0;
  81. private static final int I_PRINTER = 1;
  82. private static final String NAMES[] = {
  83. "file", "printer"
  84. };
  85. /**
  86. * The DestinationType instance to use for specifying print to file.
  87. */
  88. public static final DestinationType FILE =
  89. new DestinationType(I_FILE);
  90. /**
  91. * The DestinationType instance to use for specifying print to printer.
  92. */
  93. public static final DestinationType PRINTER =
  94. new DestinationType(I_PRINTER);
  95. private DestinationType(int type) {
  96. super(type, NAMES);
  97. }
  98. }
  99. /**
  100. * A type-safe enumeration of possible dialogs to display to the user.
  101. */
  102. public static final class DialogType extends AttributeValue {
  103. private static final int I_COMMON = 0;
  104. private static final int I_NATIVE = 1;
  105. private static final int I_NONE = 2;
  106. private static final String NAMES[] = {
  107. "common", "native", "none"
  108. };
  109. /**
  110. * The DialogType instance to use for specifying the cross-platform,
  111. * pure Java print dialog.
  112. */
  113. public static final DialogType COMMON = new DialogType(I_COMMON);
  114. /**
  115. * The DialogType instance to use for specifying the platform's
  116. * native print dialog.
  117. */
  118. public static final DialogType NATIVE = new DialogType(I_NATIVE);
  119. /**
  120. * The DialogType instance to use for specifying no print dialog.
  121. */
  122. public static final DialogType NONE = new DialogType(I_NONE);
  123. private DialogType(int type) {
  124. super(type, NAMES);
  125. }
  126. }
  127. /**
  128. * A type-safe enumeration of possible multiple document handling states.
  129. * These states are in partial compliance with IPP 1.1.
  130. */
  131. public static final class MultipleDocumentHandlingType extends
  132. AttributeValue {
  133. private static final int I_SEPARATE_DOCUMENTS_COLLATED_COPIES = 0;
  134. private static final int I_SEPARATE_DOCUMENTS_UNCOLLATED_COPIES = 1;
  135. private static final String NAMES[] = {
  136. "separate-documents-collated-copies",
  137. "separate-documents-uncollated-copies"
  138. };
  139. /**
  140. * The MultipleDocumentHandlingType instance to use for specifying
  141. * that the job should be divided into separate, collated documents.
  142. */
  143. public static final MultipleDocumentHandlingType
  144. SEPARATE_DOCUMENTS_COLLATED_COPIES =
  145. new MultipleDocumentHandlingType(
  146. I_SEPARATE_DOCUMENTS_COLLATED_COPIES);
  147. /**
  148. * The MultipleDocumentHandlingType instance to use for specifying
  149. * that the job should be divided into separate, uncollated documents.
  150. */
  151. public static final MultipleDocumentHandlingType
  152. SEPARATE_DOCUMENTS_UNCOLLATED_COPIES =
  153. new MultipleDocumentHandlingType(
  154. I_SEPARATE_DOCUMENTS_UNCOLLATED_COPIES);
  155. private MultipleDocumentHandlingType(int type) {
  156. super(type, NAMES);
  157. }
  158. }
  159. /**
  160. * A type-safe enumeration of possible multi-page impositions. These
  161. * impositions are in compliance with IPP 1.1.
  162. */
  163. public static final class SidesType extends AttributeValue {
  164. private static final int I_ONE_SIDED = 0;
  165. private static final int I_TWO_SIDED_LONG_EDGE = 1;
  166. private static final int I_TWO_SIDED_SHORT_EDGE = 2;
  167. private static final String NAMES[] = {
  168. "one-sided", "two-sided-long-edge", "two-sided-short-edge"
  169. };
  170. /**
  171. * The SidesType instance to use for specifying that consecutive job
  172. * pages should be printed upon the same side of consecutive media
  173. * sheets.
  174. */
  175. public static final SidesType ONE_SIDED = new SidesType(I_ONE_SIDED);
  176. /**
  177. * The SidesType instance to use for specifying that consecutive job
  178. * pages should be printed upon front and back sides of consecutive
  179. * media sheets, such that the orientation of each pair of pages on
  180. * the medium would be correct for the reader as if for binding on
  181. * the long edge.
  182. */
  183. public static final SidesType TWO_SIDED_LONG_EDGE =
  184. new SidesType(I_TWO_SIDED_LONG_EDGE);
  185. /**
  186. * The SidesType instance to use for specifying that consecutive job
  187. * pages should be printed upon front and back sides of consecutive
  188. * media sheets, such that the orientation of each pair of pages on
  189. * the medium would be correct for the reader as if for binding on
  190. * the short edge.
  191. */
  192. public static final SidesType TWO_SIDED_SHORT_EDGE =
  193. new SidesType(I_TWO_SIDED_SHORT_EDGE);
  194. private SidesType(int type) {
  195. super(type, NAMES);
  196. }
  197. }
  198. private int copies;
  199. private DefaultSelectionType defaultSelection;
  200. private DestinationType destination;
  201. private DialogType dialog;
  202. private String fileName;
  203. private int fromPage;
  204. private int maxPage;
  205. private int minPage;
  206. private MultipleDocumentHandlingType multipleDocumentHandling;
  207. private int[][] pageRanges;
  208. private int prFirst;
  209. private int prLast;
  210. private String printer;
  211. private SidesType sides;
  212. private int toPage;
  213. /**
  214. * Constructs a JobAttributes instance with default values for every
  215. * attribute.
  216. */
  217. public JobAttributes() {
  218. setCopiesToDefault();
  219. setDefaultSelection(DefaultSelectionType.ALL);
  220. setDestination(DestinationType.PRINTER);
  221. setDialog(DialogType.NATIVE);
  222. setMaxPage(Integer.MAX_VALUE);
  223. setMinPage(1);
  224. setMultipleDocumentHandlingToDefault();
  225. setSidesToDefault();
  226. }
  227. /**
  228. * Constructs a JobAttributes instance which is a copy of the supplied
  229. * JobAttributes.
  230. *
  231. * @param obj the JobAttributes to copy.
  232. */
  233. public JobAttributes(JobAttributes obj) {
  234. set(obj);
  235. }
  236. /**
  237. * Constructs a JobAttributes instance with the specified values for
  238. * every attribute.
  239. *
  240. * @param copies an integer greater than 0.
  241. * @param defaultSelection DefaultSelectionType.ALL,
  242. * DefaultSelectionType.RANGE, or DefaultSelectionType.SELECTION.
  243. * @param destination DesintationType.FILE or DesintationType.PRINTER.
  244. * @param dialog DialogType.COMMON, DialogType.NATIVE, or
  245. * DialogType.NONE.
  246. * @param fileName the possibly null file name.
  247. * @param maxPage an integer greater than zero and greater than or equal
  248. * to <i>minPage</i>.
  249. * @param minPage an integer greater than zero and less than or equal
  250. * to <i>maxPage</i>.
  251. * @param multipleDocumentHandling
  252. * MultipleDocumentHandlingType.SEPARATE_DOCUMENTS_COLLATED_COPIES or
  253. * MultipleDocumentHandlingType.SEPARATE_DOCUMENTS_UNCOLLATED_COPIES.
  254. * @param pageRanges an array of integer arrays of 2 elements. An array
  255. * is interpreted as a range spanning all pages including and
  256. * between the specified pages. Ranges must be in ascending
  257. * order and must not overlap. Specified page numbers cannot be
  258. * less than <i>minPage</i> nor greater than <i>maxPage</i>.
  259. * For example:
  260. * (new int[][] { new int[] { 1, 3 }, new int[] { 5, 5 },
  261. * new int[] { 15, 19 } }),
  262. * specifies pages 1, 2, 3, 5, 15, 16, 17, 18, and 19. Note that
  263. * (new int[][] { new int[] { 1, 1 }, new int[] { 1, 2 } }),
  264. * is an invalid set of page ranges because the two ranges
  265. * overlap.
  266. * @param printer the possibly null printer name.
  267. * @param sides SidesType.ONE_SIDED, SidesType.TWO_SIDED_LONG_EDGE, or
  268. * SidesType.TWO_SIDED_SHORT_EDGE.
  269. * @throws IllegalArgumentException if one or more of the above
  270. * conditions is violated.
  271. */
  272. public JobAttributes(int copies, DefaultSelectionType defaultSelection,
  273. DestinationType destination, DialogType dialog,
  274. String fileName, int maxPage, int minPage,
  275. MultipleDocumentHandlingType multipleDocumentHandling,
  276. int[][] pageRanges, String printer, SidesType sides) {
  277. setCopies(copies);
  278. setDefaultSelection(defaultSelection);
  279. setDestination(destination);
  280. setDialog(dialog);
  281. setFileName(fileName);
  282. setMaxPage(maxPage);
  283. setMinPage(minPage);
  284. setMultipleDocumentHandling(multipleDocumentHandling);
  285. setPageRanges(pageRanges);
  286. setPrinter(printer);
  287. setSides(sides);
  288. }
  289. /**
  290. * Creates and returns a copy of this JobAttributes.
  291. *
  292. * @return the newly created copy. It is safe to cast this Object into
  293. * a JobAttributes.
  294. */
  295. public Object clone() {
  296. try {
  297. return super.clone();
  298. } catch (CloneNotSupportedException e) {
  299. // Since we implement Cloneable, this should never happen
  300. throw new InternalError();
  301. }
  302. }
  303. /**
  304. * Sets all of the attributes of this JobAttributes to the same values as
  305. * the attributes of obj.
  306. *
  307. * @param obj the JobAttributes to copy.
  308. */
  309. public void set(JobAttributes obj) {
  310. copies = obj.copies;
  311. defaultSelection = obj.defaultSelection;
  312. destination = obj.destination;
  313. dialog = obj.dialog;
  314. fileName = obj.fileName;
  315. fromPage = obj.fromPage;
  316. maxPage = obj.maxPage;
  317. minPage = obj.minPage;
  318. multipleDocumentHandling = obj.multipleDocumentHandling;
  319. // okay because we never modify the contents of pageRanges
  320. pageRanges = obj.pageRanges;
  321. prFirst = obj.prFirst;
  322. prLast = obj.prLast;
  323. printer = obj.printer;
  324. sides = obj.sides;
  325. toPage = obj.toPage;
  326. }
  327. /**
  328. * Returns the number of copies the application should render for jobs
  329. * using these attributes. This attribute is updated to the value chosen
  330. * by the user.
  331. *
  332. * @return an integer greater than 0.
  333. */
  334. public int getCopies() {
  335. return copies;
  336. }
  337. /**
  338. * Specifies the number of copies the application should render for jobs
  339. * using these attributes. Not specifying this attribute is equivalent to
  340. * specifying <code>1</code>.
  341. *
  342. * @param copies an integer greater than 0.
  343. * @throws IllegalArgumentException if copies is less than or equal to 0.
  344. */
  345. public void setCopies(int copies) {
  346. if (copies <= 0) {
  347. throw new IllegalArgumentException("Invalid value for attribute "+
  348. "copies");
  349. }
  350. this.copies = copies;
  351. }
  352. /**
  353. * Sets the number of copies the application should render for jobs using
  354. * these attributes to the default. The default number of copies is 1.
  355. */
  356. public void setCopiesToDefault() {
  357. setCopies(1);
  358. }
  359. /**
  360. * Specifies whether, for jobs using these attributes, the application
  361. * should print all pages, the range specified by the return value of
  362. * <code>getPageRanges</code>, or the current selection. This attribute
  363. * is updated to the value chosen by the user.
  364. *
  365. * @return DefaultSelectionType.ALL, DefaultSelectionType.RANGE, or
  366. * DefaultSelectionType.SELECTION.
  367. */
  368. public DefaultSelectionType getDefaultSelection() {
  369. return defaultSelection;
  370. }
  371. /**
  372. * Specifies whether, for jobs using these attributes, the application
  373. * should print all pages, the range specified by the return value of
  374. * <code>getPageRanges</code>, or the current selection. Not specifying
  375. * this attribute is equivalent to specifying DefaultSelectionType.ALL.
  376. *
  377. * @param defaultSelection DefaultSelectionType.ALL,
  378. * DefaultSelectionType.RANGE, or DefaultSelectionType.SELECTION.
  379. * @throws IllegalArgumentException if defaultSelection is null.
  380. */
  381. public void setDefaultSelection(DefaultSelectionType defaultSelection) {
  382. if (defaultSelection == null) {
  383. throw new IllegalArgumentException("Invalid value for attribute "+
  384. "defaultSelection");
  385. }
  386. this.defaultSelection = defaultSelection;
  387. }
  388. /**
  389. * Specifies whether output will be to a printer or a file for jobs using
  390. * these attributes. This attribute is updated to the value chosen by the
  391. * user.
  392. *
  393. * @return DesintationType.FILE or DesintationType.PRINTER.
  394. */
  395. public DestinationType getDestination() {
  396. return destination;
  397. }
  398. /**
  399. * Specifies whether output will be to a printer or a file for jobs using
  400. * these attributes. Not specifying this attribute is equivalent to
  401. * specifying DesintationType.PRINTER.
  402. *
  403. * @param destination DesintationType.FILE or DesintationType.PRINTER.
  404. * @throws IllegalArgumentException if destination is null.
  405. */
  406. public void setDestination(DestinationType destination) {
  407. if (destination == null) {
  408. throw new IllegalArgumentException("Invalid value for attribute "+
  409. "destination");
  410. }
  411. this.destination = destination;
  412. }
  413. /**
  414. * Returns whether, for jobs using these attributes, the user should see
  415. * a print dialog in which to modify the print settings, and which type of
  416. * print dialog should be displayed. DialogType.COMMON denotes a cross-
  417. * platform, pure Java print dialog. DialogType.NATIVE denotes the
  418. * platform's native print dialog. If a platform does not support a native
  419. * print dialog, the pure Java print dialog is displayed instead.
  420. * DialogType.NONE specifies no print dialog (i.e., background printing).
  421. * This attribute cannot be modified by, and is not subject to any
  422. * limitations of, the implementation or the target printer.
  423. *
  424. * @return DialogType.COMMON, DialogType.NATIVE, or
  425. * DialogType.NONE.
  426. */
  427. public DialogType getDialog() {
  428. return dialog;
  429. }
  430. /**
  431. * Specifies whether, for jobs using these attributes, the user should see
  432. * a print dialog in which to modify the print settings, and which type of
  433. * print dialog should be displayed. DialogType.COMMON denotes a cross-
  434. * platform, pure Java print dialog. DialogType.NATIVE denotes the
  435. * platform's native print dialog. If a platform does not support a native
  436. * print dialog, the pure Java print dialog is displayed instead.
  437. * DialogType.NONE specifies no print dialog (i.e., background printing).
  438. * Not specifying this attribute is equivalent to specifying
  439. * DialogType.NATIVE.
  440. *
  441. * @param dialog DialogType.COMMON, DialogType.NATIVE, or
  442. * DialogType.NONE.
  443. * @throws IllegalArgumentException if dialog is null.
  444. */
  445. public void setDialog(DialogType dialog) {
  446. if (dialog == null) {
  447. throw new IllegalArgumentException("Invalid value for attribute "+
  448. "dialog");
  449. }
  450. this.dialog = dialog;
  451. }
  452. /**
  453. * Specifies the file name for the output file for jobs using these
  454. * attributes. This attribute is updated to the value chosen by the user.
  455. *
  456. * @return the possibly null file name.
  457. */
  458. public String getFileName() {
  459. return fileName;
  460. }
  461. /**
  462. * Specifies the file name for the output file for jobs using these
  463. * attributes. Default is platform-dependent and implementation-defined.
  464. *
  465. * @param fileName the possibly null file name.
  466. */
  467. public void setFileName(String fileName) {
  468. this.fileName = fileName;
  469. }
  470. /**
  471. * Returns, for jobs using these attributes, the first page to be
  472. * printed, if a range of pages is to be printed. This attribute is
  473. * updated to the value chosen by the user. An application should ignore
  474. * this attribute on output, unless the return value of the <code>
  475. * getDefaultSelection</code> method is DefaultSelectionType.RANGE. An
  476. * application should honor the return value of <code>getPageRanges</code>
  477. * over the return value of this method, if possible.
  478. *
  479. * @return an integer greater than zero and less than or equal to
  480. * <i>toPage</i> and greater than or equal to <i>minPage</i> and
  481. * less than or equal to <i>maxPage</i>.
  482. */
  483. public int getFromPage() {
  484. if (fromPage != 0) {
  485. return fromPage;
  486. } else if (toPage != 0) {
  487. return getMinPage();
  488. } else if (pageRanges != null) {
  489. return prFirst;
  490. } else {
  491. return getMinPage();
  492. }
  493. }
  494. /**
  495. * Specifies, for jobs using these attributes, the first page to be
  496. * printed, if a range of pages is to be printed. If this attribute is not
  497. * specified, then the values from the pageRanges attribute are used. If
  498. * pageRanges and either or both of fromPage and toPage are specified,
  499. * pageRanges takes precedence. Specifying none of pageRanges, fromPage,
  500. * or toPage is equivalent to calling
  501. * setPageRanges(new int[][] { new int[] { <i>minPage</i> } });
  502. *
  503. * @param fromPage an integer greater than zero and less than or equal to
  504. * <i>toPage</i> and greater than or equal to <i>minPage</i> and
  505. * less than or equal to <i>maxPage</i>.
  506. * @throws IllegalArgumentException if one or more of the above
  507. * conditions is violated.
  508. */
  509. public void setFromPage(int fromPage) {
  510. if (fromPage <= 0 ||
  511. (toPage != 0 && fromPage > toPage) ||
  512. fromPage < minPage ||
  513. fromPage > maxPage) {
  514. throw new IllegalArgumentException("Invalid value for attribute "+
  515. "fromPage");
  516. }
  517. this.fromPage = fromPage;
  518. }
  519. /**
  520. * Specifies the maximum value the user can specify as the last page to
  521. * be printed for jobs using these attributes. This attribute cannot be
  522. * modified by, and is not subject to any limitations of, the
  523. * implementation or the target printer.
  524. *
  525. * @return an integer greater than zero and greater than or equal
  526. * to <i>minPage</i>.
  527. */
  528. public int getMaxPage() {
  529. return maxPage;
  530. }
  531. /**
  532. * Specifies the maximum value the user can specify as the last page to
  533. * be printed for jobs using these attributes. Not specifying this
  534. * attribute is equivalent to specifying <code>Integer.MAX_VALUE</code>.
  535. *
  536. * @param maxPage an integer greater than zero and greater than or equal
  537. * to <i>minPage</i>.
  538. * @throws IllegalArgumentException if one or more of the above
  539. * conditions is violated.
  540. */
  541. public void setMaxPage(int maxPage) {
  542. if (maxPage <= 0 || maxPage < minPage) {
  543. throw new IllegalArgumentException("Invalid value for attribute "+
  544. "maxPage");
  545. }
  546. this.maxPage = maxPage;
  547. }
  548. /**
  549. * Specifies the minimum value the user can specify as the first page to
  550. * be printed for jobs using these attributes. This attribute cannot be
  551. * modified by, and is not subject to any limitations of, the
  552. * implementation or the target printer.
  553. *
  554. * @return an integer greater than zero and less than or equal
  555. * to <i>maxPage</i>.
  556. */
  557. public int getMinPage() {
  558. return minPage;
  559. }
  560. /**
  561. * Specifies the minimum value the user can specify as the first page to
  562. * be printed for jobs using these attributes. Not specifying this
  563. * attribute is equivalent to specifying <code>1</code>.
  564. *
  565. * @param minPage an integer greater than zero and less than or equal
  566. * to <i>maxPage</i>.
  567. * @throws IllegalArgumentException if one or more of the above
  568. * conditions is violated.
  569. */
  570. public void setMinPage(int minPage) {
  571. if (minPage <= 0 || minPage > maxPage) {
  572. throw new IllegalArgumentException("Invalid value for attribute "+
  573. "minPage");
  574. }
  575. this.minPage = minPage;
  576. }
  577. /**
  578. * Specifies the handling of multiple documents, including collation, for
  579. * jobs using these attributes. This attribute is updated to the value
  580. * chosen by the user.
  581. *
  582. * @return
  583. * MultipleDocumentHandlingType.SEPARATE_DOCUMENTS_COLLATED_COPIES or
  584. * MultipleDocumentHandlingType.SEPARATE_DOCUMENTS_UNCOLLATED_COPIES.
  585. */
  586. public MultipleDocumentHandlingType getMultipleDocumentHandling() {
  587. return multipleDocumentHandling;
  588. }
  589. /**
  590. * Specifies the handling of multiple documents, including collation, for
  591. * jobs using these attributes. Not specifying this attribute is equivalent
  592. * to specifying
  593. * MultipleDocumentHandlingType.SEPARATE_DOCUMENTS_UNCOLLATED_COPIES.
  594. *
  595. * @param multipleDocumentHandling
  596. * MultipleDocumentHandlingType.SEPARATE_DOCUMENTS_COLLATED_COPIES or
  597. * MultipleDocumentHandlingType.SEPARATE_DOCUMENTS_UNCOLLATED_COPIES.
  598. * @throws IllegalArgumentException if multipleDocumentHandling is null.
  599. */
  600. public void setMultipleDocumentHandling(MultipleDocumentHandlingType
  601. multipleDocumentHandling) {
  602. if (multipleDocumentHandling == null) {
  603. throw new IllegalArgumentException("Invalid value for attribute "+
  604. "multipleDocumentHandling");
  605. }
  606. this.multipleDocumentHandling = multipleDocumentHandling;
  607. }
  608. /**
  609. * Sets the handling of multiple documents, including collation, for jobs
  610. * using these attributes to the default. The default handling is
  611. * MultipleDocumentHandlingType.SEPARATE_DOCUMENTS_UNCOLLATED_COPIES.
  612. */
  613. public void setMultipleDocumentHandlingToDefault() {
  614. setMultipleDocumentHandling(
  615. MultipleDocumentHandlingType.SEPARATE_DOCUMENTS_UNCOLLATED_COPIES);
  616. }
  617. /**
  618. * Specifies, for jobs using these attributes, the ranges of pages to be
  619. * printed, if a range of pages is to be printed. All range numbers are
  620. * inclusive. This attribute is updated to the value chosen by the user.
  621. * An application should ignore this attribute on output, unless the
  622. * return value of the <code>getDefaultSelection</code> method is
  623. * DefaultSelectionType.RANGE.
  624. *
  625. * @return an array of integer arrays of 2 elements. An array
  626. * is interpreted as a range spanning all pages including and
  627. * between the specified pages. Ranges must be in ascending
  628. * order and must not overlap. Specified page numbers cannot be
  629. * less than <i>minPage</i> nor greater than <i>maxPage</i>.
  630. * For example:
  631. * (new int[][] { new int[] { 1, 3 }, new int[] { 5, 5 },
  632. * new int[] { 15, 19 } }),
  633. * specifies pages 1, 2, 3, 5, 15, 16, 17, 18, and 19.
  634. */
  635. public int[][] getPageRanges() {
  636. if (pageRanges != null) {
  637. // Return a copy because otherwise client code could circumvent the
  638. // the checks made in setPageRanges by modifying the returned
  639. // array.
  640. int[][] copy = new int[pageRanges.length][2];
  641. for (int i = 0; i < pageRanges.length; i++) {
  642. copy[i][0] = pageRanges[i][0];
  643. copy[i][1] = pageRanges[i][1];
  644. }
  645. return copy;
  646. } else if (fromPage != 0 || toPage != 0) {
  647. int fromPage = getFromPage();
  648. int toPage = getToPage();
  649. return new int[][] { new int[] { fromPage, toPage } };
  650. } else {
  651. int minPage = getMinPage();
  652. return new int[][] { new int[] { minPage, minPage } };
  653. }
  654. }
  655. /**
  656. * Specifies, for jobs using these attributes, the ranges of pages to be
  657. * printed, if a range of pages is to be printed. All range numbers are
  658. * inclusive. If this attribute is not specified, then the values from the
  659. * fromPage and toPages attributes are used. If pageRanges and either or
  660. * both of fromPage and toPage are specified, pageRanges takes precedence.
  661. * Specifying none of pageRanges, fromPage, or toPage is equivalent to
  662. * calling setPageRanges(new int[][] { new int[] { <i>minPage</i>,
  663. * <i>minPage</i> } });
  664. *
  665. * @param pageRanges an array of integer arrays of 2 elements. An array
  666. * is interpreted as a range spanning all pages including and
  667. * between the specified pages. Ranges must be in ascending
  668. * order and must not overlap. Specified page numbers cannot be
  669. * less than <i>minPage</i> nor greater than <i>maxPage</i>.
  670. * For example:
  671. * (new int[][] { new int[] { 1, 3 }, new int[] { 5, 5 },
  672. * new int[] { 15, 19 } }),
  673. * specifies pages 1, 2, 3, 5, 15, 16, 17, 18, and 19. Note that
  674. * (new int[][] { new int[] { 1, 1 }, new int[] { 1, 2 } }),
  675. * is an invalid set of page ranges because the two ranges
  676. * overlap.
  677. * @throws IllegalArgumentException if one or more of the above
  678. * conditions is violated.
  679. */
  680. public void setPageRanges(int[][] pageRanges) {
  681. String xcp = "Invalid value for attribute pageRanges";
  682. int first = 0;
  683. int last = 0;
  684. if (pageRanges == null) {
  685. throw new IllegalArgumentException(xcp);
  686. }
  687. for (int i = 0; i < pageRanges.length; i++) {
  688. if (pageRanges[i] == null ||
  689. pageRanges[i].length != 2 ||
  690. pageRanges[i][0] <= last ||
  691. pageRanges[i][1] < pageRanges[i][0]) {
  692. throw new IllegalArgumentException(xcp);
  693. }
  694. last = pageRanges[i][1];
  695. if (first == 0) {
  696. first = pageRanges[i][0];
  697. }
  698. }
  699. if (first < minPage || last > maxPage) {
  700. throw new IllegalArgumentException(xcp);
  701. }
  702. // Store a copy because otherwise client code could circumvent the
  703. // the checks made above by holding a reference to the array and
  704. // modifying it after calling setPageRanges.
  705. int[][] copy = new int[pageRanges.length][2];
  706. for (int i = 0; i < pageRanges.length; i++) {
  707. copy[i][0] = pageRanges[i][0];
  708. copy[i][1] = pageRanges[i][1];
  709. }
  710. this.pageRanges = copy;
  711. this.prFirst = first;
  712. this.prLast = last;
  713. }
  714. /**
  715. * Returns the destination printer for jobs using these attributes. This
  716. * attribute is updated to the value chosen by the user.
  717. *
  718. * @return the possibly null printer name.
  719. */
  720. public String getPrinter() {
  721. return printer;
  722. }
  723. /**
  724. * Specifies the destination printer for jobs using these attributes.
  725. * Default is platform-dependent and implementation-defined.
  726. *
  727. * @param printer the possibly null printer name.
  728. */
  729. public void setPrinter(String printer) {
  730. this.printer = printer;
  731. }
  732. /**
  733. * Returns how consecutive pages should be imposed upon the sides of the
  734. * print medium for jobs using these attributes. SidesType.ONE_SIDED
  735. * imposes each consecutive page upon the same side of consecutive media
  736. * sheets. This imposition is sometimes called <i>simplex</i>.
  737. * SidesType.TWO_SIDED_LONG_EDGE imposes each consecutive pair of pages
  738. * upon front and back sides of consecutive media sheets, such that the
  739. * orientation of each pair of pages on the medium would be correct for
  740. * the reader as if for binding on the long edge. This imposition is
  741. * sometimes called <i>duplex</i>. SidesType.TWO_SIDED_SHORT_EDGE imposes
  742. * each consecutive pair of pages upon front and back sides of consecutive
  743. * media sheets, such that the orientation of each pair of pages on the
  744. * medium would be correct for the reader as if for binding on the short
  745. * edge. This imposition is sometimes called <i>tumble</i>. This attribute
  746. * is updated to the value chosen by the user.
  747. *
  748. * @return SidesType.ONE_SIDED, SidesType.TWO_SIDED_LONG_EDGE, or
  749. * SidesType.TWO_SIDED_SHORT_EDGE.
  750. */
  751. public SidesType getSides() {
  752. return sides;
  753. }
  754. /**
  755. * Specifies how consecutive pages should be imposed upon the sides of the
  756. * print medium for jobs using these attributes. SidesType.ONE_SIDED
  757. * imposes each consecutive page upon the same side of consecutive media
  758. * sheets. This imposition is sometimes called <i>simplex</i>.
  759. * SidesType.TWO_SIDED_LONG_EDGE imposes each consecutive pair of pages
  760. * upon front and back sides of consecutive media sheets, such that the
  761. * orientation of each pair of pages on the medium would be correct for
  762. * the reader as if for binding on the long edge. This imposition is
  763. * sometimes called <i>duplex</i>. SidesType.TWO_SIDED_SHORT_EDGE imposes
  764. * each consecutive pair of pages upon front and back sides of consecutive
  765. * media sheets, such that the orientation of each pair of pages on the
  766. * medium would be correct for the reader as if for binding on the short
  767. * edge. This imposition is sometimes called <i>tumble</i>. Not specifying
  768. * this attribute is equivalent to specifying SidesType.ONE_SIDED.
  769. *
  770. * @param sides SidesType.ONE_SIDED, SidesType.TWO_SIDED_LONG_EDGE, or
  771. * SidesType.TWO_SIDED_SHORT_EDGE.
  772. * @throws IllegalArgumentException if sides is null.
  773. */
  774. public void setSides(SidesType sides) {
  775. if (sides == null) {
  776. throw new IllegalArgumentException("Invalid value for attribute "+
  777. "sides");
  778. }
  779. this.sides = sides;
  780. }
  781. /**
  782. * Sets how consecutive pages should be imposed upon the sides of the
  783. * print medium for jobs using these attributes to the default. The
  784. * default imposition is SidesType.ONE_SIDED.
  785. */
  786. public void setSidesToDefault() {
  787. setSides(SidesType.ONE_SIDED);
  788. }
  789. /**
  790. * Returns, for jobs using these attributes, the last page (inclusive)
  791. * to be printed, if a range of pages is to be printed. This attribute is
  792. * updated to the value chosen by the user. An application should ignore
  793. * this attribute on output, unless the return value of the <code>
  794. * getDefaultSelection</code> method is DefaultSelectionType.RANGE. An
  795. * application should honor the return value of <code>getPageRanges</code>
  796. * over the return value of this method, if possible.
  797. *
  798. * @return an integer greater than zero and greater than or equal
  799. * to <i>toPage</i> and greater than or equal to <i>minPage</i>
  800. * and less than or equal to <i>maxPage</i>.
  801. */
  802. public int getToPage() {
  803. if (toPage != 0) {
  804. return toPage;
  805. } else if (fromPage != 0) {
  806. return fromPage;
  807. } else if (pageRanges != null) {
  808. return prLast;
  809. } else {
  810. return getMinPage();
  811. }
  812. }
  813. /**
  814. * Specifies, for jobs using these attributes, the last page (inclusive)
  815. * to be printed, if a range of pages is to be printed.
  816. * If this attribute is not specified, then the values from the pageRanges
  817. * attribute are used. If pageRanges and either or both of fromPage and
  818. * toPage are specified, pageRanges takes precedence. Specifying none of
  819. * pageRanges, fromPage, or toPage is equivalent to calling
  820. * setPageRanges(new int[][] { new int[] { <i>minPage</i> } });
  821. *
  822. * @param toPage an integer greater than zero and greater than or equal
  823. * to <i>fromPage</i> and greater than or equal to <i>minPage</i>
  824. * and less than or equal to <i>maxPage</i>.
  825. * @throws IllegalArgumentException if one or more of the above
  826. * conditions is violated.
  827. */
  828. public void setToPage(int toPage) {
  829. if (toPage <= 0 ||
  830. (fromPage != 0 && toPage < fromPage) ||
  831. toPage < minPage ||
  832. toPage > maxPage) {
  833. throw new IllegalArgumentException("Invalid value for attribute "+
  834. "toPage");
  835. }
  836. this.toPage = toPage;
  837. }
  838. /**
  839. * Determines whether two JobAttributes are equal to each other.
  840. * <p>
  841. * Two JobAttributes are equal if and only if each of their attributes are
  842. * equal. Attributes of enumeration type are equal if and only if the
  843. * fields refer to the same unique enumeration object. A set of page
  844. * ranges is equal if and only if the sets are of equal length, each range
  845. * enumerates the same pages, and the ranges are in the same order.
  846. *
  847. * @param obj the object whose equality will be checked.
  848. * @return whether obj is equal to this JobAttribute according to the
  849. * above criteria.
  850. */
  851. public boolean equals(Object obj) {
  852. if (!(obj instanceof JobAttributes)) {
  853. return false;
  854. }
  855. JobAttributes rhs = (JobAttributes)obj;
  856. if (fileName == null) {
  857. if (rhs.fileName != null) {
  858. return false;
  859. }
  860. } else {
  861. if (!fileName.equals(rhs.fileName)) {
  862. return false;
  863. }
  864. }
  865. if (pageRanges == null) {
  866. if (rhs.pageRanges != null) {
  867. return false;
  868. }
  869. } else {
  870. if (rhs.pageRanges == null ||
  871. pageRanges.length != rhs.pageRanges.length) {
  872. return false;
  873. }
  874. for (int i = 0; i < pageRanges.length; i++) {
  875. if (pageRanges[i][0] != rhs.pageRanges[i][0] ||
  876. pageRanges[i][1] != rhs.pageRanges[i][1]) {
  877. return false;
  878. }
  879. }
  880. }
  881. if (printer == null) {
  882. if (rhs.printer != null) {
  883. return false;
  884. }
  885. } else {
  886. if (!printer.equals(rhs.printer)) {
  887. return false;
  888. }
  889. }
  890. return (copies == rhs.copies &&
  891. defaultSelection == rhs.defaultSelection &&
  892. destination == rhs.destination &&
  893. dialog == rhs.dialog &&
  894. fromPage == rhs.fromPage &&
  895. maxPage == rhs.maxPage &&
  896. minPage == rhs.minPage &&
  897. multipleDocumentHandling == rhs.multipleDocumentHandling &&
  898. prFirst == rhs.prFirst &&
  899. prLast == rhs.prLast &&
  900. sides == rhs.sides &&
  901. toPage == rhs.toPage);
  902. }
  903. /**
  904. * Returns a hash code value for this JobAttributes.
  905. *
  906. * @return the hash code.
  907. */
  908. public int hashCode() {
  909. int rest = ((copies + fromPage + maxPage + minPage + prFirst + prLast +
  910. toPage) * 31) << 21;
  911. if (pageRanges != null) {
  912. int sum = 0;
  913. for (int i = 0; i < pageRanges.length; i++) {
  914. sum += pageRanges[i][0] + pageRanges[i][1];
  915. }
  916. rest ^= (sum * 31) << 11;
  917. }
  918. if (fileName != null) {
  919. rest ^= fileName.hashCode();
  920. }
  921. if (printer != null) {
  922. rest ^= printer.hashCode();
  923. }
  924. return (defaultSelection.hashCode() << 6 ^
  925. destination.hashCode() << 5 ^
  926. dialog.hashCode() << 3 ^
  927. multipleDocumentHandling.hashCode() << 2 ^
  928. sides.hashCode() ^
  929. rest);
  930. }
  931. /**
  932. * Returns a string representation of this JobAttributes.
  933. *
  934. * @return the string representation.
  935. */
  936. public String toString() {
  937. int[][] pageRanges = getPageRanges();
  938. String prStr = "[";
  939. boolean first = true;
  940. for (int i = 0; i < pageRanges.length; i++) {
  941. if (first) {
  942. first = false;
  943. } else {
  944. prStr += ",";
  945. }
  946. prStr += pageRanges[i][0] + ":" + pageRanges[i][1];
  947. }
  948. prStr += "]";
  949. return "copies=" + getCopies() + ",defaultSelection=" +
  950. getDefaultSelection() + ",destination=" + getDestination() +
  951. ",dialog=" + getDialog() + ",fileName=" + getFileName() +
  952. ",fromPage=" + getFromPage() + ",maxPage=" + getMaxPage() +
  953. ",minPage=" + getMinPage() + ",multiple-document-handling=" +
  954. getMultipleDocumentHandling() + ",page-ranges=" + prStr +
  955. ",printer=" + getPrinter() + ",sides=" + getSides() + ",toPage=" +
  956. getToPage();
  957. }
  958. }