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

Last change on this file since 171 was 171, checked in by sherbold, 13 years ago
  • code documentation and formatting
File size: 4.9 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        @SuppressWarnings("unchecked")
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                // store targets in GlobalDataContainer to be able to work on with them
117                // in DlgInsert
118                List<String> treeTargets = new ArrayList<String>();
119
120                if (GlobalDataContainer.getInstance().getData("ListTargets") == null) {
121                        GlobalDataContainer.getInstance().addData("ListTargets",
122                                        treeTargets);
123                }
124
125                try {
126                        treeTargets = (List<String>) GlobalDataContainer.getInstance()
127                                        .getData("ListTargets");
128                } catch (ClassCastException e) {
129                        Console.println("Not able to cast data in GlobalDataContainer to List of Strings");
130                }
131
132                treeTargets.add(nodes.get(childHwnd).xmlRepresentation());
133        }
134
135        /**
136         * <p>
137         * Removes a window (defined by its hwnd) from the tree. All children of the
138         * window will be removed recursively.
139         * </p>
140         *
141         * @param hwnd
142         *            hwnd of the window to be removed
143         * @return number of windows that were removed
144         */
145        public int remove(int hwnd) {
146                int removedCounter = 0;
147                WindowTreeNode node = nodes.get(hwnd);
148                if (node != null) {
149                        List<WindowTreeNode> nodesToBeRemoved = node.remove();
150                        for (int i = 0; i < nodesToBeRemoved.size(); i++) {
151                                WindowTreeNode nodeToBeRemoved = nodesToBeRemoved.get(i);
152                                nodesToBeRemoved.addAll(nodeToBeRemoved.getChildren());
153                                nodes.remove(nodeToBeRemoved.getHwnd());
154                                removedCounter++;
155                        }
156                        nodes.remove(hwnd);
157                        removedCounter++;
158                }
159                return removedCounter;
160        }
161
162        /**
163         * <p>
164         * Searches the tree for a window with the specified hwnd and returns its
165         * {@link WindowTreeNode}.
166         * </p>
167         *
168         * @param hwnd
169         *            hwnd that is looked for
170         * @return {@link WindowTreeNode} of the window with the given hwnd if
171         *         found, null otherwise
172         */
173        public WindowTreeNode find(int hwnd) {
174                return nodes.get(hwnd);
175        }
176
177        /**
178         * <p>
179         * Returns the number of nodes contained in the WindowTree.
180         * </p>
181         *
182         * @return number of nodes
183         */
184        public int size() {
185                return nodes.size();
186        }
187}
Note: See TracBrowser for help on using the repository browser.