source: trunk/EventBenchConsole/src/de/ugoe/cs/eventbench/windows/data/WindowTree.java @ 230

Last change on this file since 230 was 229, checked in by sherbold, 13 years ago
File size: 4.6 KB
Line 
1package de.ugoe.cs.eventbench.windows.data;
2
3import java.util.HashMap;
4import java.util.List;
5import java.util.Map;
6import java.util.SortedSet;
7
8/**
9 * <p>
10 * This class provides an the interfaces for window trees.
11 * </p>
12 * <p>
13 * The window tree represents the hierarchical structure of the windows
14 * "as it is" currently during a session. It may change during the session due
15 * to creation and destruction of windows.
16 * </p>
17 * <p>
18 * The class is implemented as a singleton. The rational behind implementing
19 * this class as a singleton is to ease the access of all class that may request
20 * information about the windows during the parsing of a session. As the tree
21 * may change during the session, it does not make sense to preserve it after a
22 * session. Thus, it can just be deleted. Therefore, as long as only one session
23 * is parsed at a time, a single instance is sufficient.
24 * </p>
25 *
26 * @author Steffen Herbold
27 * @version 1.0
28 */
29public class WindowTree {
30
31        /**
32         * <p>
33         * Handle to the window instance.
34         * </p>
35         */
36        private static WindowTree theInstance = null;
37
38        /**
39         * <p>
40         * Maintains a set of all the target strings of all widgets that were at
41         * some point part of the window tree.
42         * </p>
43         */
44        private SortedSet<String> targets;
45
46        /**
47         * <p>
48         * Obtain a handle to the window instance.
49         * </p>
50         *
51         * @return instance of the window tree
52         */
53        public static WindowTree getInstance() {
54                if (theInstance == null) {
55                        theInstance = new WindowTree();
56                }
57                return theInstance;
58        }
59
60        /**
61         * <p>
62         * Resets the tree. Should be used between sessions.
63         * </p>
64         */
65        public static void resetTree() {
66                theInstance = null;
67        }
68
69        /**
70         * <p>
71         * Map of all windows that are part of the tree for efficient searching. The
72         * keys of the map are the hwnd's of the windows.
73         * </p>
74         */
75        private Map<Integer, WindowTreeNode> nodes;
76
77        /**
78         * <p>
79         * Creates a new WindowTree.
80         * </p>
81         * <p>
82         * Private, as the class is a singleton.
83         * </p>
84         */
85        private WindowTree() {
86                nodes = new HashMap<Integer, WindowTreeNode>();
87        }
88
89        /**
90         * <p>
91         * Adds a new window to the tree.
92         * </p>
93         *
94         * @param parentHwnd
95         *            hwnd of the parent window
96         * @param childHwnd
97         *            hwnd of the window to be created
98         * @param childWindowName
99         *            resource id of the window to be created
100         * @param resourceId
101         *            resource id of the window to be created
102         * @param className
103         *            class name of the window to be created
104         */
105        public void add(int parentHwnd, int childHwnd, String childWindowName,
106                        int resourceId, String className, boolean isModal) {
107                WindowTreeNode parent = nodes.get(parentHwnd);
108                WindowTreeNode child = nodes.get(childHwnd);
109                if (child == null) {
110                        if (parent != null) {
111                                child = parent.addChild(childHwnd, childWindowName, resourceId,
112                                                className, isModal);
113                        } else {
114                                child = new WindowTreeNode(childHwnd, null, childWindowName,
115                                                resourceId, className, isModal);
116                        }
117                        nodes.put(childHwnd, child);
118                        targets.add(child.xmlRepresentation());
119                }
120        }
121
122        /**
123         * <p>
124         * Removes a window (defined by its hwnd) from the tree. All children of the
125         * window will be removed recursively.
126         * </p>
127         *
128         * @param hwnd
129         *            hwnd of the window to be removed
130         * @return number of windows that were removed
131         */
132        public int remove(int hwnd) {
133                int removedCounter = 0;
134                WindowTreeNode node = nodes.get(hwnd);
135                if (node != null) {
136                        List<WindowTreeNode> nodesToBeRemoved = node.remove();
137                        for (int i = 0; i < nodesToBeRemoved.size(); i++) {
138                                WindowTreeNode nodeToBeRemoved = nodesToBeRemoved.get(i);
139                                nodesToBeRemoved.addAll(nodeToBeRemoved.getChildren());
140                                nodes.remove(nodeToBeRemoved.getHwnd());
141                                removedCounter++;
142                        }
143                        nodes.remove(hwnd);
144                        removedCounter++;
145                }
146                return removedCounter;
147        }
148
149        /**
150         * <p>
151         * Searches the tree for a window with the specified hwnd and returns its
152         * {@link WindowTreeNode}.
153         * </p>
154         *
155         * @param hwnd
156         *            hwnd that is looked for
157         * @return {@link WindowTreeNode} of the window with the given hwnd if
158         *         found, null otherwise
159         */
160        public WindowTreeNode find(int hwnd) {
161                return nodes.get(hwnd);
162        }
163
164        /**
165         * <p>
166         * Returns the number of nodes contained in the WindowTree.
167         * </p>
168         *
169         * @return number of nodes
170         */
171        public int size() {
172                return nodes.size();
173        }
174
175        /**
176         * <p>
177         * Returns a sorted set of all targets that existed any time in the window
178         * tree.
179         * </p>
180         *
181         * @return set of targets
182         */
183        public SortedSet<String> getTargets() {
184                return targets;
185        }
186}
Note: See TracBrowser for help on using the repository browser.