source: vanHelsing/branches/guitechdemo/src/de/dass_it/vanhelsing/gui/TreeNavigation.java@ 891

Last change on this file since 891 was 849, checked in by tobias, on Mar 23, 2010 at 3:01:06 PM

configView with item data

File size: 8.0 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 */
18public class TreeNavigation extends BeanUtil {
19 private DefaultTreeModel tree;
20 //private Logger logger = Logger.getLogger(this.getClass());
21 public DefaultTreeModel createTree(DefaultMutableTreeNode root,
22 String[] nodes, String title){
23
24 DefaultMutableTreeNode node;
25 DefaultMutableTreeNode myNode;
26 tree = new DefaultTreeModel(root);
27 UserObjectItemType item = new UserObjectItemType(root);
28 UserObjectItem uoi = new TreeItem("Configuration");
29 uoi.setResType("Tree");
30 item.setUserObjectItem(uoi);
31 root.setUserObject(item);
32
33 for(int i = 0; i< nodes.length; i++){
34 //root.add(addNode(root, "Tree", nodes[i]));
35 myNode = null;
36 myNode = addNode(root, nodes[i], nodes[i]);
37 if (myNode != null){
38 root.add(myNode);
39 }
40 }
41 return tree;
42 }
43
44 public DefaultMutableTreeNode addNode(DefaultMutableTreeNode parent, String type, String title){
45 DefaultMutableTreeNode node = new DefaultMutableTreeNode();
46 Class subClass;
47 Class myClass;
48 Method[] methods;
49 Object o;
50 UserObjectItemType userObject = new UserObjectItemType(node);
51 String type2 = "de.dass_it.vanhelsing.gui.items." + type + "Item";
52
53 try {
54 subClass = Class.forName(type2);
55 o = subClass.newInstance();
56 UserObjectItem item = (UserObjectItem) o;
57 item.setResType(type);
58 myClass = item.getClass().asSubclass(subClass);
59 methods = myClass.getMethods();
60 for (Method m : methods){
61 //System.err.println(m.getName());
62 if(m.getName().contains("setValue")){
63 m.invoke(item, title);
64 }
65 }
66 //ItemObjekt erzeugen und an das UserObjekt referenzieren
67 userObject.setUserObjectItem((UserObjectItem)o);
68 //Eine Referenz auf das UserObjekt wird an das Nodeobjekt übergeben
69 node.setUserObject(userObject);
70 return node;
71 } catch (ClassNotFoundException e1){
72 System.err.println("Itemclass "+ type2 +" must be in the classpath");
73 } catch (InstantiationException e2){
74 System.err.println("Class "+type2+" must be a concrete class, not an interface or an abstract class");
75 } catch (IllegalAccessException e3){
76 System.err.println("Class " + type2 + " lacks a no-argument constructor");
77 } catch (Exception e){
78 System.err.println("addHeader: " + e.toString());
79 }
80
81 return null;
82 }
83 public DefaultMutableTreeNode addNode(DefaultMutableTreeNode parent, String type, ResourceInfo ri){
84 DefaultMutableTreeNode node = new DefaultMutableTreeNode();
85 Class subClass;
86 Class myClass;
87 Method[] riMethods;
88 Method[] methods;
89 Object o;
90 UserObjectItemType userObject = new UserObjectItemType(node);
91 String type2 = "de.dass_it.vanhelsing.gui.items." + type + "Item";
92
93 try {
94 subClass = Class.forName(type2);
95 o = subClass.newInstance();
96 UserObjectItem item = (UserObjectItem) o;
97 item.setResType(type);
98 myClass = item.getClass().asSubclass(subClass);
99 methods = myClass.getMethods();
100 ;
101 /*for (Method m : methods){
102 if(m.getName().contains("set" + p)){
103 m.invoke(item, title);
104 }
105 }*/
106 for (Method n : ri.getClass().getDeclaredMethods()){
107 if (n.getName().startsWith("get")
108 && !(n.getName().contains("OMElement") || n.getName().contains("PullParser"))){
109
110 //System.err.println("Name: " + n.getName() +" Feld: "+ n.getName().substring(3));
111 for (Method m : methods){
112 if (m.getName().equals("set"+n.getName().substring(3))){
113 m.invoke(item, n.invoke(ri, (Object[])null));
114 break;
115 }
116 }
117
118
119 }
120
121 }
122 //Value-Wert für die TreeView übertragen
123 ((ItemType)o).setValue(((ItemType)o).getResName());
124 //ItemObjekt erzeugen und an das UserObjekt referenzieren
125 userObject.setUserObjectItem((UserObjectItem)o);
126 //Eine Referenz auf das UserObjekt wird an das Nodeobjekt übergeben
127 node.setUserObject(userObject);
128 return node;
129 } catch (ClassNotFoundException e1){
130 System.err.println("Itemclass "+ type2 +" must be in the classpath");
131 } catch (InstantiationException e2){
132 System.err.println("Class "+type2+" must be a concrete class, not an interface or an abstract class");
133 } catch (IllegalAccessException e3){
134 System.err.println("Class " + type2 + " lacks a no-argument constructor");
135 } catch (Exception e){
136 System.err.println("RI: " + e.toString());
137 }
138
139 return null;
140 }
141 public DefaultMutableTreeNode addNode(DefaultMutableTreeNode parent, String type, ResourceAttributeType[] ra){
142
143 //DefaultMutableTreeNode node = new DefaultMutableTreeNode();
144 Class subClass;
145 Class myClass;
146 Method[] raMethods;
147 Method[] methods;
148 Object o;
149 //UserObjectItemType userObject = new UserObjectItemType(node);
150 String type2 = "de.dass_it.vanhelsing.gui.items." + type + "Item";
151 String key = null, value = null;
152 try {
153 subClass = Class.forName(type2);
154 o = subClass.newInstance();
155 UserObjectItem item = ((UserObjectItemType)parent.getUserObject()).getUserObject();
156 //item.setResType(type);
157 myClass = item.getClass().asSubclass(subClass);
158 methods = myClass.getMethods();
159 Method m;
160 Method n;
161 Class[] args = new Class[1];
162 args[0] = String.class;
163 for(ResourceAttributeType rat : ra){
164 /*for (Method n : rat.getClass().getDeclaredMethods()){
165 if (n.getName().equals("getKey")) {
166 key = (String)(n.invoke(rat, (Object[])null));
167 }
168 if (n.getName().equals("getValue")){
169 value = (String)(n.invoke(rat, (Object[])null));
170
171 }
172
173 }*/
174 n = (rat.getClass().getMethod("getKey", (Class[])null));
175 key = (String)n.invoke(rat, (Object[])null);
176 n = (rat.getClass().getMethod("getValue", (Class[])null));
177 value = (String)n.invoke(rat, (Object[])null);
178
179 if(!(key.isEmpty() && value.isEmpty())){
180 key = key.replaceAll(" ", "");
181 key = key.replaceFirst(new Character(key.charAt(0)).toString(),
182 new Character(Character.toUpperCase(key.charAt(0))).toString());
183 m = myClass.getMethod(("set"+key), args);
184 m.invoke(item, value);
185 /*if(m.getName().equals("getName") || m.getName().equals("setName")){
186 ((ItemType)o).setValue(key + ": " + value);
187 }*/
188
189 }
190 }
191
192
193
194
195 //Value-Wert aus Key: Value zusammenbauen
196
197 //ItemObjekt erzeugen und an das UserObjekt referenzieren
198 //userObject.setUserObjectItem((UserObjectItem)o);
199 //Eine Referenz auf das UserObjekt wird an das Nodeobjekt übergeben
200 //node.setUserObject(userObject);
201 //System.out.println(((ItemType)((UserObjectItem)(node.getUserObject()))).getResId());
202 return parent;
203 } catch (ClassNotFoundException e1){
204 System.err.println("Itemclass "+ type2 +" must be in the classpath");
205 } catch (InstantiationException e2){
206 System.err.println("Class "+type2+" must be a concrete class, not an interface or an abstract class");
207 } catch (IllegalAccessException e3){
208 System.err.println("Class " + type2 + " lacks a no-argument constructor");
209 } catch (Exception e){
210 System.err.println("Leaf: " + e.toString());
211 }
212
213 return null;
214 }
215 public DefaultMutableTreeNode getNode(String id, DefaultTreeModel tree){
216 DefaultMutableTreeNode root;
217 DefaultMutableTreeNode node;
218 int resId = new Integer(id);
219 root = (DefaultMutableTreeNode)tree.getRoot();
220 Enumeration<DefaultMutableTreeNode> e = root.depthFirstEnumeration();
221
222 while (e.hasMoreElements()){
223 node = e.nextElement();
224 if (resId ==((ItemType)(((UserObjectItemType)node.getUserObject()).getUserObject())).getResId()) {
225 return node;
226 }
227 }
228 return null;
229 }
230 public UserObjectItem getNodeObject(String id, DefaultTreeModel tree){
231 DefaultMutableTreeNode node = getNode(id, tree);
232 return (UserObjectItem)((UserObjectItem)(node.getUserObject()));
233 }
234}
Note: See TracBrowser for help on using the repository browser.