author | malenkov |
Tue, 29 Oct 2013 17:01:06 +0400 | |
changeset 21278 | ef8a3a2a72f2 |
parent 19781 | 91b06f016821 |
child 21283 | b24d2a6e59b8 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
14342
8435a30053c1
7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents:
12047
diff
changeset
|
2 |
* Copyright (c) 1998, 2012, 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 sun.print; |
|
27 |
||
28 |
import java.io.FilePermission; |
|
29 |
||
30 |
import java.awt.Color; |
|
31 |
import java.awt.Dialog; |
|
32 |
import java.awt.Frame; |
|
33 |
import java.awt.Graphics; |
|
34 |
import java.awt.Graphics2D; |
|
35 |
import java.awt.GraphicsConfiguration; |
|
36 |
import java.awt.GraphicsEnvironment; |
|
37 |
import java.awt.HeadlessException; |
|
38 |
import java.awt.KeyboardFocusManager; |
|
39 |
import java.awt.Rectangle; |
|
40 |
import java.awt.Shape; |
|
41 |
import java.awt.geom.AffineTransform; |
|
42 |
import java.awt.geom.Area; |
|
43 |
import java.awt.geom.Point2D; |
|
44 |
import java.awt.geom.Rectangle2D; |
|
45 |
import java.awt.image.BufferedImage; |
|
46 |
import java.awt.print.Book; |
|
47 |
import java.awt.print.Pageable; |
|
48 |
import java.awt.print.PageFormat; |
|
49 |
import java.awt.print.Paper; |
|
50 |
import java.awt.print.Printable; |
|
51 |
import java.awt.print.PrinterAbortException; |
|
52 |
import java.awt.print.PrinterException; |
|
53 |
import java.awt.print.PrinterJob; |
|
54 |
import java.awt.Window; |
|
55 |
import java.io.File; |
|
56 |
import java.io.IOException; |
|
57 |
import java.util.ArrayList; |
|
58 |
import java.util.Enumeration; |
|
59 |
import java.util.Locale; |
|
60 |
import sun.awt.image.ByteInterleavedRaster; |
|
61 |
||
62 |
import javax.print.Doc; |
|
63 |
import javax.print.DocFlavor; |
|
64 |
import javax.print.DocPrintJob; |
|
65 |
import javax.print.PrintException; |
|
66 |
import javax.print.PrintService; |
|
67 |
import javax.print.PrintServiceLookup; |
|
68 |
import javax.print.ServiceUI; |
|
69 |
import javax.print.StreamPrintService; |
|
70 |
import javax.print.StreamPrintServiceFactory; |
|
71 |
import javax.print.attribute.Attribute; |
|
72 |
import javax.print.attribute.AttributeSet; |
|
73 |
import javax.print.attribute.HashPrintRequestAttributeSet; |
|
74 |
import javax.print.attribute.PrintRequestAttributeSet; |
|
75 |
import javax.print.attribute.Size2DSyntax; |
|
76 |
import javax.print.attribute.standard.Chromaticity; |
|
77 |
import javax.print.attribute.standard.Copies; |
|
78 |
import javax.print.attribute.standard.Destination; |
|
79 |
import javax.print.attribute.standard.DialogTypeSelection; |
|
80 |
import javax.print.attribute.standard.Fidelity; |
|
81 |
import javax.print.attribute.standard.JobName; |
|
82 |
import javax.print.attribute.standard.JobSheets; |
|
83 |
import javax.print.attribute.standard.Media; |
|
84 |
import javax.print.attribute.standard.MediaPrintableArea; |
|
85 |
import javax.print.attribute.standard.MediaSize; |
|
86 |
import javax.print.attribute.standard.MediaSizeName; |
|
87 |
import javax.print.attribute.standard.OrientationRequested; |
|
88 |
import javax.print.attribute.standard.PageRanges; |
|
89 |
import javax.print.attribute.standard.PrinterState; |
|
90 |
import javax.print.attribute.standard.PrinterStateReason; |
|
91 |
import javax.print.attribute.standard.PrinterStateReasons; |
|
92 |
import javax.print.attribute.standard.PrinterIsAcceptingJobs; |
|
93 |
import javax.print.attribute.standard.RequestingUserName; |
|
94 |
import javax.print.attribute.standard.SheetCollate; |
|
95 |
import javax.print.attribute.standard.Sides; |
|
96 |
||
97 |
import sun.print.PageableDoc; |
|
98 |
import sun.print.ServiceDialog; |
|
99 |
import sun.print.SunPrinterJobService; |
|
100 |
import sun.print.SunPageSelection; |
|
101 |
||
102 |
/** |
|
103 |
* A class which rasterizes a printer job. |
|
104 |
* |
|
105 |
* @author Richard Blanchard |
|
106 |
*/ |
|
107 |
public abstract class RasterPrinterJob extends PrinterJob { |
|
108 |
||
109 |
/* Class Constants */ |
|
110 |
||
111 |
/* Printer destination type. */ |
|
112 |
protected static final int PRINTER = 0; |
|
113 |
||
114 |
/* File destination type. */ |
|
115 |
protected static final int FILE = 1; |
|
116 |
||
117 |
/* Stream destination type. */ |
|
118 |
protected static final int STREAM = 2; |
|
119 |
||
120 |
/** |
|
121 |
* Maximum amount of memory in bytes to use for the |
|
122 |
* buffered image "band". 4Mb is a compromise between |
|
123 |
* limiting the number of bands on hi-res printers and |
|
124 |
* not using too much of the Java heap or causing paging |
|
125 |
* on systems with little RAM. |
|
126 |
*/ |
|
127 |
private static final int MAX_BAND_SIZE = (1024 * 1024 * 4); |
|
128 |
||
129 |
/* Dots Per Inch */ |
|
130 |
private static final float DPI = 72.0f; |
|
131 |
||
132 |
/** |
|
133 |
* Useful mainly for debugging, this system property |
|
134 |
* can be used to force the printing code to print |
|
135 |
* using a particular pipeline. The two currently |
|
136 |
* supported values are FORCE_RASTER and FORCE_PDL. |
|
137 |
*/ |
|
138 |
private static final String FORCE_PIPE_PROP = "sun.java2d.print.pipeline"; |
|
139 |
||
140 |
/** |
|
141 |
* When the system property FORCE_PIPE_PROP has this value |
|
142 |
* then each page of a print job will be rendered through |
|
143 |
* the raster pipeline. |
|
144 |
*/ |
|
145 |
private static final String FORCE_RASTER = "raster"; |
|
146 |
||
147 |
/** |
|
148 |
* When the system property FORCE_PIPE_PROP has this value |
|
149 |
* then each page of a print job will be rendered through |
|
150 |
* the PDL pipeline. |
|
151 |
*/ |
|
152 |
private static final String FORCE_PDL = "pdl"; |
|
153 |
||
154 |
/** |
|
155 |
* When the system property SHAPE_TEXT_PROP has this value |
|
156 |
* then text is always rendered as a shape, and no attempt is made |
|
157 |
* to match the font through GDI |
|
158 |
*/ |
|
159 |
private static final String SHAPE_TEXT_PROP = "sun.java2d.print.shapetext"; |
|
160 |
||
161 |
/** |
|
162 |
* values obtained from System properties in static initialiser block |
|
163 |
*/ |
|
164 |
public static boolean forcePDL = false; |
|
165 |
public static boolean forceRaster = false; |
|
166 |
public static boolean shapeTextProp = false; |
|
167 |
||
168 |
static { |
|
169 |
/* The system property FORCE_PIPE_PROP |
|
170 |
* can be used to force the printing code to |
|
171 |
* use a particular pipeline. Either the raster |
|
172 |
* pipeline or the pdl pipeline can be forced. |
|
173 |
*/ |
|
174 |
String forceStr = |
|
175 |
(String)java.security.AccessController.doPrivileged( |
|
176 |
new sun.security.action.GetPropertyAction(FORCE_PIPE_PROP)); |
|
177 |
||
178 |
if (forceStr != null) { |
|
179 |
if (forceStr.equalsIgnoreCase(FORCE_PDL)) { |
|
180 |
forcePDL = true; |
|
181 |
} else if (forceStr.equalsIgnoreCase(FORCE_RASTER)) { |
|
182 |
forceRaster = true; |
|
183 |
} |
|
184 |
} |
|
185 |
||
186 |
String shapeTextStr = |
|
187 |
(String)java.security.AccessController.doPrivileged( |
|
188 |
new sun.security.action.GetPropertyAction(SHAPE_TEXT_PROP)); |
|
189 |
||
190 |
if (shapeTextStr != null) { |
|
191 |
shapeTextProp = true; |
|
192 |
} |
|
193 |
} |
|
194 |
||
195 |
/* Instance Variables */ |
|
196 |
||
197 |
/** |
|
21278 | 198 |
* Used to minimize GC & reallocation of band when printing |
2 | 199 |
*/ |
200 |
private int cachedBandWidth = 0; |
|
201 |
private int cachedBandHeight = 0; |
|
202 |
private BufferedImage cachedBand = null; |
|
203 |
||
204 |
/** |
|
205 |
* The number of book copies to be printed. |
|
206 |
*/ |
|
207 |
private int mNumCopies = 1; |
|
208 |
||
209 |
/** |
|
210 |
* Collation effects the order of the pages printed |
|
211 |
* when multiple copies are requested. For two copies |
|
212 |
* of a three page document the page order is: |
|
213 |
* mCollate true: 1, 2, 3, 1, 2, 3 |
|
214 |
* mCollate false: 1, 1, 2, 2, 3, 3 |
|
215 |
*/ |
|
216 |
private boolean mCollate = false; |
|
217 |
||
218 |
/** |
|
219 |
* The zero based indices of the first and last |
|
220 |
* pages to be printed. If 'mFirstPage' is |
|
221 |
* UNDEFINED_PAGE_NUM then the first page to |
|
222 |
* be printed is page 0. If 'mLastPage' is |
|
223 |
* UNDEFINED_PAGE_NUM then the last page to |
|
224 |
* be printed is the last one in the book. |
|
225 |
*/ |
|
226 |
private int mFirstPage = Pageable.UNKNOWN_NUMBER_OF_PAGES; |
|
227 |
private int mLastPage = Pageable.UNKNOWN_NUMBER_OF_PAGES; |
|
228 |
||
229 |
/** |
|
230 |
* The previous print stream Paper |
|
231 |
* Used to check if the paper size has changed such that the |
|
232 |
* implementation needs to emit the new paper size information |
|
233 |
* into the print stream. |
|
234 |
* Since we do our own rotation, and the margins aren't relevant, |
|
235 |
* Its strictly the dimensions of the paper that we will check. |
|
236 |
*/ |
|
237 |
private Paper previousPaper; |
|
238 |
||
239 |
/** |
|
240 |
* The document to be printed. It is initialized to an |
|
241 |
* empty (zero pages) book. |
|
242 |
*/ |
|
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
243 |
// MacOSX - made protected so subclasses can reference it. |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
244 |
protected Pageable mDocument = new Book(); |
2 | 245 |
|
246 |
/** |
|
247 |
* The name of the job being printed. |
|
248 |
*/ |
|
438
2ae294e4518c
6613529: Avoid duplicate object creation within JDK packages
dav
parents:
2
diff
changeset
|
249 |
private String mDocName = "Java Printing"; |
2 | 250 |
|
251 |
||
252 |
/** |
|
253 |
* Printing cancellation flags |
|
254 |
*/ |
|
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
255 |
// MacOSX - made protected so subclasses can reference it. |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
256 |
protected boolean performingPrinting = false; |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
257 |
// MacOSX - made protected so subclasses can reference it. |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
258 |
protected boolean userCancelled = false; |
2 | 259 |
|
260 |
/** |
|
261 |
* Print to file permission variables. |
|
262 |
*/ |
|
263 |
private FilePermission printToFilePermission; |
|
264 |
||
265 |
/** |
|
266 |
* List of areas & the graphics state for redrawing |
|
267 |
*/ |
|
268 |
private ArrayList redrawList = new ArrayList(); |
|
269 |
||
270 |
||
271 |
/* variables representing values extracted from an attribute set. |
|
272 |
* These take precedence over values set on a printer job |
|
273 |
*/ |
|
274 |
private int copiesAttr; |
|
275 |
private String jobNameAttr; |
|
276 |
private String userNameAttr; |
|
277 |
private PageRanges pageRangesAttr; |
|
278 |
protected Sides sidesAttr; |
|
279 |
protected String destinationAttr; |
|
280 |
protected boolean noJobSheet = false; |
|
281 |
protected int mDestType = RasterPrinterJob.FILE; |
|
282 |
protected String mDestination = ""; |
|
283 |
protected boolean collateAttReq = false; |
|
284 |
||
285 |
/** |
|
286 |
* Device rotation flag, if it support 270, this is set to true; |
|
287 |
*/ |
|
288 |
protected boolean landscapeRotates270 = false; |
|
289 |
||
290 |
/** |
|
291 |
* attributes used by no-args page and print dialog and print method to |
|
292 |
* communicate state |
|
293 |
*/ |
|
294 |
protected PrintRequestAttributeSet attributes = null; |
|
295 |
||
296 |
/** |
|
297 |
* Class to keep state information for redrawing areas |
|
298 |
* "region" is an area at as a high a resolution as possible. |
|
299 |
* The redrawing code needs to look at sx, sy to calculate the scale |
|
300 |
* to device resolution. |
|
301 |
*/ |
|
302 |
private class GraphicsState { |
|
303 |
Rectangle2D region; // Area of page to repaint |
|
304 |
Shape theClip; // image drawing clip. |
|
305 |
AffineTransform theTransform; // to transform clip to dev coords. |
|
306 |
double sx; // X scale from region to device resolution |
|
307 |
double sy; // Y scale from region to device resolution |
|
308 |
} |
|
309 |
||
310 |
/** |
|
311 |
* Service for this job |
|
312 |
*/ |
|
313 |
protected PrintService myService; |
|
314 |
||
315 |
/* Constructors */ |
|
316 |
||
317 |
public RasterPrinterJob() |
|
318 |
{ |
|
319 |
} |
|
320 |
||
321 |
/* Abstract Methods */ |
|
322 |
||
323 |
/** |
|
324 |
* Returns the resolution in dots per inch across the width |
|
325 |
* of the page. |
|
326 |
*/ |
|
327 |
abstract protected double getXRes(); |
|
328 |
||
329 |
/** |
|
330 |
* Returns the resolution in dots per inch down the height |
|
331 |
* of the page. |
|
332 |
*/ |
|
333 |
abstract protected double getYRes(); |
|
334 |
||
335 |
/** |
|
336 |
* Must be obtained from the current printer. |
|
337 |
* Value is in device pixels. |
|
338 |
* Not adjusted for orientation of the paper. |
|
339 |
*/ |
|
340 |
abstract protected double getPhysicalPrintableX(Paper p); |
|
341 |
||
342 |
/** |
|
343 |
* Must be obtained from the current printer. |
|
344 |
* Value is in device pixels. |
|
345 |
* Not adjusted for orientation of the paper. |
|
346 |
*/ |
|
347 |
abstract protected double getPhysicalPrintableY(Paper p); |
|
348 |
||
349 |
/** |
|
350 |
* Must be obtained from the current printer. |
|
351 |
* Value is in device pixels. |
|
352 |
* Not adjusted for orientation of the paper. |
|
353 |
*/ |
|
354 |
abstract protected double getPhysicalPrintableWidth(Paper p); |
|
355 |
||
356 |
/** |
|
357 |
* Must be obtained from the current printer. |
|
358 |
* Value is in device pixels. |
|
359 |
* Not adjusted for orientation of the paper. |
|
360 |
*/ |
|
361 |
abstract protected double getPhysicalPrintableHeight(Paper p); |
|
362 |
||
363 |
/** |
|
364 |
* Must be obtained from the current printer. |
|
365 |
* Value is in device pixels. |
|
366 |
* Not adjusted for orientation of the paper. |
|
367 |
*/ |
|
368 |
abstract protected double getPhysicalPageWidth(Paper p); |
|
369 |
||
370 |
/** |
|
371 |
* Must be obtained from the current printer. |
|
372 |
* Value is in device pixels. |
|
373 |
* Not adjusted for orientation of the paper. |
|
374 |
*/ |
|
375 |
abstract protected double getPhysicalPageHeight(Paper p); |
|
376 |
||
377 |
/** |
|
378 |
* Begin a new page. |
|
379 |
*/ |
|
380 |
abstract protected void startPage(PageFormat format, Printable painter, |
|
381 |
int index, boolean paperChanged) |
|
382 |
throws PrinterException; |
|
383 |
||
384 |
/** |
|
385 |
* End a page. |
|
386 |
*/ |
|
387 |
abstract protected void endPage(PageFormat format, Printable painter, |
|
388 |
int index) |
|
389 |
throws PrinterException; |
|
390 |
||
391 |
/** |
|
392 |
* Prints the contents of the array of ints, 'data' |
|
393 |
* to the current page. The band is placed at the |
|
394 |
* location (x, y) in device coordinates on the |
|
395 |
* page. The width and height of the band is |
|
396 |
* specified by the caller. |
|
397 |
*/ |
|
398 |
abstract protected void printBand(byte[] data, int x, int y, |
|
399 |
int width, int height) |
|
400 |
throws PrinterException; |
|
401 |
||
402 |
/* Instance Methods */ |
|
403 |
||
404 |
/** |
|
405 |
* save graphics state of a PathGraphics for later redrawing |
|
406 |
* of part of page represented by the region in that state |
|
407 |
*/ |
|
408 |
||
409 |
public void saveState(AffineTransform at, Shape clip, |
|
410 |
Rectangle2D region, double sx, double sy) { |
|
411 |
GraphicsState gstate = new GraphicsState(); |
|
412 |
gstate.theTransform = at; |
|
413 |
gstate.theClip = clip; |
|
414 |
gstate.region = region; |
|
415 |
gstate.sx = sx; |
|
416 |
gstate.sy = sy; |
|
417 |
redrawList.add(gstate); |
|
418 |
} |
|
419 |
||
420 |
||
421 |
/* |
|
422 |
* A convenience method which returns the default service |
|
423 |
* for 2D <code>PrinterJob</code>s. |
|
424 |
* May return null if there is no suitable default (although there |
|
425 |
* may still be 2D services available). |
|
426 |
* @return default 2D print service, or null. |
|
427 |
* @since 1.4 |
|
428 |
*/ |
|
429 |
protected static PrintService lookupDefaultPrintService() { |
|
430 |
PrintService service = PrintServiceLookup.lookupDefaultPrintService(); |
|
431 |
||
432 |
/* Pageable implies Printable so checking both isn't strictly needed */ |
|
433 |
if (service != null && |
|
434 |
service.isDocFlavorSupported( |
|
435 |
DocFlavor.SERVICE_FORMATTED.PAGEABLE) && |
|
436 |
service.isDocFlavorSupported( |
|
437 |
DocFlavor.SERVICE_FORMATTED.PRINTABLE)) { |
|
438 |
return service; |
|
439 |
} else { |
|
440 |
PrintService []services = |
|
441 |
PrintServiceLookup.lookupPrintServices( |
|
442 |
DocFlavor.SERVICE_FORMATTED.PAGEABLE, null); |
|
443 |
if (services.length > 0) { |
|
444 |
return services[0]; |
|
445 |
} |
|
446 |
} |
|
447 |
return null; |
|
448 |
} |
|
449 |
||
450 |
/** |
|
451 |
* Returns the service (printer) for this printer job. |
|
452 |
* Implementations of this class which do not support print services |
|
453 |
* may return null; |
|
454 |
* @return the service for this printer job. |
|
455 |
* |
|
456 |
*/ |
|
457 |
public PrintService getPrintService() { |
|
458 |
if (myService == null) { |
|
459 |
PrintService svc = PrintServiceLookup.lookupDefaultPrintService(); |
|
460 |
if (svc != null && |
|
461 |
svc.isDocFlavorSupported( |
|
462 |
DocFlavor.SERVICE_FORMATTED.PAGEABLE)) { |
|
463 |
try { |
|
464 |
setPrintService(svc); |
|
465 |
myService = svc; |
|
466 |
} catch (PrinterException e) { |
|
467 |
} |
|
468 |
} |
|
469 |
if (myService == null) { |
|
470 |
PrintService[] svcs = PrintServiceLookup.lookupPrintServices( |
|
471 |
DocFlavor.SERVICE_FORMATTED.PAGEABLE, null); |
|
472 |
if (svcs.length > 0) { |
|
473 |
try { |
|
474 |
setPrintService(svcs[0]); |
|
475 |
myService = svcs[0]; |
|
476 |
} catch (PrinterException e) { |
|
477 |
} |
|
478 |
} |
|
479 |
} |
|
480 |
} |
|
481 |
return myService; |
|
482 |
} |
|
483 |
||
484 |
/** |
|
485 |
* Associate this PrinterJob with a new PrintService. |
|
486 |
* |
|
487 |
* Throws <code>PrinterException</code> if the specified service |
|
488 |
* cannot support the <code>Pageable</code> and |
|
489 |
* <code>Printable</code> interfaces necessary to support 2D printing. |
|
490 |
* @param a print service which supports 2D printing. |
|
491 |
* |
|
492 |
* @throws PrinterException if the specified service does not support |
|
493 |
* 2D printing or no longer available. |
|
494 |
*/ |
|
495 |
public void setPrintService(PrintService service) |
|
496 |
throws PrinterException { |
|
497 |
if (service == null) { |
|
498 |
throw new PrinterException("Service cannot be null"); |
|
499 |
} else if (!(service instanceof StreamPrintService) && |
|
500 |
service.getName() == null) { |
|
501 |
throw new PrinterException("Null PrintService name."); |
|
502 |
} else { |
|
503 |
// Check the list of services. This service may have been |
|
504 |
// deleted already |
|
505 |
PrinterState prnState = (PrinterState)service.getAttribute( |
|
506 |
PrinterState.class); |
|
507 |
if (prnState == PrinterState.STOPPED) { |
|
508 |
PrinterStateReasons prnStateReasons = |
|
509 |
(PrinterStateReasons)service.getAttribute( |
|
510 |
PrinterStateReasons.class); |
|
511 |
if ((prnStateReasons != null) && |
|
512 |
(prnStateReasons.containsKey(PrinterStateReason.SHUTDOWN))) |
|
513 |
{ |
|
514 |
throw new PrinterException("PrintService is no longer available."); |
|
515 |
} |
|
516 |
} |
|
517 |
||
518 |
||
519 |
if (service.isDocFlavorSupported( |
|
520 |
DocFlavor.SERVICE_FORMATTED.PAGEABLE) && |
|
521 |
service.isDocFlavorSupported( |
|
522 |
DocFlavor.SERVICE_FORMATTED.PRINTABLE)) { |
|
523 |
myService = service; |
|
524 |
} else { |
|
525 |
throw new PrinterException("Not a 2D print service: " + service); |
|
526 |
} |
|
527 |
} |
|
528 |
} |
|
529 |
||
15969
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
530 |
private PageFormat attributeToPageFormat(PrintService service, |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
531 |
PrintRequestAttributeSet attSet) { |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
532 |
PageFormat page = defaultPage(); |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
533 |
|
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
534 |
if (service == null) { |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
535 |
return page; |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
536 |
} |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
537 |
|
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
538 |
OrientationRequested orient = (OrientationRequested) |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
539 |
attSet.get(OrientationRequested.class); |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
540 |
if (orient == null) { |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
541 |
orient = (OrientationRequested) |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
542 |
service.getDefaultAttributeValue(OrientationRequested.class); |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
543 |
} |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
544 |
if (orient == OrientationRequested.REVERSE_LANDSCAPE) { |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
545 |
page.setOrientation(PageFormat.REVERSE_LANDSCAPE); |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
546 |
} else if (orient == OrientationRequested.LANDSCAPE) { |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
547 |
page.setOrientation(PageFormat.LANDSCAPE); |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
548 |
} else { |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
549 |
page.setOrientation(PageFormat.PORTRAIT); |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
550 |
} |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
551 |
|
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
552 |
Media media = (Media)attSet.get(Media.class); |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
553 |
if (media == null) { |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
554 |
media = |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
555 |
(Media)service.getDefaultAttributeValue(Media.class); |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
556 |
} |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
557 |
if (!(media instanceof MediaSizeName)) { |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
558 |
media = MediaSizeName.NA_LETTER; |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
559 |
} |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
560 |
MediaSize size = |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
561 |
MediaSize.getMediaSizeForName((MediaSizeName)media); |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
562 |
if (size == null) { |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
563 |
size = MediaSize.NA.LETTER; |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
564 |
} |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
565 |
Paper paper = new Paper(); |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
566 |
float dim[] = size.getSize(1); //units == 1 to avoid FP error |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
567 |
double w = Math.rint((dim[0]*72.0)/Size2DSyntax.INCH); |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
568 |
double h = Math.rint((dim[1]*72.0)/Size2DSyntax.INCH); |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
569 |
paper.setSize(w, h); |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
570 |
MediaPrintableArea area = |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
571 |
(MediaPrintableArea) |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
572 |
attSet.get(MediaPrintableArea.class); |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
573 |
double ix, iw, iy, ih; |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
574 |
|
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
575 |
if (area != null) { |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
576 |
// Should pass in same unit as updatePageAttributes |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
577 |
// to avoid rounding off errors. |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
578 |
ix = Math.rint( |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
579 |
area.getX(MediaPrintableArea.INCH) * DPI); |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
580 |
iy = Math.rint( |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
581 |
area.getY(MediaPrintableArea.INCH) * DPI); |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
582 |
iw = Math.rint( |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
583 |
area.getWidth(MediaPrintableArea.INCH) * DPI); |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
584 |
ih = Math.rint( |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
585 |
area.getHeight(MediaPrintableArea.INCH) * DPI); |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
586 |
} |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
587 |
else { |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
588 |
if (w >= 72.0 * 6.0) { |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
589 |
ix = 72.0; |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
590 |
iw = w - 2 * 72.0; |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
591 |
} else { |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
592 |
ix = w / 6.0; |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
593 |
iw = w * 0.75; |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
594 |
} |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
595 |
if (h >= 72.0 * 6.0) { |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
596 |
iy = 72.0; |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
597 |
ih = h - 2 * 72.0; |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
598 |
} else { |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
599 |
iy = h / 6.0; |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
600 |
ih = h * 0.75; |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
601 |
} |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
602 |
} |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
603 |
paper.setImageableArea(ix, iy, iw, ih); |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
604 |
page.setPaper(paper); |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
605 |
return page; |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
606 |
} |
2 | 607 |
|
608 |
protected void updatePageAttributes(PrintService service, |
|
609 |
PageFormat page) { |
|
17405
9071dd38156b
8011069: Printing: NullPointerException since jdk8 b82 showing native Page Setup Dialog.
jgodinez
parents:
15969
diff
changeset
|
610 |
if (this.attributes == null) { |
9071dd38156b
8011069: Printing: NullPointerException since jdk8 b82 showing native Page Setup Dialog.
jgodinez
parents:
15969
diff
changeset
|
611 |
this.attributes = new HashPrintRequestAttributeSet(); |
9071dd38156b
8011069: Printing: NullPointerException since jdk8 b82 showing native Page Setup Dialog.
jgodinez
parents:
15969
diff
changeset
|
612 |
} |
9071dd38156b
8011069: Printing: NullPointerException since jdk8 b82 showing native Page Setup Dialog.
jgodinez
parents:
15969
diff
changeset
|
613 |
|
15969
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
614 |
updateAttributesWithPageFormat(service, page, this.attributes); |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
615 |
} |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
616 |
|
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
617 |
protected void updateAttributesWithPageFormat(PrintService service, |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
618 |
PageFormat page, |
17405
9071dd38156b
8011069: Printing: NullPointerException since jdk8 b82 showing native Page Setup Dialog.
jgodinez
parents:
15969
diff
changeset
|
619 |
PrintRequestAttributeSet pageAttributes) { |
9071dd38156b
8011069: Printing: NullPointerException since jdk8 b82 showing native Page Setup Dialog.
jgodinez
parents:
15969
diff
changeset
|
620 |
if (service == null || page == null || pageAttributes == null) { |
2 | 621 |
return; |
622 |
} |
|
623 |
||
624 |
float x = (float)Math.rint( |
|
625 |
(page.getPaper().getWidth()*Size2DSyntax.INCH)/ |
|
626 |
(72.0))/(float)Size2DSyntax.INCH; |
|
627 |
float y = (float)Math.rint( |
|
628 |
(page.getPaper().getHeight()*Size2DSyntax.INCH)/ |
|
629 |
(72.0))/(float)Size2DSyntax.INCH; |
|
630 |
||
631 |
// We should limit the list where we search the matching |
|
632 |
// media, this will prevent mapping to wrong media ex. Ledger |
|
633 |
// can be mapped to B. Especially useful when creating |
|
634 |
// custom MediaSize. |
|
635 |
Media[] mediaList = (Media[])service.getSupportedAttributeValues( |
|
636 |
Media.class, null, null); |
|
637 |
Media media = null; |
|
638 |
try { |
|
639 |
media = CustomMediaSizeName.findMedia(mediaList, x, y, |
|
640 |
Size2DSyntax.INCH); |
|
641 |
} catch (IllegalArgumentException iae) { |
|
642 |
} |
|
643 |
if ((media == null) || |
|
644 |
!(service.isAttributeValueSupported(media, null, null))) { |
|
645 |
media = (Media)service.getDefaultAttributeValue(Media.class); |
|
646 |
} |
|
647 |
||
648 |
OrientationRequested orient; |
|
649 |
switch (page.getOrientation()) { |
|
650 |
case PageFormat.LANDSCAPE : |
|
651 |
orient = OrientationRequested.LANDSCAPE; |
|
652 |
break; |
|
653 |
case PageFormat.REVERSE_LANDSCAPE: |
|
654 |
orient = OrientationRequested.REVERSE_LANDSCAPE; |
|
655 |
break; |
|
656 |
default: |
|
657 |
orient = OrientationRequested.PORTRAIT; |
|
658 |
} |
|
659 |
||
17405
9071dd38156b
8011069: Printing: NullPointerException since jdk8 b82 showing native Page Setup Dialog.
jgodinez
parents:
15969
diff
changeset
|
660 |
if (media != null) { |
9071dd38156b
8011069: Printing: NullPointerException since jdk8 b82 showing native Page Setup Dialog.
jgodinez
parents:
15969
diff
changeset
|
661 |
pageAttributes.add(media); |
2 | 662 |
} |
17405
9071dd38156b
8011069: Printing: NullPointerException since jdk8 b82 showing native Page Setup Dialog.
jgodinez
parents:
15969
diff
changeset
|
663 |
pageAttributes.add(orient); |
2 | 664 |
|
665 |
float ix = (float)(page.getPaper().getImageableX()/DPI); |
|
666 |
float iw = (float)(page.getPaper().getImageableWidth()/DPI); |
|
667 |
float iy = (float)(page.getPaper().getImageableY()/DPI); |
|
668 |
float ih = (float)(page.getPaper().getImageableHeight()/DPI); |
|
669 |
if (ix < 0) ix = 0f; if (iy < 0) iy = 0f; |
|
670 |
try { |
|
17405
9071dd38156b
8011069: Printing: NullPointerException since jdk8 b82 showing native Page Setup Dialog.
jgodinez
parents:
15969
diff
changeset
|
671 |
pageAttributes.add(new MediaPrintableArea(ix, iy, iw, ih, |
2 | 672 |
MediaPrintableArea.INCH)); |
673 |
} catch (IllegalArgumentException iae) { |
|
674 |
} |
|
675 |
} |
|
676 |
||
677 |
/** |
|
678 |
* Display a dialog to the user allowing the modification of a |
|
679 |
* PageFormat instance. |
|
680 |
* The <code>page</code> argument is used to initialize controls |
|
681 |
* in the page setup dialog. |
|
682 |
* If the user cancels the dialog, then the method returns the |
|
683 |
* original <code>page</code> object unmodified. |
|
684 |
* If the user okays the dialog then the method returns a new |
|
685 |
* PageFormat object with the indicated changes. |
|
686 |
* In either case the original <code>page</code> object will |
|
687 |
* not be modified. |
|
688 |
* @param page the default PageFormat presented to the user |
|
689 |
* for modification |
|
690 |
* @return the original <code>page</code> object if the dialog |
|
691 |
* is cancelled, or a new PageFormat object containing |
|
692 |
* the format indicated by the user if the dialog is |
|
693 |
* acknowledged |
|
694 |
* @exception HeadlessException if GraphicsEnvironment.isHeadless() |
|
695 |
* returns true. |
|
696 |
* @see java.awt.GraphicsEnvironment#isHeadless |
|
697 |
* @since 1.2 |
|
698 |
*/ |
|
699 |
public PageFormat pageDialog(PageFormat page) |
|
700 |
throws HeadlessException { |
|
701 |
if (GraphicsEnvironment.isHeadless()) { |
|
702 |
throw new HeadlessException(); |
|
703 |
} |
|
704 |
||
705 |
final GraphicsConfiguration gc = |
|
706 |
GraphicsEnvironment.getLocalGraphicsEnvironment(). |
|
707 |
getDefaultScreenDevice().getDefaultConfiguration(); |
|
708 |
||
709 |
PrintService service = |
|
710 |
(PrintService)java.security.AccessController.doPrivileged( |
|
711 |
new java.security.PrivilegedAction() { |
|
712 |
public Object run() { |
|
713 |
PrintService service = getPrintService(); |
|
714 |
if (service == null) { |
|
715 |
ServiceDialog.showNoPrintService(gc); |
|
716 |
return null; |
|
717 |
} |
|
718 |
return service; |
|
719 |
} |
|
720 |
}); |
|
721 |
||
722 |
if (service == null) { |
|
723 |
return page; |
|
724 |
} |
|
725 |
updatePageAttributes(service, page); |
|
726 |
||
727 |
PageFormat newPage = pageDialog(attributes); |
|
728 |
||
729 |
if (newPage == null) { |
|
730 |
return page; |
|
731 |
} else { |
|
732 |
return newPage; |
|
733 |
} |
|
734 |
} |
|
735 |
||
736 |
/** |
|
737 |
* return a PageFormat corresponding to the updated attributes, |
|
738 |
* or null if the user cancelled the dialog. |
|
739 |
*/ |
|
740 |
public PageFormat pageDialog(final PrintRequestAttributeSet attributes) |
|
741 |
throws HeadlessException { |
|
742 |
if (GraphicsEnvironment.isHeadless()) { |
|
743 |
throw new HeadlessException(); |
|
744 |
} |
|
745 |
||
15969
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
746 |
DialogTypeSelection dlg = |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
747 |
(DialogTypeSelection)attributes.get(DialogTypeSelection.class); |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
748 |
|
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
749 |
// Check for native, note that default dialog is COMMON. |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
750 |
if (dlg == DialogTypeSelection.NATIVE) { |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
751 |
PrintService pservice = getPrintService(); |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
752 |
PageFormat page = pageDialog(attributeToPageFormat(pservice, |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
753 |
attributes)); |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
754 |
updateAttributesWithPageFormat(pservice, page, attributes); |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
755 |
return page; |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
756 |
} |
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
757 |
|
2 | 758 |
final GraphicsConfiguration gc = |
759 |
GraphicsEnvironment.getLocalGraphicsEnvironment(). |
|
760 |
getDefaultScreenDevice().getDefaultConfiguration(); |
|
761 |
Rectangle bounds = gc.getBounds(); |
|
762 |
int x = bounds.x+bounds.width/3; |
|
763 |
int y = bounds.y+bounds.height/3; |
|
764 |
||
765 |
PrintService service = |
|
766 |
(PrintService)java.security.AccessController.doPrivileged( |
|
767 |
new java.security.PrivilegedAction() { |
|
768 |
public Object run() { |
|
769 |
PrintService service = getPrintService(); |
|
770 |
if (service == null) { |
|
771 |
ServiceDialog.showNoPrintService(gc); |
|
772 |
return null; |
|
773 |
} |
|
774 |
return service; |
|
775 |
} |
|
776 |
}); |
|
777 |
||
778 |
if (service == null) { |
|
779 |
return null; |
|
780 |
} |
|
781 |
||
782 |
ServiceDialog pageDialog = new ServiceDialog(gc, x, y, service, |
|
783 |
DocFlavor.SERVICE_FORMATTED.PAGEABLE, |
|
784 |
attributes, (Frame)null); |
|
785 |
pageDialog.show(); |
|
786 |
||
787 |
if (pageDialog.getStatus() == ServiceDialog.APPROVE) { |
|
788 |
PrintRequestAttributeSet newas = |
|
789 |
pageDialog.getAttributes(); |
|
790 |
Class amCategory = SunAlternateMedia.class; |
|
791 |
||
792 |
if (attributes.containsKey(amCategory) && |
|
793 |
!newas.containsKey(amCategory)) { |
|
794 |
attributes.remove(amCategory); |
|
795 |
} |
|
796 |
attributes.addAll(newas); |
|
15969
c7ed36e13460
8006110: pageDialog is showing the swing dialog with DialogTypeSelection.NATIVE
jgodinez
parents:
14342
diff
changeset
|
797 |
return attributeToPageFormat(service, attributes); |
2 | 798 |
} else { |
799 |
return null; |
|
800 |
} |
|
801 |
} |
|
802 |
||
803 |
/** |
|
804 |
* Presents the user a dialog for changing properties of the |
|
805 |
* print job interactively. |
|
806 |
* The services browsable here are determined by the type of |
|
807 |
* service currently installed. |
|
808 |
* If the application installed a StreamPrintService on this |
|
809 |
* PrinterJob, only the available StreamPrintService (factories) are |
|
810 |
* browsable. |
|
811 |
* |
|
812 |
* @param attributes to store changed properties. |
|
813 |
* @return false if the user cancels the dialog and true otherwise. |
|
814 |
* @exception HeadlessException if GraphicsEnvironment.isHeadless() |
|
815 |
* returns true. |
|
816 |
* @see java.awt.GraphicsEnvironment#isHeadless |
|
817 |
*/ |
|
818 |
public boolean printDialog(final PrintRequestAttributeSet attributes) |
|
819 |
throws HeadlessException { |
|
820 |
if (GraphicsEnvironment.isHeadless()) { |
|
821 |
throw new HeadlessException(); |
|
822 |
} |
|
823 |
||
824 |
DialogTypeSelection dlg = |
|
825 |
(DialogTypeSelection)attributes.get(DialogTypeSelection.class); |
|
826 |
||
827 |
// Check for native, note that default dialog is COMMON. |
|
828 |
if (dlg == DialogTypeSelection.NATIVE) { |
|
829 |
this.attributes = attributes; |
|
830 |
try { |
|
831 |
debug_println("calling setAttributes in printDialog"); |
|
832 |
setAttributes(attributes); |
|
833 |
||
834 |
} catch (PrinterException e) { |
|
835 |
||
836 |
} |
|
837 |
||
838 |
boolean ret = printDialog(); |
|
839 |
this.attributes = attributes; |
|
840 |
return ret; |
|
841 |
||
842 |
} |
|
843 |
||
844 |
/* A security check has already been performed in the |
|
845 |
* java.awt.print.printerJob.getPrinterJob method. |
|
846 |
* So by the time we get here, it is OK for the current thread |
|
847 |
* to print either to a file (from a Dialog we control!) or |
|
848 |
* to a chosen printer. |
|
849 |
* |
|
850 |
* We raise privilege when we put up the dialog, to avoid |
|
851 |
* the "warning applet window" banner. |
|
852 |
*/ |
|
853 |
final GraphicsConfiguration gc = |
|
854 |
GraphicsEnvironment.getLocalGraphicsEnvironment(). |
|
855 |
getDefaultScreenDevice().getDefaultConfiguration(); |
|
856 |
||
857 |
PrintService service = |
|
858 |
(PrintService)java.security.AccessController.doPrivileged( |
|
859 |
new java.security.PrivilegedAction() { |
|
860 |
public Object run() { |
|
861 |
PrintService service = getPrintService(); |
|
862 |
if (service == null) { |
|
863 |
ServiceDialog.showNoPrintService(gc); |
|
864 |
return null; |
|
865 |
} |
|
866 |
return service; |
|
867 |
} |
|
868 |
}); |
|
869 |
||
870 |
if (service == null) { |
|
871 |
return false; |
|
872 |
} |
|
873 |
||
874 |
PrintService[] services; |
|
875 |
StreamPrintServiceFactory[] spsFactories = null; |
|
876 |
if (service instanceof StreamPrintService) { |
|
877 |
spsFactories = lookupStreamPrintServices(null); |
|
878 |
services = new StreamPrintService[spsFactories.length]; |
|
879 |
for (int i=0; i<spsFactories.length; i++) { |
|
880 |
services[i] = spsFactories[i].getPrintService(null); |
|
881 |
} |
|
882 |
} else { |
|
883 |
services = |
|
884 |
(PrintService[])java.security.AccessController.doPrivileged( |
|
885 |
new java.security.PrivilegedAction() { |
|
886 |
public Object run() { |
|
887 |
PrintService[] services = PrinterJob.lookupPrintServices(); |
|
888 |
return services; |
|
889 |
} |
|
890 |
}); |
|
891 |
||
892 |
if ((services == null) || (services.length == 0)) { |
|
893 |
/* |
|
894 |
* No services but default PrintService exists? |
|
895 |
* Create services using defaultService. |
|
896 |
*/ |
|
897 |
services = new PrintService[1]; |
|
898 |
services[0] = service; |
|
899 |
} |
|
900 |
} |
|
901 |
||
902 |
Rectangle bounds = gc.getBounds(); |
|
903 |
int x = bounds.x+bounds.width/3; |
|
904 |
int y = bounds.y+bounds.height/3; |
|
905 |
PrintService newService; |
|
19781
91b06f016821
4673406: RFE: Java Printing: Provide a way to display win32 printer driver's dialog
prr
parents:
17405
diff
changeset
|
906 |
// temporarily add an attribute pointing back to this job. |
91b06f016821
4673406: RFE: Java Printing: Provide a way to display win32 printer driver's dialog
prr
parents:
17405
diff
changeset
|
907 |
PrinterJobWrapper jobWrapper = new PrinterJobWrapper(this); |
91b06f016821
4673406: RFE: Java Printing: Provide a way to display win32 printer driver's dialog
prr
parents:
17405
diff
changeset
|
908 |
attributes.add(jobWrapper); |
2 | 909 |
try { |
910 |
newService = |
|
911 |
ServiceUI.printDialog(gc, x, y, |
|
912 |
services, service, |
|
913 |
DocFlavor.SERVICE_FORMATTED.PAGEABLE, |
|
914 |
attributes); |
|
915 |
} catch (IllegalArgumentException iae) { |
|
916 |
newService = ServiceUI.printDialog(gc, x, y, |
|
917 |
services, services[0], |
|
918 |
DocFlavor.SERVICE_FORMATTED.PAGEABLE, |
|
919 |
attributes); |
|
920 |
} |
|
19781
91b06f016821
4673406: RFE: Java Printing: Provide a way to display win32 printer driver's dialog
prr
parents:
17405
diff
changeset
|
921 |
attributes.remove(PrinterJobWrapper.class); |
2 | 922 |
|
923 |
if (newService == null) { |
|
924 |
return false; |
|
925 |
} |
|
926 |
||
927 |
if (!service.equals(newService)) { |
|
928 |
try { |
|
929 |
setPrintService(newService); |
|
930 |
} catch (PrinterException e) { |
|
931 |
/* |
|
932 |
* The only time it would throw an exception is when |
|
933 |
* newService is no longer available but we should still |
|
934 |
* select this printer. |
|
935 |
*/ |
|
936 |
myService = newService; |
|
937 |
} |
|
938 |
} |
|
939 |
return true; |
|
940 |
} |
|
941 |
||
942 |
/** |
|
943 |
* Presents the user a dialog for changing properties of the |
|
944 |
* print job interactively. |
|
945 |
* @returns false if the user cancels the dialog and |
|
946 |
* true otherwise. |
|
947 |
* @exception HeadlessException if GraphicsEnvironment.isHeadless() |
|
948 |
* returns true. |
|
949 |
* @see java.awt.GraphicsEnvironment#isHeadless |
|
950 |
*/ |
|
951 |
public boolean printDialog() throws HeadlessException { |
|
952 |
||
953 |
if (GraphicsEnvironment.isHeadless()) { |
|
954 |
throw new HeadlessException(); |
|
955 |
} |
|
956 |
||
957 |
PrintRequestAttributeSet attributes = |
|
958 |
new HashPrintRequestAttributeSet(); |
|
959 |
attributes.add(new Copies(getCopies())); |
|
960 |
attributes.add(new JobName(getJobName(), null)); |
|
961 |
boolean doPrint = printDialog(attributes); |
|
962 |
if (doPrint) { |
|
963 |
JobName jobName = (JobName)attributes.get(JobName.class); |
|
964 |
if (jobName != null) { |
|
965 |
setJobName(jobName.getValue()); |
|
966 |
} |
|
967 |
Copies copies = (Copies)attributes.get(Copies.class); |
|
968 |
if (copies != null) { |
|
969 |
setCopies(copies.getValue()); |
|
970 |
} |
|
971 |
||
972 |
Destination dest = (Destination)attributes.get(Destination.class); |
|
973 |
||
974 |
if (dest != null) { |
|
975 |
try { |
|
976 |
mDestType = RasterPrinterJob.FILE; |
|
977 |
mDestination = (new File(dest.getURI())).getPath(); |
|
978 |
} catch (Exception e) { |
|
979 |
mDestination = "out.prn"; |
|
980 |
PrintService ps = getPrintService(); |
|
981 |
if (ps != null) { |
|
982 |
Destination defaultDest = (Destination)ps. |
|
983 |
getDefaultAttributeValue(Destination.class); |
|
984 |
if (defaultDest != null) { |
|
985 |
mDestination = (new File(defaultDest.getURI())).getPath(); |
|
986 |
} |
|
987 |
} |
|
988 |
} |
|
989 |
} else { |
|
990 |
mDestType = RasterPrinterJob.PRINTER; |
|
991 |
PrintService ps = getPrintService(); |
|
992 |
if (ps != null) { |
|
993 |
mDestination = ps.getName(); |
|
994 |
} |
|
995 |
} |
|
996 |
} |
|
997 |
||
998 |
return doPrint; |
|
999 |
} |
|
1000 |
||
1001 |
/** |
|
1002 |
* The pages in the document to be printed by this PrinterJob |
|
1003 |
* are drawn by the Printable object 'painter'. The PageFormat |
|
1004 |
* for each page is the default page format. |
|
1005 |
* @param Printable Called to render each page of the document. |
|
1006 |
*/ |
|
1007 |
public void setPrintable(Printable painter) { |
|
1008 |
setPageable(new OpenBook(defaultPage(new PageFormat()), painter)); |
|
1009 |
} |
|
1010 |
||
1011 |
/** |
|
1012 |
* The pages in the document to be printed by this PrinterJob |
|
1013 |
* are drawn by the Printable object 'painter'. The PageFormat |
|
1014 |
* of each page is 'format'. |
|
1015 |
* @param Printable Called to render each page of the document. |
|
1016 |
* @param PageFormat The size and orientation of each page to |
|
1017 |
* be printed. |
|
1018 |
*/ |
|
1019 |
public void setPrintable(Printable painter, PageFormat format) { |
|
1020 |
setPageable(new OpenBook(format, painter)); |
|
1021 |
updatePageAttributes(getPrintService(), format); |
|
1022 |
} |
|
1023 |
||
1024 |
/** |
|
1025 |
* The pages in the document to be printed are held by the |
|
1026 |
* Pageable instance 'document'. 'document' will be queried |
|
1027 |
* for the number of pages as well as the PageFormat and |
|
1028 |
* Printable for each page. |
|
1029 |
* @param Pageable The document to be printed. It may not be null. |
|
1030 |
* @exception NullPointerException the Pageable passed in was null. |
|
1031 |
* @see PageFormat |
|
1032 |
* @see Printable |
|
1033 |
*/ |
|
1034 |
public void setPageable(Pageable document) throws NullPointerException { |
|
1035 |
if (document != null) { |
|
1036 |
mDocument = document; |
|
1037 |
||
1038 |
} else { |
|
1039 |
throw new NullPointerException(); |
|
1040 |
} |
|
1041 |
} |
|
1042 |
||
1043 |
protected void initPrinter() { |
|
1044 |
return; |
|
1045 |
} |
|
1046 |
||
1047 |
protected boolean isSupportedValue(Attribute attrval, |
|
1048 |
PrintRequestAttributeSet attrset) { |
|
1049 |
PrintService ps = getPrintService(); |
|
1050 |
return |
|
1051 |
(attrval != null && ps != null && |
|
1052 |
ps.isAttributeValueSupported(attrval, |
|
1053 |
DocFlavor.SERVICE_FORMATTED.PAGEABLE, |
|
1054 |
attrset)); |
|
1055 |
} |
|
1056 |
||
1057 |
/* subclasses may need to pull extra information out of the attribute set |
|
1058 |
* They can override this method & call super.setAttributes() |
|
1059 |
*/ |
|
1060 |
protected void setAttributes(PrintRequestAttributeSet attributes) |
|
1061 |
throws PrinterException { |
|
1062 |
/* reset all values to defaults */ |
|
1063 |
setCollated(false); |
|
1064 |
sidesAttr = null; |
|
1065 |
pageRangesAttr = null; |
|
1066 |
copiesAttr = 0; |
|
1067 |
jobNameAttr = null; |
|
1068 |
userNameAttr = null; |
|
1069 |
destinationAttr = null; |
|
1070 |
collateAttReq = false; |
|
1071 |
||
1072 |
PrintService service = getPrintService(); |
|
1073 |
if (attributes == null || service == null) { |
|
1074 |
return; |
|
1075 |
} |
|
1076 |
||
1077 |
boolean fidelity = false; |
|
1078 |
Fidelity attrFidelity = (Fidelity)attributes.get(Fidelity.class); |
|
1079 |
if (attrFidelity != null && attrFidelity == Fidelity.FIDELITY_TRUE) { |
|
1080 |
fidelity = true; |
|
1081 |
} |
|
1082 |
||
1083 |
if (fidelity == true) { |
|
1084 |
AttributeSet unsupported = |
|
1085 |
service.getUnsupportedAttributes( |
|
1086 |
DocFlavor.SERVICE_FORMATTED.PAGEABLE, |
|
1087 |
attributes); |
|
1088 |
if (unsupported != null) { |
|
1089 |
throw new PrinterException("Fidelity cannot be satisfied"); |
|
1090 |
} |
|
1091 |
} |
|
1092 |
||
1093 |
/* |
|
1094 |
* Since we have verified supported values if fidelity is true, |
|
1095 |
* we can either ignore unsupported values, or substitute a |
|
1096 |
* reasonable alternative |
|
1097 |
*/ |
|
1098 |
||
1099 |
SheetCollate collateAttr = |
|
1100 |
(SheetCollate)attributes.get(SheetCollate.class); |
|
1101 |
if (isSupportedValue(collateAttr, attributes)) { |
|
1102 |
setCollated(collateAttr == SheetCollate.COLLATED); |
|
1103 |
} |
|
1104 |
||
1105 |
sidesAttr = (Sides)attributes.get(Sides.class); |
|
1106 |
if (!isSupportedValue(sidesAttr, attributes)) { |
|
1107 |
sidesAttr = Sides.ONE_SIDED; |
|
1108 |
} |
|
1109 |
||
1110 |
pageRangesAttr = (PageRanges)attributes.get(PageRanges.class); |
|
1111 |
if (!isSupportedValue(pageRangesAttr, attributes)) { |
|
1112 |
pageRangesAttr = null; |
|
1113 |
} else { |
|
1114 |
if ((SunPageSelection)attributes.get(SunPageSelection.class) |
|
1115 |
== SunPageSelection.RANGE) { |
|
1116 |
// get to, from, min, max page ranges |
|
1117 |
int[][] range = pageRangesAttr.getMembers(); |
|
1118 |
// setPageRanges uses 0-based indexing so we subtract 1 |
|
1119 |
setPageRange(range[0][0] - 1, range[0][1] - 1); |
|
1120 |
} else { |
|
1121 |
setPageRange(-1, - 1); |
|
1122 |
} |
|
1123 |
} |
|
1124 |
||
1125 |
Copies copies = (Copies)attributes.get(Copies.class); |
|
1126 |
if (isSupportedValue(copies, attributes) || |
|
1127 |
(!fidelity && copies != null)) { |
|
1128 |
copiesAttr = copies.getValue(); |
|
1129 |
setCopies(copiesAttr); |
|
1130 |
} else { |
|
1131 |
copiesAttr = getCopies(); |
|
1132 |
} |
|
1133 |
||
1134 |
Destination destination = |
|
1135 |
(Destination)attributes.get(Destination.class); |
|
1136 |
||
1137 |
if (isSupportedValue(destination, attributes)) { |
|
1138 |
try { |
|
1139 |
// Old code (new File(destination.getURI())).getPath() |
|
1140 |
// would generate a "URI is not hierarchical" IAE |
|
1141 |
// for "file:out.prn" so we use getSchemeSpecificPart instead |
|
1142 |
destinationAttr = "" + new File(destination.getURI(). |
|
1143 |
getSchemeSpecificPart()); |
|
1144 |
} catch (Exception e) { // paranoid exception |
|
1145 |
Destination defaultDest = (Destination)service. |
|
1146 |
getDefaultAttributeValue(Destination.class); |
|
1147 |
if (defaultDest != null) { |
|
1148 |
destinationAttr = "" + new File(defaultDest.getURI(). |
|
1149 |
getSchemeSpecificPart()); |
|
1150 |
} |
|
1151 |
} |
|
1152 |
} |
|
1153 |
||
1154 |
JobSheets jobSheets = (JobSheets)attributes.get(JobSheets.class); |
|
1155 |
if (jobSheets != null) { |
|
1156 |
noJobSheet = jobSheets == JobSheets.NONE; |
|
1157 |
} |
|
1158 |
||
1159 |
JobName jobName = (JobName)attributes.get(JobName.class); |
|
1160 |
if (isSupportedValue(jobName, attributes) || |
|
1161 |
(!fidelity && jobName != null)) { |
|
1162 |
jobNameAttr = jobName.getValue(); |
|
1163 |
setJobName(jobNameAttr); |
|
1164 |
} else { |
|
1165 |
jobNameAttr = getJobName(); |
|
1166 |
} |
|
1167 |
||
1168 |
RequestingUserName userName = |
|
1169 |
(RequestingUserName)attributes.get(RequestingUserName.class); |
|
1170 |
if (isSupportedValue(userName, attributes) || |
|
1171 |
(!fidelity && userName != null)) { |
|
1172 |
userNameAttr = userName.getValue(); |
|
1173 |
} else { |
|
1174 |
try { |
|
1175 |
userNameAttr = getUserName(); |
|
1176 |
} catch (SecurityException e) { |
|
1177 |
userNameAttr = ""; |
|
1178 |
} |
|
1179 |
} |
|
1180 |
||
1181 |
/* OpenBook is used internally only when app uses Printable. |
|
1182 |
* This is the case when we use the values from the attribute set. |
|
1183 |
*/ |
|
1184 |
Media media = (Media)attributes.get(Media.class); |
|
1185 |
OrientationRequested orientReq = |
|
1186 |
(OrientationRequested)attributes.get(OrientationRequested.class); |
|
1187 |
MediaPrintableArea mpa = |
|
1188 |
(MediaPrintableArea)attributes.get(MediaPrintableArea.class); |
|
1189 |
||
1190 |
if ((orientReq != null || media != null || mpa != null) && |
|
1191 |
getPageable() instanceof OpenBook) { |
|
1192 |
||
1193 |
/* We could almost(!) use PrinterJob.getPageFormat() except |
|
1194 |
* here we need to start with the PageFormat from the OpenBook : |
|
1195 |
*/ |
|
1196 |
Pageable pageable = getPageable(); |
|
1197 |
Printable printable = pageable.getPrintable(0); |
|
1198 |
PageFormat pf = (PageFormat)pageable.getPageFormat(0).clone(); |
|
1199 |
Paper paper = pf.getPaper(); |
|
1200 |
||
1201 |
/* If there's a media but no media printable area, we can try |
|
1202 |
* to retrieve the default value for mpa and use that. |
|
1203 |
*/ |
|
1204 |
if (mpa == null && media != null && |
|
1205 |
service. |
|
1206 |
isAttributeCategorySupported(MediaPrintableArea.class)) { |
|
1207 |
Object mpaVals = service. |
|
1208 |
getSupportedAttributeValues(MediaPrintableArea.class, |
|
1209 |
null, attributes); |
|
1210 |
if (mpaVals instanceof MediaPrintableArea[] && |
|
1211 |
((MediaPrintableArea[])mpaVals).length > 0) { |
|
1212 |
mpa = ((MediaPrintableArea[])mpaVals)[0]; |
|
1213 |
} |
|
1214 |
} |
|
1215 |
||
1216 |
if (isSupportedValue(orientReq, attributes) || |
|
1217 |
(!fidelity && orientReq != null)) { |
|
1218 |
int orient; |
|
1219 |
if (orientReq.equals(OrientationRequested.REVERSE_LANDSCAPE)) { |
|
1220 |
orient = PageFormat.REVERSE_LANDSCAPE; |
|
1221 |
} else if (orientReq.equals(OrientationRequested.LANDSCAPE)) { |
|
1222 |
orient = PageFormat.LANDSCAPE; |
|
1223 |
} else { |
|
1224 |
orient = PageFormat.PORTRAIT; |
|
1225 |
} |
|
1226 |
pf.setOrientation(orient); |
|
1227 |
} |
|
1228 |
||
1229 |
if (isSupportedValue(media, attributes) || |
|
1230 |
(!fidelity && media != null)) { |
|
1231 |
if (media instanceof MediaSizeName) { |
|
1232 |
MediaSizeName msn = (MediaSizeName)media; |
|
1233 |
MediaSize msz = MediaSize.getMediaSizeForName(msn); |
|
1234 |
if (msz != null) { |
|
1235 |
float paperWid = msz.getX(MediaSize.INCH) * 72.0f; |
|
1236 |
float paperHgt = msz.getY(MediaSize.INCH) * 72.0f; |
|
1237 |
paper.setSize(paperWid, paperHgt); |
|
1238 |
if (mpa == null) { |
|
1239 |
paper.setImageableArea(72.0, 72.0, |
|
1240 |
paperWid-144.0, |
|
1241 |
paperHgt-144.0); |
|
1242 |
} |
|
1243 |
} |
|
1244 |
} |
|
1245 |
} |
|
1246 |
||
1247 |
if (isSupportedValue(mpa, attributes) || |
|
1248 |
(!fidelity && mpa != null)) { |
|
1249 |
float [] printableArea = |
|
1250 |
mpa.getPrintableArea(MediaPrintableArea.INCH); |
|
1251 |
for (int i=0; i < printableArea.length; i++) { |
|
1252 |
printableArea[i] = printableArea[i]*72.0f; |
|
1253 |
} |
|
1254 |
paper.setImageableArea(printableArea[0], printableArea[1], |
|
1255 |
printableArea[2], printableArea[3]); |
|
1256 |
} |
|
1257 |
||
1258 |
pf.setPaper(paper); |
|
1259 |
pf = validatePage(pf); |
|
1260 |
setPrintable(printable, pf); |
|
1261 |
} else { |
|
1262 |
// for AWT where pageable is not an instance of OpenBook, |
|
1263 |
// we need to save paper info |
|
1264 |
this.attributes = attributes; |
|
1265 |
} |
|
1266 |
||
1267 |
} |
|
1268 |
||
1269 |
/* |
|
1270 |
* Services we don't recognize as built-in services can't be |
|
1271 |
* implemented as subclasses of PrinterJob, therefore we create |
|
1272 |
* a DocPrintJob from their service and pass a Doc representing |
|
1273 |
* the application's printjob |
|
1274 |
*/ |
|
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
1275 |
// MacOSX - made protected so subclasses can reference it. |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
1276 |
protected void spoolToService(PrintService psvc, |
2 | 1277 |
PrintRequestAttributeSet attributes) |
1278 |
throws PrinterException { |
|
1279 |
||
1280 |
if (psvc == null) { |
|
1281 |
throw new PrinterException("No print service found."); |
|
1282 |
} |
|
1283 |
||
1284 |
DocPrintJob job = psvc.createPrintJob(); |
|
1285 |
Doc doc = new PageableDoc(getPageable()); |
|
1286 |
if (attributes == null) { |
|
1287 |
attributes = new HashPrintRequestAttributeSet(); |
|
1288 |
} |
|
1289 |
try { |
|
1290 |
job.print(doc, attributes); |
|
1291 |
} catch (PrintException e) { |
|
1292 |
throw new PrinterException(e.toString()); |
|
1293 |
} |
|
1294 |
} |
|
1295 |
||
1296 |
/** |
|
1297 |
* Prints a set of pages. |
|
1298 |
* @exception java.awt.print.PrinterException an error in the print system |
|
1299 |
* caused the job to be aborted |
|
1300 |
* @see java.awt.print.Book |
|
1301 |
* @see java.awt.print.Pageable |
|
1302 |
* @see java.awt.print.Printable |
|
1303 |
*/ |
|
1304 |
public void print() throws PrinterException { |
|
1305 |
print(attributes); |
|
1306 |
} |
|
1307 |
||
1308 |
public static boolean debugPrint = false; |
|
1309 |
protected void debug_println(String str) { |
|
1310 |
if (debugPrint) { |
|
1311 |
System.out.println("RasterPrinterJob "+str+" "+this); |
|
1312 |
} |
|
1313 |
} |
|
1314 |
||
1315 |
public void print(PrintRequestAttributeSet attributes) |
|
1316 |
throws PrinterException { |
|
1317 |
||
1318 |
/* |
|
1319 |
* In the future PrinterJob will probably always dispatch |
|
1320 |
* the print job to the PrintService. |
|
1321 |
* This is how third party 2D Print Services will be invoked |
|
1322 |
* when applications use the PrinterJob API. |
|
1323 |
* However the JRE's concrete PrinterJob implementations have |
|
1324 |
* not yet been re-worked to be implemented as standalone |
|
1325 |
* services, and are implemented only as subclasses of PrinterJob. |
|
1326 |
* So here we dispatch only those services we do not recognize |
|
1327 |
* as implemented through platform subclasses of PrinterJob |
|
1328 |
* (and this class). |
|
1329 |
*/ |
|
1330 |
PrintService psvc = getPrintService(); |
|
1331 |
debug_println("psvc = "+psvc); |
|
1332 |
if (psvc == null) { |
|
1333 |
throw new PrinterException("No print service found."); |
|
1334 |
} |
|
1335 |
||
1336 |
// Check the list of services. This service may have been |
|
1337 |
// deleted already |
|
1338 |
PrinterState prnState = (PrinterState)psvc.getAttribute( |
|
1339 |
PrinterState.class); |
|
1340 |
if (prnState == PrinterState.STOPPED) { |
|
1341 |
PrinterStateReasons prnStateReasons = |
|
1342 |
(PrinterStateReasons)psvc.getAttribute( |
|
1343 |
PrinterStateReasons.class); |
|
1344 |
if ((prnStateReasons != null) && |
|
1345 |
(prnStateReasons.containsKey(PrinterStateReason.SHUTDOWN))) |
|
1346 |
{ |
|
1347 |
throw new PrinterException("PrintService is no longer available."); |
|
1348 |
} |
|
1349 |
} |
|
1350 |
||
1351 |
if ((PrinterIsAcceptingJobs)(psvc.getAttribute( |
|
1352 |
PrinterIsAcceptingJobs.class)) == |
|
1353 |
PrinterIsAcceptingJobs.NOT_ACCEPTING_JOBS) { |
|
1354 |
throw new PrinterException("Printer is not accepting job."); |
|
1355 |
} |
|
1356 |
||
1357 |
if ((psvc instanceof SunPrinterJobService) && |
|
1358 |
((SunPrinterJobService)psvc).usesClass(getClass())) { |
|
1359 |
setAttributes(attributes); |
|
1360 |
// throw exception for invalid destination |
|
1361 |
if (destinationAttr != null) { |
|
1362 |
// destinationAttr is null for Destination(new URI("")) |
|
1363 |
// because isAttributeValueSupported returns false in setAttributes |
|
1364 |
||
1365 |
// Destination(new URI(" ")) throws URISyntaxException |
|
1366 |
File f = new File(destinationAttr); |
|
1367 |
try { |
|
1368 |
// check if this is a new file and if filename chars are valid |
|
1369 |
if (f.createNewFile()) { |
|
1370 |
f.delete(); |
|
1371 |
} |
|
1372 |
} catch (IOException ioe) { |
|
1373 |
throw new PrinterException("Cannot write to file:"+ |
|
1374 |
destinationAttr); |
|
1375 |
} catch (SecurityException se) { |
|
1376 |
//There is already file read/write access so at this point |
|
1377 |
// only delete access is denied. Just ignore it because in |
|
1378 |
// most cases the file created in createNewFile gets overwritten |
|
1379 |
// anyway. |
|
1380 |
} |
|
1381 |
||
1382 |
File pFile = f.getParentFile(); |
|
1383 |
if ((f.exists() && |
|
1384 |
(!f.isFile() || !f.canWrite())) || |
|
1385 |
((pFile != null) && |
|
1386 |
(!pFile.exists() || (pFile.exists() && !pFile.canWrite())))) { |
|
1387 |
throw new PrinterException("Cannot write to file:"+ |
|
1388 |
destinationAttr); |
|
1389 |
} |
|
1390 |
} |
|
1391 |
} else { |
|
1392 |
spoolToService(psvc, attributes); |
|
1393 |
return; |
|
1394 |
} |
|
1395 |
/* We need to make sure that the collation and copies |
|
1396 |
* settings are initialised */ |
|
1397 |
initPrinter(); |
|
1398 |
||
1399 |
int numCollatedCopies = getCollatedCopies(); |
|
1400 |
int numNonCollatedCopies = getNoncollatedCopies(); |
|
1401 |
debug_println("getCollatedCopies() "+numCollatedCopies |
|
1402 |
+ " getNoncollatedCopies() "+ numNonCollatedCopies); |
|
1403 |
||
1404 |
/* Get the range of pages we are to print. If the |
|
1405 |
* last page to print is unknown, then we print to |
|
1406 |
* the end of the document. Note that firstPage |
|
1407 |
* and lastPage are 0 based page indices. |
|
1408 |
*/ |
|
1409 |
int numPages = mDocument.getNumberOfPages(); |
|
1410 |
if (numPages == 0) { |
|
1411 |
return; |
|
1412 |
} |
|
1413 |
||
1414 |
int firstPage = getFirstPage(); |
|
1415 |
int lastPage = getLastPage(); |
|
1416 |
if(lastPage == Pageable.UNKNOWN_NUMBER_OF_PAGES){ |
|
1417 |
int totalPages = mDocument.getNumberOfPages(); |
|
1418 |
if (totalPages != Pageable.UNKNOWN_NUMBER_OF_PAGES) { |
|
1419 |
lastPage = mDocument.getNumberOfPages() - 1; |
|
1420 |
} |
|
1421 |
} |
|
1422 |
||
1423 |
try { |
|
1424 |
synchronized (this) { |
|
1425 |
performingPrinting = true; |
|
1426 |
userCancelled = false; |
|
1427 |
} |
|
1428 |
||
1429 |
startDoc(); |
|
1430 |
if (isCancelled()) { |
|
1431 |
cancelDoc(); |
|
1432 |
} |
|
1433 |
||
1434 |
// PageRanges can be set even if RANGE is not selected |
|
1435 |
// so we need to check if it is selected. |
|
1436 |
boolean rangeIsSelected = true; |
|
1437 |
if (attributes != null) { |
|
1438 |
SunPageSelection pages = |
|
1439 |
(SunPageSelection)attributes.get(SunPageSelection.class); |
|
1440 |
if ((pages != null) && (pages != SunPageSelection.RANGE)) { |
|
1441 |
rangeIsSelected = false; |
|
1442 |
} |
|
1443 |
} |
|
1444 |
||
1445 |
||
1446 |
debug_println("after startDoc rangeSelected? "+rangeIsSelected |
|
1447 |
+ " numNonCollatedCopies "+ numNonCollatedCopies); |
|
1448 |
||
1449 |
||
1450 |
/* Three nested loops iterate over the document. The outer loop |
|
1451 |
* counts the number of collated copies while the inner loop |
|
1452 |
* counts the number of nonCollated copies. Normally, one of |
|
1453 |
* these two loops will only execute once; that is we will |
|
1454 |
* either print collated copies or noncollated copies. The |
|
1455 |
* middle loop iterates over the pages. |
|
1456 |
* If a PageRanges attribute is used, it constrains the pages |
|
1457 |
* that are imaged. If a platform subclass (though a user dialog) |
|
1458 |
* requests a page range via setPageRange(). it too can |
|
1459 |
* constrain the page ranges that are imaged. |
|
1460 |
* It is expected that only one of these will be used in a |
|
1461 |
* job but both should be able to co-exist. |
|
1462 |
*/ |
|
1463 |
for(int collated = 0; collated < numCollatedCopies; collated++) { |
|
1464 |
for(int i = firstPage, pageResult = Printable.PAGE_EXISTS; |
|
1465 |
(i <= lastPage || |
|
1466 |
lastPage == Pageable.UNKNOWN_NUMBER_OF_PAGES) |
|
1467 |
&& pageResult == Printable.PAGE_EXISTS; |
|
1468 |
i++) |
|
1469 |
{ |
|
1470 |
||
1471 |
if ((pageRangesAttr != null) && rangeIsSelected ){ |
|
1472 |
int nexti = pageRangesAttr.next(i); |
|
1473 |
if (nexti == -1) { |
|
1474 |
break; |
|
1475 |
} else if (nexti != i+1) { |
|
1476 |
continue; |
|
1477 |
} |
|
1478 |
} |
|
1479 |
||
1480 |
for(int nonCollated = 0; |
|
1481 |
nonCollated < numNonCollatedCopies |
|
1482 |
&& pageResult == Printable.PAGE_EXISTS; |
|
1483 |
nonCollated++) |
|
1484 |
{ |
|
1485 |
if (isCancelled()) { |
|
1486 |
cancelDoc(); |
|
1487 |
} |
|
1488 |
debug_println("printPage "+i); |
|
1489 |
pageResult = printPage(mDocument, i); |
|
1490 |
||
1491 |
} |
|
1492 |
} |
|
1493 |
} |
|
1494 |
||
1495 |
if (isCancelled()) { |
|
1496 |
cancelDoc(); |
|
1497 |
} |
|
1498 |
||
1499 |
} finally { |
|
1500 |
// reset previousPaper in case this job is invoked again. |
|
1501 |
previousPaper = null; |
|
1502 |
synchronized (this) { |
|
1503 |
if (performingPrinting) { |
|
1504 |
endDoc(); |
|
1505 |
} |
|
1506 |
performingPrinting = false; |
|
1507 |
notify(); |
|
1508 |
} |
|
1509 |
} |
|
1510 |
} |
|
1511 |
||
1512 |
/** |
|
1513 |
* updates a Paper object to reflect the current printer's selected |
|
1514 |
* paper size and imageable area for that paper size. |
|
1515 |
* Default implementation copies settings from the original, applies |
|
1516 |
* applies some validity checks, changes them only if they are |
|
1517 |
* clearly unreasonable, then sets them into the new Paper. |
|
1518 |
* Subclasses are expected to override this method to make more |
|
1519 |
* informed decisons. |
|
1520 |
*/ |
|
1521 |
protected void validatePaper(Paper origPaper, Paper newPaper) { |
|
1522 |
if (origPaper == null || newPaper == null) { |
|
1523 |
return; |
|
1524 |
} else { |
|
1525 |
double wid = origPaper.getWidth(); |
|
1526 |
double hgt = origPaper.getHeight(); |
|
1527 |
double ix = origPaper.getImageableX(); |
|
1528 |
double iy = origPaper.getImageableY(); |
|
1529 |
double iw = origPaper.getImageableWidth(); |
|
1530 |
double ih = origPaper.getImageableHeight(); |
|
1531 |
||
1532 |
/* Assume any +ve values are legal. Overall paper dimensions |
|
1533 |
* take precedence. Make sure imageable area fits on the paper. |
|
1534 |
*/ |
|
1535 |
Paper defaultPaper = new Paper(); |
|
1536 |
wid = ((wid > 0.0) ? wid : defaultPaper.getWidth()); |
|
1537 |
hgt = ((hgt > 0.0) ? hgt : defaultPaper.getHeight()); |
|
1538 |
ix = ((ix > 0.0) ? ix : defaultPaper.getImageableX()); |
|
1539 |
iy = ((iy > 0.0) ? iy : defaultPaper.getImageableY()); |
|
1540 |
iw = ((iw > 0.0) ? iw : defaultPaper.getImageableWidth()); |
|
1541 |
ih = ((ih > 0.0) ? ih : defaultPaper.getImageableHeight()); |
|
1542 |
/* full width/height is not likely to be imageable, but since we |
|
1543 |
* don't know the limits we have to allow it |
|
1544 |
*/ |
|
1545 |
if (iw > wid) { |
|
1546 |
iw = wid; |
|
1547 |
} |
|
1548 |
if (ih > hgt) { |
|
1549 |
ih = hgt; |
|
1550 |
} |
|
1551 |
if ((ix + iw) > wid) { |
|
1552 |
ix = wid - iw; |
|
1553 |
} |
|
1554 |
if ((iy + ih) > hgt) { |
|
1555 |
iy = hgt - ih; |
|
1556 |
} |
|
1557 |
newPaper.setSize(wid, hgt); |
|
1558 |
newPaper.setImageableArea(ix, iy, iw, ih); |
|
1559 |
} |
|
1560 |
} |
|
1561 |
||
1562 |
/** |
|
1563 |
* The passed in PageFormat will be copied and altered to describe |
|
1564 |
* the default page size and orientation of the PrinterJob's |
|
1565 |
* current printer. |
|
1566 |
* Platform subclasses which can access the actual default paper size |
|
1567 |
* for a printer may override this method. |
|
1568 |
*/ |
|
1569 |
public PageFormat defaultPage(PageFormat page) { |
|
1570 |
PageFormat newPage = (PageFormat)page.clone(); |
|
1571 |
newPage.setOrientation(PageFormat.PORTRAIT); |
|
1572 |
Paper newPaper = new Paper(); |
|
1573 |
double ptsPerInch = 72.0; |
|
1574 |
double w, h; |
|
1575 |
Media media = null; |
|
1576 |
||
1577 |
PrintService service = getPrintService(); |
|
1578 |
if (service != null) { |
|
1579 |
MediaSize size; |
|
1580 |
media = |
|
1581 |
(Media)service.getDefaultAttributeValue(Media.class); |
|
1582 |
||
1583 |
if (media instanceof MediaSizeName && |
|
1584 |
((size = MediaSize.getMediaSizeForName((MediaSizeName)media)) != |
|
1585 |
null)) { |
|
1586 |
w = size.getX(MediaSize.INCH) * ptsPerInch; |
|
1587 |
h = size.getY(MediaSize.INCH) * ptsPerInch; |
|
1588 |
newPaper.setSize(w, h); |
|
1589 |
newPaper.setImageableArea(ptsPerInch, ptsPerInch, |
|
1590 |
w - 2.0*ptsPerInch, |
|
1591 |
h - 2.0*ptsPerInch); |
|
1592 |
newPage.setPaper(newPaper); |
|
1593 |
return newPage; |
|
1594 |
||
1595 |
} |
|
1596 |
} |
|
1597 |
||
1598 |
/* Default to A4 paper outside North America. |
|
1599 |
*/ |
|
1600 |
String defaultCountry = Locale.getDefault().getCountry(); |
|
1601 |
if (!Locale.getDefault().equals(Locale.ENGLISH) && // ie "C" |
|
1602 |
defaultCountry != null && |
|
1603 |
!defaultCountry.equals(Locale.US.getCountry()) && |
|
1604 |
!defaultCountry.equals(Locale.CANADA.getCountry())) { |
|
1605 |
||
1606 |
double mmPerInch = 25.4; |
|
1607 |
w = Math.rint((210.0*ptsPerInch)/mmPerInch); |
|
1608 |
h = Math.rint((297.0*ptsPerInch)/mmPerInch); |
|
1609 |
newPaper.setSize(w, h); |
|
1610 |
newPaper.setImageableArea(ptsPerInch, ptsPerInch, |
|
1611 |
w - 2.0*ptsPerInch, |
|
1612 |
h - 2.0*ptsPerInch); |
|
1613 |
} |
|
1614 |
||
1615 |
newPage.setPaper(newPaper); |
|
1616 |
||
1617 |
return newPage; |
|
1618 |
} |
|
1619 |
||
1620 |
/** |
|
1621 |
* The passed in PageFormat is cloned and altered to be usable on |
|
1622 |
* the PrinterJob's current printer. |
|
1623 |
*/ |
|
1624 |
public PageFormat validatePage(PageFormat page) { |
|
1625 |
PageFormat newPage = (PageFormat)page.clone(); |
|
1626 |
Paper newPaper = new Paper(); |
|
1627 |
validatePaper(newPage.getPaper(), newPaper); |
|
1628 |
newPage.setPaper(newPaper); |
|
1629 |
||
1630 |
return newPage; |
|
1631 |
} |
|
1632 |
||
1633 |
/** |
|
1634 |
* Set the number of copies to be printed. |
|
1635 |
*/ |
|
1636 |
public void setCopies(int copies) { |
|
1637 |
mNumCopies = copies; |
|
1638 |
} |
|
1639 |
||
1640 |
/** |
|
1641 |
* Get the number of copies to be printed. |
|
1642 |
*/ |
|
1643 |
public int getCopies() { |
|
1644 |
return mNumCopies; |
|
1645 |
} |
|
1646 |
||
1647 |
/* Used when executing a print job where an attribute set may |
|
1648 |
* over ride API values. |
|
1649 |
*/ |
|
1650 |
protected int getCopiesInt() { |
|
1651 |
return (copiesAttr > 0) ? copiesAttr : getCopies(); |
|
1652 |
} |
|
1653 |
||
1654 |
/** |
|
1655 |
* Get the name of the printing user. |
|
1656 |
* The caller must have security permission to read system properties. |
|
1657 |
*/ |
|
1658 |
public String getUserName() { |
|
1659 |
return System.getProperty("user.name"); |
|
1660 |
} |
|
1661 |
||
1662 |
/* Used when executing a print job where an attribute set may |
|
1663 |
* over ride API values. |
|
1664 |
*/ |
|
1665 |
protected String getUserNameInt() { |
|
1666 |
if (userNameAttr != null) { |
|
1667 |
return userNameAttr; |
|
1668 |
} else { |
|
1669 |
try { |
|
1670 |
return getUserName(); |
|
1671 |
} catch (SecurityException e) { |
|
1672 |
return ""; |
|
1673 |
} |
|
1674 |
} |
|
1675 |
} |
|
1676 |
||
1677 |
/** |
|
1678 |
* Set the name of the document to be printed. |
|
1679 |
* The document name can not be null. |
|
1680 |
*/ |
|
1681 |
public void setJobName(String jobName) { |
|
1682 |
if (jobName != null) { |
|
1683 |
mDocName = jobName; |
|
1684 |
} else { |
|
1685 |
throw new NullPointerException(); |
|
1686 |
} |
|
1687 |
} |
|
1688 |
||
1689 |
/** |
|
1690 |
* Get the name of the document to be printed. |
|
1691 |
*/ |
|
1692 |
public String getJobName() { |
|
1693 |
return mDocName; |
|
1694 |
} |
|
1695 |
||
1696 |
/* Used when executing a print job where an attribute set may |
|
1697 |
* over ride API values. |
|
1698 |
*/ |
|
1699 |
protected String getJobNameInt() { |
|
1700 |
return (jobNameAttr != null) ? jobNameAttr : getJobName(); |
|
1701 |
} |
|
1702 |
||
1703 |
/** |
|
1704 |
* Set the range of pages from a Book to be printed. |
|
1705 |
* Both 'firstPage' and 'lastPage' are zero based |
|
1706 |
* page indices. If either parameter is less than |
|
1707 |
* zero then the page range is set to be from the |
|
1708 |
* first page to the last. |
|
1709 |
*/ |
|
1710 |
protected void setPageRange(int firstPage, int lastPage) { |
|
1711 |
if(firstPage >= 0 && lastPage >= 0) { |
|
1712 |
mFirstPage = firstPage; |
|
1713 |
mLastPage = lastPage; |
|
1714 |
if(mLastPage < mFirstPage) mLastPage = mFirstPage; |
|
1715 |
} else { |
|
1716 |
mFirstPage = Pageable.UNKNOWN_NUMBER_OF_PAGES; |
|
1717 |
mLastPage = Pageable.UNKNOWN_NUMBER_OF_PAGES; |
|
1718 |
} |
|
1719 |
} |
|
1720 |
||
1721 |
/** |
|
1722 |
* Return the zero based index of the first page to |
|
1723 |
* be printed in this job. |
|
1724 |
*/ |
|
1725 |
protected int getFirstPage() { |
|
1726 |
return mFirstPage == Book.UNKNOWN_NUMBER_OF_PAGES ? 0 : mFirstPage; |
|
1727 |
} |
|
1728 |
||
1729 |
/** |
|
1730 |
* Return the zero based index of the last page to |
|
1731 |
* be printed in this job. |
|
1732 |
*/ |
|
1733 |
protected int getLastPage() { |
|
1734 |
return mLastPage; |
|
1735 |
} |
|
1736 |
||
1737 |
/** |
|
1738 |
* Set whether copies should be collated or not. |
|
1739 |
* Two collated copies of a three page document |
|
1740 |
* print in this order: 1, 2, 3, 1, 2, 3 while |
|
1741 |
* uncollated copies print in this order: |
|
1742 |
* 1, 1, 2, 2, 3, 3. |
|
1743 |
* This is set when request is using an attribute set. |
|
1744 |
*/ |
|
1745 |
protected void setCollated(boolean collate) { |
|
1746 |
mCollate = collate; |
|
1747 |
collateAttReq = true; |
|
1748 |
} |
|
1749 |
||
1750 |
/** |
|
1751 |
* Return true if collated copies will be printed as determined |
|
1752 |
* in an attribute set. |
|
1753 |
*/ |
|
1754 |
protected boolean isCollated() { |
|
1755 |
return mCollate; |
|
1756 |
} |
|
1757 |
||
1758 |
/** |
|
1759 |
* Called by the print() method at the start of |
|
1760 |
* a print job. |
|
1761 |
*/ |
|
1762 |
protected abstract void startDoc() throws PrinterException; |
|
1763 |
||
1764 |
/** |
|
1765 |
* Called by the print() method at the end of |
|
1766 |
* a print job. |
|
1767 |
*/ |
|
1768 |
protected abstract void endDoc() throws PrinterException; |
|
1769 |
||
1770 |
/* Called by cancelDoc */ |
|
1771 |
protected abstract void abortDoc(); |
|
1772 |
||
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
1773 |
// MacOSX - made protected so subclasses can reference it. |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
1774 |
protected void cancelDoc() throws PrinterAbortException { |
2 | 1775 |
abortDoc(); |
1776 |
synchronized (this) { |
|
1777 |
userCancelled = false; |
|
1778 |
performingPrinting = false; |
|
1779 |
notify(); |
|
1780 |
} |
|
1781 |
throw new PrinterAbortException(); |
|
1782 |
} |
|
1783 |
||
1784 |
/** |
|
1785 |
* Returns how many times the entire book should |
|
1786 |
* be printed by the PrintJob. If the printer |
|
1787 |
* itself supports collation then this method |
|
1788 |
* should return 1 indicating that the entire |
|
1789 |
* book need only be printed once and the copies |
|
1790 |
* will be collated and made in the printer. |
|
1791 |
*/ |
|
1792 |
protected int getCollatedCopies() { |
|
1793 |
return isCollated() ? getCopiesInt() : 1; |
|
1794 |
} |
|
1795 |
||
1796 |
/** |
|
1797 |
* Returns how many times each page in the book |
|
1798 |
* should be consecutively printed by PrintJob. |
|
1799 |
* If the printer makes copies itself then this |
|
1800 |
* method should return 1. |
|
1801 |
*/ |
|
1802 |
protected int getNoncollatedCopies() { |
|
1803 |
return isCollated() ? 1 : getCopiesInt(); |
|
1804 |
} |
|
1805 |
||
1806 |
||
1807 |
/* The printer graphics config is cached on the job, so that it can |
|
1808 |
* be created once, and updated only as needed (for now only to change |
|
1809 |
* the bounds if when using a Pageable the page sizes changes). |
|
1810 |
*/ |
|
1811 |
||
1812 |
private int deviceWidth, deviceHeight; |
|
1813 |
private AffineTransform defaultDeviceTransform; |
|
1814 |
private PrinterGraphicsConfig pgConfig; |
|
1815 |
||
1816 |
synchronized void setGraphicsConfigInfo(AffineTransform at, |
|
1817 |
double pw, double ph) { |
|
1818 |
Point2D.Double pt = new Point2D.Double(pw, ph); |
|
1819 |
at.transform(pt, pt); |
|
1820 |
||
1821 |
if (pgConfig == null || |
|
1822 |
defaultDeviceTransform == null || |
|
1823 |
!at.equals(defaultDeviceTransform) || |
|
1824 |
deviceWidth != (int)pt.getX() || |
|
1825 |
deviceHeight != (int)pt.getY()) { |
|
1826 |
||
1827 |
deviceWidth = (int)pt.getX(); |
|
1828 |
deviceHeight = (int)pt.getY(); |
|
1829 |
defaultDeviceTransform = at; |
|
1830 |
pgConfig = null; |
|
1831 |
} |
|
1832 |
} |
|
1833 |
||
1834 |
synchronized PrinterGraphicsConfig getPrinterGraphicsConfig() { |
|
1835 |
if (pgConfig != null) { |
|
1836 |
return pgConfig; |
|
1837 |
} |
|
1838 |
String deviceID = "Printer Device"; |
|
1839 |
PrintService service = getPrintService(); |
|
1840 |
if (service != null) { |
|
1841 |
deviceID = service.toString(); |
|
1842 |
} |
|
1843 |
pgConfig = new PrinterGraphicsConfig(deviceID, |
|
1844 |
defaultDeviceTransform, |
|
1845 |
deviceWidth, deviceHeight); |
|
1846 |
return pgConfig; |
|
1847 |
} |
|
1848 |
||
1849 |
/** |
|
1850 |
* Print a page from the provided document. |
|
1851 |
* @return int Printable.PAGE_EXISTS if the page existed and was drawn and |
|
1852 |
* Printable.NO_SUCH_PAGE if the page did not exist. |
|
1853 |
* @see java.awt.print.Printable |
|
1854 |
*/ |
|
1855 |
protected int printPage(Pageable document, int pageIndex) |
|
1856 |
throws PrinterException |
|
1857 |
{ |
|
1858 |
PageFormat page; |
|
1859 |
PageFormat origPage; |
|
1860 |
Printable painter; |
|
1861 |
try { |
|
1862 |
origPage = document.getPageFormat(pageIndex); |
|
1863 |
page = (PageFormat)origPage.clone(); |
|
1864 |
painter = document.getPrintable(pageIndex); |
|
1865 |
} catch (Exception e) { |
|
1866 |
PrinterException pe = |
|
1867 |
new PrinterException("Error getting page or printable.[ " + |
|
1868 |
e +" ]"); |
|
1869 |
pe.initCause(e); |
|
1870 |
throw pe; |
|
1871 |
} |
|
1872 |
||
1873 |
/* Get the imageable area from Paper instead of PageFormat |
|
1874 |
* because we do not want it adjusted by the page orientation. |
|
1875 |
*/ |
|
1876 |
Paper paper = page.getPaper(); |
|
1877 |
// if non-portrait and 270 degree landscape rotation |
|
1878 |
if (page.getOrientation() != PageFormat.PORTRAIT && |
|
1879 |
landscapeRotates270) { |
|
1880 |
||
1881 |
double left = paper.getImageableX(); |
|
1882 |
double top = paper.getImageableY(); |
|
1883 |
double width = paper.getImageableWidth(); |
|
1884 |
double height = paper.getImageableHeight(); |
|
1885 |
paper.setImageableArea(paper.getWidth()-left-width, |
|
1886 |
paper.getHeight()-top-height, |
|
1887 |
width, height); |
|
1888 |
page.setPaper(paper); |
|
1889 |
if (page.getOrientation() == PageFormat.LANDSCAPE) { |
|
1890 |
page.setOrientation(PageFormat.REVERSE_LANDSCAPE); |
|
1891 |
} else { |
|
1892 |
page.setOrientation(PageFormat.LANDSCAPE); |
|
1893 |
} |
|
1894 |
} |
|
1895 |
||
1896 |
double xScale = getXRes() / 72.0; |
|
1897 |
double yScale = getYRes() / 72.0; |
|
1898 |
||
1899 |
/* The deviceArea is the imageable area in the printer's |
|
1900 |
* resolution. |
|
1901 |
*/ |
|
1902 |
Rectangle2D deviceArea = |
|
1903 |
new Rectangle2D.Double(paper.getImageableX() * xScale, |
|
1904 |
paper.getImageableY() * yScale, |
|
1905 |
paper.getImageableWidth() * xScale, |
|
1906 |
paper.getImageableHeight() * yScale); |
|
1907 |
||
1908 |
/* Build and hold on to a uniform transform so that |
|
1909 |
* we can get back to device space at the beginning |
|
1910 |
* of each band. |
|
1911 |
*/ |
|
1912 |
AffineTransform uniformTransform = new AffineTransform(); |
|
1913 |
||
1914 |
/* The scale transform is used to switch from the |
|
1915 |
* device space to the user's 72 dpi space. |
|
1916 |
*/ |
|
1917 |
AffineTransform scaleTransform = new AffineTransform(); |
|
1918 |
scaleTransform.scale(xScale, yScale); |
|
1919 |
||
1920 |
/* bandwidth is multiple of 4 as the data is used in a win32 DIB and |
|
1921 |
* some drivers behave badly if scanlines aren't multiples of 4 bytes. |
|
1922 |
*/ |
|
1923 |
int bandWidth = (int) deviceArea.getWidth(); |
|
1924 |
if (bandWidth % 4 != 0) { |
|
1925 |
bandWidth += (4 - (bandWidth % 4)); |
|
1926 |
} |
|
1927 |
if (bandWidth <= 0) { |
|
1928 |
throw new PrinterException("Paper's imageable width is too small."); |
|
1929 |
} |
|
1930 |
||
1931 |
int deviceAreaHeight = (int)deviceArea.getHeight(); |
|
1932 |
if (deviceAreaHeight <= 0) { |
|
1933 |
throw new PrinterException("Paper's imageable height is too small."); |
|
1934 |
} |
|
1935 |
||
1936 |
/* Figure out the number of lines that will fit into |
|
1937 |
* our maximum band size. The hard coded 3 reflects the |
|
1938 |
* fact that we can only create 24 bit per pixel 3 byte BGR |
|
1939 |
* BufferedImages. FIX. |
|
1940 |
*/ |
|
1941 |
int bandHeight = (int)(MAX_BAND_SIZE / bandWidth / 3); |
|
1942 |
||
1943 |
int deviceLeft = (int)Math.rint(paper.getImageableX() * xScale); |
|
1944 |
int deviceTop = (int)Math.rint(paper.getImageableY() * yScale); |
|
1945 |
||
1946 |
/* The device transform is used to move the band down |
|
1947 |
* the page using translates. Normally this is all it |
|
1948 |
* would do, but since, when printing, the Window's |
|
1949 |
* DIB format wants the last line to be first (lowest) in |
|
1950 |
* memory, the deviceTransform moves the origin to the |
|
1951 |
* bottom of the band and flips the origin. This way the |
|
1952 |
* app prints upside down into the band which is the DIB |
|
1953 |
* format. |
|
1954 |
*/ |
|
1955 |
AffineTransform deviceTransform = new AffineTransform(); |
|
1956 |
deviceTransform.translate(-deviceLeft, deviceTop); |
|
1957 |
deviceTransform.translate(0, bandHeight); |
|
1958 |
deviceTransform.scale(1, -1); |
|
1959 |
||
1960 |
/* Create a BufferedImage to hold the band. We set the clip |
|
1961 |
* of the band to be tight around the bits so that the |
|
1962 |
* application can use it to figure what part of the |
|
1963 |
* page needs to be drawn. The clip is never altered in |
|
1964 |
* this method, but we do translate the band's coordinate |
|
1965 |
* system so that the app will see the clip moving down the |
|
1966 |
* page though it s always around the same set of pixels. |
|
1967 |
*/ |
|
1968 |
BufferedImage pBand = new BufferedImage(1, 1, |
|
1969 |
BufferedImage.TYPE_3BYTE_BGR); |
|
1970 |
||
1971 |
/* Have the app draw into a PeekGraphics object so we can |
|
1972 |
* learn something about the needs of the print job. |
|
1973 |
*/ |
|
1974 |
||
1975 |
PeekGraphics peekGraphics = createPeekGraphics(pBand.createGraphics(), |
|
1976 |
this); |
|
1977 |
||
1978 |
Rectangle2D.Double pageFormatArea = |
|
1979 |
new Rectangle2D.Double(page.getImageableX(), |
|
1980 |
page.getImageableY(), |
|
1981 |
page.getImageableWidth(), |
|
1982 |
page.getImageableHeight()); |
|
1983 |
peekGraphics.transform(scaleTransform); |
|
1984 |
peekGraphics.translate(-getPhysicalPrintableX(paper) / xScale, |
|
1985 |
-getPhysicalPrintableY(paper) / yScale); |
|
1986 |
peekGraphics.transform(new AffineTransform(page.getMatrix())); |
|
1987 |
initPrinterGraphics(peekGraphics, pageFormatArea); |
|
1988 |
AffineTransform pgAt = peekGraphics.getTransform(); |
|
1989 |
||
1990 |
/* Update the information used to return a GraphicsConfiguration |
|
1991 |
* for this printer device. It needs to be updated per page as |
|
1992 |
* not all pages in a job may be the same size (different bounds) |
|
1993 |
* The transform is the scaling transform as this corresponds to |
|
1994 |
* the default transform for the device. The width and height are |
|
1995 |
* those of the paper, not the page format, as we want to describe |
|
1996 |
* the bounds of the device in its natural coordinate system of |
|
1997 |
* device coordinate whereas a page format may be in a rotated context. |
|
1998 |
*/ |
|
1999 |
setGraphicsConfigInfo(scaleTransform, |
|
2000 |
paper.getWidth(), paper.getHeight()); |
|
2001 |
int pageResult = painter.print(peekGraphics, origPage, pageIndex); |
|
2002 |
debug_println("pageResult "+pageResult); |
|
2003 |
if (pageResult == Printable.PAGE_EXISTS) { |
|
2004 |
debug_println("startPage "+pageIndex); |
|
2005 |
||
2006 |
/* We need to check if the paper size is changed. |
|
2007 |
* Note that it is not sufficient to ask for the pageformat |
|
2008 |
* of "pageIndex-1", since PageRanges mean that pages can be |
|
2009 |
* skipped. So we have to look at the actual last paper size used. |
|
2010 |
*/ |
|
2011 |
Paper thisPaper = page.getPaper(); |
|
2012 |
boolean paperChanged = |
|
2013 |
previousPaper == null || |
|
2014 |
thisPaper.getWidth() != previousPaper.getWidth() || |
|
2015 |
thisPaper.getHeight() != previousPaper.getHeight(); |
|
2016 |
previousPaper = thisPaper; |
|
2017 |
||
2018 |
startPage(page, painter, pageIndex, paperChanged); |
|
2019 |
Graphics2D pathGraphics = createPathGraphics(peekGraphics, this, |
|
2020 |
painter, page, |
|
2021 |
pageIndex); |
|
2022 |
||
2023 |
/* If we can convert the page directly to the |
|
2024 |
* underlying graphics system then we do not |
|
2025 |
* need to rasterize. We also may not need to |
|
2026 |
* create the 'band' if all the pages can take |
|
2027 |
* this path. |
|
2028 |
*/ |
|
2029 |
if (pathGraphics != null) { |
|
2030 |
pathGraphics.transform(scaleTransform); |
|
2031 |
// user (0,0) should be origin of page, not imageable area |
|
2032 |
pathGraphics.translate(-getPhysicalPrintableX(paper) / xScale, |
|
2033 |
-getPhysicalPrintableY(paper) / yScale); |
|
2034 |
pathGraphics.transform(new AffineTransform(page.getMatrix())); |
|
2035 |
initPrinterGraphics(pathGraphics, pageFormatArea); |
|
2036 |
||
2037 |
redrawList.clear(); |
|
2038 |
||
2039 |
AffineTransform initialTx = pathGraphics.getTransform(); |
|
2040 |
||
2041 |
painter.print(pathGraphics, origPage, pageIndex); |
|
2042 |
||
2043 |
for (int i=0;i<redrawList.size();i++) { |
|
2044 |
GraphicsState gstate = (GraphicsState)redrawList.get(i); |
|
2045 |
pathGraphics.setTransform(initialTx); |
|
2046 |
((PathGraphics)pathGraphics).redrawRegion( |
|
2047 |
gstate.region, |
|
2048 |
gstate.sx, |
|
2049 |
gstate.sy, |
|
2050 |
gstate.theClip, |
|
2051 |
gstate.theTransform); |
|
2052 |
} |
|
2053 |
||
2054 |
/* This is the banded-raster printing loop. |
|
2055 |
* It should be moved into its own method. |
|
2056 |
*/ |
|
2057 |
} else { |
|
2058 |
BufferedImage band = cachedBand; |
|
2059 |
if (cachedBand == null || |
|
2060 |
bandWidth != cachedBandWidth || |
|
2061 |
bandHeight != cachedBandHeight) { |
|
2062 |
band = new BufferedImage(bandWidth, bandHeight, |
|
2063 |
BufferedImage.TYPE_3BYTE_BGR); |
|
2064 |
cachedBand = band; |
|
2065 |
cachedBandWidth = bandWidth; |
|
2066 |
cachedBandHeight = bandHeight; |
|
2067 |
} |
|
2068 |
Graphics2D bandGraphics = band.createGraphics(); |
|
2069 |
||
2070 |
Rectangle2D.Double clipArea = |
|
2071 |
new Rectangle2D.Double(0, 0, bandWidth, bandHeight); |
|
2072 |
||
2073 |
initPrinterGraphics(bandGraphics, clipArea); |
|
2074 |
||
2075 |
ProxyGraphics2D painterGraphics = |
|
2076 |
new ProxyGraphics2D(bandGraphics, this); |
|
2077 |
||
2078 |
Graphics2D clearGraphics = band.createGraphics(); |
|
2079 |
clearGraphics.setColor(Color.white); |
|
2080 |
||
2081 |
/* We need the actual bits of the BufferedImage to send to |
|
2082 |
* the native Window's code. 'data' points to the actual |
|
2083 |
* pixels. Right now these are in ARGB format with 8 bits |
|
2084 |
* per component. We need to use a monochrome BufferedImage |
|
2085 |
* for monochrome printers when this is supported by |
|
2086 |
* BufferedImage. FIX |
|
2087 |
*/ |
|
2088 |
ByteInterleavedRaster tile = (ByteInterleavedRaster)band.getRaster(); |
|
2089 |
byte[] data = tile.getDataStorage(); |
|
2090 |
||
2091 |
/* Loop over the page moving our band down the page, |
|
2092 |
* calling the app to render the band, and then send the band |
|
2093 |
* to the printer. |
|
2094 |
*/ |
|
2095 |
int deviceBottom = deviceTop + deviceAreaHeight; |
|
2096 |
||
2097 |
/* device's printable x,y is really addressable origin |
|
2098 |
* we address relative to media origin so when we print a |
|
2099 |
* band we need to adjust for the different methods of |
|
2100 |
* addressing it. |
|
2101 |
*/ |
|
2102 |
int deviceAddressableX = (int)getPhysicalPrintableX(paper); |
|
2103 |
int deviceAddressableY = (int)getPhysicalPrintableY(paper); |
|
2104 |
||
2105 |
for (int bandTop = 0; bandTop <= deviceAreaHeight; |
|
2106 |
bandTop += bandHeight) |
|
2107 |
{ |
|
2108 |
||
2109 |
/* Put the band back into device space and |
|
2110 |
* erase the contents of the band. |
|
2111 |
*/ |
|
2112 |
clearGraphics.fillRect(0, 0, bandWidth, bandHeight); |
|
2113 |
||
2114 |
/* Put the band into the correct location on the |
|
2115 |
* page. Once the band is moved we translate the |
|
2116 |
* device transform so that the band will move down |
|
2117 |
* the page on the next iteration of the loop. |
|
2118 |
*/ |
|
2119 |
bandGraphics.setTransform(uniformTransform); |
|
2120 |
bandGraphics.transform(deviceTransform); |
|
2121 |
deviceTransform.translate(0, -bandHeight); |
|
2122 |
||
2123 |
/* Switch the band from device space to user, |
|
2124 |
* 72 dpi, space. |
|
2125 |
*/ |
|
2126 |
bandGraphics.transform(scaleTransform); |
|
2127 |
bandGraphics.transform(new AffineTransform(page.getMatrix())); |
|
2128 |
||
2129 |
Rectangle clip = bandGraphics.getClipBounds(); |
|
2130 |
clip = pgAt.createTransformedShape(clip).getBounds(); |
|
2131 |
||
2132 |
if ((clip == null) || peekGraphics.hitsDrawingArea(clip) && |
|
2133 |
(bandWidth > 0 && bandHeight > 0)) { |
|
2134 |
||
2135 |
/* if the client has specified an imageable X or Y |
|
2136 |
* which is off than the physically addressable |
|
2137 |
* area of the page, then we need to adjust for that |
|
2138 |
* here so that we pass only non -ve band coordinates |
|
2139 |
* We also need to translate by the adjusted amount |
|
2140 |
* so that printing appears in the correct place. |
|
2141 |
*/ |
|
2142 |
int bandX = deviceLeft - deviceAddressableX; |
|
2143 |
if (bandX < 0) { |
|
2144 |
bandGraphics.translate(bandX/xScale,0); |
|
2145 |
bandX = 0; |
|
2146 |
} |
|
2147 |
int bandY = deviceTop + bandTop - deviceAddressableY; |
|
2148 |
if (bandY < 0) { |
|
2149 |
bandGraphics.translate(0,bandY/yScale); |
|
2150 |
bandY = 0; |
|
2151 |
} |
|
2152 |
/* Have the app's painter image into the band |
|
2153 |
* and then send the band to the printer. |
|
2154 |
*/ |
|
2155 |
painterGraphics.setDelegate((Graphics2D) bandGraphics.create()); |
|
2156 |
painter.print(painterGraphics, origPage, pageIndex); |
|
2157 |
painterGraphics.dispose(); |
|
2158 |
printBand(data, bandX, bandY, bandWidth, bandHeight); |
|
2159 |
} |
|
2160 |
} |
|
2161 |
||
2162 |
clearGraphics.dispose(); |
|
2163 |
bandGraphics.dispose(); |
|
2164 |
||
2165 |
} |
|
2166 |
debug_println("calling endPage "+pageIndex); |
|
2167 |
endPage(page, painter, pageIndex); |
|
2168 |
} |
|
2169 |
||
2170 |
return pageResult; |
|
2171 |
} |
|
2172 |
||
2173 |
/** |
|
2174 |
* If a print job is in progress, print() has been |
|
2175 |
* called but has not returned, then this signals |
|
2176 |
* that the job should be cancelled and the next |
|
2177 |
* chance. If there is no print job in progress then |
|
2178 |
* this call does nothing. |
|
2179 |
*/ |
|
2180 |
public void cancel() { |
|
2181 |
synchronized (this) { |
|
2182 |
if (performingPrinting) { |
|
2183 |
userCancelled = true; |
|
2184 |
} |
|
2185 |
notify(); |
|
2186 |
} |
|
2187 |
} |
|
2188 |
||
2189 |
/** |
|
2190 |
* Returns true is a print job is ongoing but will |
|
2191 |
* be cancelled and the next opportunity. false is |
|
2192 |
* returned otherwise. |
|
2193 |
*/ |
|
2194 |
public boolean isCancelled() { |
|
2195 |
||
2196 |
boolean cancelled = false; |
|
2197 |
||
2198 |
synchronized (this) { |
|
2199 |
cancelled = (performingPrinting && userCancelled); |
|
2200 |
notify(); |
|
2201 |
} |
|
2202 |
||
2203 |
return cancelled; |
|
2204 |
} |
|
2205 |
||
2206 |
/** |
|
2207 |
* Return the Pageable describing the pages to be printed. |
|
2208 |
*/ |
|
2209 |
protected Pageable getPageable() { |
|
2210 |
return mDocument; |
|
2211 |
} |
|
2212 |
||
2213 |
/** |
|
2214 |
* Examine the metrics captured by the |
|
2215 |
* <code>PeekGraphics</code> instance and |
|
2216 |
* if capable of directly converting this |
|
2217 |
* print job to the printer's control language |
|
2218 |
* or the native OS's graphics primitives, then |
|
2219 |
* return a <code>PathGraphics</code> to perform |
|
2220 |
* that conversion. If there is not an object |
|
2221 |
* capable of the conversion then return |
|
2222 |
* <code>null</code>. Returning <code>null</code> |
|
2223 |
* causes the print job to be rasterized. |
|
2224 |
*/ |
|
2225 |
protected Graphics2D createPathGraphics(PeekGraphics graphics, |
|
2226 |
PrinterJob printerJob, |
|
2227 |
Printable painter, |
|
2228 |
PageFormat pageFormat, |
|
2229 |
int pageIndex) { |
|
2230 |
||
2231 |
return null; |
|
2232 |
} |
|
2233 |
||
2234 |
/** |
|
2235 |
* Create and return an object that will |
|
2236 |
* gather and hold metrics about the print |
|
2237 |
* job. This method is passed a <code>Graphics2D</code> |
|
2238 |
* object that can be used as a proxy for the |
|
2239 |
* object gathering the print job matrics. The |
|
2240 |
* method is also supplied with the instance |
|
2241 |
* controlling the print job, <code>printerJob</code>. |
|
2242 |
*/ |
|
2243 |
protected PeekGraphics createPeekGraphics(Graphics2D graphics, |
|
2244 |
PrinterJob printerJob) { |
|
2245 |
||
2246 |
return new PeekGraphics(graphics, printerJob); |
|
2247 |
} |
|
2248 |
||
2249 |
/** |
|
2250 |
* Configure the passed in Graphics2D so that |
|
2251 |
* is contains the defined initial settings |
|
2252 |
* for a print job. These settings are: |
|
2253 |
* color: black. |
|
2254 |
* clip: <as passed in> |
|
2255 |
*/ |
|
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
2256 |
// MacOSX - made protected so subclasses can reference it. |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
2257 |
protected void initPrinterGraphics(Graphics2D g, Rectangle2D clip) { |
2 | 2258 |
|
2259 |
g.setClip(clip); |
|
2260 |
g.setPaint(Color.black); |
|
2261 |
} |
|
2262 |
||
2263 |
||
2264 |
/** |
|
2265 |
* User dialogs should disable "File" buttons if this returns false. |
|
2266 |
* |
|
2267 |
*/ |
|
2268 |
public boolean checkAllowedToPrintToFile() { |
|
2269 |
try { |
|
2270 |
throwPrintToFile(); |
|
2271 |
return true; |
|
2272 |
} catch (SecurityException e) { |
|
2273 |
return false; |
|
2274 |
} |
|
2275 |
} |
|
2276 |
||
2277 |
/** |
|
2278 |
* Break this out as it may be useful when we allow API to |
|
2279 |
* specify printing to a file. In that case its probably right |
|
2280 |
* to throw a SecurityException if the permission is not granted |
|
2281 |
*/ |
|
2282 |
private void throwPrintToFile() { |
|
2283 |
SecurityManager security = System.getSecurityManager(); |
|
2284 |
if (security != null) { |
|
2285 |
if (printToFilePermission == null) { |
|
2286 |
printToFilePermission = |
|
2287 |
new FilePermission("<<ALL FILES>>", "read,write"); |
|
2288 |
} |
|
2289 |
security.checkPermission(printToFilePermission); |
|
2290 |
} |
|
2291 |
} |
|
2292 |
||
2293 |
/* On-screen drawString renders most control chars as the missing glyph |
|
2294 |
* and have the non-zero advance of that glyph. |
|
2295 |
* Exceptions are \t, \n and \r which are considered zero-width. |
|
2296 |
* This is a utility method used by subclasses to remove them so we |
|
2297 |
* don't have to worry about platform or font specific handling of them. |
|
2298 |
*/ |
|
2299 |
protected String removeControlChars(String s) { |
|
2300 |
char[] in_chars = s.toCharArray(); |
|
2301 |
int len = in_chars.length; |
|
2302 |
char[] out_chars = new char[len]; |
|
2303 |
int pos = 0; |
|
2304 |
||
2305 |
for (int i = 0; i < len; i++) { |
|
2306 |
char c = in_chars[i]; |
|
2307 |
if (c > '\r' || c < '\t' || c == '\u000b' || c == '\u000c') { |
|
2308 |
out_chars[pos++] = c; |
|
2309 |
} |
|
2310 |
} |
|
2311 |
if (pos == len) { |
|
2312 |
return s; // no need to make a new String. |
|
2313 |
} else { |
|
2314 |
return new String(out_chars, 0, pos); |
|
2315 |
} |
|
2316 |
} |
|
2317 |
} |