KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > image > ImagePrintSupport


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 /** Printing support.
35  * Manipulations of the image to suit page size and orientation
36  * occur through the method prepareImage( PageFormat ).
37  * Subclass' override this method to honour image handling.
38  *
39  * @author michael wever [hair@netbeans.org]
40  * @author Marian Petras
41  * @version $Revision: 1.5 $
42  */

43 public class ImagePrintSupport implements PrintCookie, Printable, ImageObserver {
44     /* associated dataObject */
45     protected ImageDataObject dataObject;
46     /* image to print */
47     protected Image image;
48     /* image to print */
49     protected RenderedImage renderedImage;
50     
51     /** Creates new ImagePrintSupport */
52     public ImagePrintSupport( ImageDataObject ido ) {
53         dataObject = ido;
54     }
55     
56     /** Prepare the image to fit on the given page, within the given margins.
57      * Returns null if it were unable to prepare the image for the given page.
58      * Throws a IllegalArgumentException if the page were too small for the image.
59      **/

60     protected static RenderedImage transformImage(RenderedImage image,
61                                                   PageFormat pf)
62             throws IllegalArgumentException JavaDoc {
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             /** notify if too big for page **/
71             if( pf.getImageableWidth() - pf.getImageableX() < image.getWidth()
72                 || pf.getImageableHeight() - pf.getImageableY() < image.getHeight() )
73                     throw new IllegalArgumentException JavaDoc("Page too small for image"); //NOI18N
74

75             /* Translate image */
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 JavaDoc ex){
81             ex.printStackTrace();
82         }
83         return null;
84     }
85     
86     /** Print the content of the object. */
87     public void print() {
88         
89         /* Try to load the image from the ImageDataObject: */
90         String JavaDoc errMsgKey;
91         try {
92             image = dataObject.getImage();
93             errMsgKey = (image == null) ? "MSG_CouldNotLoad" : null; //NOI18N
94
} catch (IOException ex) {
95             image = null;
96             errMsgKey = "MSG_ErrorWhileLoading"; //NOI18N
97
}
98         assert (image == null) != (errMsgKey == null);
99         
100         /* If an error occured during loading, display a message and quit: */
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         // Print
113
try {
114             if (image instanceof RenderedImage) {
115                 // Make sure not to print in the paper's margin.
116
renderedImage = transformImage((RenderedImage) image, pf);
117             }
118             if (job.printDialog()) {
119                 job.print();
120             }
121         } catch (PrinterAbortException e) { // user exception
122
displayMessage("CTL_Printer_Abort", //NOI18N
123
NotifyDescriptor.INFORMATION_MESSAGE);
124         } catch (Exception JavaDoc e) {
125             ErrorManager.getDefault().notify(e);
126         } finally {
127             renderedImage = null;
128             image = null;
129         }
130     }
131     
132     /**
133      * Displays a localized user message.
134      * It is guaranteed that the displaying routine is called from
135      * the AWT event dispatching thread.
136      *
137      * @param msgKey bundle key of the message
138      * @param msgType message type - see {@link NotifyDescriptor} fields
139      */

140     private void displayMessage(String JavaDoc msgKey, final int msgType) {
141         final String JavaDoc msg = NbBundle.getMessage(ImagePrintSupport.class, msgKey);
142         java.awt.EventQueue.invokeLater(new Runnable JavaDoc() { // display in the awt thread
143
public void run() {
144                 DialogDisplayer.getDefault().notify(
145                         new NotifyDescriptor.Message(msg, msgType));
146             }
147         });
148     }
149     
150     /* Implements Printable */
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             /**
157              * most probably cause is image does not implement RenderedImage,
158              * just draw the image then.
159              **/

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 JavaDoc image, int flags, int param2, int param3, int param4, int param5) {
168         return false;
169     }
170     
171 }
172
Popular Tags