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

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