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