use of javax.print.attribute.standard.MediaSizeName in project jdk8u_jdk by JetBrains.
the class WPrinterJob method setAttributes.
/**
* copy the attributes to the native print job
* Note that this method, and hence the re-initialisation
* of the GDI values is done on each entry to the print dialog since
* an app could redisplay the print dialog for the same job and
* 1) the application may have changed attribute settings
* 2) the application may have changed the printer.
* In the event that the user changes the printer using the
dialog, then it is up to GDI to report back all changed values.
*/
@Override
protected void setAttributes(PrintRequestAttributeSet attributes) throws PrinterException {
// initialize attribute values
initAttributeMembers();
super.setAttributes(attributes);
mAttCopies = getCopiesInt();
mDestination = destinationAttr;
if (attributes == null) {
// now always use attributes, so this shouldn't happen.
return;
}
Attribute[] attrs = attributes.toArray();
for (int i = 0; i < attrs.length; i++) {
Attribute attr = attrs[i];
try {
if (attr.getCategory() == Sides.class) {
setSidesAttrib(attr);
} else if (attr.getCategory() == Chromaticity.class) {
setColorAttrib(attr);
} else if (attr.getCategory() == PrinterResolution.class) {
setResolutionAttrib(attr);
} else if (attr.getCategory() == PrintQuality.class) {
setQualityAttrib(attr);
} else if (attr.getCategory() == SheetCollate.class) {
setCollateAttrib(attr);
} else if (attr.getCategory() == Media.class || attr.getCategory() == SunAlternateMedia.class) {
/* SunAlternateMedia is used if its a tray, and
* any Media that is specified is not a tray.
*/
if (attr.getCategory() == SunAlternateMedia.class) {
Media media = (Media) attributes.get(Media.class);
if (media == null || !(media instanceof MediaTray)) {
attr = ((SunAlternateMedia) attr).getMedia();
}
}
if (attr instanceof MediaSizeName) {
setWin32MediaAttrib(attr);
}
if (attr instanceof MediaTray) {
setMediaTrayAttrib(attr);
}
}
} catch (ClassCastException e) {
}
}
}
use of javax.print.attribute.standard.MediaSizeName in project jdk8u_jdk by JetBrains.
the class PrintJob2D method mapMedia.
public static MediaSizeName mapMedia(MediaType mType) {
MediaSizeName media = null;
// JAVAXSIZES.length and SIZES.length must be equal!
// Attempt to recover by getting the smaller size.
int length = Math.min(SIZES.length, JAVAXSIZES.length);
for (int i = 0; i < length; i++) {
if (SIZES[i] == mType) {
if ((JAVAXSIZES[i] != null) && MediaSize.getMediaSizeForName(JAVAXSIZES[i]) != null) {
media = JAVAXSIZES[i];
break;
} else {
/* create Custom Media */
media = new CustomMediaSizeName(SIZES[i].toString());
float w = (float) Math.rint(WIDTHS[i] / 72.0);
float h = (float) Math.rint(LENGTHS[i] / 72.0);
if (w > 0.0 && h > 0.0) {
// add new created MediaSize to our static map
// so it will be found when we call findMedia
new MediaSize(w, h, Size2DSyntax.INCH, media);
}
break;
}
}
}
return media;
}
use of javax.print.attribute.standard.MediaSizeName in project jdk8u_jdk by JetBrains.
the class CustomMediaSizeName method findMedia.
// moved from RasterPrinterJob
/**
* Returns closest matching MediaSizeName among given array of Media
*/
public static MediaSizeName findMedia(Media[] media, float x, float y, int units) {
if (x <= 0.0f || y <= 0.0f || units < 1) {
throw new IllegalArgumentException("args must be +ve values");
}
if (media == null || media.length == 0) {
throw new IllegalArgumentException("args must have valid array of media");
}
int size = 0;
MediaSizeName[] msn = new MediaSizeName[media.length];
for (int i = 0; i < media.length; i++) {
if (media[i] instanceof MediaSizeName) {
msn[size++] = (MediaSizeName) media[i];
}
}
if (size == 0) {
return null;
}
int match = 0;
double ls = x * x + y * y;
double tmp_ls;
float[] dim;
float diffx = x;
float diffy = y;
for (int i = 0; i < size; i++) {
MediaSize mediaSize = MediaSize.getMediaSizeForName(msn[i]);
if (mediaSize == null) {
continue;
}
dim = mediaSize.getSize(units);
if (x == dim[0] && y == dim[1]) {
match = i;
break;
} else {
diffx = x - dim[0];
diffy = y - dim[1];
tmp_ls = diffx * diffx + diffy * diffy;
if (tmp_ls < ls) {
ls = tmp_ls;
match = i;
}
}
}
return msn[match];
}
use of javax.print.attribute.standard.MediaSizeName in project jdk8u_jdk by JetBrains.
the class UnixPrintJob method getAttributeValues.
private void getAttributeValues(DocFlavor flavor) throws PrintException {
Attribute attr;
Class category;
if (reqAttrSet.get(Fidelity.class) == Fidelity.FIDELITY_TRUE) {
fidelity = true;
} else {
fidelity = false;
}
Attribute[] attrs = reqAttrSet.toArray();
for (int i = 0; i < attrs.length; i++) {
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 {
mDestType = DESTFILE;
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 == JobSheets.class) {
if ((JobSheets) attr == JobSheets.NONE) {
mNoJobSheet = true;
}
} 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;
IPPPrintService.debug_println(debugPrefix + "mediaName " + mediaName);
if (!service.isAttributeValueSupported(attr, null, null)) {
mediaSize = MediaSize.getMediaSizeForName(mediaName);
}
} else if (attr instanceof CustomMediaTray) {
customTray = (CustomMediaTray) attr;
}
} else if (category == OrientationRequested.class) {
orient = (OrientationRequested) attr;
} else if (category == NumberUp.class) {
nUp = (NumberUp) attr;
} else if (category == Sides.class) {
sides = (Sides) attr;
}
}
}
use of javax.print.attribute.standard.MediaSizeName in project jdk8u_jdk by JetBrains.
the class UnixPrintService method getSupportedAttributeValues.
public Object getSupportedAttributeValues(Class<? extends Attribute> category, DocFlavor flavor, AttributeSet attributes) {
if (category == null) {
throw new NullPointerException("null category");
}
if (!Attribute.class.isAssignableFrom(category)) {
throw new IllegalArgumentException(category + " does not implement Attribute");
}
if (flavor != null) {
if (!isDocFlavorSupported(flavor)) {
throw new IllegalArgumentException(flavor + " is an unsupported flavor");
} else if (isAutoSense(flavor)) {
return null;
}
}
if (!isAttributeCategorySupported(category)) {
return null;
}
if (category == Chromaticity.class) {
if (flavor == null || isServiceFormattedFlavor(flavor)) {
Chromaticity[] arr = new Chromaticity[1];
arr[0] = Chromaticity.COLOR;
return (arr);
} else {
return null;
}
} else if (category == Destination.class) {
try {
return new Destination((new File("out.ps")).toURI());
} catch (SecurityException se) {
try {
return new Destination(new URI("file:out.ps"));
} catch (URISyntaxException e) {
return null;
}
}
} else if (category == JobName.class) {
return new JobName("Java Printing", null);
} else if (category == JobSheets.class) {
JobSheets[] arr = new JobSheets[2];
arr[0] = JobSheets.NONE;
arr[1] = JobSheets.STANDARD;
return arr;
} else if (category == RequestingUserName.class) {
String userName = "";
try {
userName = System.getProperty("user.name", "");
} catch (SecurityException se) {
}
return new RequestingUserName(userName, null);
} else if (category == OrientationRequested.class) {
if (flavor == null || isServiceFormattedFlavor(flavor)) {
OrientationRequested[] arr = new OrientationRequested[3];
arr[0] = OrientationRequested.PORTRAIT;
arr[1] = OrientationRequested.LANDSCAPE;
arr[2] = OrientationRequested.REVERSE_LANDSCAPE;
return arr;
} else {
return null;
}
} else if ((category == Copies.class) || (category == CopiesSupported.class)) {
if (flavor == null || !(flavor.equals(DocFlavor.INPUT_STREAM.POSTSCRIPT) || flavor.equals(DocFlavor.URL.POSTSCRIPT) || flavor.equals(DocFlavor.BYTE_ARRAY.POSTSCRIPT))) {
return new CopiesSupported(1, MAXCOPIES);
} else {
return null;
}
} else if (category == Media.class) {
Media[] arr = new Media[mediaSizes.length];
System.arraycopy(mediaSizes, 0, arr, 0, mediaSizes.length);
return arr;
} else if (category == Fidelity.class) {
Fidelity[] arr = new Fidelity[2];
arr[0] = Fidelity.FIDELITY_FALSE;
arr[1] = Fidelity.FIDELITY_TRUE;
return arr;
} else if (category == MediaPrintableArea.class) {
/* The code below implements the behaviour that if no Media or
* MediaSize attribute is specified, return an array of
* MediaPrintableArea, one for each supported Media.
* If a MediaSize is specified, return a MPA consistent for that,
* and if a Media is specified locate its MediaSize and return
* its MPA, and if none is found, return an MPA for the default
* Media for this service.
*/
if (attributes == null) {
return getAllPrintableAreas();
}
MediaSize mediaSize = (MediaSize) attributes.get(MediaSize.class);
Media media = (Media) attributes.get(Media.class);
MediaPrintableArea[] arr = new MediaPrintableArea[1];
if (mediaSize == null) {
if (media instanceof MediaSizeName) {
MediaSizeName msn = (MediaSizeName) media;
mediaSize = MediaSize.getMediaSizeForName(msn);
if (mediaSize == null) {
/* try to get a size from the default media */
media = (Media) getDefaultAttributeValue(Media.class);
if (media instanceof MediaSizeName) {
msn = (MediaSizeName) media;
mediaSize = MediaSize.getMediaSizeForName(msn);
}
if (mediaSize == null) {
/* shouldn't happen, return a default */
arr[0] = new MediaPrintableArea(0.25f, 0.25f, 8f, 10.5f, MediaSize.INCH);
return arr;
}
}
} else {
return getAllPrintableAreas();
}
}
/* If reach here MediaSize is non-null */
assert mediaSize != null;
arr[0] = new MediaPrintableArea(0.25f, 0.25f, mediaSize.getX(MediaSize.INCH) - 0.5f, mediaSize.getY(MediaSize.INCH) - 0.5f, MediaSize.INCH);
return arr;
} else if (category == PageRanges.class) {
if (flavor == null || flavor.equals(DocFlavor.SERVICE_FORMATTED.PAGEABLE) || flavor.equals(DocFlavor.SERVICE_FORMATTED.PRINTABLE)) {
PageRanges[] arr = new PageRanges[1];
arr[0] = new PageRanges(1, Integer.MAX_VALUE);
return arr;
} else {
return null;
}
} else if (category == SheetCollate.class) {
if (flavor == null || flavor.equals(DocFlavor.SERVICE_FORMATTED.PAGEABLE) || flavor.equals(DocFlavor.SERVICE_FORMATTED.PRINTABLE)) {
SheetCollate[] arr = new SheetCollate[2];
arr[0] = SheetCollate.UNCOLLATED;
arr[1] = SheetCollate.COLLATED;
return arr;
} else {
return null;
}
} else if (category == Sides.class) {
if (flavor == null || flavor.equals(DocFlavor.SERVICE_FORMATTED.PAGEABLE) || flavor.equals(DocFlavor.SERVICE_FORMATTED.PRINTABLE)) {
Sides[] arr = new Sides[3];
arr[0] = Sides.ONE_SIDED;
arr[1] = Sides.TWO_SIDED_LONG_EDGE;
arr[2] = Sides.TWO_SIDED_SHORT_EDGE;
return arr;
} else {
return null;
}
} else {
return null;
}
}
Aggregations