1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package org.mlc.swing.layout;
31
32 import java.awt.Component;
33 import java.beans.BeanInfo;
34 import java.beans.IntrospectionException;
35 import java.beans.Introspector;
36 import java.beans.PropertyDescriptor;
37 import java.lang.reflect.InvocationTargetException;
38 import java.lang.reflect.Method;
39 import java.util.ArrayList;
40 import java.util.HashMap;
41 import java.util.Iterator;
42 import java.util.List;
43 import java.util.Map;
44 import javax.swing.JPanel;
45
46 /***
47 * This is meant to be subclassed when you want to create a ComponentBuilder
48 * that has simplistic behavior.
49 *
50 * @author Michael Connor mlconnor@yahoo.com
51 */
52 public class DefaultComponentBuilder implements ComponentBuilder
53 {
54 Class clazz;
55
56 BeanInfo beanInfo;
57
58 List<PropertyDescriptor> editableProperties = new ArrayList<PropertyDescriptor>();
59
60 Map<String, PropertyDescriptor> nameToDescriptor = new HashMap<String, PropertyDescriptor>();
61
62 List<BeanProperty> properties = new ArrayList<BeanProperty>();
63
64 /*** Creates a new instance of DefaultComponentFactory */
65 public DefaultComponentBuilder(Class clazz) throws IntrospectionException
66 {
67 this(clazz, null);
68 }
69
70 /*** Creates a new instance of DefaultComponentFactory */
71 public DefaultComponentBuilder(Class clazz, String[] properties)
72 throws IntrospectionException
73 {
74 this.clazz = clazz;
75
76 beanInfo = Introspector.getBeanInfo(clazz);
77 PropertyDescriptor[] propertyDescriptors = beanInfo
78 .getPropertyDescriptors();
79
80 if (properties != null)
81 {
82 for (int i = 0; i < properties.length; i++)
83 {
84 String propertyName = properties[i];
85 PropertyDescriptor propertyDescriptor = null;
86
87 for (int index = 0; index < propertyDescriptors.length; index++)
88 {
89 PropertyDescriptor thisDescriptor = propertyDescriptors[index];
90 if (thisDescriptor.getName().equals(propertyName))
91 {
92 propertyDescriptor = thisDescriptor;
93 break;
94 }
95 }
96
97 if (propertyDescriptor == null)
98 throw new RuntimeException("Could not find property '" + propertyName
99 + "' in class " + clazz.getName());
100 else
101 {
102 this.properties.add(new BeanProperty(propertyDescriptor.getName(),
103 propertyDescriptor.getPropertyType()));
104 nameToDescriptor
105 .put(propertyDescriptor.getName(), propertyDescriptor);
106 }
107 }
108 }
109 }
110
111 public String getDeclaration(String name, Map<String, Object> beanProperties)
112 {
113 StringBuffer buffer = new StringBuffer();
114 buffer.append(clazz.getName() + " " + name + " = new " + clazz.getName()
115 + "(");
116
117 if (beanProperties.containsKey("text"))
118 buffer.append("\"" + (String) beanProperties.get("text") + "\"");
119
120 buffer.append(");\n");
121 return buffer.toString();
122 }
123
124 public ComponentDef getComponentDef(String name, Map<String, Object> beanProperties)
125 {
126
127
128
129
130 String imp = "import " + clazz.getName() + ";";
131
132
133 String decl = clazz.getName() + " ${name}= new " + clazz.getName() + "(";
134
135 if (beanProperties.containsKey("text"))
136 decl += "\"" + (String) beanProperties.get("text") + "\"";
137 decl += ");";
138
139
140 String add = "${container}.add(${name}, \"${name}\");";
141
142 ComponentDef cd = new ComponentDef(name,imp,decl,add);
143 return cd;
144 }
145
146 public java.awt.Component getInstance(
147 java.util.Map<String, Object> objectProperties)
148 throws InstantiationException, IllegalAccessException,
149 InvocationTargetException
150 {
151 Object instance = clazz.newInstance();
152
153 for (Iterator i = objectProperties.keySet().iterator(); i.hasNext();)
154 {
155 String key = (String) i.next();
156 PropertyDescriptor propertyDescriptor = nameToDescriptor.get(key);
157 Object value = objectProperties.get(key);
158 Method writeMethod = propertyDescriptor.getWriteMethod();
159 writeMethod.invoke(instance, new Object[] { value });
160 }
161
162 return (Component) instance;
163 }
164
165 public boolean isComponentALayoutContainer()
166 {
167 return clazz.equals(JPanel.class);
168 }
169
170 public String toString()
171 {
172 return clazz.getName();
173 }
174
175 public List<BeanProperty> getProperties()
176 {
177 return properties;
178 }
179
180 }