001: /**
002: * L2FProd.com Common Components 7.3 License.
003: *
004: * Copyright 2005-2007 L2FProd.com
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package com.l2fprod.common.propertysheet;
018:
019: import com.l2fprod.common.beans.BeanUtils;
020:
021: import java.lang.reflect.Method;
022: import java.util.ArrayList;
023: import java.util.Arrays;
024: import java.util.Collection;
025: import java.util.Iterator;
026: import java.util.List;
027:
028: /**
029: * DefaultProperty. <br>
030: *
031: */
032: public class DefaultProperty extends AbstractProperty {
033:
034: private String name;
035: private String displayName;
036: private String shortDescription;
037: private Class type;
038: private boolean editable = true;
039: private String category;
040: private Property parent;
041: private List subProperties = new ArrayList();
042:
043: public String getName() {
044: return name;
045: }
046:
047: public void setName(String name) {
048: this .name = name;
049: }
050:
051: public String getDisplayName() {
052: return displayName;
053: }
054:
055: public void setDisplayName(String displayName) {
056: this .displayName = displayName;
057: }
058:
059: public String getShortDescription() {
060: return shortDescription;
061: }
062:
063: public void setShortDescription(String shortDescription) {
064: this .shortDescription = shortDescription;
065: }
066:
067: public Class getType() {
068: return type;
069: }
070:
071: public void setType(Class type) {
072: this .type = type;
073: }
074:
075: public boolean isEditable() {
076: return editable;
077: }
078:
079: public void setEditable(boolean editable) {
080: this .editable = editable;
081: }
082:
083: public String getCategory() {
084: return category;
085: }
086:
087: public void setCategory(String category) {
088: this .category = category;
089: }
090:
091: /**
092: * Reads the value of this Property from the given object. It uses reflection
093: * and looks for a method starting with "is" or "get" followed by the
094: * capitalized Property name.
095: */
096: public void readFromObject(Object object) {
097: try {
098: Method method = BeanUtils.getReadMethod(object.getClass(),
099: getName());
100: if (method != null) {
101: Object value = method.invoke(object, null);
102: initializeValue(value); // avoid updating parent or firing property change
103: if (value != null) {
104: for (Iterator iter = subProperties.iterator(); iter
105: .hasNext();) {
106: Property subProperty = (Property) iter.next();
107: subProperty.readFromObject(value);
108: }
109: }
110: }
111: } catch (Exception e) {
112: throw new RuntimeException(e);
113: }
114: }
115:
116: /**
117: * Writes the value of the Property to the given object. It uses reflection
118: * and looks for a method starting with "set" followed by the capitalized
119: * Property name and with one parameter with the same type as the Property.
120: */
121: public void writeToObject(Object object) {
122: try {
123: Method method = BeanUtils.getWriteMethod(object.getClass(),
124: getName(), getType());
125: if (method != null) {
126: method.invoke(object, new Object[] { getValue() });
127: }
128: } catch (Exception e) {
129: throw new RuntimeException(e);
130: }
131: }
132:
133: /* (non-Javadoc)
134: * @see com.l2fprod.common.propertysheet.Property#setValue(java.lang.Object)
135: */
136: public void setValue(Object value) {
137: super .setValue(value);
138: if (parent != null) {
139: Object parentValue = parent.getValue();
140: if (parentValue != null) {
141: writeToObject(parentValue);
142: parent.setValue(parentValue);
143: }
144: }
145: if (value != null) {
146: for (Iterator iter = subProperties.iterator(); iter
147: .hasNext();) {
148: Property subProperty = (Property) iter.next();
149: subProperty.readFromObject(value);
150: }
151: }
152: }
153:
154: public int hashCode() {
155: return 28
156: + ((name != null) ? name.hashCode() : 3)
157: + ((displayName != null) ? displayName.hashCode() : 94)
158: + ((shortDescription != null) ? shortDescription
159: .hashCode() : 394)
160: + ((category != null) ? category.hashCode() : 34)
161: + ((type != null) ? type.hashCode() : 39)
162: + Boolean.valueOf(editable).hashCode();
163: }
164:
165: /**
166: * Compares two DefaultProperty objects. Two DefaultProperty objects are equal
167: * if they are the same object or if their name, display name, short
168: * description, category, type and editable property are the same. Note the
169: * property value is not considered in the implementation.
170: */
171: public boolean equals(Object other) {
172: if (other == null || getClass() != other.getClass()) {
173: return false;
174: }
175:
176: if (other == this ) {
177: return true;
178: }
179:
180: DefaultProperty dp = (DefaultProperty) other;
181:
182: return compare(name, dp.name)
183: && compare(displayName, dp.displayName)
184: && compare(shortDescription, dp.shortDescription)
185: && compare(category, dp.category)
186: && compare(type, dp.type) && editable == dp.editable;
187: }
188:
189: private boolean compare(Object o1, Object o2) {
190: return (o1 != null) ? o1.equals(o2) : o2 == null;
191: }
192:
193: public String toString() {
194: return "name=" + getName() + ", displayName="
195: + getDisplayName() + ", type=" + getType()
196: + ", category=" + getCategory() + ", editable="
197: + isEditable() + ", value=" + getValue();
198: }
199:
200: public Property getParentProperty() {
201: return parent;
202: }
203:
204: public void setParentProperty(Property parent) {
205: this .parent = parent;
206: }
207:
208: public Property[] getSubProperties() {
209: return (Property[]) subProperties
210: .toArray(new Property[subProperties.size()]);
211: }
212:
213: public void clearSubProperties() {
214: for (Iterator iter = this .subProperties.iterator(); iter
215: .hasNext();) {
216: Property subProp = (Property) iter.next();
217: if (subProp instanceof DefaultProperty)
218: ((DefaultProperty) subProp).setParentProperty(null);
219: }
220: this .subProperties.clear();
221: }
222:
223: public void addSubProperties(Collection subProperties) {
224: this .subProperties.addAll(subProperties);
225: for (Iterator iter = this .subProperties.iterator(); iter
226: .hasNext();) {
227: Property subProp = (Property) iter.next();
228: if (subProp instanceof DefaultProperty)
229: ((DefaultProperty) subProp).setParentProperty(this );
230: }
231: }
232:
233: public void addSubProperties(Property[] subProperties) {
234: this .addSubProperties(Arrays.asList(subProperties));
235: }
236:
237: public void addSubProperty(Property subProperty) {
238: this .subProperties.add(subProperty);
239: if (subProperty instanceof DefaultProperty)
240: ((DefaultProperty) subProperty).setParentProperty(this);
241: }
242: }
|