001: package com.xoetrope.data.pojo;
002:
003: import java.lang.reflect.Method;
004: import java.lang.reflect.Type;
005: import java.lang.reflect.TypeVariable;
006: import net.xoetrope.optional.data.pojo.XPojoModel;
007: import net.xoetrope.xui.data.XModel;
008:
009: /**
010: * Finder arguments model.
011: * <p> Copyright (c) Xoetrope Ltd., 2001-2007, This software is licensed under
012: * the GNU Public License (GPL), please see license.txt for more details. If
013: * you make commercial use of this software you must purchase a commercial
014: * license from Xoetrope.</p>
015: */
016: public class XPojoMethodArgs extends XPojoModelVis {
017: protected XPojoModelEx pojoFinder;
018: protected Class[] parameterTypes;
019: protected String[] genericNames;
020: protected XPojoMethodArg[] pojoArgs;
021:
022: /**
023: * Creates a new instance of XPojoFinderArgs
024: * @param ds XPojoDataSourceEx object
025: * @param pt types of parameters
026: */
027:
028: public XPojoMethodArgs(XPojoModelEx pc, Method method,
029: XPojoDataSourceEx ds) {
030: super (pc, method, ds);
031: pojoFinder = pc;
032: setParameters(method);
033: setFinderArgs();
034: }
035:
036: public Class getPojoClass() {
037: return null;
038: }
039:
040: /**
041: * Stores the information about the method arguments
042: * @param method the Method to be queried
043: */
044: private void setParameters(Method method) {
045: if (method == null)
046: return;
047: parameterTypes = method.getParameterTypes();
048: if (parameterTypes == null)
049: parameterTypes = new Class[0];
050: int numParameters = parameterTypes.length;
051: Type[] types = method.getGenericParameterTypes();
052: genericNames = new String[numParameters];
053: for (int i = 0; i < numParameters; i++) {
054: if (types[i] instanceof TypeVariable)
055: genericNames[i] = ((TypeVariable) types[i]).getName();
056: else
057: genericNames[i] = null;
058: }
059: }
060:
061: /**
062: * Returns the table containin parent method
063: * argument types
064: * @return the table containing types
065: */
066: public Class[] getParameterTypes() {
067: return parameterTypes;
068: }
069:
070: /**
071: * Gets the number of children
072: * @return the number of children (parameters)
073: */
074: public int getNumChildren() {
075: return (parameterTypes != null ? parameterTypes.length : 0);
076: }
077:
078: /**
079: * Gets the finder argument.
080: * @param i position of finder's argument in the list of arguments
081: * @return XModel object representing finder's argument.
082: */
083: public XModel get(int i) {
084: if (i >= getNumChildren())
085: return null;
086: return pojoArgs[i];
087: }
088:
089: /**
090: * Defines this finder arguments and restores its values in order
091: * to determine the number of children of this node.
092: */
093: protected void setFinderArgs() {
094: int numChildren = getNumChildren();
095: pojoArgs = new XPojoMethodArg[numChildren];
096: XPropertiesRetriever af = dataSource.getPropertiesRetriever();
097: for (int i = 0; i < numChildren; i++) {
098: Class type = parameterTypes[i];
099: String name = genericNames[i];
100: XPojoMethodArg arg = new XPojoMethodArg(this , i, type,
101: name, af);
102: arg.restoreArgument();
103: pojoArgs[i] = arg;
104: }
105: }
106:
107: /**
108: * Removes values of all arguments
109: */
110: public void removeAllArgumentValues() {
111: int numArguments = pojoArgs.length;
112: for (int i = 0; i < numArguments; i++) {
113: pojoArgs[i].removeValue();
114: }
115: }
116:
117: /**
118: * Determines whether values of all the arguments
119: * hass been set
120: * @return true if the values has been set,
121: * false otherwise
122: */
123: public boolean argumentValuesSet() {
124: int numArguments = pojoArgs.length;
125: boolean argsSet = true;
126: for (int i = 0; (i < numArguments) && argsSet; i++)
127: argsSet = pojoArgs[i].hasValue();
128: return argsSet;
129: }
130:
131: /**
132: * Gets the value of the element located at the path
133: * @param element the path to the XPojoModel required
134: * @return the value of the XPojoModel
135: */
136: public Object get(String element) {
137: if ((element == null) || (element.length() == 0))
138: return null;
139: if (element.charAt(0) == '/')
140: element = element.substring(1);
141:
142: XModel model = null;
143: int pos = element.indexOf('/');
144: String subPath = pos > -1 ? element.substring(0, pos) : element;
145:
146: if ((subPath != null) && (subPath.length() > 3)) {
147: String idxS = element.substring(3);
148: try {
149: model = get(Integer.parseInt(idxS));
150: } catch (NumberFormatException ex) {
151: }
152: }
153:
154: if ((pos >= 0) && (model != null)) {
155: model = (XPojoModel) model.get(element.substring(pos + 1));
156: }
157:
158: if (model != null)
159: model.setParent(this );
160:
161: return model;
162: }
163:
164: /**
165: * Gets the visualiser tree caption of this node
166: * @return the caption
167: */
168: public String getCaption() {
169: String caption = "arguments: ";
170: setCaption(caption);
171: return caption;
172: }
173:
174: public void set(Object value) {
175:
176: }
177:
178: public boolean getAttribRuntime(int i) {
179: return true;
180: }
181:
182: }
|