2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 import com
.sun
.star
.awt
.XWindow
;
20 import com
.sun
.star
.awt
.XExtendedToolkit
;
21 import com
.sun
.star
.accessibility
.XAccessible
;
22 import com
.sun
.star
.accessibility
.XAccessibleContext
;
23 import com
.sun
.star
.uno
.UnoRuntime
;
25 /** Listen for top window events and create or delete children of the tree
28 class TopWindowListener
30 TopWindowListener (AccessibilityTreeModel aModel
, SimpleOffice aOffice
)
39 /** Use this function to initially fill the accessibility object tree
40 view with nodes for top level windows.
42 public void Initialize ()
44 XExtendedToolkit xToolkit
= maOffice
.getExtendedToolkit();
48 int nTopWindowCount
= xToolkit
.getTopWindowCount();
49 MessageArea
.println ("There are " + nTopWindowCount
+ " top windows.");
50 for (int i
=0; i
<nTopWindowCount
; i
++)
54 XAccessible xAccessible
= maOffice
.getAccessibleObject(
55 xToolkit
.getTopWindow (i
));
56 // Uncomment the following line to get the real root of
57 // the accessible tree that xAccessible belongs to.
58 // xAccessible = maOffice.getAccessibleRoot(xAccessible);
59 AddTopLevelNode (xAccessible
);
63 System
.out
.println ("caught exception: " + e
);
67 maModel
.unlock ((AccessibleTreeNode
)maModel
.getRoot());
73 /** Add a new top level node which, to be exact, will be placed on the
74 second layer of the tree.
75 @param xNewTopLevelObject
76 The accessible object of the new top level window.
78 private void AddTopLevelNode (XAccessible xNewTopLevelObject
)
80 System
.out
.println ("adding top level window");
81 if (xNewTopLevelObject
!= null)
83 XAccessibleContext xContext
= xNewTopLevelObject
.getAccessibleContext();
85 System
.out
.println ("top level window not accessible");
88 if ( ! FilterTopLevelNode (xContext
))
90 Object aRootObject
= maModel
.getRoot();
91 if (aRootObject
instanceof VectorNode
)
93 VectorNode aRoot
= (VectorNode
) aRootObject
;
94 AccessibleTreeNode aNode
=
95 NodeFactory
.Instance().createDefaultNode (xNewTopLevelObject
, aRoot
);
96 aRoot
.addChild (aNode
);
97 maModel
.fireTreeNodesInserted (maModel
.createEvent (aRoot
, aNode
));
104 /** Ignore windows that have no accessible name, i.e. do not represent
107 Returns <true/> when the given object should not be displayed,
110 private boolean FilterTopLevelNode (XAccessibleContext xContext
)
112 // No filtering at the moment.
114 // return xContext.getAccessibleName().length() == 0;
120 /** Remove an existing top level node from the tree.
121 @param xNewTopLevelObject
122 The accessible object to remove.
124 private void RemoveTopLevelNode (XAccessible xTopLevelObject
)
126 Object aObject
= maModel
.getRoot();
127 if (aObject
instanceof VectorNode
&& xTopLevelObject
!= null)
129 System
.out
.println ("removing node " + xTopLevelObject
);
130 VectorNode aRoot
= (VectorNode
) aObject
;
131 maModel
.removeNode (xTopLevelObject
.getAccessibleContext());
139 /** This method exists for debugging. It prints a list of all top
142 private void ShowAllTopLevelWindows ()
144 XExtendedToolkit xToolkit
= maOffice
.getExtendedToolkit();
145 if (xToolkit
!= null)
147 int nTopWindowCount
= xToolkit
.getTopWindowCount();
148 for (int i
=0; i
<nTopWindowCount
; i
++)
152 System
.out
.println (i
+ " : " + xToolkit
.getTopWindow (i
));
156 System
.out
.println ("caught exception; " + e
);
165 // XTopWindowListener
166 public void windowOpened (final com
.sun
.star
.lang
.EventObject aEvent
)
167 throws RuntimeException
171 XWindow xWindow
= UnoRuntime
.queryInterface(
172 XWindow
.class, aEvent
.Source
);
174 System
.out
.println ("event source is no XWindow");
177 XAccessible xAccessible
= maOffice
.getAccessibleObject(xWindow
);
178 if (xAccessible
== null)
179 System
.out
.println ("event source is no XAccessible");
181 AddTopLevelNode (xAccessible
);
189 public void windowClosed (final com
.sun
.star
.lang
.EventObject aEvent
)
190 throws RuntimeException
194 XWindow xWindow
= UnoRuntime
.queryInterface(
195 XWindow
.class, aEvent
.Source
);
197 System
.out
.println ("event source is no XWindow");
200 XAccessible xAccessible
= maOffice
.getAccessibleObject(xWindow
);
201 if (xAccessible
== null)
202 System
.out
.println ("event source is no XAccessible");
204 RemoveTopLevelNode (xAccessible
);
209 public void disposing (final com
.sun
.star
.lang
.EventObject aEvent
)
211 System
.out
.println ("Top window disposed: " + aEvent
);
217 private AccessibilityTreeModel