1 package com.sri.emo.dbobj.model_tree;
2
3 import com.jcorporate.expresso.core.controller.Transition;
4
5 import java.util.ArrayList;
6 import java.util.Collections;
7 import java.util.Iterator;
8 import java.util.List;
9
10 /***
11 * Simple base class of all the mutable nodes. Allows for adding and
12 * subtracting any types of nodes we wish. Just derive to add your specific
13 * type and factory.
14 *
15 * @author Michael Rimov
16 */
17 abstract public class AbstractModelNode implements MutableModelNode {
18
19 /***
20 * Children of the model
21 */
22 private List children = null;
23
24 /***
25 * The parent of the node.
26 */
27 private final ModelNode parent;
28
29 /***
30 * The label for the item.
31 */
32 private String label;
33
34 /***
35 * The link to view/edit the node.
36 */
37 private Transition link;
38
39
40 /***
41 * Constructor for when there is no parent.
42 */
43 public AbstractModelNode() {
44 parent = ModelNode.NO_PARENT;
45 link = NO_LINK;
46 label = "";
47 }
48
49 /***
50 * Constructor for when we want to attach to a parent. The parent
51 * node automatically receives the addChild() message from this object.
52 *
53 * @param nodeParent MutableModelNode
54 */
55 public AbstractModelNode(final MutableModelNode nodeParent) {
56 parent = nodeParent;
57 nodeParent.addChild(this);
58 link = NO_LINK;
59 label = "";
60 }
61
62 /***
63 * Add a child node to the given mutable model.
64 *
65 * @param newNode ModelCompositeNode
66 * @todo Implement this com.sri.emo.dbobj.model_tree.MutableModelNode
67 * method
68 */
69 public void addChild(final ModelNode newNode) {
70 if (children == null) {
71 children = new ArrayList();
72 }
73 children.add(newNode);
74 }
75
76 /***
77 * Retrieve the items in the composite model.
78 *
79 * @return a list of ModelComposite objects or NO_CHILDREN if this is a
80 * leaf node.
81 */
82 public List getChildren() {
83 if (children == null || children.size() == 0) {
84 return NO_CHILDREN;
85 } else {
86 return Collections.unmodifiableList(children);
87 }
88 }
89
90 /***
91 * Retrieves the parent or returns NO_PARENT if we are already at the top
92 * of the node.
93 *
94 * @return ModelCompositeNode or NO_PARENT.
95 */
96 public ModelNode getParent() {
97 return parent;
98 }
99
100
101 /***
102 * Allows for iteration of the contents of the node, possibly removing
103 * them if the iterator supports removal.
104 *
105 * @return Iterator that returns MutableModelNode instances.
106 */
107 public Iterator iterator() {
108
109
110
111 if (children == null) {
112 return NO_CHILDREN.iterator();
113 } else {
114
115 return children.iterator();
116 }
117 }
118
119 /***
120 * Retrieve the label assigned to the node.
121 *
122 * @return String label.
123 */
124 public String getLabel() {
125 return label;
126 }
127
128 /***
129 * Retrieve the edit link
130 *
131 * @return String
132 */
133 public Transition getLink() {
134 return link;
135 }
136
137 /***
138 * Set the label of the node.
139 *
140 * @param label String
141 */
142 public void setLabel(final String label) {
143 this.label = label;
144 }
145
146 /***
147 * Set the transition.
148 *
149 * @param link Transition
150 */
151 public void setLink(final Transition link) {
152 this.link = link;
153 }
154 }