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