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.util.Collection;
033: import java.util.HashMap;
034:
035: import com.jeta.forms.gui.common.FormUtils;
036: import com.jeta.forms.store.properties.JETAProperty;
037:
038: /**
039: * Used for managing a set of JETAProperties. A BeanProperties is only used
040: * during instantiation/initialization of a Java Bean (or more specifically its
041: * JETABean container.) It is passed to a BeanFactory during the instantiation
042: * of the Java Bean. This allows specialized factories to create JETABeans with
043: * customized properties.
044: *
045: * @author Jeff Tassin
046: */
047: public class BeanProperties {
048: /**
049: * The BeanInfo associated with the Java Bean. This defines the property
050: * descriptors for the bean.
051: */
052: private DynamicBeanInfo m_beaninfo;
053:
054: /**
055: * A map of default property values for the bean that are not declared by
056: * the bean itself. BeanFactories can provide their own default properties.
057: * m_props<String,JETAProperty> where: String is the property name.
058: * JETAProperty is the property value.
059: */
060: private HashMap m_props = new HashMap();
061:
062: /**
063: * Creates a <code>BeanProperties</code> instance with the specified
064: * DynamicBeanInfo object.
065: *
066: * @param beaninfo
067: * the beaninfo (PropertyDescriptors) associated with these
068: * properties
069: */
070: public BeanProperties(DynamicBeanInfo beaninfo) {
071: m_beaninfo = beaninfo;
072: FormUtils.safeAssert(beaninfo != null);
073: }
074:
075: /**
076: * Returns the BeanInfo associated with these properties.
077: *
078: * @return the underyling BeanInfo
079: */
080: public DynamicBeanInfo getBeanInfo() {
081: return m_beaninfo;
082: }
083:
084: /**
085: * Returns a collection of JETAProperty objects that define customized
086: * properties to be associated with a Java Bean.
087: *
088: * @return a collection of JETAProperty values.
089: */
090: public Collection getPropertyValues() {
091: return m_props.values();
092: }
093:
094: /**
095: * Removes the property with the given name.
096: *
097: * @param propName
098: * the name of the property to remove.
099: */
100: public void removeProperty(String propName) {
101: m_props.remove(propName);
102: m_beaninfo.removePropertyDescriptor(propName);
103: }
104:
105: /**
106: * Registers a custom property to be associated with a Java Bean.
107: *
108: * @param prop
109: * the custom property to add.
110: */
111: public void register(JETAProperty prop) {
112: if (prop != null) {
113: m_props.put(prop.getName(), prop);
114: m_beaninfo.register(new DynamicPropertyDescriptor(prop
115: .getName(), prop.getClass(), prop.isPreferred(),
116: prop.isTransient()));
117: }
118: }
119:
120: /**
121: * Sets the preferred flag for the property with the given name.
122: *
123: * @param propName
124: * the name of the property to set.
125: * @param pref
126: * true if the property should be set to preferred.
127: */
128: public void setPreferred(String propName, boolean pref) {
129: JETAPropertyDescriptor jpd = m_beaninfo
130: .getPropertyDescriptor(propName);
131: if (jpd != null)
132: jpd.setPreferred(pref);
133: }
134:
135: }
|