001: /*
002: * Copyright (C) 2005 Jeff Tassin
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package com.jeta.swingbuilder.store;
020:
021: import java.awt.Graphics2D;
022: import java.awt.image.BufferedImage;
023: import java.io.Externalizable;
024: import java.io.IOException;
025:
026: import javax.swing.Icon;
027: import javax.swing.ImageIcon;
028:
029: import com.jeta.forms.store.memento.IconMemento;
030:
031: /**
032: * Represents an imported java bean used in the builder.
033: *
034: * @author Jeff Tassin
035: */
036: public class ImportedBeanInfo implements Externalizable,
037: RegisteredBean, Comparable, Cloneable {
038: static final long serialVersionUID = -6517746440901771760L;
039:
040: public static final int VERSION = 2;
041:
042: /**
043: * The class name of the imported java bean
044: */
045: private String m_bean_name;
046:
047: /**
048: * Used to store the icon for this bean
049: */
050: private IconMemento m_icon_memento;
051:
052: /**
053: * Flag that indicates if this bean is scrollable
054: */
055: private boolean m_scrollable;
056:
057: /**
058: * This is a scaled version of the m_icon_memento icon. It is scaled to
059: * 16x16 pixels if the icon_memento is not exactly 16x16. We do this so it
060: * will fit on the toolbar.
061: */
062: private transient ImageIcon m_scaled_icon;
063:
064: /**
065: * ctor - for serialization
066: */
067: public ImportedBeanInfo() {
068:
069: }
070:
071: /**
072: * ctor
073: */
074: public ImportedBeanInfo(String beanName, boolean scrollable) {
075: m_bean_name = beanName;
076: m_scrollable = scrollable;
077: }
078:
079: /**
080: * @return the icon
081: */
082: public Icon getIcon() {
083: Icon result = null;
084: if (m_scaled_icon == null) {
085: if (m_icon_memento != null) {
086: ImageIcon ii = m_icon_memento.getImageIcon();
087: if (ii != null) {
088: if ((ii.getIconWidth() == 16)
089: && (ii.getIconHeight() == 16)) {
090: m_scaled_icon = ii;
091: } else {
092: // we need to scale the image to fit 16x16 pixels if it
093: // is too big or too small
094: BufferedImage bimage = new BufferedImage(16,
095: 16, BufferedImage.TYPE_INT_RGB);
096: Graphics2D bg = bimage.createGraphics();
097: bg.drawImage(ii.getImage(), 0, 0, 16, 16, null);
098: bg.dispose();
099: m_scaled_icon = new ImageIcon(bimage);
100: }
101: }
102: }
103: }
104: return m_scaled_icon;
105: }
106:
107: public String getDescription() {
108: return getBeanName();
109: }
110:
111: public String getClassName() {
112: return getBeanName();
113: }
114:
115: /**
116: * @return the class name of the imported java bean
117: */
118: public String getBeanName() {
119: return m_bean_name;
120: }
121:
122: /**
123: * @return true if this component is scrollable
124: */
125: public boolean isScrollable() {
126: return m_scrollable;
127: }
128:
129: /**
130: * Sets the class name of the imported java bean
131: */
132: public void setBeanName(String name) {
133: m_bean_name = name;
134: }
135:
136: /**
137: * Sets the icon memento for this object
138: */
139: public void setIconMemento(IconMemento im) {
140: m_icon_memento = im;
141: m_scaled_icon = null;
142: }
143:
144: /**
145: */
146: public void setScrollable(boolean scrollable) {
147: m_scrollable = scrollable;
148: }
149:
150: public boolean equals(Object obj) {
151: if (this == obj)
152: return true;
153:
154: if (null == obj)
155: return false;
156:
157: if (!(obj instanceof ImportedBeanInfo))
158: return false;
159:
160: ImportedBeanInfo mObj = (ImportedBeanInfo) obj;
161:
162: if ((m_scrollable == mObj.m_scrollable)
163: && ((null == m_bean_name && null == mObj.m_bean_name)
164: || (m_bean_name != null && m_bean_name
165: .equals(mObj.m_bean_name)) || (mObj.m_bean_name != null && mObj.m_bean_name
166: .equals(m_bean_name)))
167: && ((null == m_icon_memento && null == mObj.m_icon_memento)
168: || (m_icon_memento != null && m_icon_memento
169: .equals(mObj.m_icon_memento)) || (mObj.m_icon_memento != null && mObj.m_icon_memento
170: .equals(m_icon_memento)))) {
171: return true;
172: } else {
173: return false;
174: }
175: }
176:
177: public int compareTo(Object o) throws ClassCastException {
178: if (!(o instanceof ImportedBeanInfo))
179: throw new ClassCastException(
180: "An ImportedBeanInfo object was expected.");
181: ImportedBeanInfo beanInfo = (ImportedBeanInfo) o;
182: return this .m_bean_name.compareTo(beanInfo.getBeanName());
183: }
184:
185: public Object clone() {
186: ImportedBeanInfo other = null;
187: try {
188: other = (ImportedBeanInfo) super .clone();
189: //
190: other.m_bean_name = m_bean_name;
191: other.m_scrollable = m_scrollable;
192: //
193: m_scaled_icon = null;
194: getIcon();
195: other.m_scaled_icon = m_scaled_icon;
196: m_scaled_icon = null;
197: getIcon();
198: //
199: other.m_icon_memento = (m_icon_memento != null ? (IconMemento) m_icon_memento
200: .clone()
201: : (IconMemento) null);
202: } catch (CloneNotSupportedException e) {
203: // this shouldn't happen, since we are Cloneable
204: throw new InternalError();
205: }
206: //
207: return other;
208: }
209:
210: /**
211: * Externalizable Implementation
212: */
213: public void readExternal(java.io.ObjectInput in)
214: throws ClassNotFoundException, IOException {
215: int version = in.readInt();
216: m_bean_name = (String) in.readObject();
217: m_icon_memento = (IconMemento) in.readObject();
218: if (version >= 2)
219: m_scrollable = in.readBoolean();
220: }
221:
222: /**
223: * Externalizable Implementation
224: */
225: public void writeExternal(java.io.ObjectOutput out)
226: throws IOException {
227: out.writeInt(VERSION);
228: out.writeObject(m_bean_name);
229: out.writeObject(m_icon_memento);
230: out.writeBoolean(m_scrollable);
231: }
232: }
|