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
12 class TopWindowListener
14 TopWindowListener (AccessibilityTreeModel aModel
, SimpleOffice 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();
32 int nTopWindowCount
= xToolkit
.getTopWindowCount();
33 MessageArea
.println ("There are " + nTopWindowCount
+ " top windows.");
34 for (int i
=0; i
<nTopWindowCount
; i
++)
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
);
47 System
.out
.println ("caught exception: " + e
);
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();
69 System
.out
.println ("top level window not accessible");
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
91 Returns <true/> when the given object should not be displayed,
94 private boolean FilterTopLevelNode (XAccessibleContext xContext
)
96 // No filtering at the moment.
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
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
));
140 System
.out
.println ("caught exception; " + e
);
149 // XTopWindowListener
150 public void windowOpened (final com
.sun
.star
.lang
.EventObject aEvent
)
151 throws RuntimeException
155 XWindow xWindow
= (XWindow
) UnoRuntime
.queryInterface(
156 XWindow
.class, aEvent
.Source
);
158 System
.out
.println ("event source is no XWindow");
161 XAccessible xAccessible
= maOffice
.getAccessibleObject(xWindow
);
162 if (xAccessible
== null)
163 System
.out
.println ("event source is no XAccessible");
165 AddTopLevelNode (xAccessible
);
173 public void windowClosed (final com
.sun
.star
.lang
.EventObject aEvent
)
174 throws RuntimeException
178 XWindow xWindow
= (XWindow
) UnoRuntime
.queryInterface(
179 XWindow
.class, aEvent
.Source
);
181 System
.out
.println ("event source is no XWindow");
184 XAccessible xAccessible
= maOffice
.getAccessibleObject(xWindow
);
185 if (xAccessible
== null)
186 System
.out
.println ("event source is no XAccessible");
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