public void printableJob(Printable printable) throws PrintException { try { synchronized (this) { if (job != null) { // shouldn't happen throw new PrintException("already printing"); } else { job = new sun.awt.windows.WPrinterJob(); } } PrintService svc = getPrintService(); job.setPrintService(svc); if (copies == 0) { Copies c = (Copies) svc.getDefaultAttributeValue(Copies.class); copies = c.getValue(); } if (mediaName == null) { Object media = svc.getDefaultAttributeValue(Media.class); if (media instanceof MediaSizeName) { mediaName = (MediaSizeName) media; mediaSize = MediaSize.getMediaSizeForName(mediaName); } } if (orient == null) { orient = (OrientationRequested) svc.getDefaultAttributeValue(OrientationRequested.class); } job.setCopies(copies); job.setJobName(jobName); PageFormat pf = new PageFormat(); if (mediaSize != null) { Paper p = new Paper(); p.setSize(mediaSize.getX(MediaSize.INCH) * 72.0, mediaSize.getY(MediaSize.INCH) * 72.0); p.setImageableArea(72.0, 72.0, p.getWidth() - 144.0, p.getHeight() - 144.0); pf.setPaper(p); } if (orient == OrientationRequested.REVERSE_LANDSCAPE) { pf.setOrientation(PageFormat.REVERSE_LANDSCAPE); } else if (orient == OrientationRequested.LANDSCAPE) { pf.setOrientation(PageFormat.LANDSCAPE); } job.setPrintable(printable, pf); job.print(reqAttrSet); notifyEvent(PrintJobEvent.DATA_TRANSFER_COMPLETE); return; } catch (PrinterException pe) { notifyEvent(PrintJobEvent.JOB_FAILED); throw new PrintException(pe); } finally { printReturned = true; notifyEvent(PrintJobEvent.NO_MORE_EVENTS); } }
/** 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]; }
private final synchronized Paper addPaper(MediaSizeName media) { if (mediaToPaperMap == null) { mediaToPaperMap = new HashMap<MediaSizeName, Paper>(); } Paper paper = predefinedPaperMap.get(media); if (paper == null) { MediaSize sz = MediaSize.getMediaSizeForName(media); if (sz != null) { int mm = (int) ((25400 / 72.0) + 0.5); paper = PrintHelper.createPaper(media.toString(), sz.getX(mm), sz.getY(mm), Units.MM); } } if (paper == null) { paper = Paper.NA_LETTER; } mediaToPaperMap.put(media, paper); return paper; }
// {{{ getPageFormat() method public static PageFormat getPageFormat() { // convert from PrintRequestAttributeSet to the pageFormat PrinterJob prnJob = getPrintJob(" "); // $NON-NLS-1$ PageFormat pf = prnJob.defaultPage(); Paper pap = pf.getPaper(); MediaSizeName media = (MediaSizeName) format.get(Media.class); MediaSize ms = MediaSize.getMediaSizeForName(media); MediaPrintableArea mediaarea = (MediaPrintableArea) format.get(MediaPrintableArea.class); if (mediaarea != null) { pap.setImageableArea( (mediaarea.getX(MediaPrintableArea.INCH) * 72), (mediaarea.getY(MediaPrintableArea.INCH) * 72), (mediaarea.getWidth(MediaPrintableArea.INCH) * 72), (mediaarea.getHeight(MediaPrintableArea.INCH) * 72)); } if (ms != null) { pap.setSize((ms.getX(Size2DSyntax.INCH) * 72), (ms.getY(Size2DSyntax.INCH) * 72)); } pf.setPaper(pap); OrientationRequested orientation = (OrientationRequested) format.get(OrientationRequested.class); if (orientation != null) { if (orientation.getValue() == OrientationRequested.LANDSCAPE.getValue()) { pf.setOrientation(PageFormat.LANDSCAPE); } else if (orientation.getValue() == OrientationRequested.REVERSE_LANDSCAPE.getValue()) { pf.setOrientation(PageFormat.REVERSE_LANDSCAPE); } else if (orientation.getValue() == OrientationRequested.PORTRAIT.getValue()) { pf.setOrientation(PageFormat.PORTRAIT); } else if (orientation.getValue() == OrientationRequested.REVERSE_PORTRAIT.getValue()) { // doesnt exist?? // pf.setOrientation(PageFormat.REVERSE_PORTRAIT); // then just do the next best thing pf.setOrientation(PageFormat.PORTRAIT); } } return pf; } // }}}
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; // If requested MediaSizeName is not supported, // get the corresponding media size - this will // 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; } } }