001: /*
002: * Copyright (c) 2004 JETA Software, Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without modification,
005: * are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JETA Software nor the names of its contributors may
015: * be used to endorse or promote products derived from this software without
016: * specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
021: * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
022: * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
023: * INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
024: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
025: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
026: * INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: */
029:
030: package com.jeta.forms.store.properties;
031:
032: import java.awt.Component;
033: import java.awt.Graphics;
034: import java.io.IOException;
035:
036: import javax.swing.Icon;
037: import javax.swing.ImageIcon;
038:
039: import com.jeta.forms.gui.beans.JETABean;
040: import com.jeta.forms.gui.common.FormUtils;
041: import com.jeta.forms.project.ProjectManager;
042: import com.jeta.forms.store.JETAObjectInput;
043: import com.jeta.forms.store.JETAObjectOutput;
044: import com.jeta.open.registry.JETARegistry;
045:
046: /**
047: * A class for handling handling icon properties in a Java bean. All icons are
048: * loaded from the CLASSPATH. In the designer, the user specifies the package
049: * and filename that contains the icon resource for a given Java bean icon
050: * property. This object is responsible for serializing the path to the icon
051: * resource as well as loading the icon from the CLASSPATH at runtime.
052: *
053: * @author Jeff Tassin
054: */
055: public class IconProperty extends JETAProperty implements Icon {
056:
057: static final long serialVersionUID = -7743120720855547949L;
058:
059: /**
060: * The version of this class.
061: */
062: public static final int VERSION = 1;
063:
064: /**
065: * Flag that indicates if the image is stored in the form file or referenced
066: * from the CLASSPATH This is currently not used. All images must be
067: * referenced from the CLASSPATH.
068: */
069: private boolean m_embedded = false;
070:
071: /**
072: * If the image is loaded from the CLASSPATH, this is the package/name where
073: * the image is stored relative to the CLASSPATH
074: */
075: private String m_path;
076:
077: /**
078: * The actual image.
079: */
080: private transient ImageIcon m_image;
081:
082: /**
083: * A description for the image. This is useful if the image is embedded.
084: */
085: private String m_description;
086:
087: /**
088: * Currently not used.
089: */
090: private int[] m_pixels;
091:
092: /**
093: * Creates an unitialized <code>IconProperty</code> instance.
094: */
095: public IconProperty() {
096: }
097:
098: /**
099: * @return the description for this property
100: */
101: public String getDescription() {
102: return m_description;
103: }
104:
105: /**
106: * Returns the underlying image icon.
107: *
108: * @return the underlying image icon;
109: */
110: public ImageIcon imageIcon() {
111: return m_image;
112: }
113:
114: /**
115: * Returns the width of the icon in pixels.
116: *
117: * @return the width of the image in pixels
118: */
119: public int getIconWidth() {
120: return m_image == null ? 0 : m_image.getIconWidth();
121: }
122:
123: /**
124: * Returns the height of the icon in pixels.
125: *
126: * @return the height of the image in pixels
127: */
128: public int getIconHeight() {
129: return m_image == null ? 0 : m_image.getIconHeight();
130: }
131:
132: /**
133: * Returns the path relative to the current CLASSPATH that contains the
134: * icon.
135: *
136: * @return the relative path
137: */
138: public String getRelativePath() {
139: return m_path;
140: }
141:
142: /**
143: * Returns true if this image is embedded. Currently this should always
144: * return false.
145: *
146: * @return true if this image is embedded
147: */
148: public boolean isEmbedded() {
149: return m_embedded;
150: }
151:
152: /**
153: * Loads the image from the project manager and caches the resulting
154: * ImageIcon.
155: */
156: public void loadImage() {
157: try {
158: ProjectManager pmgr = (ProjectManager) JETARegistry
159: .lookup(ProjectManager.COMPONENT_ID);
160: if (pmgr != null && m_path != null && m_path.length() > 0) {
161: m_image = pmgr.loadImage(m_path);
162: }
163: } catch (Exception e) {
164: e.printStackTrace();
165: }
166: }
167:
168: /**
169: * Icon implementation. Renders the icon specified by this property on the
170: * given graphics context.
171: */
172: public void paintIcon(Component c, Graphics g, int x, int y) {
173: if (m_image != null)
174: m_image.paintIcon(c, g, x, y);
175: }
176:
177: /**
178: * Prints this value to the console
179: */
180: public void print() {
181: System.out.println("IconProperty..................");
182: System.out.println(" embedded: " + isEmbedded());
183: System.out.println(" description: " + getDescription());
184: System.out.println(" path: " + getRelativePath());
185: System.out.println(" width: " + getIconWidth());
186: System.out.println(" height: " + getIconHeight());
187: }
188:
189: /**
190: * Sets the flag that indicates if this image is embedded or not Not
191: * currently used. Embedded icons are not currently supported.
192: */
193: public void setEmbedded(boolean bembedded) {
194: m_embedded = false;
195: }
196:
197: /**
198: * Sets the description for this icon. This is only useful for embedded
199: * images.
200: *
201: * @param desc
202: * the description for this icon.
203: */
204: public void setDescription(String desc) {
205: m_description = desc;
206: }
207:
208: /**
209: * Sets the relative path where the icon file is located. This path is
210: * relative to the classpath.
211: *
212: * @param path
213: * the relative path to the icon resource.
214: */
215: public void setRelativePath(String path) {
216: m_path = path;
217: m_image = null;
218: loadImage();
219:
220: if (path != null) {
221: int pos = path.lastIndexOf('\\');
222: pos = Math.max(pos, path.lastIndexOf('/'));
223: if (pos > 0) {
224: m_description = path.substring(pos + 1, path.length());
225: } else {
226: m_description = path;
227: }
228: }
229: }
230:
231: /**
232: * Sets this property to that of another IconProperty.
233: */
234: public void setValue(Object prop) {
235: if (prop == this )
236: return;
237:
238: if (prop instanceof IconProperty) {
239: IconProperty iprop = (IconProperty) prop;
240: m_embedded = false;
241: m_path = iprop.m_path;
242: m_image = iprop.m_image;
243: m_description = iprop.m_description;
244:
245: if (!m_embedded && m_image == null) {
246: loadImage();
247: }
248: } else if (prop == null) {
249: m_path = "";
250: m_image = null;
251: m_description = "";
252: } else {
253: assert (false);
254: }
255: }
256:
257: /**
258: * Updates the bean. No op for this property.
259: */
260: public void updateBean(JETABean jbean) {
261:
262: }
263:
264: /**
265: * Externalizable Implementation
266: */
267: public void read(JETAObjectInput in) throws ClassNotFoundException,
268: IOException {
269: super .read(in.getSuperClassInput());
270: int version = in.readVersion();
271: m_embedded = in.readBoolean("embedded");
272: m_path = in.readString("path");
273: if (FormUtils.isDesignMode()) {
274: m_path = FormUtils.fixPath(m_path);
275: }
276:
277: m_description = in.readString("description");
278: int w = in.readInt("width");
279: int h = in.readInt("height");
280:
281: m_embedded = false;
282: loadImage();
283:
284: }
285:
286: /**
287: * Externalizable Implementation
288: */
289: public void write(JETAObjectOutput out) throws IOException {
290: super .write(out.getSuperClassOutput(JETAProperty.class));
291: out.writeVersion(VERSION);
292: out.writeBoolean("embedded", m_embedded);
293: out.writeObject("path", m_path);
294: out.writeObject("description", m_description);
295:
296: int width = getIconWidth();
297: int height = getIconHeight();
298:
299: out.writeInt("width", width);
300: out.writeInt("height", height);
301:
302: }
303:
304: }
|