1 /*
2  * Copyright (c) 2004, 2011, 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 
24 
25 /**
26  * @test
27  * @bug 4910388 4871089 4998624
28  * @summary Confirm that
29  *      1. After choosing Reverse Landscape in the system default print
30  *       Print Service (2nd in the list), it
31  *          will reset to portrait in "Test Printer"
32  *      2. Print To File button is not cleared when switching between the
33  *         2nd service (system default printer) and Test Printer.
34  *      3. Make sure "Postscript" printer is the default and make sure the
35  *         "print to file" button is disabled.  File Dialog should not be
36  *         shown after pressing print button.
37  *
38  * @run main/manual ServiceDialogTest
39  */
40 import java.awt.*;
41 import javax.print.*;
42 import javax.print.attribute.standard.*;
43 import javax.print.attribute.*;
44 import javax.print.event.*;
45 import java.io.*;
46 import java.util.Locale;
47 
48 public class ServiceDialogTest {
49         /**
50          * Constructor
51          */
ServiceDialogTest()52          public ServiceDialogTest() {
53                 super();
54         }
55         /**
56          * Starts the application.
57          */
main(java.lang.String[] args)58         public static void main(java.lang.String[] args) {
59                 ServiceDialogTest pd = new ServiceDialogTest();
60                 PrintService services[] = new PrintService[3];
61                 services[1] = PrintServiceLookup.lookupDefaultPrintService();
62 
63                 FileOutputStream fos = null;
64                 File f = null;
65                 String mType = "application/postscript";
66                 DocFlavor flavor = DocFlavor.INPUT_STREAM.JPEG;
67                 try {
68                         f = new File("streamexample.ps");
69                         fos = new FileOutputStream(f);
70                         StreamPrintServiceFactory[] factories = StreamPrintServiceFactory.lookupStreamPrintServiceFactories(flavor, mType);
71                         if (factories.length > 0) {
72                                 services[0] = factories[0].getPrintService(fos);
73                         } else {
74                                 throw new RuntimeException("No StreamPrintService available which would support "+flavor);
75                         }
76 
77                         services[2] = new TestPrintService("Test Printer");
78 
79             //System.out.println("is "+flavor+" supported? "+services[0].isDocFlavorSupported(flavor));
80             //System.out.println("is Orientation supported? "+services[0].isAttributeCategorySupported(OrientationRequested.class));
81             //System.out.println("is REVERSE PORTRAIT supported ? "+services[0].isAttributeValueSupported(OrientationRequested.REVERSE_PORTRAIT, flavor, null));
82 
83             HashPrintRequestAttributeSet prSet = new HashPrintRequestAttributeSet();
84             prSet.add(new Destination(new File("./dest.prn").toURI()));
85             PrintService selService = ServiceUI.printDialog(null, 200, 200, services, services[0], flavor, prSet);
86                         Attribute attr[] = prSet.toArray();
87                         for (int x = 0; x < attr.length; x ++) {
88                                 System.out.println(attr[x]);
89                         }
90 
91                         //DocPrintJob pj = service.createPrintJob();
92                         //PrintDocument prDoc = new PrintDocument();
93                         //pj.print(prDoc, null);
94 
95                 } catch (Exception e) {
96                         e.printStackTrace();
97                 }
98         }
99 }
100 
101 
102 class TestPrintService implements PrintService
103 {
104 
105     private static DocFlavor textByteFlavor = null;
106     private static final DocFlavor supportedDocFlavors[] = (new DocFlavor[] {
107              javax.print.DocFlavor.INPUT_STREAM.JPEG
108     });
109 
110     private static final Class serviceAttrCats[] = (new Class[] {
111              javax.print.attribute.standard.PrinterName.class
112     });
113 
114     private static final Class otherAttrCats[] = (new Class [] {
115              javax.print.attribute.standard.Copies.class,
116              javax.print.attribute.standard.OrientationRequested.class,
117              javax.print.attribute.standard.Destination.class,
118     });
119 
120     private String printer = null;
121 
TestPrintService()122     public TestPrintService() {
123     }
124 
TestPrintService(String printerName)125     public TestPrintService(String printerName) {
126         if (printerName == null) {
127             throw new IllegalArgumentException("null printer name");
128         } else {
129             printer = printerName;
130         }
131     }
132 
getName()133     public String getName()
134     {
135         return printer;
136     }
137 
138 
createPrintJob()139     public DocPrintJob createPrintJob()
140     {
141         return  null;
142     }
143 
getUpdatedAttributes()144     public PrintServiceAttributeSet getUpdatedAttributes()
145     {
146         return null;
147     }
148 
149 
addPrintServiceAttributeListener(PrintServiceAttributeListener printserviceattributelistener)150     public void addPrintServiceAttributeListener(PrintServiceAttributeListener printserviceattributelistener)
151     {
152     }
153 
removePrintServiceAttributeListener(PrintServiceAttributeListener printserviceattributelistener)154     public void removePrintServiceAttributeListener(PrintServiceAttributeListener printserviceattributelistener)
155     {
156     }
157 
getAttribute(Class category)158     public PrintServiceAttribute getAttribute(Class category)
159     {
160         return null;
161     }
162 
getAttributes()163     public PrintServiceAttributeSet getAttributes()
164     {
165         HashPrintServiceAttributeSet aSet = new HashPrintServiceAttributeSet();
166             return aSet;
167     }
168 
getSupportedDocFlavors()169     public DocFlavor[] getSupportedDocFlavors()
170     {
171         int i = supportedDocFlavors.length;
172         DocFlavor adocflavor[] = new DocFlavor[i];
173         System.arraycopy(supportedDocFlavors, 0, adocflavor, 0, i);
174         return adocflavor;
175     }
176 
isDocFlavorSupported(DocFlavor docflavor)177     public boolean isDocFlavorSupported(DocFlavor docflavor)
178     {
179         for (int i = 0; i < supportedDocFlavors.length; i++) {
180             if (docflavor.equals(supportedDocFlavors[i])) {
181                 return true;
182             }
183         }
184         return false;
185     }
186 
getSupportedAttributeCategories()187     public Class[] getSupportedAttributeCategories()
188     {
189         int i = otherAttrCats.length;
190         Class aclass[] = new Class[i];
191         System.arraycopy(otherAttrCats, 0, aclass, 0, otherAttrCats.length);
192         return aclass;
193     }
194 
isAttributeCategorySupported(Class category)195     public boolean isAttributeCategorySupported(Class category)
196     {
197         if (category == null) {
198             throw new NullPointerException("null category");
199         }
200 
201         for (int i = 0; i < otherAttrCats.length; i++) {
202             if (category == otherAttrCats[i]) {
203                 return true;
204             }
205         }
206         return false;
207     }
208 
isAttributeValueSupported(Attribute attrval, DocFlavor flavor, AttributeSet attributes)209     public boolean isAttributeValueSupported(Attribute attrval, DocFlavor flavor, AttributeSet attributes) {
210 
211         if (attrval == OrientationRequested.PORTRAIT)
212                 return true;
213         else if (attrval == OrientationRequested.LANDSCAPE)
214                 return true;
215                 else
216                         return false;
217     }
218 
getDefaultAttributeValue(Class category)219     public Object getDefaultAttributeValue(Class category)
220     {
221         if (category == null) {
222             throw new NullPointerException("null category");
223         }
224         if (category == javax.print.attribute.standard.Copies.class)
225                 return new Copies(1);
226 
227         if (category == javax.print.attribute.standard.OrientationRequested.class)
228                 return OrientationRequested.PORTRAIT;
229 
230         return null;
231     }
232 
getSupportedAttributeValues(Class category, DocFlavor docflavor, AttributeSet attributeset)233     public Object getSupportedAttributeValues(Class category, DocFlavor docflavor, AttributeSet attributeset)
234     {
235         if (category == null) {
236             throw new NullPointerException("null category");
237         }
238 
239         if (docflavor != null) {
240             if (!isDocFlavorSupported(docflavor)) {
241                 throw new IllegalArgumentException(docflavor + " is an unsupported flavor");
242             }
243         }
244         if (!isAttributeCategorySupported(category)) {
245             return null;
246         }
247         if (category == javax.print.attribute.standard.Copies.class ) {
248                return new CopiesSupported(1, 5);
249         }
250         if (category == javax.print.attribute.standard.OrientationRequested.class ) {
251                OrientationRequested req[] = { OrientationRequested.PORTRAIT, OrientationRequested.LANDSCAPE };
252                return req;
253         }
254 
255         return null;
256     }
257 
getUnsupportedAttributes(DocFlavor docflavor, AttributeSet attributeset)258     public AttributeSet getUnsupportedAttributes(DocFlavor docflavor, AttributeSet attributeset) {
259 
260         if (docflavor != null && !isDocFlavorSupported(docflavor)) {
261             throw new IllegalArgumentException("flavor " + docflavor + "is not supported");
262         }
263         if (attributeset == null) {
264             return null;
265         }
266 
267         HashAttributeSet hashattributeset = new HashAttributeSet();
268         Attribute attributearray[] = attributeset.toArray();
269         for (int i = 0; i < attributearray.length; i++) {
270             try {
271                 Attribute attribute = attributearray[i];
272                 if (!isAttributeCategorySupported(attribute.getCategory())) {
273                      hashattributeset.add(attribute);
274                 } else {
275                   if (!isAttributeValueSupported(attribute, docflavor, attributeset)) {
276                      hashattributeset.add(attribute);
277                   }
278                 }
279             }
280             catch (ClassCastException classcastexception) {
281 
282             }
283         }
284 
285         if (hashattributeset.isEmpty()) {
286             return null;
287         }
288         return hashattributeset;
289     }
290 
getServiceUIFactory()291     public ServiceUIFactory getServiceUIFactory() {
292         return null;
293     }
294 
toString()295     public String toString() {
296         return "Printer : " + getName();
297     }
298 
equals(Object obj)299     public boolean equals(Object obj) {
300         return obj == this || (obj instanceof TestPrintService) && ((TestPrintService)obj).getName().equals(getName());
301     }
302 
hashCode()303     public int hashCode() {
304         return getClass().hashCode() + getName().hashCode();
305     }
306 
307 }
308