下面列出了怎么用java.awt.JobAttributes的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Sets all of the attributes of this <code>JobAttributes</code> to
* the same values as the attributes of obj.
*
* @param obj the <code>JobAttributes</code> to copy
*/
public void set(JobAttributes obj) {
copies = obj.copies;
defaultSelection = obj.defaultSelection;
destination = obj.destination;
dialog = obj.dialog;
fileName = obj.fileName;
fromPage = obj.fromPage;
maxPage = obj.maxPage;
minPage = obj.minPage;
multipleDocumentHandling = obj.multipleDocumentHandling;
// okay because we never modify the contents of pageRanges
pageRanges = obj.pageRanges;
prFirst = obj.prFirst;
prLast = obj.prLast;
printer = obj.printer;
sides = obj.sides;
toPage = obj.toPage;
}
/**
* Sets all of the attributes of this <code>JobAttributes</code> to
* the same values as the attributes of obj.
*
* @param obj the <code>JobAttributes</code> to copy
*/
public void set(JobAttributes obj) {
copies = obj.copies;
defaultSelection = obj.defaultSelection;
destination = obj.destination;
dialog = obj.dialog;
fileName = obj.fileName;
fromPage = obj.fromPage;
maxPage = obj.maxPage;
minPage = obj.minPage;
multipleDocumentHandling = obj.multipleDocumentHandling;
// okay because we never modify the contents of pageRanges
pageRanges = obj.pageRanges;
prFirst = obj.prFirst;
prLast = obj.prLast;
printer = obj.printer;
sides = obj.sides;
toPage = obj.toPage;
}
/**
* Sets all of the attributes of this <code>JobAttributes</code> to
* the same values as the attributes of obj.
*
* @param obj the <code>JobAttributes</code> to copy
*/
public void set(JobAttributes obj) {
copies = obj.copies;
defaultSelection = obj.defaultSelection;
destination = obj.destination;
dialog = obj.dialog;
fileName = obj.fileName;
fromPage = obj.fromPage;
maxPage = obj.maxPage;
minPage = obj.minPage;
multipleDocumentHandling = obj.multipleDocumentHandling;
// okay because we never modify the contents of pageRanges
pageRanges = obj.pageRanges;
prFirst = obj.prFirst;
prLast = obj.prLast;
printer = obj.printer;
sides = obj.sides;
toPage = obj.toPage;
}
/**
* Sets all of the attributes of this <code>JobAttributes</code> to
* the same values as the attributes of obj.
*
* @param obj the <code>JobAttributes</code> to copy
*/
public void set(JobAttributes obj) {
copies = obj.copies;
defaultSelection = obj.defaultSelection;
destination = obj.destination;
dialog = obj.dialog;
fileName = obj.fileName;
fromPage = obj.fromPage;
maxPage = obj.maxPage;
minPage = obj.minPage;
multipleDocumentHandling = obj.multipleDocumentHandling;
// okay because we never modify the contents of pageRanges
pageRanges = obj.pageRanges;
prFirst = obj.prFirst;
prLast = obj.prLast;
printer = obj.printer;
sides = obj.sides;
toPage = obj.toPage;
}
/**
* Sets all of the attributes of this <code>JobAttributes</code> to
* the same values as the attributes of obj.
*
* @param obj the <code>JobAttributes</code> to copy
*/
public void set(JobAttributes obj) {
copies = obj.copies;
defaultSelection = obj.defaultSelection;
destination = obj.destination;
dialog = obj.dialog;
fileName = obj.fileName;
fromPage = obj.fromPage;
maxPage = obj.maxPage;
minPage = obj.minPage;
multipleDocumentHandling = obj.multipleDocumentHandling;
// okay because we never modify the contents of pageRanges
pageRanges = obj.pageRanges;
prFirst = obj.prFirst;
prLast = obj.prLast;
printer = obj.printer;
sides = obj.sides;
toPage = obj.toPage;
}
/**
* Sets all of the attributes of this <code>JobAttributes</code> to
* the same values as the attributes of obj.
*
* @param obj the <code>JobAttributes</code> to copy
*/
public void set(JobAttributes obj) {
copies = obj.copies;
defaultSelection = obj.defaultSelection;
destination = obj.destination;
dialog = obj.dialog;
fileName = obj.fileName;
fromPage = obj.fromPage;
maxPage = obj.maxPage;
minPage = obj.minPage;
multipleDocumentHandling = obj.multipleDocumentHandling;
// okay because we never modify the contents of pageRanges
pageRanges = obj.pageRanges;
prFirst = obj.prFirst;
prLast = obj.prLast;
printer = obj.printer;
sides = obj.sides;
toPage = obj.toPage;
}
/**
* Sets all of the attributes of this <code>JobAttributes</code> to
* the same values as the attributes of obj.
*
* @param obj the <code>JobAttributes</code> to copy
*/
public void set(JobAttributes obj) {
copies = obj.copies;
defaultSelection = obj.defaultSelection;
destination = obj.destination;
dialog = obj.dialog;
fileName = obj.fileName;
fromPage = obj.fromPage;
maxPage = obj.maxPage;
minPage = obj.minPage;
multipleDocumentHandling = obj.multipleDocumentHandling;
// okay because we never modify the contents of pageRanges
pageRanges = obj.pageRanges;
prFirst = obj.prFirst;
prLast = obj.prLast;
printer = obj.printer;
sides = obj.sides;
toPage = obj.toPage;
}
/**
* Sets all of the attributes of this <code>JobAttributes</code> to
* the same values as the attributes of obj.
*
* @param obj the <code>JobAttributes</code> to copy
*/
public void set(JobAttributes obj) {
copies = obj.copies;
defaultSelection = obj.defaultSelection;
destination = obj.destination;
dialog = obj.dialog;
fileName = obj.fileName;
fromPage = obj.fromPage;
maxPage = obj.maxPage;
minPage = obj.minPage;
multipleDocumentHandling = obj.multipleDocumentHandling;
// okay because we never modify the contents of pageRanges
pageRanges = obj.pageRanges;
prFirst = obj.prFirst;
prLast = obj.prLast;
printer = obj.printer;
sides = obj.sides;
toPage = obj.toPage;
}
/**
* Sets all of the attributes of this {@code JobAttributes} to
* the same values as the attributes of obj.
*
* @param obj the {@code JobAttributes} to copy
*/
public void set(JobAttributes obj) {
copies = obj.copies;
defaultSelection = obj.defaultSelection;
destination = obj.destination;
dialog = obj.dialog;
fileName = obj.fileName;
fromPage = obj.fromPage;
maxPage = obj.maxPage;
minPage = obj.minPage;
multipleDocumentHandling = obj.multipleDocumentHandling;
// okay because we never modify the contents of pageRanges
pageRanges = obj.pageRanges;
prFirst = obj.prFirst;
prLast = obj.prLast;
printer = obj.printer;
sides = obj.sides;
toPage = obj.toPage;
}
/**
* Sets all of the attributes of this {@code JobAttributes} to
* the same values as the attributes of obj.
*
* @param obj the {@code JobAttributes} to copy
*/
public void set(JobAttributes obj) {
copies = obj.copies;
defaultSelection = obj.defaultSelection;
destination = obj.destination;
dialog = obj.dialog;
fileName = obj.fileName;
fromPage = obj.fromPage;
maxPage = obj.maxPage;
minPage = obj.minPage;
multipleDocumentHandling = obj.multipleDocumentHandling;
// okay because we never modify the contents of pageRanges
pageRanges = obj.pageRanges;
prFirst = obj.prFirst;
prLast = obj.prLast;
printer = obj.printer;
sides = obj.sides;
toPage = obj.toPage;
}
/**
* Sets all of the attributes of this <code>JobAttributes</code> to
* the same values as the attributes of obj.
*
* @param obj the <code>JobAttributes</code> to copy
*/
public void set(JobAttributes obj) {
copies = obj.copies;
defaultSelection = obj.defaultSelection;
destination = obj.destination;
dialog = obj.dialog;
fileName = obj.fileName;
fromPage = obj.fromPage;
maxPage = obj.maxPage;
minPage = obj.minPage;
multipleDocumentHandling = obj.multipleDocumentHandling;
// okay because we never modify the contents of pageRanges
pageRanges = obj.pageRanges;
prFirst = obj.prFirst;
prLast = obj.prLast;
printer = obj.printer;
sides = obj.sides;
toPage = obj.toPage;
}
/**
* Sets all of the attributes of this <code>JobAttributes</code> to
* the same values as the attributes of obj.
*
* @param obj the <code>JobAttributes</code> to copy
*/
public void set(JobAttributes obj) {
copies = obj.copies;
defaultSelection = obj.defaultSelection;
destination = obj.destination;
dialog = obj.dialog;
fileName = obj.fileName;
fromPage = obj.fromPage;
maxPage = obj.maxPage;
minPage = obj.minPage;
multipleDocumentHandling = obj.multipleDocumentHandling;
// okay because we never modify the contents of pageRanges
pageRanges = obj.pageRanges;
prFirst = obj.prFirst;
prLast = obj.prLast;
printer = obj.printer;
sides = obj.sides;
toPage = obj.toPage;
}
public PrintJob2D(Frame frame, String doctitle,
final Properties props) {
this.props = props;
this.jobAttributes = new JobAttributes();
this.pageAttributes = new PageAttributes();
translateInputProps();
initPrintJob2D(frame, doctitle,
this.jobAttributes, this.pageAttributes);
}
public PrintJob2D(Frame frame, String doctitle,
final Properties props) {
this.props = props;
this.jobAttributes = new JobAttributes();
this.pageAttributes = new PageAttributes();
translateInputProps();
initPrintJob2D(frame, doctitle,
this.jobAttributes, this.pageAttributes);
}
public PrintJob2D(Frame frame, String doctitle,
final Properties props) {
this.props = props;
this.jobAttributes = new JobAttributes();
this.pageAttributes = new PageAttributes();
translateInputProps();
initPrintJob2D(frame, doctitle,
this.jobAttributes, this.pageAttributes);
}
public PrintJob2D(Frame frame, String doctitle,
final Properties props) {
this.props = props;
this.jobAttributes = new JobAttributes();
this.pageAttributes = new PageAttributes();
translateInputProps();
initPrintJob2D(frame, doctitle,
this.jobAttributes, this.pageAttributes);
}
public static void main(String[] args) throws Exception {
Robot robot = new Robot();
Thread t = new Thread (() -> {
robot.waitForIdle();
robot.delay(2000);
robot.keyPress(KeyEvent.VK_ESCAPE);
robot.keyRelease(KeyEvent.VK_ESCAPE);
});
Toolkit tk = Toolkit.getDefaultToolkit();
PrintJob pj = null;
int[][] pageRange = new int[][]{new int[]{1,1}};
JobAttributes ja = new JobAttributes(1,
java.awt.JobAttributes.DefaultSelectionType.ALL,
JobAttributes.DestinationType.FILE, JobAttributes.DialogType.NATIVE,
"filename.ps", Integer.MAX_VALUE, 1,
JobAttributes.MultipleDocumentHandlingType.SEPARATE_DOCUMENTS_UNCOLLATED_COPIES,
pageRange, "", JobAttributes.SidesType.ONE_SIDED);
Frame testFrame = new Frame("print");
try {
if (tk != null) {
t.start();
pj = tk.getPrintJob(testFrame, null, ja, null);
}
} finally {
testFrame.dispose();
}
}
public static void main(String[] args) {
JobAttributes ja = new JobAttributes();
ja.setDialog(JobAttributes.DialogType.COMMON);
boolean npeThrown = false;
try {
Toolkit.getDefaultToolkit().getPrintJob(null,
"test Printing", ja, null);
} catch (NullPointerException ex) {
npeThrown = true;
}
if (!npeThrown) {
throw
new RuntimeException("getPrintJob didn't throw NPE for null Frame");
}
}
public PrintJob2D(Frame frame, String doctitle,
final Properties props) {
this.props = props;
this.jobAttributes = new JobAttributes();
this.pageAttributes = new PageAttributes();
translateInputProps();
initPrintJob2D(frame, doctitle,
this.jobAttributes, this.pageAttributes);
}
/**
* Determines whether two JobAttributes are equal to each other.
* <p>
* Two JobAttributes are equal if and only if each of their attributes are
* equal. Attributes of enumeration type are equal if and only if the
* fields refer to the same unique enumeration object. A set of page
* ranges is equal if and only if the sets are of equal length, each range
* enumerates the same pages, and the ranges are in the same order.
*
* @param obj the object whose equality will be checked.
* @return whether obj is equal to this JobAttribute according to the
* above criteria.
*/
public boolean equals(Object obj) {
if (!(obj instanceof JobAttributes)) {
return false;
}
JobAttributes rhs = (JobAttributes)obj;
if (fileName == null) {
if (rhs.fileName != null) {
return false;
}
} else {
if (!fileName.equals(rhs.fileName)) {
return false;
}
}
if (pageRanges == null) {
if (rhs.pageRanges != null) {
return false;
}
} else {
if (rhs.pageRanges == null ||
pageRanges.length != rhs.pageRanges.length) {
return false;
}
for (int i = 0; i < pageRanges.length; i++) {
if (pageRanges[i][0] != rhs.pageRanges[i][0] ||
pageRanges[i][1] != rhs.pageRanges[i][1]) {
return false;
}
}
}
if (printer == null) {
if (rhs.printer != null) {
return false;
}
} else {
if (!printer.equals(rhs.printer)) {
return false;
}
}
return (copies == rhs.copies &&
defaultSelection == rhs.defaultSelection &&
destination == rhs.destination &&
dialog == rhs.dialog &&
fromPage == rhs.fromPage &&
maxPage == rhs.maxPage &&
minPage == rhs.minPage &&
multipleDocumentHandling == rhs.multipleDocumentHandling &&
prFirst == rhs.prFirst &&
prLast == rhs.prLast &&
sides == rhs.sides &&
toPage == rhs.toPage);
}
/**
* Determines whether two JobAttributes are equal to each other.
* <p>
* Two JobAttributes are equal if and only if each of their attributes are
* equal. Attributes of enumeration type are equal if and only if the
* fields refer to the same unique enumeration object. A set of page
* ranges is equal if and only if the sets are of equal length, each range
* enumerates the same pages, and the ranges are in the same order.
*
* @param obj the object whose equality will be checked.
* @return whether obj is equal to this JobAttribute according to the
* above criteria.
*/
public boolean equals(Object obj) {
if (!(obj instanceof JobAttributes)) {
return false;
}
JobAttributes rhs = (JobAttributes)obj;
if (fileName == null) {
if (rhs.fileName != null) {
return false;
}
} else {
if (!fileName.equals(rhs.fileName)) {
return false;
}
}
if (pageRanges == null) {
if (rhs.pageRanges != null) {
return false;
}
} else {
if (rhs.pageRanges == null ||
pageRanges.length != rhs.pageRanges.length) {
return false;
}
for (int i = 0; i < pageRanges.length; i++) {
if (pageRanges[i][0] != rhs.pageRanges[i][0] ||
pageRanges[i][1] != rhs.pageRanges[i][1]) {
return false;
}
}
}
if (printer == null) {
if (rhs.printer != null) {
return false;
}
} else {
if (!printer.equals(rhs.printer)) {
return false;
}
}
return (copies == rhs.copies &&
defaultSelection == rhs.defaultSelection &&
destination == rhs.destination &&
dialog == rhs.dialog &&
fromPage == rhs.fromPage &&
maxPage == rhs.maxPage &&
minPage == rhs.minPage &&
multipleDocumentHandling == rhs.multipleDocumentHandling &&
prFirst == rhs.prFirst &&
prLast == rhs.prLast &&
sides == rhs.sides &&
toPage == rhs.toPage);
}
/**
* Determines whether two JobAttributes are equal to each other.
* <p>
* Two JobAttributes are equal if and only if each of their attributes are
* equal. Attributes of enumeration type are equal if and only if the
* fields refer to the same unique enumeration object. A set of page
* ranges is equal if and only if the sets are of equal length, each range
* enumerates the same pages, and the ranges are in the same order.
*
* @param obj the object whose equality will be checked.
* @return whether obj is equal to this JobAttribute according to the
* above criteria.
*/
public boolean equals(Object obj) {
if (!(obj instanceof JobAttributes)) {
return false;
}
JobAttributes rhs = (JobAttributes)obj;
if (fileName == null) {
if (rhs.fileName != null) {
return false;
}
} else {
if (!fileName.equals(rhs.fileName)) {
return false;
}
}
if (pageRanges == null) {
if (rhs.pageRanges != null) {
return false;
}
} else {
if (rhs.pageRanges == null ||
pageRanges.length != rhs.pageRanges.length) {
return false;
}
for (int i = 0; i < pageRanges.length; i++) {
if (pageRanges[i][0] != rhs.pageRanges[i][0] ||
pageRanges[i][1] != rhs.pageRanges[i][1]) {
return false;
}
}
}
if (printer == null) {
if (rhs.printer != null) {
return false;
}
} else {
if (!printer.equals(rhs.printer)) {
return false;
}
}
return (copies == rhs.copies &&
defaultSelection == rhs.defaultSelection &&
destination == rhs.destination &&
dialog == rhs.dialog &&
fromPage == rhs.fromPage &&
maxPage == rhs.maxPage &&
minPage == rhs.minPage &&
multipleDocumentHandling == rhs.multipleDocumentHandling &&
prFirst == rhs.prFirst &&
prLast == rhs.prLast &&
sides == rhs.sides &&
toPage == rhs.toPage);
}
/**
* Constructs a <code>JobAttributes</code> instance with the
* specified values for every attribute.
*
* @param copies an integer greater than 0
* @param defaultSelection <code>DefaultSelectionType.ALL</code>,
* <code>DefaultSelectionType.RANGE</code>, or
* <code>DefaultSelectionType.SELECTION</code>
* @param destination <code>DesintationType.FILE</code> or
* <code>DesintationType.PRINTER</code>
* @param dialog <code>DialogType.COMMON</code>,
* <code>DialogType.NATIVE</code>, or
* <code>DialogType.NONE</code>
* @param fileName the possibly <code>null</code> file name
* @param maxPage an integer greater than zero and greater than or equal
* to <i>minPage</i>
* @param minPage an integer greater than zero and less than or equal
* to <i>maxPage</i>
* @param multipleDocumentHandling
* <code>MultipleDocumentHandlingType.SEPARATE_DOCUMENTS_COLLATED_COPIES</code> or
* <code>MultipleDocumentHandlingType.SEPARATE_DOCUMENTS_UNCOLLATED_COPIES</code>
* @param pageRanges an array of integer arrays of two elements; an array
* is interpreted as a range spanning all pages including and
* between the specified pages; ranges must be in ascending
* order and must not overlap; specified page numbers cannot be
* less than <i>minPage</i> nor greater than <i>maxPage</i>;
* for example:
* <pre>
* (new int[][] { new int[] { 1, 3 }, new int[] { 5, 5 },
* new int[] { 15, 19 } }),
* </pre>
* specifies pages 1, 2, 3, 5, 15, 16, 17, 18, and 19. Note that
* (<code>new int[][] { new int[] { 1, 1 }, new int[] { 1, 2 } }</code>),
* is an invalid set of page ranges because the two ranges
* overlap
* @param printer the possibly <code>null</code> printer name
* @param sides <code>SidesType.ONE_SIDED</code>,
* <code>SidesType.TWO_SIDED_LONG_EDGE</code>, or
* <code>SidesType.TWO_SIDED_SHORT_EDGE</code>
* @throws IllegalArgumentException if one or more of the above
* conditions is violated
*/
public JobAttributes(int copies, DefaultSelectionType defaultSelection,
DestinationType destination, DialogType dialog,
String fileName, int maxPage, int minPage,
MultipleDocumentHandlingType multipleDocumentHandling,
int[][] pageRanges, String printer, SidesType sides) {
setCopies(copies);
setDefaultSelection(defaultSelection);
setDestination(destination);
setDialog(dialog);
setFileName(fileName);
setMaxPage(maxPage);
setMinPage(minPage);
setMultipleDocumentHandling(multipleDocumentHandling);
setPageRanges(pageRanges);
setPrinter(printer);
setSides(sides);
}
/**
* Determines whether two JobAttributes are equal to each other.
* <p>
* Two JobAttributes are equal if and only if each of their attributes are
* equal. Attributes of enumeration type are equal if and only if the
* fields refer to the same unique enumeration object. A set of page
* ranges is equal if and only if the sets are of equal length, each range
* enumerates the same pages, and the ranges are in the same order.
*
* @param obj the object whose equality will be checked.
* @return whether obj is equal to this JobAttribute according to the
* above criteria.
*/
public boolean equals(Object obj) {
if (!(obj instanceof JobAttributes)) {
return false;
}
JobAttributes rhs = (JobAttributes)obj;
if (fileName == null) {
if (rhs.fileName != null) {
return false;
}
} else {
if (!fileName.equals(rhs.fileName)) {
return false;
}
}
if (pageRanges == null) {
if (rhs.pageRanges != null) {
return false;
}
} else {
if (rhs.pageRanges == null ||
pageRanges.length != rhs.pageRanges.length) {
return false;
}
for (int i = 0; i < pageRanges.length; i++) {
if (pageRanges[i][0] != rhs.pageRanges[i][0] ||
pageRanges[i][1] != rhs.pageRanges[i][1]) {
return false;
}
}
}
if (printer == null) {
if (rhs.printer != null) {
return false;
}
} else {
if (!printer.equals(rhs.printer)) {
return false;
}
}
return (copies == rhs.copies &&
defaultSelection == rhs.defaultSelection &&
destination == rhs.destination &&
dialog == rhs.dialog &&
fromPage == rhs.fromPage &&
maxPage == rhs.maxPage &&
minPage == rhs.minPage &&
multipleDocumentHandling == rhs.multipleDocumentHandling &&
prFirst == rhs.prFirst &&
prLast == rhs.prLast &&
sides == rhs.sides &&
toPage == rhs.toPage);
}
/**
* Determines whether two JobAttributes are equal to each other.
* <p>
* Two JobAttributes are equal if and only if each of their attributes are
* equal. Attributes of enumeration type are equal if and only if the
* fields refer to the same unique enumeration object. A set of page
* ranges is equal if and only if the sets are of equal length, each range
* enumerates the same pages, and the ranges are in the same order.
*
* @param obj the object whose equality will be checked.
* @return whether obj is equal to this JobAttribute according to the
* above criteria.
*/
public boolean equals(Object obj) {
if (!(obj instanceof JobAttributes)) {
return false;
}
JobAttributes rhs = (JobAttributes)obj;
if (fileName == null) {
if (rhs.fileName != null) {
return false;
}
} else {
if (!fileName.equals(rhs.fileName)) {
return false;
}
}
if (pageRanges == null) {
if (rhs.pageRanges != null) {
return false;
}
} else {
if (rhs.pageRanges == null ||
pageRanges.length != rhs.pageRanges.length) {
return false;
}
for (int i = 0; i < pageRanges.length; i++) {
if (pageRanges[i][0] != rhs.pageRanges[i][0] ||
pageRanges[i][1] != rhs.pageRanges[i][1]) {
return false;
}
}
}
if (printer == null) {
if (rhs.printer != null) {
return false;
}
} else {
if (!printer.equals(rhs.printer)) {
return false;
}
}
return (copies == rhs.copies &&
defaultSelection == rhs.defaultSelection &&
destination == rhs.destination &&
dialog == rhs.dialog &&
fromPage == rhs.fromPage &&
maxPage == rhs.maxPage &&
minPage == rhs.minPage &&
multipleDocumentHandling == rhs.multipleDocumentHandling &&
prFirst == rhs.prFirst &&
prLast == rhs.prLast &&
sides == rhs.sides &&
toPage == rhs.toPage);
}
private void initPrintJob2D(Frame frame, String doctitle,
JobAttributes jobAttributes,
PageAttributes pageAttributes) {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkPrintJobAccess();
}
if (frame == null &&
(jobAttributes == null ||
jobAttributes.getDialog() == DialogType.NATIVE)) {
throw new NullPointerException("Frame must not be null");
}
this.frame = frame;
this.docTitle = (doctitle == null) ? "" : doctitle;
this.jobAttributes = (jobAttributes != null)
? jobAttributes : new JobAttributes();
this.pageAttributes = (pageAttributes != null)
? pageAttributes : new PageAttributes();
// Currently, we always reduce page ranges to xxx or xxx-xxx
int[][] pageRanges = this.jobAttributes.getPageRanges();
int first = pageRanges[0][0];
int last = pageRanges[pageRanges.length - 1][1];
this.jobAttributes.setPageRanges(new int[][] {
new int[] { first, last }
});
this.jobAttributes.setToPage(last);
this.jobAttributes.setFromPage(first);
// Verify that the cross feed and feed resolutions are the same
int[] res = this.pageAttributes.getPrinterResolution();
if (res[0] != res[1]) {
throw new IllegalArgumentException("Differing cross feed and feed"+
" resolutions not supported.");
}
// Verify that the app has access to the file system
DestinationType dest= this.jobAttributes.getDestination();
if (dest == DestinationType.FILE) {
throwPrintToFile();
// check if given filename is valid
String destStr = jobAttributes.getFileName();
if ((destStr != null) &&
(jobAttributes.getDialog() == JobAttributes.DialogType.NONE)) {
File f = new File(destStr);
try {
// check if this is a new file and if filename chars are valid
// createNewFile returns false if file exists
if (f.createNewFile()) {
f.delete();
}
} catch (IOException ioe) {
throw new IllegalArgumentException("Cannot write to file:"+
destStr);
} catch (SecurityException se) {
//There is already file read/write access so at this point
// only delete access is denied. Just ignore it because in
// most cases the file created in createNewFile gets overwritten
// anyway.
}
File pFile = f.getParentFile();
if ((f.exists() &&
(!f.isFile() || !f.canWrite())) ||
((pFile != null) &&
(!pFile.exists() || (pFile.exists() && !pFile.canWrite())))) {
throw new IllegalArgumentException("Cannot write to file:"+
destStr);
}
}
}
}
/**
* Determines whether two JobAttributes are equal to each other.
* <p>
* Two JobAttributes are equal if and only if each of their attributes are
* equal. Attributes of enumeration type are equal if and only if the
* fields refer to the same unique enumeration object. A set of page
* ranges is equal if and only if the sets are of equal length, each range
* enumerates the same pages, and the ranges are in the same order.
*
* @param obj the object whose equality will be checked.
* @return whether obj is equal to this JobAttribute according to the
* above criteria.
*/
public boolean equals(Object obj) {
if (!(obj instanceof JobAttributes)) {
return false;
}
JobAttributes rhs = (JobAttributes)obj;
if (fileName == null) {
if (rhs.fileName != null) {
return false;
}
} else {
if (!fileName.equals(rhs.fileName)) {
return false;
}
}
if (pageRanges == null) {
if (rhs.pageRanges != null) {
return false;
}
} else {
if (rhs.pageRanges == null ||
pageRanges.length != rhs.pageRanges.length) {
return false;
}
for (int i = 0; i < pageRanges.length; i++) {
if (pageRanges[i][0] != rhs.pageRanges[i][0] ||
pageRanges[i][1] != rhs.pageRanges[i][1]) {
return false;
}
}
}
if (printer == null) {
if (rhs.printer != null) {
return false;
}
} else {
if (!printer.equals(rhs.printer)) {
return false;
}
}
return (copies == rhs.copies &&
defaultSelection == rhs.defaultSelection &&
destination == rhs.destination &&
dialog == rhs.dialog &&
fromPage == rhs.fromPage &&
maxPage == rhs.maxPage &&
minPage == rhs.minPage &&
multipleDocumentHandling == rhs.multipleDocumentHandling &&
prFirst == rhs.prFirst &&
prLast == rhs.prLast &&
sides == rhs.sides &&
toPage == rhs.toPage);
}
/**
* Constructs a <code>JobAttributes</code> instance with the
* specified values for every attribute.
*
* @param copies an integer greater than 0
* @param defaultSelection <code>DefaultSelectionType.ALL</code>,
* <code>DefaultSelectionType.RANGE</code>, or
* <code>DefaultSelectionType.SELECTION</code>
* @param destination <code>DesintationType.FILE</code> or
* <code>DesintationType.PRINTER</code>
* @param dialog <code>DialogType.COMMON</code>,
* <code>DialogType.NATIVE</code>, or
* <code>DialogType.NONE</code>
* @param fileName the possibly <code>null</code> file name
* @param maxPage an integer greater than zero and greater than or equal
* to <i>minPage</i>
* @param minPage an integer greater than zero and less than or equal
* to <i>maxPage</i>
* @param multipleDocumentHandling
* <code>MultipleDocumentHandlingType.SEPARATE_DOCUMENTS_COLLATED_COPIES</code> or
* <code>MultipleDocumentHandlingType.SEPARATE_DOCUMENTS_UNCOLLATED_COPIES</code>
* @param pageRanges an array of integer arrays of two elements; an array
* is interpreted as a range spanning all pages including and
* between the specified pages; ranges must be in ascending
* order and must not overlap; specified page numbers cannot be
* less than <i>minPage</i> nor greater than <i>maxPage</i>;
* for example:
* <pre>
* (new int[][] { new int[] { 1, 3 }, new int[] { 5, 5 },
* new int[] { 15, 19 } }),
* </pre>
* specifies pages 1, 2, 3, 5, 15, 16, 17, 18, and 19. Note that
* (<code>new int[][] { new int[] { 1, 1 }, new int[] { 1, 2 } }</code>),
* is an invalid set of page ranges because the two ranges
* overlap
* @param printer the possibly <code>null</code> printer name
* @param sides <code>SidesType.ONE_SIDED</code>,
* <code>SidesType.TWO_SIDED_LONG_EDGE</code>, or
* <code>SidesType.TWO_SIDED_SHORT_EDGE</code>
* @throws IllegalArgumentException if one or more of the above
* conditions is violated
*/
public JobAttributes(int copies, DefaultSelectionType defaultSelection,
DestinationType destination, DialogType dialog,
String fileName, int maxPage, int minPage,
MultipleDocumentHandlingType multipleDocumentHandling,
int[][] pageRanges, String printer, SidesType sides) {
setCopies(copies);
setDefaultSelection(defaultSelection);
setDestination(destination);
setDialog(dialog);
setFileName(fileName);
setMaxPage(maxPage);
setMinPage(minPage);
setMultipleDocumentHandling(multipleDocumentHandling);
setPageRanges(pageRanges);
setPrinter(printer);
setSides(sides);
}
private void initPrintJob2D(Frame frame, String doctitle,
JobAttributes jobAttributes,
PageAttributes pageAttributes) {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkPrintJobAccess();
}
if (frame == null &&
(jobAttributes == null ||
jobAttributes.getDialog() == DialogType.NATIVE)) {
throw new NullPointerException("Frame must not be null");
}
this.frame = frame;
this.docTitle = (doctitle == null) ? "" : doctitle;
this.jobAttributes = (jobAttributes != null)
? jobAttributes : new JobAttributes();
this.pageAttributes = (pageAttributes != null)
? pageAttributes : new PageAttributes();
// Currently, we always reduce page ranges to xxx or xxx-xxx
int[][] pageRanges = this.jobAttributes.getPageRanges();
int first = pageRanges[0][0];
int last = pageRanges[pageRanges.length - 1][1];
this.jobAttributes.setPageRanges(new int[][] {
new int[] { first, last }
});
this.jobAttributes.setToPage(last);
this.jobAttributes.setFromPage(first);
// Verify that the cross feed and feed resolutions are the same
int[] res = this.pageAttributes.getPrinterResolution();
if (res[0] != res[1]) {
throw new IllegalArgumentException("Differing cross feed and feed"+
" resolutions not supported.");
}
// Verify that the app has access to the file system
DestinationType dest= this.jobAttributes.getDestination();
if (dest == DestinationType.FILE) {
throwPrintToFile();
// check if given filename is valid
String destStr = jobAttributes.getFileName();
if ((destStr != null) &&
(jobAttributes.getDialog() == JobAttributes.DialogType.NONE)) {
File f = new File(destStr);
try {
// check if this is a new file and if filename chars are valid
// createNewFile returns false if file exists
if (f.createNewFile()) {
f.delete();
}
} catch (IOException ioe) {
throw new IllegalArgumentException("Cannot write to file:"+
destStr);
} catch (SecurityException se) {
//There is already file read/write access so at this point
// only delete access is denied. Just ignore it because in
// most cases the file created in createNewFile gets overwritten
// anyway.
}
File pFile = f.getParentFile();
if ((f.exists() &&
(!f.isFile() || !f.canWrite())) ||
((pFile != null) &&
(!pFile.exists() || (pFile.exists() && !pFile.canWrite())))) {
throw new IllegalArgumentException("Cannot write to file:"+
destStr);
}
}
}
}
private void initPrintJob2D(Frame frame, String doctitle,
JobAttributes jobAttributes,
PageAttributes pageAttributes) {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkPrintJobAccess();
}
if (frame == null &&
(jobAttributes == null ||
jobAttributes.getDialog() == DialogType.NATIVE)) {
throw new NullPointerException("Frame must not be null");
}
this.frame = frame;
this.docTitle = (doctitle == null) ? "" : doctitle;
this.jobAttributes = (jobAttributes != null)
? jobAttributes : new JobAttributes();
this.pageAttributes = (pageAttributes != null)
? pageAttributes : new PageAttributes();
// Currently, we always reduce page ranges to xxx or xxx-xxx
int[][] pageRanges = this.jobAttributes.getPageRanges();
int first = pageRanges[0][0];
int last = pageRanges[pageRanges.length - 1][1];
this.jobAttributes.setPageRanges(new int[][] {
new int[] { first, last }
});
this.jobAttributes.setToPage(last);
this.jobAttributes.setFromPage(first);
// Verify that the cross feed and feed resolutions are the same
int[] res = this.pageAttributes.getPrinterResolution();
if (res[0] != res[1]) {
throw new IllegalArgumentException("Differing cross feed and feed"+
" resolutions not supported.");
}
// Verify that the app has access to the file system
DestinationType dest= this.jobAttributes.getDestination();
if (dest == DestinationType.FILE) {
throwPrintToFile();
// check if given filename is valid
String destStr = jobAttributes.getFileName();
if ((destStr != null) &&
(jobAttributes.getDialog() == JobAttributes.DialogType.NONE)) {
File f = new File(destStr);
try {
// check if this is a new file and if filename chars are valid
// createNewFile returns false if file exists
if (f.createNewFile()) {
f.delete();
}
} catch (IOException ioe) {
throw new IllegalArgumentException("Cannot write to file:"+
destStr);
} catch (SecurityException se) {
//There is already file read/write access so at this point
// only delete access is denied. Just ignore it because in
// most cases the file created in createNewFile gets overwritten
// anyway.
}
File pFile = f.getParentFile();
if ((f.exists() &&
(!f.isFile() || !f.canWrite())) ||
((pFile != null) &&
(!pFile.exists() || (pFile.exists() && !pFile.canWrite())))) {
throw new IllegalArgumentException("Cannot write to file:"+
destStr);
}
}
}
}