KickJava   Java API By Example, From Geeks To Geeks.

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


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.Image JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.Serializable JavaDoc;
27 import javax.swing.ImageIcon JavaDoc;
28
29
30 /**
31  * ImageIcon with serialization.
32  *
33  * @author Petr Hamernik, Michael Wever
34  * @author Marian Petras
35  */

36 class NBImageIcon extends ImageIcon JavaDoc implements Serializable JavaDoc {
37
38     /** generated Serialized Version UID */
39     static final long serialVersionUID = -1730253055388017036L;
40
41     /** Appropriate image data object */
42     ImageDataObject obj;
43
44     /**
45      * Loads an image from an <code>ImageDataObject</code>.
46      * If an error occures during reading the image, an exception is thrown.
47      * If the image format is not supported, <code>null</code> is returned.
48      *
49      * @param obj <code>ImageDataObject</code> to load the image from
50      * @return loaded image if loaded successfully,
51      * or <code>null</code> if no registered <code>ImageReader</code>
52      * claims to be able to read the image
53      * @exception java.io.IOException
54      * if an error occurs during reading the image
55      * @see javax.imageio.ImageIO#read(java.io.InputStream)
56      */

57     public static NBImageIcon load(ImageDataObject obj) throws IOException JavaDoc {
58         Image JavaDoc image = obj.getImage();
59         return (image != null) ? new NBImageIcon(obj, image) : null;
60     }
61     
62     /** Construct a new icon.
63      * @param obj the data object to represent the image in
64      */

65     private NBImageIcon(ImageDataObject obj, Image JavaDoc image) {
66         //super(obj.getImageURL()); // PENDING for the time URL is incorrectly cached (in Toolkit)
67
super(image); //mw
68
this.obj = obj;
69     }
70     
71     
72     /** Get an object to be written to the stream instead of this object. */
73     public Object JavaDoc writeReplace() {
74         return new ResolvableHelper(obj);
75     }
76
77     
78     /** Helper class for serialization. */
79     static class ResolvableHelper implements Serializable JavaDoc {
80         
81         /** generated Serialized Version UID. */
82         static final long serialVersionUID = -1120520132882774882L;
83         
84         /** serializable data object. */
85         ImageDataObject obj;
86         
87         /** Constructs ResolvableHelper object for given ImageDataObject. */
88         ResolvableHelper(ImageDataObject obj) {
89             this.obj = obj;
90         }
91
92         /** Restore with the same data object. */
93         public Object JavaDoc readResolve() {
94             Image JavaDoc image;
95             try {
96                 image = obj.getImage();
97             } catch (IOException JavaDoc ex) {
98                 image = null;
99             }
100             return new NBImageIcon(
101                     obj,
102                     (image != null) ? image : new ImageIcon JavaDoc().getImage());
103         }
104     } // End of nested class ResolvableHelper.
105
}
106
Popular Tags