[963] | 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.plugin.jfc.guimodel;
|
---|
| 16 |
|
---|
| 17 | import java.util.ArrayList;
|
---|
| 18 | import java.util.HashMap;
|
---|
| 19 | import java.util.List;
|
---|
| 20 | import java.util.Map;
|
---|
| 21 |
|
---|
| 22 | import de.ugoe.cs.autoquest.eventcore.guimodel.GUIElementFactory;
|
---|
| 23 | import de.ugoe.cs.autoquest.eventcore.guimodel.GUIModel;
|
---|
| 24 | import de.ugoe.cs.autoquest.eventcore.guimodel.GUIModelException;
|
---|
| 25 | import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElementFactory;
|
---|
| 26 |
|
---|
| 27 | /**
|
---|
| 28 | * <p>
|
---|
[1003] | 29 | * @deprecated Use GUIElementTree instead, which provides a generalization.
|
---|
[963] | 30 | * </p>
|
---|
| 31 | * <p>
|
---|
[1003] | 32 | * This class provides the interfaces for component trees.
|
---|
| 33 | * </p>
|
---|
| 34 | * <p>
|
---|
[963] | 35 | * The component tree represents the hierarchical structure of the components "as it is" currently during
|
---|
| 36 | * a session. It may change during the session due to creation and destruction of components.
|
---|
| 37 | * </p>
|
---|
| 38 | *
|
---|
| 39 | * @author Steffen Herbold
|
---|
| 40 | * @author Fabian Glaser
|
---|
| 41 | * @version 1.0
|
---|
| 42 | */
|
---|
| 43 | public class JFCComponentTree {
|
---|
| 44 |
|
---|
| 45 | /**
|
---|
| 46 | * <p>
|
---|
| 47 | * Map of all GUI element specifications that are part of the tree for efficient searching. The
|
---|
| 48 | * keys of the map are the hash values of the GUI elements.
|
---|
| 49 | * </p>
|
---|
| 50 | */
|
---|
[965] | 51 | private Map<Integer, JFCGUIElementSpec> guiElementSpecs;
|
---|
[963] | 52 |
|
---|
| 53 | /**
|
---|
| 54 | * <p>
|
---|
| 55 | * Map of all children of GUI elements that are part of the tree. The keys of the map are the
|
---|
| 56 | * hash values of the parent GUI elements.
|
---|
| 57 | * </p>
|
---|
| 58 | */
|
---|
[965] | 59 | private Map<Integer, List<JFCGUIElementSpec>> childRelations;
|
---|
[963] | 60 |
|
---|
| 61 | /**
|
---|
| 62 | * <p>
|
---|
| 63 | * Map of all parents of GUI elements that are part of the tree. The keys of the map are the
|
---|
| 64 | * hash values of the child GUI elements.
|
---|
| 65 | * </p>
|
---|
| 66 | */
|
---|
[965] | 67 | private Map<Integer, JFCGUIElementSpec> parentRelations;
|
---|
[963] | 68 |
|
---|
| 69 | /**
|
---|
| 70 | * <p>
|
---|
| 71 | * the internally created GUI model
|
---|
| 72 | * </p>
|
---|
| 73 | */
|
---|
[984] | 74 | private GUIModel guiModel;
|
---|
[963] | 75 |
|
---|
| 76 | /**
|
---|
| 77 | * <p>
|
---|
| 78 | * the GUI element factory used in the model
|
---|
| 79 | * </p>
|
---|
| 80 | */
|
---|
| 81 | private IGUIElementFactory guiElementFactory = GUIElementFactory.getInstance();
|
---|
| 82 |
|
---|
| 83 | /**
|
---|
| 84 | * <p>
|
---|
| 85 | * Map of all GUI elements that are part of the tree for efficient searching. The keys of the
|
---|
| 86 | * map are the hash values of the GUI elements.
|
---|
| 87 | * </p>
|
---|
| 88 | */
|
---|
[965] | 89 | private Map<Integer, JFCGUIElement> guiElements;
|
---|
[963] | 90 |
|
---|
| 91 | /**
|
---|
| 92 | * <p>
|
---|
| 93 | * Creates a new JFCComponentTree.
|
---|
| 94 | * </p>
|
---|
| 95 | */
|
---|
| 96 | public JFCComponentTree() {
|
---|
[965] | 97 | guiElementSpecs = new HashMap<Integer, JFCGUIElementSpec>();
|
---|
| 98 | childRelations = new HashMap<Integer, List<JFCGUIElementSpec>>();
|
---|
| 99 | parentRelations = new HashMap<Integer, JFCGUIElementSpec>();
|
---|
| 100 | guiElements = new HashMap<Integer, JFCGUIElement>();
|
---|
[984] | 101 | guiModel = new GUIModel();
|
---|
[963] | 102 | }
|
---|
[984] | 103 |
|
---|
| 104 | /**
|
---|
| 105 | * <p>
|
---|
| 106 | * Creates a JFCComponentTree with an already existing guiModel
|
---|
| 107 | * @param guiModel
|
---|
| 108 | * </p>
|
---|
| 109 | */
|
---|
| 110 | public JFCComponentTree(GUIModel guiModel){
|
---|
| 111 | guiElementSpecs = new HashMap<Integer, JFCGUIElementSpec>();
|
---|
| 112 | childRelations = new HashMap<Integer, List<JFCGUIElementSpec>>();
|
---|
| 113 | parentRelations = new HashMap<Integer, JFCGUIElementSpec>();
|
---|
| 114 | guiElements = new HashMap<Integer, JFCGUIElement>();
|
---|
| 115 | this.guiModel = guiModel;
|
---|
| 116 | }
|
---|
[963] | 117 |
|
---|
| 118 | /**
|
---|
| 119 | * <p>
|
---|
| 120 | * Adds a new component to the tree.
|
---|
| 121 | * </p>
|
---|
| 122 | *
|
---|
[965] | 123 | * @param componentHash
|
---|
| 124 | * hash of the window to be created
|
---|
[963] | 125 | * @param parentHash
|
---|
| 126 | * hash of the parent window
|
---|
| 127 | * @param componentSpec
|
---|
| 128 | * the component specification
|
---|
| 129 | */
|
---|
[965] | 130 | public void add(Integer componentHash,
|
---|
| 131 | Integer parentHash,
|
---|
[963] | 132 | JFCGUIElementSpec componentSpec)
|
---|
| 133 | {
|
---|
| 134 | JFCGUIElement guiElement = guiElements.get(componentHash);
|
---|
| 135 |
|
---|
| 136 | if (guiElement == null) {
|
---|
| 137 | JFCGUIElementSpec parent = guiElementSpecs.get(parentHash);
|
---|
| 138 | if (parent != null) {
|
---|
| 139 | List<JFCGUIElementSpec> otherChildren = childRelations.get(parentHash);
|
---|
| 140 |
|
---|
| 141 | if (otherChildren == null) {
|
---|
| 142 | otherChildren = new ArrayList<JFCGUIElementSpec>();
|
---|
| 143 | childRelations.put(parentHash, otherChildren);
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | otherChildren.add(componentSpec);
|
---|
| 147 |
|
---|
| 148 | parentRelations.put(componentHash, parent);
|
---|
| 149 | }
|
---|
| 150 | guiElementSpecs.put(componentHash, componentSpec);
|
---|
| 151 |
|
---|
| 152 | List<JFCGUIElementSpec> guiElementPath = new ArrayList<JFCGUIElementSpec>();
|
---|
| 153 |
|
---|
| 154 | while (componentSpec != null) {
|
---|
| 155 | guiElementPath.add(0, componentSpec);
|
---|
| 156 | componentSpec = parentRelations.get(componentSpec.getElementHash());
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | try {
|
---|
| 160 | guiElement =
|
---|
| 161 | (JFCGUIElement) guiModel.integratePath(guiElementPath, guiElementFactory);
|
---|
| 162 | }
|
---|
| 163 | catch (GUIModelException e) {
|
---|
| 164 | throw new RuntimeException("could not instantiate GUI element with id " + componentHash, e);
|
---|
| 165 | }
|
---|
| 166 | guiElements.put(componentHash, guiElement);
|
---|
| 167 | }
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | /**
|
---|
| 171 | * <p>
|
---|
| 172 | * Searches the tree for a component with the specified hash and returns its
|
---|
| 173 | * {@link JFCGUIElementSpec} .
|
---|
| 174 | * </p>
|
---|
| 175 | *
|
---|
| 176 | * @param hash
|
---|
| 177 | * hash that is looked for
|
---|
| 178 | * @return {@link JFCGUIElementSpec} of the window with the given hash if found, null otherwise
|
---|
| 179 | */
|
---|
[965] | 180 | public JFCGUIElement find(int hash) {
|
---|
[963] | 181 | JFCGUIElement guiElement = guiElements.get(hash);
|
---|
| 182 | if (guiElement == null) {
|
---|
| 183 | List<JFCGUIElementSpec> guiElementPath = new ArrayList<JFCGUIElementSpec>();
|
---|
| 184 |
|
---|
| 185 | JFCGUIElementSpec child = guiElementSpecs.get(hash);
|
---|
| 186 |
|
---|
| 187 | if (child == null) {
|
---|
| 188 | throw new RuntimeException("no GUI element found with hash " + hash);
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | while (child != null) {
|
---|
| 192 | guiElementPath.add(0, child);
|
---|
| 193 | child = parentRelations.get(child.getElementHash());
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | try {
|
---|
| 197 | guiElement =
|
---|
| 198 | (JFCGUIElement) guiModel.integratePath(guiElementPath, guiElementFactory);
|
---|
| 199 | }
|
---|
| 200 | catch (GUIModelException e) {
|
---|
| 201 | throw new RuntimeException("could not instantiate GUI element with id " + hash, e);
|
---|
| 202 | }
|
---|
| 203 | guiElements.put(hash, guiElement);
|
---|
| 204 | }
|
---|
| 205 | return guiElement;
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | /**
|
---|
| 209 | * <p>
|
---|
| 210 | * Sets the name of a GUI element given its hash.
|
---|
| 211 | * </p>
|
---|
| 212 | *
|
---|
| 213 | * @param hash
|
---|
| 214 | * hash of the GUI element
|
---|
| 215 | * @param windowName
|
---|
| 216 | * new name of the GUI element
|
---|
| 217 | */
|
---|
[965] | 218 | public void setName(int hash, String windowName) {
|
---|
[963] | 219 | JFCGUIElementSpec child = guiElementSpecs.get(hash);
|
---|
| 220 | if (child != null) {
|
---|
| 221 | child.setName(windowName);
|
---|
| 222 |
|
---|
| 223 | JFCGUIElement guiElement = guiElements.remove(hash);
|
---|
| 224 | if (guiElement == null) {
|
---|
| 225 | // we need to update the GUI model as well
|
---|
| 226 | find(hash);
|
---|
| 227 | }
|
---|
| 228 | }
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | /**
|
---|
| 232 | * <p>
|
---|
| 233 | * Removes a window (defined by its hash) from the tree. All children of the window will be
|
---|
| 234 | * removed recursively.
|
---|
| 235 | * </p>
|
---|
| 236 | *
|
---|
| 237 | * @param hash
|
---|
| 238 | * hash of the window to be removed
|
---|
| 239 | * @return number of windows that were removed
|
---|
| 240 | */
|
---|
| 241 | public int remove(long hash) {
|
---|
| 242 | JFCGUIElementSpec node = guiElementSpecs.remove(hash);
|
---|
| 243 | int removedCounter = 1;
|
---|
| 244 |
|
---|
| 245 | if (node != null) {
|
---|
| 246 | List<JFCGUIElementSpec> nodesToBeRemoved = childRelations.remove(hash);
|
---|
| 247 |
|
---|
| 248 | // remove all children and sub-children, if any
|
---|
| 249 | if (nodesToBeRemoved != null) {
|
---|
| 250 | for (int i = 0; i < nodesToBeRemoved.size(); i++) {
|
---|
| 251 | JFCGUIElementSpec nodeToBeRemoved = nodesToBeRemoved.get(i);
|
---|
| 252 | List<JFCGUIElementSpec> children =
|
---|
| 253 | childRelations.remove(nodeToBeRemoved.getElementHash());
|
---|
| 254 |
|
---|
| 255 | if (children != null) {
|
---|
| 256 | nodesToBeRemoved.addAll(children);
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 | guiElementSpecs.remove(nodeToBeRemoved.getElementHash());
|
---|
| 260 | parentRelations.remove(nodeToBeRemoved.getElementHash());
|
---|
| 261 | removedCounter++;
|
---|
| 262 | }
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | // the node may be a child node of a parent. So search for it and remove it
|
---|
| 266 | JFCGUIElementSpec parent = parentRelations.remove(hash);
|
---|
| 267 | if (parent != null) {
|
---|
| 268 | List<JFCGUIElementSpec> children = childRelations.get(parent.getElementHash());
|
---|
| 269 |
|
---|
| 270 | if (children != null) {
|
---|
| 271 | for (int i = 0; i < children.size(); i++) {
|
---|
| 272 | if (children.get(i).getElementHash() == hash) {
|
---|
| 273 | children.remove(i);
|
---|
| 274 | break;
|
---|
| 275 | }
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | if (children.size() <= 0) {
|
---|
| 279 | childRelations.remove(parent.getElementHash());
|
---|
| 280 | }
|
---|
| 281 | }
|
---|
| 282 | }
|
---|
| 283 | }
|
---|
| 284 | return removedCounter;
|
---|
| 285 | }
|
---|
| 286 |
|
---|
| 287 | /**
|
---|
| 288 | * @return the guiModel
|
---|
| 289 | */
|
---|
| 290 | public GUIModel getGUIModel() {
|
---|
| 291 | return guiModel;
|
---|
| 292 | }
|
---|
| 293 |
|
---|
| 294 | /**
|
---|
| 295 | * <p>
|
---|
| 296 | * Returns the number of nodes contained in the JFCComponentTree.
|
---|
| 297 | * </p>
|
---|
| 298 | *
|
---|
| 299 | * @return number of nodes
|
---|
| 300 | */
|
---|
| 301 | public int size() {
|
---|
| 302 | return guiElementSpecs.size();
|
---|
| 303 | }
|
---|
| 304 |
|
---|
| 305 | }
|
---|