001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.image;
043:
044: import java.awt.Image;
045: import java.io.IOException;
046: import java.io.Serializable;
047: import javax.swing.ImageIcon;
048:
049: /**
050: * ImageIcon with serialization.
051: *
052: * @author Petr Hamernik, Michael Wever
053: * @author Marian Petras
054: */
055: class NBImageIcon extends ImageIcon implements Serializable {
056:
057: /** generated Serialized Version UID */
058: static final long serialVersionUID = -1730253055388017036L;
059:
060: /** Appropriate image data object */
061: ImageDataObject obj;
062:
063: /**
064: * Loads an image from an <code>ImageDataObject</code>.
065: * If an error occures during reading the image, an exception is thrown.
066: * If the image format is not supported, <code>null</code> is returned.
067: *
068: * @param obj <code>ImageDataObject</code> to load the image from
069: * @return loaded image if loaded successfully,
070: * or <code>null</code> if no registered <code>ImageReader</code>
071: * claims to be able to read the image
072: * @exception java.io.IOException
073: * if an error occurs during reading the image
074: * @see javax.imageio.ImageIO#read(java.io.InputStream)
075: */
076: public static NBImageIcon load(ImageDataObject obj)
077: throws IOException {
078: Image image = obj.getImage();
079: return (image != null) ? new NBImageIcon(obj, image) : null;
080: }
081:
082: /** Construct a new icon.
083: * @param obj the data object to represent the image in
084: */
085: private NBImageIcon(ImageDataObject obj, Image image) {
086: //super(obj.getImageURL()); // PENDING for the time URL is incorrectly cached (in Toolkit)
087: super (image); //mw
088: this .obj = obj;
089: }
090:
091: /** Get an object to be written to the stream instead of this object. */
092: public Object writeReplace() {
093: return new ResolvableHelper(obj);
094: }
095:
096: /** Helper class for serialization. */
097: static class ResolvableHelper implements Serializable {
098:
099: /** generated Serialized Version UID. */
100: static final long serialVersionUID = -1120520132882774882L;
101:
102: /** serializable data object. */
103: ImageDataObject obj;
104:
105: /** Constructs ResolvableHelper object for given ImageDataObject. */
106: ResolvableHelper(ImageDataObject obj) {
107: this .obj = obj;
108: }
109:
110: /** Restore with the same data object. */
111: public Object readResolve() {
112: Image image;
113: try {
114: image = obj.getImage();
115: } catch (IOException ex) {
116: image = null;
117: }
118: return new NBImageIcon(obj, (image != null) ? image
119: : new ImageIcon().getImage());
120: }
121: } // End of nested class ResolvableHelper.
122: }
|