diff options
author | smallsql <smallsql> | 2009-11-01 15:03:01 +0300 |
---|---|---|
committer | smallsql <smallsql> | 2009-11-01 15:03:01 +0300 |
commit | f65cead627f2afb37b256af177bec08c40023865 (patch) | |
tree | dadfbc9538238b32c59355413b6acc1e9a4f3bcc /openjdk/sun/print/Win32PrintService.java | |
parent | 8877fb077d825bc0f09b4c7966975299b73faeee (diff) |
Printing API next step
Diffstat (limited to 'openjdk/sun/print/Win32PrintService.java')
-rw-r--r-- | openjdk/sun/print/Win32PrintService.java | 223 |
1 files changed, 199 insertions, 24 deletions
diff --git a/openjdk/sun/print/Win32PrintService.java b/openjdk/sun/print/Win32PrintService.java index 2bd1c931..d028d4b0 100644 --- a/openjdk/sun/print/Win32PrintService.java +++ b/openjdk/sun/print/Win32PrintService.java @@ -23,53 +23,206 @@ */ package sun.print; +import java.util.ArrayList; + 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.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.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.Drawing.Printing.PrinterSettings; /** * @author Volker Berlin */ public class Win32PrintService implements PrintService{ - public Win32PrintService(String string){ - // TODO Auto-generated constructor stub + private static final DocFlavor[] supportedFlavors = { + DocFlavor.SERVICE_FORMATTED.PAGEABLE, + DocFlavor.SERVICE_FORMATTED.PRINTABLE, + }; + + /* 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 + }; + + private final PrintPeer peer; + + private final String printer; + private final PrinterSettings settings; + private PrinterName name; + + 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 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){ - // TODO Auto-generated method stub + 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(){ - // TODO Auto-generated method stub - return null; + SecurityManager security = System.getSecurityManager(); + if(security != null){ + security.checkPrintJobAccess(); + } + return new Win32PrintJob(this, peer); } @Override public <T extends PrintServiceAttribute>T getAttribute(Class<T> category){ - // TODO Auto-generated method stub - return null; + if(category == null){ + throw new NullPointerException("category"); + } + if(!(PrintServiceAttribute.class.isAssignableFrom(category))){ + throw new IllegalArgumentException("Not a PrintServiceAttribute"); + } + if(category == ColorSupported.class){ + if(settings.get_SupportsColor()){ + return (T)ColorSupported.SUPPORTED; + }else{ + return (T)ColorSupported.NOT_SUPPORTED; + } + }else if(category == PrinterName.class){ + return (T)getPrinterName(); + }else if(category == PrinterState.class){ + return (T)PrinterState.UNKNOWN; // TODO + }else if(category == PrinterStateReasons.class){ + return null; // TODO + }else{ + // QueuedJobCount and PrinterIsAcceptingJobs + return (T)peer.getPrinterStatus(printer, category); + } } @Override public PrintServiceAttributeSet getAttributes(){ - // TODO Auto-generated method stub - return null; + 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); + } + if(settings.get_SupportsColor()){ + attrs.add(ColorSupported.SUPPORTED); + }else{ + attrs.add(ColorSupported.NOT_SUPPORTED); + } + + return AttributeSetUtilities.unmodifiableView(attrs); } @@ -81,13 +234,6 @@ public class Win32PrintService implements PrintService{ @Override - public String getName(){ - // TODO Auto-generated method stub - return null; - } - - - @Override public ServiceUIFactory getServiceUIFactory(){ // TODO Auto-generated method stub return null; @@ -96,8 +242,20 @@ public class Win32PrintService implements PrintService{ @Override public Class<?>[] getSupportedAttributeCategories(){ - // TODO Auto-generated method stub - return null; + 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()]); } @@ -111,8 +269,10 @@ public class Win32PrintService implements PrintService{ @Override public DocFlavor[] getSupportedDocFlavors(){ - // TODO Auto-generated method stub - return null; + int len = supportedFlavors.length; + DocFlavor[] result = new DocFlavor[len]; + System.arraycopy(supportedFlavors, 0, result, 0, len); + return result; } @@ -139,15 +299,30 @@ public class Win32PrintService implements PrintService{ @Override public boolean isDocFlavorSupported(DocFlavor flavor){ - // TODO Auto-generated method stub + for (int f=0; f<supportedFlavors.length; f++) { + if (flavor.equals(supportedFlavors[f])) { + return true; + } + } return false; } @Override - public void removePrintServiceAttributeListener(PrintServiceAttributeListener listener){ - // TODO Auto-generated method stub + 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(); + } } |