001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: ParticlePropertyModel.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.gui.model;
009:
010: import java.lang.reflect.InvocationTargetException;
011: import java.lang.reflect.Method;
012:
013: import com.uwyn.rife.gui.model.exceptions.ParticlePropertyCreationException;
014: import com.uwyn.rife.gui.model.exceptions.GuiModelException;
015:
016: public abstract class ParticlePropertyModel {
017: private ParticleModel mParticle = null;
018: private String mName = null;
019: private String mDescription = null;
020:
021: private ParticlePropertyModel() {
022: }
023:
024: protected ParticlePropertyModel(ParticleModel particle, String name)
025: throws GuiModelException {
026: assert particle != null;
027: assert name != null;
028:
029: setName(name);
030: try {
031: particle.addProperty(this );
032: } catch (GuiModelException e) {
033: throw new ParticlePropertyCreationException(e);
034: }
035: }
036:
037: public ParticleModel getParticle() {
038: synchronized (this ) {
039: return mParticle;
040: }
041: }
042:
043: protected void setParticle(ParticleModel particle) {
044: synchronized (this ) {
045: mParticle = particle;
046: }
047: }
048:
049: public String getDescription() {
050: synchronized (this ) {
051: return mDescription;
052: }
053: }
054:
055: public void setDescription(String description) {
056: synchronized (this ) {
057: mDescription = description;
058: }
059: }
060:
061: public boolean equals(Object object) {
062: if (object instanceof ParticlePropertyModel) {
063: ParticlePropertyModel property = (ParticlePropertyModel) object;
064: if (property.getParticle() == getParticle()
065: && property.getClass() == getClass()
066: && property.getName().equals(getName())) {
067: return true;
068: }
069: }
070:
071: return false;
072: }
073:
074: public int hashCode() {
075: int result = 17;
076:
077: result = 37 * result + getParticle().hashCode();
078: result = 37 * result + getClass().hashCode();
079: result = 37 * result + getName().hashCode();
080:
081: return result;
082: }
083:
084: public String getName() {
085: synchronized (this ) {
086: return mName;
087: }
088: }
089:
090: protected void setName(String name) {
091: assert name != null;
092: assert name.length() > 0;
093:
094: synchronized (this ) {
095: mName = name;
096: }
097: }
098:
099: public final boolean isValidName(String name) {
100: if (null == name)
101: throw new IllegalArgumentException("name can't be null.");
102: if (0 == name.length())
103: throw new IllegalArgumentException("name can't be empty.");
104:
105: ParticlePropertyModel temp_property = findConflictingProperty(
106: getParticle(), getClass(), name);
107:
108: if (temp_property == this || temp_property == null) {
109: return true;
110: } else {
111: return false;
112: }
113: }
114:
115: public static boolean isValidName(ParticleModel particle,
116: Class type, String name) {
117: if (null == particle)
118: throw new IllegalArgumentException(
119: "particle can't be null.");
120: if (null == type)
121: throw new IllegalArgumentException("type can't be null.");
122: if (null == name)
123: throw new IllegalArgumentException("name can't be null.");
124: if (0 == name.length())
125: throw new IllegalArgumentException("name can't be empty.");
126:
127: ParticlePropertyModel temp_property = null;
128: Method find_conflicting_property_method = null;
129:
130: Class class_to_search = type;
131:
132: while (class_to_search != null) {
133: try {
134: find_conflicting_property_method = class_to_search
135: .getDeclaredMethod("findConflictingProperty",
136: new Class[] { ParticleModel.class,
137: Class.class, String.class });
138: break;
139: } catch (NoSuchMethodException e) {
140: class_to_search = class_to_search.getSuperclass();
141: find_conflicting_property_method = null;
142: }
143: }
144:
145: if (find_conflicting_property_method != null) {
146: try {
147: temp_property = (ParticlePropertyModel) find_conflicting_property_method
148: .invoke(null, new Object[] { particle, type,
149: name });
150: } catch (IllegalAccessException e) {
151: temp_property = null;
152: } catch (IllegalArgumentException e) {
153: temp_property = null;
154: } catch (InvocationTargetException e) {
155: temp_property = null;
156: }
157: }
158:
159: if (temp_property == null) {
160: return true;
161: } else {
162: return false;
163: }
164: }
165:
166: protected static ParticlePropertyModel findConflictingProperty(
167: ParticleModel particle, Class type, String name) {
168: assert particle != null;
169: assert type != null;
170: assert name != null;
171: assert name.length() > 0;
172:
173: return particle.getProperty(type, name);
174: }
175: }
|