414
415 /**
416 * save graphics state of a PathGraphics for later redrawing
417 * of part of page represented by the region in that state
418 */
419
420 public void saveState(AffineTransform at, Shape clip,
421 Rectangle2D region, double sx, double sy) {
422 GraphicsState gstate = new GraphicsState();
423 gstate.theTransform = at;
424 gstate.theClip = clip;
425 gstate.region = region;
426 gstate.sx = sx;
427 gstate.sy = sy;
428 redrawList.add(gstate);
429 }
430
431
432 /*
433 * A convenience method which returns the default service
434 * for 2D <code>PrinterJob</code>s.
435 * May return null if there is no suitable default (although there
436 * may still be 2D services available).
437 * @return default 2D print service, or null.
438 * @since 1.4
439 */
440 protected static PrintService lookupDefaultPrintService() {
441 PrintService service = PrintServiceLookup.lookupDefaultPrintService();
442
443 /* Pageable implies Printable so checking both isn't strictly needed */
444 if (service != null &&
445 service.isDocFlavorSupported(
446 DocFlavor.SERVICE_FORMATTED.PAGEABLE) &&
447 service.isDocFlavorSupported(
448 DocFlavor.SERVICE_FORMATTED.PRINTABLE)) {
449 return service;
450 } else {
451 PrintService []services =
452 PrintServiceLookup.lookupPrintServices(
453 DocFlavor.SERVICE_FORMATTED.PAGEABLE, null);
454 if (services.length > 0) {
478 }
479 }
480 if (myService == null) {
481 PrintService[] svcs = PrintServiceLookup.lookupPrintServices(
482 DocFlavor.SERVICE_FORMATTED.PAGEABLE, null);
483 if (svcs.length > 0) {
484 try {
485 setPrintService(svcs[0]);
486 myService = svcs[0];
487 } catch (PrinterException e) {
488 }
489 }
490 }
491 }
492 return myService;
493 }
494
495 /**
496 * Associate this PrinterJob with a new PrintService.
497 *
498 * Throws <code>PrinterException</code> if the specified service
499 * cannot support the <code>Pageable</code> and
500 * <code>Printable</code> interfaces necessary to support 2D printing.
501 * @param service print service which supports 2D printing.
502 *
503 * @throws PrinterException if the specified service does not support
504 * 2D printing or no longer available.
505 */
506 public void setPrintService(PrintService service)
507 throws PrinterException {
508 if (service == null) {
509 throw new PrinterException("Service cannot be null");
510 } else if (!(service instanceof StreamPrintService) &&
511 service.getName() == null) {
512 throw new PrinterException("Null PrintService name.");
513 } else {
514 // Check the list of services. This service may have been
515 // deleted already
516 PrinterState prnState = service.getAttribute(PrinterState.class);
517 if (prnState == PrinterState.STOPPED) {
518 PrinterStateReasons prnStateReasons =
519 service.getAttribute(PrinterStateReasons.class);
520 if ((prnStateReasons != null) &&
677 if (media != null) {
678 pageAttributes.add(media);
679 }
680 pageAttributes.add(orient);
681
682 float ix = (float)(page.getPaper().getImageableX()/DPI);
683 float iw = (float)(page.getPaper().getImageableWidth()/DPI);
684 float iy = (float)(page.getPaper().getImageableY()/DPI);
685 float ih = (float)(page.getPaper().getImageableHeight()/DPI);
686 if (ix < 0) ix = 0f; if (iy < 0) iy = 0f;
687 try {
688 pageAttributes.add(new MediaPrintableArea(ix, iy, iw, ih,
689 MediaPrintableArea.INCH));
690 } catch (IllegalArgumentException iae) {
691 }
692 }
693
694 /**
695 * Display a dialog to the user allowing the modification of a
696 * PageFormat instance.
697 * The <code>page</code> argument is used to initialize controls
698 * in the page setup dialog.
699 * If the user cancels the dialog, then the method returns the
700 * original <code>page</code> object unmodified.
701 * If the user okays the dialog then the method returns a new
702 * PageFormat object with the indicated changes.
703 * In either case the original <code>page</code> object will
704 * not be modified.
705 * @param page the default PageFormat presented to the user
706 * for modification
707 * @return the original <code>page</code> object if the dialog
708 * is cancelled, or a new PageFormat object containing
709 * the format indicated by the user if the dialog is
710 * acknowledged
711 * @exception HeadlessException if GraphicsEnvironment.isHeadless()
712 * returns true.
713 * @see java.awt.GraphicsEnvironment#isHeadless
714 * @since 1.2
715 */
716 public PageFormat pageDialog(PageFormat page)
717 throws HeadlessException {
718 if (GraphicsEnvironment.isHeadless()) {
719 throw new HeadlessException();
720 }
721
722 final GraphicsConfiguration gc =
723 GraphicsEnvironment.getLocalGraphicsEnvironment().
724 getDefaultScreenDevice().getDefaultConfiguration();
725
726 PrintService service = java.security.AccessController.doPrivileged(
727 new java.security.PrivilegedAction<PrintService>() {
2314
2315 boolean cancelled = false;
2316
2317 synchronized (this) {
2318 cancelled = (performingPrinting && userCancelled);
2319 notify();
2320 }
2321
2322 return cancelled;
2323 }
2324
2325 /**
2326 * Return the Pageable describing the pages to be printed.
2327 */
2328 protected Pageable getPageable() {
2329 return mDocument;
2330 }
2331
2332 /**
2333 * Examine the metrics captured by the
2334 * <code>PeekGraphics</code> instance and
2335 * if capable of directly converting this
2336 * print job to the printer's control language
2337 * or the native OS's graphics primitives, then
2338 * return a <code>PathGraphics</code> to perform
2339 * that conversion. If there is not an object
2340 * capable of the conversion then return
2341 * <code>null</code>. Returning <code>null</code>
2342 * causes the print job to be rasterized.
2343 */
2344 protected Graphics2D createPathGraphics(PeekGraphics graphics,
2345 PrinterJob printerJob,
2346 Printable painter,
2347 PageFormat pageFormat,
2348 int pageIndex) {
2349
2350 return null;
2351 }
2352
2353 /**
2354 * Create and return an object that will
2355 * gather and hold metrics about the print
2356 * job. This method is passed a <code>Graphics2D</code>
2357 * object that can be used as a proxy for the
2358 * object gathering the print job matrics. The
2359 * method is also supplied with the instance
2360 * controlling the print job, <code>printerJob</code>.
2361 */
2362 protected PeekGraphics createPeekGraphics(Graphics2D graphics,
2363 PrinterJob printerJob) {
2364
2365 return new PeekGraphics(graphics, printerJob);
2366 }
2367
2368 /**
2369 * Configure the passed in Graphics2D so that
2370 * is contains the defined initial settings
2371 * for a print job. These settings are:
2372 * color: black.
2373 * clip: <as passed in>
2374 */
2375 // MacOSX - made protected so subclasses can reference it.
2376 protected void initPrinterGraphics(Graphics2D g, Rectangle2D clip) {
2377
2378 g.setClip(clip);
2379 g.setPaint(Color.black);
2380 }
|
414
415 /**
416 * save graphics state of a PathGraphics for later redrawing
417 * of part of page represented by the region in that state
418 */
419
420 public void saveState(AffineTransform at, Shape clip,
421 Rectangle2D region, double sx, double sy) {
422 GraphicsState gstate = new GraphicsState();
423 gstate.theTransform = at;
424 gstate.theClip = clip;
425 gstate.region = region;
426 gstate.sx = sx;
427 gstate.sy = sy;
428 redrawList.add(gstate);
429 }
430
431
432 /*
433 * A convenience method which returns the default service
434 * for 2D {@code PrinterJob}s.
435 * May return null if there is no suitable default (although there
436 * may still be 2D services available).
437 * @return default 2D print service, or null.
438 * @since 1.4
439 */
440 protected static PrintService lookupDefaultPrintService() {
441 PrintService service = PrintServiceLookup.lookupDefaultPrintService();
442
443 /* Pageable implies Printable so checking both isn't strictly needed */
444 if (service != null &&
445 service.isDocFlavorSupported(
446 DocFlavor.SERVICE_FORMATTED.PAGEABLE) &&
447 service.isDocFlavorSupported(
448 DocFlavor.SERVICE_FORMATTED.PRINTABLE)) {
449 return service;
450 } else {
451 PrintService []services =
452 PrintServiceLookup.lookupPrintServices(
453 DocFlavor.SERVICE_FORMATTED.PAGEABLE, null);
454 if (services.length > 0) {
478 }
479 }
480 if (myService == null) {
481 PrintService[] svcs = PrintServiceLookup.lookupPrintServices(
482 DocFlavor.SERVICE_FORMATTED.PAGEABLE, null);
483 if (svcs.length > 0) {
484 try {
485 setPrintService(svcs[0]);
486 myService = svcs[0];
487 } catch (PrinterException e) {
488 }
489 }
490 }
491 }
492 return myService;
493 }
494
495 /**
496 * Associate this PrinterJob with a new PrintService.
497 *
498 * Throws {@code PrinterException} if the specified service
499 * cannot support the {@code Pageable} and
500 * {@code Printable} interfaces necessary to support 2D printing.
501 * @param service print service which supports 2D printing.
502 *
503 * @throws PrinterException if the specified service does not support
504 * 2D printing or no longer available.
505 */
506 public void setPrintService(PrintService service)
507 throws PrinterException {
508 if (service == null) {
509 throw new PrinterException("Service cannot be null");
510 } else if (!(service instanceof StreamPrintService) &&
511 service.getName() == null) {
512 throw new PrinterException("Null PrintService name.");
513 } else {
514 // Check the list of services. This service may have been
515 // deleted already
516 PrinterState prnState = service.getAttribute(PrinterState.class);
517 if (prnState == PrinterState.STOPPED) {
518 PrinterStateReasons prnStateReasons =
519 service.getAttribute(PrinterStateReasons.class);
520 if ((prnStateReasons != null) &&
677 if (media != null) {
678 pageAttributes.add(media);
679 }
680 pageAttributes.add(orient);
681
682 float ix = (float)(page.getPaper().getImageableX()/DPI);
683 float iw = (float)(page.getPaper().getImageableWidth()/DPI);
684 float iy = (float)(page.getPaper().getImageableY()/DPI);
685 float ih = (float)(page.getPaper().getImageableHeight()/DPI);
686 if (ix < 0) ix = 0f; if (iy < 0) iy = 0f;
687 try {
688 pageAttributes.add(new MediaPrintableArea(ix, iy, iw, ih,
689 MediaPrintableArea.INCH));
690 } catch (IllegalArgumentException iae) {
691 }
692 }
693
694 /**
695 * Display a dialog to the user allowing the modification of a
696 * PageFormat instance.
697 * The {@code page} argument is used to initialize controls
698 * in the page setup dialog.
699 * If the user cancels the dialog, then the method returns the
700 * original {@code page} object unmodified.
701 * If the user okays the dialog then the method returns a new
702 * PageFormat object with the indicated changes.
703 * In either case the original {@code page} object will
704 * not be modified.
705 * @param page the default PageFormat presented to the user
706 * for modification
707 * @return the original {@code page} object if the dialog
708 * is cancelled, or a new PageFormat object containing
709 * the format indicated by the user if the dialog is
710 * acknowledged
711 * @exception HeadlessException if GraphicsEnvironment.isHeadless()
712 * returns true.
713 * @see java.awt.GraphicsEnvironment#isHeadless
714 * @since 1.2
715 */
716 public PageFormat pageDialog(PageFormat page)
717 throws HeadlessException {
718 if (GraphicsEnvironment.isHeadless()) {
719 throw new HeadlessException();
720 }
721
722 final GraphicsConfiguration gc =
723 GraphicsEnvironment.getLocalGraphicsEnvironment().
724 getDefaultScreenDevice().getDefaultConfiguration();
725
726 PrintService service = java.security.AccessController.doPrivileged(
727 new java.security.PrivilegedAction<PrintService>() {
2314
2315 boolean cancelled = false;
2316
2317 synchronized (this) {
2318 cancelled = (performingPrinting && userCancelled);
2319 notify();
2320 }
2321
2322 return cancelled;
2323 }
2324
2325 /**
2326 * Return the Pageable describing the pages to be printed.
2327 */
2328 protected Pageable getPageable() {
2329 return mDocument;
2330 }
2331
2332 /**
2333 * Examine the metrics captured by the
2334 * {@code PeekGraphics} instance and
2335 * if capable of directly converting this
2336 * print job to the printer's control language
2337 * or the native OS's graphics primitives, then
2338 * return a {@code PathGraphics} to perform
2339 * that conversion. If there is not an object
2340 * capable of the conversion then return
2341 * {@code null}. Returning {@code null}
2342 * causes the print job to be rasterized.
2343 */
2344 protected Graphics2D createPathGraphics(PeekGraphics graphics,
2345 PrinterJob printerJob,
2346 Printable painter,
2347 PageFormat pageFormat,
2348 int pageIndex) {
2349
2350 return null;
2351 }
2352
2353 /**
2354 * Create and return an object that will
2355 * gather and hold metrics about the print
2356 * job. This method is passed a {@code Graphics2D}
2357 * object that can be used as a proxy for the
2358 * object gathering the print job matrics. The
2359 * method is also supplied with the instance
2360 * controlling the print job, {@code printerJob}.
2361 */
2362 protected PeekGraphics createPeekGraphics(Graphics2D graphics,
2363 PrinterJob printerJob) {
2364
2365 return new PeekGraphics(graphics, printerJob);
2366 }
2367
2368 /**
2369 * Configure the passed in Graphics2D so that
2370 * is contains the defined initial settings
2371 * for a print job. These settings are:
2372 * color: black.
2373 * clip: <as passed in>
2374 */
2375 // MacOSX - made protected so subclasses can reference it.
2376 protected void initPrinterGraphics(Graphics2D g, Rectangle2D clip) {
2377
2378 g.setClip(clip);
2379 g.setPaint(Color.black);
2380 }
|