diff options
Diffstat (limited to 'openjdk/sun/print')
-rw-r--r-- | openjdk/sun/print/PrintPeer.java | 42 | ||||
-rw-r--r-- | openjdk/sun/print/UnixPrintServiceLookup.java | 33 | ||||
-rw-r--r-- | openjdk/sun/print/Win32PrintJob.java | 1119 | ||||
-rw-r--r-- | openjdk/sun/print/Win32PrintService.java | 920 | ||||
-rw-r--r-- | openjdk/sun/print/Win32PrintServiceLookup.java | 264 |
5 files changed, 0 insertions, 2378 deletions
diff --git a/openjdk/sun/print/PrintPeer.java b/openjdk/sun/print/PrintPeer.java deleted file mode 100644 index 892d591d..00000000 --- a/openjdk/sun/print/PrintPeer.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - Copyright (C) 2009 Volker Berlin (i-net software) - - This software is provided 'as-is', without any express or implied - warranty. In no event will the authors be held liable for any damages - arising from the use of this software. - - Permission is granted to anyone to use this software for any purpose, - including commercial applications, and to alter it and redistribute it - freely, subject to the following restrictions: - - 1. The origin of this software must not be misrepresented; you must not - claim that you wrote the original software. If you use this software - in a product, an acknowledgment in the product documentation would be - appreciated but is not required. - 2. Altered source versions must be plainly marked as such, and must not be - misrepresented as being the original software. - 3. This notice may not be removed or altered from any source distribution. - - Jeroen Frijters - jeroen@frijters.net - - */ -package sun.print; - -import java.awt.Graphics2D; - - - -/** - * A helper interface for implementing the print API for IKVM - */ -public interface PrintPeer{ - - public Object getPrinterStatus(String PrinterName, Class category); - - public String getDefaultPrinterName(); - - public String[] getAllPrinterNames(); - - public Graphics2D createGraphics(cli.System.Drawing.Graphics g); -} diff --git a/openjdk/sun/print/UnixPrintServiceLookup.java b/openjdk/sun/print/UnixPrintServiceLookup.java deleted file mode 100644 index 8c18f4e6..00000000 --- a/openjdk/sun/print/UnixPrintServiceLookup.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - Copyright (C) 2009 Volker Berlin (i-net software) - - This software is provided 'as-is', without any express or implied - warranty. In no event will the authors be held liable for any damages - arising from the use of this software. - - Permission is granted to anyone to use this software for any purpose, - including commercial applications, and to alter it and redistribute it - freely, subject to the following restrictions: - - 1. The origin of this software must not be misrepresented; you must not - claim that you wrote the original software. If you use this software - in a product, an acknowledgment in the product documentation would be - appreciated but is not required. - 2. Altered source versions must be plainly marked as such, and must not be - misrepresented as being the original software. - 3. This notice may not be removed or altered from any source distribution. - - Jeroen Frijters - jeroen@frijters.net - -*/ -package sun.print; - - - -/** - * Currently this class is called via Reflection. - */ -public class UnixPrintServiceLookup extends Win32PrintServiceLookup{ - -} diff --git a/openjdk/sun/print/Win32PrintJob.java b/openjdk/sun/print/Win32PrintJob.java deleted file mode 100644 index 654f742b..00000000 --- a/openjdk/sun/print/Win32PrintJob.java +++ /dev/null @@ -1,1119 +0,0 @@ -/* - Copyright (C) 2009, 2012 Volker Berlin (i-net software) - Copyright (C) 2010, 2011 Karsten Heinrich (i-net software) - - This software is provided 'as-is', without any express or implied - warranty. In no event will the authors be held liable for any damages - arising from the use of this software. - - Permission is granted to anyone to use this software for any purpose, - including commercial applications, and to alter it and redistribute it - freely, subject to the following restrictions: - - 1. The origin of this software must not be misrepresented; you must not - claim that you wrote the original software. If you use this software - in a product, an acknowledgment in the product documentation would be - appreciated but is not required. - 2. Altered source versions must be plainly marked as such, and must not be - misrepresented as being the original software. - 3. This notice may not be removed or altered from any source distribution. - - Jeroen Frijters - jeroen@frijters.net - - */ -package sun.print; - -import java.awt.Color; -import java.awt.Graphics; -import java.awt.Graphics2D; -import java.awt.geom.AffineTransform; -import java.awt.image.BufferedImage; -import java.awt.print.PageFormat; -import java.awt.print.Pageable; -import java.awt.print.Paper; -import java.awt.print.Printable; -import java.awt.print.PrinterException; -import java.awt.print.PrinterJob; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.net.URI; -import java.net.URL; -import java.util.ArrayList; -import java.util.List; -import java.util.TreeSet; - -import javax.print.CancelablePrintJob; -import javax.print.Doc; -import javax.print.DocFlavor; -import javax.print.PrintException; -import javax.print.PrintService; -import javax.print.attribute.Attribute; -import javax.print.attribute.AttributeSetUtilities; -import javax.print.attribute.DocAttributeSet; -import javax.print.attribute.HashPrintJobAttributeSet; -import javax.print.attribute.HashPrintRequestAttributeSet; -import javax.print.attribute.PrintJobAttribute; -import javax.print.attribute.PrintJobAttributeSet; -import javax.print.attribute.PrintRequestAttribute; -import javax.print.attribute.PrintRequestAttributeSet; -import javax.print.attribute.standard.Chromaticity; -import javax.print.attribute.standard.Copies; -import javax.print.attribute.standard.Destination; -import javax.print.attribute.standard.DocumentName; -import javax.print.attribute.standard.Fidelity; -import javax.print.attribute.standard.JobName; -import javax.print.attribute.standard.JobOriginatingUserName; -import javax.print.attribute.standard.Media; -import javax.print.attribute.standard.MediaPrintableArea; -import javax.print.attribute.standard.MediaSize; -import javax.print.attribute.standard.MediaSizeName; -import javax.print.attribute.standard.MediaTray; -import javax.print.attribute.standard.OrientationRequested; -import javax.print.attribute.standard.PageRanges; -import javax.print.attribute.standard.PrinterIsAcceptingJobs; -import javax.print.attribute.standard.PrinterState; -import javax.print.attribute.standard.PrinterStateReason; -import javax.print.attribute.standard.PrinterStateReasons; -import javax.print.attribute.standard.RequestingUserName; -import javax.print.attribute.standard.SheetCollate; -import javax.print.event.PrintJobAttributeListener; -import javax.print.event.PrintJobEvent; -import javax.print.event.PrintJobListener; - -import sun.awt.windows.WPrinterJob; -import sun.print.Win32PrintService.NetMediaTray; - -import cli.System.Drawing.Printing.*; - -/** - * @author Volker Berlin - */ -public class Win32PrintJob implements CancelablePrintJob{ - - private ArrayList<PrintJobListener> jobListeners; - - private ArrayList<PrintJobAttributeListener> attrListeners; - - private ArrayList<PrintJobAttributeSet> listenedAttributeSets; - - private final Win32PrintService service; - - - private boolean printing; - private boolean printReturned; - - private PrintRequestAttributeSet reqAttrSet; - - private PrintJobAttributeSet jobAttrSet; - - private PrinterJob job = new WPrinterJob(); - private Doc doc; - private String mDestination; - - /* these variables used globally to store reference to the print - * data retrieved as a stream. On completion these are always closed - * if non-null. - */ - private InputStream instream; - - /* default values overridden by those extracted from the attributes */ - private boolean fidelity; - private boolean printColor; - private String jobName = "Java Printing"; - private int copies; - private MediaSizeName mediaName; - private MediaSize mediaSize; - private OrientationRequested orient; - - private final PrintPeer peer; - private PrinterException printerException; - - private PageNumberConverter pageRanges; - - private MediaTray mediaTray; - - - Win32PrintJob(Win32PrintService service, PrintPeer peer){ - this.service = service; - this.peer = peer; - } - - - @Override - public PrintService getPrintService(){ - return service; - } - - - @Override - public PrintJobAttributeSet getAttributes(){ - synchronized(this){ - if(jobAttrSet == null){ - /* just return an empty set until the job is submitted */ - PrintJobAttributeSet jobSet = new HashPrintJobAttributeSet(); - return AttributeSetUtilities.unmodifiableView(jobSet); - }else{ - return jobAttrSet; - } - } - } - - - @Override - public void addPrintJobListener(PrintJobListener listener){ - synchronized(this){ - if(listener == null){ - return; - } - if(jobListeners == null){ - jobListeners = new ArrayList<PrintJobListener>(); - } - jobListeners.add(listener); - } - } - - - @Override - public void removePrintJobListener(PrintJobListener listener){ - synchronized(this){ - if(listener == null || jobListeners == null){ - return; - } - jobListeners.remove(listener); - if(jobListeners.isEmpty()){ - jobListeners = null; - } - } - } - - - /* Closes any stream already retrieved for the data. - * We want to avoid unnecessarily asking the Doc to create a stream only - * to get a reference in order to close it because the job failed. - * If the representation class is itself a "stream", this - * closes that stream too. - */ - private void closeDataStreams() { - // TODO - } - - private void notifyEvent(int reason) { - - /* since this method should always get called, here's where - * we will perform the clean up of any data stream supplied. - */ - switch (reason) { - case PrintJobEvent.DATA_TRANSFER_COMPLETE: - case PrintJobEvent.JOB_CANCELED : - case PrintJobEvent.JOB_FAILED : - case PrintJobEvent.NO_MORE_EVENTS : - case PrintJobEvent.JOB_COMPLETE : - closeDataStreams(); - } - - synchronized (this) { - if (jobListeners != null) { - PrintJobListener listener; - PrintJobEvent event = new PrintJobEvent(this, reason); - for (int i = 0; i < jobListeners.size(); i++) { - listener = jobListeners.get(i); - switch (reason) { - - case PrintJobEvent.JOB_COMPLETE : - listener.printJobCompleted(event); - break; - - case PrintJobEvent.JOB_CANCELED : - listener.printJobCanceled(event); - break; - - case PrintJobEvent.JOB_FAILED : - listener.printJobFailed(event); - break; - - case PrintJobEvent.DATA_TRANSFER_COMPLETE : - listener.printDataTransferCompleted(event); - break; - - case PrintJobEvent.NO_MORE_EVENTS : - listener.printJobNoMoreEvents(event); - break; - - default: - break; - } - } - } - } - } - - @Override - public void addPrintJobAttributeListener(PrintJobAttributeListener listener, PrintJobAttributeSet attributes){ - synchronized(this){ - if(listener == null){ - return; - } - if(attrListeners == null){ - attrListeners = new ArrayList<PrintJobAttributeListener>(); - listenedAttributeSets = new ArrayList<PrintJobAttributeSet>(); - } - attrListeners.add(listener); - if(attributes == null){ - attributes = new HashPrintJobAttributeSet(); - } - listenedAttributeSets.add(attributes); - } - } - - - @Override - public void removePrintJobAttributeListener(PrintJobAttributeListener listener){ - synchronized(this){ - if(listener == null || attrListeners == null){ - return; - } - int index = attrListeners.indexOf(listener); - if(index == -1){ - return; - }else{ - attrListeners.remove(index); - listenedAttributeSets.remove(index); - if(attrListeners.isEmpty()){ - attrListeners = null; - listenedAttributeSets = null; - } - } - } - } - - - @Override - public void print(Doc doc, PrintRequestAttributeSet attributes) throws PrintException{ - - synchronized(this){ - if(printing){ - throw new PrintException("already printing"); - }else{ - printing = true; - } - } - - PrinterState prnState = (PrinterState)service.getAttribute(PrinterState.class); - if(prnState == PrinterState.STOPPED){ - PrinterStateReasons prnStateReasons = (PrinterStateReasons)service.getAttribute(PrinterStateReasons.class); - if((prnStateReasons != null) && (prnStateReasons.containsKey(PrinterStateReason.SHUTDOWN))){ - throw new PrintException("PrintService is no longer available."); - } - } - - if((PrinterIsAcceptingJobs)(service.getAttribute(PrinterIsAcceptingJobs.class)) == PrinterIsAcceptingJobs.NOT_ACCEPTING_JOBS){ - throw new PrintException("Printer is not accepting job."); - } - - this.doc = doc; - /* check if the parameters are valid before doing much processing */ - DocFlavor flavor = doc.getDocFlavor(); - Object data; - - try{ - data = doc.getPrintData(); - }catch(IOException e){ - notifyEvent(PrintJobEvent.JOB_FAILED); - throw new PrintException("can't get print data: " + e.toString()); - } - - if(flavor == null || (!service.isDocFlavorSupported(flavor))){ - notifyEvent(PrintJobEvent.JOB_FAILED); - throw new PrintJobFlavorException("invalid flavor", flavor); - } - - initializeAttributeSets(doc, attributes); - - getAttributeValues(flavor); - - String repClassName = flavor.getRepresentationClassName(); - - if(flavor.equals(DocFlavor.INPUT_STREAM.GIF) || flavor.equals(DocFlavor.INPUT_STREAM.JPEG) - || flavor.equals(DocFlavor.INPUT_STREAM.PNG) || flavor.equals(DocFlavor.BYTE_ARRAY.GIF) - || flavor.equals(DocFlavor.BYTE_ARRAY.JPEG) || flavor.equals(DocFlavor.BYTE_ARRAY.PNG)){ - try{ - instream = doc.getStreamForBytes(); - if(instream == null){ - notifyEvent(PrintJobEvent.JOB_FAILED); - throw new PrintException("No stream for data"); - } - printableJob(new ImagePrinter(instream)); - service.wakeNotifier(); - return; - }catch(ClassCastException cce){ - notifyEvent(PrintJobEvent.JOB_FAILED); - throw new PrintException(cce); - }catch(IOException ioe){ - notifyEvent(PrintJobEvent.JOB_FAILED); - throw new PrintException(ioe); - } - }else if(flavor.equals(DocFlavor.URL.GIF) || flavor.equals(DocFlavor.URL.JPEG) - || flavor.equals(DocFlavor.URL.PNG)){ - try{ - printableJob(new ImagePrinter((URL)data)); - service.wakeNotifier(); - return; - }catch(ClassCastException cce){ - notifyEvent(PrintJobEvent.JOB_FAILED); - throw new PrintException(cce); - } - }else if(repClassName.equals("java.awt.print.Pageable")){ - try{ - pageableJob((Pageable)doc.getPrintData()); - service.wakeNotifier(); - return; - }catch(ClassCastException cce){ - notifyEvent(PrintJobEvent.JOB_FAILED); - throw new PrintException(cce); - }catch(IOException ioe){ - notifyEvent(PrintJobEvent.JOB_FAILED); - throw new PrintException(ioe); - } - }else if(repClassName.equals("java.awt.print.Printable")){ - try{ - printableJob((Printable)doc.getPrintData()); - service.wakeNotifier(); - return; - }catch(ClassCastException cce){ - notifyEvent(PrintJobEvent.JOB_FAILED); - throw new PrintException(cce); - }catch(IOException ioe){ - notifyEvent(PrintJobEvent.JOB_FAILED); - throw new PrintException(ioe); - } - }else if(repClassName.equals("[B") || repClassName.equals("java.io.InputStream") - || repClassName.equals("java.net.URL")){ - - if(repClassName.equals("java.net.URL")){ - URL url = (URL)data; - try{ - instream = url.openStream(); - }catch(IOException e){ - notifyEvent(PrintJobEvent.JOB_FAILED); - throw new PrintException(e.toString()); - } - }else{ - try{ - instream = doc.getStreamForBytes(); - }catch(IOException ioe){ - notifyEvent(PrintJobEvent.JOB_FAILED); - throw new PrintException(ioe.toString()); - } - } - - if(instream == null){ - notifyEvent(PrintJobEvent.JOB_FAILED); - throw new PrintException("No stream for data"); - } - - if(mDestination != null){ // if destination attribute is set - try{ - FileOutputStream fos = new FileOutputStream(mDestination); - byte[] buffer = new byte[1024]; - int cread; - - while((cread = instream.read(buffer, 0, buffer.length)) >= 0){ - fos.write(buffer, 0, cread); - } - fos.flush(); - fos.close(); - }catch(FileNotFoundException fnfe){ - notifyEvent(PrintJobEvent.JOB_FAILED); - throw new PrintException(fnfe.toString()); - }catch(IOException ioe){ - notifyEvent(PrintJobEvent.JOB_FAILED); - throw new PrintException(ioe.toString()); - } - notifyEvent(PrintJobEvent.DATA_TRANSFER_COMPLETE); - notifyEvent(PrintJobEvent.JOB_COMPLETE); - service.wakeNotifier(); - return; - } - - notifyEvent(PrintJobEvent.JOB_FAILED); - throw new PrintException("Print job failed. IKVM does not support raw data currently."); - }else{ - notifyEvent(PrintJobEvent.JOB_FAILED); - throw new PrintException("unrecognized class: " + repClassName); - } - } - - - public void printableJob(Printable printable) throws PrintException { - throw new PrintException("Win32PrintJob.printableJob() not implemented:"); - } - - public void pageableJob(Pageable pageable) throws PrintException { - // a general hint for this code here: The PrintJob implementation (WPrinterJob) in the Oracle - // JRE implements SunPrinterJobService - which we don't here. Unfortunately this is used as a hint - // to distinguish between the build-in printer service and custom printer services. So for the RasterPrinterJob - // this implementation of the Win32PrintJob is a custom print job! That's why several methods of - // RasterPrintJob(which only apply to the SunPrinterJobService otherwise) had to be moved here. - // This includes the attribute resolving and the media patch for instance. - try { - PrintDocument printDocument = createPrintDocument( ); - pageable = patchMedia( pageable ); - - printDocument.add_QueryPageSettings(new QueryPageSettingsEventHandler(new QueryPage( pageable, printColor, mediaTray ) ) ); - printDocument.add_PrintPage(new PrintPageEventHandler(new PrintPage(pageable))); - printDocument.Print(); - if(printerException != null){ - throw printerException; - } - //TODO throw exception on Cancel - 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); - } - } - - - private PrintDocument createPrintDocument() throws PrintException{ - PrintDocument printDocument = new PrintDocument(); - PrinterSettings settings = printDocument.get_PrinterSettings(); - settings.set_PrinterName( service.getName() ); - if( !settings.get_IsValid() ){ - throw new PrintException("Printer name ''" + service.getName() + "' is invalid."); - } - - if( jobName != null ){ - printDocument.set_DocumentName( jobName ); - } - printDocument.get_DefaultPageSettings().set_Color(printColor); - - Attribute destination = reqAttrSet.get(Destination.class); - if(destination instanceof Destination){ - File destFile = new File(((Destination)destination).getURI()); - settings.set_PrintFileName(destFile.getAbsolutePath()); - settings.set_PrintToFile(true); - } - - settings.set_Copies((short)copies); - boolean collated = false; - if(copies > 1){ - Object collate = reqAttrSet.get(SheetCollate.class); - if( collate == null ){ - collate = service.getDefaultAttributeValue(SheetCollate.class); - } - collated = collate == SheetCollate.COLLATED; - settings.set_Collate( collated ); - } - Attribute pageRangeObj = reqAttrSet.get(PageRanges.class); - if( pageRangeObj != null ){ - int[][] ranges = ((PageRanges)pageRangeObj).getMembers(); - if( ranges.length > 1 ){ - settings.set_PrintRange( PrintRange.wrap( PrintRange.Selection ) ); - } else { - if( ranges.length > 0 ){ - settings.set_FromPage(ranges[0][0]); - settings.set_ToPage(ranges[0][1]); - settings.set_PrintRange( PrintRange.wrap( PrintRange.SomePages ) ); - } // else allPages??? - } - } else { - settings.set_PrintRange( PrintRange.wrap( PrintRange.AllPages ) ); - } - pageRanges = new PageNumberConverter( (PageRanges)pageRangeObj, copies, collated ); - return printDocument; - } - - // copied from RasterPrintJob - // Since we don't implement SunPrinterJobService the hack in RasterPrintService which applies - // the media format to the auto-generated OpenBook doesn't work here. So we have to modify - // the page format of the OpenBook here - equal to the code in RasterPrintJob - private Pageable patchMedia( Pageable pageable ){ - /* 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)reqAttrSet.get(Media.class); - OrientationRequested orientReq = (OrientationRequested)reqAttrSet.get(OrientationRequested.class); - MediaPrintableArea mpa = (MediaPrintableArea)reqAttrSet.get(MediaPrintableArea.class); - - if ((orientReq != null || media != null || mpa != null) && pageable instanceof OpenBook) { - - /* We could almost(!) use PrinterJob.getPageFormat() except - * here we need to start with the PageFormat from the OpenBook : - */ - 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, reqAttrSet); - if (mpaVals instanceof MediaPrintableArea[] && ((MediaPrintableArea[])mpaVals).length > 0) { - mpa = ((MediaPrintableArea[])mpaVals)[0]; - } - } - - if (isSupportedValue(orientReq, reqAttrSet) || (!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, reqAttrSet) || (!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, reqAttrSet) || (!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); - return new OpenBook(pf, printable); - } - return pageable; - } - - // copied from RasterPrintJob to since we don't implement SunPrinterJobService - /** - * The passed in PageFormat is cloned and altered to be usable on - * the PrinterJob's current printer. - */ - private PageFormat validatePage(PageFormat page) { - PageFormat newPage = (PageFormat)page.clone(); - Paper newPaper = new Paper(); - validatePaper(newPage.getPaper(), newPaper); - newPage.setPaper(newPaper); - - return newPage; - } - - // copied from RasterPrintJob to since we don't implement SunPrinterJobService - /** - * updates a Paper object to reflect the current printer's selected - * paper size and imageable area for that paper size. - * Default implementation copies settings from the original, applies - * applies some validity checks, changes them only if they are - * clearly unreasonable, then sets them into the new Paper. - * Subclasses are expected to override this method to make more - * informed decisons. - */ - protected void validatePaper(Paper origPaper, Paper newPaper) { - if (origPaper == null || newPaper == null) { - return; - } else { - double wid = origPaper.getWidth(); - double hgt = origPaper.getHeight(); - double ix = origPaper.getImageableX(); - double iy = origPaper.getImageableY(); - double iw = origPaper.getImageableWidth(); - double ih = origPaper.getImageableHeight(); - - /* Assume any +ve values are legal. Overall paper dimensions - * take precedence. Make sure imageable area fits on the paper. - */ - Paper defaultPaper = new Paper(); - wid = ((wid > 0.0) ? wid : defaultPaper.getWidth()); - hgt = ((hgt > 0.0) ? hgt : defaultPaper.getHeight()); - ix = ((ix > 0.0) ? ix : defaultPaper.getImageableX()); - iy = ((iy > 0.0) ? iy : defaultPaper.getImageableY()); - iw = ((iw > 0.0) ? iw : defaultPaper.getImageableWidth()); - ih = ((ih > 0.0) ? ih : defaultPaper.getImageableHeight()); - /* full width/height is not likely to be imageable, but since we - * don't know the limits we have to allow it - */ - if (iw > wid) { - iw = wid; - } - if (ih > hgt) { - ih = hgt; - } - if ((ix + iw) > wid) { - ix = wid - iw; - } - if ((iy + ih) > hgt) { - iy = hgt - ih; - } - newPaper.setSize(wid, hgt); - newPaper.setImageableArea(ix, iy, iw, ih); - } - } - - // copied from RasterPrintJob - /** - * Checks whether a certain attribute value is valid for the current print service - * @param attrval the attribute value - * @param attrset Set of printing attributes for a supposed job (both job-level attributes and document-level attributes), or null. - * @return true if valid - */ - protected boolean isSupportedValue(Attribute attrval, PrintRequestAttributeSet attrset) { - return (attrval != null && service != null && service.isAttributeValueSupported(attrval, DocFlavor.SERVICE_FORMATTED.PAGEABLE, attrset)); - } - - - /* - * 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); - } - - private void getAttributeValues(DocFlavor flavor) throws PrintException { - - if (reqAttrSet.get(Fidelity.class) == Fidelity.FIDELITY_TRUE) { - fidelity = true; - } else { - fidelity = false; - } - - Attribute chroma = reqAttrSet.get( Chromaticity.class ); - // TODO check whether supported by the print service - printColor = chroma == Chromaticity.COLOR; - - Attribute newTray = reqAttrSet.get( Media.class ); - if( newTray instanceof MediaTray ){ - mediaTray = (MediaTray)newTray; - } - // TODO check whether supported by the print service - - 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, flavor, null)) { - mediaSize = MediaSize.getMediaSizeForName(mediaName); - } - } - } else if (category == OrientationRequested.class) { - orient = (OrientationRequested)attr; - } - } - } - - - @Override - public void cancel() throws PrintException{ - synchronized(this){ - if(!printing){ - throw new PrintException("Job is not yet submitted."); - }else if(job != null && !printReturned){ - job.cancel(); - notifyEvent(PrintJobEvent.JOB_CANCELED); - return; - }else{ - throw new PrintException("Job could not be cancelled."); - } - } - } - - /** - * Converts the Java 1/72 inch to .NET 1/100 inch - * @param javaLength the java length in 1/72 inch - * @return the .NET length in 1/100 inch - */ - private static int java2netLength( int javaLength ){ - return (int) Math.round( (double)(javaLength * 100) / 72d ); - } - - /** - * Converts the Java 1/72 inch to .NET 1/100 inch - * @param javaLength the java length in 1/72 inch - * @return the .NET length in 1/100 inch - */ - private static int java2netLength( double javaLength ){ - return (int) Math.round( (javaLength * 100) / 72d ); - } - - - private class PrintPage implements PrintPageEventHandler.Method{ - - private final Pageable pageable; - private int pageIndex; - - - PrintPage(Pageable pageable){ - this.pageable = pageable; - } - - - @Override - public void Invoke(Object paramObject, PrintPageEventArgs ev){ - - try{ - int realPage = pageRanges.getPageForIndex(pageIndex); - - Printable printable = pageable.getPrintable(realPage); - PageFormat pageFormat = pageable.getPageFormat(realPage); - - - BufferedImage pBand = new BufferedImage(1, 1, BufferedImage.TYPE_3BYTE_BGR); - Graphics2D imageGraphics = pBand.createGraphics(); - ((RasterPrinterJob)job).setGraphicsConfigInfo(imageGraphics.getTransform(), pageFormat.getImageableWidth(), pageFormat.getImageableHeight()); - PeekGraphics peekGraphics = new PeekGraphics(imageGraphics, job ); - - /* - * Because Sun is calling first with a PeekGraphics that we do it else for compatibility - */ - if(realPage == 0){ - int pageResult = printable.print(peekGraphics, pageFormat, realPage); - if(pageResult != Printable.PAGE_EXISTS){ - ev.set_HasMorePages(false); - ev.set_Cancel(true); - return; - } - } - Graphics2D printGraphics = peer.createGraphics(ev.get_Graphics()); - Graphics2D g2d = ((Graphics2D)printGraphics); - int tX = (int) pageFormat.getWidth(); - int tY = (int) pageFormat.getHeight(); - // apply Java to .NET scaling (1/72 inch to 1/100 inch) - g2d.scale(100d/72d, 100d/72d); - // NOTE on Landscape printing: - // Setting landscape to true on the printer settings - // of a page already rotates the page! The orig. java code rotates the page itself, - // for .NET this is not required. - if( orient == OrientationRequested.REVERSE_LANDSCAPE){ - g2d.translate( tX, tY ); - g2d.rotate( Math.PI ); - } - - printable.print(printGraphics, pageFormat, realPage); - - realPage = pageRanges.getPageForIndex(++pageIndex); - if( realPage >= 0 ){ - printable = pageable.getPrintable(realPage); - pageFormat = pageable.getPageFormat(realPage); - int pageResult = printable.print(peekGraphics, pageFormat, realPage); - ev.set_HasMorePages( pageResult == Printable.PAGE_EXISTS ); - } else { - ev.set_HasMorePages( false ); - } - }catch(PrinterException ex){ - printerException = ex; - ex.printStackTrace(); - ev.set_HasMorePages(false); - } - - } - - } - - private class QueryPage implements QueryPageSettingsEventHandler.Method{ - - private final Pageable pageable; - private int pageIndex; - private final boolean printColor; - private final MediaTray tray; - - - QueryPage(Pageable pageable, boolean printColor, MediaTray tray ){ - this.printColor = printColor; - this.pageable = pageable; - this.tray = tray; - } - - @Override - public void Invoke(Object source, QueryPageSettingsEventArgs e) { - int realPage = pageRanges.getPageForIndex(pageIndex); - // apply page settings to the current page - PageFormat format = pageable.getPageFormat(realPage); - PageSettings pageSettings = e.get_PageSettings(); - pageSettings.set_Color( printColor ); - PaperSource paperSource = service.getPaperSourceForTray( tray ); - if( paperSource != null ){ - pageSettings.set_PaperSource( paperSource ); - } - - PaperSize ps = new PaperSize(); - ps.set_Height( java2netLength( format.getHeight() ) ); - ps.set_Width( java2netLength( format.getWidth() ) ); - pageSettings.set_PaperSize( ps ); - - Margins margins = new Margins(); - margins.set_Left( java2netLength( format.getImageableX() ) ); - margins.set_Top( java2netLength( format.getImageableY() ) ); - margins.set_Right( java2netLength(format.getWidth() - format.getImageableX() - format.getImageableWidth() ) ); - margins.set_Bottom( java2netLength(format.getHeight() - format.getImageableY() - format.getImageableHeight() ) ); - pageSettings.set_Margins( margins ); - pageIndex++; - } - } - - /** - * Determines which logical page to print for a certain physical page - */ - public static class PageNumberConverter{ - - private List<Range> ranges; - private int totalPages = -1; - private final int copies; - - public PageNumberConverter( PageRanges pages, int copies, boolean collated ) { - // NOTE: uncollated is handled by the printer driver! If we would handle that here, - // we would get copies^2 copies! - this.copies = collated ? copies : 1; - if( pages != null && pages.getMembers() != null && pages.getMembers().length > 0 ){ - TreeSet<Range> rangesSort = new TreeSet<Range>(); - OUTER: - for( int[] range : pages.getMembers() ){ - Range r = new Range( range[0], range[1] + 1 ); // +1 to inlucde the uppre end - for( Range recent : rangesSort ){ - if( recent.canMerge(r) ){ - recent.merge(r); - continue OUTER; - } - } - rangesSort.add( r ); - } - // finally merge - Range recent = null; - ranges = new ArrayList<Range>(); - for( Range r : rangesSort ){ - if( recent != null && recent.canMerge( r ) ){ - recent.merge( r ); - } else { - ranges.add( r ); - recent = r; - } - } - // calculate total pages, required for collated copies - totalPages = 0; - for( Range r : rangesSort ){ - int diff = r.end - r.start; - totalPages += diff; - } - } - } - - /** - * Must be called in case the printable returns no-more-pages. Will return whether the print job - * will continue due to pending copies - * @param index the current page index - * @return if false, the print job will continue with copies, if true terminate the job - */ - public boolean checkJobComplete( int index ){ - if( ranges != null ){ - return true; - } - if( totalPages < 0 ){ - // this is the first time, this was called for 'all-pages' so it's the total number - totalPages = index; - } - return index > copies * totalPages; - } - - /** - * Returns which page to be printed for a certain page index - * @param index the inex to be printed - * @return the page number or -1, if there is no page for this index - */ - public int getPageForIndex( int index ){ - if( index < 0 || ( totalPages >=0 && index >= copies * totalPages ) ){ - return -1; - } - if( ranges == null ){ - return totalPages >=0 ? index % totalPages : index; - } - int counter = 0; - if( copies > 1 ){ - counter += (index / totalPages) * totalPages; - } - for( Range r : ranges ){ - int upper = counter + (r.end - r.start); - if( index < upper ){ - // so we're in the correct range - return r.start + ( index - counter ) - 1; - } else { - counter = upper; - } - } - return -1; - } - - /** - * A singular page range - */ - private static class Range implements Comparable<Range> { - - public int start; - public int end; - - public Range(int start, int end) { - this.start = start; - this.end = end; - } - - /** - * Checks whether the ranges intersect of have no gap in between - * @param otherRange the range to be checked - * @return true, if the ranges can be merged - */ - public boolean canMerge( Range otherRange ){ - if( otherRange.end >= start && otherRange.end <= end ){ - return true; - } - if( otherRange.start >= start && otherRange.start <= end ){ - return true; - } - return false; - } - - /** - * Merges the other range into this range. Ignores the gap between the ranges if there is any - * @param otherRange the range to be merged - */ - public void merge( Range otherRange ){ - start = Math.min(start, otherRange.start); - end = Math.max(end, otherRange.end); - } - - public int compareTo(Range o) { - return start - o.start; - } - } - } -} diff --git a/openjdk/sun/print/Win32PrintService.java b/openjdk/sun/print/Win32PrintService.java deleted file mode 100644 index 134c7a94..00000000 --- a/openjdk/sun/print/Win32PrintService.java +++ /dev/null @@ -1,920 +0,0 @@ -/* - * Copyright (c) 2000, 2006, Oracle and/or its affiliates. All rights reserved. - * Copyright (C) 2009, 2012 Volker Berlin (i-net software) - * Copyright (C) 2010, 2011 Karsten Heinrich (i-net software) - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ -package sun.print; - -import java.io.File; -import java.net.URI; -import java.net.URISyntaxException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashSet; -import java.util.Hashtable; -import java.util.List; -import java.util.Set; -import java.util.concurrent.atomic.AtomicInteger; - -import javax.print.DocFlavor; -import javax.print.DocPrintJob; -import javax.print.PrintService; -import javax.print.ServiceUIFactory; -import javax.print.attribute.Attribute; -import javax.print.attribute.AttributeSet; -import javax.print.attribute.AttributeSetUtilities; -import javax.print.attribute.HashPrintServiceAttributeSet; -import javax.print.attribute.PrintServiceAttribute; -import javax.print.attribute.PrintServiceAttributeSet; -import javax.print.attribute.standard.Chromaticity; -import javax.print.attribute.standard.ColorSupported; -import javax.print.attribute.standard.Copies; -import javax.print.attribute.standard.CopiesSupported; -import javax.print.attribute.standard.Destination; -import javax.print.attribute.standard.Fidelity; -import javax.print.attribute.standard.JobName; -import javax.print.attribute.standard.Media; -import javax.print.attribute.standard.MediaPrintableArea; -import javax.print.attribute.standard.MediaSize; -import javax.print.attribute.standard.MediaSizeName; -import javax.print.attribute.standard.MediaTray; -import javax.print.attribute.standard.OrientationRequested; -import javax.print.attribute.standard.PageRanges; -import javax.print.attribute.standard.PrintQuality; -import javax.print.attribute.standard.PrinterIsAcceptingJobs; -import javax.print.attribute.standard.PrinterName; -import javax.print.attribute.standard.PrinterResolution; -import javax.print.attribute.standard.PrinterState; -import javax.print.attribute.standard.PrinterStateReasons; -import javax.print.attribute.standard.QueuedJobCount; -import javax.print.attribute.standard.RequestingUserName; -import javax.print.attribute.standard.SheetCollate; -import javax.print.attribute.standard.Sides; -import javax.print.event.PrintServiceAttributeListener; - -import cli.System.NewsStyleUriParser; -import cli.System.Type; -import cli.System.Collections.IEnumerator; -import cli.System.Drawing.RectangleF; -import cli.System.Drawing.Printing.Duplex; -import cli.System.Drawing.Printing.PaperKind; -import cli.System.Drawing.Printing.PaperSize; -import cli.System.Drawing.Printing.PaperSource; -import cli.System.Drawing.Printing.PrintDocument; -import cli.System.Drawing.Printing.PrinterSettings; -import cli.System.Drawing.Printing.PrinterSettings.PaperSizeCollection; -import cli.System.Drawing.Printing.PrinterSettings.PaperSourceCollection; -import cli.System.Drawing.Printing.PrinterSettings.PrinterResolutionCollection; -import cli.System.Net.Mime.MediaTypeNames; - -/** - * @author Volker Berlin - */ -public class Win32PrintService implements PrintService { - // note: the Win32PrintService is implemented as foreign service (doesn't implement SunPrinterJobService) - // to avoid implementing the WPrinterJob - - private static final DocFlavor[] supportedFlavors = { - DocFlavor.SERVICE_FORMATTED.PAGEABLE, - DocFlavor.SERVICE_FORMATTED.PRINTABLE, - }; - - /** Mapping for PageSize.RawKind to predefined MediaSizeName */ - private static final MediaSizeName[] MEDIA_NAMES = new MediaSizeName[70]; - - private static final Hashtable<String, MediaSizeName> CUSTOM_MEDIA_NAME = new Hashtable<>(); - - /* it turns out to be inconvenient to store the other categories - * separately because many attributes are in multiple categories. - */ - private static Class[] otherAttrCats = { - JobName.class, - RequestingUserName.class, - Copies.class, - Destination.class, - OrientationRequested.class, - PageRanges.class, - Media.class, - MediaPrintableArea.class, - Fidelity.class, - // We support collation on 2D printer jobs, even if the driver can't. - SheetCollate.class, - SunAlternateMedia.class, - Chromaticity.class - }; - - // conversion from 1/100 Inch (.NET) to um (Java) - private static final int INCH100_TO_MYM = 254; - private static final int MATCH_DIFF = 500; // 0.5 mm - - static { - MEDIA_NAMES[0] = MediaSizeName.NA_LETTER; - MEDIA_NAMES[1] = MediaSizeName.NA_LETTER ; - MEDIA_NAMES[2] = MediaSizeName.TABLOID ; - MEDIA_NAMES[3] = MediaSizeName.LEDGER ; - MEDIA_NAMES[4] = MediaSizeName.NA_LEGAL ; - MEDIA_NAMES[5] = MediaSizeName.INVOICE ; // Statement - MEDIA_NAMES[6] = MediaSizeName.EXECUTIVE ; - MEDIA_NAMES[7] = MediaSizeName.ISO_A3 ; - MEDIA_NAMES[8] = MediaSizeName.ISO_A4 ; - MEDIA_NAMES[9] = MediaSizeName.ISO_A4 ; // A4Small, 10 - MEDIA_NAMES[10] = MediaSizeName.ISO_A5 ; - MEDIA_NAMES[11] = MediaSizeName.JIS_B4 ; - MEDIA_NAMES[12] = MediaSizeName.JIS_B5 ; - MEDIA_NAMES[13] = MediaSizeName.FOLIO ; - MEDIA_NAMES[14] = MediaSizeName.QUARTO ; - MEDIA_NAMES[15] = MediaSizeName.NA_10X14_ENVELOPE ; - MEDIA_NAMES[16] = MediaSizeName.B ; // 10x17 Envelope - MEDIA_NAMES[17] = MediaSizeName.NA_LETTER ; // Note - MEDIA_NAMES[18] = MediaSizeName.NA_NUMBER_9_ENVELOPE ; - MEDIA_NAMES[19] = MediaSizeName.NA_NUMBER_10_ENVELOPE ; // 20 - MEDIA_NAMES[20] = MediaSizeName.NA_NUMBER_11_ENVELOPE ; - MEDIA_NAMES[21] = MediaSizeName.NA_NUMBER_12_ENVELOPE ; - MEDIA_NAMES[22] = MediaSizeName.NA_NUMBER_14_ENVELOPE ; - MEDIA_NAMES[23] = MediaSizeName.C ; - MEDIA_NAMES[24] = MediaSizeName.D ; - MEDIA_NAMES[25] = MediaSizeName.E ; - MEDIA_NAMES[26] = MediaSizeName.ISO_DESIGNATED_LONG ; - MEDIA_NAMES[27] = MediaSizeName.ISO_C5 ; - MEDIA_NAMES[28] = MediaSizeName.ISO_C3 ; - MEDIA_NAMES[29] = MediaSizeName.ISO_C4 ; // 30 - MEDIA_NAMES[30] = MediaSizeName.ISO_C6 ; - MEDIA_NAMES[31] = MediaSizeName.ITALY_ENVELOPE ; - MEDIA_NAMES[32] = MediaSizeName.ISO_B4 ; - MEDIA_NAMES[33] = MediaSizeName.ISO_B5 ; - MEDIA_NAMES[34] = MediaSizeName.ISO_B6 ; - MEDIA_NAMES[35] = MediaSizeName.ITALY_ENVELOPE ; - MEDIA_NAMES[36] = MediaSizeName.MONARCH_ENVELOPE ; - MEDIA_NAMES[37] = MediaSizeName.PERSONAL_ENVELOPE ; - MEDIA_NAMES[38] = MediaSizeName.NA_10X15_ENVELOPE ; // USStandardFanfold - MEDIA_NAMES[39] = MediaSizeName.NA_9X12_ENVELOPE ; // GermanStandardFanfold, 40 - MEDIA_NAMES[40] = MediaSizeName.FOLIO ; // GermanLegalFanfold - MEDIA_NAMES[41] = MediaSizeName.ISO_B4 ; - MEDIA_NAMES[42] = MediaSizeName.JAPANESE_POSTCARD ; - MEDIA_NAMES[43] = MediaSizeName.NA_9X11_ENVELOPE ; - - MEDIA_NAMES[65] = MediaSizeName.ISO_A2 ; - - MEDIA_NAMES[69] = MediaSizeName.ISO_A6 ; - -// // augment the media size with the .NET default sizes available on the printer -// PrinterSettings ps = new PrinterSettings(); -// IEnumerator printers = PrinterSettings.get_InstalledPrinters().GetEnumerator(); -// printers.Reset(); -// while( printers.MoveNext() ){ -// ps.set_PrinterName( (String) printers.get_Current() ); -// IEnumerator sizes = ps.get_PaperSizes().GetEnumerator(); -// sizes.Reset(); -// while( sizes.MoveNext() ){ -// PaperSize size = (PaperSize) sizes.get_Current(); -// int kind = size.get_RawKind(); -// if( kind >= 0 && kind < MEDIA_NAMES.length && MEDIA_NAMES[kind] == null ){ -// MEDIA_NAMES[kind] = new CustomMediaSizeName( size.get_PaperName() ); -// int x = size.get_Width(); -// int y = size.get_Height(); -// if( x > y ){ // not allowed by MediaSize -// int tmp = x; -// x = y; -// y = tmp; -// } -// new MediaSize(x, y, INCH100_TO_MYM, MEDIA_NAMES[kind]); // cache entry in map -// } -// } -// } - } - - private final PrintPeer peer; - - private final String printer; - private final PrinterSettings settings; - private PrinterName name; - - private MediaTray[] mediaTrays; - - transient private ServiceNotifier notifier = null; - - public Win32PrintService(String name, PrintPeer peer){ - if(name == null){ - throw new IllegalArgumentException("null printer name"); - } - this.peer = peer; - printer = name; - settings = new PrintDocument().get_PrinterSettings(); - settings.set_PrinterName(printer); - } - - - @Override - public String getName(){ - return printer; - } - - - private PrinterName getPrinterName(){ - if(name == null){ - name = new PrinterName(printer, null); - } - return name; - } - - - public void wakeNotifier() { - synchronized (this) { - if (notifier != null) { - notifier.wake(); - } - } - } - - - @Override - public void addPrintServiceAttributeListener(PrintServiceAttributeListener listener){ - synchronized (this) { - if (listener == null) { - return; - } - if (notifier == null) { - notifier = new ServiceNotifier(this); - } - notifier.addListener(listener); - } - } - - - @Override - public void removePrintServiceAttributeListener(PrintServiceAttributeListener listener){ - synchronized (this) { - if (listener == null || notifier == null ) { - return; - } - notifier.removeListener(listener); - if (notifier.isEmpty()) { - notifier.stopNotifier(); - notifier = null; - } - } - } - - - @Override - public DocPrintJob createPrintJob(){ - SecurityManager security = System.getSecurityManager(); - if(security != null){ - security.checkPrintJobAccess(); - } - return new Win32PrintJob(this, peer); - } - - - @Override - public <T extends PrintServiceAttribute>T getAttribute(Class<T> category){ - if(category == null){ - throw new NullPointerException("category"); - } - if(!(PrintServiceAttribute.class.isAssignableFrom(category))){ - throw new IllegalArgumentException("The categhory '" + category + "' is not a valid PrintServiceAttribute"); - } - if(category == ColorSupported.class){ - // works better than settings.get_SupportsColor(); - if(settings.get_DefaultPageSettings().get_Color()){ - return (T)ColorSupported.SUPPORTED; - }else{ - return (T)ColorSupported.NOT_SUPPORTED; - } - }else if(category == PrinterName.class){ - return (T)getPrinterName(); - } else { - // QueuedJobCount and PrinterIsAcceptingJobs - return (T)peer.getPrinterStatus(printer, category); - } - } - - - @Override - public PrintServiceAttributeSet getAttributes(){ - PrintServiceAttributeSet attrs = new HashPrintServiceAttributeSet(); - attrs.add(getPrinterName()); - PrinterIsAcceptingJobs acptJobs = getAttribute(PrinterIsAcceptingJobs.class); - if(acptJobs != null){ - attrs.add(acptJobs); - } - PrinterState prnState = getAttribute(PrinterState.class); - if(prnState != null){ - attrs.add(prnState); - } - PrinterStateReasons prnStateReasons = getAttribute(PrinterStateReasons.class); - if(prnStateReasons != null){ - attrs.add(prnStateReasons); - } - QueuedJobCount jobCount = getAttribute(QueuedJobCount.class); - if(jobCount != null){ - attrs.add(jobCount); - } - // TODO: Seems to be more accurate than settings.get_SupportsColor(), which doesn't work for CutePDF - if(settings.get_DefaultPageSettings().get_Color()){ - attrs.add(ColorSupported.SUPPORTED); - }else{ - attrs.add(ColorSupported.NOT_SUPPORTED); - } - - return AttributeSetUtilities.unmodifiableView(attrs); - } - - - @Override - public Object getDefaultAttributeValue(Class<? extends Attribute> category){ - if (category == null) { - throw new NullPointerException("category must not be null"); - } - if ( !Attribute.class.isAssignableFrom( category ) ) { - throw new IllegalArgumentException( category +" has to be an " + Attribute.class.getName() ); - } - if ( !isAttributeCategorySupported( category ) ) { - return null; - } - if (category == Copies.class) { - short copies = settings.get_Copies(); - return new Copies( copies > 0 ? copies : 1 ); - } else if (category == Chromaticity.class) { - // NOTE: this works for CutePDF, settings.get_SupportsColor() does not - return settings.get_DefaultPageSettings().get_Color() ? Chromaticity.COLOR : Chromaticity.MONOCHROME; - } else if (category == JobName.class) { - return new JobName( "Java Printing", null ); // TODO this is Java-Default, use another one for IKVM? - } else if (category == OrientationRequested.class) { - return settings.get_DefaultPageSettings().get_Landscape() ? OrientationRequested.LANDSCAPE : OrientationRequested.PORTRAIT; - } else if (category == PageRanges.class) { - return new PageRanges(1, Integer.MAX_VALUE ); - } else if (category == Media.class) { - int rawKind = settings.get_DefaultPageSettings().get_PaperSize().get_RawKind(); - if( rawKind > MEDIA_NAMES.length || rawKind < 1 || MEDIA_NAMES[ rawKind - 1 ] == null ){ // custom page format - return findMatchingMedia( settings.get_DefaultPageSettings().get_PaperSize() ); - } else { - return MEDIA_NAMES[ rawKind - 1 ]; - } - } else if (category == MediaPrintableArea.class) { - RectangleF area = settings.get_DefaultPageSettings().get_PrintableArea(); - // get_PrintableArea is in 1/100 inch, see http://msdn.microsoft.com/de-de/library/system.drawing.printing.pagesettings.printablearea(v=VS.90).aspx - return new MediaPrintableArea(area.get_X()/100, area.get_Y()/100, area.get_Width()/100, area.get_Height()/100, MediaPrintableArea.INCH); - } else if (category == Destination.class) { - String path = "out.prn"; - try { - return new Destination( ( new File( path ) ).toURI() ); - } catch (SecurityException se) { - try { - return new Destination( new URI( "file:" + path) ); - } catch (URISyntaxException e) { - return null; - } - } - } else if (category == Sides.class) { - switch( settings.get_Duplex().Value ){ - case cli.System.Drawing.Printing.Duplex.Default: // MSDN: 'The printer's default duplex setting.' - what ever that might be - case cli.System.Drawing.Printing.Duplex.Simplex: - return Sides.ONE_SIDED; - case cli.System.Drawing.Printing.Duplex.Horizontal: - return Sides.TWO_SIDED_LONG_EDGE; - case cli.System.Drawing.Printing.Duplex.Vertical: - return Sides.TWO_SIDED_SHORT_EDGE; - } - } else if (category == PrinterResolution.class) { - cli.System.Drawing.Printing.PrinterResolution pRes = settings.get_DefaultPageSettings().get_PrinterResolution(); - int xRes = pRes.get_X(); - int yRes = pRes.get_Y(); - 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) { - if ( settings.get_SupportsColor() ) { - return ColorSupported.SUPPORTED; - } else { - return ColorSupported.NOT_SUPPORTED; - } - } else if( category == PrintQuality.class ){ - cli.System.Drawing.Printing.PrinterResolutionKind kind = settings.get_DefaultPageSettings().get_PrinterResolution().get_Kind(); - switch (kind.Value) { - case cli.System.Drawing.Printing.PrinterResolutionKind.High: - return PrintQuality.HIGH; - case cli.System.Drawing.Printing.PrinterResolutionKind.Medium: - case cli.System.Drawing.Printing.PrinterResolutionKind.Low: - return PrintQuality.NORMAL; - case cli.System.Drawing.Printing.PrinterResolutionKind.Draft: - return PrintQuality.DRAFT; - } - } else if (category == RequestingUserName.class) { - try{ - return new RequestingUserName( System.getProperty("user.name", ""), null); - } catch( SecurityException e ){ - return new RequestingUserName( "", null); - } - } else if (category == SheetCollate.class){ - return settings.get_Collate() ? SheetCollate.COLLATED : SheetCollate.UNCOLLATED; - } else if (category == Fidelity.class) { - return Fidelity.FIDELITY_FALSE; - } - return null; - } - - - @Override - public ServiceUIFactory getServiceUIFactory(){ - return null; - } - - - @Override - public Class<?>[] getSupportedAttributeCategories(){ - ArrayList<Class> categList = new ArrayList<Class>(otherAttrCats.length+3); - for (int i=0; i < otherAttrCats.length; i++) { - categList.add(otherAttrCats[i]); - } - - if (settings.get_CanDuplex()) { - categList.add(Sides.class); - } - - if (settings.get_PrinterResolutions().get_Count() > 0) { - categList.add(PrinterResolution.class); - } - - return categList.toArray(new Class[categList.size()]); - } - - - @Override - public Object getSupportedAttributeValues(Class<? extends Attribute> category, DocFlavor flavor, AttributeSet attributes){ - if ( category == null || !Attribute.class.isAssignableFrom( category ) ) { - throw new IllegalArgumentException( "The category '" + category + "' is not an Attribute" ); - } - if( !isAttributeCategorySupported(category) ){ - return null; - } - if (category == JobName.class || category == RequestingUserName.class || category == ColorSupported.class - || category == Destination.class ) { - return getDefaultAttributeValue(category); - } - if( category == Copies.class ){ - return new CopiesSupported(1, settings.get_MaximumCopies() ); - } - if( category == Media.class ){ - PaperSizeCollection sizes = settings.get_PaperSizes(); - List<Media> medias = new ArrayList<Media>(); - for( int i = 0; i < sizes.get_Count(); i++ ){ - PaperSize media = sizes.get_Item(i); - MediaSizeName mediaName = findMatchingMedia( sizes.get_Item(i) ); - if( mediaName != null - && !medias.contains( mediaName )){ // slow but better than creating a HashSet here - medias.add( mediaName); - } - } - // add media trays - MediaTray[] trays = getMediaTrays(); - for( MediaTray tray : trays ){ - medias.add( tray ); - } - return medias.size() > 0 ? medias.toArray( new Media[medias.size() ] ) : null; - } - 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; - } - } - if (category == Fidelity.class) { - return new Fidelity[]{ Fidelity.FIDELITY_FALSE, Fidelity.FIDELITY_TRUE}; - } - if (category == PrintQuality.class) { - return new PrintQuality[]{ PrintQuality.DRAFT, PrintQuality.HIGH, PrintQuality.NORMAL }; - } - - boolean printPageAble = flavor == null|| flavor.equals(DocFlavor.SERVICE_FORMATTED.PAGEABLE) - || flavor.equals(DocFlavor.SERVICE_FORMATTED.PRINTABLE); - if (category == Sides.class) { - if ( printPageAble ) { - return new Sides[]{ Sides.ONE_SIDED, Sides.TWO_SIDED_LONG_EDGE, Sides.TWO_SIDED_SHORT_EDGE}; - } else { - return null; - } - } - if (category == SheetCollate.class) { - if ( printPageAble ) { - return new SheetCollate[]{ SheetCollate.COLLATED, SheetCollate.UNCOLLATED} ; - } else { - return null; - } - } - boolean imageBased = printPageAble || flavor.equals(DocFlavor.INPUT_STREAM.GIF) - || flavor.equals(DocFlavor.INPUT_STREAM.JPEG) - || flavor.equals(DocFlavor.INPUT_STREAM.PNG) - || flavor.equals(DocFlavor.BYTE_ARRAY.GIF) - || flavor.equals(DocFlavor.BYTE_ARRAY.JPEG) - || flavor.equals(DocFlavor.BYTE_ARRAY.PNG) - || flavor.equals(DocFlavor.URL.GIF) - || flavor.equals(DocFlavor.URL.JPEG) - || flavor.equals(DocFlavor.URL.PNG); - if (category == OrientationRequested.class) { - if( imageBased ){ - return new OrientationRequested[]{ OrientationRequested.PORTRAIT, OrientationRequested.LANDSCAPE, OrientationRequested.REVERSE_LANDSCAPE }; - } else { - return null; - } - } - if (category == Chromaticity.class) { - if( imageBased ){ - if( settings.get_DefaultPageSettings().get_Color() ){ - return new Chromaticity[]{ Chromaticity.MONOCHROME, Chromaticity.COLOR }; - } else { - return new Chromaticity[]{ Chromaticity.MONOCHROME }; - } - } else { - return null; - } - } - return null; - } - - private MediaTray[] getMediaTrays(){ - if( mediaTrays != null ){ - // the print service is a singleton per printer so we only do this once - return mediaTrays; - } - PaperSourceCollection trays = settings.get_PaperSources(); - int count = trays.get_Count(); - List<MediaTray> trayList = new ArrayList<MediaTray>(); - for( int i=0; i < count; i++ ){ - PaperSource tray = trays.get_Item(i); - MediaTray javaTray = getDefaultTray(tray); - if( javaTray != null ){ - trayList.add( javaTray ); - } else { - trayList.add( new NetMediaTray( tray ) ); - } - } - mediaTrays = trayList.toArray( new MediaTray[trayList.size()]); - return mediaTrays; - } - - - @Override - public DocFlavor[] getSupportedDocFlavors(){ - int len = supportedFlavors.length; - DocFlavor[] result = new DocFlavor[len]; - System.arraycopy(supportedFlavors, 0, result, 0, len); - return result; - } - - - @Override - public AttributeSet getUnsupportedAttributes(DocFlavor flavor, AttributeSet attributes){ - // TODO Auto-generated method stub - return null; - } - - - @Override - public boolean isAttributeCategorySupported(Class<? extends Attribute> category){ - if ( category == null || !Attribute.class.isAssignableFrom( category ) ) { - throw new IllegalArgumentException( "The category '" + category + "' is not an Attribute" ); - } - Class<?>[] supported = getSupportedAttributeCategories(); - for( int i=0; i < supported.length; i++ ){ - if( category == supported[i] ){ - return true; - } - } - return false; - } - - - private boolean isPostScriptFlavor(DocFlavor flavor) { - if (flavor.equals(DocFlavor.BYTE_ARRAY.POSTSCRIPT) || - flavor.equals(DocFlavor.INPUT_STREAM.POSTSCRIPT) || - flavor.equals(DocFlavor.URL.POSTSCRIPT)) { - return true; - } - else { - return false; - } - } - - private boolean isPSDocAttr(Class category) { - if (category == OrientationRequested.class || category == Copies.class) { - return true; - } - else { - return false; - } - } - - private boolean isAutoSense(DocFlavor flavor) { - if (flavor.equals(DocFlavor.BYTE_ARRAY.AUTOSENSE) || - flavor.equals(DocFlavor.INPUT_STREAM.AUTOSENSE) || - flavor.equals(DocFlavor.URL.AUTOSENSE)) { - return true; - } - else { - return false; - } - } - - @Override - public boolean isAttributeValueSupported(Attribute attr, DocFlavor flavor, AttributeSet attributes){ - if (attr == null) { - throw new NullPointerException("null attribute"); - } - Class category = attr.getCategory(); - if (flavor != null) { - if (!isDocFlavorSupported(flavor)) { - throw new IllegalArgumentException(flavor + - " is an unsupported flavor"); - // if postscript & category is already specified within the PostScript data - // we return false - } else if (isAutoSense(flavor) || (isPostScriptFlavor(flavor) && - (isPSDocAttr(category)))) { - return false; - } - } - - if (!isAttributeCategorySupported(category)) { - return false; - } - else if (category == Chromaticity.class) { - if ((flavor == null) || - flavor.equals(DocFlavor.SERVICE_FORMATTED.PAGEABLE) || - flavor.equals(DocFlavor.SERVICE_FORMATTED.PRINTABLE) || - flavor.equals(DocFlavor.BYTE_ARRAY.GIF) || - flavor.equals(DocFlavor.INPUT_STREAM.GIF) || - flavor.equals(DocFlavor.URL.GIF) || - flavor.equals(DocFlavor.BYTE_ARRAY.JPEG) || - flavor.equals(DocFlavor.INPUT_STREAM.JPEG) || - flavor.equals(DocFlavor.URL.JPEG) || - flavor.equals(DocFlavor.BYTE_ARRAY.PNG) || - flavor.equals(DocFlavor.INPUT_STREAM.PNG) || - flavor.equals(DocFlavor.URL.PNG)) { - if (settings.get_DefaultPageSettings().get_Color()) { - return true; - } else { - return attr == Chromaticity.MONOCHROME; - } - } else { - return false; - } - } else if (category == Copies.class) { - return ((Copies)attr).getValue() >= 1 && ((Copies)attr).getValue() <= settings.get_MaximumCopies(); - - } else if (category == Destination.class) { - URI uri = ((Destination)attr).getURI(); - if ("file".equals(uri.getScheme()) && - !(uri.getSchemeSpecificPart().equals(""))) { - return true; - } else { - return false; - } - - } else if (category == Media.class) { - Media[] medias = (Media[])getSupportedAttributeValues( Media.class, flavor, attributes ); - if( medias != null ) { - return Arrays.asList( medias ).contains( attr ); - } - - } else if (category == MediaPrintableArea.class) { - //TODO - return true; - - } else if (category == SunAlternateMedia.class) { - Media media = ((SunAlternateMedia)attr).getMedia(); - return isAttributeValueSupported(media, flavor, attributes); - - } else if (category == PageRanges.class || - category == SheetCollate.class || - category == Sides.class) { - if (flavor != null && - !(flavor.equals(DocFlavor.SERVICE_FORMATTED.PAGEABLE) || - flavor.equals(DocFlavor.SERVICE_FORMATTED.PRINTABLE))) { - return false; - } - } else if (category == PrinterResolution.class) { - if (attr instanceof PrinterResolution) { - int[] jRes = ((PrinterResolution)attr).getResolution( PrinterResolution.DPI ); - PrinterResolutionCollection resolutions = settings.get_PrinterResolutions(); - for( int i=0; i< resolutions.get_Count(); i++ ) { - cli.System.Drawing.Printing.PrinterResolution nRes = resolutions.get_Item( i ); - if( nRes.get_X() == jRes[0] && nRes.get_Y() == jRes[1] ) { - return true; - } - } - return false; - } - } else if (category == OrientationRequested.class) { - if (attr == OrientationRequested.REVERSE_PORTRAIT || - (flavor != null) && - !(flavor.equals(DocFlavor.SERVICE_FORMATTED.PAGEABLE) || - flavor.equals(DocFlavor.SERVICE_FORMATTED.PRINTABLE) || - flavor.equals(DocFlavor.INPUT_STREAM.GIF) || - flavor.equals(DocFlavor.INPUT_STREAM.JPEG) || - flavor.equals(DocFlavor.INPUT_STREAM.PNG) || - flavor.equals(DocFlavor.BYTE_ARRAY.GIF) || - flavor.equals(DocFlavor.BYTE_ARRAY.JPEG) || - flavor.equals(DocFlavor.BYTE_ARRAY.PNG) || - flavor.equals(DocFlavor.URL.GIF) || - flavor.equals(DocFlavor.URL.JPEG) || - flavor.equals(DocFlavor.URL.PNG))) { - return false; - } - - } else if (category == ColorSupported.class) { - boolean isColorSup = settings.get_DefaultPageSettings().get_Color(); - if ((!isColorSup && (attr == ColorSupported.SUPPORTED)) || - (isColorSup && (attr == ColorSupported.NOT_SUPPORTED))) { - return false; - } - } - return true; - } - - - @Override - public boolean isDocFlavorSupported(DocFlavor flavor){ - for (int f=0; f<supportedFlavors.length; f++) { - if (flavor.equals(supportedFlavors[f])) { - return true; - } - } - return false; - } - - - @Override - public String toString(){ - return "Win32 Printer : " + getName(); - } - - - @Override - public boolean equals(Object obj){ - return (obj == this || (obj instanceof Win32PrintService && ((Win32PrintService)obj).getName() - .equals(getName()))); - } - - - @Override - public int hashCode(){ - return this.getClass().hashCode() + getName().hashCode(); - } - - /** - * Tries to find a matching {@link MediaSizeName} for a paper by it's size - * @param paper - * @return - */ - private MediaSizeName findMatchingMedia( PaperSize paper ){ - int rawKind = paper.get_RawKind(); - if( rawKind > 0 && rawKind <= MEDIA_NAMES.length ){ - // match to predefined size - MediaSizeName media = MEDIA_NAMES[ rawKind - 1 ]; - if( media != null ) { - return media; - } - } - int x = paper.get_Width() * INCH100_TO_MYM; - int y = paper.get_Height() * INCH100_TO_MYM; - if( x > y ){ // MediaSizes are always portrait! - int tmp = x; - x = y; - y = tmp; - } - for( MediaSizeName name : MEDIA_NAMES ){ - MediaSize media = MediaSize.getMediaSizeForName(name); - if( media != null ){ - if( Math.abs( x - media.getX(1) ) < MATCH_DIFF && Math.abs( y - media.getY(1) ) < MATCH_DIFF ){ - return name; - } - } - } - MediaSizeName media = CUSTOM_MEDIA_NAME.get(paper.get_PaperName()); - if (media == null) { - media = new CustomMediaSizeName(paper.get_PaperName()); - CUSTOM_MEDIA_NAME.put(paper.get_PaperName(), media); - new MediaSize( x, y, MediaSize.INCH/100, media); - } - return media; - } - - /** - * Returns the Java-default {@link MediaTray} for a paper source. This is required since these default - * trays are public constants which can be used without checking for the actually present media trays - * @param source the .NET paper source to get the predefined source for - * @return the media tray or null, in case there is no mapping for the paper source - */ - private MediaTray getDefaultTray( PaperSource source ){ - // convert from .NET kind to java's pre defined MediaTrays - switch( source.get_RawKind() ){ - case 1 : return MediaTray.TOP; - case 2 : return MediaTray.BOTTOM; - case 3 : return MediaTray.MIDDLE; - case 4 : return MediaTray.MANUAL; - case 5 : return MediaTray.ENVELOPE; - case 6 : return Win32MediaTray.ENVELOPE_MANUAL; - case 7 : return Win32MediaTray.AUTO; - case 8 : return Win32MediaTray.TRACTOR; - case 9 : return Win32MediaTray.SMALL_FORMAT; - case 10 : return Win32MediaTray.LARGE_FORMAT; - case 11 : return MediaTray.LARGE_CAPACITY; - case 14 : return MediaTray.MAIN; - case 15 : return Win32MediaTray.FORMSOURCE; - // FIXME which PaperSourceKind is MediaTray.SIDE ??? - } - return null; - } - - /** - * Returns the .NET {@link PaperSource} for a media tray. This will be done either by mapping or - * directly in case the tray is a {@link NetMediaTray} - * @param tray the tray to get the paper source for, must not be null - * @return the selected {@link PaperSource} or null, in case there is no matching {@link PaperSource} - */ - public PaperSource getPaperSourceForTray( MediaTray tray ){ - if( tray instanceof NetMediaTray ){ - return ((NetMediaTray)tray).getPaperSource( this ); - } - // try to find the appropriate paper source for the Java-Defined tray - PaperSourceCollection trays = settings.get_PaperSources(); - int count = trays.get_Count(); - for( int i=0; i < count; i++ ){ - PaperSource paperSource = trays.get_Item(i); - if( getDefaultTray( paperSource ) == tray ){ - return paperSource; - } - } - return null; - } - - public static class NetMediaTray extends MediaTray{ - - private static final long serialVersionUID = 1L; - - /** Not really used but required by the EnumSyntax super class */ - private static AtomicInteger idCounter = new AtomicInteger(8); - - private int rawKind; - private String name; - private transient PaperSource netSource; - - public NetMediaTray( PaperSource netSource ) { - super( idCounter.getAndIncrement() ); - this.rawKind = netSource.get_RawKind(); - this.name = netSource.get_SourceName(); - this.netSource = netSource; - } - - public PaperSource getPaperSource( Win32PrintService service ){ - if( netSource == null ){ - PaperSourceCollection sources = service.settings.get_PaperSources(); - int count = sources.get_Count(); - for( int i=0; i < count; i++ ){ - PaperSource source = sources.get_Item(i); - if( source.get_RawKind() == rawKind ){ - netSource = source; - break; - } - } - } - return netSource; - } - - @Override - public String toString() { - return netSource != null ? netSource.get_SourceName() : name; - } - } -} diff --git a/openjdk/sun/print/Win32PrintServiceLookup.java b/openjdk/sun/print/Win32PrintServiceLookup.java deleted file mode 100644 index f230c852..00000000 --- a/openjdk/sun/print/Win32PrintServiceLookup.java +++ /dev/null @@ -1,264 +0,0 @@ -/* - Copyright (C) 2009 Volker Berlin (i-net software) - - This software is provided 'as-is', without any express or implied - warranty. In no event will the authors be held liable for any damages - arising from the use of this software. - - Permission is granted to anyone to use this software for any purpose, - including commercial applications, and to alter it and redistribute it - freely, subject to the following restrictions: - - 1. The origin of this software must not be misrepresented; you must not - claim that you wrote the original software. If you use this software - in a product, an acknowledgment in the product documentation would be - appreciated but is not required. - 2. Altered source versions must be plainly marked as such, and must not be - misrepresented as being the original software. - 3. This notice may not be removed or altered from any source distribution. - - Jeroen Frijters - jeroen@frijters.net - - */ -package sun.print; - -import ikvm.awt.IkvmToolkit; - -import java.awt.Toolkit; - -import java.io.BufferedReader; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.IOException; -import java.util.ArrayList; -import java.security.AccessController; -import java.security.PrivilegedActionException; -import java.security.PrivilegedExceptionAction; -import javax.print.DocFlavor; -import javax.print.MultiDocPrintService; -import javax.print.PrintService; -import javax.print.PrintServiceLookup; -import javax.print.attribute.Attribute; -import javax.print.attribute.AttributeSet; -import javax.print.attribute.HashPrintRequestAttributeSet; -import javax.print.attribute.HashPrintServiceAttributeSet; -import javax.print.attribute.PrintRequestAttribute; -import javax.print.attribute.PrintRequestAttributeSet; -import javax.print.attribute.PrintServiceAttribute; -import javax.print.attribute.PrintServiceAttributeSet; -import javax.print.attribute.standard.PrinterName; - -public class Win32PrintServiceLookup extends PrintServiceLookup { - - private final PrintPeer peer = IkvmToolkit.DefaultToolkit.get().getPrintPeer(); - - private String defaultPrinter; - private PrintService defaultPrintService; - private String[] printers; /* excludes the default printer */ - private PrintService[] printServices; /* includes the default printer */ - - - /* Want the PrintService which is default print service to have - * equality of reference with the equivalent in list of print services - * This isn't required by the API and there's a risk doing this will - * lead people to assume its guaranteed. - */ - public synchronized PrintService[] getPrintServices() { - SecurityManager security = System.getSecurityManager(); - if (security != null) { - security.checkPrintJobAccess(); - } - if (printServices == null) { - refreshServices(); - } - return printServices; - } - - private synchronized void refreshServices() { - printers = peer.getAllPrinterNames(); - if (printers == null) { - // In Windows it is safe to assume no default if printers == null so we - // don't get the default. - printServices = new PrintService[0]; - return; - } - - PrintService[] newServices = new PrintService[printers.length]; - PrintService defService = getDefaultPrintService(); - for (int p = 0; p < printers.length; p++) { - if (defService != null && - printers[p].equals(defService.getName())) { - newServices[p] = defService; - } else { - if (printServices == null) { - newServices[p] = new Win32PrintService(printers[p], peer); - } else { - int j; - for (j = 0; j < printServices.length; j++) { - if ((printServices[j]!= null) && - (printers[p].equals(printServices[j].getName()))) { - newServices[p] = printServices[j]; - printServices[j] = null; - break; - } - } - if (j == printServices.length) { - newServices[p] = new Win32PrintService(printers[p], peer); - } - } - } - } - - printServices = newServices; - } - - - public synchronized PrintService getPrintServiceByName(String name) { - - if (name == null || name.equals("")) { - return null; - } else { - /* getPrintServices() is now very fast. */ - PrintService[] printServices = getPrintServices(); - for (int i=0; i<printServices.length; i++) { - if (printServices[i].getName().equals(name)) { - return printServices[i]; - } - } - return null; - } - } - - boolean matchingService(PrintService service, - PrintServiceAttributeSet serviceSet) { - if (serviceSet != null) { - Attribute [] attrs = serviceSet.toArray(); - Attribute serviceAttr; - for (int i=0; i<attrs.length; i++) { - serviceAttr - = service.getAttribute((Class<PrintServiceAttribute>)attrs[i].getCategory()); - if (serviceAttr == null || !serviceAttr.equals(attrs[i])) { - return false; - } - } - } - return true; - } - - public PrintService[] getPrintServices(DocFlavor flavor, - AttributeSet attributes) { - - SecurityManager security = System.getSecurityManager(); - if (security != null) { - security.checkPrintJobAccess(); - } - PrintRequestAttributeSet requestSet = null; - PrintServiceAttributeSet serviceSet = null; - - if (attributes != null && !attributes.isEmpty()) { - - requestSet = new HashPrintRequestAttributeSet(); - serviceSet = new HashPrintServiceAttributeSet(); - - Attribute[] attrs = attributes.toArray(); - for (int i=0; i<attrs.length; i++) { - if (attrs[i] instanceof PrintRequestAttribute) { - requestSet.add(attrs[i]); - } else if (attrs[i] instanceof PrintServiceAttribute) { - serviceSet.add(attrs[i]); - } - } - } - - /* - * Special case: If client is asking for a particular printer - * (by name) then we can save time by getting just that service - * to check against the rest of the specified attributes. - */ - PrintService[] services = null; - if (serviceSet != null && serviceSet.get(PrinterName.class) != null) { - PrinterName name = (PrinterName)serviceSet.get(PrinterName.class); - PrintService service = getPrintServiceByName(name.getValue()); - if (service == null || !matchingService(service, serviceSet)) { - services = new PrintService[0]; - } else { - services = new PrintService[1]; - services[0] = service; - } - } else { - services = getPrintServices(); - } - - if (services.length == 0) { - return services; - } else { - ArrayList matchingServices = new ArrayList(); - for (int i=0; i<services.length; i++) { - try { - if (services[i]. - getUnsupportedAttributes(flavor, requestSet) == null) { - matchingServices.add(services[i]); - } - } catch (IllegalArgumentException e) { - } - } - services = new PrintService[matchingServices.size()]; - return (PrintService[])matchingServices.toArray(services); - } - } - - /* - * return empty array as don't support multi docs - */ - public MultiDocPrintService[] - getMultiDocPrintServices(DocFlavor[] flavors, - AttributeSet attributes) { - SecurityManager security = System.getSecurityManager(); - if (security != null) { - security.checkPrintJobAccess(); - } - return new MultiDocPrintService[0]; - } - - - public synchronized PrintService getDefaultPrintService() { - SecurityManager security = System.getSecurityManager(); - if (security != null) { - security.checkPrintJobAccess(); - } - - - // Windows does not have notification for a change in default - // so we always get the latest. - defaultPrinter = peer.getDefaultPrinterName(); - if (defaultPrinter == null) { - return null; - } - - if ((defaultPrintService != null) && - defaultPrintService.getName().equals(defaultPrinter)) { - - return defaultPrintService; - } - - // Not the same as default so proceed to get new PrintService. - - // clear defaultPrintService - defaultPrintService = null; - - if (printServices != null) { - for (int j=0; j<printServices.length; j++) { - if (defaultPrinter.equals(printServices[j].getName())) { - defaultPrintService = printServices[j]; - break; - } - } - } - - if (defaultPrintService == null) { - defaultPrintService = new Win32PrintService(defaultPrinter, peer); - } - return defaultPrintService; - } -} |