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

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