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.gui.beans;
031:
032: import java.awt.Image;
033: import java.beans.BeanDescriptor;
034: import java.beans.BeanInfo;
035: import java.beans.IntrospectionException;
036: import java.beans.MethodDescriptor;
037: import java.beans.PropertyDescriptor;
038: import java.util.Collection;
039: import java.util.Iterator;
040: import java.util.TreeMap;
041:
042: import com.jeta.forms.gui.common.FormUtils;
043: import com.jeta.forms.store.properties.JETAProperty;
044:
045: /**
046: * A <code>DynamicBeanInfo</code> is a BeanInfo for a Java Bean that also has
047: * the ability to dynamically add and remove PropertyDescriptors for that bean.
048: *
049: * @author Jeff Tassin
050: */
051: public class DynamicBeanInfo {
052: /**
053: * The actual BeanInfo for the Java Bean.
054: */
055: private BeanInfo m_delegate;
056:
057: /**
058: * An ordered set of JETAPropertyDescriptor objects m_props<String,PropertyDescriptor>
059: * where String is the property name.
060: */
061: private TreeMap m_props = new TreeMap();
062:
063: /**
064: * Creates a <code>DynamicBeanInfo</code> instance with the specified
065: * BeanInfo delegate.
066: *
067: * @param delegate
068: * the actual BeanInfo for the associated Java Bean class.
069: */
070: public DynamicBeanInfo(BeanInfo delegate)
071: throws IntrospectionException, IllegalAccessException {
072: this (delegate, null);
073: }
074:
075: /**
076: * Creates a <code>DynamicBeanInfo</code> instance with the specified
077: * BeanInfo delegate and adds a set of custom properties for the associated
078: * Java Bean class.
079: *
080: * @param delegate
081: * the default BeanInfo for the bean.
082: * @param customProps
083: * a collection of JETAProperty objects that are in addition to
084: * the default properties for the bean.
085: */
086: public DynamicBeanInfo(BeanInfo delegate, Collection customProps)
087: throws IntrospectionException, IllegalAccessException {
088: m_delegate = delegate;
089:
090: PropertyDescriptor[] pds = delegate.getPropertyDescriptors();
091: for (int index = 0; index < pds.length; index++) {
092: StandardPropertyDescriptor std_pds = new StandardPropertyDescriptor(
093: pds[index]);
094: m_props.put(std_pds.getName(), std_pds);
095: }
096:
097: if (customProps != null) {
098: Iterator iter = customProps.iterator();
099: while (iter.hasNext()) {
100: JETAProperty prop = (JETAProperty) iter.next();
101: FormUtils.safeAssert(prop.getName() != null);
102: m_props.put(prop.getName(),
103: new DynamicPropertyDescriptor(prop.getName(),
104: prop.getClass(), prop.isPreferred(),
105: prop.isTransient()));
106: }
107: }
108: /** this is not used in the designer */
109: removePropertyDescriptor("debugGraphicsOptions");
110: }
111:
112: /**
113: * BeanInfo implementation. Just forward the call to the delegate.
114: */
115: public int getDefaultPropertyIndex() {
116: return m_delegate.getDefaultPropertyIndex();
117: }
118:
119: /**
120: * BeanInfo implementation. Just forward the call to the delegate.
121: */
122: public Image getIcon(int i) {
123: return m_delegate.getIcon(i);
124: }
125:
126: /**
127: * BeanInfo implementation. Just forward the call to the delegate.
128: */
129: public BeanDescriptor getBeanDescriptor() {
130: return m_delegate.getBeanDescriptor();
131: }
132:
133: /**
134: * BeanInfo implementation. Just forward the call to the delegate.
135: */
136: public BeanInfo[] getAdditionalBeanInfo() {
137: return m_delegate.getAdditionalBeanInfo();
138: }
139:
140: /**
141: * BeanInfo implementation. Just forward the call to the delegate.
142: */
143: public MethodDescriptor[] getMethodDescriptors() {
144: return m_delegate.getMethodDescriptors();
145: }
146:
147: /**
148: * Return the property descriptor with the given name. Null is returned if
149: * the descriptor is not found.
150: *
151: * @return a the property descriptor with the given name.
152: */
153: public JETAPropertyDescriptor getPropertyDescriptor(String propName) {
154: return (JETAPropertyDescriptor) m_props.get(propName);
155: }
156:
157: /**
158: * Returns the property descriptors for the Java Bean class associated with
159: * this bean information.
160: *
161: * @return a collection of JETAPropertyDescriptor objects
162: */
163: public Collection getPropertyDescriptors() {
164: return m_props.values();
165: }
166:
167: /**
168: * Registers a property descriptor for this info object. This allows us to
169: * add properties dynamically.
170: *
171: * @param prop
172: * the property descriptor to register with this bean
173: * information.
174: */
175: void register(JETAPropertyDescriptor prop) {
176: if (prop != null) {
177: m_props.put(prop.getName(), prop);
178: }
179: }
180:
181: /**
182: * Removes a property descriptor from this info object. This allows us to
183: * remove properties that are either deprecated or irrelevent for the
184: * designer.
185: *
186: * @param propName
187: * the name of the property descriptor to remove.
188: */
189: void removePropertyDescriptor(String propName) {
190: m_props.remove(propName);
191: }
192: }
|