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

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

Some refactoring done: new method to store a target in globalDataContainer for later use -> private void storeTarget(int hwnd)

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