merged tag ooo/DEV300_m102
[LibreOffice.git] / toolkit / test / accessibility / TopWindowListener.java
blobc0aea933ece76302a3c178cfd7d726f33676d048
1 import com.sun.star.awt.XWindow;
2 import com.sun.star.awt.XExtendedToolkit;
3 import com.sun.star.accessibility.XAccessible;
4 import com.sun.star.accessibility.XAccessibleContext;
5 import com.sun.star.uno.XInterface;
6 import com.sun.star.uno.UnoRuntime;
7 import javax.swing.event.TreeModelEvent;
9 /** Listen for top window events and create or delete children of the tree
10 model accordingly.
12 class TopWindowListener
14 TopWindowListener (AccessibilityTreeModel aModel, SimpleOffice aOffice)
16 maModel = aModel;
17 maOffice = aOffice;
23 /** Use this function to initially fill the accessibility object tree
24 view with nodes for top level windows.
26 public void Initialize ()
28 XExtendedToolkit xToolkit = maOffice.getExtendedToolkit();
29 if (xToolkit != null)
31 maModel.lock ();
32 int nTopWindowCount = xToolkit.getTopWindowCount();
33 MessageArea.println ("There are " + nTopWindowCount + " top windows.");
34 for (int i=0; i<nTopWindowCount; i++)
36 try
38 XAccessible xAccessible = maOffice.getAccessibleObject(
39 xToolkit.getTopWindow (i));
40 // Uncomment the following line to get the real root of
41 // the accessible tree that xAccessible belongs to.
42 // xAccessible = maOffice.getAccessibleRoot(xAccessible);
43 AddTopLevelNode (xAccessible);
45 catch (Exception e)
47 System.out.println ("caught exception: " + e);
48 e.printStackTrace();
51 maModel.unlock ((AccessibleTreeNode)maModel.getRoot());
57 /** Add a new top level node which, to be exact, will be placed on the
58 second layer of the tree.
59 @param xNewTopLevelObject
60 The accessible object of the new top level window.
62 private void AddTopLevelNode (XAccessible xNewTopLevelObject)
64 System.out.println ("adding top level window");
65 if (xNewTopLevelObject != null)
67 XAccessibleContext xContext = xNewTopLevelObject.getAccessibleContext();
68 if (xContext == null)
69 System.out.println ("top level window not accessible");
70 else
72 if ( ! FilterTopLevelNode (xContext))
74 Object aRootObject = maModel.getRoot();
75 if (aRootObject instanceof VectorNode)
77 VectorNode aRoot = (VectorNode) aRootObject;
78 AccessibleTreeNode aNode =
79 NodeFactory.Instance().createDefaultNode (xNewTopLevelObject, aRoot);
80 aRoot.addChild (aNode);
81 maModel.fireTreeNodesInserted (maModel.createEvent (aRoot, aNode));
88 /** Ignore windows that have no accessible name, i.e. do not represent
89 document windows.
90 @return
91 Returns <true/> when the given object should not be displayed,
92 i.e. filtered out.
94 private boolean FilterTopLevelNode (XAccessibleContext xContext)
96 // No filtering at the moment.
97 return false;
98 // return xContext.getAccessibleName().length() == 0;
104 /** Remove an existing top level node from the tree.
105 @param xNewTopLevelObject
106 The accessible object to remove.
108 private void RemoveTopLevelNode (XAccessible xTopLevelObject)
110 Object aObject = maModel.getRoot();
111 if (aObject instanceof VectorNode && xTopLevelObject != null)
113 System.out.println ("removing node " + xTopLevelObject);
114 VectorNode aRoot = (VectorNode) aObject;
115 maModel.removeNode (xTopLevelObject.getAccessibleContext());
123 /** This method exists for debugging. It prints a list of all top
124 level windows.
126 private void ShowAllTopLevelWindows ()
128 XExtendedToolkit xToolkit = maOffice.getExtendedToolkit();
129 if (xToolkit != null)
131 int nTopWindowCount = xToolkit.getTopWindowCount();
132 for (int i=0; i<nTopWindowCount; i++)
136 System.out.println (i + " : " + xToolkit.getTopWindow (i));
138 catch (Exception e)
140 System.out.println ("caught exception; " + e);
149 // XTopWindowListener
150 public void windowOpened (final com.sun.star.lang.EventObject aEvent)
151 throws RuntimeException
153 if (maModel != null)
155 XWindow xWindow = (XWindow) UnoRuntime.queryInterface(
156 XWindow.class, aEvent.Source);
157 if (xWindow == null)
158 System.out.println ("event source is no XWindow");
159 else
161 XAccessible xAccessible = maOffice.getAccessibleObject(xWindow);
162 if (xAccessible == null)
163 System.out.println ("event source is no XAccessible");
164 else
165 AddTopLevelNode (xAccessible);
173 public void windowClosed (final com.sun.star.lang.EventObject aEvent)
174 throws RuntimeException
176 if (maModel != null)
178 XWindow xWindow = (XWindow) UnoRuntime.queryInterface(
179 XWindow.class, aEvent.Source);
180 if (xWindow == null)
181 System.out.println ("event source is no XWindow");
182 else
184 XAccessible xAccessible = maOffice.getAccessibleObject(xWindow);
185 if (xAccessible == null)
186 System.out.println ("event source is no XAccessible");
187 else
188 RemoveTopLevelNode (xAccessible);
193 public void disposing (final com.sun.star.lang.EventObject aEvent)
195 System.out.println ("Top window disposed: " + aEvent);
201 private AccessibilityTreeModel
202 maModel;
203 private SimpleOffice
204 maOffice;