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

Last change on this file since 858 was 858, checked in by tobias, on Apr 9, 2010 at 10:56:07 AM

visual design slightly modified. tree functions extended

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