001: /*
002: * <copyright>
003: *
004: * Copyright 2000-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.tools.csmart.core.property;
028:
029: import org.cougaar.tools.csmart.core.property.name.ComponentName;
030: import org.cougaar.tools.csmart.core.property.name.CompositeName;
031:
032: import java.io.IOException;
033: import java.io.ObjectInputStream;
034: import java.util.Set;
035:
036: /**
037: * A ConfigurableComponentProperty is a property within a ConfigurableComponent.
038: * Usually these properties are displayed on the GUI, however, they don't always
039: * have to be displayed.
040: * Every property has these attributes:
041: * name, propertyClass, label, defaultValue, value, allowedValues,
042: * These values should be get/set through the ComponentProperties interface.
043: */
044: public class ConfigurableComponentProperty extends PropertyBase
045: implements Property {
046: static final long serialVersionUID = -1591739578094464420L;
047:
048: private ComponentName name;
049: private Class propertyClass;
050: private String label;
051: private Object defaultValue;
052: private Object value;
053: private Set allowedValues;
054:
055: /**
056: * Construct a Property with default values based on the name and value
057: **/
058: public ConfigurableComponentProperty(ConfigurableComponent c,
059: String name, Object value) {
060: super (c);
061: this .name = new ComponentName(c, name);
062: setValue(value);
063: setDefaultValue(value);
064: setLabel(name);
065: }
066:
067: /**
068: * Copy constructor creates a new ConfigurableComponentProperty from
069: * an existing Property
070: **/
071: public ConfigurableComponentProperty(ConfigurableComponent c,
072: String name, Property prop) {
073: super (c);
074: this .name = new ComponentName(c, name);
075: setValue(prop.getValue());
076: setPropertyClass(prop.getPropertyClass());
077: setLabel(prop.getLabel());
078: setDefaultValue(prop.getDefaultValue());
079: setAllowedValues(prop.getAllowedValues());
080: }
081:
082: /**
083: * Gets the name of this Property
084: *
085: * @return a <code>CompositeName</code> value
086: */
087: public CompositeName getName() {
088: return name;
089: }
090:
091: /**
092: * Gets the class of this Property. The Class of the Property
093: * is the Class Type of values that are accepted by this property.
094: *
095: * @return a <code>Class</code> value
096: */
097: public Class getPropertyClass() {
098: return propertyClass;
099: }
100:
101: /**
102: * Sets the class of this Property. The Class indicates what type
103: * of values are excepted in this Property.
104: *
105: * @param c Class of this property
106: */
107: public void setPropertyClass(Class c) {
108: Class old = propertyClass;
109: propertyClass = c;
110: try {
111: fireOtherChanged(old, PropertyEvent.CLASS_CHANGED);
112: } catch (RuntimeException re) {
113: propertyClass = old;
114: throw re;
115: }
116: }
117:
118: /**
119: * Gets the Label of this Property. The Label is a text string
120: * describing this property.
121: *
122: * @return a <code>String</code> value
123: */
124: public String getLabel() {
125: return label;
126: }
127:
128: /**
129: * Sets the label of this Property. The label is a text string
130: * describing this property.
131: *
132: * @param label of this property
133: */
134: public void setLabel(String label) {
135: String old = this .label;
136: this .label = label;
137: try {
138: fireOtherChanged(old, PropertyEvent.LABEL_CHANGED);
139: } catch (RuntimeException re) {
140: this .label = old;
141: throw re;
142: }
143: }
144:
145: /**
146: * Gets the default value of this Property. All Properties
147: * should contain a default value.
148: *
149: * @return an <code>Object</code> value
150: */
151: public Object getDefaultValue() {
152: return defaultValue;
153: }
154:
155: /**
156: * Sets the default value for this Property. All Properties
157: * should contain a default value.
158: *
159: * @param defaultValue of the Property
160: */
161: public void setDefaultValue(Object defaultValue) {
162: Object old = this .defaultValue;
163: this .defaultValue = defaultValue;
164: try {
165: fireOtherChanged(old, PropertyEvent.DEFAULTVALUE_CHANGED);
166: if (value == null)
167: fireValueChanged(old);
168: } catch (RuntimeException re) {
169: this .defaultValue = old;
170: throw re;
171: }
172: }
173:
174: /**
175: * Gets the value of this Property
176: *
177: * @return an <code>Object</code> value
178: */
179: public Object getValue() {
180: if (value == null)
181: return defaultValue;
182: return value;
183: }
184:
185: /**
186: * Sets the value of this Property
187: *
188: * @param value of the property
189: */
190: public void setValue(Object value) {
191: // MIK: should this be .equals?
192: if ((value == null && this .value != null)
193: || (value != null && !value.equals(this .value))) {
194: // if(value != this.value) {
195: Object old = this .value;
196:
197: // intern short (< 16 chars) strings
198: if (value instanceof String) {
199: String v = (String) value;
200: if (v.length() < 16) {
201: value = v.intern();
202: }
203: }
204:
205: this .value = value;
206: try {
207: fireValueChanged(old);
208: } catch (RuntimeException re) {
209: this .value = old;
210: throw re;
211: }
212: }
213: }
214:
215: /**
216: * Gets a Set of all allowed values.
217: * Properties allow ranges of allowable values,
218: * If defined, they method returns that list.
219: *
220: * @return a <code>Set</code> value
221: */
222: public Set getAllowedValues() {
223: return allowedValues;
224: }
225:
226: /**
227: * Sets a range of allowed values.
228: * Properties allow ranges of allowable values,
229: *
230: * @param allowedValues
231: */
232: public void setAllowedValues(Set allowedValues) {
233: Set old = allowedValues;
234: this .allowedValues = allowedValues;
235: try {
236: fireOtherChanged(old, PropertyEvent.ALLOWEDVALUES_CHANGED);
237: } catch (RuntimeException re) {
238: this .allowedValues = old;
239: throw re;
240: }
241: }
242:
243: /**
244: * Indicates if a value has been set for this Property.
245: *
246: * @return a <code>boolean</code> value
247: */
248: public boolean isValueSet() {
249: return value != null;
250: }
251:
252: private void readObject(ObjectInputStream stream)
253: throws IOException, ClassNotFoundException {
254: stream.defaultReadObject();
255: // intern short (< 16 chars) strings
256: if (value instanceof String) {
257: String v = (String) value;
258: if (v.length() < 16) {
259: value = v.intern();
260: }
261: }
262: }
263:
264: }
|