use of javax.print.attribute.standard.Destination in project jdk8u_jdk by JetBrains.
the class PrintCrashTest method main.
public static void main(String[] args) throws Exception {
PrinterJob printerJob = PrinterJob.getPrinterJob();
printerJob.setPrintable((graphics, pageFormat, pageIndex) -> {
if (pageIndex != 0) {
return Printable.NO_SUCH_PAGE;
} else {
Shape shape = new Rectangle(110, 110, 10, 10);
Rectangle rect = shape.getBounds();
BufferedImage image = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().createCompatibleImage(rect.width, rect.height, Transparency.BITMASK);
graphics.drawImage(image, rect.x, rect.y, rect.width, rect.height, null);
return Printable.PAGE_EXISTS;
}
});
File file = null;
try {
HashPrintRequestAttributeSet hashPrintRequestAttributeSet = new HashPrintRequestAttributeSet();
file = File.createTempFile("out", "ps");
file.deleteOnExit();
Destination destination = new Destination(file.toURI());
hashPrintRequestAttributeSet.add(destination);
printerJob.print(hashPrintRequestAttributeSet);
} finally {
if (file != null) {
file.delete();
}
}
}
use of javax.print.attribute.standard.Destination in project jdk8u_jdk by JetBrains.
the class PSPrinterJob method printDialog.
/* Instance Methods */
/**
* Presents the user a dialog for changing properties of the
* print job interactively.
* @returns false if the user cancels the dialog and
* true otherwise.
* @exception HeadlessException if GraphicsEnvironment.isHeadless()
* returns true.
* @see java.awt.GraphicsEnvironment#isHeadless
*/
public boolean printDialog() throws HeadlessException {
if (GraphicsEnvironment.isHeadless()) {
throw new HeadlessException();
}
if (attributes == null) {
attributes = new HashPrintRequestAttributeSet();
}
attributes.add(new Copies(getCopies()));
attributes.add(new JobName(getJobName(), null));
boolean doPrint = false;
DialogTypeSelection dts = (DialogTypeSelection) attributes.get(DialogTypeSelection.class);
if (dts == DialogTypeSelection.NATIVE) {
// Remove DialogTypeSelection.NATIVE to prevent infinite loop in
// RasterPrinterJob.
attributes.remove(DialogTypeSelection.class);
doPrint = printDialog(attributes);
// restore attribute
attributes.add(DialogTypeSelection.NATIVE);
} else {
doPrint = printDialog(attributes);
}
if (doPrint) {
JobName jobName = (JobName) attributes.get(JobName.class);
if (jobName != null) {
setJobName(jobName.getValue());
}
Copies copies = (Copies) attributes.get(Copies.class);
if (copies != null) {
setCopies(copies.getValue());
}
Destination dest = (Destination) attributes.get(Destination.class);
if (dest != null) {
try {
mDestType = RasterPrinterJob.FILE;
mDestination = (new File(dest.getURI())).getPath();
} catch (Exception e) {
mDestination = "out.ps";
}
} else {
mDestType = RasterPrinterJob.PRINTER;
PrintService pServ = getPrintService();
if (pServ != null) {
mDestination = pServ.getName();
if (isMac) {
PrintServiceAttributeSet psaSet = pServ.getAttributes();
if (psaSet != null) {
mDestination = psaSet.get(PrinterName.class).toString();
}
}
}
}
}
return doPrint;
}
use of javax.print.attribute.standard.Destination in project jdk8u_jdk by JetBrains.
the class PrintJob2D method updateAttributes.
private void updateAttributes() {
Copies c = (Copies) attributes.get(Copies.class);
jobAttributes.setCopies(c.getValue());
SunPageSelection sel = (SunPageSelection) attributes.get(SunPageSelection.class);
if (sel == SunPageSelection.RANGE) {
jobAttributes.setDefaultSelection(DefaultSelectionType.RANGE);
} else if (sel == SunPageSelection.SELECTION) {
jobAttributes.setDefaultSelection(DefaultSelectionType.SELECTION);
} else {
jobAttributes.setDefaultSelection(DefaultSelectionType.ALL);
}
Destination dest = (Destination) attributes.get(Destination.class);
if (dest != null) {
jobAttributes.setDestination(DestinationType.FILE);
jobAttributes.setFileName(dest.getURI().getPath());
} else {
jobAttributes.setDestination(DestinationType.PRINTER);
}
PrintService serv = printerJob.getPrintService();
if (serv != null) {
jobAttributes.setPrinter(serv.getName());
}
PageRanges range = (PageRanges) attributes.get(PageRanges.class);
int[][] members = range.getMembers();
jobAttributes.setPageRanges(members);
SheetCollate collation = (SheetCollate) attributes.get(SheetCollate.class);
if (collation == SheetCollate.COLLATED) {
jobAttributes.setMultipleDocumentHandling(MultipleDocumentHandlingType.SEPARATE_DOCUMENTS_COLLATED_COPIES);
} else {
jobAttributes.setMultipleDocumentHandling(MultipleDocumentHandlingType.SEPARATE_DOCUMENTS_UNCOLLATED_COPIES);
}
Sides sides = (Sides) attributes.get(Sides.class);
if (sides == Sides.TWO_SIDED_LONG_EDGE) {
jobAttributes.setSides(SidesType.TWO_SIDED_LONG_EDGE);
} else if (sides == Sides.TWO_SIDED_SHORT_EDGE) {
jobAttributes.setSides(SidesType.TWO_SIDED_SHORT_EDGE);
} else {
jobAttributes.setSides(SidesType.ONE_SIDED);
}
// PageAttributes
Chromaticity color = (Chromaticity) attributes.get(Chromaticity.class);
if (color == Chromaticity.COLOR) {
pageAttributes.setColor(ColorType.COLOR);
} else {
pageAttributes.setColor(ColorType.MONOCHROME);
}
OrientationRequested orient = (OrientationRequested) attributes.get(OrientationRequested.class);
if (orient == OrientationRequested.LANDSCAPE) {
pageAttributes.setOrientationRequested(OrientationRequestedType.LANDSCAPE);
} else {
pageAttributes.setOrientationRequested(OrientationRequestedType.PORTRAIT);
}
PrintQuality qual = (PrintQuality) attributes.get(PrintQuality.class);
if (qual == PrintQuality.DRAFT) {
pageAttributes.setPrintQuality(PrintQualityType.DRAFT);
} else if (qual == PrintQuality.HIGH) {
pageAttributes.setPrintQuality(PrintQualityType.HIGH);
} else {
// NORMAL
pageAttributes.setPrintQuality(PrintQualityType.NORMAL);
}
Media msn = (Media) attributes.get(Media.class);
if (msn != null && msn instanceof MediaSizeName) {
MediaType mType = unMapMedia((MediaSizeName) msn);
if (mType != null) {
pageAttributes.setMedia(mType);
}
}
debugPrintAttributes(false, false);
}
use of javax.print.attribute.standard.Destination in project jdk8u_jdk by JetBrains.
the class Win32PrintJob method getAttributeValues.
private void getAttributeValues(DocFlavor flavor) throws PrintException {
if (reqAttrSet.get(Fidelity.class) == Fidelity.FIDELITY_TRUE) {
fidelity = true;
} else {
fidelity = false;
}
Class category;
Attribute[] attrs = reqAttrSet.toArray();
for (int i = 0; i < attrs.length; i++) {
Attribute attr = attrs[i];
category = attr.getCategory();
if (fidelity == true) {
if (!service.isAttributeCategorySupported(category)) {
notifyEvent(PrintJobEvent.JOB_FAILED);
throw new PrintJobAttributeException("unsupported category: " + category, category, null);
} else if (!service.isAttributeValueSupported(attr, flavor, null)) {
notifyEvent(PrintJobEvent.JOB_FAILED);
throw new PrintJobAttributeException("unsupported attribute: " + attr, null, attr);
}
}
if (category == Destination.class) {
URI uri = ((Destination) attr).getURI();
if (!"file".equals(uri.getScheme())) {
notifyEvent(PrintJobEvent.JOB_FAILED);
throw new PrintException("Not a file: URI");
} else {
try {
mDestination = (new File(uri)).getPath();
} catch (Exception e) {
throw new PrintException(e);
}
// check write access
SecurityManager security = System.getSecurityManager();
if (security != null) {
try {
security.checkWrite(mDestination);
} catch (SecurityException se) {
notifyEvent(PrintJobEvent.JOB_FAILED);
throw new PrintException(se);
}
}
}
} else if (category == JobName.class) {
jobName = ((JobName) attr).getValue();
} else if (category == Copies.class) {
copies = ((Copies) attr).getValue();
} else if (category == Media.class) {
if (attr instanceof MediaSizeName) {
mediaName = (MediaSizeName) attr;
// be used to create a new PageFormat.
if (!service.isAttributeValueSupported(attr, null, null)) {
mediaSize = MediaSize.getMediaSizeForName(mediaName);
}
}
} else if (category == OrientationRequested.class) {
orient = (OrientationRequested) attr;
}
}
}
use of javax.print.attribute.standard.Destination in project jdk8u_jdk by JetBrains.
the class Win32MediaSize method getDefaultAttributeValue.
public Object getDefaultAttributeValue(Class<? extends Attribute> category) {
if (category == null) {
throw new NullPointerException("null category");
}
if (!Attribute.class.isAssignableFrom(category)) {
throw new IllegalArgumentException(category + " is not an Attribute");
}
if (!isAttributeCategorySupported(category)) {
return null;
}
int[] defaults = getDefaultPrinterSettings();
// indices must match those in WPrinterJob.cpp
int defPaper = defaults[0];
int defYRes = defaults[2];
int defQuality = defaults[3];
int defCopies = defaults[4];
int defOrient = defaults[5];
int defSides = defaults[6];
int defCollate = defaults[7];
int defColor = defaults[8];
if (category == Copies.class) {
if (defCopies > 0) {
return new Copies(defCopies);
} else {
return new Copies(1);
}
} else if (category == Chromaticity.class) {
if (defColor == DMCOLOR_COLOR) {
return Chromaticity.COLOR;
} else {
return Chromaticity.MONOCHROME;
}
} else if (category == JobName.class) {
return new JobName("Java Printing", null);
} else if (category == OrientationRequested.class) {
if (defOrient == DMORIENT_LANDSCAPE) {
return OrientationRequested.LANDSCAPE;
} else {
return OrientationRequested.PORTRAIT;
}
} else if (category == PageRanges.class) {
return new PageRanges(1, Integer.MAX_VALUE);
} else if (category == Media.class) {
MediaSizeName msn = findWin32Media(defPaper);
if (msn != null) {
if (!isSupportedMedia(msn) && mediaSizeNames != null) {
msn = mediaSizeNames[0];
defPaper = findPaperID(msn);
}
return msn;
} else {
initMedia();
if ((mediaSizeNames != null) && (mediaSizeNames.length > 0)) {
// cannot be null but to be safe, add a check
if ((idList != null) && (mediaSizes != null) && (idList.size() == mediaSizes.length)) {
Integer defIdObj = Integer.valueOf(defPaper);
int index = idList.indexOf(defIdObj);
if (index >= 0 && index < mediaSizes.length) {
return mediaSizes[index].getMediaSizeName();
}
}
return mediaSizeNames[0];
}
}
} else if (category == MediaPrintableArea.class) {
/* Verify defPaper */
MediaSizeName msn = findWin32Media(defPaper);
if (msn != null && !isSupportedMedia(msn) && mediaSizeNames != null) {
defPaper = findPaperID(mediaSizeNames[0]);
}
float[] prnArea = getMediaPrintableArea(printer, defPaper);
if (prnArea != null) {
MediaPrintableArea printableArea = null;
try {
printableArea = new MediaPrintableArea(prnArea[0], prnArea[1], prnArea[2], prnArea[3], MediaPrintableArea.INCH);
} catch (IllegalArgumentException e) {
}
return printableArea;
}
return null;
} else if (category == SunAlternateMedia.class) {
return null;
} else if (category == Destination.class) {
try {
return new Destination((new File("out.prn")).toURI());
} catch (SecurityException se) {
try {
return new Destination(new URI("file:out.prn"));
} catch (URISyntaxException e) {
return null;
}
}
} else if (category == Sides.class) {
switch(defSides) {
case DMDUP_VERTICAL:
return Sides.TWO_SIDED_LONG_EDGE;
case DMDUP_HORIZONTAL:
return Sides.TWO_SIDED_SHORT_EDGE;
default:
return Sides.ONE_SIDED;
}
} else if (category == PrinterResolution.class) {
int yRes = defYRes;
int xRes = defQuality;
if ((xRes < 0) || (yRes < 0)) {
int res = (yRes > xRes) ? yRes : xRes;
if (res > 0) {
return new PrinterResolution(res, res, PrinterResolution.DPI);
}
} else {
return new PrinterResolution(xRes, yRes, PrinterResolution.DPI);
}
} else if (category == ColorSupported.class) {
int caps = getPrinterCapabilities();
if ((caps & DEVCAP_COLOR) != 0) {
return ColorSupported.SUPPORTED;
} else {
return ColorSupported.NOT_SUPPORTED;
}
} else if (category == PrintQuality.class) {
if ((defQuality < 0) && (defQuality >= DMRES_HIGH)) {
switch(defQuality) {
case DMRES_HIGH:
return PrintQuality.HIGH;
case DMRES_MEDIUM:
return PrintQuality.NORMAL;
default:
return PrintQuality.DRAFT;
}
}
} else if (category == RequestingUserName.class) {
String userName = "";
try {
userName = System.getProperty("user.name", "");
} catch (SecurityException se) {
}
return new RequestingUserName(userName, null);
} else if (category == SheetCollate.class) {
if (defCollate == DMCOLLATE_TRUE) {
return SheetCollate.COLLATED;
} else {
return SheetCollate.UNCOLLATED;
}
} else if (category == Fidelity.class) {
return Fidelity.FIDELITY_FALSE;
}
return null;
}
Aggregations