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