[1113] | 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 | |
---|
[922] | 15 | package de.ugoe.cs.autoquest.tasktrees.treeimpl; |
---|
[439] | 16 | |
---|
[1126] | 17 | import java.util.List; |
---|
| 18 | |
---|
[922] | 19 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration; |
---|
[1126] | 20 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptional; |
---|
[922] | 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; |
---|
[439] | 25 | |
---|
| 26 | /** |
---|
| 27 | * TODO comment |
---|
| 28 | * |
---|
| 29 | * @version $Revision: $ $Date: 21.02.2012$ |
---|
| 30 | * @author 2012, last modified by $Author: patrick$ |
---|
| 31 | */ |
---|
[557] | 32 | public class TaskTreeBuilder implements ITaskTreeBuilder { |
---|
[439] | 33 | |
---|
[557] | 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 | } |
---|
[439] | 45 | |
---|
[557] | 46 | addChildInternal(parent, -1, child); |
---|
[439] | 47 | } |
---|
| 48 | |
---|
[557] | 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 | } |
---|
[439] | 60 | |
---|
[557] | 61 | addChildInternal(parent, index, child); |
---|
[439] | 62 | } |
---|
| 63 | |
---|
[1126] | 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 | |
---|
[557] | 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 | } |
---|
[439] | 89 | |
---|
[557] | 90 | addChildInternal(parent, -1, child); |
---|
[439] | 91 | } |
---|
| 92 | |
---|
[557] | 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 | } |
---|
[439] | 104 | |
---|
[557] | 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); |
---|
[439] | 111 | } |
---|
| 112 | |
---|
[1126] | 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 | |
---|
[557] | 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 | } |
---|
[439] | 142 | |
---|
[557] | 143 | ((TaskTreeNode) parent).removeChild(index); |
---|
[439] | 144 | } |
---|
| 145 | |
---|
[557] | 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 | |
---|
[1126] | 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)))) |
---|
[557] | 163 | { |
---|
| 164 | ((TaskTreeNode) parent).removeChild(i); |
---|
| 165 | break; |
---|
| 166 | } |
---|
| 167 | } |
---|
[439] | 168 | } |
---|
| 169 | |
---|
[1126] | 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 | |
---|
[557] | 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); |
---|
[439] | 206 | } |
---|
| 207 | |
---|
[557] | 208 | /** |
---|
[988] | 209 | * |
---|
| 210 | */ |
---|
[557] | 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 | } |
---|
[439] | 216 | |
---|
[557] | 217 | if (index > -1) { |
---|
| 218 | ((TaskTreeNode) parent).addChild(index, child); |
---|
| 219 | } |
---|
| 220 | else { |
---|
| 221 | ((TaskTreeNode) parent).addChild(child); |
---|
| 222 | } |
---|
[439] | 223 | } |
---|
| 224 | |
---|
| 225 | } |
---|