KickJava   Java API By Example, From Geeks To Geeks.

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


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
21 package org.netbeans.modules.image;
22
23
24 import java.awt.Graphics JavaDoc;
25 import java.awt.Image JavaDoc;
26 import java.awt.Rectangle JavaDoc;
27 import java.beans.PropertyEditor JavaDoc;
28 import java.beans.PropertyEditorSupport JavaDoc;
29 import java.io.BufferedInputStream JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.lang.reflect.InvocationTargetException JavaDoc;
32 import java.net.URL JavaDoc;
33 import javax.swing.Icon JavaDoc;
34 import javax.swing.ImageIcon JavaDoc;
35
36 import org.openide.actions.OpenAction;
37 import org.openide.filesystems.FileObject;
38 import org.openide.filesystems.FileStateInvalidException;
39 import org.openide.loaders.*;
40 import org.openide.nodes.*;
41 import org.openide.ErrorManager;
42 import org.openide.util.actions.SystemAction;
43 import org.openide.util.HelpCtx;
44 import org.openide.util.NbBundle;
45
46
47 /**
48  * Object that represents one file containing an image.
49  * @author Petr Hamernik, Jaroslav Tulach, Ian Formanek, Michael Wever
50  * @author Marian Petras
51  */

52 public class ImageDataObject extends MultiDataObject implements CookieSet.Factory {
53     
54     /** Generated serialized version UID. */
55     static final long serialVersionUID = -6035788991669336965L;
56
57     /** Base for image resource. */
58     private static final String JavaDoc IMAGE_ICON_BASE = "org/netbeans/modules/image/imageObject.png"; // NOI18N
59

60     /** Open support for this image data object. */
61     private transient ImageOpenSupport openSupport;
62     /** Print support for this image data object **/
63     private transient ImagePrintSupport printSupport;
64  
65     /** Constructor.
66      * @param pf primary file object for this data object
67      * @param loader the data loader creating it
68      * @exception DataObjectExistsException if there was already a data object for it
69      */

70     public ImageDataObject(FileObject pf, MultiFileLoader loader) throws DataObjectExistsException {
71         super(pf, loader);
72         
73         getCookieSet().add(ImageOpenSupport.class, this);
74         getCookieSet().add(ImagePrintSupport.class, this);
75     }
76
77
78     /** Implements <code>CookieSet.Factory</code> interface. */
79     public Node.Cookie createCookie(Class JavaDoc clazz) {
80         if(clazz.isAssignableFrom(ImageOpenSupport.class))
81             return getOpenSupport();
82         else if( clazz.isAssignableFrom(ImagePrintSupport.class))
83             return getPrintSupport();
84         else
85             return null;
86     }
87     
88     /** Gets image open support. */
89     private ImageOpenSupport getOpenSupport() {
90         if(openSupport == null) {
91             synchronized(this) {
92                 if(openSupport == null)
93                     openSupport = new ImageOpenSupport(getPrimaryEntry());
94             }
95         }
96         
97         return openSupport;
98     }
99
100     protected ImagePrintSupport getPrintSupport(){
101         if(printSupport == null) {
102             synchronized(this) {
103                 if(printSupport == null)
104                     printSupport = new ImagePrintSupport( this );
105             }
106         }
107         return printSupport;
108     }
109     
110     /** Help context for this object.
111      * @return the help context
112      */

113     public HelpCtx getHelpCtx() {
114         return HelpCtx.DEFAULT_HELP;
115     }
116
117     /** Get a URL for the image.
118      * @return the image url
119      */

120     URL JavaDoc getImageURL() {
121         try {
122             return getPrimaryFile().getURL();
123         } catch (FileStateInvalidException ex) {
124             return null;
125         }
126     }
127
128     /** Gets image data for the image object.
129      * @return the image data
130      * @deprecated use getImage() instead
131      */

132     private byte[] getImageData() {
133         try {
134             FileObject fo = getPrimaryFile();
135             byte[] imageData = new byte[(int)fo.getSize()];
136             BufferedInputStream JavaDoc in = new BufferedInputStream JavaDoc(fo.getInputStream());
137             in.read(imageData, 0, (int)fo.getSize());
138             in.close();
139             return imageData;
140         } catch(IOException JavaDoc ioe) {
141             return new byte[0];
142         }
143     }
144
145     // Michael Wever 26/09/2001
146
/** Gets image for the image data
147      * @return the image or <code>null</code> if image could not be created
148      * @return java.io.IOException if an error occurs during reading
149      */

150     public Image JavaDoc getImage() throws IOException JavaDoc {
151         return javax.imageio.ImageIO.read(getPrimaryFile().getInputStream());
152     }
153
154
155     /** Create a node to represent the image. Overrides superclass method.
156      * @return node delegate */

157     protected Node createNodeDelegate () {
158         return new ImageNode(this);
159     }
160     
161     
162     /** Node representing <code>ImageDataObject</code>. */
163     private static final class ImageNode extends DataNode {
164         /** Constructs image node. */
165         public ImageNode(ImageDataObject obj) {
166             super(obj, Children.LEAF);
167             //setIconBase(IMAGE_ICON_BASE);
168
setIconBaseWithExtension(IMAGE_ICON_BASE);
169             setDefaultAction (SystemAction.get (OpenAction.class));
170         }
171         
172         /** Creates property sheet. Ovrrides superclass method. */
173         protected Sheet createSheet() {
174             Sheet s = super.createSheet();
175             Sheet.Set ss = s.get(Sheet.PROPERTIES);
176             if (ss == null) {
177                 ss = Sheet.createPropertiesSet();
178                 s.put(ss);
179             }
180             ss.put(new ThumbnailProperty(getDataObject()));
181             return s;
182         }
183         
184
185         /** Property representing for thumbanil property in the sheet. */
186         private static final class ThumbnailProperty extends PropertySupport.ReadOnly {
187             /** (Image) data object associated with. */
188             private final DataObject obj;
189             
190             /** Constructs property. */
191             public ThumbnailProperty(DataObject obj) {
192                 super("thumbnail", Icon JavaDoc.class, // NOI18N
193
NbBundle.getMessage(ImageDataObject.class, "PROP_Thumbnail"),
194                     NbBundle.getMessage(ImageDataObject.class, "HINT_Thumbnail"));
195                 this.obj = obj;
196             }
197             
198             /** Gets value of property. Overrides superclass method. */
199             public Object JavaDoc getValue() throws InvocationTargetException JavaDoc {
200                 try {
201                     return new ImageIcon JavaDoc(obj.getPrimaryFile().getURL());
202                 } catch (FileStateInvalidException fsie) {
203                     throw new InvocationTargetException JavaDoc(fsie);
204                 }
205             }
206             
207             /** Gets property editor. */
208             public PropertyEditor JavaDoc getPropertyEditor() {
209                 return new ThumbnailPropertyEditor();
210             }
211             
212             
213             /** Property editor for thumbnail property. */
214             private final class ThumbnailPropertyEditor extends PropertyEditorSupport JavaDoc {
215                 /** Overrides superclass method.
216                  * @return <code>true</code> */

217                 public boolean isPaintable() {
218                     return true;
219                 }
220                 
221                 /** Patins thumbanil of the image. Overrides superclass method. */
222                 public void paintValue(Graphics JavaDoc g, Rectangle JavaDoc r) {
223                     ImageIcon JavaDoc icon = null;
224                     
225                     try {
226                         icon = (ImageIcon JavaDoc)ThumbnailProperty.this.getValue();
227                     } catch(InvocationTargetException JavaDoc ioe) {
228                         if(Boolean.getBoolean("netbeans.debug.exceptions")) { // NOI18N
229
ErrorManager.getDefault().notify(ioe);
230                         }
231                     }
232                     
233                     if(icon != null) {
234                         int iconWidth = icon.getIconWidth();
235                         int iconHeight = icon.getIconHeight();
236                         
237
238                         // Shrink image if necessary.
239
double scale = (double)iconWidth / iconHeight;
240                         
241                         if(iconWidth > r.width) {
242                             iconWidth = r.width;
243                             iconHeight = (int) (iconWidth / scale);
244                         }
245
246                         if(iconHeight > r.height) {
247                             iconHeight = r.height;
248                             iconWidth = (int) (iconHeight * scale);
249                         }
250                         
251                         // Try to center it if it fits, else paint as much as possible.
252
int x;
253                         if(iconWidth < r.x) {
254                             x = (r.x - iconWidth) / 2;
255                         } else {
256                             x = 5; // XXX Indent.
257
}
258                         
259                         int y;
260                         if(iconHeight < r.y) {
261                             y = (r.y - iconHeight) / 2;
262                         } else {
263                             y = 0;
264                         }
265                         
266                         Graphics JavaDoc g2 = g.create(r.x, r.y, r.width, r.height);
267                         g.drawImage(icon.getImage(), x, y, iconWidth, iconHeight, null);
268                     }
269                 }
270
271                 /** Overrides superclass method.
272                  * @return <code>null</code> */

273                 public String JavaDoc getAsText() {
274                     return null;
275                 }
276             } // End of class ThumbnailPropertyEditor.
277
} // End of class ThumbnailProperty.
278
} // End of class ImageNode.
279

280 }
281
Popular Tags