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

Last change on this file since 854 was 854, checked in by tobias, on Mar 26, 2010 at 6:01:33 PM

some beautification regarding the tree view

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