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