001: package org.concern.model;
002:
003: import java.beans.PropertyChangeListener;
004: import java.beans.PropertyChangeSupport;
005: import java.io.Serializable;
006: import java.lang.reflect.Constructor;
007: import java.util.*;
008:
009: public abstract class Element implements Comparable, Serializable,
010: DynaBean, Cloneable {
011: String name;
012: String description;
013: Map properties = new HashMap();
014: PropertyChangeSupport changes = new PropertyChangeSupport(this );
015: List environment = new ArrayList();
016:
017: protected Element(String name) {
018: this .name = name;
019: }
020:
021: public String getName() {
022: return name;
023: }
024:
025: public void setName(String name) {
026: Object old = this .name;
027: this .name = name;
028: changes.firePropertyChange("name", old, name);
029: }
030:
031: public String getDescription() {
032: return description;
033: }
034:
035: public void setDescription(String description) {
036: Object old = this .description;
037: this .description = description;
038: changes.firePropertyChange("description", old, description);
039: }
040:
041: public void set(String name, Object value) {
042: Object old = properties.put(name, value);
043: changes.firePropertyChange(name, old, value);
044: }
045:
046: public Object get(String name) {
047: return properties.get(name);
048: }
049:
050: public Set getDynaPropertyNames() {
051: return properties.keySet();
052: }
053:
054: public void removePropertyChangeListener(
055: PropertyChangeListener listener) {
056: changes.removePropertyChangeListener(listener);
057: }
058:
059: public void removePropertyChangeListener(String propertyName,
060: PropertyChangeListener listener) {
061: changes.removePropertyChangeListener(propertyName, listener);
062: }
063:
064: public void addPropertyChangeListener(
065: PropertyChangeListener listener) {
066: changes.addPropertyChangeListener(listener);
067: }
068:
069: public void addPropertyChangeListener(String propertyName,
070: PropertyChangeListener listener) {
071: changes.addPropertyChangeListener(propertyName, listener);
072: }
073:
074: public PropertyChangeListener[] getPropertyChangeListeners() {
075: return changes.getPropertyChangeListeners();
076: }
077:
078: public PropertyChangeListener[] getPropertyChangeListeners(
079: String propertyName) {
080: return changes.getPropertyChangeListeners(propertyName);
081: }
082:
083: public int compareTo(Object o) {
084: Element element = (Element) o;
085: return name.compareTo(element.name);
086: }
087:
088: public String toString() {
089: return name;
090: }
091:
092: public List getEnvironment() {
093: return environment;
094: }
095:
096: public void setEnvironment(List environment) {
097: Object old = this .environment;
098: this .environment = environment;
099: changes.firePropertyChange("environment", old, environment);
100: }
101:
102: public Object getEnvironmentEntry(String name) {
103: if (name == null)
104: return null;
105: for (Iterator iterator = environment.iterator(); iterator
106: .hasNext();) {
107: EnvironmentEntry environmentEntry = (EnvironmentEntry) iterator
108: .next();
109: if (name.equals(environmentEntry.getName())) {
110: try {
111: Class clazz = getClass().getClassLoader()
112: .loadClass(environmentEntry.getType());
113: Constructor constructor = clazz
114: .getConstructor(new Class[] { String.class });
115: return constructor
116: .newInstance(new Object[] { environmentEntry
117: .getValue() });
118: } catch (Exception e) {
119: }
120: }
121: }
122: return null;
123: }
124:
125: public Object clone() {
126: try {
127: Element element = (Element) super .clone();
128: element.changes = new PropertyChangeSupport(element);
129: element.environment = new ArrayList(element.environment);
130: element.properties = new HashMap(element.properties);
131: return element;
132: } catch (CloneNotSupportedException e) {
133: throw new RuntimeException(e);
134: }
135: }
136: }
|