PrintServiceStub.java revision 8729:0242fce0f717
1/*
2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24import java.util.HashMap;
25import java.util.HashSet;
26import java.util.Map;
27import java.util.Set;
28
29import javax.print.DocFlavor;
30import javax.print.DocPrintJob;
31import javax.print.PrintService;
32import javax.print.ServiceUIFactory;
33import javax.print.attribute.Attribute;
34import javax.print.attribute.AttributeSet;
35import javax.print.attribute.HashPrintServiceAttributeSet;
36import javax.print.attribute.PrintServiceAttribute;
37import javax.print.attribute.PrintServiceAttributeSet;
38import javax.print.attribute.standard.Media;
39import javax.print.attribute.standard.MediaSizeName;
40import javax.print.attribute.standard.PrinterInfo;
41import javax.print.attribute.standard.PrinterIsAcceptingJobs;
42import javax.print.attribute.standard.PrinterMakeAndModel;
43import javax.print.attribute.standard.PrinterName;
44import javax.print.attribute.standard.PrinterState;
45import javax.print.event.PrintServiceAttributeListener;
46
47/**
48 * Stub implementation of a custom {@link PrintService}.
49 *
50 * @author reinhapa
51 */
52public class PrintServiceStub implements PrintService {
53    private final String _name;
54    private final Set<DocFlavor> _flavors;
55    private final Map<Class<?>, Object> _attributes;
56
57    public PrintServiceStub(String name) {
58        _name = name;
59        _flavors = new HashSet<DocFlavor>();
60        _flavors.add(DocFlavor.SERVICE_FORMATTED.PAGEABLE);
61        _flavors.add(DocFlavor.SERVICE_FORMATTED.PRINTABLE);
62        _attributes = new HashMap<>();
63        _attributes.put(PrinterName.class, new PrinterName(name, null));
64        _attributes.put(PrinterState.class, PrinterState.IDLE);
65        _attributes.put(PrinterInfo.class, new PrinterInfo("Custom location",
66                null));
67        _attributes.put(PrinterIsAcceptingJobs.class,
68                PrinterIsAcceptingJobs.ACCEPTING_JOBS);
69        _attributes.put(PrinterMakeAndModel.class, new PrinterMakeAndModel(
70                "Custom printer", null));
71        _attributes.put(Media.class, new Media[] { MediaSizeName.ISO_A4 });
72    }
73
74    @Override
75    public String getName() {
76        return _name;
77    }
78
79    @Override
80    public boolean isDocFlavorSupported(DocFlavor flavor) {
81        return _flavors.contains(flavor);
82    }
83
84    @Override
85    public Object getSupportedAttributeValues(
86            Class<? extends Attribute> category, DocFlavor flavor,
87            AttributeSet attributes) {
88        return _attributes.get(category);
89    }
90
91    @Override
92    public boolean isAttributeCategorySupported(
93            Class<? extends Attribute> category) {
94        return _attributes.containsKey(category);
95    }
96
97    @Override
98    public <T extends PrintServiceAttribute> T getAttribute(Class<T> category) {
99        return category.cast(_attributes.get(category));
100    }
101
102    @Override
103    public PrintServiceAttributeSet getAttributes() {
104        return new HashPrintServiceAttributeSet(_attributes.values().toArray(
105                new PrintServiceAttribute[_attributes.size()]));
106    }
107
108    @Override
109    public DocFlavor[] getSupportedDocFlavors() {
110        return _flavors.toArray(new DocFlavor[_flavors.size()]);
111    }
112
113    // not implemented methods
114
115    @Override
116    public DocPrintJob createPrintJob() {
117        return null;
118    }
119
120    @Override
121    public void addPrintServiceAttributeListener(
122            PrintServiceAttributeListener listener) {
123
124    }
125
126    @Override
127    public void removePrintServiceAttributeListener(
128            PrintServiceAttributeListener listener) {
129
130    }
131
132    @Override
133    public Class<?>[] getSupportedAttributeCategories() {
134        return null;
135    }
136
137    @Override
138    public Object getDefaultAttributeValue(Class<? extends Attribute> category) {
139        return null;
140    }
141
142    @Override
143    public boolean isAttributeValueSupported(Attribute attrval,
144            DocFlavor flavor, AttributeSet attributes) {
145        return false;
146    }
147
148    @Override
149    public AttributeSet getUnsupportedAttributes(DocFlavor flavor,
150            AttributeSet attributes) {
151        return null;
152    }
153
154    @Override
155    public ServiceUIFactory getServiceUIFactory() {
156        return null;
157    }
158}
159