001: package com.xoetrope.data.pojo;
002:
003: import com.xoetrope.carousel.visualizer.TreeNodeCaption;
004: import java.lang.reflect.Method;
005: import java.util.Collection;
006: import java.util.Iterator;
007: import java.util.Vector;
008: import net.xoetrope.editor.project.pages.XModelVis;
009: import net.xoetrope.xui.data.XModel;
010:
011: /**
012: * An abstract class representing design-time pojo model (for use by "data visualiser")
013: * <p> Copyright (c) Xoetrope Ltd., 2001-2007, This software is licensed under
014: * the GNU Public License (GPL), please see license.txt for more details. If
015: * you make commercial use of this software you must purchase a commercial
016: * license from Xoetrope.</p>
017: */
018: public abstract class XPojoModelVis extends XModel implements
019: TreeNodeCaption, XModelVis {
020: public static final int ATTRIB_NEDITABLE = -1;
021: public static final int ATTRIB_COMBO = 0;
022: public static final int ATTRIB_FREETEXT = 1;
023:
024: public static final int ID_ATTRIBUTE = 0;
025: public static final int VALUE_ATTRIBUTE = 1;
026: public static final int NUM_FIXED_ATTRIBUTE = 2;
027:
028: protected volatile int hashcode;
029: protected String idAttrib;
030: protected String valueAttrib;
031: protected XPojoDataSourceEx dataSource;
032: protected Object pojo;
033: protected XPojoModelVis[] pojoCollection;
034: protected Class pojoClass;
035: protected Method pojoGetter;
036: protected Class[] getterSig;
037: protected Method pojoSetter;
038: protected Class[] setterSig;
039: protected XPojoAdapterEx adapter;
040:
041: /**
042: * Creates a new instance of XPojoModelVis
043: * @param pr the parent model node.
044: * @param pojo Object being wrapped by this model node.
045: * @param ds the data source object.
046: */
047: public XPojoModelVis(XModel pr, Object pj, XPojoDataSourceEx ds) {
048: setParent(pr);
049: dataSource = ds;
050: setPojo(pj);
051: pojoGetter = null;
052: pojoSetter = null;
053: }
054:
055: /**
056: * Creates a new instance of XPojoModelVis class.
057: * @param pr the parent mode node
058: * @param mth the getter method being used to obtain
059: * the pojo to be wrapped by this model node.
060: * @param ds the data source object.
061: */
062: public XPojoModelVis(XModel pr, Method gtr, Method str,
063: XPojoDataSourceEx ds) {
064: setParent(pr);
065: dataSource = ds;
066: setPojoGetter(gtr);
067: setPojoSetter(str);
068: pojo = null;
069: pojoCollection = null;
070: }
071:
072: /**
073: * Creates a new instance of XPojoModelVis
074: * @param pr the parent model node
075: * @param clazz the type of the underlying pojo.
076: * @param ds the data source object.
077: */
078: public XPojoModelVis(XModel pr, Class clazz, XPojoDataSourceEx ds) {
079: setParent(pr);
080: dataSource = ds;
081: setPojoClass(clazz);
082: pojo = null;
083: pojoCollection = null;
084: pojoGetter = null;
085: pojoSetter = null;
086: }
087:
088: /**
089: * Creates a new instance of XPojoModelVis class.
090: * @param pr the parent model node
091: * @parm ds the data source object.
092: */
093: public XPojoModelVis(XModel pr, XPojoDataSourceEx ds) {
094: setParent(pr);
095: dataSource = ds;
096: pojo = null;
097: pojoCollection = null;
098: pojoGetter = null;
099:
100: }
101:
102: /**
103: * Gets the properties of the encapsulated POJO
104: * @return XPojoProperties object
105: */
106: public XPojoProperties getProperties() {
107: return (adapter != null ? adapter.getProperties() : null);
108: }
109:
110: /**
111: * Sets the underlying pojo of this model node
112: * @param aPojo the new pojo
113: */
114: protected void setPojo(Object aPojo) {
115: pojo = aPojo;
116: pojoClass = (pojo != null ? pojo.getClass() : null);
117: // store the collection elements
118: if (pojo instanceof Collection) {
119: pojoCollection = new XPojoModelVis[((Collection) pojo)
120: .size()];
121: Iterator iter = ((Collection) pojo).iterator();
122: for (int i = 0; iter.hasNext(); i++)
123: pojoCollection[i] = dataSource.adaptPojo(iter.next(),
124: this );
125: }
126: }
127:
128: /**
129: * Gets the adapter of this model node
130: * @return the adapter
131: */
132: public XPojoAdapterEx getAdapter() {
133: return adapter;
134: }
135:
136: /**
137: * Sets the method being used to obtain
138: * the pojo of this node.
139: * @param aMethod the getter method
140: */
141: protected void setPojoGetter(Method aMethod) {
142: pojoGetter = aMethod;
143: if (pojoGetter != null) {
144: pojoClass = pojoGetter.getReturnType();
145: getterSig = pojoGetter.getParameterTypes();
146: } else {
147: pojoClass = null;
148: getterSig = null;
149: }
150: }
151:
152: /**
153: * Sets the method being used to
154: * set the pojo of this node
155: * @param aMethod the setter
156: */
157: protected void setPojoSetter(Method aMethod) {
158: pojoSetter = aMethod;
159: setterSig = (pojoSetter != null ? pojoSetter
160: .getParameterTypes() : null);
161: }
162:
163: /**
164: * Sets the type of the underlying pojo.
165: * @param clazz the type of the underlying pojo
166: */
167: protected void setPojoClass(Class clazz) {
168: pojoClass = clazz;
169: }
170:
171: public Class getPojoClass() {
172: return pojoClass;
173: }
174:
175: /**
176: * Sets the <code>dirty</code> flag of this model node.
177: * @param state the new value of the <code>dirty</code> flag
178: */
179: public void setDirty(boolean state) {
180: XPropertiesRetriever pr = dataSource.getPropertiesRetriever();
181: if (state)
182: pr.setPropertyValue(this , "dirty", null);
183: else
184: pr.removeProperty(this , "dirty");
185: }
186:
187: /**
188: * Gets the value of the dirty property
189: */
190: public boolean isDirty() {
191: XPropertiesRetriever pr = dataSource.getPropertiesRetriever();
192: return (pr.containsProperty(this , "dirty"));
193: }
194:
195: /**
196: * Gets the runtime binding path of this model node
197: * @return the runtime binding path
198: */
199: public String getBindingPath() {
200: String path = "";
201: XModel parentNode = getParent();
202: if ((parentNode != null)
203: && (parentNode instanceof XPojoModelVis))
204: path = (((XPojoModelVis) parentNode).getBindingPath() + "/");
205: return (path + getId());
206: }
207:
208: /**
209: * Gets the visualiser tree caption of this node.
210: * @return the caption.
211: */
212: public String getCaption() {
213: return (idAttrib + ": " + valueAttrib);
214: }
215:
216: /**
217: * Sets the caption of this node which will be shown in the model visualiser tree.
218: * @param caption the caption to be set
219: */
220: public void setCaption(String caption) {
221: int idx = caption.indexOf(": ");
222: idAttrib = caption.substring(0, idx);
223: valueAttrib = caption.substring(idx + 2, caption.length());
224: // fix the name if the underlying POJO is a proxy object.
225: idx = idAttrib.indexOf("$");
226: if (idx > 0)
227: idAttrib = idAttrib.substring(0, idx);
228: setId(idAttrib);
229: }
230:
231: /**
232: * Gets the ID attribute of this node
233: * @return the id
234: */
235: public String getId() {
236: return idAttrib;
237: }
238:
239: /**
240: * Sets the ID attribute
241: * @return newId the new id
242: */
243: public void setId(String newId) {
244: hashcode = newId.hashCode();
245: }
246:
247: /**
248: * Gets the number of attributes
249: * @return the number of attributes
250: */
251: public int getNumAttributes() {
252: return NUM_FIXED_ATTRIBUTE;
253: }
254:
255: public String getAttribName(int i) {
256: if (i == ID_ATTRIBUTE)
257: return "id";
258: else if (i == VALUE_ATTRIBUTE)
259: return "value";
260: else
261: return null;
262: }
263:
264: public Object getAttribValue(int i) {
265: if (i == ID_ATTRIBUTE)
266: return idAttrib;
267: else if (i == VALUE_ATTRIBUTE)
268: return valueAttrib;
269: else
270: return null;
271: }
272:
273: public String getAttribValueAsString(int i) {
274: return (String) getAttribValue(i);
275: }
276:
277: /**
278: * The hashcode of this instance. Based on the ID String
279: * @return The hashcode of this instance
280: */
281: public int hashCode() {
282: return hashcode;
283: }
284:
285: /**
286: * Gest the binding attributes that this model node can provide.
287: * By default this method returns an empty Vector object.
288: * @return Vector containing binding attributes.
289: */
290: public Vector getBindingAttributes() {
291: return (new Vector());
292: }
293:
294: public void set(String string, Object object) {
295: }
296:
297: public int getAttribute(String string) {
298: return -1;
299: }
300:
301: public void setAttribValue(int i, String string, Object object) {
302: }
303:
304: public Object get() {
305: return null;
306: }
307:
308: public double getAttribValueAsDouble(int i) {
309: return Double.NaN;
310: }
311:
312: public double getAttribValueAsDouble(int i, char c, char c0) {
313: return Double.NaN;
314: }
315:
316: public int getAttribValueAsInt(int i) {
317: return -1;
318: }
319:
320: public double getValueAsDouble(String string) {
321: return Double.NaN;
322: }
323:
324: public int getValueAsInt(String string) {
325: return -1;
326: }
327:
328: public String getValueAsString(String string) {
329: return null;
330: }
331:
332: public void append(XModel xModel) {
333: }
334:
335: public Object append(String string) {
336: return null;
337: }
338:
339: public XModel get(int i) {
340: return null;
341: }
342:
343: public void set(Object obj) {
344: }
345:
346: /**
347: * Indicates whether the attribute will be available
348: * in the run-time mode.
349: * @param i the index of the attribute
350: * @return true if the attribute will be available in
351: * the runtime mode, false otherwise
352: */
353: public boolean getAttribRuntime(int i) {
354: return true;
355: }
356:
357: public int getAttribType(int i) {
358: return ATTRIB_NEDITABLE;
359: }
360:
361: public String[] getAttribAvailableValues(int i) {
362: return null;
363: }
364:
365: public boolean isAttribEditable(int i) {
366: return false;
367: }
368:
369: public void setAttribValue(int i, Object value) {
370: }
371:
372: }
|