KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nextapp > echo2 > app > AwtImageReference


1 /*
2  * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3  * Copyright (C) 2002-2005 NextApp, Inc.
4  *
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the MPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the MPL, the GPL or the LGPL.
28  */

29
30 package nextapp.echo2.app;
31
32 import java.awt.Image JavaDoc;
33 import java.awt.Toolkit JavaDoc;
34 import java.awt.image.ColorModel JavaDoc;
35 import java.awt.image.ImageObserver JavaDoc;
36 import java.awt.image.MemoryImageSource JavaDoc;
37 import java.awt.image.PixelGrabber JavaDoc;
38 import java.io.IOException JavaDoc;
39 import java.io.ObjectInputStream JavaDoc;
40 import java.io.ObjectOutputStream JavaDoc;
41
42 /**
43  * An ImageReference describing an image which may be rendered from
44  * a <code>java.awt.Image</code>.
45  * Note that the JVM running the Echo Application Container will require
46  * access to a graphics context for the Java AWT to function.
47  */

48 public class AwtImageReference
49 implements ImageReference {
50
51     private transient Image JavaDoc image;
52     private String JavaDoc id;
53     
54     /**
55      * Default constructor for use only when a class is derived from
56      * <code>AwtImageReference</code> and the
57      * <code>getImage()</code> method is overridden.
58      */

59     public AwtImageReference() {
60         this(null);
61     }
62     
63     /**
64      * Creates an <code>AwtImageReference</code> to the specified
65      * <code>java.awt.Image</code>.
66      * Note that changes to the underlying image will not necessarily be
67      * reflected on the client unless the image-containing property of the
68      * target component is update.
69      *
70      * @param image the <code>java.awt.Image </code>to be displayed.
71      */

72     public AwtImageReference(Image JavaDoc image) {
73         super();
74         this.image = image;
75         id = ApplicationInstance.generateSystemId();
76     }
77     
78     /**
79      * @see java.lang.Object#equals(java.lang.Object)
80      */

81     public boolean equals(Object JavaDoc o) {
82         if (!(o instanceof AwtImageReference)) {
83             return false;
84         }
85         AwtImageReference that = (AwtImageReference) o;
86         if (!(this.image == that.image || (this.image != null && this.image.equals(that.image)))) {
87             return false;
88         }
89         return true;
90     }
91
92     /**
93      * @see nextapp.echo2.app.ImageReference#getHeight()
94      */

95     public Extent getHeight() {
96         if (image == null) {
97             return null;
98         }
99         int height = image.getHeight(null);
100         if (height > 0) {
101             return new Extent(height, Extent.PX);
102         } else {
103             return null;
104         }
105     }
106     
107     /**
108      * @see nextapp.echo2.app.RenderIdSupport#getRenderId()
109      */

110     public String JavaDoc getRenderId() {
111         return id;
112     }
113
114     /**
115      * Retrieves the image. Calls to this method will be minimized such that
116      * applications may extend this class and override this method such that
117      * images are created only when they are needed, thereby reducing memory
118      * usage at the cost of increased processor workload.
119      * You should also override the <code>getWidth()</code> and
120      * <code>getHeight()</code> methods.
121      */

122     public Image JavaDoc getImage() {
123         return image;
124     }
125     
126     /**
127      * @see nextapp.echo2.app.ImageReference#getWidth()
128      */

129     public Extent getWidth() {
130         if (image == null) {
131             return null;
132         }
133         int width = image.getWidth(null);
134         if (width > 0) {
135             return new Extent(width, Extent.PX);
136         } else {
137             return null;
138         }
139     }
140
141     /**
142      * @see java.io.Serializable
143      */

144     private void readObject(ObjectInputStream JavaDoc in)
145     throws IOException JavaDoc, ClassNotFoundException JavaDoc {
146         in.defaultReadObject();
147         
148         int width = in.readInt();
149         int height = in.readInt();
150         int[] pixels = (int[]) in.readObject();
151         
152         if (pixels != null) {
153             Toolkit JavaDoc toolkit = Toolkit.getDefaultToolkit();
154             ColorModel JavaDoc colorModel = ColorModel.getRGBdefault();
155             image = toolkit.createImage(new MemoryImageSource JavaDoc(width, height, colorModel, pixels, 0, width));
156         }
157     }
158
159     /**
160      * @see java.io.Serializable
161      */

162     private void writeObject(ObjectOutputStream JavaDoc out)
163     throws IOException JavaDoc {
164         out.defaultWriteObject();
165         
166         int width = image.getWidth(null);
167         int height = image.getHeight(null);
168         
169         out.writeInt(width);
170         out.writeInt(height);
171         
172         if (image == null) {
173             out.writeObject(null);
174         } else {
175             int[] pixels = new int[width * height];
176             try {
177                 PixelGrabber JavaDoc pg = new PixelGrabber JavaDoc(image, 0, 0, width, height, pixels, 0, width);
178                 pg.grabPixels();
179                 if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
180                     throw new IOException JavaDoc("Unable to serialize java.awt.image: PixelGrabber aborted.");
181                 }
182             } catch (InterruptedException JavaDoc ex) {
183                 throw new IOException JavaDoc("Unable to serialize java.awt.Image: PixelGrabber interrupted.");
184             }
185             out.writeObject(pixels);
186         }
187     }
188 }
189
Popular Tags