bump product version to 7.2.5.1
[LibreOffice.git] / toolkit / test / accessibility / TopWindowListener.java
bloba35b51a5ff9ac4d1b83064d556a85a73f0334a45
1 /*
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
26 model accordingly.
28 class TopWindowListener
30 TopWindowListener (AccessibilityTreeModel aModel, SimpleOffice aOffice)
32 maModel = aModel;
33 maOffice = 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();
45 if (xToolkit != null)
47 maModel.lock ();
48 int nTopWindowCount = xToolkit.getTopWindowCount();
49 MessageArea.println ("There are " + nTopWindowCount + " top windows.");
50 for (int i=0; i<nTopWindowCount; i++)
52 try
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);
61 catch (Exception e)
63 System.out.println ("caught exception: " + e);
64 e.printStackTrace();
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();
84 if (xContext == null)
85 System.out.println ("top level window not accessible");
86 else
88 Object aRootObject = maModel.getRoot();
89 if (aRootObject instanceof VectorNode)
91 VectorNode aRoot = (VectorNode) aRootObject;
92 AccessibleTreeNode aNode =
93 NodeFactory.Instance().createDefaultNode (xNewTopLevelObject, aRoot);
94 aRoot.addChild (aNode);
95 maModel.fireTreeNodesInserted (maModel.createEvent (aRoot, aNode));
101 /** Remove an existing top level node from the tree.
102 @param xNewTopLevelObject
103 The accessible object to remove.
105 private void RemoveTopLevelNode (XAccessible xTopLevelObject)
107 Object aObject = maModel.getRoot();
108 if (aObject instanceof VectorNode && xTopLevelObject != null)
110 System.out.println ("removing node " + xTopLevelObject);
111 maModel.removeNode (xTopLevelObject.getAccessibleContext());
119 // XTopWindowListener
120 public void windowOpened (final com.sun.star.lang.EventObject aEvent)
121 throws RuntimeException
123 if (maModel != null)
125 XWindow xWindow = UnoRuntime.queryInterface(
126 XWindow.class, aEvent.Source);
127 if (xWindow == null)
128 System.out.println ("event source is no XWindow");
129 else
131 XAccessible xAccessible = maOffice.getAccessibleObject(xWindow);
132 if (xAccessible == null)
133 System.out.println ("event source is no XAccessible");
134 else
135 AddTopLevelNode (xAccessible);
143 public void windowClosed (final com.sun.star.lang.EventObject aEvent)
144 throws RuntimeException
146 if (maModel != null)
148 XWindow xWindow = UnoRuntime.queryInterface(
149 XWindow.class, aEvent.Source);
150 if (xWindow == null)
151 System.out.println ("event source is no XWindow");
152 else
154 XAccessible xAccessible = maOffice.getAccessibleObject(xWindow);
155 if (xAccessible == null)
156 System.out.println ("event source is no XAccessible");
157 else
158 RemoveTopLevelNode (xAccessible);
163 public void disposing (final com.sun.star.lang.EventObject aEvent)
165 System.out.println ("Top window disposed: " + aEvent);
171 private final AccessibilityTreeModel
172 maModel;
173 private final SimpleOffice
174 maOffice;