1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany |
---|
2 | // |
---|
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
---|
4 | // you may not use this file except in compliance with the License. |
---|
5 | // You may obtain a copy of the License at |
---|
6 | // |
---|
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
---|
8 | // |
---|
9 | // Unless required by applicable law or agreed to in writing, software |
---|
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
---|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
12 | // See the License for the specific language governing permissions and |
---|
13 | // limitations under the License. |
---|
14 | |
---|
15 | package de.ugoe.cs.autoquest.tasktrees.treeimpl; |
---|
16 | |
---|
17 | import java.util.List; |
---|
18 | |
---|
19 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration; |
---|
20 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptional; |
---|
21 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection; |
---|
22 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence; |
---|
23 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeBuilder; |
---|
24 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNode; |
---|
25 | |
---|
26 | /** |
---|
27 | * TODO comment |
---|
28 | * |
---|
29 | * @version $Revision: $ $Date: 21.02.2012$ |
---|
30 | * @author 2012, last modified by $Author: patrick$ |
---|
31 | */ |
---|
32 | public class TaskTreeBuilder implements ITaskTreeBuilder { |
---|
33 | |
---|
34 | /* |
---|
35 | * (non-Javadoc) |
---|
36 | * |
---|
37 | * @see de.ugoe.cs.tasktree.treeifc.TaskTreeBuilder#addChild(Sequence, TaskTreeNode) |
---|
38 | */ |
---|
39 | @Override |
---|
40 | public void addChild(ISequence parent, ITaskTreeNode child) { |
---|
41 | if (!(parent instanceof Sequence)) { |
---|
42 | throw new IllegalArgumentException |
---|
43 | ("illegal type of task tree node provided: " + parent.getClass()); |
---|
44 | } |
---|
45 | |
---|
46 | addChildInternal(parent, -1, child); |
---|
47 | } |
---|
48 | |
---|
49 | /* |
---|
50 | * (non-Javadoc) |
---|
51 | * |
---|
52 | * @see de.ugoe.cs.tasktree.treeifc.TaskTreeBuilder#addChild(Sequence, int, TaskTreeNode) |
---|
53 | */ |
---|
54 | @Override |
---|
55 | public void addChild(ISequence parent, int index, ITaskTreeNode child) { |
---|
56 | if (!(parent instanceof Sequence)) { |
---|
57 | throw new IllegalArgumentException |
---|
58 | ("illegal type of task tree node provided: " + parent.getClass()); |
---|
59 | } |
---|
60 | |
---|
61 | addChildInternal(parent, index, child); |
---|
62 | } |
---|
63 | |
---|
64 | /* (non-Javadoc) |
---|
65 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeBuilder#setChild(de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence, int, de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNode) |
---|
66 | */ |
---|
67 | @Override |
---|
68 | public void setChild(ISequence parent, int index, ITaskTreeNode child) { |
---|
69 | if (!(parent instanceof Sequence)) { |
---|
70 | throw new IllegalArgumentException |
---|
71 | ("illegal type of task tree node provided: " + parent.getClass()); |
---|
72 | } |
---|
73 | |
---|
74 | ((TaskTreeNode) parent).removeChild(index); |
---|
75 | addChildInternal(parent, index, child); |
---|
76 | } |
---|
77 | |
---|
78 | /* |
---|
79 | * (non-Javadoc) |
---|
80 | * |
---|
81 | * @see de.ugoe.cs.tasktree.treeifc.TaskTreeBuilder#addChild(Selection, TaskTreeNode) |
---|
82 | */ |
---|
83 | @Override |
---|
84 | public void addChild(ISelection parent, ITaskTreeNode child) { |
---|
85 | if (!(parent instanceof Selection)) { |
---|
86 | throw new IllegalArgumentException |
---|
87 | ("illegal type of task tree node provided: " + parent.getClass()); |
---|
88 | } |
---|
89 | |
---|
90 | addChildInternal(parent, -1, child); |
---|
91 | } |
---|
92 | |
---|
93 | /* |
---|
94 | * (non-Javadoc) |
---|
95 | * |
---|
96 | * @see de.ugoe.cs.tasktree.treeifc.TaskTreeBuilder#setChild(Iteration, TaskTreeNode) |
---|
97 | */ |
---|
98 | @Override |
---|
99 | public void setChild(IIteration iteration, ITaskTreeNode newChild) { |
---|
100 | if (!(iteration instanceof Iteration)) { |
---|
101 | throw new IllegalArgumentException |
---|
102 | ("illegal type of iteration provided: " + iteration.getClass()); |
---|
103 | } |
---|
104 | |
---|
105 | if (!(newChild instanceof TaskTreeNode)) { |
---|
106 | throw new IllegalArgumentException |
---|
107 | ("illegal type of task tree node provided: " + newChild.getClass()); |
---|
108 | } |
---|
109 | |
---|
110 | ((Iteration) iteration).setChild(newChild); |
---|
111 | } |
---|
112 | |
---|
113 | /* (non-Javadoc) |
---|
114 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeBuilder#setChild(de.ugoe.cs.autoquest.tasktrees.treeifc.IOptional, de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNode) |
---|
115 | */ |
---|
116 | @Override |
---|
117 | public void setChild(IOptional optional, ITaskTreeNode newChild) { |
---|
118 | if (!(optional instanceof Optional)) { |
---|
119 | throw new IllegalArgumentException |
---|
120 | ("illegal type of optional provided: " + optional.getClass()); |
---|
121 | } |
---|
122 | |
---|
123 | if (!(newChild instanceof TaskTreeNode)) { |
---|
124 | throw new IllegalArgumentException |
---|
125 | ("illegal type of task tree node provided: " + newChild.getClass()); |
---|
126 | } |
---|
127 | |
---|
128 | ((Optional) optional).setChild(newChild); |
---|
129 | } |
---|
130 | |
---|
131 | /* |
---|
132 | * (non-Javadoc) |
---|
133 | * |
---|
134 | * @see de.ugoe.cs.tasktree.treeifc.TaskTreeBuilder#removeChild(Sequence, int) |
---|
135 | */ |
---|
136 | @Override |
---|
137 | public void removeChild(ISequence parent, int index) { |
---|
138 | if (!(parent instanceof TaskTreeNode)) { |
---|
139 | throw new IllegalArgumentException |
---|
140 | ("illegal type of task tree node provided: " + parent.getClass()); |
---|
141 | } |
---|
142 | |
---|
143 | ((TaskTreeNode) parent).removeChild(index); |
---|
144 | } |
---|
145 | |
---|
146 | /* |
---|
147 | * (non-Javadoc) |
---|
148 | * |
---|
149 | * @see de.ugoe.cs.tasktree.treeifc.TaskTreeBuilder#removeChild(Selection, TaskTreeNode) |
---|
150 | */ |
---|
151 | @Override |
---|
152 | public void removeChild(ISelection parent, ITaskTreeNode child) { |
---|
153 | if (!(parent instanceof TaskTreeNode)) { |
---|
154 | throw new IllegalArgumentException |
---|
155 | ("illegal type of task tree node provided: " + parent.getClass()); |
---|
156 | } |
---|
157 | |
---|
158 | List<ITaskTreeNode> children = parent.getChildren(); |
---|
159 | |
---|
160 | for (int i = 0; i < children.size(); i++) { |
---|
161 | if ((children.get(i) == child) || |
---|
162 | ((children.get(i) != null) && (children.get(i).equals(child)))) |
---|
163 | { |
---|
164 | ((TaskTreeNode) parent).removeChild(i); |
---|
165 | break; |
---|
166 | } |
---|
167 | } |
---|
168 | } |
---|
169 | |
---|
170 | /* (non-Javadoc) |
---|
171 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeBuilder#replaceChild(de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection, de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNode, de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNode) |
---|
172 | */ |
---|
173 | @Override |
---|
174 | public void replaceChild(ISelection parent, ITaskTreeNode oldChild, ITaskTreeNode newChild) { |
---|
175 | if (!(parent instanceof TaskTreeNode)) { |
---|
176 | throw new IllegalArgumentException |
---|
177 | ("illegal type of task tree node provided: " + parent.getClass()); |
---|
178 | } |
---|
179 | |
---|
180 | List<ITaskTreeNode> children = parent.getChildren(); |
---|
181 | |
---|
182 | for (int i = 0; i < children.size(); i++) { |
---|
183 | if ((children.get(i) == oldChild) || |
---|
184 | ((children.get(i) != null) && (children.get(i).equals(oldChild)))) |
---|
185 | { |
---|
186 | ((TaskTreeNode) parent).removeChild(i); |
---|
187 | ((TaskTreeNode) parent).addChild(i, newChild); |
---|
188 | break; |
---|
189 | } |
---|
190 | } |
---|
191 | } |
---|
192 | |
---|
193 | /* |
---|
194 | * (non-Javadoc) |
---|
195 | * |
---|
196 | * @see de.ugoe.cs.tasktree.treeifc.TaskTreeBuilder#setDescription(TaskTreeNode, String) |
---|
197 | */ |
---|
198 | @Override |
---|
199 | public void setDescription(ITaskTreeNode parent, String description) { |
---|
200 | if (!(parent instanceof TaskTreeNode)) { |
---|
201 | throw new IllegalArgumentException |
---|
202 | ("illegal type of task tree node provided: " + parent.getClass()); |
---|
203 | } |
---|
204 | |
---|
205 | ((TaskTreeNode) parent).setDescription(description); |
---|
206 | } |
---|
207 | |
---|
208 | /** |
---|
209 | * |
---|
210 | */ |
---|
211 | private void addChildInternal(ITaskTreeNode parent, int index, ITaskTreeNode child) { |
---|
212 | if (!(child instanceof TaskTreeNode)) { |
---|
213 | throw new IllegalArgumentException |
---|
214 | ("illegal type of task tree node provided: " + child.getClass()); |
---|
215 | } |
---|
216 | |
---|
217 | if (index > -1) { |
---|
218 | ((TaskTreeNode) parent).addChild(index, child); |
---|
219 | } |
---|
220 | else { |
---|
221 | ((TaskTreeNode) parent).addChild(child); |
---|
222 | } |
---|
223 | } |
---|
224 | |
---|
225 | } |
---|