1 /*
   2  * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
   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
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  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  *
  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.
  24  */
  25 
  26 package sun.lwawt.macosx;
  27 
  28 
  29 import java.awt.*;
  30 import java.awt.geom.Rectangle2D;
  31 import java.awt.image.BufferedImage;
  32 import java.awt.print.*;
  33 
  34 import javax.print.*;
  35 import javax.print.attribute.PrintRequestAttributeSet;
  36 import javax.print.attribute.standard.PageRanges;
  37 
  38 import sun.java2d.*;
  39 import sun.print.*;
  40 
  41 public class CPrinterJob extends RasterPrinterJob {
  42     // NOTE: This uses RasterPrinterJob as a base, but it doesn't use
  43     // all of the RasterPrinterJob functions. RasterPrinterJob will
  44     // break down printing to pieces that aren't necessary under MacOSX
  45     // printing, such as controlling the # of copies and collating. These
  46     // are handled by the native printing. RasterPrinterJob is kept for
  47     // future compatibility and the state keeping that it handles.
  48 
  49     private static String sShouldNotReachHere = "Should not reach here.";
  50 
  51     private boolean noDefaultPrinter = false;
  52 
  53     private static Font defaultFont;
  54 
  55     // This is the NSPrintInfo for this PrinterJob. Protect multi thread
  56     //  access to it. It is used by the pageDialog, jobDialog, and printLoop.
  57     //  This way the state of these items is shared across these calls.
  58     //  PageFormat data is passed in and set on the fNSPrintInfo on a per call
  59     //  basis.
  60     private long fNSPrintInfo = -1;
  61     private Object fNSPrintInfoLock = new Object();
  62 
  63     static {
  64         // AWT has to be initialized for the native code to function correctly.
  65         Toolkit.getDefaultToolkit();
  66     }
  67 
  68     /**
  69      * Presents a dialog to the user for changing the properties of
  70      * the print job.
  71      * This method will display a native dialog if a native print
  72      * service is selected, and user choice of printers will be restricted
  73      * to these native print services.
  74      * To present the cross platform print dialog for all services,
  75      * including native ones instead use
  76      * <code>printDialog(PrintRequestAttributeSet)</code>.
  77      * <p>
  78      * PrinterJob implementations which can use PrintService's will update
  79      * the PrintService for this PrinterJob to reflect the new service
  80      * selected by the user.
  81      * @return <code>true</code> if the user does not cancel the dialog;
  82      * <code>false</code> otherwise.
  83      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  84      * returns true.
  85      * @see java.awt.GraphicsEnvironment#isHeadless
  86      */
  87     public boolean printDialog() throws HeadlessException {
  88         if (GraphicsEnvironment.isHeadless()) {
  89             throw new HeadlessException();
  90         }
  91 
  92         if (noDefaultPrinter) {
  93             return false;
  94         }
  95 
  96         return jobSetup(getPageable(), checkAllowedToPrintToFile());
  97     }
  98 
  99     /**
 100      * Displays a dialog that allows modification of a
 101      * <code>PageFormat</code> instance.
 102      * The <code>page</code> argument is used to initialize controls
 103      * in the page setup dialog.
 104      * If the user cancels the dialog then this method returns the
 105      * original <code>page</code> object unmodified.
 106      * If the user okays the dialog then this method returns a new
 107      * <code>PageFormat</code> object with the indicated changes.
 108      * In either case, the original <code>page</code> object is
 109      * not modified.
 110      * @param page the default <code>PageFormat</code> presented to the
 111      *            user for modification
 112      * @return    the original <code>page</code> object if the dialog
 113      *            is cancelled; a new <code>PageFormat</code> object
 114      *          containing the format indicated by the user if the
 115      *          dialog is acknowledged.
 116      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
 117      * returns true.
 118      * @see java.awt.GraphicsEnvironment#isHeadless
 119      * @since     1.2
 120      */
 121     public PageFormat pageDialog(PageFormat page) throws HeadlessException {
 122         if (GraphicsEnvironment.isHeadless()) {
 123             throw new HeadlessException();
 124         }
 125 
 126         if (noDefaultPrinter) {
 127             return page;
 128         }
 129 
 130         PageFormat pageClone = (PageFormat) page.clone();
 131         boolean doIt = pageSetup(pageClone, null);
 132         return doIt ? pageClone : page;
 133     }
 134 
 135     /**
 136      * Clones the <code>PageFormat</code> argument and alters the
 137      * clone to describe a default page size and orientation.
 138      * @param page the <code>PageFormat</code> to be cloned and altered
 139      * @return clone of <code>page</code>, altered to describe a default
 140      *                      <code>PageFormat</code>.
 141      */
 142     public PageFormat defaultPage(PageFormat page) {
 143         PageFormat newPage = (PageFormat)page.clone();
 144         getDefaultPage(newPage);
 145         return newPage;
 146     }
 147 
 148     protected void setAttributes(PrintRequestAttributeSet attributes) throws PrinterException {
 149         if (attributes != null) {
 150             PageRanges pageRangesAttr =
 151                 (PageRanges)attributes.get(PageRanges.class);
 152             if (pageRangesAttr != null) {
 153                 SunPageSelection psel = (SunPageSelection)attributes.get(SunPageSelection.class);
 154                 if (psel == null) {
 155                     attributes.add(SunPageSelection.RANGE);
 156                 }
 157             }
 158         }
 159 
 160         super.setAttributes(attributes);
 161 
 162         if (attributes == null) {
 163             return;
 164         }
 165 
 166         // See if this has an NSPrintInfo in it.
 167         NSPrintInfo nsPrintInfo = (NSPrintInfo)attributes.get(NSPrintInfo.class);
 168         if (nsPrintInfo != null) {
 169             fNSPrintInfo = nsPrintInfo.getValue();
 170         }
 171     }
 172 
 173     volatile boolean onEventThread;
 174 
 175     private void completePrintLoop() {
 176         Runnable r = new Runnable() { public void run() {
 177             synchronized(this) {
 178                 performingPrinting = false;
 179             }
 180         }};
 181 
 182         if (onEventThread) {
 183             try { EventQueue.invokeAndWait(r); } catch (Exception e) { e.printStackTrace(); }
 184         } else {
 185             r.run();
 186         }
 187     }
 188 
 189 
 190     public void print(PrintRequestAttributeSet attributes) throws PrinterException {
 191         // NOTE: Some of this code is copied from RasterPrinterJob.
 192 
 193 
 194         // this code uses javax.print APIs
 195         // this will make it print directly to the printer
 196         // this will not work if the user clicks on the "Preview" button
 197         // However if the printer is a StreamPrintService, its the right path.
 198         PrintService psvc = getPrintService();
 199         if (psvc instanceof StreamPrintService) {
 200             spoolToService(psvc, attributes);
 201             return;
 202         }
 203 
 204 
 205         setAttributes(attributes);
 206 
 207         /* Get the range of pages we are to print. If the
 208          * last page to print is unknown, then we print to
 209          * the end of the document. Note that firstPage
 210          * and lastPage are 0 based page indices.
 211          */
 212         int numPages = mDocument.getNumberOfPages();
 213 
 214         int firstPage = getFirstPage();
 215         int lastPage = getLastPage();
 216         if(lastPage == Pageable.UNKNOWN_NUMBER_OF_PAGES) {
 217             int totalPages = mDocument.getNumberOfPages();
 218             if (totalPages != Pageable.UNKNOWN_NUMBER_OF_PAGES) {
 219                 lastPage = mDocument.getNumberOfPages() - 1;
 220             }
 221         }
 222 
 223         try {
 224             synchronized (this) {
 225                 performingPrinting = true;
 226                 userCancelled = false;
 227             }
 228 
 229             if (EventQueue.isDispatchThread()) {
 230                 // This is an AWT EventQueue, and this print rendering loop needs to block it.
 231 
 232                 onEventThread = true;
 233 
 234                 try {
 235                     // Fire off the print rendering loop on the AppKit thread, and don't have
 236                     //  it wait and block this thread.
 237                     if (printLoop(false, firstPage, lastPage)) {
 238                         // Fire off the EventConditional that will what until the condition is met,
 239                         //  but will still process AWTEvent's as they occur.
 240                         new EventDispatchAccess() {
 241                             public boolean evaluate() {
 242                                 return performingPrinting;
 243                             }
 244                         }.pumpEventsAndWait();
 245                     }
 246                 } catch (Exception e) {
 247                     e.printStackTrace();
 248                 }
 249             } else {
 250                 // Fire off the print rendering loop on the AppKit, and block this thread
 251                 //  until it is done.
 252                 // But don't actually block... we need to come back here!
 253                 onEventThread = false;
 254 
 255                 try {
 256                     printLoop(true, firstPage, lastPage);
 257                 } catch (Exception e) {
 258                     e.printStackTrace();
 259                 }
 260             }
 261         } finally {
 262             synchronized (this) {
 263                 // NOTE: Native code shouldn't allow exceptions out while
 264                 // printing. They should cancel the print loop.
 265                 performingPrinting = false;
 266                 notify();
 267             }
 268         }
 269 
 270         // Normalize the collated, # copies, numPages, first/last pages. Need to
 271         //  make note of pageRangesAttr.
 272 
 273         // Set up NSPrintInfo with the java settings (PageFormat & Paper).
 274 
 275         // Create an NSView for printing. Have knowsPageRange return YES, and give the correct
 276         //  range, or MAX? if unknown. Have rectForPage do a peekGraphics check before returning
 277         //  the rectangle. Have drawRect do the real render of the page. Have printJobTitle do
 278         //  the right thing.
 279 
 280         // Call NSPrintOperation, it will call NSView.drawRect: for each page.
 281 
 282         // NSView.drawRect: will create a CPrinterGraphics with the current CGContextRef, and then
 283         //  pass this Graphics onto the Printable with the appropriate PageFormat and index.
 284 
 285         // Need to be able to cancel the NSPrintOperation (using code from RasterPrinterJob, be
 286         //  sure to initialize userCancelled and performingPrinting member variables).
 287 
 288         // Extensions available from AppKit: Print to PDF or EPS file!
 289     }
 290 
 291     /**
 292      * Returns the resolution in dots per inch across the width
 293      * of the page.
 294      */
 295     protected double getXRes() {
 296         // NOTE: This is not used in the CPrinterJob code path.
 297         return 0;
 298     }
 299 
 300     /**
 301      * Returns the resolution in dots per inch down the height
 302      * of the page.
 303      */
 304     protected double getYRes() {
 305         // NOTE: This is not used in the CPrinterJob code path.
 306         return 0;
 307     }
 308 
 309     /**
 310      * Must be obtained from the current printer.
 311      * Value is in device pixels.
 312      * Not adjusted for orientation of the paper.
 313      */
 314     protected double getPhysicalPrintableX(Paper p) {
 315         // NOTE: This is not used in the CPrinterJob code path.
 316         return 0;
 317     }
 318 
 319     /**
 320      * Must be obtained from the current printer.
 321      * Value is in device pixels.
 322      * Not adjusted for orientation of the paper.
 323      */
 324     protected double getPhysicalPrintableY(Paper p) {
 325         // NOTE: This is not used in the CPrinterJob code path.
 326         return 0;
 327     }
 328 
 329     /**
 330      * Must be obtained from the current printer.
 331      * Value is in device pixels.
 332      * Not adjusted for orientation of the paper.
 333      */
 334     protected double getPhysicalPrintableWidth(Paper p) {
 335         // NOTE: This is not used in the CPrinterJob code path.
 336         return 0;
 337     }
 338 
 339     /**
 340      * Must be obtained from the current printer.
 341      * Value is in device pixels.
 342      * Not adjusted for orientation of the paper.
 343      */
 344     protected double getPhysicalPrintableHeight(Paper p) {
 345         // NOTE: This is not used in the CPrinterJob code path.
 346         return 0;
 347     }
 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     protected double getPhysicalPageWidth(Paper p) {
 355         // NOTE: This is not used in the CPrinterJob code path.
 356         return 0;
 357     }
 358 
 359     /**
 360      * Must be obtained from the current printer.
 361      * Value is in device pixels.
 362      * Not adjusted for orientation of the paper.
 363      */
 364     protected double getPhysicalPageHeight(Paper p) {
 365         // NOTE: This is not used in the CPrinterJob code path.
 366         return 0;
 367     }
 368 
 369     /**
 370      * Begin a new page. This call's Window's
 371      * StartPage routine.
 372      */
 373     protected void startPage(PageFormat format, Printable painter, int index) throws PrinterException {
 374         // NOTE: This is not used in the CPrinterJob code path.
 375         throw new PrinterException(sShouldNotReachHere);
 376     }
 377 
 378     /**
 379      * End a page.
 380      */
 381     protected void endPage(PageFormat format, Printable painter, int index) throws PrinterException {
 382         // NOTE: This is not used in the CPrinterJob code path.
 383         throw new PrinterException(sShouldNotReachHere);
 384     }
 385 
 386     /**
 387      * Prints the contents of the array of ints, 'data'
 388      * to the current page. The band is placed at the
 389      * location (x, y) in device coordinates on the
 390      * page. The width and height of the band is
 391      * specified by the caller.
 392      */
 393     protected void printBand(byte[] data, int x, int y, int width, int height) throws PrinterException {
 394         // NOTE: This is not used in the CPrinterJob code path.
 395         throw new PrinterException(sShouldNotReachHere);
 396     }
 397 
 398     /**
 399      * Called by the print() method at the start of
 400      * a print job.
 401      */
 402     protected void startDoc() throws PrinterException {
 403         // NOTE: This is not used in the CPrinterJob code path.
 404         throw new PrinterException(sShouldNotReachHere);
 405     }
 406 
 407     /**
 408      * Called by the print() method at the end of
 409      * a print job.
 410      */
 411     protected void endDoc() throws PrinterException {
 412         // NOTE: This is not used in the CPrinterJob code path.
 413         throw new PrinterException(sShouldNotReachHere);
 414     }
 415 
 416     /* Called by cancelDoc */
 417     protected native void abortDoc();
 418 
 419     /**
 420      * Displays the page setup dialog placing the user's
 421      * settings into 'page'.
 422      */
 423     public boolean pageSetup(PageFormat page, Printable painter) {
 424         CPrinterDialog printerDialog = new CPrinterPageDialog(null, this, page, painter);
 425         printerDialog.setVisible(true);
 426         boolean result = printerDialog.getRetVal();
 427         printerDialog.dispose();
 428         return result;
 429     }
 430 
 431     /**
 432      * Displays the print dialog and records the user's settings
 433      * into this object. Return false if the user cancels the
 434      * dialog.
 435      * If the dialog is to use a set of attributes, useAttributes is true.
 436      */
 437     private boolean jobSetup(Pageable doc, boolean allowPrintToFile) {
 438         CPrinterDialog printerDialog = new CPrinterJobDialog(null, this, doc, allowPrintToFile);
 439         printerDialog.setVisible(true);
 440         boolean result = printerDialog.getRetVal();
 441         printerDialog.dispose();
 442         return result;
 443     }
 444 
 445     /**
 446      * Alters the orientation and Paper to match defaults obtained
 447      * from a printer.
 448      */
 449     private native void getDefaultPage(PageFormat page);
 450 
 451     /**
 452      * validate the paper size against the current printer.
 453      */
 454     protected native void validatePaper(Paper origPaper, Paper newPaper );
 455 
 456     // The following methods are CPrinterJob specific.
 457 
 458     protected void finalize() {
 459         if (fNSPrintInfo != -1) {
 460             dispose(fNSPrintInfo);
 461         }
 462     }
 463 
 464     private native long createNSPrintInfo();
 465     private native void dispose(long printInfo);
 466 
 467     private long getNSPrintInfo() {
 468         // This is called from the native side.
 469         synchronized (fNSPrintInfoLock) {
 470             if (fNSPrintInfo == -1) {
 471                 fNSPrintInfo = createNSPrintInfo();
 472             }
 473             return fNSPrintInfo;
 474         }
 475     }
 476 
 477     private native boolean printLoop(boolean waitUntilDone, int firstPage, int lastPage) throws PrinterException;
 478 
 479     private PageFormat getPageFormat(int pageIndex) {
 480         // This is called from the native side.
 481         PageFormat page;
 482         try {
 483             page = getPageable().getPageFormat(pageIndex);
 484         } catch (Exception e) {
 485             return null;
 486         }
 487         return page;
 488     }
 489 
 490     private Printable getPrintable(int pageIndex) {
 491         // This is called from the native side.
 492         Printable painter;
 493         try {
 494             painter = getPageable().getPrintable(pageIndex);
 495         } catch (Exception e) {
 496             return null;
 497         }
 498         return painter;
 499     }
 500 
 501     private String getPrinterName(){
 502         // This is called from the native side.
 503         PrintService service = getPrintService();
 504         if (service == null) return null;
 505         return service.getName();
 506     }
 507 
 508     private void setPrinterServiceFromNative(String printerName) {
 509         // This is called from the native side.
 510         PrintService[] services = PrintServiceLookup.lookupPrintServices(DocFlavor.SERVICE_FORMATTED.PAGEABLE, null);
 511 
 512         for (int i = 0; i < services.length; i++) {
 513             PrintService service = services[i];
 514 
 515             if (printerName.equals(service.getName())) {
 516                 try {
 517                     setPrintService(service);
 518                 } catch (PrinterException e) {
 519                     // ignored
 520                 }
 521                 return;
 522             }
 523         }
 524     }
 525 
 526     private Rectangle2D getPageFormatArea(PageFormat page) {
 527         Rectangle2D.Double pageFormatArea =
 528             new Rectangle2D.Double(page.getImageableX(),
 529                     page.getImageableY(),
 530                     page.getImageableWidth(),
 531                     page.getImageableHeight());
 532         return pageFormatArea;
 533     }
 534 
 535     private boolean cancelCheck() {
 536         // This is called from the native side.
 537 
 538         // This is used to avoid deadlock
 539         // We would like to just call if isCancelled(),
 540         // but that will block the AppKit thread against whomever is holding the synchronized lock
 541         boolean cancelled = (performingPrinting && userCancelled);
 542         if (cancelled) {
 543             try {
 544                 LWCToolkit.invokeLater(new Runnable() { public void run() {
 545                     try {
 546                     cancelDoc();
 547                     } catch (PrinterAbortException pae) {
 548                         // no-op, let the native side handle it
 549                     }
 550                 }}, null);
 551             } catch (java.lang.reflect.InvocationTargetException ite) {}
 552         }
 553         return cancelled;
 554     }
 555 
 556     private PeekGraphics createFirstPassGraphics(PrinterJob printerJob, PageFormat page) {
 557         // This is called from the native side.
 558         BufferedImage bimg = new BufferedImage((int)Math.round(page.getWidth()), (int)Math.round(page.getHeight()), BufferedImage.TYPE_INT_ARGB_PRE);
 559         PeekGraphics peekGraphics = createPeekGraphics(bimg.createGraphics(), printerJob);
 560         Rectangle2D pageFormatArea = getPageFormatArea(page);
 561         initPrinterGraphics(peekGraphics, pageFormatArea);
 562         return peekGraphics;
 563     }
 564 
 565     private void printToPathGraphics(    final PeekGraphics graphics, // Always an actual PeekGraphics
 566                                         final PrinterJob printerJob, // Always an actual CPrinterJob
 567                                         final Printable painter, // Client class
 568                                         final PageFormat page, // Client class
 569                                         final int pageIndex,
 570                                         final long context) throws PrinterException {
 571         // This is called from the native side.
 572         Runnable r = new Runnable() { public void run() {
 573             try {
 574                 SurfaceData sd = CPrinterSurfaceData.createData(page, context); // Just stores page into an ivar
 575                 if (defaultFont == null) {
 576                     defaultFont = new Font("Dialog", Font.PLAIN, 12);
 577                 }
 578                 Graphics2D delegate = new SunGraphics2D(sd, Color.black, Color.white, defaultFont);
 579 
 580                 Graphics2D pathGraphics = new CPrinterGraphics(delegate, printerJob); // Just stores delegate into an ivar
 581                 Rectangle2D pageFormatArea = getPageFormatArea(page);
 582                 initPrinterGraphics(pathGraphics, pageFormatArea);
 583                 painter.print(pathGraphics, page, pageIndex);
 584                 delegate.dispose();
 585                 delegate = null;
 586         } catch (PrinterException pe) { throw new java.lang.reflect.UndeclaredThrowableException(pe); }
 587         }};
 588 
 589         if (onEventThread) {
 590             try { EventQueue.invokeAndWait(r);
 591             } catch (java.lang.reflect.InvocationTargetException ite) {
 592                 Throwable te = (Throwable)ite.getTargetException();
 593                 if (te instanceof PrinterException) throw (PrinterException)te;
 594                 else te.printStackTrace();
 595             } catch (Exception e) { e.printStackTrace(); }
 596         } else {
 597             r.run();
 598         }
 599 
 600     }
 601 
 602     // Returns either 1. an array of 3 object (PageFormat, Printable, PeekGraphics) or 2. null
 603     private Object[] getPageformatPrintablePeekgraphics(final int pageIndex) {
 604         final Object[] ret = new Object[3];
 605         final PrinterJob printerJob = this;
 606 
 607         Runnable r = new Runnable() { public void run() { synchronized(ret) {
 608             try {
 609                 Pageable pageable = getPageable();
 610                 PageFormat pageFormat = pageable.getPageFormat(pageIndex);
 611                 if (pageFormat != null) {
 612                     Printable printable = pageable.getPrintable(pageIndex);
 613                     if (printable != null) {
 614                         BufferedImage bimg = new BufferedImage((int)Math.round(pageFormat.getWidth()), (int)Math.round(pageFormat.getHeight()), BufferedImage.TYPE_INT_ARGB_PRE);
 615                         PeekGraphics peekGraphics = createPeekGraphics(bimg.createGraphics(), printerJob);
 616                         Rectangle2D pageFormatArea = getPageFormatArea(pageFormat);
 617                         initPrinterGraphics(peekGraphics, pageFormatArea);
 618 
 619                         // Do the assignment here!
 620                         ret[0] = pageFormat;
 621                         ret[1] = printable;
 622                         ret[2] = peekGraphics;
 623                     }
 624                 }
 625             } catch (Exception e) {} // Original code bailed on any exception
 626         }}};
 627 
 628         if (onEventThread) {
 629             try { EventQueue.invokeAndWait(r); } catch (Exception e) { e.printStackTrace(); }
 630         } else {
 631             r.run();
 632         }
 633 
 634         synchronized(ret) {
 635             if (ret[2] != null)
 636                 return ret;
 637             return null;
 638         }
 639     }
 640 
 641     private Rectangle2D printAndGetPageFormatArea(final Printable printable, final Graphics graphics, final PageFormat pageFormat, final int pageIndex) {
 642         final Rectangle2D[] ret = new Rectangle2D[1];
 643 
 644         Runnable r = new Runnable() { public void run() { synchronized(ret) {
 645             try {
 646                 int pageResult = printable.print(graphics, pageFormat, pageIndex);
 647                 if (pageResult != Printable.NO_SUCH_PAGE) {
 648                     ret[0] = getPageFormatArea(pageFormat);
 649                 }
 650             } catch (Exception e) {} // Original code bailed on any exception
 651         }}};
 652 
 653         if (onEventThread) {
 654             try { EventQueue.invokeAndWait(r); } catch (Exception e) { e.printStackTrace(); }
 655         } else {
 656             r.run();
 657         }
 658 
 659         synchronized(ret) { return ret[0]; }
 660     }
 661 
 662     // upcall from native
 663     private static void detachPrintLoop(final long target, final long arg) {
 664         new Thread() { public void run() {
 665             _safePrintLoop(target, arg);
 666         }}.start();
 667     }
 668     private static native void _safePrintLoop(long target, long arg);
 669 
 670     @Override
 671     protected void startPage(PageFormat arg0, Printable arg1, int arg2, boolean arg3) throws PrinterException {
 672         // TODO Auto-generated method stub
 673     }
 674 }