[927] | 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. |
---|
[820] | 14 | |
---|
[922] | 15 | package de.ugoe.cs.autoquest.eventcore.guimodel; |
---|
[545] | 16 | |
---|
[643] | 17 | import java.io.OutputStream; |
---|
| 18 | import java.io.PrintStream; |
---|
[1116] | 19 | import java.io.Serializable; |
---|
[746] | 20 | import java.io.UnsupportedEncodingException; |
---|
[545] | 21 | import java.util.ArrayList; |
---|
[603] | 22 | import java.util.LinkedList; |
---|
[545] | 23 | import java.util.List; |
---|
[1086] | 24 | import java.util.Stack; |
---|
[821] | 25 | import java.util.logging.Level; |
---|
[545] | 26 | |
---|
[821] | 27 | import de.ugoe.cs.util.console.Console; |
---|
| 28 | |
---|
[545] | 29 | /** |
---|
[576] | 30 | * <p> |
---|
[746] | 31 | * A GUI model is a tree of {@link IGUIElements} and represents a complete GUI of a software. It is |
---|
| 32 | * platform independent. It may have several root nodes, as some GUIs are made up of several Frames |
---|
| 33 | * being independent from each other. The GUI model is filled using the |
---|
[820] | 34 | * {@link #integratePath(List, IGUIElementFactory)} method. |
---|
[576] | 35 | * </p> |
---|
[545] | 36 | * |
---|
[821] | 37 | * @version 1.0 |
---|
[820] | 38 | * @author Patrick Harms, Steffen Herbold |
---|
[545] | 39 | */ |
---|
[1116] | 40 | public class GUIModel implements Serializable { |
---|
[820] | 41 | |
---|
[1116] | 42 | /** */ |
---|
| 43 | private static final long serialVersionUID = 1L; |
---|
| 44 | |
---|
[545] | 45 | /** |
---|
[746] | 46 | * <p> |
---|
[831] | 47 | * The root node of the tree not provided externally. |
---|
[746] | 48 | * </p> |
---|
[545] | 49 | */ |
---|
[576] | 50 | private TreeNode root = new TreeNode(); |
---|
[820] | 51 | |
---|
[545] | 52 | /** |
---|
[746] | 53 | * <p> |
---|
[831] | 54 | * A list with all nodes currently known |
---|
[746] | 55 | * </p> |
---|
[545] | 56 | */ |
---|
[576] | 57 | private List<TreeNode> allNodes = new ArrayList<TreeNode>(); |
---|
[545] | 58 | |
---|
| 59 | /** |
---|
[746] | 60 | * <p> |
---|
| 61 | * Integrates a path of GUI elements into the GUI model. The GUI model itself is a tree and |
---|
| 62 | * therefore a set of different paths through the tree that start with a root node and end with |
---|
| 63 | * a leaf node. Such a path can be added to the tree. The method checks, if any of the GUI |
---|
| 64 | * elements denoted by the path already exists. If so, it reuses it. It may therefore also |
---|
[820] | 65 | * return an existing GUI element being the leaf node of the provided path. If a GUI element of |
---|
| 66 | * the path does not exist yet, it creates a new one using the provided GUI element factory. |
---|
[746] | 67 | * </p> |
---|
| 68 | * <p> |
---|
[820] | 69 | * If a GUI element specification describes an existing GUI element or not is determined through |
---|
| 70 | * comparing the GUI element specifications of the existing GUI elements with the ones provided |
---|
| 71 | * in the path. The comparison is done using the |
---|
| 72 | * {@link IGUIElementSpec#getSimilarity(IGUIElementSpec)} method. The comparison is only done on |
---|
| 73 | * the correct levels. I.e. the currently known root elements of the tree are only compared to |
---|
| 74 | * the first element in the path. If the correct one is found or created, its children are |
---|
[746] | 75 | * compared only to the second specification in the path, and so on. |
---|
| 76 | * </p> |
---|
| 77 | * <p> |
---|
| 78 | * The returned GUI elements are singletons. I.e. it is tried to return always the identical |
---|
[820] | 79 | * object for the same denoted element. However, while creating the GUI model, the similarity of |
---|
| 80 | * GUI elements may change. Therefore, the method might determine, that two formerly different |
---|
| 81 | * nodes are now similar. (This may happen, e.g. if GUI elements do not have initial names which |
---|
| 82 | * are set afterwards. Therefore, first they are handled differently and later they can be |
---|
| 83 | * identified as being the same.) In such a case, there are already several GUI element objects |
---|
| 84 | * instantiated for the same GUI element. The singleton paradigm gets broken. Therefore, such |
---|
| 85 | * GUI element objects are registered with each other, so that their equal method can determine |
---|
| 86 | * equality again correctly, although the objects are no singletons anymore. |
---|
[746] | 87 | * </p> |
---|
[820] | 88 | * |
---|
| 89 | * @param guiElementPath |
---|
| 90 | * the path to integrate into the model |
---|
| 91 | * @param guiElementFactory |
---|
| 92 | * the GUI element factory to be used for instantiating GUI element objects |
---|
| 93 | * |
---|
[746] | 94 | * @return The GUI element object representing the GUI element denoted by the provided path |
---|
| 95 | * |
---|
[820] | 96 | * @throws GUIModelException |
---|
| 97 | * thrown in cases such as the GUI element object could not be instantiated |
---|
| 98 | * @throws IllegalArgumentException |
---|
| 99 | * if the provided path is invalid. |
---|
[545] | 100 | */ |
---|
[576] | 101 | public IGUIElement integratePath(List<? extends IGUIElementSpec> guiElementPath, |
---|
[820] | 102 | IGUIElementFactory guiElementFactory) |
---|
[746] | 103 | throws GUIModelException, IllegalArgumentException |
---|
[576] | 104 | { |
---|
[611] | 105 | if ((guiElementPath == null) || (guiElementPath.size() <= 0)) { |
---|
[820] | 106 | throw new IllegalArgumentException("GUI element path must contain at least one element"); |
---|
[611] | 107 | } |
---|
[820] | 108 | |
---|
[603] | 109 | List<IGUIElementSpec> remainingPath = new LinkedList<IGUIElementSpec>(); |
---|
[820] | 110 | |
---|
| 111 | for (IGUIElementSpec spec : guiElementPath) { |
---|
[576] | 112 | remainingPath.add(spec); |
---|
[545] | 113 | } |
---|
[820] | 114 | |
---|
[576] | 115 | return integratePath(root, remainingPath, guiElementFactory); |
---|
[545] | 116 | } |
---|
| 117 | |
---|
| 118 | /** |
---|
[746] | 119 | * <p> |
---|
[820] | 120 | * Returns all children of the provided GUI element or null, if it does not have any or the node |
---|
| 121 | * is unknown. |
---|
[746] | 122 | * </p> |
---|
| 123 | * |
---|
[820] | 124 | * @param guiElement |
---|
| 125 | * the GUI element of which the children shall be returned |
---|
| 126 | * |
---|
[746] | 127 | * @return As described |
---|
[545] | 128 | */ |
---|
[576] | 129 | public List<IGUIElement> getChildren(IGUIElement guiElement) { |
---|
[821] | 130 | List<IGUIElement> result = null; |
---|
[576] | 131 | for (TreeNode node : allNodes) { |
---|
| 132 | if (node.guiElement.equals(guiElement)) { |
---|
[821] | 133 | if (result == null) { |
---|
| 134 | result = new ArrayList<IGUIElement>(); |
---|
[820] | 135 | |
---|
[821] | 136 | if (node.children != null) { |
---|
| 137 | for (TreeNode child : node.children) { |
---|
| 138 | result.add(child.guiElement); |
---|
| 139 | } |
---|
[576] | 140 | } |
---|
| 141 | } |
---|
[821] | 142 | else { |
---|
| 143 | Console |
---|
| 144 | .traceln(Level.SEVERE, |
---|
| 145 | "Multiple nodes in the internal GUI model match the same GUI element. " |
---|
| 146 | + "This should not be the case and the GUI model is probably invalid."); |
---|
| 147 | } |
---|
[576] | 148 | } |
---|
| 149 | } |
---|
[820] | 150 | |
---|
[821] | 151 | return result; |
---|
[545] | 152 | } |
---|
| 153 | |
---|
| 154 | /** |
---|
[593] | 155 | * <p> |
---|
[746] | 156 | * Returns the parent GUI element of the provided GUI element or null, if it does not have a |
---|
| 157 | * parent (i.e. if it is a root node) or if the node is unknown. |
---|
[593] | 158 | * </p> |
---|
[746] | 159 | * |
---|
[820] | 160 | * @param guiElement |
---|
| 161 | * the GUI element of which the parent shall be returned |
---|
| 162 | * |
---|
[746] | 163 | * @return As described |
---|
[545] | 164 | */ |
---|
[593] | 165 | public IGUIElement getParent(IGUIElement guiElement) { |
---|
[821] | 166 | IGUIElement parent = null; |
---|
| 167 | |
---|
[593] | 168 | for (TreeNode node : allNodes) { |
---|
[1088] | 169 | if (node.children != null) { |
---|
| 170 | for (TreeNode child : node.children) { |
---|
| 171 | if (child.guiElement.equals(guiElement)) { |
---|
| 172 | if (parent != null) { |
---|
| 173 | parent = node.guiElement; |
---|
| 174 | } |
---|
| 175 | else { |
---|
| 176 | Console |
---|
[821] | 177 | .traceln(Level.SEVERE, |
---|
| 178 | "Multiple nodes in the internal GUI model match the same GUI element. " |
---|
[1088] | 179 | + "This should not be the case and the GUI model is probably invalid."); |
---|
| 180 | } |
---|
[821] | 181 | } |
---|
[593] | 182 | } |
---|
| 183 | } |
---|
| 184 | } |
---|
[820] | 185 | |
---|
[821] | 186 | return parent; |
---|
[593] | 187 | } |
---|
| 188 | |
---|
| 189 | /** |
---|
[746] | 190 | * <p> |
---|
| 191 | * Returns all root GUI elements of the model or an empty list, if the model is empty |
---|
| 192 | * </p> |
---|
[820] | 193 | * |
---|
[746] | 194 | * @return As described |
---|
[593] | 195 | */ |
---|
[576] | 196 | public List<IGUIElement> getRootElements() { |
---|
| 197 | List<IGUIElement> roots = new ArrayList<IGUIElement>(); |
---|
[820] | 198 | |
---|
[611] | 199 | if (root.children != null) { |
---|
| 200 | for (TreeNode rootChild : root.children) { |
---|
| 201 | roots.add(rootChild.guiElement); |
---|
| 202 | } |
---|
[545] | 203 | } |
---|
[820] | 204 | |
---|
[545] | 205 | return roots; |
---|
| 206 | } |
---|
[1086] | 207 | |
---|
| 208 | /** |
---|
| 209 | * returns a traverser for the GUI model to have efficient access to the tree of GUI elements |
---|
| 210 | * without having direct access. |
---|
| 211 | * |
---|
| 212 | * @return a traverser |
---|
| 213 | */ |
---|
| 214 | public Traverser getTraverser() { |
---|
| 215 | return new Traverser(); |
---|
| 216 | } |
---|
[545] | 217 | |
---|
| 218 | /** |
---|
[1088] | 219 | * returns a traverser for the GUI model starting at the given GUI element. Returns null, if |
---|
| 220 | * the GUI element is not part of the model. |
---|
| 221 | * |
---|
| 222 | * @return a traverser |
---|
| 223 | */ |
---|
| 224 | public Traverser getTraverser(IGUIElement startingAt) { |
---|
| 225 | TreeNode node = findNode(startingAt); |
---|
| 226 | |
---|
| 227 | if (node != null) { |
---|
| 228 | Traverser traverser = new Traverser(); |
---|
| 229 | traverser.navigateTo(node); |
---|
| 230 | return traverser; |
---|
| 231 | } |
---|
| 232 | else { |
---|
| 233 | return null; |
---|
| 234 | } |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | /** |
---|
[746] | 238 | * <p> |
---|
| 239 | * dumps the GUI model to the provided stream. Each node is represented through its toString() |
---|
| 240 | * method. If a node has children, those are dumped indented and surrounded by braces. |
---|
| 241 | * </p> |
---|
| 242 | * |
---|
[820] | 243 | * @param out |
---|
| 244 | * The stream to dump the textual representation of the model to |
---|
| 245 | * @param encoding |
---|
| 246 | * The encoding to be used while dumping |
---|
[643] | 247 | */ |
---|
[746] | 248 | public void dump(OutputStream out, String encoding) { |
---|
[643] | 249 | PrintStream stream; |
---|
[820] | 250 | |
---|
[643] | 251 | if (out instanceof PrintStream) { |
---|
| 252 | stream = (PrintStream) out; |
---|
| 253 | } |
---|
| 254 | else { |
---|
[746] | 255 | String enc = encoding == null ? "UTF-8" : encoding; |
---|
| 256 | try { |
---|
| 257 | stream = new PrintStream(out, true, enc); |
---|
| 258 | } |
---|
| 259 | catch (UnsupportedEncodingException e) { |
---|
[747] | 260 | throw new IllegalArgumentException("encodind " + enc + " not supported"); |
---|
[746] | 261 | } |
---|
[643] | 262 | } |
---|
[820] | 263 | |
---|
[1086] | 264 | for (TreeNode node : root.children) { |
---|
| 265 | dumpGUIElement(stream, node, ""); |
---|
[643] | 266 | } |
---|
| 267 | } |
---|
| 268 | |
---|
| 269 | /** |
---|
[576] | 270 | * <p> |
---|
[821] | 271 | * By calling this method, the GUIModel is traversed and similar nodes are merged. |
---|
[576] | 272 | * </p> |
---|
[820] | 273 | * |
---|
| 274 | */ |
---|
| 275 | public void condenseModel() { |
---|
[821] | 276 | mergeSubTree(root); |
---|
[820] | 277 | } |
---|
[846] | 278 | |
---|
| 279 | /** |
---|
| 280 | * <p> |
---|
| 281 | * Merges the tree nodes of two GUI elements. The GUI elements need to have the same parent. |
---|
| 282 | * </p> |
---|
| 283 | * |
---|
| 284 | * @param guiElement1 |
---|
| 285 | * the first merge GUI element |
---|
| 286 | * @param guiElement2 |
---|
| 287 | * the second merge GUI element |
---|
| 288 | * @throws IllegalArgumentException |
---|
| 289 | * thrown if the two GUI elements do not have the same parent |
---|
| 290 | */ |
---|
| 291 | public void mergeGUIElements(IGUIElement guiElement1, IGUIElement guiElement2) |
---|
| 292 | throws IllegalArgumentException |
---|
| 293 | { |
---|
| 294 | // check if both nodes have the same parent |
---|
| 295 | IGUIElement parentElement = guiElement1.getParent(); |
---|
| 296 | if (parentElement != null && !parentElement.equals(guiElement2.getParent())) { |
---|
| 297 | throw new IllegalArgumentException("can only merge nodes with the same parent"); |
---|
| 298 | } |
---|
[820] | 299 | |
---|
[846] | 300 | // get the TreeNode of the parent of the GUI elements |
---|
| 301 | TreeNode parent = findNode(parentElement); |
---|
| 302 | |
---|
| 303 | // get the TreeNodes for both GUI elements |
---|
| 304 | TreeNode node1 = findNode(guiElement1); |
---|
| 305 | TreeNode node2 = findNode(guiElement2); |
---|
| 306 | |
---|
| 307 | if (node1 == null || node2 == null) { |
---|
| 308 | throw new IllegalArgumentException( |
---|
| 309 | "Error while merging nodes: one element is not part of the GUI model!"); |
---|
| 310 | } |
---|
| 311 | |
---|
| 312 | TreeNode replacement = mergeTreeNodes(node1, node2); |
---|
| 313 | |
---|
| 314 | if (parent != null) { |
---|
| 315 | // remove node1 and node2 from the parent's children and add the replacement instead |
---|
| 316 | // assumes that there are no duplicates of node1 and node2 |
---|
| 317 | if (parent.children != null) { |
---|
| 318 | parent.children.set(parent.children.indexOf(node1), replacement); |
---|
| 319 | parent.children.remove(node2); |
---|
| 320 | } |
---|
| 321 | } |
---|
| 322 | |
---|
| 323 | } |
---|
| 324 | |
---|
[820] | 325 | /** |
---|
| 326 | * <p> |
---|
| 327 | * internally integrates a path as the children of the provided parent node. This method is |
---|
| 328 | * recursive and calls itself, for the child of the parent node, that matches the first element |
---|
| 329 | * in the remaining path. |
---|
| 330 | * </p> |
---|
| 331 | * |
---|
| 332 | * @param parentNode |
---|
| 333 | * the parent node to add children for |
---|
| 334 | * @param guiElementPath |
---|
| 335 | * the path of children to be created starting with the parent node |
---|
| 336 | * @param guiElementFactory |
---|
| 337 | * the GUI element factory to be used for instantiating GUI element objects |
---|
| 338 | * |
---|
[746] | 339 | * @return The GUI element object representing the GUI element denoted by the provided path |
---|
| 340 | * |
---|
[820] | 341 | * @throws GUIModelException |
---|
| 342 | * thrown in cases such as the GUI element object could not be instantiated |
---|
[545] | 343 | */ |
---|
[820] | 344 | private IGUIElement integratePath(TreeNode parentNode, |
---|
[576] | 345 | List<? extends IGUIElementSpec> remainingPath, |
---|
[820] | 346 | IGUIElementFactory guiElementFactory) |
---|
[576] | 347 | throws GUIModelException |
---|
| 348 | { |
---|
| 349 | IGUIElementSpec specToIntegrateElementFor = remainingPath.remove(0); |
---|
[611] | 350 | |
---|
[820] | 351 | TreeNode child = findEqualChild(parentNode, specToIntegrateElementFor); |
---|
[821] | 352 | if (child == null) { |
---|
[820] | 353 | IGUIElement newElement = |
---|
| 354 | guiElementFactory.instantiateGUIElement(specToIntegrateElementFor, |
---|
| 355 | parentNode.guiElement); |
---|
[611] | 356 | |
---|
[820] | 357 | child = parentNode.addChild(newElement); |
---|
[545] | 358 | } |
---|
[820] | 359 | |
---|
[576] | 360 | if (remainingPath.size() > 0) { |
---|
[820] | 361 | return integratePath(child, remainingPath, guiElementFactory); |
---|
[576] | 362 | } |
---|
| 363 | else { |
---|
[820] | 364 | return child.guiElement; |
---|
[576] | 365 | } |
---|
[545] | 366 | } |
---|
| 367 | |
---|
[576] | 368 | /** |
---|
| 369 | * <p> |
---|
[821] | 370 | * Searches the children of a tree node to see if the {@link IGUIElementSpec} of equals the |
---|
| 371 | * specification of the {@link TreeNode#guiElement} of the child. If a match is found, the child |
---|
| 372 | * is returned. |
---|
[611] | 373 | * </p> |
---|
[831] | 374 | * |
---|
| 375 | * @param parentNode |
---|
| 376 | * parent node whose children are searched |
---|
| 377 | * @param specToMatch |
---|
| 378 | * specification that is searched for |
---|
| 379 | * @return matching child node or null if no child matches |
---|
[611] | 380 | */ |
---|
[820] | 381 | private TreeNode findEqualChild(TreeNode parentNode, IGUIElementSpec specToMatch) { |
---|
[611] | 382 | if (parentNode.children != null) { |
---|
| 383 | for (TreeNode child : parentNode.children) { |
---|
[820] | 384 | if (specToMatch.equals(child.guiElement.getSpecification())) { |
---|
| 385 | return child; |
---|
[611] | 386 | } |
---|
| 387 | } |
---|
| 388 | } |
---|
[820] | 389 | return null; |
---|
[611] | 390 | } |
---|
| 391 | |
---|
| 392 | /** |
---|
| 393 | * <p> |
---|
[821] | 394 | * Merges all similar nodes in the sub-tree of the GUI model defined by the subTreeRoot. |
---|
[611] | 395 | * </p> |
---|
[821] | 396 | * <p> |
---|
| 397 | * The merging order is a bottom-up. This means, that we first call mergeSubTree recursively for |
---|
| 398 | * the grand children of the subTreeRoot, before we merge subTreeRoot. |
---|
| 399 | * </p> |
---|
| 400 | * <p> |
---|
| 401 | * The merging strategy is top-down. This means, that every time we merge two child nodes, we |
---|
| 402 | * call mergeSubTree recursively for all children of the merged nodes in order to check if we |
---|
| 403 | * can merge the children, too. |
---|
| 404 | * </p> |
---|
[831] | 405 | * |
---|
| 406 | * @param subTreeRoot |
---|
| 407 | * root node of the sub-tree that is merged |
---|
[611] | 408 | */ |
---|
[821] | 409 | private void mergeSubTree(TreeNode subTreeRoot) { |
---|
| 410 | if (subTreeRoot.children == null || subTreeRoot.children.isEmpty()) { |
---|
[820] | 411 | return; |
---|
[611] | 412 | } |
---|
[820] | 413 | |
---|
[821] | 414 | // lets first merge the grand children of the parentNode |
---|
| 415 | for (TreeNode child : subTreeRoot.children) { |
---|
| 416 | mergeSubTree(child); |
---|
[611] | 417 | } |
---|
| 418 | |
---|
| 419 | boolean performedMerge; |
---|
[820] | 420 | |
---|
[611] | 421 | do { |
---|
| 422 | performedMerge = false; |
---|
[821] | 423 | for (int i = 0; !performedMerge && i < subTreeRoot.children.size(); i++) { |
---|
[820] | 424 | IGUIElementSpec elemSpec1 = |
---|
[821] | 425 | subTreeRoot.children.get(i).guiElement.getSpecification(); |
---|
| 426 | for (int j = i + 1; !performedMerge && j < subTreeRoot.children.size(); j++) { |
---|
[820] | 427 | IGUIElementSpec elemSpec2 = |
---|
[821] | 428 | subTreeRoot.children.get(j).guiElement.getSpecification(); |
---|
[820] | 429 | if (elemSpec1.getSimilarity(elemSpec2)) { |
---|
| 430 | TreeNode replacement = |
---|
[821] | 431 | mergeTreeNodes(subTreeRoot.children.get(i), subTreeRoot.children.get(j)); |
---|
[820] | 432 | |
---|
[821] | 433 | subTreeRoot.children.set(i, replacement); |
---|
| 434 | subTreeRoot.children.remove(j); |
---|
[820] | 435 | performedMerge = true; |
---|
| 436 | i--; |
---|
| 437 | break; |
---|
[611] | 438 | } |
---|
| 439 | } |
---|
| 440 | } |
---|
| 441 | } |
---|
| 442 | while (performedMerge); |
---|
| 443 | } |
---|
| 444 | |
---|
| 445 | /** |
---|
| 446 | * <p> |
---|
[820] | 447 | * merges two nodes with each other. Merging means registering the GUI element objects with each |
---|
| 448 | * other for equality checks. Further it add all children of both nodes to a new replacing node. |
---|
| 449 | * Afterwards, all similar nodes of the replacement node are merged as well. |
---|
[611] | 450 | * </p> |
---|
[820] | 451 | * |
---|
[831] | 452 | * @param treeNode1 |
---|
| 453 | * the first of the two nodes to be merged |
---|
| 454 | * @param treeNode2 |
---|
| 455 | * the second of the two nodes to be merged |
---|
[746] | 456 | * @return a tree node being the merge of the two provided nodes. |
---|
[611] | 457 | */ |
---|
| 458 | private TreeNode mergeTreeNodes(TreeNode treeNode1, TreeNode treeNode2) { |
---|
| 459 | // the following two lines are needed to preserve the references to the existing GUI |
---|
| 460 | // elements. If two elements are the same, one should be deleted to make the elements |
---|
| 461 | // singletons again. However, there may exist references to both objects. To preserve |
---|
| 462 | // these, we simply register the equal GUI elements with each other so that an equals |
---|
| 463 | // check can return true. |
---|
[820] | 464 | treeNode1.guiElement.addEqualGUIElement(treeNode2.guiElement); |
---|
| 465 | treeNode2.guiElement.addEqualGUIElement(treeNode1.guiElement); |
---|
[1116] | 466 | |
---|
[820] | 467 | // and now a replacement node that is the merge of treeNode1 and treeNode2 is created |
---|
| 468 | TreeNode replacement = new TreeNode(); |
---|
| 469 | replacement.guiElement = treeNode1.guiElement; |
---|
[611] | 470 | if (treeNode1.children != null) { |
---|
| 471 | for (TreeNode child : treeNode1.children) { |
---|
[820] | 472 | replacement.addChildNode(child); |
---|
[611] | 473 | } |
---|
| 474 | } |
---|
| 475 | if (treeNode2.children != null) { |
---|
| 476 | for (TreeNode child : treeNode2.children) { |
---|
[820] | 477 | replacement.addChildNode(child); |
---|
[611] | 478 | } |
---|
| 479 | } |
---|
[820] | 480 | |
---|
[821] | 481 | mergeSubTree(replacement); |
---|
[820] | 482 | |
---|
[611] | 483 | replacement.guiElement.updateSpecification(treeNode2.guiElement.getSpecification()); |
---|
[820] | 484 | |
---|
| 485 | // finally, update the known nodes list |
---|
| 486 | // if you don't do this getChildren will return wrong things and very bad things happen! |
---|
| 487 | allNodes.remove(treeNode1); |
---|
| 488 | allNodes.remove(treeNode2); |
---|
| 489 | allNodes.add(replacement); |
---|
| 490 | |
---|
[611] | 491 | return replacement; |
---|
| 492 | } |
---|
| 493 | |
---|
| 494 | /** |
---|
[746] | 495 | * <p> |
---|
[820] | 496 | * dumps a GUI element to the stream. A dump contains the toString() representation of the GUI |
---|
[1086] | 497 | * element as well as a indented list of its children surrounded by braces. Therefore, not the |
---|
| 498 | * GUI element itself but its tree node is provided to have an efficient access to its children |
---|
[746] | 499 | * </p> |
---|
[831] | 500 | * |
---|
| 501 | * @param out |
---|
| 502 | * {@link PrintStream} where the guiElement is dumped to |
---|
[1086] | 503 | * @param node |
---|
| 504 | * the guiElement's tree node of which the string representation is dumped |
---|
[831] | 505 | * @param indent |
---|
| 506 | * indent string of the dumping |
---|
[643] | 507 | */ |
---|
[1086] | 508 | private void dumpGUIElement(PrintStream out, TreeNode node, String indent) { |
---|
[643] | 509 | out.print(indent); |
---|
[1086] | 510 | out.print(node.guiElement); |
---|
[643] | 511 | |
---|
[1086] | 512 | if ((node.children != null) && (node.children.size() > 0)) { |
---|
[643] | 513 | out.println(" {"); |
---|
| 514 | |
---|
[1086] | 515 | for (TreeNode child : node.children) { |
---|
[643] | 516 | dumpGUIElement(out, child, indent + " "); |
---|
| 517 | } |
---|
| 518 | |
---|
| 519 | out.print(indent); |
---|
| 520 | out.print("}"); |
---|
| 521 | } |
---|
| 522 | |
---|
| 523 | out.println(); |
---|
| 524 | } |
---|
[846] | 525 | |
---|
| 526 | /** |
---|
| 527 | * <p> |
---|
| 528 | * Retrieves the TreeNode associated with a GUI element. Returns null if no such TreeNode is |
---|
| 529 | * found. |
---|
| 530 | * </p> |
---|
| 531 | * |
---|
| 532 | * @param element |
---|
| 533 | * the GUI element |
---|
| 534 | * @return associated TreeNode; null if no such node exists |
---|
| 535 | */ |
---|
| 536 | private TreeNode findNode(IGUIElement element) { |
---|
| 537 | if (element == null) { |
---|
| 538 | return null; |
---|
| 539 | } |
---|
[643] | 540 | |
---|
[846] | 541 | TreeNode result = null; |
---|
| 542 | for (TreeNode node : allNodes) { |
---|
| 543 | if (node.guiElement.equals(element)) { |
---|
| 544 | if (result == null) { |
---|
| 545 | result = node; |
---|
| 546 | } |
---|
| 547 | else { |
---|
| 548 | Console |
---|
| 549 | .traceln(Level.SEVERE, |
---|
| 550 | "Multiple nodes in the internal GUI model match the same GUI element. " |
---|
| 551 | + "This should not be the case and the GUI model is probably invalid."); |
---|
| 552 | } |
---|
| 553 | } |
---|
| 554 | } |
---|
| 555 | return result; |
---|
| 556 | } |
---|
| 557 | |
---|
[643] | 558 | /** |
---|
[611] | 559 | * <p> |
---|
[1086] | 560 | * Used externally for tree traversal without providing direct access to the tree nodes |
---|
| 561 | * </p> |
---|
| 562 | * |
---|
| 563 | * @version 1.0 |
---|
| 564 | * @author Patrick Harms, Steffen Herbold |
---|
| 565 | */ |
---|
| 566 | public class Traverser { |
---|
| 567 | |
---|
| 568 | /** |
---|
| 569 | * <p> |
---|
| 570 | * the stack of nodes on which the traverser currently works |
---|
| 571 | * </p> |
---|
| 572 | */ |
---|
| 573 | private Stack<StackEntry> nodeStack = new Stack<StackEntry>(); |
---|
| 574 | |
---|
| 575 | /** |
---|
| 576 | * <p> |
---|
| 577 | * initializes the traverser by adding the root node of the GUI model to the stack |
---|
| 578 | * </p> |
---|
| 579 | */ |
---|
| 580 | private Traverser() { |
---|
| 581 | nodeStack.push(new StackEntry(root, 0)); |
---|
| 582 | } |
---|
| 583 | |
---|
| 584 | /** |
---|
| 585 | * <p> |
---|
| 586 | * returns the first child of the current GUI element. On the first call of this method on |
---|
| 587 | * the traverser the first of the root GUI elements of the GUI model is returned. If the |
---|
| 588 | * current GUI element does not have children, the method returns null. If the GUI model |
---|
| 589 | * is empty, then a call to this method will return null. The returned GUI element is the |
---|
| 590 | * next one the traverser points to. |
---|
| 591 | * </p> |
---|
| 592 | * |
---|
| 593 | * @return as described. |
---|
| 594 | */ |
---|
| 595 | public IGUIElement firstChild() { |
---|
| 596 | return pushChild(0); |
---|
| 597 | } |
---|
| 598 | |
---|
| 599 | /** |
---|
| 600 | * <p> |
---|
| 601 | * returns true, if the current GUI element has a first child, i.e. if the next call to the |
---|
| 602 | * method {@link #firstChild()} would return a GUI element or null. |
---|
| 603 | * </p> |
---|
| 604 | * |
---|
| 605 | * @return as described |
---|
| 606 | */ |
---|
| 607 | public boolean hasFirstChild() { |
---|
| 608 | return |
---|
| 609 | (nodeStack.peek().treeNode.children != null) && |
---|
| 610 | (nodeStack.peek().treeNode.children.size() > 0); |
---|
| 611 | } |
---|
| 612 | |
---|
| 613 | /** |
---|
| 614 | * <p> |
---|
| 615 | * returns the next sibling of the current GUI element. If there is no further sibling, |
---|
| 616 | * null is returned. If the current GUI element is one of the root nodes, the next root |
---|
| 617 | * node of the GUI model is returned. The returned GUI element is the next one the |
---|
| 618 | * traverser points to. |
---|
| 619 | * </p> |
---|
| 620 | * |
---|
| 621 | * @return as described |
---|
| 622 | */ |
---|
| 623 | public IGUIElement nextSibling() { |
---|
| 624 | int lastIndex = nodeStack.pop().index; |
---|
| 625 | |
---|
| 626 | IGUIElement retval = pushChild(lastIndex + 1); |
---|
| 627 | if (retval == null) { |
---|
| 628 | pushChild(lastIndex); |
---|
| 629 | } |
---|
| 630 | |
---|
| 631 | return retval; |
---|
| 632 | } |
---|
| 633 | |
---|
| 634 | /** |
---|
| 635 | * <p> |
---|
| 636 | * returns true, if the current GUI element has a further sibling, i.e. if a call to the |
---|
| 637 | * method {@link #nextSibling()} will return a GUI element; |
---|
| 638 | * </p> |
---|
| 639 | * |
---|
| 640 | * @return as described |
---|
| 641 | */ |
---|
| 642 | public boolean hasNextSibling() { |
---|
[1088] | 643 | boolean result = false; |
---|
| 644 | if (nodeStack.size() > 1) { |
---|
| 645 | StackEntry entry = nodeStack.pop(); |
---|
| 646 | result = nodeStack.peek().treeNode.children.size() > (entry.index + 1); |
---|
| 647 | pushChild(entry.index); |
---|
| 648 | } |
---|
| 649 | |
---|
[1086] | 650 | return result; |
---|
| 651 | } |
---|
| 652 | |
---|
| 653 | /** |
---|
| 654 | * <p> |
---|
| 655 | * returns the parent GUI element of the current GUI element. If the current GUI element |
---|
| 656 | * is a root node, null is returned. If there is no current GUI element yet as the method |
---|
| 657 | * {@link #firstChild()} was not called yet, null is returned. |
---|
| 658 | * </p> |
---|
| 659 | * |
---|
| 660 | * @return as described |
---|
| 661 | */ |
---|
| 662 | public IGUIElement parent() { |
---|
| 663 | IGUIElement retval = null; |
---|
| 664 | |
---|
| 665 | if (nodeStack.size() > 1) { |
---|
| 666 | nodeStack.pop(); |
---|
| 667 | retval = nodeStack.peek().treeNode.guiElement; |
---|
| 668 | } |
---|
| 669 | |
---|
| 670 | return retval; |
---|
| 671 | } |
---|
| 672 | |
---|
| 673 | /** |
---|
| 674 | * <p> |
---|
| 675 | * internal method used for changing the state of the traverser. I.e. to switch to a |
---|
| 676 | * specific child GUI element of the current one. |
---|
| 677 | * </p> |
---|
| 678 | */ |
---|
| 679 | private IGUIElement pushChild(int index) { |
---|
| 680 | IGUIElement retVal = null; |
---|
| 681 | |
---|
| 682 | if ((nodeStack.peek().treeNode.children != null) && |
---|
| 683 | (nodeStack.peek().treeNode.children.size() > index)) |
---|
| 684 | { |
---|
| 685 | nodeStack.push |
---|
| 686 | (new StackEntry(nodeStack.peek().treeNode.children.get(index), index)); |
---|
| 687 | retVal = nodeStack.peek().treeNode.guiElement; |
---|
| 688 | } |
---|
| 689 | |
---|
| 690 | return retVal; |
---|
| 691 | } |
---|
| 692 | |
---|
| 693 | /** |
---|
| 694 | * <p> |
---|
[1088] | 695 | * navigates the traverser to the given node in the GUI model |
---|
| 696 | * </p> |
---|
| 697 | */ |
---|
| 698 | private boolean navigateTo(TreeNode node) { |
---|
| 699 | if (hasFirstChild()) { |
---|
| 700 | IGUIElement childElement = firstChild(); |
---|
| 701 | |
---|
| 702 | while (childElement != null) { |
---|
| 703 | if (childElement.equals(node.guiElement)) { |
---|
| 704 | return true; |
---|
| 705 | } |
---|
| 706 | else if (navigateTo(node)) { |
---|
| 707 | return true; |
---|
| 708 | } |
---|
| 709 | else { |
---|
| 710 | childElement = nextSibling(); |
---|
| 711 | } |
---|
| 712 | } |
---|
| 713 | |
---|
| 714 | parent(); |
---|
| 715 | } |
---|
| 716 | |
---|
| 717 | return false; |
---|
| 718 | } |
---|
| 719 | |
---|
| 720 | /** |
---|
| 721 | * <p> |
---|
[1086] | 722 | * internal class needed to fill the stack with nodes of the GUI models and their |
---|
| 723 | * respective index in the children of the parent node. |
---|
| 724 | * </p> |
---|
| 725 | */ |
---|
| 726 | private class StackEntry { |
---|
| 727 | |
---|
| 728 | /** */ |
---|
| 729 | private TreeNode treeNode; |
---|
| 730 | |
---|
| 731 | /** */ |
---|
| 732 | private int index; |
---|
| 733 | |
---|
| 734 | /** |
---|
| 735 | * <p> |
---|
| 736 | * creates a new stack entry. |
---|
| 737 | * </p> |
---|
| 738 | */ |
---|
| 739 | private StackEntry(TreeNode treeNode, int index) { |
---|
| 740 | this.treeNode = treeNode; |
---|
| 741 | this.index = index; |
---|
| 742 | } |
---|
| 743 | } |
---|
| 744 | } |
---|
| 745 | |
---|
| 746 | /** |
---|
| 747 | * <p> |
---|
[831] | 748 | * Used internally for building up the tree of GUI elements. |
---|
[576] | 749 | * </p> |
---|
[831] | 750 | * |
---|
| 751 | * @version 1.0 |
---|
| 752 | * @author Patrick Harms, Steffen Herbold |
---|
[576] | 753 | */ |
---|
[1116] | 754 | private class TreeNode implements Serializable { |
---|
[831] | 755 | |
---|
[1116] | 756 | /** */ |
---|
| 757 | private static final long serialVersionUID = 1L; |
---|
| 758 | |
---|
[831] | 759 | /** |
---|
| 760 | * <p> |
---|
| 761 | * GUI element associated with the TreeNode. |
---|
| 762 | * </p> |
---|
| 763 | */ |
---|
[576] | 764 | private IGUIElement guiElement; |
---|
[820] | 765 | |
---|
[831] | 766 | /** |
---|
| 767 | * <p> |
---|
| 768 | * Children of the TreeNode. |
---|
| 769 | * </p> |
---|
| 770 | */ |
---|
[576] | 771 | private List<TreeNode> children; |
---|
[820] | 772 | |
---|
[576] | 773 | /** |
---|
| 774 | * <p> |
---|
[831] | 775 | * Adds a child to the current node while keeping all lists of nodes up to date |
---|
[576] | 776 | * </p> |
---|
[831] | 777 | * |
---|
| 778 | * @param guiElement |
---|
| 779 | * GUI element that will be associated with the new child |
---|
| 780 | * @return the added child |
---|
[576] | 781 | */ |
---|
[820] | 782 | private TreeNode addChild(IGUIElement guiElement) { |
---|
| 783 | if (children == null) { |
---|
[576] | 784 | children = new ArrayList<TreeNode>(); |
---|
| 785 | } |
---|
[820] | 786 | |
---|
[576] | 787 | TreeNode child = new TreeNode(); |
---|
| 788 | child.guiElement = guiElement; |
---|
| 789 | children.add(child); |
---|
[820] | 790 | |
---|
[576] | 791 | allNodes.add(child); |
---|
[820] | 792 | |
---|
[576] | 793 | return child; |
---|
| 794 | } |
---|
[709] | 795 | |
---|
[820] | 796 | /** |
---|
| 797 | * |
---|
| 798 | * <p> |
---|
[821] | 799 | * Adds a TreeNode as child to the current node. This way, the whole sub-tree is added. |
---|
[820] | 800 | * </p> |
---|
[831] | 801 | * |
---|
| 802 | * @param node |
---|
| 803 | * child node that is added |
---|
| 804 | * @return node that has been added |
---|
[820] | 805 | */ |
---|
| 806 | private TreeNode addChildNode(TreeNode node) { |
---|
| 807 | if (children == null) { |
---|
| 808 | children = new ArrayList<TreeNode>(); |
---|
| 809 | } |
---|
| 810 | children.add(node); |
---|
| 811 | return node; |
---|
| 812 | } |
---|
| 813 | |
---|
| 814 | /* |
---|
| 815 | * (non-Javadoc) |
---|
| 816 | * |
---|
[709] | 817 | * @see java.lang.Object#toString() |
---|
| 818 | */ |
---|
| 819 | @Override |
---|
| 820 | public String toString() { |
---|
| 821 | return guiElement.toString(); |
---|
| 822 | } |
---|
[576] | 823 | } |
---|
[545] | 824 | } |
---|