source: vanHelsing/trunk/gui/src/de/dass_it/vanhelsing/gui/DataTree.java@ 865

Last change on this file since 865 was 865, checked in by tobias, on Apr 20, 2010 at 5:21:42 PM

comments added
all methods of the wsdl file implemented

File size: 4.1 KB
Line 
1package de.dass_it.vanhelsing.gui;
2
3import java.util.Enumeration;
4import javax.swing.tree.DefaultMutableTreeNode;
5import javax.swing.tree.DefaultTreeModel;
6import de.dass_it.vanhelsing.gui.items.ConcreteUserObjectItem;
7import de.dass_it.vanhelsing.gui.items.UserObjectItem;
8import de.dass_it.vanhelsing.gui.items.UserObjectItemType;
9/**
10 * DataTree contains the data tree of a navigation view and the method used by the view regarding the data tree
11 * @author tgoecke
12 */
13public class DataTree {
14 private DefaultTreeModel tree;
15 private DefaultMutableTreeNode root;
16
17 public DataTree() {}
18 /**
19 * creates a new data tree and sets the name value of the root node to the value of argument name
20 * @param name label of the root node
21 */
22 public void setTree(String name){
23 setRoot(new DefaultMutableTreeNode());
24 setTree(new DefaultTreeModel(getRoot()));
25 UserObjectItemType userType = new UserObjectItemType(getRoot());
26 ConcreteUserObjectItem obj = new ConcreteUserObjectItem();
27 getRoot().setUserObject(userType);
28 userType.setUserObject(obj);
29 userType.setLeaf(false);
30 obj.setResName(name);
31 }
32 /**
33 * creates a tree node as a child to the parent node containing the given UserObectItem
34 * @param parent root node of the created tree node
35 * @param userObject a user object which implements the UserObjectItem interface, usually a resource type object
36 * @return returns the created node object
37 */
38 public DefaultMutableTreeNode createNode(DefaultMutableTreeNode parent, UserObjectItem userObject){
39 DefaultMutableTreeNode node = new DefaultMutableTreeNode();
40 UserObjectItemType uoit = new UserObjectItemType(node);
41 node.setUserObject(uoit);
42 uoit.setUserObject(userObject);
43 parent.add(node);
44 ((UserObjectItemType)parent.getUserObject()).setLeaf(false);
45 return node;
46 }
47 /**
48 * replace a tree nodes UserObject
49 * @param node node object whose userObject will be updated
50 * @param userObject new UserObject to replace the nodes current UserObject
51 */
52 public void updateNode(DefaultMutableTreeNode node, UserObjectItem userObject){
53 ((UserObjectItemType)node.getUserObject()).setUserObject(userObject);
54 }
55 /**
56 * retrieves a node by the value of the UserObjects resId
57 * @param id value to be matched by the returned object
58 * @return first node whose UserObject attribute resId matches the argument id
59 */
60 public DefaultMutableTreeNode getNodeById(int id){
61 DefaultMutableTreeNode node;
62 int resId = new Integer(id);
63 Enumeration<DefaultMutableTreeNode> e = root.depthFirstEnumeration();
64
65 while (e.hasMoreElements()){
66 node = e.nextElement();
67 if (resId ==((ConcreteUserObjectItem)(((UserObjectItemType)node.getUserObject()).
68 getUserObject())).getResId()) {
69 return node;
70 }
71 }
72 return null;
73 }
74 /**
75 * retrieves a node by the value of the UserObjects resName
76 * @param name string value to be matched by the returned object
77 * @return first node whose UserObject attribute matches the argument name
78 */
79 public DefaultMutableTreeNode getNodeByName(String name){
80 DefaultMutableTreeNode node;
81 Enumeration<DefaultMutableTreeNode> e = root.depthFirstEnumeration();
82
83 while (e.hasMoreElements()){
84 node = e.nextElement();
85 if (name.equals(((ConcreteUserObjectItem)(((UserObjectItemType)node.getUserObject())
86 .getUserObject())).getResName())) {
87 return node;
88 }
89 }
90 return null;
91 }
92 /**
93 * deletes the given node from the data tree
94 * @param node the node which is deleted by this operation
95 * @return node the deleted node
96 * @throws ConstraintViolationException if the node has child nodes
97 */
98 public DefaultMutableTreeNode deleteNode(DefaultMutableTreeNode node) throws ConstraintViolationException{
99 if (node.getChildCount()>0) throw new ConstraintViolationException();
100 DefaultMutableTreeNode parent = (DefaultMutableTreeNode) node.getParent();
101 parent.remove(node);
102 return node;
103 }
104
105 public DefaultMutableTreeNode getRoot(){
106 return root;
107 }
108 public void setRoot(DefaultMutableTreeNode root){
109 this.root = root;
110 }
111 public DefaultTreeModel getTree() {
112 return tree;
113 }
114 public void setTree(DefaultTreeModel tree) {
115 this.tree = tree;
116 }
117}
Note: See TracBrowser for help on using the repository browser.