source: vanHelsing/trunk/gui/src/de/dass_it/vanhelsing/gui/TreeNavigation.java@ 867

Last change on this file since 867 was 867, checked in by tobias, on Apr 26, 2010 at 6:20:44 PM

create Client function added

File size: 10.1 KB
Line 
1package de.dass_it.vanhelsing.gui;
2
3//import org.apache.log4j.Logger;
4import java.lang.reflect.Method;
5import java.util.Enumeration;
6
7import javax.swing.tree.DefaultTreeModel;
8import javax.swing.tree.DefaultMutableTreeNode;
9import de.dass_it.vanhelsing.gui.items.*;
10import de.dass_it.www.vanhelsing.*;
11import de.dass_it.www.vanhelsing.VanHelsingCallbackHandler.*;
12import de.dass_it.www.vanhelsing.VanHelsingStub.*;
13
14/**
15 * Helper class to encapsulate all procedures
16 * to work with the tree structure.
17 * @author tgoecke
18 */
19public class TreeNavigation extends BeanUtil {
20 private DefaultTreeModel tree;
21 //private Logger logger = Logger.getLogger(this.getClass());
22 /**
23 * @param root root node of the tree to be created
24 * @param nodes list of names (resource types) used to create header nodes
25 * @param title argument currently not used.
26 * @return tree returns a data tree to be used by a view bean
27 */
28 public DefaultTreeModel createTree(DefaultMutableTreeNode root,
29 String[] nodes, String title){
30
31 DefaultMutableTreeNode node;
32 DefaultMutableTreeNode myNode;
33 tree = new DefaultTreeModel(root);
34 UserObjectItemType item = new UserObjectItemType(root);
35 item.setLeaf(false);
36 item.setExpanded(true);
37 UserObjectItem uoi = new TreeItem("Configuration");
38 uoi.setResType("Tree");
39 item.setUserObject(uoi);
40 root.setUserObject(item);
41
42 for(int i = 0; i< nodes.length; i++){
43 //root.add(addNode(root, "Tree", nodes[i]));
44 myNode = null;
45 myNode = addNode(root, nodes[i], nodes[i]);
46 if (myNode != null){
47 root.add(myNode);
48 }
49 }
50 return tree;
51 }
52 /**
53 * adds a node to the given parent node, of resource type string and label value title
54 * @param parent parent node of the created node
55 * @param type resource type of the created node
56 * @param title label value of the created type
57 * @return node the created node
58 */
59 public DefaultMutableTreeNode addNode(DefaultMutableTreeNode parent, String type, String title){
60 DefaultMutableTreeNode node = new DefaultMutableTreeNode();
61 Class subClass;
62 Class myClass;
63 Method[] methods;
64 Object o;
65 UserObjectItemType userObject = new UserObjectItemType(node);
66 userObject.setLeaf(false);
67 String type2 = "de.dass_it.vanhelsing.gui.items." + type + "Item";
68
69 try {
70 subClass = Class.forName(type2);
71 o = subClass.newInstance();
72 UserObjectItem item = (UserObjectItem) o;
73 item.setResType(type);
74 myClass = item.getClass().asSubclass(subClass);
75 methods = myClass.getMethods();
76 for (Method m : methods){
77 //System.err.println(m.getName());
78 if(m.getName().contains("setValue")){
79 m.invoke(item, title);
80 }
81 }
82 //ItemObjekt erzeugen und an das UserObjekt referenzieren
83 userObject.setUserObject((UserObjectItem)o);
84 //Eine Referenz auf das UserObjekt wird an das Nodeobjekt übergeben
85 node.setUserObject(userObject);
86 return node;
87 } catch (ClassNotFoundException e1){
88 System.err.println("Itemclass "+ type2 +" must be in the classpath");
89 } catch (InstantiationException e2){
90 System.err.println("Class "+type2+" must be a concrete class, not an interface or an abstract class");
91 } catch (IllegalAccessException e3){
92 System.err.println("Class " + type2 + " lacks a no-argument constructor");
93 } catch (Exception e){
94 System.err.println("addHeader: " + e.toString());
95 }
96
97 return null;
98 }
99 /**
100 * adds a node to the given parent node of resourcetype type which contains the values of the ResourceInfo() object
101 * @param parent parent node of the created node
102 * @param type resource type of the created node
103 * @param ri the data object which is used to update the node UserObject
104 * @return node the created node
105 */
106 public DefaultMutableTreeNode addNode(DefaultMutableTreeNode parent, String type, ResourceInfo ri){
107 DefaultMutableTreeNode node = new DefaultMutableTreeNode();
108 Class subClass;
109 Class myClass;
110 Method[] riMethods;
111 Method[] methods;
112 Object o;
113 UserObjectItemType userObject = new UserObjectItemType(node);
114 String type2 = "de.dass_it.vanhelsing.gui.items." + type + "Item";
115
116 try {
117 subClass = Class.forName(type2);
118 o = subClass.newInstance();
119 UserObjectItem item = (UserObjectItem) o;
120 item.setResType(type);
121 myClass = item.getClass().asSubclass(subClass);
122 methods = myClass.getMethods();
123 ;
124 /*for (Method m : methods){
125 if(m.getName().contains("set" + p)){
126 m.invoke(item, title);
127 }
128 }*/
129 for (Method n : ri.getClass().getDeclaredMethods()){
130 if (n.getName().startsWith("get")
131 && !(n.getName().contains("OMElement") || n.getName().contains("PullParser"))){
132
133 //System.err.println("Name: " + n.getName() +" Feld: "+ n.getName().substring(3));
134 for (Method m : methods){
135 if (m.getName().equals("set"+n.getName().substring(3))){
136 m.invoke(item, n.invoke(ri, (Object[])null));
137 break;
138 }
139 }
140
141
142 }
143
144 }
145 //Value-Wert für die TreeView übertragen
146 ((ItemType)o).setValue(((ItemType)o).getResName());
147 //ItemObjekt erzeugen und an das UserObjekt referenzieren
148 userObject.setUserObject((UserObjectItem)o);
149 //Eine Referenz auf das UserObjekt wird an das Nodeobjekt übergeben
150 node.setUserObject(userObject);
151 return node;
152 } catch (ClassNotFoundException e1){
153 System.err.println("Itemclass "+ type2 +" must be in the classpath");
154 } catch (InstantiationException e2){
155 System.err.println("Class "+type2+" must be a concrete class, not an interface or an abstract class");
156 } catch (IllegalAccessException e3){
157 System.err.println("Class " + type2 + " lacks a no-argument constructor");
158 } catch (Exception e){
159 System.err.println("RI: " + e.toString());
160 }
161
162 return null;
163 }
164 /**
165 * adds a node to the given parent node of resourcetype type which contains the values of the ResourceAttributeType[]
166 * @param parent parent node of the created node
167 * @param type resource type of the created node
168 * @param ra a list of key value pairs returned by the web service access methods
169 * @return node the updated parent node
170 */
171 public DefaultMutableTreeNode addNode(DefaultMutableTreeNode parent, String type, ResourceAttributeType[] ra){
172
173 //DefaultMutableTreeNode node = new DefaultMutableTreeNode();
174 Class subClass;
175 Class myClass;
176 Method[] raMethods;
177 Method[] methods;
178 Object o;
179 //UserObjectItemType userObject = new UserObjectItemType(node);
180 String type2 = "de.dass_it.vanhelsing.gui.items." + type + "Item";
181 String key = null, value = null;
182 try {
183 subClass = Class.forName(type2);
184 o = subClass.newInstance();
185 UserObjectItem item = ((UserObjectItemType)parent.getUserObject()).getUserObject();
186 //item.setResType(type);
187 myClass = item.getClass().asSubclass(subClass);
188 methods = myClass.getMethods();
189 Method m;
190 Method n;
191 Class[] args = new Class[1];
192 args[0] = String.class;
193 for(ResourceAttributeType rat : ra){
194 /*for (Method n : rat.getClass().getDeclaredMethods()){
195 if (n.getName().equals("getKey")) {
196 key = (String)(n.invoke(rat, (Object[])null));
197 }
198 if (n.getName().equals("getValue")){
199 value = (String)(n.invoke(rat, (Object[])null));
200
201 }
202
203 }*/
204 n = (rat.getClass().getMethod("getKey", (Class[])null));
205 key = (String)n.invoke(rat, (Object[])null);
206 n = (rat.getClass().getMethod("getValue", (Class[])null));
207 value = (String)n.invoke(rat, (Object[])null);
208
209 if(!(key.isEmpty() && value.isEmpty())){
210 key = key.replaceAll(" ", "");
211 key = key.toLowerCase();
212 key = key.replaceFirst(new Character(key.charAt(0)).toString(),
213 new Character(Character.toUpperCase(key.charAt(0))).toString());
214 m = myClass.getMethod(("set"+key), args);
215 m.invoke(item, value);
216 /*if(m.getName().equals("getName") || m.getName().equals("setName")){
217 ((ItemType)o).setValue(key + ": " + value);
218 }*/
219
220 }
221 }
222 //Value-Wert aus Key: Value zusammenbauen
223
224 //ItemObjekt erzeugen und an das UserObjekt referenzieren
225 //userObject.setUserObjectItem((UserObjectItem)o);
226 //Eine Referenz auf das UserObjekt wird an das Nodeobjekt übergeben
227 //node.setUserObject(userObject);
228 //System.out.println(((ItemType)((UserObjectItem)(node.getUserObject()))).getResId());
229 return parent;
230 } catch (ClassNotFoundException e1){
231 System.err.println("Itemclass "+ type2 +" must be in the classpath");
232 } catch (InstantiationException e2){
233 System.err.println("Class "+type2+" must be a concrete class, not an interface or an abstract class");
234 } catch (IllegalAccessException e3){
235 System.err.println("Class " + type2 + " lacks a no-argument constructor");
236 } catch (Exception e){
237 System.err.println("Leaf: " + e.toString());
238 }
239
240 return null;
241 }
242 /**
243 * returns the first node whose UserObject attribute resId matches the given id or null if no node is found
244 * @param id resId value of the node
245 * @param tree tree object which contains the node
246 * @return matching node or null if no mathcing node is found
247 */
248 public DefaultMutableTreeNode getNode(String id, DefaultTreeModel tree){
249 DefaultMutableTreeNode root;
250 DefaultMutableTreeNode node;
251 int resId = new Integer(id);
252 root = (DefaultMutableTreeNode)tree.getRoot();
253 Enumeration<DefaultMutableTreeNode> e = root.depthFirstEnumeration();
254
255 while (e.hasMoreElements()){
256 node = e.nextElement();
257 if (resId ==((ItemType)(((UserObjectItemType)node.getUserObject()).getUserObject())).getResId()) {
258 return node;
259 }
260
261
262 }
263 return null;
264 }
265
266 public DefaultMutableTreeNode getNodeByName(String value, DefaultTreeModel tree){
267 DefaultMutableTreeNode root;
268 DefaultMutableTreeNode node;
269
270 root = (DefaultMutableTreeNode)tree.getRoot();
271 Enumeration<DefaultMutableTreeNode> e = root.depthFirstEnumeration();
272
273 while (e.hasMoreElements()){
274 node = e.nextElement();
275 if (value.equals( ((ItemType)(((UserObjectItemType)node.getUserObject()).getUserObject())).getValue() )){
276 return node;
277 }
278 }
279 return null;
280 }
281
282 /**
283 * returns the UserObject of a node whose resId attribute matches id
284 */
285 public UserObjectItem getNodeObject(String id, DefaultTreeModel tree){
286 DefaultMutableTreeNode node = getNode(id, tree);
287 return (UserObjectItem)((UserObjectItem)(node.getUserObject()));
288 }
289}
Note: See TracBrowser for help on using the repository browser.