author | avstepan |
Mon, 11 Jan 2016 17:51:54 +0300 | |
changeset 35667 | ed476aba94de |
parent 26749 | b6598aa90114 |
child 37706 | 818b9c789c6c |
permissions | -rw-r--r-- |
2 | 1 |
/* |
24549
147a5c8b7793
8039109: Fix unchecked and raw lint warnings in java.awt
darcy
parents:
23010
diff
changeset
|
2 |
* Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.awt.print; |
|
27 |
||
28 |
import java.awt.AWTError; |
|
29 |
import java.awt.HeadlessException; |
|
30 |
import java.util.Enumeration; |
|
31 |
||
32 |
import javax.print.DocFlavor; |
|
33 |
import javax.print.PrintService; |
|
34 |
import javax.print.PrintServiceLookup; |
|
35 |
import javax.print.StreamPrintServiceFactory; |
|
36 |
import javax.print.attribute.PrintRequestAttributeSet; |
|
37 |
import javax.print.attribute.standard.Media; |
|
38 |
import javax.print.attribute.standard.MediaPrintableArea; |
|
39 |
import javax.print.attribute.standard.MediaSize; |
|
40 |
import javax.print.attribute.standard.MediaSizeName; |
|
41 |
import javax.print.attribute.standard.OrientationRequested; |
|
42 |
||
43 |
import sun.security.action.GetPropertyAction; |
|
44 |
||
45 |
/** |
|
35667 | 46 |
* The {@code PrinterJob} class is the principal class that controls |
2 | 47 |
* printing. An application calls methods in this class to set up a job, |
48 |
* optionally to invoke a print dialog with the user, and then to print |
|
49 |
* the pages of the job. |
|
50 |
*/ |
|
51 |
public abstract class PrinterJob { |
|
52 |
||
53 |
/* Public Class Methods */ |
|
54 |
||
55 |
/** |
|
35667 | 56 |
* Creates and returns a {@code PrinterJob} which is initially |
2 | 57 |
* associated with the default printer. |
58 |
* If no printers are available on the system, a PrinterJob will still |
|
35667 | 59 |
* be returned from this method, but {@code getPrintService()} |
60 |
* will return {@code null}, and calling |
|
61 |
* {@link #print() print} with this {@code PrinterJob} might |
|
2 | 62 |
* generate an exception. Applications that need to determine if |
35667 | 63 |
* there are suitable printers before creating a {@code PrinterJob} |
2 | 64 |
* should ensure that the array returned from |
65 |
* {@link #lookupPrintServices() lookupPrintServices} is not empty. |
|
35667 | 66 |
* @return a new {@code PrinterJob}. |
2 | 67 |
* |
68 |
* @throws SecurityException if a security manager exists and its |
|
69 |
* {@link java.lang.SecurityManager#checkPrintJobAccess} |
|
70 |
* method disallows this thread from creating a print job request |
|
71 |
*/ |
|
72 |
public static PrinterJob getPrinterJob() { |
|
73 |
SecurityManager security = System.getSecurityManager(); |
|
74 |
if (security != null) { |
|
75 |
security.checkPrintJobAccess(); |
|
76 |
} |
|
24549
147a5c8b7793
8039109: Fix unchecked and raw lint warnings in java.awt
darcy
parents:
23010
diff
changeset
|
77 |
return java.security.AccessController.doPrivileged( |
147a5c8b7793
8039109: Fix unchecked and raw lint warnings in java.awt
darcy
parents:
23010
diff
changeset
|
78 |
new java.security.PrivilegedAction<PrinterJob>() { |
147a5c8b7793
8039109: Fix unchecked and raw lint warnings in java.awt
darcy
parents:
23010
diff
changeset
|
79 |
public PrinterJob run() { |
2 | 80 |
String nm = System.getProperty("java.awt.printerjob", null); |
81 |
try { |
|
82 |
return (PrinterJob)Class.forName(nm).newInstance(); |
|
83 |
} catch (ClassNotFoundException e) { |
|
84 |
throw new AWTError("PrinterJob not found: " + nm); |
|
85 |
} catch (InstantiationException e) { |
|
86 |
throw new AWTError("Could not instantiate PrinterJob: " + nm); |
|
87 |
} catch (IllegalAccessException e) { |
|
88 |
throw new AWTError("Could not access PrinterJob: " + nm); |
|
89 |
} |
|
90 |
} |
|
91 |
}); |
|
92 |
} |
|
93 |
||
94 |
/** |
|
95 |
* A convenience method which looks up 2D print services. |
|
96 |
* Services returned from this method may be installed on |
|
35667 | 97 |
* {@code PrinterJob}s which support print services. |
2 | 98 |
* Calling this method is equivalent to calling |
99 |
* {@link javax.print.PrintServiceLookup#lookupPrintServices( |
|
100 |
* DocFlavor, AttributeSet) |
|
20451
4cedf4e1560a
8025409: Fix javadoc comments errors and warning reported by doclint report
cl
parents:
5506
diff
changeset
|
101 |
* PrintServiceLookup.lookupPrintServices()} |
2 | 102 |
* and specifying a Pageable DocFlavor. |
103 |
* @return a possibly empty array of 2D print services. |
|
104 |
* @since 1.4 |
|
105 |
*/ |
|
106 |
public static PrintService[] lookupPrintServices() { |
|
107 |
return PrintServiceLookup. |
|
108 |
lookupPrintServices(DocFlavor.SERVICE_FORMATTED.PAGEABLE, null); |
|
109 |
} |
|
110 |
||
111 |
||
112 |
/** |
|
113 |
* A convenience method which locates factories for stream print |
|
114 |
* services which can image 2D graphics. |
|
115 |
* Sample usage : |
|
21777 | 116 |
* <pre>{@code |
2 | 117 |
* FileOutputStream outstream; |
118 |
* StreamPrintService psPrinter; |
|
119 |
* String psMimeType = "application/postscript"; |
|
1740
fb0972d6e026
6665212: PrinterJob class, method lookupStreamPrintServices(), "fos" in docs is unknown
jgodinez
parents:
2
diff
changeset
|
120 |
* PrinterJob pj = PrinterJob.getPrinterJob(); |
2 | 121 |
* |
122 |
* StreamPrintServiceFactory[] factories = |
|
123 |
* PrinterJob.lookupStreamPrintServices(psMimeType); |
|
124 |
* if (factories.length > 0) { |
|
125 |
* try { |
|
126 |
* outstream = new File("out.ps"); |
|
1740
fb0972d6e026
6665212: PrinterJob class, method lookupStreamPrintServices(), "fos" in docs is unknown
jgodinez
parents:
2
diff
changeset
|
127 |
* psPrinter = factories[0].getPrintService(outstream); |
2 | 128 |
* // psPrinter can now be set as the service on a PrinterJob |
1740
fb0972d6e026
6665212: PrinterJob class, method lookupStreamPrintServices(), "fos" in docs is unknown
jgodinez
parents:
2
diff
changeset
|
129 |
* pj.setPrintService(psPrinter) |
fb0972d6e026
6665212: PrinterJob class, method lookupStreamPrintServices(), "fos" in docs is unknown
jgodinez
parents:
2
diff
changeset
|
130 |
* } catch (Exception e) { |
fb0972d6e026
6665212: PrinterJob class, method lookupStreamPrintServices(), "fos" in docs is unknown
jgodinez
parents:
2
diff
changeset
|
131 |
* e.printStackTrace(); |
2 | 132 |
* } |
133 |
* } |
|
21777 | 134 |
* }</pre> |
2 | 135 |
* Services returned from this method may be installed on |
35667 | 136 |
* {@code PrinterJob} instances which support print services. |
2 | 137 |
* Calling this method is equivalent to calling |
138 |
* {@link javax.print.StreamPrintServiceFactory#lookupStreamPrintServiceFactories(DocFlavor, String) |
|
20451
4cedf4e1560a
8025409: Fix javadoc comments errors and warning reported by doclint report
cl
parents:
5506
diff
changeset
|
139 |
* StreamPrintServiceFactory.lookupStreamPrintServiceFactories() |
4cedf4e1560a
8025409: Fix javadoc comments errors and warning reported by doclint report
cl
parents:
5506
diff
changeset
|
140 |
* } and specifying a Pageable DocFlavor. |
2 | 141 |
* |
142 |
* @param mimeType the required output format, or null to mean any format. |
|
143 |
* @return a possibly empty array of 2D stream print service factories. |
|
144 |
* @since 1.4 |
|
145 |
*/ |
|
146 |
public static StreamPrintServiceFactory[] |
|
147 |
lookupStreamPrintServices(String mimeType) { |
|
148 |
return StreamPrintServiceFactory.lookupStreamPrintServiceFactories( |
|
149 |
DocFlavor.SERVICE_FORMATTED.PAGEABLE, |
|
150 |
mimeType); |
|
151 |
} |
|
152 |
||
153 |
||
154 |
/* Public Methods */ |
|
155 |
||
156 |
/** |
|
35667 | 157 |
* A {@code PrinterJob} object should be created using the |
20451
4cedf4e1560a
8025409: Fix javadoc comments errors and warning reported by doclint report
cl
parents:
5506
diff
changeset
|
158 |
* static {@link #getPrinterJob() getPrinterJob} method. |
2 | 159 |
*/ |
160 |
public PrinterJob() { |
|
161 |
} |
|
162 |
||
163 |
/** |
|
164 |
* Returns the service (printer) for this printer job. |
|
165 |
* Implementations of this class which do not support print services |
|
166 |
* may return null. null will also be returned if no printers are |
|
167 |
* available. |
|
168 |
* @return the service for this printer job. |
|
169 |
* @see #setPrintService(PrintService) |
|
170 |
* @see #getPrinterJob() |
|
171 |
* @since 1.4 |
|
172 |
*/ |
|
173 |
public PrintService getPrintService() { |
|
174 |
return null; |
|
175 |
} |
|
176 |
||
177 |
/** |
|
178 |
* Associate this PrinterJob with a new PrintService. |
|
179 |
* This method is overridden by subclasses which support |
|
180 |
* specifying a Print Service. |
|
181 |
* |
|
35667 | 182 |
* Throws {@code PrinterException} if the specified service |
183 |
* cannot support the {@code Pageable} and |
|
184 |
* {@code Printable} interfaces necessary to support 2D printing. |
|
2 | 185 |
* @param service a print service that supports 2D printing |
186 |
* @exception PrinterException if the specified service does not support |
|
187 |
* 2D printing, or this PrinterJob class does not support |
|
188 |
* setting a 2D print service, or the specified service is |
|
189 |
* otherwise not a valid print service. |
|
190 |
* @see #getPrintService |
|
191 |
* @since 1.4 |
|
192 |
*/ |
|
193 |
public void setPrintService(PrintService service) |
|
194 |
throws PrinterException { |
|
195 |
throw new PrinterException( |
|
196 |
"Setting a service is not supported on this class"); |
|
197 |
} |
|
198 |
||
199 |
/** |
|
35667 | 200 |
* Calls {@code painter} to render the pages. The pages in the |
2 | 201 |
* document to be printed by this |
35667 | 202 |
* {@code PrinterJob} are rendered by the {@link Printable} |
203 |
* object, {@code painter}. The {@link PageFormat} for each page |
|
2 | 204 |
* is the default page format. |
35667 | 205 |
* @param painter the {@code Printable} that renders each page of |
2 | 206 |
* the document. |
207 |
*/ |
|
208 |
public abstract void setPrintable(Printable painter); |
|
209 |
||
210 |
/** |
|
35667 | 211 |
* Calls {@code painter} to render the pages in the specified |
212 |
* {@code format}. The pages in the document to be printed by |
|
213 |
* this {@code PrinterJob} are rendered by the |
|
214 |
* {@code Printable} object, {@code painter}. The |
|
215 |
* {@code PageFormat} of each page is {@code format}. |
|
216 |
* @param painter the {@code Printable} called to render |
|
2 | 217 |
* each page of the document |
218 |
* @param format the size and orientation of each page to |
|
219 |
* be printed |
|
220 |
*/ |
|
221 |
public abstract void setPrintable(Printable painter, PageFormat format); |
|
222 |
||
223 |
/** |
|
35667 | 224 |
* Queries {@code document} for the number of pages and |
225 |
* the {@code PageFormat} and {@code Printable} for each |
|
226 |
* page held in the {@code Pageable} instance, |
|
227 |
* {@code document}. |
|
2 | 228 |
* @param document the pages to be printed. It can not be |
35667 | 229 |
* {@code null}. |
230 |
* @exception NullPointerException the {@code Pageable} passed in |
|
231 |
* was {@code null}. |
|
2 | 232 |
* @see PageFormat |
233 |
* @see Printable |
|
234 |
*/ |
|
235 |
public abstract void setPageable(Pageable document) |
|
236 |
throws NullPointerException; |
|
237 |
||
238 |
/** |
|
239 |
* Presents a dialog to the user for changing the properties of |
|
240 |
* the print job. |
|
241 |
* This method will display a native dialog if a native print |
|
242 |
* service is selected, and user choice of printers will be restricted |
|
243 |
* to these native print services. |
|
244 |
* To present the cross platform print dialog for all services, |
|
245 |
* including native ones instead use |
|
35667 | 246 |
* {@code printDialog(PrintRequestAttributeSet)}. |
2 | 247 |
* <p> |
248 |
* PrinterJob implementations which can use PrintService's will update |
|
249 |
* the PrintService for this PrinterJob to reflect the new service |
|
250 |
* selected by the user. |
|
35667 | 251 |
* @return {@code true} if the user does not cancel the dialog; |
252 |
* {@code false} otherwise. |
|
2 | 253 |
* @exception HeadlessException if GraphicsEnvironment.isHeadless() |
254 |
* returns true. |
|
255 |
* @see java.awt.GraphicsEnvironment#isHeadless |
|
256 |
*/ |
|
257 |
public abstract boolean printDialog() throws HeadlessException; |
|
258 |
||
259 |
/** |
|
260 |
* A convenience method which displays a cross-platform print dialog |
|
261 |
* for all services which are capable of printing 2D graphics using the |
|
35667 | 262 |
* {@code Pageable} interface. The selected printer when the |
2 | 263 |
* dialog is initially displayed will reflect the print service currently |
264 |
* attached to this print job. |
|
265 |
* If the user changes the print service, the PrinterJob will be |
|
266 |
* updated to reflect this, unless the user cancels the dialog. |
|
267 |
* As well as allowing the user to select the destination printer, |
|
268 |
* the user can also select values of various print request attributes. |
|
269 |
* <p> |
|
270 |
* The attributes parameter on input will reflect the applications |
|
271 |
* required initial selections in the user dialog. Attributes not |
|
272 |
* specified display using the default for the service. On return it |
|
273 |
* will reflect the user's choices. Selections may be updated by |
|
274 |
* the implementation to be consistent with the supported values |
|
275 |
* for the currently selected print service. |
|
276 |
* <p> |
|
277 |
* As the user scrolls to a new print service selection, the values |
|
278 |
* copied are based on the settings for the previous service, together |
|
279 |
* with any user changes. The values are not based on the original |
|
280 |
* settings supplied by the client. |
|
281 |
* <p> |
|
282 |
* With the exception of selected printer, the PrinterJob state is |
|
283 |
* not updated to reflect the user's changes. |
|
284 |
* For the selections to affect a printer job, the attributes must |
|
285 |
* be specified in the call to the |
|
35667 | 286 |
* {@code print(PrintRequestAttributeSet)} method. If using |
2 | 287 |
* the Pageable interface, clients which intend to use media selected |
288 |
* by the user must create a PageFormat derived from the user's |
|
289 |
* selections. |
|
290 |
* If the user cancels the dialog, the attributes will not reflect |
|
291 |
* any changes made by the user. |
|
292 |
* @param attributes on input is application supplied attributes, |
|
293 |
* on output the contents are updated to reflect user choices. |
|
294 |
* This parameter may not be null. |
|
35667 | 295 |
* @return {@code true} if the user does not cancel the dialog; |
296 |
* {@code false} otherwise. |
|
2 | 297 |
* @exception HeadlessException if GraphicsEnvironment.isHeadless() |
298 |
* returns true. |
|
35667 | 299 |
* @exception NullPointerException if {@code attributes} parameter |
2 | 300 |
* is null. |
301 |
* @see java.awt.GraphicsEnvironment#isHeadless |
|
302 |
* @since 1.4 |
|
303 |
* |
|
304 |
*/ |
|
305 |
public boolean printDialog(PrintRequestAttributeSet attributes) |
|
306 |
throws HeadlessException { |
|
307 |
||
308 |
if (attributes == null) { |
|
309 |
throw new NullPointerException("attributes"); |
|
310 |
} |
|
311 |
return printDialog(); |
|
312 |
} |
|
313 |
||
314 |
/** |
|
315 |
* Displays a dialog that allows modification of a |
|
35667 | 316 |
* {@code PageFormat} instance. |
317 |
* The {@code page} argument is used to initialize controls |
|
2 | 318 |
* in the page setup dialog. |
319 |
* If the user cancels the dialog then this method returns the |
|
35667 | 320 |
* original {@code page} object unmodified. |
2 | 321 |
* If the user okays the dialog then this method returns a new |
35667 | 322 |
* {@code PageFormat} object with the indicated changes. |
323 |
* In either case, the original {@code page} object is |
|
2 | 324 |
* not modified. |
35667 | 325 |
* @param page the default {@code PageFormat} presented to the |
2 | 326 |
* user for modification |
35667 | 327 |
* @return the original {@code page} object if the dialog |
328 |
* is cancelled; a new {@code PageFormat} object |
|
2 | 329 |
* containing the format indicated by the user if the |
330 |
* dialog is acknowledged. |
|
331 |
* @exception HeadlessException if GraphicsEnvironment.isHeadless() |
|
332 |
* returns true. |
|
333 |
* @see java.awt.GraphicsEnvironment#isHeadless |
|
334 |
* @since 1.2 |
|
335 |
*/ |
|
336 |
public abstract PageFormat pageDialog(PageFormat page) |
|
337 |
throws HeadlessException; |
|
338 |
||
339 |
/** |
|
340 |
* A convenience method which displays a cross-platform page setup dialog. |
|
341 |
* The choices available will reflect the print service currently |
|
342 |
* set on this PrinterJob. |
|
343 |
* <p> |
|
344 |
* The attributes parameter on input will reflect the client's |
|
345 |
* required initial selections in the user dialog. Attributes which are |
|
346 |
* not specified display using the default for the service. On return it |
|
347 |
* will reflect the user's choices. Selections may be updated by |
|
348 |
* the implementation to be consistent with the supported values |
|
349 |
* for the currently selected print service. |
|
350 |
* <p> |
|
351 |
* The return value will be a PageFormat equivalent to the |
|
352 |
* selections in the PrintRequestAttributeSet. |
|
353 |
* If the user cancels the dialog, the attributes will not reflect |
|
354 |
* any changes made by the user, and the return value will be null. |
|
355 |
* @param attributes on input is application supplied attributes, |
|
356 |
* on output the contents are updated to reflect user choices. |
|
357 |
* This parameter may not be null. |
|
358 |
* @return a page format if the user does not cancel the dialog; |
|
35667 | 359 |
* {@code null} otherwise. |
2 | 360 |
* @exception HeadlessException if GraphicsEnvironment.isHeadless() |
361 |
* returns true. |
|
35667 | 362 |
* @exception NullPointerException if {@code attributes} parameter |
2 | 363 |
* is null. |
364 |
* @see java.awt.GraphicsEnvironment#isHeadless |
|
365 |
* @since 1.4 |
|
366 |
* |
|
367 |
*/ |
|
368 |
public PageFormat pageDialog(PrintRequestAttributeSet attributes) |
|
369 |
throws HeadlessException { |
|
370 |
||
371 |
if (attributes == null) { |
|
372 |
throw new NullPointerException("attributes"); |
|
373 |
} |
|
374 |
return pageDialog(defaultPage()); |
|
375 |
} |
|
376 |
||
377 |
/** |
|
35667 | 378 |
* Clones the {@code PageFormat} argument and alters the |
2 | 379 |
* clone to describe a default page size and orientation. |
35667 | 380 |
* @param page the {@code PageFormat} to be cloned and altered |
381 |
* @return clone of {@code page}, altered to describe a default |
|
382 |
* {@code PageFormat}. |
|
2 | 383 |
*/ |
384 |
public abstract PageFormat defaultPage(PageFormat page); |
|
385 |
||
386 |
/** |
|
35667 | 387 |
* Creates a new {@code PageFormat} instance and |
2 | 388 |
* sets it to a default size and orientation. |
35667 | 389 |
* @return a {@code PageFormat} set to a default size and |
2 | 390 |
* orientation. |
391 |
*/ |
|
392 |
public PageFormat defaultPage() { |
|
393 |
return defaultPage(new PageFormat()); |
|
394 |
} |
|
395 |
||
396 |
/** |
|
35667 | 397 |
* Calculates a {@code PageFormat} with values consistent with those |
398 |
* supported by the current {@code PrintService} for this job |
|
399 |
* (ie the value returned by {@code getPrintService()}) and media, |
|
400 |
* printable area and orientation contained in {@code attributes}. |
|
2 | 401 |
* <p> |
402 |
* Calling this method does not update the job. |
|
403 |
* It is useful for clients that have a set of attributes obtained from |
|
35667 | 404 |
* {@code printDialog(PrintRequestAttributeSet attributes)} |
2 | 405 |
* and need a PageFormat to print a Pageable object. |
406 |
* @param attributes a set of printing attributes, for example obtained |
|
35667 | 407 |
* from calling printDialog. If {@code attributes} is null a default |
2 | 408 |
* PageFormat is returned. |
35667 | 409 |
* @return a {@code PageFormat} whose settings conform with |
2 | 410 |
* those of the current service and the specified attributes. |
411 |
* @since 1.6 |
|
412 |
*/ |
|
413 |
public PageFormat getPageFormat(PrintRequestAttributeSet attributes) { |
|
414 |
||
415 |
PrintService service = getPrintService(); |
|
416 |
PageFormat pf = defaultPage(); |
|
417 |
||
418 |
if (service == null || attributes == null) { |
|
419 |
return pf; |
|
420 |
} |
|
421 |
||
422 |
Media media = (Media)attributes.get(Media.class); |
|
423 |
MediaPrintableArea mpa = |
|
424 |
(MediaPrintableArea)attributes.get(MediaPrintableArea.class); |
|
425 |
OrientationRequested orientReq = |
|
426 |
(OrientationRequested)attributes.get(OrientationRequested.class); |
|
427 |
||
428 |
if (media == null && mpa == null && orientReq == null) { |
|
429 |
return pf; |
|
430 |
} |
|
431 |
Paper paper = pf.getPaper(); |
|
432 |
||
433 |
/* If there's a media but no media printable area, we can try |
|
434 |
* to retrieve the default value for mpa and use that. |
|
435 |
*/ |
|
436 |
if (mpa == null && media != null && |
|
437 |
service.isAttributeCategorySupported(MediaPrintableArea.class)) { |
|
438 |
Object mpaVals = |
|
439 |
service.getSupportedAttributeValues(MediaPrintableArea.class, |
|
440 |
null, attributes); |
|
441 |
if (mpaVals instanceof MediaPrintableArea[] && |
|
442 |
((MediaPrintableArea[])mpaVals).length > 0) { |
|
443 |
mpa = ((MediaPrintableArea[])mpaVals)[0]; |
|
444 |
} |
|
445 |
} |
|
446 |
||
447 |
if (media != null && |
|
448 |
service.isAttributeValueSupported(media, null, attributes)) { |
|
449 |
if (media instanceof MediaSizeName) { |
|
450 |
MediaSizeName msn = (MediaSizeName)media; |
|
451 |
MediaSize msz = MediaSize.getMediaSizeForName(msn); |
|
452 |
if (msz != null) { |
|
453 |
double inch = 72.0; |
|
454 |
double paperWid = msz.getX(MediaSize.INCH) * inch; |
|
455 |
double paperHgt = msz.getY(MediaSize.INCH) * inch; |
|
456 |
paper.setSize(paperWid, paperHgt); |
|
457 |
if (mpa == null) { |
|
458 |
paper.setImageableArea(inch, inch, |
|
459 |
paperWid-2*inch, |
|
460 |
paperHgt-2*inch); |
|
461 |
} |
|
462 |
} |
|
463 |
} |
|
464 |
} |
|
465 |
||
466 |
if (mpa != null && |
|
467 |
service.isAttributeValueSupported(mpa, null, attributes)) { |
|
468 |
float [] printableArea = |
|
469 |
mpa.getPrintableArea(MediaPrintableArea.INCH); |
|
470 |
for (int i=0; i < printableArea.length; i++) { |
|
471 |
printableArea[i] = printableArea[i]*72.0f; |
|
472 |
} |
|
473 |
paper.setImageableArea(printableArea[0], printableArea[1], |
|
474 |
printableArea[2], printableArea[3]); |
|
475 |
} |
|
476 |
||
477 |
if (orientReq != null && |
|
478 |
service.isAttributeValueSupported(orientReq, null, attributes)) { |
|
479 |
int orient; |
|
480 |
if (orientReq.equals(OrientationRequested.REVERSE_LANDSCAPE)) { |
|
481 |
orient = PageFormat.REVERSE_LANDSCAPE; |
|
482 |
} else if (orientReq.equals(OrientationRequested.LANDSCAPE)) { |
|
483 |
orient = PageFormat.LANDSCAPE; |
|
484 |
} else { |
|
485 |
orient = PageFormat.PORTRAIT; |
|
486 |
} |
|
487 |
pf.setOrientation(orient); |
|
488 |
} |
|
489 |
||
490 |
pf.setPaper(paper); |
|
491 |
pf = validatePage(pf); |
|
492 |
return pf; |
|
493 |
} |
|
494 |
||
495 |
/** |
|
35667 | 496 |
* Returns the clone of {@code page} with its settings |
2 | 497 |
* adjusted to be compatible with the current printer of this |
35667 | 498 |
* {@code PrinterJob}. For example, the returned |
499 |
* {@code PageFormat} could have its imageable area |
|
2 | 500 |
* adjusted to fit within the physical area of the paper that |
501 |
* is used by the current printer. |
|
35667 | 502 |
* @param page the {@code PageFormat} that is cloned and |
2 | 503 |
* whose settings are changed to be compatible with |
504 |
* the current printer |
|
35667 | 505 |
* @return a {@code PageFormat} that is cloned from |
506 |
* {@code page} and whose settings are changed |
|
507 |
* to conform with this {@code PrinterJob}. |
|
2 | 508 |
*/ |
509 |
public abstract PageFormat validatePage(PageFormat page); |
|
510 |
||
511 |
/** |
|
512 |
* Prints a set of pages. |
|
513 |
* @exception PrinterException an error in the print system |
|
514 |
* caused the job to be aborted. |
|
515 |
* @see Book |
|
516 |
* @see Pageable |
|
517 |
* @see Printable |
|
518 |
*/ |
|
519 |
public abstract void print() throws PrinterException; |
|
520 |
||
521 |
/** |
|
522 |
* Prints a set of pages using the settings in the attribute |
|
523 |
* set. The default implementation ignores the attribute set. |
|
524 |
* <p> |
|
525 |
* Note that some attributes may be set directly on the PrinterJob |
|
526 |
* by equivalent method calls, (for example), copies: |
|
35667 | 527 |
* {@code setCopies(int)}, job name: {@code setJobName(String)} |
2 | 528 |
* and specifying media size and orientation though the |
35667 | 529 |
* {@code PageFormat} object. |
2 | 530 |
* <p> |
531 |
* If a supported attribute-value is specified in this attribute set, |
|
532 |
* it will take precedence over the API settings for this print() |
|
533 |
* operation only. |
|
534 |
* The following behaviour is specified for PageFormat: |
|
535 |
* If a client uses the Printable interface, then the |
|
35667 | 536 |
* {@code attributes} parameter to this method is examined |
2 | 537 |
* for attributes which specify media (by size), orientation, and |
538 |
* imageable area, and those are used to construct a new PageFormat |
|
539 |
* which is passed to the Printable object's print() method. |
|
540 |
* See {@link Printable} for an explanation of the required |
|
541 |
* behaviour of a Printable to ensure optimal printing via PrinterJob. |
|
542 |
* For clients of the Pageable interface, the PageFormat will always |
|
543 |
* be as supplied by that interface, on a per page basis. |
|
544 |
* <p> |
|
545 |
* These behaviours allow an application to directly pass the |
|
546 |
* user settings returned from |
|
35667 | 547 |
* {@code printDialog(PrintRequestAttributeSet attributes} to |
2 | 548 |
* this print() method. |
549 |
* |
|
550 |
* @param attributes a set of attributes for the job |
|
551 |
* @exception PrinterException an error in the print system |
|
552 |
* caused the job to be aborted. |
|
553 |
* @see Book |
|
554 |
* @see Pageable |
|
555 |
* @see Printable |
|
556 |
* @since 1.4 |
|
557 |
*/ |
|
558 |
public void print(PrintRequestAttributeSet attributes) |
|
559 |
throws PrinterException { |
|
560 |
print(); |
|
561 |
} |
|
562 |
||
563 |
/** |
|
564 |
* Sets the number of copies to be printed. |
|
565 |
* @param copies the number of copies to be printed |
|
566 |
* @see #getCopies |
|
567 |
*/ |
|
568 |
public abstract void setCopies(int copies); |
|
569 |
||
570 |
/** |
|
571 |
* Gets the number of copies to be printed. |
|
572 |
* @return the number of copies to be printed. |
|
573 |
* @see #setCopies |
|
574 |
*/ |
|
575 |
public abstract int getCopies(); |
|
576 |
||
577 |
/** |
|
578 |
* Gets the name of the printing user. |
|
579 |
* @return the name of the printing user |
|
580 |
*/ |
|
581 |
public abstract String getUserName(); |
|
582 |
||
583 |
/** |
|
584 |
* Sets the name of the document to be printed. |
|
35667 | 585 |
* The document name can not be {@code null}. |
2 | 586 |
* @param jobName the name of the document to be printed |
587 |
* @see #getJobName |
|
588 |
*/ |
|
589 |
public abstract void setJobName(String jobName); |
|
590 |
||
591 |
/** |
|
592 |
* Gets the name of the document to be printed. |
|
593 |
* @return the name of the document to be printed. |
|
594 |
* @see #setJobName |
|
595 |
*/ |
|
596 |
public abstract String getJobName(); |
|
597 |
||
598 |
/** |
|
599 |
* Cancels a print job that is in progress. If |
|
600 |
* {@link #print() print} has been called but has not |
|
601 |
* returned then this method signals |
|
602 |
* that the job should be cancelled at the next |
|
603 |
* chance. If there is no print job in progress then |
|
604 |
* this call does nothing. |
|
605 |
*/ |
|
606 |
public abstract void cancel(); |
|
607 |
||
608 |
/** |
|
35667 | 609 |
* Returns {@code true} if a print job is |
2 | 610 |
* in progress, but is going to be cancelled |
611 |
* at the next opportunity; otherwise returns |
|
35667 | 612 |
* {@code false}. |
613 |
* @return {@code true} if the job in progress |
|
614 |
* is going to be cancelled; {@code false} otherwise. |
|
2 | 615 |
*/ |
616 |
public abstract boolean isCancelled(); |
|
617 |
||
618 |
} |