1 | package de.ugoe.cs.eventbench.windows.data;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 | import java.util.HashMap;
|
---|
5 | import java.util.List;
|
---|
6 | import java.util.Map;
|
---|
7 |
|
---|
8 | import de.ugoe.cs.eventbench.data.GlobalDataContainer;
|
---|
9 | import 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 | */
|
---|
31 | public class WindowTree {
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * <p>
|
---|
35 | * Handle to the window instance.
|
---|
36 | * </p>
|
---|
37 | */
|
---|
38 | private static WindowTree theInstance = null;
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * <p>
|
---|
42 | * Obtain a handle to the window instance.
|
---|
43 | * </p>
|
---|
44 | *
|
---|
45 | * @return
|
---|
46 | */
|
---|
47 | public static WindowTree getInstance() {
|
---|
48 | if (theInstance == null) {
|
---|
49 | theInstance = new WindowTree();
|
---|
50 | }
|
---|
51 | return theInstance;
|
---|
52 | }
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * <p>
|
---|
56 | * Resets the tree. Should be used between sessions.
|
---|
57 | * </p>
|
---|
58 | */
|
---|
59 | public static void resetTree() {
|
---|
60 | theInstance = null;
|
---|
61 | }
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * <p>
|
---|
65 | * Map of all windows that are part of the tree for efficient searching. The
|
---|
66 | * keys of the map are the hwnd's of the windows.
|
---|
67 | * </p>
|
---|
68 | */
|
---|
69 | private Map<Integer, WindowTreeNode> nodes;
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * <p>
|
---|
73 | * Creates a new WindowTree.
|
---|
74 | * </p>
|
---|
75 | * <p>
|
---|
76 | * Private, as the class is a singleton.
|
---|
77 | * </p>
|
---|
78 | */
|
---|
79 | private WindowTree() {
|
---|
80 | nodes = new HashMap<Integer, WindowTreeNode>();
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * <p>
|
---|
85 | * Adds a new window to the tree.
|
---|
86 | * </p>
|
---|
87 | *
|
---|
88 | * @param parentHwnd
|
---|
89 | * hwnd of the parent window
|
---|
90 | * @param childHwnd
|
---|
91 | * hwnd of the window to be created
|
---|
92 | * @param childWindowName
|
---|
93 | * resource id of the window to be created
|
---|
94 | * @param resourceId
|
---|
95 | * resource id of the window to be created
|
---|
96 | * @param className
|
---|
97 | * class name of the window to be created
|
---|
98 | */
|
---|
99 | @SuppressWarnings("unchecked")
|
---|
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 | // store targets in GlobalDataContainer to be able to work on with them
|
---|
116 | // in DlgInsert
|
---|
117 | List<String> treeTargets = new ArrayList<String>();
|
---|
118 |
|
---|
119 | if (GlobalDataContainer.getInstance().getData("ListTargets") == null) {
|
---|
120 | GlobalDataContainer.getInstance().addData("ListTargets",
|
---|
121 | treeTargets);
|
---|
122 | }
|
---|
123 |
|
---|
124 | try {
|
---|
125 | treeTargets = (List<String>) GlobalDataContainer.getInstance()
|
---|
126 | .getData("ListTargets");
|
---|
127 | } catch (ClassCastException e) {
|
---|
128 | Console.println("Not able to cast data in GlobalDataContainer to List of Strings");
|
---|
129 | }
|
---|
130 |
|
---|
131 | treeTargets.add(nodes.get(childHwnd).xmlRepresentation());
|
---|
132 | }
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * <p>
|
---|
136 | * Removes a window (defined by its hwnd) from the tree. All children of the
|
---|
137 | * window will be removed recursively.
|
---|
138 | * </p>
|
---|
139 | *
|
---|
140 | * @param hwnd
|
---|
141 | * hwnd of the window to be removed
|
---|
142 | * @return number of windows that were removed
|
---|
143 | */
|
---|
144 | public int remove(int hwnd) {
|
---|
145 | int removedCounter = 0;
|
---|
146 | WindowTreeNode node = nodes.get(hwnd);
|
---|
147 | if (node != null) {
|
---|
148 | List<WindowTreeNode> nodesToBeRemoved = node.remove();
|
---|
149 | for (int i = 0; i < nodesToBeRemoved.size(); i++) {
|
---|
150 | WindowTreeNode nodeToBeRemoved = nodesToBeRemoved.get(i);
|
---|
151 | nodesToBeRemoved.addAll(nodeToBeRemoved.getChildren());
|
---|
152 | nodes.remove(nodeToBeRemoved.getHwnd());
|
---|
153 | removedCounter++;
|
---|
154 | }
|
---|
155 | nodes.remove(hwnd);
|
---|
156 | removedCounter++;
|
---|
157 | }
|
---|
158 | return removedCounter;
|
---|
159 | }
|
---|
160 |
|
---|
161 | /**
|
---|
162 | * <p>
|
---|
163 | * Searches the tree for a window with the specified hwnd and returns its
|
---|
164 | * {@link WindowTreeNode}.
|
---|
165 | * </p>
|
---|
166 | *
|
---|
167 | * @param hwnd
|
---|
168 | * hwnd that is looked for
|
---|
169 | * @return {@link WindowTreeNode} of the window with the given hwnd if
|
---|
170 | * found, null otherwise
|
---|
171 | */
|
---|
172 | public WindowTreeNode find(int hwnd) {
|
---|
173 | return nodes.get(hwnd);
|
---|
174 | }
|
---|
175 |
|
---|
176 | /**
|
---|
177 | * <p>
|
---|
178 | * Returns the number of nodes contained in the WindowTree.
|
---|
179 | * </p>
|
---|
180 | *
|
---|
181 | * @return number of nodes
|
---|
182 | */
|
---|
183 | public int size() {
|
---|
184 | return nodes.size();
|
---|
185 | }
|
---|
186 | }
|
---|