File: ServiceDialogTest.java

package info (click to toggle)
openjdk-24 24.0.2%2B12-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 831,900 kB
  • sloc: java: 5,677,020; cpp: 1,323,154; xml: 1,320,524; ansic: 486,889; asm: 405,131; objc: 21,025; sh: 15,221; javascript: 11,049; python: 8,222; makefile: 2,504; perl: 357; awk: 351; sed: 172; pascal: 103; exp: 54; jsp: 24; csh: 3
file content (308 lines) | stat: -rw-r--r-- 10,512 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
 * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */


/**
 * @test
 * @bug 4910388 4871089 4998624
 * @key printer
 * @summary Confirm that
 *      1. After choosing Reverse Landscape in the system default print
 *       Print Service (2nd in the list), it
 *          will reset to portrait in "Test Printer"
 *      2. Print To File button is not cleared when switching between the
 *         2nd service (system default printer) and Test Printer.
 *      3. Make sure "Postscript" printer is the default and make sure the
 *         "print to file" button is disabled.  File Dialog should not be
 *         shown after pressing print button.
 *
 * @run main/manual ServiceDialogTest
 */
import java.awt.*;
import javax.print.*;
import javax.print.attribute.standard.*;
import javax.print.attribute.*;
import javax.print.event.*;
import java.io.*;
import java.util.Locale;

public class ServiceDialogTest {
        /**
         * Constructor
         */
         public ServiceDialogTest() {
                super();
        }
        /**
         * Starts the application.
         */
        public static void main(java.lang.String[] args) {
                ServiceDialogTest pd = new ServiceDialogTest();
                PrintService services[] = new PrintService[3];
                services[1] = PrintServiceLookup.lookupDefaultPrintService();

                FileOutputStream fos = null;
                File f = null;
                String mType = "application/postscript";
                DocFlavor flavor = DocFlavor.INPUT_STREAM.JPEG;
                try {
                        f = new File("streamexample.ps");
                        fos = new FileOutputStream(f);
                        StreamPrintServiceFactory[] factories = StreamPrintServiceFactory.lookupStreamPrintServiceFactories(flavor, mType);
                        if (factories.length > 0) {
                                services[0] = factories[0].getPrintService(fos);
                        } else {
                                throw new RuntimeException("No StreamPrintService available which would support "+flavor);
                        }

                        services[2] = new TestPrintService("Test Printer");

            //System.out.println("is "+flavor+" supported? "+services[0].isDocFlavorSupported(flavor));
            //System.out.println("is Orientation supported? "+services[0].isAttributeCategorySupported(OrientationRequested.class));
            //System.out.println("is REVERSE PORTRAIT supported ? "+services[0].isAttributeValueSupported(OrientationRequested.REVERSE_PORTRAIT, flavor, null));

            HashPrintRequestAttributeSet prSet = new HashPrintRequestAttributeSet();
            prSet.add(new Destination(new File("./dest.prn").toURI()));
            PrintService selService = ServiceUI.printDialog(null, 200, 200, services, services[0], flavor, prSet);
                        Attribute attr[] = prSet.toArray();
                        for (int x = 0; x < attr.length; x ++) {
                                System.out.println(attr[x]);
                        }

                        //DocPrintJob pj = service.createPrintJob();
                        //PrintDocument prDoc = new PrintDocument();
                        //pj.print(prDoc, null);

                } catch (Exception e) {
                        e.printStackTrace();
                }
        }
}


class TestPrintService implements PrintService
{

    private static DocFlavor textByteFlavor = null;
    private static final DocFlavor supportedDocFlavors[] = (new DocFlavor[] {
             javax.print.DocFlavor.INPUT_STREAM.JPEG
    });

    private static final Class serviceAttrCats[] = (new Class[] {
             javax.print.attribute.standard.PrinterName.class
    });

    private static final Class otherAttrCats[] = (new Class [] {
             javax.print.attribute.standard.Copies.class,
             javax.print.attribute.standard.OrientationRequested.class,
             javax.print.attribute.standard.Destination.class,
    });

    private String printer = null;

    public TestPrintService() {
    }

    public TestPrintService(String printerName) {
        if (printerName == null) {
            throw new IllegalArgumentException("null printer name");
        } else {
            printer = printerName;
        }
    }

    public String getName()
    {
        return printer;
    }


    public DocPrintJob createPrintJob()
    {
        return  null;
    }

    public PrintServiceAttributeSet getUpdatedAttributes()
    {
        return null;
    }


    public void addPrintServiceAttributeListener(PrintServiceAttributeListener printserviceattributelistener)
    {
    }

    public void removePrintServiceAttributeListener(PrintServiceAttributeListener printserviceattributelistener)
    {
    }

    public PrintServiceAttribute getAttribute(Class category)
    {
        return null;
    }

    public PrintServiceAttributeSet getAttributes()
    {
        HashPrintServiceAttributeSet aSet = new HashPrintServiceAttributeSet();
            return aSet;
    }

    public DocFlavor[] getSupportedDocFlavors()
    {
        int i = supportedDocFlavors.length;
        DocFlavor adocflavor[] = new DocFlavor[i];
        System.arraycopy(supportedDocFlavors, 0, adocflavor, 0, i);
        return adocflavor;
    }

    public boolean isDocFlavorSupported(DocFlavor docflavor)
    {
        for (int i = 0; i < supportedDocFlavors.length; i++) {
            if (docflavor.equals(supportedDocFlavors[i])) {
                return true;
            }
        }
        return false;
    }

    public Class[] getSupportedAttributeCategories()
    {
        int i = otherAttrCats.length;
        Class aclass[] = new Class[i];
        System.arraycopy(otherAttrCats, 0, aclass, 0, otherAttrCats.length);
        return aclass;
    }

    public boolean isAttributeCategorySupported(Class category)
    {
        if (category == null) {
            throw new NullPointerException("null category");
        }

        for (int i = 0; i < otherAttrCats.length; i++) {
            if (category == otherAttrCats[i]) {
                return true;
            }
        }
        return false;
    }

    public boolean isAttributeValueSupported(Attribute attrval, DocFlavor flavor, AttributeSet attributes) {

        if (attrval == OrientationRequested.PORTRAIT)
                return true;
        else if (attrval == OrientationRequested.LANDSCAPE)
                return true;
                else
                        return false;
    }

    public Object getDefaultAttributeValue(Class category)
    {
        if (category == null) {
            throw new NullPointerException("null category");
        }
        if (category == javax.print.attribute.standard.Copies.class)
                return new Copies(1);

        if (category == javax.print.attribute.standard.OrientationRequested.class)
                return OrientationRequested.PORTRAIT;

        return null;
    }

    public Object getSupportedAttributeValues(Class category, DocFlavor docflavor, AttributeSet attributeset)
    {
        if (category == null) {
            throw new NullPointerException("null category");
        }

        if (docflavor != null) {
            if (!isDocFlavorSupported(docflavor)) {
                throw new IllegalArgumentException(docflavor + " is an unsupported flavor");
            }
        }
        if (!isAttributeCategorySupported(category)) {
            return null;
        }
        if (category == javax.print.attribute.standard.Copies.class ) {
               return new CopiesSupported(1, 5);
        }
        if (category == javax.print.attribute.standard.OrientationRequested.class ) {
               OrientationRequested req[] = { OrientationRequested.PORTRAIT, OrientationRequested.LANDSCAPE };
               return req;
        }

        return null;
    }

    public AttributeSet getUnsupportedAttributes(DocFlavor docflavor, AttributeSet attributeset) {

        if (docflavor != null && !isDocFlavorSupported(docflavor)) {
            throw new IllegalArgumentException("flavor " + docflavor + "is not supported");
        }
        if (attributeset == null) {
            return null;
        }

        HashAttributeSet hashattributeset = new HashAttributeSet();
        Attribute attributearray[] = attributeset.toArray();
        for (int i = 0; i < attributearray.length; i++) {
            try {
                Attribute attribute = attributearray[i];
                if (!isAttributeCategorySupported(attribute.getCategory())) {
                     hashattributeset.add(attribute);
                } else {
                  if (!isAttributeValueSupported(attribute, docflavor, attributeset)) {
                     hashattributeset.add(attribute);
                  }
                }
            }
            catch (ClassCastException classcastexception) {

            }
        }

        if (hashattributeset.isEmpty()) {
            return null;
        }
        return hashattributeset;
    }

    public ServiceUIFactory getServiceUIFactory() {
        return null;
    }

    public String toString() {
        return "Printer : " + getName();
    }

    public boolean equals(Object obj) {
        return obj == this || (obj instanceof TestPrintService) && ((TestPrintService)obj).getName().equals(getName());
    }

    public int hashCode() {
        return getClass().hashCode() + getName().hashCode();
    }

}