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.factories;
031:
032: import java.awt.Component;
033: import java.beans.BeanInfo;
034: import java.beans.Introspector;
035:
036: import com.jeta.forms.gui.beans.BeanProperties;
037: import com.jeta.forms.gui.beans.DynamicBeanInfo;
038: import com.jeta.forms.gui.beans.JETABean;
039: import com.jeta.forms.gui.common.FormException;
040: import com.jeta.forms.logger.FormsLogger;
041: import com.jeta.forms.store.properties.CompoundBorderProperty;
042: import com.jeta.forms.store.properties.DefaultBorderProperty;
043: import com.jeta.forms.store.properties.ScrollBarsProperty;
044:
045: /**
046: * Base class for Swing component bean factories. This factory includes support
047: * for adding a scrollable property to any Swing component. This means that
048: * before the component is added to a form, it is first added to a JScrollPane
049: * and the scroll pane is added instead. This occurs only if the scroll flag is
050: * set to true. Many swing component factoriesn don't require this
051: * functionality, so they would set the flag to false. Other component factories
052: * such as JTree, JList, JTextArea would set this flag to true.
053: *
054: * Since the architecture only supports lightweight components, all factories
055: * will probably extend from this class.
056: *
057: * @author Jeff Tassin
058: */
059: public class JComponentBeanFactory implements BeanFactory {
060: /**
061: * The component class for the Swing component that we will instantiate
062: */
063: private Class m_comp_class;
064:
065: /**
066: * The name of the bean class without any package qualifiers
067: */
068: private String m_short_name;
069:
070: /**
071: * Flag that indicates if the component should be contained in a JScrollPane
072: * when created on the form.
073: */
074: private boolean m_scrollable = false;
075:
076: /**
077: * Creates a <code>JComponentBeanFactory</code> instance with the
078: * specified Java Bean class object.
079: *
080: * @param compClass
081: * the class object of the Java bean to associate with this
082: * factory.
083: */
084: public JComponentBeanFactory(Class compClass) {
085: setBeanClass(compClass);
086: }
087:
088: /**
089: * BeanFactory implementation. Instantiates a JETABean and assigns any
090: * custom properties needed by the application.
091: *
092: * @param compName
093: * the name to assign to this component by calling
094: * Component.setName
095: * @param instantiateBean
096: * set to true if the underlying Java Bean should be instantiated
097: * as well. During deserialization we don't want to do this
098: * because the BeanDeserializer will create the JavaBean for us.
099: * @param setDefaults
100: * sets default properties for the bean. If false, no properties
101: * will be set (e.g. the text for a JButton)
102: * @return the newly instantiated JETABean
103: */
104: public JETABean createBean(String compName,
105: boolean instantiateBean, boolean setDefaults)
106: throws FormException {
107: Component comp = null;
108: if (instantiateBean) {
109: comp = (Component) instantiateBean();
110: comp.setName(compName);
111: }
112:
113: DynamicBeanInfo beaninfo = createBeanInfo(m_comp_class);
114: BeanProperties default_props = new BeanProperties(beaninfo);
115: defineProperties(default_props);
116: return new JETABean(comp, default_props);
117: }
118:
119: /**
120: * Creates a dynamic bean info object for the given class. A dynamic bean
121: * info object contains both standard and custom property descriptors for a
122: * Java bean.
123: *
124: * @param compClass
125: * the class object for the component whose bean information to
126: * return.
127: * @return the dynamic bean information for the given bean class object.
128: */
129: public static DynamicBeanInfo createBeanInfo(Class compClass)
130: throws FormException {
131: try {
132: BeanInfo info = Introspector.getBeanInfo(compClass);
133: return new DynamicBeanInfo(info);
134: } catch (Exception e) {
135: FormsLogger.severe(e);
136: if (e instanceof FormException)
137: throw (FormException) e;
138: else
139: throw new FormException(e);
140: }
141: }
142:
143: /**
144: * Returns the class for the bean associated with this factory.
145: *
146: * @return the class for the bean associated with this factory.
147: */
148: public Class getBeanClass() {
149: return m_comp_class;
150: }
151:
152: /**
153: * Defines the custom properties and default values for those properties for
154: * the Swing component associated with this factory. Specialized factories
155: * should override this method to provide any additional custom properties
156: * for their components. If you override this method, make sure to call
157: * super.defineProperties in the specialized class.
158: *
159: * @param props
160: * used to register any custom properties.
161: */
162: public void defineProperties(BeanProperties props) {
163: CompoundBorderProperty prop = new CompoundBorderProperty();
164: prop.addBorder(new DefaultBorderProperty());
165: props.register(prop);
166:
167: if (isScrollable())
168: props.register(new ScrollBarsProperty());
169:
170: }
171:
172: /**
173: * Returns the bean class name without any package qualifiers.
174: *
175: * @return the bean class name.
176: */
177: public String getShortBeanClassName() {
178: return m_short_name;
179: }
180:
181: /**
182: * Creates a instance of the bean associated with this factory.
183: *
184: * @return the new Java bean instance.
185: */
186: public Component instantiateBean() throws FormException {
187: try {
188: return (Component) m_comp_class.newInstance();
189: } catch (Exception e) {
190: e.printStackTrace();
191: throw new FormException(e);
192: }
193: }
194:
195: /**
196: * Returns the flag that determines if the Java bean should be contained in
197: * a JScrollPane when added to the form.
198: *
199: * @return tha flag that controls whether the ScrollBarsProperty will be
200: * added to the component.
201: */
202: public boolean isScrollable() {
203: return m_scrollable;
204: }
205:
206: /**
207: * Sets the class object for the Java bean associated with this factory
208: *
209: * @param compClass
210: * the class object for the Java Bean associated with this
211: * factory.
212: */
213: public void setBeanClass(Class compClass) {
214: m_comp_class = compClass;
215: try {
216: String name = compClass.getName();
217: int pos = name.lastIndexOf(".");
218: if (pos > 0)
219: name = name.substring(pos + 1, name.length());
220:
221: m_short_name = name;
222: } catch (Exception e) {
223: m_short_name = compClass.getName();
224: }
225: }
226:
227: /**
228: * Sets that flag that controls whether the ScrollBarsProperty will be added
229: * to the component.
230: *
231: * @param scrollable
232: * set to true if the Java bean should be contained within a
233: * JScrollPane on the form.
234: */
235: public void setScrollable(boolean scrollable) {
236: m_scrollable = scrollable;
237: }
238: }
|