use of javax.print.attribute.standard.JobName in project jdk8u_jdk by JetBrains.
the class RasterPrinterJob method printDialog.
/**
* 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();
}
PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
attributes.add(new Copies(getCopies()));
attributes.add(new JobName(getJobName(), null));
boolean 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.prn";
PrintService ps = getPrintService();
if (ps != null) {
Destination defaultDest = (Destination) ps.getDefaultAttributeValue(Destination.class);
if (defaultDest != null) {
mDestination = (new File(defaultDest.getURI())).getPath();
}
}
}
} else {
mDestType = RasterPrinterJob.PRINTER;
PrintService ps = getPrintService();
if (ps != null) {
mDestination = ps.getName();
}
}
}
return doPrint;
}
use of javax.print.attribute.standard.JobName in project jdk8u_jdk by JetBrains.
the class RasterPrinterJob method setAttributes.
/* subclasses may need to pull extra information out of the attribute set
* They can override this method & call super.setAttributes()
*/
protected void setAttributes(PrintRequestAttributeSet attributes) throws PrinterException {
/* reset all values to defaults */
setCollated(false);
sidesAttr = null;
pageRangesAttr = null;
copiesAttr = 0;
jobNameAttr = null;
userNameAttr = null;
destinationAttr = null;
collateAttReq = false;
PrintService service = getPrintService();
if (attributes == null || service == null) {
return;
}
boolean fidelity = false;
Fidelity attrFidelity = (Fidelity) attributes.get(Fidelity.class);
if (attrFidelity != null && attrFidelity == Fidelity.FIDELITY_TRUE) {
fidelity = true;
}
if (fidelity == true) {
AttributeSet unsupported = service.getUnsupportedAttributes(DocFlavor.SERVICE_FORMATTED.PAGEABLE, attributes);
if (unsupported != null) {
throw new PrinterException("Fidelity cannot be satisfied");
}
}
/*
* Since we have verified supported values if fidelity is true,
* we can either ignore unsupported values, or substitute a
* reasonable alternative
*/
SheetCollate collateAttr = (SheetCollate) attributes.get(SheetCollate.class);
if (isSupportedValue(collateAttr, attributes)) {
setCollated(collateAttr == SheetCollate.COLLATED);
}
sidesAttr = (Sides) attributes.get(Sides.class);
if (!isSupportedValue(sidesAttr, attributes)) {
sidesAttr = Sides.ONE_SIDED;
}
pageRangesAttr = (PageRanges) attributes.get(PageRanges.class);
if (!isSupportedValue(pageRangesAttr, attributes)) {
pageRangesAttr = null;
} else {
if ((SunPageSelection) attributes.get(SunPageSelection.class) == SunPageSelection.RANGE) {
// get to, from, min, max page ranges
int[][] range = pageRangesAttr.getMembers();
// setPageRanges uses 0-based indexing so we subtract 1
setPageRange(range[0][0] - 1, range[0][1] - 1);
} else {
setPageRange(-1, -1);
}
}
Copies copies = (Copies) attributes.get(Copies.class);
if (isSupportedValue(copies, attributes) || (!fidelity && copies != null)) {
copiesAttr = copies.getValue();
setCopies(copiesAttr);
} else {
copiesAttr = getCopies();
}
Destination destination = (Destination) attributes.get(Destination.class);
if (isSupportedValue(destination, attributes)) {
try {
// Old code (new File(destination.getURI())).getPath()
// would generate a "URI is not hierarchical" IAE
// for "file:out.prn" so we use getSchemeSpecificPart instead
destinationAttr = "" + new File(destination.getURI().getSchemeSpecificPart());
} catch (Exception e) {
// paranoid exception
Destination defaultDest = (Destination) service.getDefaultAttributeValue(Destination.class);
if (defaultDest != null) {
destinationAttr = "" + new File(defaultDest.getURI().getSchemeSpecificPart());
}
}
}
JobSheets jobSheets = (JobSheets) attributes.get(JobSheets.class);
if (jobSheets != null) {
noJobSheet = jobSheets == JobSheets.NONE;
}
JobName jobName = (JobName) attributes.get(JobName.class);
if (isSupportedValue(jobName, attributes) || (!fidelity && jobName != null)) {
jobNameAttr = jobName.getValue();
setJobName(jobNameAttr);
} else {
jobNameAttr = getJobName();
}
RequestingUserName userName = (RequestingUserName) attributes.get(RequestingUserName.class);
if (isSupportedValue(userName, attributes) || (!fidelity && userName != null)) {
userNameAttr = userName.getValue();
} else {
try {
userNameAttr = getUserName();
} catch (SecurityException e) {
userNameAttr = "";
}
}
/* OpenBook is used internally only when app uses Printable.
* This is the case when we use the values from the attribute set.
*/
Media media = (Media) attributes.get(Media.class);
OrientationRequested orientReq = (OrientationRequested) attributes.get(OrientationRequested.class);
MediaPrintableArea mpa = (MediaPrintableArea) attributes.get(MediaPrintableArea.class);
if ((orientReq != null || media != null || mpa != null) && getPageable() instanceof OpenBook) {
/* We could almost(!) use PrinterJob.getPageFormat() except
* here we need to start with the PageFormat from the OpenBook :
*/
Pageable pageable = getPageable();
Printable printable = pageable.getPrintable(0);
PageFormat pf = (PageFormat) pageable.getPageFormat(0).clone();
Paper paper = pf.getPaper();
/* If there's a media but no media printable area, we can try
* to retrieve the default value for mpa and use that.
*/
if (mpa == null && media != null && service.isAttributeCategorySupported(MediaPrintableArea.class)) {
Object mpaVals = service.getSupportedAttributeValues(MediaPrintableArea.class, null, attributes);
if (mpaVals instanceof MediaPrintableArea[] && ((MediaPrintableArea[]) mpaVals).length > 0) {
mpa = ((MediaPrintableArea[]) mpaVals)[0];
}
}
if (isSupportedValue(orientReq, attributes) || (!fidelity && orientReq != null)) {
int orient;
if (orientReq.equals(OrientationRequested.REVERSE_LANDSCAPE)) {
orient = PageFormat.REVERSE_LANDSCAPE;
} else if (orientReq.equals(OrientationRequested.LANDSCAPE)) {
orient = PageFormat.LANDSCAPE;
} else {
orient = PageFormat.PORTRAIT;
}
pf.setOrientation(orient);
}
if (isSupportedValue(media, attributes) || (!fidelity && media != null)) {
if (media instanceof MediaSizeName) {
MediaSizeName msn = (MediaSizeName) media;
MediaSize msz = MediaSize.getMediaSizeForName(msn);
if (msz != null) {
float paperWid = msz.getX(MediaSize.INCH) * 72.0f;
float paperHgt = msz.getY(MediaSize.INCH) * 72.0f;
paper.setSize(paperWid, paperHgt);
if (mpa == null) {
paper.setImageableArea(72.0, 72.0, paperWid - 144.0, paperHgt - 144.0);
}
}
}
}
if (isSupportedValue(mpa, attributes) || (!fidelity && mpa != null)) {
float[] printableArea = mpa.getPrintableArea(MediaPrintableArea.INCH);
for (int i = 0; i < printableArea.length; i++) {
printableArea[i] = printableArea[i] * 72.0f;
}
paper.setImageableArea(printableArea[0], printableArea[1], printableArea[2], printableArea[3]);
}
pf.setPaper(paper);
pf = validatePage(pf);
setPrintable(printable, pf);
} else {
// for AWT where pageable is not an instance of OpenBook,
// we need to save paper info
this.attributes = attributes;
}
}
use of javax.print.attribute.standard.JobName in project jdk8u_jdk by JetBrains.
the class PrintJob2D method copyAttributes.
/* From JobAttributes we will copy job name and duplex printing
* and destination.
* The majority of the rest of the attributes are reflected
* attributes.
*
* From PageAttributes we copy color, media size, orientation,
* origin type, resolution and print quality.
* We use the media, orientation in creating the page format, and
* the origin type to set its imageable area.
*
* REMIND: Interpretation of resolution, additional media sizes.
*/
private void copyAttributes(PrintService printServ) {
attributes = new HashPrintRequestAttributeSet();
attributes.add(new JobName(docTitle, null));
PrintService pServ = printServ;
String printerName = jobAttributes.getPrinter();
if (printerName != null && printerName != "" && !printerName.equals(pServ.getName())) {
// Search for the given printerName in the list of PrintServices
PrintService[] services = PrinterJob.lookupPrintServices();
try {
for (int i = 0; i < services.length; i++) {
if (printerName.equals(services[i].getName())) {
printerJob.setPrintService(services[i]);
pServ = services[i];
break;
}
}
} catch (PrinterException pe) {
}
}
DestinationType dest = jobAttributes.getDestination();
if (dest == DestinationType.FILE && pServ.isAttributeCategorySupported(Destination.class)) {
String fileName = jobAttributes.getFileName();
Destination defaultDest;
if (fileName == null && (defaultDest = (Destination) pServ.getDefaultAttributeValue(Destination.class)) != null) {
attributes.add(defaultDest);
} else {
URI uri = null;
try {
if (fileName != null) {
if (fileName.equals("")) {
fileName = ".";
}
} else {
// defaultDest should not be null. The following code
// is only added to safeguard against a possible
// buggy implementation of a PrintService having a
// null default Destination.
fileName = "out.prn";
}
uri = (new File(fileName)).toURI();
} catch (SecurityException se) {
try {
// '\\' file separator is illegal character in opaque
// part and causes URISyntaxException, so we replace
// it with '/'
fileName = fileName.replace('\\', '/');
uri = new URI("file:" + fileName);
} catch (URISyntaxException e) {
}
}
if (uri != null) {
attributes.add(new Destination(uri));
}
}
}
attributes.add(new SunMinMaxPage(jobAttributes.getMinPage(), jobAttributes.getMaxPage()));
SidesType sType = jobAttributes.getSides();
if (sType == SidesType.TWO_SIDED_LONG_EDGE) {
attributes.add(Sides.TWO_SIDED_LONG_EDGE);
} else if (sType == SidesType.TWO_SIDED_SHORT_EDGE) {
attributes.add(Sides.TWO_SIDED_SHORT_EDGE);
} else if (sType == SidesType.ONE_SIDED) {
attributes.add(Sides.ONE_SIDED);
}
MultipleDocumentHandlingType hType = jobAttributes.getMultipleDocumentHandling();
if (hType == MultipleDocumentHandlingType.SEPARATE_DOCUMENTS_COLLATED_COPIES) {
attributes.add(SheetCollate.COLLATED);
} else {
attributes.add(SheetCollate.UNCOLLATED);
}
attributes.add(new Copies(jobAttributes.getCopies()));
attributes.add(new PageRanges(jobAttributes.getFromPage(), jobAttributes.getToPage()));
if (pageAttributes.getColor() == ColorType.COLOR) {
attributes.add(Chromaticity.COLOR);
} else {
attributes.add(Chromaticity.MONOCHROME);
}
pageFormat = printerJob.defaultPage();
if (pageAttributes.getOrientationRequested() == OrientationRequestedType.LANDSCAPE) {
pageFormat.setOrientation(PageFormat.LANDSCAPE);
attributes.add(OrientationRequested.LANDSCAPE);
} else {
pageFormat.setOrientation(PageFormat.PORTRAIT);
attributes.add(OrientationRequested.PORTRAIT);
}
MediaType media = pageAttributes.getMedia();
MediaSizeName msn = mapMedia(media);
if (msn != null) {
attributes.add(msn);
}
PrintQualityType qType = pageAttributes.getPrintQuality();
if (qType == PrintQualityType.DRAFT) {
attributes.add(PrintQuality.DRAFT);
} else if (qType == PrintQualityType.NORMAL) {
attributes.add(PrintQuality.NORMAL);
} else if (qType == PrintQualityType.HIGH) {
attributes.add(PrintQuality.HIGH);
}
}
use of javax.print.attribute.standard.JobName in project jdk8u_jdk by JetBrains.
the class PrintJob2D method printDialog.
public boolean printDialog() {
boolean proceedWithPrint = false;
printerJob = PrinterJob.getPrinterJob();
if (printerJob == null) {
return false;
}
DialogType d = this.jobAttributes.getDialog();
PrintService pServ = printerJob.getPrintService();
if ((pServ == null) && (d == DialogType.NONE)) {
return false;
}
copyAttributes(pServ);
DefaultSelectionType select = this.jobAttributes.getDefaultSelection();
if (select == DefaultSelectionType.RANGE) {
attributes.add(SunPageSelection.RANGE);
} else if (select == DefaultSelectionType.SELECTION) {
attributes.add(SunPageSelection.SELECTION);
} else {
attributes.add(SunPageSelection.ALL);
}
if (frame != null) {
attributes.add(new DialogOwner(frame));
}
if (d == DialogType.NONE) {
proceedWithPrint = true;
} else {
if (d == DialogType.NATIVE) {
attributes.add(DialogTypeSelection.NATIVE);
} else {
// (d == DialogType.COMMON)
attributes.add(DialogTypeSelection.COMMON);
}
if (proceedWithPrint = printerJob.printDialog(attributes)) {
if (pServ == null) {
// Windows gives an option to install a service
// when it detects there are no printers so
// we make sure we get the updated print service.
pServ = printerJob.getPrintService();
if (pServ == null) {
return false;
}
}
updateAttributes();
translateOutputProps();
}
}
if (proceedWithPrint) {
JobName jname = (JobName) attributes.get(JobName.class);
if (jname != null) {
printerJob.setJobName(jname.toString());
}
pageFormat = new PageFormat();
Media media = (Media) attributes.get(Media.class);
MediaSize mediaSize = null;
if (media != null && media instanceof MediaSizeName) {
mediaSize = MediaSize.getMediaSizeForName((MediaSizeName) media);
}
Paper p = pageFormat.getPaper();
if (mediaSize != null) {
p.setSize(mediaSize.getX(MediaSize.INCH) * 72.0, mediaSize.getY(MediaSize.INCH) * 72.0);
}
if (pageAttributes.getOrigin() == OriginType.PRINTABLE) {
// AWT uses 1/4" borders by default
p.setImageableArea(18.0, 18.0, p.getWidth() - 36.0, p.getHeight() - 36.0);
} else {
p.setImageableArea(0.0, 0.0, p.getWidth(), p.getHeight());
}
pageFormat.setPaper(p);
OrientationRequested orient = (OrientationRequested) attributes.get(OrientationRequested.class);
if (orient != null && orient == OrientationRequested.REVERSE_LANDSCAPE) {
pageFormat.setOrientation(PageFormat.REVERSE_LANDSCAPE);
} else if (orient == OrientationRequested.LANDSCAPE) {
pageFormat.setOrientation(PageFormat.LANDSCAPE);
} else {
pageFormat.setOrientation(PageFormat.PORTRAIT);
}
printerJob.setPrintable(this, pageFormat);
}
return proceedWithPrint;
}
use of javax.print.attribute.standard.JobName in project jdk8u_jdk by JetBrains.
the class PSStreamPrintJob method initializeAttributeSets.
/* There's some inefficiency here as the job set is created even though
* it may never be requested.
*/
private synchronized void initializeAttributeSets(Doc doc, PrintRequestAttributeSet reqSet) {
reqAttrSet = new HashPrintRequestAttributeSet();
jobAttrSet = new HashPrintJobAttributeSet();
Attribute[] attrs;
if (reqSet != null) {
reqAttrSet.addAll(reqSet);
attrs = reqSet.toArray();
for (int i = 0; i < attrs.length; i++) {
if (attrs[i] instanceof PrintJobAttribute) {
jobAttrSet.add(attrs[i]);
}
}
}
DocAttributeSet docSet = doc.getAttributes();
if (docSet != null) {
attrs = docSet.toArray();
for (int i = 0; i < attrs.length; i++) {
if (attrs[i] instanceof PrintRequestAttribute) {
reqAttrSet.add(attrs[i]);
}
if (attrs[i] instanceof PrintJobAttribute) {
jobAttrSet.add(attrs[i]);
}
}
}
/* add the user name to the job */
String userName = "";
try {
userName = System.getProperty("user.name");
} catch (SecurityException se) {
}
if (userName == null || userName.equals("")) {
RequestingUserName ruName = (RequestingUserName) reqSet.get(RequestingUserName.class);
if (ruName != null) {
jobAttrSet.add(new JobOriginatingUserName(ruName.getValue(), ruName.getLocale()));
} else {
jobAttrSet.add(new JobOriginatingUserName("", null));
}
} else {
jobAttrSet.add(new JobOriginatingUserName(userName, null));
}
/* if no job name supplied use doc name (if supplied), if none and
* its a URL use that, else finally anything .. */
if (jobAttrSet.get(JobName.class) == null) {
JobName jobName;
if (docSet != null && docSet.get(DocumentName.class) != null) {
DocumentName docName = (DocumentName) docSet.get(DocumentName.class);
jobName = new JobName(docName.getValue(), docName.getLocale());
jobAttrSet.add(jobName);
} else {
String str = "JPS Job:" + doc;
try {
Object printData = doc.getPrintData();
if (printData instanceof URL) {
str = ((URL) (doc.getPrintData())).toString();
}
} catch (IOException e) {
}
jobName = new JobName(str, null);
jobAttrSet.add(jobName);
}
}
jobAttrSet = AttributeSetUtilities.unmodifiableView(jobAttrSet);
}
Aggregations