1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package java.awt.print;
19
20 import java.awt.AWTError;
21 import java.awt.HeadlessException;
22 import java.security.AccessController;
23 import java.security.PrivilegedAction;
24
25 import javax.print.PrintService;
26 import javax.print.PrintServiceLookup;
27 import javax.print.StreamPrintServiceFactory;
28 import javax.print.attribute.PrintRequestAttributeSet;
29
30 import org.apache.harmony.awt.internal.nls.Messages;
31
32 public abstract class PrinterJob {
33
34 /* abstract section */
35 public abstract void cancel();
36
37 public abstract void setPrintable(Printable painter);
38
39 public abstract void setPrintable(Printable painter, PageFormat format);
40
41 public abstract void setPageable(Pageable document)
42 throws NullPointerException;
43
44 public abstract void print() throws PrinterException;
45
46 public abstract void setJobName(String jobName);
47
48 public abstract void setCopies(int copies);
49
50 public abstract int getCopies();
51
52 public abstract boolean printDialog() throws HeadlessException;
53
54 public abstract boolean isCancelled();
55
56 public abstract String getUserName();
57
58 public abstract String getJobName();
59
60 public abstract PageFormat pageDialog(PageFormat page)
61 throws HeadlessException;
62
63 public abstract PageFormat defaultPage(PageFormat page);
64
65 public abstract PageFormat validatePage(PageFormat page);
66
67 /* static section */
68 public static PrinterJob getPrinterJob(){
69
70 SecurityManager securitymanager = System.getSecurityManager();
71 if(securitymanager != null) {
72 securitymanager.checkPrintJobAccess();
73 }
74 /* This code has been developed according to API documentation
75 * for Priviledged Blocks.
76 */
77 return AccessController.doPrivileged(
78 new PrivilegedAction<PrinterJob>() {
79 public PrinterJob run() {
80 String s = org.apache.harmony.awt.Utils.getSystemProperty("java.awt.printerjob"); //$NON-NLS-1$
81
82 if (s == null || s.equals("")){ //$NON-NLS-1$
83 s = "java.awt.print.PrinterJobImpl"; //$NON-NLS-1$
84 }
85 try {
86 return (PrinterJob) Class.forName(s).newInstance();
87 } catch (ClassNotFoundException cnfe) {
88 // awt.5A=Default class for PrinterJob is not found
89 throw new AWTError(Messages.getString("awt.5A")); //$NON-NLS-1$
90 } catch (IllegalAccessException iae) {
91 // awt.5B=No access to default class for PrinterJob
92 throw new AWTError(Messages.getString("awt.5B")); //$NON-NLS-1$
93 } catch (InstantiationException ie) {
94 // awt.5C=Instantiation exception for PrinterJob
95 throw new AWTError(Messages.getString("awt.5C")); //$NON-NLS-1$
96 }
97 }
98 });
99 }
100
101
102 public static PrintService[] lookupPrintServices(){
103 return PrintServiceLookup.lookupPrintServices(
104 javax.print.DocFlavor.SERVICE_FORMATTED.PAGEABLE, null);
105 }
106
107 public static StreamPrintServiceFactory[] lookupStreamPrintServices(
108 String mimeType) {
109 return StreamPrintServiceFactory.lookupStreamPrintServiceFactories(
110 javax.print.DocFlavor.SERVICE_FORMATTED.PAGEABLE, mimeType);
111 }
112
113 /* public section*/
114 public PrinterJob() {
115 super();
116 }
117
118 public PageFormat defaultPage(){
119 return defaultPage(new PageFormat());
120 }
121
122 public PrintService getPrintService(){
123 return null;
124 }
125
126 public void print(PrintRequestAttributeSet attributes)
127 throws PrinterException {
128 // This implementation ignores the attribute set.
129 print();
130 }
131
132 public boolean printDialog(PrintRequestAttributeSet attributes)
133 throws HeadlessException {
134 if (attributes == null) {
135 // awt.01='{0}' parameter is null
136 throw new NullPointerException(Messages.getString("awt.01", "attributes")); //$NON-NLS-1$ //$NON-NLS-2$
137 }
138 //This implementation ignores the attribute set.
139 return printDialog();
140 }
141
142 public void setPrintService(PrintService printservice)
143 throws PrinterException {
144 // awt.5D={0} is not supported
145 throw new PrinterException(Messages.getString(
146 "awt.5D", printservice.toString())); //$NON-NLS-1$
147 }
148
149 public PageFormat pageDialog(PrintRequestAttributeSet attributes)
150 throws HeadlessException {
151 //This implementation ignores the attribute set.
152 if(attributes == null) {
153 // awt.01='{0}' parameter is null
154 throw new NullPointerException(Messages.getString("awt.01", "attributes")); //$NON-NLS-1$ //$NON-NLS-2$
155 }
156 return pageDialog(defaultPage());
157 }
158
159 }