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

Last change on this file since 847 was 847, checked in by tobias, on Mar 19, 2010 at 5:51:31 PM

displays key value pairs as part of the tree

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