1 19 20 package org.netbeans.modules.image; 21 22 import java.awt.*; 23 import java.awt.image.*; 24 import java.awt.geom.*; 25 import java.awt.print.*; 26 import java.io.*; 27 28 import org.openide.NotifyDescriptor; 29 import org.openide.ErrorManager; 30 import org.openide.DialogDisplayer; 31 import org.openide.cookies.PrintCookie; 32 import org.openide.util.NbBundle; 33 34 43 public class ImagePrintSupport implements PrintCookie, Printable, ImageObserver { 44 45 protected ImageDataObject dataObject; 46 47 protected Image image; 48 49 protected RenderedImage renderedImage; 50 51 52 public ImagePrintSupport( ImageDataObject ido ) { 53 dataObject = ido; 54 } 55 56 60 protected static RenderedImage transformImage(RenderedImage image, 61 PageFormat pf) 62 throws IllegalArgumentException { 63 try{ 64 AffineTransform af = new AffineTransform(); 65 if( pf.getOrientation() == pf.LANDSCAPE ){ 66 }else{ 67 af.translate( (double)pf.getImageableX(), (double)pf.getImageableY() ); 68 } 69 70 71 if( pf.getImageableWidth() - pf.getImageableX() < image.getWidth() 72 || pf.getImageableHeight() - pf.getImageableY() < image.getHeight() ) 73 throw new IllegalArgumentException ("Page too small for image"); 75 76 AffineTransformOp afo = new AffineTransformOp( af, AffineTransformOp.TYPE_NEAREST_NEIGHBOR ); 77 BufferedImage o = (BufferedImage)image; 78 BufferedImage i = new BufferedImage( o.getWidth()+(int)pf.getImageableX(), o.getHeight()+(int)pf.getImageableY(), o.getType() ); 79 return afo.filter( (BufferedImage)image, i ); 80 }catch(Exception ex){ 81 ex.printStackTrace(); 82 } 83 return null; 84 } 85 86 87 public void print() { 88 89 90 String errMsgKey; 91 try { 92 image = dataObject.getImage(); 93 errMsgKey = (image == null) ? "MSG_CouldNotLoad" : null; } catch (IOException ex) { 95 image = null; 96 errMsgKey = "MSG_ErrorWhileLoading"; } 98 assert (image == null) != (errMsgKey == null); 99 100 101 if (errMsgKey != null) { 102 displayMessage(errMsgKey, NotifyDescriptor.WARNING_MESSAGE); 103 return; 104 } 105 106 PrinterJob job = PrinterJob.getPrinterJob(); 107 Book book = new Book(); 108 PageFormat pf = org.openide.text.PrintSettings.getPageFormat(job); 109 book.append( this, pf ); 110 job.setPageable( book ); 111 112 try { 114 if (image instanceof RenderedImage) { 115 renderedImage = transformImage((RenderedImage) image, pf); 117 } 118 if (job.printDialog()) { 119 job.print(); 120 } 121 } catch (PrinterAbortException e) { displayMessage("CTL_Printer_Abort", NotifyDescriptor.INFORMATION_MESSAGE); 124 } catch (Exception e) { 125 ErrorManager.getDefault().notify(e); 126 } finally { 127 renderedImage = null; 128 image = null; 129 } 130 } 131 132 140 private void displayMessage(String msgKey, final int msgType) { 141 final String msg = NbBundle.getMessage(ImagePrintSupport.class, msgKey); 142 java.awt.EventQueue.invokeLater(new Runnable () { public void run() { 144 DialogDisplayer.getDefault().notify( 145 new NotifyDescriptor.Message(msg, msgType)); 146 } 147 }); 148 } 149 150 151 public int print(Graphics graphics, PageFormat pageFormat, int page) throws PrinterException { 152 if( page != 0 ) return Printable.NO_SUCH_PAGE; 153 154 Graphics2D g2 = (Graphics2D)graphics; 155 if( renderedImage == null ){ 156 160 graphics.drawImage(image, (int)pageFormat.getImageableX(), (int)pageFormat.getImageableY(), this ); 161 }else{ 162 g2.drawRenderedImage( renderedImage, new AffineTransform() ); 163 } 164 return Printable.PAGE_EXISTS; 165 } 166 167 public boolean imageUpdate(java.awt.Image image, int flags, int param2, int param3, int param4, int param5) { 168 return false; 169 } 170 171 } 172 | Popular Tags |