001: package net.xoetrope.optional.registry;
002:
003: import java.lang.reflect.InvocationTargetException;
004: import java.lang.reflect.Method;
005: import java.util.Hashtable;
006:
007: import java.awt.Component;
008:
009: /**
010: * An adapter of component properties and settings
011: * <p> Copyright (c) Xoetrope Ltd., 2002-2004</p>
012: * <p> $Revision: 1.7 $</p>
013: * <p> License: see License.txt</p>
014: */
015: public class ComponentAdapter {
016: protected Class clazz;
017: protected String UI;
018: protected String className;
019: protected Hashtable setters;
020: protected Hashtable getters;
021:
022: /**
023: * Construct a new adapter for the specified class.
024: * @param clazzName the class name e.g. com.myorg.MyClass
025: * @param ui the type of user interface this component supports, e.g. "swing"
026: * @throws ClassNotFoundException
027: */
028: public ComponentAdapter(String clazzName, String ui)
029: throws ClassNotFoundException {
030: UI = ui;
031: className = clazzName;
032: setters = new Hashtable();
033: clazz = Class.forName(className);
034: }
035:
036: /**
037: * Construct a new adapter for the specified class.
038: * @param cl the class loader to use to load the class
039: * @param clazzName the class name e.g. com.myorg.MyClass
040: * @param ui the type of user interface this component supports, e.g. "swing"
041: * @throws ClassNotFoundException
042: */
043: public ComponentAdapter(ClassLoader cl, String clazzName, String ui)
044: throws ClassNotFoundException {
045: UI = ui;
046: className = clazzName;
047: setters = new Hashtable();
048: clazz = cl.loadClass(className);
049: }
050:
051: /**
052: * Set a property value. If the method has not been registered an attempt
053: * will be made to get the method using the bean naming convention.
054: * @param comp the comp whose property is set
055: * @param propertyName the name of the property
056: * @param paramType the type of parameter (String, int, double, boolean, Object) or null for no parameter
057: * @param content the value to set
058: */
059: public void setProperty(Component comp, String propertyName,
060: String content, String paramType) {
061: /**
062: * @todo add a method of evaluating attributes so that helper classes can be
063: * used to set the properties, e.g. if some interpretation or type conversion
064: * of the attribute values is needed
065: */
066: try {
067: if (setters != null) {
068: Method m = (Method) setters.get(propertyName);
069: if (m == null) {
070: String methodName = propertyName.substring(0, 1)
071: .toUpperCase()
072: + propertyName.substring(1);
073: m = addProperty("set", propertyName, "set"
074: + propertyName, paramType, false);
075: }
076:
077: if (m != null) {
078: Object[] args = new Object[1];
079: args[0] = content;
080:
081: m.invoke(comp, args);
082: }
083: }
084: } catch (InvocationTargetException ex) {
085: ex.printStackTrace();
086: } catch (IllegalArgumentException ex) {
087: ex.printStackTrace();
088: } catch (IllegalAccessException ex) {
089: ex.printStackTrace();
090: }
091: }
092:
093: /**
094: * Get a property value. If the method has not been registered an attempt
095: * will be made to get the method using the bean naming convention.
096: * @param comp the comp whose property is to be retrieved
097: * @param propertyName the name of the property
098: */
099: public Object getProperty(Component comp, String propertyName) {
100: try {
101: if (setters != null) {
102: Method m = (Method) setters.get(propertyName);
103: if (m == null) {
104: String methodName = propertyName.substring(0, 1)
105: .toUpperCase()
106: + propertyName.substring(1);
107: m = addProperty("get", propertyName, "get"
108: + propertyName, null, false);
109: }
110:
111: if (m != null) {
112: Object[] args = new Object[0];
113: return m.invoke(comp, args);
114: }
115: }
116: } catch (InvocationTargetException ex) {
117: ex.printStackTrace();
118: } catch (IllegalArgumentException ex) {
119: ex.printStackTrace();
120: } catch (IllegalAccessException ex) {
121: ex.printStackTrace();
122: }
123: return null;
124: }
125:
126: /**
127: * Try to get and register a method
128: * @param type either "get" or "set"
129: * @param name the name by which the method is referred to in the page XML
130: * @param methodName the method name
131: * @param paramType the type of parameter (String, int, double, boolean, Object)
132: * @param attributed is the property set via the XAttributedComponent interface
133: * @return the method or null if not found
134: */
135: public Method addProperty(String type, String name,
136: String methodName, String paramType, boolean attributed) {
137: try {
138: boolean isSetter = (type.compareTo("set") == 0);
139: boolean isGetter = (type.compareTo("get") == 0);
140: if (type.compareTo("both") == 0) {
141: isSetter = true;
142: isGetter = true;
143: }
144:
145: Class[] params = new Class[isSetter ? 1 : 0];
146: if (isSetter && !attributed) {
147: if ((paramType == null) || (paramType.length() == 0)
148: || (paramType.equals("String")))
149: params[0] = String.class;
150: else if (paramType.equals("int"))
151: params[0] = int.class;
152: else if (paramType.equals("double"))
153: params[0] = double.class;
154: else if (paramType.equals("boolean"))
155: params[0] = boolean.class;
156: else if (paramType.equals("Object"))
157: params[0] = Object.class;
158:
159: try {
160: Method m = clazz.getMethod(methodName, params);
161: setters.put(name, m);
162: } catch (Exception ex) {
163: System.err.println("Couldn't find method '"
164: + methodName + "'");
165: }
166: }
167:
168: if (isGetter) {
169: Method m = clazz.getMethod(methodName, params);
170: getters.put(name, m);
171: }
172: } catch (NoSuchMethodException ex) {
173: ex.printStackTrace();
174: } catch (SecurityException ex) {
175: ex.printStackTrace();
176: }
177: return null;
178: }
179:
180: /**
181: * Get the UI type for this component e.g. "swing" or "awt"
182: * @return the UI name
183: */
184: public String getUI() {
185: return UI;
186: }
187: }
|