bump product version to 4.2.0.1
[LibreOffice.git] / toolkit / test / accessibility / InformationWriter.java
blob994d84ca42be5a6b6788db39cae59458a3adede9
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.beans.Property;
20 import com.sun.star.beans.XPropertySet;
21 import com.sun.star.beans.XPropertySetInfo;
23 import com.sun.star.container.XIndexAccess;
24 import com.sun.star.lang.XMultiServiceFactory;
25 import com.sun.star.lang.XServiceInfo;
26 import com.sun.star.lang.XServiceName;
27 import com.sun.star.lang.XTypeProvider;
29 import com.sun.star.uno.UnoRuntime;
30 import com.sun.star.uno.XInterface;
31 import com.sun.star.uno.Type;
33 import com.sun.star.drawing.XDrawPage;
34 import com.sun.star.drawing.XShape;
35 import com.sun.star.drawing.XShapeDescriptor;
37 import com.sun.star.accessibility.XAccessible;
38 import com.sun.star.accessibility.XAccessibleContext;
39 import com.sun.star.accessibility.XAccessibleComponent;
40 import com.sun.star.accessibility.XAccessibleRelationSet;
41 import com.sun.star.accessibility.XAccessibleStateSet;
43 public class InformationWriter
45 public InformationWriter ()
49 public void drawPageTest (XInterface xPage)
51 try
53 printProperty (xPage, "BorderBottom ", "BorderBottom");
54 printProperty (xPage, "BorderLeft ", "BorderLeft");
55 printProperty (xPage, "BorderRight ", "BorderRight");
56 printProperty (xPage, "BorderTop ", "BorderTop");
57 printProperty (xPage, "Height ", "Height");
58 printProperty (xPage, "Width ", "Width");
59 printProperty (xPage, "Number ", "Number");
61 catch (Exception e)
63 System.out.println ("caught exception while testing draw page:" + e);
67 public void printProperty (XInterface xObject, String prefix, String name)
69 try
71 XPropertySet xPropertySet = UnoRuntime.queryInterface(
72 XPropertySet.class, xObject);
73 MessageArea.println (prefix +
74 xPropertySet.getPropertyValue (name));
76 catch (Exception e)
78 MessageArea.println ("caught exception while getting property "
79 + name + " : " + e);
85 public void showShapes (XDrawPage xPage)
87 try
89 XIndexAccess xShapeList = UnoRuntime.queryInterface(
90 XIndexAccess.class, xPage);
92 MessageArea.println ("There are " + xShapeList.getCount()
93 + " shapes");
94 for (int i=0; i<xShapeList.getCount(); i++)
96 XShape xShape = UnoRuntime.queryInterface(
97 XShape.class, xShapeList.getByIndex (i));
99 XShapeDescriptor xShapeDescriptor =
100 UnoRuntime.queryInterface(
101 XShapeDescriptor.class, xShape);
102 String sName = xShapeDescriptor.getShapeType ();
103 MessageArea.println (" shape " + i + " : " + sName);
105 XPropertySet xPropertySet =
106 UnoRuntime.queryInterface(
107 XPropertySet.class, xShape);
108 Integer nZOrder =
109 (Integer) xPropertySet.getPropertyValue ("ZOrder");
110 MessageArea.println (" zorder = " + nZOrder);
113 catch (Exception e)
115 MessageArea.println ("caught exception in showShapes: " + e);
122 /** @descr Print all available services of the given object to the
123 standard output.
125 public void showServices (XInterface xObject)
129 MessageArea.println ("Services:");
130 XMultiServiceFactory xMSF = UnoRuntime.queryInterface (
131 XMultiServiceFactory.class,
132 xObject
134 if (xMSF == null)
135 MessageArea.println (" object does not support interface XMultiServiceFactory");
136 else
138 String[] sServiceNames = xMSF.getAvailableServiceNames ();
139 MessageArea.println (" object can create "
140 + sServiceNames.length + " services");
141 for (int i=0; i<sServiceNames.length; i++)
142 MessageArea.println (" service " + i + " : " + sServiceNames[i]);
145 catch (Exception e)
147 MessageArea.println ("caught exception in showServices : " + e);
151 /** @descr Print the service and implementation name of the given
152 object.
154 public void showInfo (XInterface xObject)
158 System.out.println ("Info:");
159 // Use interface XServiceName to retrieve name of (main) service.
160 XServiceName xSN = UnoRuntime.queryInterface (
161 XServiceName.class, xObject);
162 if (xSN == null)
163 MessageArea.println (" interface XServiceName not supported");
164 else
166 MessageArea.println (" Service name : " + xSN.getServiceName ());
169 // Use interface XServiceInfo to retrieve information about
170 // supported services.
171 XServiceInfo xSI = UnoRuntime.queryInterface (
172 XServiceInfo.class, xObject);
173 if (xSI == null)
174 MessageArea.println (" interface XServiceInfo not supported");
175 else
177 MessageArea.println (" Implementation name : "
178 + xSI.getImplementationName ());
181 catch (Exception e)
183 MessageArea.println ("caught exception in showInfo : " + e);
190 /** @descr Print information about supported interfaces.
192 public void showInterfaces (XInterface xObject)
196 MessageArea.println ("Interfaces:");
197 // Use interface XTypeProvider to retrieve a list of supported
198 // interfaces.
199 XTypeProvider xTP = UnoRuntime.queryInterface (
200 XTypeProvider.class, xObject);
201 if (xTP == null)
202 MessageArea.println (" interface XTypeProvider not supported");
203 else
205 Type[] aTypeList = xTP.getTypes ();
206 MessageArea.println (" object supports " + aTypeList.length
207 + " interfaces");
208 for (int i=0; i<aTypeList.length; i++)
209 MessageArea.println (" " + i + " : "
210 + aTypeList[i].getTypeName());
213 catch (Exception e)
215 MessageArea.println ("caught exception in showInterfaces : " + e);
220 /** @descr Print information concerning the accessibility of the given
221 object.
223 public boolean showAccessibility (XInterface xObject, int depth)
227 // Create indentation string.
228 String sIndent = "";
229 while (depth-- > 0)
230 sIndent += " ";
232 // Get XAccessibleContext object if given object does not
233 // already support this interface.
234 XAccessibleContext xContext
235 = UnoRuntime.queryInterface (
236 XAccessibleContext.class, xObject);
237 if (xContext == null)
239 XAccessible xAccessible
240 = UnoRuntime.queryInterface (
241 XAccessible.class, xObject);
242 if (xAccessible == null)
244 MessageArea.println (sIndent + "given object " + xObject
245 + " is not accessible");
246 return false;
248 else
249 xContext = xAccessible.getAccessibleContext();
252 // Print information about the accessible context.
253 if (xContext != null)
255 MessageArea.println (sIndent + "Name : "
256 + xContext.getAccessibleName());
257 MessageArea.println (sIndent + "Description : "
258 + xContext.getAccessibleDescription());
259 MessageArea.println (sIndent + "Role : "
260 + xContext.getAccessibleRole());
261 String sHasParent;
262 if (xContext.getAccessibleParent() != null)
264 MessageArea.println (sIndent + "Has parent : yes");
265 MessageArea.println (sIndent + "Parent index : "
266 + xContext.getAccessibleIndexInParent());
268 else
269 MessageArea.println (sIndent + "Has parent : no");
270 MessageArea.println (sIndent + "Child count : "
271 + xContext.getAccessibleChildCount());
272 MessageArea.print (sIndent + "Relation set : ");
273 XAccessibleRelationSet xRelationSet
274 = xContext.getAccessibleRelationSet();
275 if (xRelationSet != null)
277 MessageArea.print (xRelationSet.getRelationCount() + " (");
278 for (int i=0; i<xRelationSet.getRelationCount(); i++)
280 if (i > 0)
281 MessageArea.print (", ");
282 MessageArea.print (xRelationSet.getRelation(i).toString());
284 MessageArea.println (")");
286 else
287 MessageArea.println ("no relation set");
289 MessageArea.print (sIndent + "State set : ");
290 XAccessibleStateSet xStateSet =
291 xContext.getAccessibleStateSet();
292 if (xStateSet != null)
294 XIndexAccess xStates =
295 UnoRuntime.queryInterface (
296 XIndexAccess.class, xStateSet);
297 MessageArea.print (xStates.getCount() + " (");
298 for (int i=0; i<xStates.getCount(); i++)
300 if (i > 0)
301 MessageArea.print (", ");
302 MessageArea.print (xStates.getByIndex(i).toString());
304 MessageArea.println (")");
306 else
307 MessageArea.println ("no state set");
309 showAccessibleComponent (xContext, sIndent);
311 else
312 MessageArea.println ("object has no accessible context.");
314 // showInfo (xContext);
315 // showServices (xContext);
316 // showInterfaces (xContext);
318 catch (Exception e)
320 System.out.println ("caught exception in showAccessibility :" + e);
322 return true;
328 /** @descr Print information about the given accessible component.
330 public void showAccessibleComponent (XInterface xObject, String sIndent)
334 XAccessibleComponent xComponent =
335 UnoRuntime.queryInterface (
336 XAccessibleComponent.class, xObject);
338 // Print information about the accessible context.
339 if (xComponent != null)
341 MessageArea.println (sIndent + "Position : "
342 + xComponent.getLocation().X+", "
343 + xComponent.getLocation().Y);
344 MessageArea.println (sIndent + "Screen position : "
345 + xComponent.getLocationOnScreen().X+", "
346 + xComponent.getLocationOnScreen().Y);
347 MessageArea.println (sIndent + "Size : "
348 + xComponent.getSize().Width+", "
349 + xComponent.getSize().Height);
352 catch (Exception e)
354 System.out.println (
355 "caught exception in showAccessibleComponent : " + e);
360 /** Show a textual representation of the accessibility subtree rooted in
361 xRoot.
363 public boolean showAccessibilityTree (XAccessible xRoot, int depth)
367 if ( ! showAccessibility (xRoot, depth))
368 return false;
370 String sIndent = "";
371 for (int i=0; i<depth; i++)
372 sIndent += " ";
374 // Iterate over children and show them.
375 XAccessibleContext xContext = xRoot.getAccessibleContext();
376 if (xContext != null)
378 int n = xContext.getAccessibleChildCount();
379 for (int i=0; i<n; i++)
381 MessageArea.println (sIndent + "child " + i + " :");
382 showAccessibilityTree (xContext.getAccessibleChild(i),depth+1);
385 else
386 MessageArea.println ("Accessible object has no context");
388 catch (Exception e)
390 System.out.println (
391 "caught exception in showAccessibleTree : " + e);
392 return false;
395 return true;
398 public void showProperties (XInterface xObject)
400 XPropertySet xSet = UnoRuntime.queryInterface (
401 XPropertySet.class, xObject);
402 if (xSet == null)
403 MessageArea.println ("object does not support XPropertySet");
404 else
406 XPropertySetInfo xInfo = xSet.getPropertySetInfo ();
407 Property[] aProperties = xInfo.getProperties ();
408 int n = aProperties.length;
409 for (int i=0; i<n; i++)
410 MessageArea.println (i + " : " + aProperties[i].Name +", " + aProperties[i].Type);