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

Last change on this file since 219 was 215, checked in by jhall, 13 years ago

Changed the type of "ListTargets?" from List<String> to SortedSet?<String>.

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