package de.ugoe.cs.eventbench.windows.data; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.SortedSet; import java.util.TreeSet; /** *

* This class provides an the interfaces for window trees. *

*

* The window tree represents the hierarchical structure of the windows * "as it is" currently during a session. It may change during the session due * to creation and destruction of windows. *

*

* The class is implemented as a singleton. The rational behind implementing * this class as a singleton is to ease the access of all class that may request * information about the windows during the parsing of a session. As the tree * may change during the session, it does not make sense to preserve it after a * session. Thus, it can just be deleted. Therefore, as long as only one session * is parsed at a time, a single instance is sufficient. *

* * @author Steffen Herbold * @version 1.0 */ public class WindowTree { /** *

* Handle to the window instance. *

*/ private static WindowTree theInstance = null; /** *

* Maintains a set of all the target strings of all widgets that were at * some point part of the window tree. *

*/ private SortedSet targets; /** *

* Obtain a handle to the window instance. *

* * @return instance of the window tree */ public static WindowTree getInstance() { if (theInstance == null) { theInstance = new WindowTree(); } return theInstance; } /** *

* Resets the tree. Should be used between sessions. *

*/ public static void resetTree() { theInstance = null; } /** *

* Map of all windows that are part of the tree for efficient searching. The * keys of the map are the hwnd's of the windows. *

*/ private Map nodes; /** *

* Creates a new WindowTree. *

*

* Private, as the class is a singleton. *

*/ private WindowTree() { nodes = new HashMap(); targets = new TreeSet(); } /** *

* Adds a new window to the tree. *

* * @param parentHwnd * hwnd of the parent window * @param childHwnd * hwnd of the window to be created * @param childWindowName * resource id of the window to be created * @param resourceId * resource id of the window to be created * @param className * class name of the window to be created */ public void add(int parentHwnd, int childHwnd, String childWindowName, int resourceId, String className, boolean isModal) { WindowTreeNode parent = nodes.get(parentHwnd); WindowTreeNode child = nodes.get(childHwnd); if (child == null) { if (parent != null) { child = parent.addChild(childHwnd, childWindowName, resourceId, className, isModal); } else { child = new WindowTreeNode(childHwnd, null, childWindowName, resourceId, className, isModal); } nodes.put(childHwnd, child); targets.add(child.xmlRepresentation()); } } /** *

* Removes a window (defined by its hwnd) from the tree. All children of the * window will be removed recursively. *

* * @param hwnd * hwnd of the window to be removed * @return number of windows that were removed */ public int remove(int hwnd) { int removedCounter = 0; WindowTreeNode node = nodes.get(hwnd); if (node != null) { List nodesToBeRemoved = node.remove(); for (int i = 0; i < nodesToBeRemoved.size(); i++) { WindowTreeNode nodeToBeRemoved = nodesToBeRemoved.get(i); nodesToBeRemoved.addAll(nodeToBeRemoved.getChildren()); nodes.remove(nodeToBeRemoved.getHwnd()); removedCounter++; } nodes.remove(hwnd); removedCounter++; } return removedCounter; } /** *

* Searches the tree for a window with the specified hwnd and returns its * {@link WindowTreeNode}. *

* * @param hwnd * hwnd that is looked for * @return {@link WindowTreeNode} of the window with the given hwnd if * found, null otherwise */ public WindowTreeNode find(int hwnd) { return nodes.get(hwnd); } /** *

* Returns the number of nodes contained in the WindowTree. *

* * @return number of nodes */ public int size() { return nodes.size(); } /** *

* Returns a sorted set of all targets that existed any time in the window * tree. *

* * @return set of targets */ public SortedSet getTargets() { return targets; } }