Path: blob/master/src/java.desktop/share/classes/javax/print/PrintService.java
41153 views
/*1* Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.print;2627import javax.print.attribute.Attribute;28import javax.print.attribute.AttributeSet;29import javax.print.attribute.PrintServiceAttribute;30import javax.print.attribute.PrintServiceAttributeSet;31import javax.print.event.PrintServiceAttributeListener;3233/**34* Interface {@code PrintService} is the factory for a {@code DocPrintJob}. A35* {@code PrintService} describes the capabilities of a printer and can be36* queried regarding a printer's supported attributes.37* <p>38* Example:39* <pre>{@code40* DocFlavor flavor = DocFlavor.INPUT_STREAM.POSTSCRIPT;41* PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();42* aset.add(MediaSizeName.ISO_A4);43* PrintService[] pservices =44* PrintServiceLookup.lookupPrintServices(flavor, aset);45* if (pservices.length > 0) {46* DocPrintJob pj = pservices[0].createPrintJob();47* try {48* FileInputStream fis = new FileInputStream("test.ps");49* Doc doc = new SimpleDoc(fis, flavor, null);50* pj.print(doc, aset);51* } catch (FileNotFoundException fe) {52* } catch (PrintException e) {53* }54* }55* }</pre>56*/57public interface PrintService {5859/**60* Returns a string name for this print service which may be used by61* applications to request a particular print service. In a suitable62* context, such as a name service, this name must be unique. In some63* environments this unique name may be the same as the user friendly64* printer name defined as the65* {@link javax.print.attribute.standard.PrinterName PrinterName} attribute.66*67* @return name of the service68*/69public String getName();7071/**72* Creates and returns a {@code PrintJob} capable of handling data from any73* of the supported document flavors.74*75* @return a {@code DocPrintJob} object76*/77public DocPrintJob createPrintJob();7879/**80* Registers a listener for events on this {@code PrintService}.81*82* @param listener a PrintServiceAttributeListener, which monitors the83* status of a print service84* @see #removePrintServiceAttributeListener85*/86public void addPrintServiceAttributeListener(87PrintServiceAttributeListener listener);8889/**90* Removes the print-service listener from this print service. This means91* the listener is no longer interested in {@code PrintService} events.92*93* @param listener a {@code PrintServiceAttributeListener} object94* @see #addPrintServiceAttributeListener95*/96public void removePrintServiceAttributeListener(97PrintServiceAttributeListener listener);9899/**100* Obtains this print service's set of printer description attributes giving101* this Print Service's status. The returned attribute set object is102* unmodifiable. The returned attribute set object is a "snapshot" of this103* Print Service's attribute set at the time of the {@code getAttributes()}104* method call: that is, the returned attribute set's contents will105* <i>not</i> be updated if this print service's attribute set's contents106* change in the future. To detect changes in attribute values, call107* {@code getAttributes()} again and compare the new attribute set to the108* previous attribute set; alternatively, register a listener for print109* service events.110*111* @return unmodifiable snapshot of this Print Service's attribute set. May112* be empty, but not {@code null}.113*/114public PrintServiceAttributeSet getAttributes();115116/**117* Gets the value of the single specified service attribute. This may be118* useful to clients which only need the value of one attribute and want to119* minimize overhead.120*121* @param <T> the type of the specified service attribute122* @param category the category of a {@code PrintServiceAttribute}123* supported by this service - may not be {@code null}124* @return the value of the supported attribute or {@code null} if the125* attribute is not supported by this service126* @throws NullPointerException if the category is {@code null}127* @throws IllegalArgumentException if {@code category} is not a128* {@code Class} that implements interface129* {@link PrintServiceAttribute PrintServiceAttribute}130*/131public <T extends PrintServiceAttribute>132T getAttribute(Class<T> category);133134/**135* Determines the print data formats a client can specify when setting up a136* job for this {@code PrintService}. A print data format is designated by a137* "doc flavor" (class {@link DocFlavor DocFlavor}) consisting of a MIME138* type plus a print data representation class.139* <p>140* Note that some doc flavors may not be supported in combination with all141* attributes. Use {@code getUnsupportedAttributes(..)} to validate specific142* combinations.143*144* @return array of supported doc flavors, should have at least one element145*/146public DocFlavor[] getSupportedDocFlavors();147148/**149* Determines if this print service supports a specific {@code DocFlavor}.150* This is a convenience method to determine if the {@code DocFlavor} would151* be a member of the result of {@code getSupportedDocFlavors()}.152* <p>153* Note that some doc flavors may not be supported in combination with all154* attributes. Use {@code getUnsupportedAttributes(..)} to validate specific155* combinations.156*157* @param flavor the {@code DocFlavor} to query for support158* @return {@code true} if this print service supports the specified159* {@code DocFlavor}; {@code false} otherwise160* @throws NullPointerException if {@code flavor} is {@code null}161*/162public boolean isDocFlavorSupported(DocFlavor flavor);163164/**165* Determines the printing attribute categories a client can specify when166* setting up a job for this print service. A printing attribute category is167* designated by a {@code Class} that implements interface168* {@link Attribute Attribute}. This method returns just the attribute169* <i>categories</i> that are supported; it does not return the particular170* attribute <i>values</i> that are supported.171* <p>172* This method returns all the printing attribute categories this print173* service supports for any possible job. Some categories may not be174* supported in a particular context (ie for a particular175* {@code DocFlavor}). Use one of the methods that include a176* {@code DocFlavor} to validate the request before submitting it, such as177* {@code getSupportedAttributeValues(..)}.178*179* @return array of printing attribute categories that the client can180* specify as a doc-level or job-level attribute in a Print Request.181* Each element in the array is a {@link Class Class} that182* implements interface {@link Attribute Attribute}. The array is183* empty if no categories are supported.184*/185public Class<?>[] getSupportedAttributeCategories();186187/**188* Determines whether a client can specify the given printing attribute189* category when setting up a job for this print service. A printing190* attribute category is designated by a {@code Class} that implements191* interface {@link Attribute Attribute}. This method192* tells whether the attribute <i>category</i> is supported; it does not193* tell whether a particular attribute <i>value</i> is supported.194* <p>195* Some categories may not be supported in a particular context (ie for a196* particular {@code DocFlavor}). Use one of the methods which include a197* {@code DocFlavor} to validate the request before submitting it, such as198* {@code getSupportedAttributeValues(..)}.199* <p>200* This is a convenience method to determine if the category would be a201* member of the result of {@code getSupportedAttributeCategories()}.202*203* @param category printing attribute category to test. It must be a204* {@code Class} that implements interface205* {@link Attribute Attribute}.206* @return {@code true} if this print service supports specifying a207* doc-level or job-level attribute in {@code category} in a Print208* Request; {@code false} if it doesn't209* @throws NullPointerException if {@code category} is {@code null}210* @throws IllegalArgumentException if {@code category} is not a211* {@code Class} that implements interface212* {@link Attribute Attribute}213*/214public boolean215isAttributeCategorySupported(Class<? extends Attribute> category);216217/**218* Determines this print service's default printing attribute value in the219* given category. A printing attribute value is an instance of a class that220* implements interface {@link Attribute Attribute}. If a client sets up a221* print job and does not specify any attribute value in the given category,222* this Print Service will use the default attribute value instead.223* <p>224* Some attributes may not be supported in a particular context (ie for a225* particular {@code DocFlavor}). Use one of the methods that include a226* {@code DocFlavor} to validate the request before submitting it, such as227* {@code getSupportedAttributeValues(..)}.228* <p>229* Not all attributes have a default value. For example the service will not230* have a default value for {@code RequestingUser} i.e. a {@code null}231* return for a supported category means there is no service default value232* for that category. Use the {@code isAttributeCategorySupported(Class)}233* method to distinguish these cases.234*235* @param category printing attribute category for which the default236* attribute value is requested. It must be a {@link Class Class}237* that implements interface {@link Attribute Attribute}.238* @return default attribute value for {@code category}, or {@code null} if239* this Print Service does not support specifying a doc-level or240* job-level attribute in {@code category} in a Print Request, or241* the service does not have a default value for this attribute242* @throws NullPointerException if {@code category} is {@code null}243* @throws IllegalArgumentException if {@code category} is not a244* {@link Class Class} that implements interface245* {@link Attribute Attribute}246*/247public Object248getDefaultAttributeValue(Class<? extends Attribute> category);249250/**251* Determines the printing attribute values a client can specify in the252* given category when setting up a job for this print service. A printing253* attribute value is an instance of a class that implements interface254* {@link Attribute Attribute}.255* <p>256* If {@code flavor} is {@code null} and {@code attributes} is {@code null}257* or is an empty set, this method returns all the printing attribute values258* this Print Service supports for any possible job. If {@code flavor} is not259* {@code null} or {@code attributes} is not an empty set, this method260* returns just the printing attribute values that are compatible with the261* given doc flavor and/or set of attributes. That is, a {@code null} return262* value may indicate that specifying this attribute is incompatible with263* the specified DocFlavor. Also if {@code DocFlavor} is not {@code null} it264* must be a flavor supported by this {@code PrintService}, else265* {@code IllegalArgumentException} will be thrown.266* <p>267* If the {@code attributes} parameter contains an {@code Attribute} whose268* category is the same as the {@code category} parameter, the service must269* ignore this attribute in the {@code AttributeSet}.270* <p>271* {@code DocAttribute}s which are to be specified on the {@code Doc} must272* be included in this set to accurately represent the context.273* <p>274* This method returns an {@code Object} because different printing275* attribute categories indicate the supported attribute values in different276* ways. The documentation for each printing attribute in package277* {@link javax.print.attribute.standard javax.print.attribute.standard}278* describes how each attribute indicates its supported values. Possible279* ways of indicating support include:280* <ul>281* <li>Return a single instance of the attribute category to indicate that282* any value is legal -- used, for example, by an attribute whose value is283* an arbitrary text string. (The value of the returned attribute object284* is irrelevant.)285* <li>Return an array of one or more instances of the attribute category,286* containing the legal values -- used, for example, by an attribute with287* a list of enumerated values. The type of the array is an array of the288* specified attribute category type as returned by its289* {@code getCategory(Class)}.290* <li>Return a single object (of some class other than the attribute291* category) that indicates bounds on the legal values -- used, for292* example, by an integer-valued attribute that must lie within a certain293* range.294* </ul>295*296* @param category printing attribute category to test. It must be a297* {@link Class Class} that implements interface298* {@link Attribute Attribute}.299* @param flavor doc flavor for a supposed job, or {@code null}300* @param attributes set of printing attributes for a supposed job (both301* job-level attributes and document-level attributes), or302* {@code null}303* @return object indicating supported values for {@code category}, or304* {@code null} if this Print Service does not support specifying a305* doc-level or job-level attribute in {@code category} in a Print306* Request307* @throws NullPointerException if {@code category} is {@code null}308* @throws IllegalArgumentException if {@code category} is not a309* {@link Class Class} that implements interface310* {@link Attribute Attribute}, or {@code DocFlavor} is not311* supported by this service312*/313public Object314getSupportedAttributeValues(Class<? extends Attribute> category,315DocFlavor flavor,316AttributeSet attributes);317318/**319* Determines whether a client can specify the given printing attribute320* value when setting up a job for this Print Service. A printing attribute321* value is an instance of a class that implements interface322* {@link Attribute Attribute}.323* <p>324* If {@code flavor} is {@code null} and {@code attributes} is {@code null}325* or is an empty set, this method tells whether this Print Service supports326* the given printing attribute value for some possible combination of doc327* flavor and set of attributes. If {@code flavor} is not {@code null} or328* {@code attributes} is not an empty set, this method tells whether this329* Print Service supports the given printing attribute value in combination330* with the given doc flavor and/or set of attributes.331* <p>332* Also if {@code DocFlavor} is not {@code null} it must be a flavor333* supported by this {@code PrintService}, else334* {@code IllegalArgumentException} will be thrown.335* <p>336* {@code DocAttribute}s which are to be specified on the {@code Doc} must337* be included in this set to accurately represent the context.338* <p>339* This is a convenience method to determine if the value would be a member340* of the result of {@code getSupportedAttributeValues(...)}.341*342* @param attrval printing attribute value to test343* @param flavor doc flavor for a supposed job, or {@code null}344* @param attributes set of printing attributes for a supposed job (both345* job-level attributes and document-level attributes), or346* {@code null}347* @return {@code true} if this Print Service supports specifying348* {@code attrval} as a doc-level or job-level attribute in a Print349* Request, {@code false} if it doesn't350* @throws NullPointerException if {@code attrval} is {@code null}351* @throws IllegalArgumentException if flavor is not supported by this352* {@code PrintService}353*/354public boolean isAttributeValueSupported(Attribute attrval,355DocFlavor flavor,356AttributeSet attributes);357358/**359* Identifies the attributes that are unsupported for a print request in the360* context of a particular {@code DocFlavor}. This method is useful for361* validating a potential print job and identifying the specific attributes362* which cannot be supported. It is important to supply only a supported363* {@code DocFlavor} or an {@code IllegalArgumentException} will be thrown.364* If the return value from this method is {@code null}, all attributes are365* supported.366* <p>367* {@code DocAttribute}s which are to be specified on the {@code Doc} must368* be included in this set to accurately represent the context.369* <p>370* If the return value is {@code non-null}, all attributes in the returned371* set are unsupported with this {@code DocFlavor}. The returned set does372* not distinguish attribute categories that are unsupported from373* unsupported attribute values.374* <p>375* A supported print request can then be created by removing all unsupported376* attributes from the original attribute set, except in the case that the377* {@code DocFlavor} is unsupported.378* <p>379* If any attributes are unsupported only because they are in conflict with380* other attributes then it is at the discretion of the service to select381* the attribute(s) to be identified as the cause of the conflict.382* <p>383* Use {@code isDocFlavorSupported()} to verify that a {@code DocFlavor} is384* supported before calling this method.385*386* @param flavor doc flavor to test, or {@code null}387* @param attributes set of printing attributes for a supposed job (both388* job-level attributes and document-level attributes), or389* {@code null}390* @return {@code null} if this Print Service supports the print request391* specification, else the unsupported attributes392* @throws IllegalArgumentException if {@code flavor} is not supported by393* this {@code PrintService}394*/395public AttributeSet getUnsupportedAttributes(DocFlavor flavor,396AttributeSet attributes);397398/**399* Returns a factory for UI components which allow users to interact with400* the service in various roles. Services which do not provide any UI should401* return {@code null}. Print Services which do provide UI but want to be402* supported in an environment with no UI support should ensure that the403* factory is not initialised unless the application calls this method to404* obtain the factory. See {@code ServiceUIFactory} for more information.405*406* @return {@code null} or a factory for UI components407*/408public ServiceUIFactory getServiceUIFactory();409410/**411* Determines if two services are referring to the same underlying service.412* Objects encapsulating a print service may not exhibit equality of413* reference even though they refer to the same underlying service.414* <p>415* Clients should call this method to determine if two services are416* referring to the same underlying service.417* <p>418* Services must implement this method and return {@code true} only if the419* service objects being compared may be used interchangeably by the client.420* Services are free to return the same object reference to an underlying421* service if that, but clients must not depend on equality of reference.422*423* @param obj the reference object with which to compare424* @return {@code true} if this service is the same as the obj argument,425* {@code false} otherwise426*/427public boolean equals(Object obj);428429/**430* This method should be implemented consistently with431* {@code equals(Object)}.432*433* @return hash code of this object434*/435public int hashCode();436}437438439