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