1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 package org.openoffice.accessibility.misc;
23 
24 import java.lang.Thread;
25 import java.io.PrintStream;
26 
27 import com.sun.star.awt.Rectangle;
28 import com.sun.star.awt.XWindow;
29 
30 import com.sun.star.beans.Property;
31 import com.sun.star.beans.PropertyValue;
32 import com.sun.star.beans.XPropertySet;
33 import com.sun.star.beans.XPropertySetInfo;
34 
35 import com.sun.star.container.XIndexAccess;
36 import com.sun.star.container.XChild;
37 import com.sun.star.container.XEnumerationAccess;
38 import com.sun.star.container.XEnumeration;
39 
40 import com.sun.star.frame.XComponentLoader;
41 import com.sun.star.frame.XController;
42 import com.sun.star.frame.XDesktop;
43 import com.sun.star.frame.XFrame;
44 import com.sun.star.frame.XTasksSupplier;
45 import com.sun.star.frame.XTask;
46 
47 import com.sun.star.lang.XComponent;
48 import com.sun.star.lang.XMultiServiceFactory;
49 import com.sun.star.lang.XServiceInfo;
50 import com.sun.star.lang.XServiceName;
51 import com.sun.star.lang.XTypeProvider;
52 
53 import com.sun.star.uno.UnoRuntime;
54 import com.sun.star.uno.XInterface;
55 import com.sun.star.uno.Type;
56 
57 import com.sun.star.drawing.XDrawView;
58 import com.sun.star.drawing.XDrawPage;
59 import com.sun.star.drawing.XShapes;
60 import com.sun.star.drawing.XShape;
61 import com.sun.star.drawing.XShapeDescriptor;
62 
63 import com.sun.star.accessibility.XAccessible;
64 import com.sun.star.accessibility.XAccessibleContext;
65 import com.sun.star.accessibility.XAccessibleComponent;
66 import com.sun.star.accessibility.XAccessibleRelationSet;
67 import com.sun.star.accessibility.XAccessibleStateSet;
68 
69 public class InformationWriter
70 {
InformationWriter(PrintStream aOut)71     public InformationWriter (PrintStream aOut)
72     {
73         maOut = aOut;
74     }
75 
drawPageTest(XInterface xPage)76     public void drawPageTest (XInterface xPage)
77     {
78         try
79         {
80             printProperty (xPage, "BorderBottom  ", "BorderBottom");
81             printProperty (xPage, "BorderLeft    ", "BorderLeft");
82             printProperty (xPage, "BorderRight   ", "BorderRight");
83             printProperty (xPage, "BorderTop     ", "BorderTop");
84             printProperty (xPage, "Height        ", "Height");
85             printProperty (xPage, "Width         ", "Width");
86             printProperty (xPage, "Number        ", "Number");
87         }
88         catch  (Exception e)
89         {
90             System.out.println ("caught exception while testing draw page:" + e);
91         }
92     }
93 
printProperty(XInterface xObject, String prefix, String name)94     public void printProperty (XInterface xObject, String prefix, String name)
95     {
96         try
97         {
98             XPropertySet xPropertySet =  (XPropertySet) UnoRuntime.queryInterface(
99                 XPropertySet.class, xObject);
100             maOut.println (prefix +
101                 xPropertySet.getPropertyValue (name));
102         }
103         catch (Exception e)
104         {
105             maOut.println ("caught exception while getting property "
106                 + name + " : " + e);
107         }
108     }
109 
110 
111 
showShapes(XDrawPage xPage)112     public void showShapes (XDrawPage xPage)
113     {
114         try
115         {
116             XIndexAccess xShapeList = (XIndexAccess) UnoRuntime.queryInterface(
117                 XIndexAccess.class, xPage);
118 
119             maOut.println ("There are " + xShapeList.getCount()
120                 + " shapes");
121             for (int i=0; i<xShapeList.getCount(); i++)
122             {
123                 XShape xShape = (XShape) UnoRuntime.queryInterface(
124                     XShape.class, xShapeList.getByIndex (i));
125 
126                 XShapeDescriptor xShapeDescriptor =
127                     (XShapeDescriptor) UnoRuntime.queryInterface(
128                         XShapeDescriptor.class, xShape);
129                 String sName = xShapeDescriptor.getShapeType ();
130                 maOut.println ("   shape " + i + " : " + sName);
131 
132                 XPropertySet xPropertySet =
133                     (XPropertySet) UnoRuntime.queryInterface(
134                         XPropertySet.class, xShape);
135                 Integer nZOrder =
136                     (Integer) xPropertySet.getPropertyValue ("ZOrder");
137                 maOut.println ("   zorder = " + nZOrder);
138             }
139         }
140         catch (Exception e)
141         {
142             maOut.println ("caught exception in showShapes: " + e);
143         }
144     }
145 
146 
147 
148 
149     /** @descr Print all available services of the given object to the
150                 standard output.
151     */
showServices(XInterface xObject)152     public void showServices (XInterface xObject)
153     {
154         try
155         {
156             maOut.println ("Services:");
157             XMultiServiceFactory xMSF = (XMultiServiceFactory) UnoRuntime.queryInterface (
158                 XMultiServiceFactory.class,
159                 xObject
160                 );
161             if (xMSF == null)
162                 maOut.println ("    object does not support interface XMultiServiceFactory");
163             else
164             {
165                 String[] sServiceNames = xMSF.getAvailableServiceNames ();
166                 maOut.println ("    object can create "
167                     + sServiceNames.length + " services");
168                 for (int i=0; i<sServiceNames.length; i++)
169                     maOut.println ("        service " + i + " : " + sServiceNames[i]);
170             }
171         }
172         catch (Exception e)
173         {
174             maOut.println ("caught exception in showServices : " + e);
175         }
176     }
177 
178     /** @descr Print the service and implementation name of the given
179                 object.
180     */
showInfo(XInterface xObject)181     public void showInfo (XInterface xObject)
182     {
183         try
184         {
185             System.out.println ("Info:");
186             // Use interface XServiceName to retrieve name of (main) service.
187             XServiceName xSN = (XServiceName) UnoRuntime.queryInterface (
188                 XServiceName.class, xObject);
189             if (xSN == null)
190                 maOut.println ("    interface XServiceName not supported");
191             else
192             {
193                 maOut.println ("    Service name        : " + xSN.getServiceName ());
194             }
195 
196             // Use interface XServiceInfo to retrieve information about
197             // supported services.
198             XServiceInfo xSI = (XServiceInfo) UnoRuntime.queryInterface (
199                 XServiceInfo.class, xObject);
200             if (xSI == null)
201                 maOut.println ("    interface XServiceInfo not supported");
202             else
203             {
204                 maOut.println ("    Implementation name : "
205                     + xSI.getImplementationName ());
206             }
207         }
208         catch (Exception e)
209         {
210             maOut.println ("caught exception in showInfo : " + e);
211         }
212     }
213 
214 
215 
216 
217     /** @descr Print information about supported interfaces.
218     */
showInterfaces(XInterface xObject)219     public void showInterfaces (XInterface xObject)
220     {
221         try
222         {
223             maOut.println ("Interfaces:");
224             // Use interface XTypeProvider to retrieve a list of supported
225             // interfaces.
226             XTypeProvider xTP = (XTypeProvider) UnoRuntime.queryInterface (
227                 XTypeProvider.class, xObject);
228             if (xTP == null)
229                 maOut.println ("    interface XTypeProvider not supported");
230             else
231             {
232                 Type[] aTypeList = xTP.getTypes ();
233                 maOut.println ("    object supports " + aTypeList.length
234                     + " interfaces");
235                 for (int i=0; i<aTypeList.length; i++)
236                     maOut.println ("        " + i + " : "
237                         + aTypeList[i].getTypeName());
238             }
239         }
240         catch (Exception e)
241         {
242             maOut.println ("caught exception in showInterfaces : " + e);
243         }
244     }
245 
246 
247     /** @descr Print information concerning the accessibility of the given
248         object.
249     */
showAccessibility(XInterface xObject, int depth)250     public boolean showAccessibility (XInterface xObject, int depth)
251     {
252         try
253         {
254             // Create indentation string.
255             String sIndent = "";
256             while (depth-- > 0)
257                 sIndent += "    ";
258 
259             //  Get XAccessibleContext object if given object does not
260             //  already support this interface.
261             XAccessibleContext xContext
262                 = (XAccessibleContext) UnoRuntime.queryInterface (
263                     XAccessibleContext.class, xObject);
264             if (xContext == null)
265             {
266                 XAccessible xAccessible
267                     = (XAccessible) UnoRuntime.queryInterface (
268                         XAccessible.class, xObject);
269                 if (xAccessible == null)
270                 {
271                     maOut.println (sIndent + "given object " + xObject
272                         + " is not accessible");
273                     return false;
274                 }
275                 else
276                     xContext = xAccessible.getAccessibleContext();
277             }
278 
279             //  Print information about the accessible context.
280             if (xContext != null)
281             {
282                 maOut.println (sIndent + "Name         : "
283                     + xContext.getAccessibleName());
284                 maOut.println (sIndent + "Description  : "
285                     + xContext.getAccessibleDescription());
286                 maOut.println (sIndent + "Role         : "
287                     + xContext.getAccessibleRole());
288                 String sHasParent;
289                 if (xContext.getAccessibleParent() != null)
290                 {
291                     maOut.println (sIndent + "Has parent   : yes");
292                     maOut.println (sIndent + "Parent index : "
293                         + xContext.getAccessibleIndexInParent());
294                 }
295                 else
296                     maOut.println (sIndent + "Has parent   : no");
297                 maOut.println (sIndent + "Child count  : "
298                     + xContext.getAccessibleChildCount());
299                 maOut.print (sIndent + "Relation set : ");
300                 XAccessibleRelationSet xRelationSet
301                     = xContext.getAccessibleRelationSet();
302                 if (xRelationSet != null)
303                 {
304                     maOut.print (xRelationSet.getRelationCount() + " (");
305                     for (int i=0; i<xRelationSet.getRelationCount(); i++)
306                     {
307                         if (i > 0)
308                             maOut.print (", ");
309                         maOut.print (xRelationSet.getRelation(i).toString());
310                     }
311                     maOut.println (")");
312                 }
313                 else
314                     maOut.println ("no relation set");
315 
316                 maOut.print (sIndent + "State set    : ");
317                 XAccessibleStateSet xStateSet =
318                     xContext.getAccessibleStateSet();
319                 if (xStateSet != null)
320                 {
321                     XIndexAccess xStates =
322                         (XIndexAccess) UnoRuntime.queryInterface (
323                             XIndexAccess.class, xStateSet);
324                     maOut.print (xStates.getCount() + " (");
325                     for (int i=0; i<xStates.getCount(); i++)
326                     {
327                         if (i > 0)
328                             maOut.print (", ");
329                         maOut.print (xStates.getByIndex(i).toString());
330                     }
331                     maOut.println (")");
332                 }
333                 else
334                     maOut.println ("no state set");
335 
336                 showAccessibleComponent (xContext, sIndent);
337             }
338             else
339                 maOut.println ("object has no accessible context.");
340 
341             //            showInfo (xContext);
342             //            showServices (xContext);
343             //            showInterfaces (xContext);
344         }
345         catch (Exception e)
346         {
347             System.out.println ("caught exception in showAccessibility :" + e);
348         }
349         return true;
350     }
351 
352 
353 
354 
355     /** @descr Print information about the given accessible component.
356     */
showAccessibleComponent(XInterface xObject, String sIndent)357     public void showAccessibleComponent (XInterface xObject, String sIndent)
358     {
359         try
360         {
361             XAccessibleComponent xComponent =
362                 (XAccessibleComponent) UnoRuntime.queryInterface (
363                     XAccessibleComponent.class, xObject);
364 
365             //  Print information about the accessible context.
366             if (xComponent != null)
367             {
368                 maOut.println (sIndent + "Position        : "
369                     + xComponent.getLocation().X+", "
370                     + xComponent.getLocation().Y);
371                 maOut.println (sIndent + "Screen position : "
372                     + xComponent.getLocationOnScreen().X+", "
373                     + xComponent.getLocationOnScreen().Y);
374                 maOut.println (sIndent + "Size            : "
375                     + xComponent.getSize().Width+", "
376                     + xComponent.getSize().Height);
377             }
378         }
379         catch (Exception e)
380         {
381             System.out.println (
382                 "caught exception in showAccessibleComponent : " + e);
383         }
384     }
385 
386 
387     /** Show a textual representation of the accessibility subtree rooted in
388         xRoot.
389     */
showAccessibilityTree(XAccessible xRoot, int depth)390     public boolean showAccessibilityTree (XAccessible xRoot, int depth)
391     {
392         try
393         {
394             if ( ! showAccessibility (xRoot, depth))
395                 return false;
396 
397             String sIndent = "";
398             for (int i=0; i<depth; i++)
399                 sIndent += "    ";
400 
401             //  Iterate over children and show them.
402             XAccessibleContext xContext = xRoot.getAccessibleContext();
403             if (xContext != null)
404             {
405                 int n = xContext.getAccessibleChildCount();
406                 for (int i=0; i<n; i++)
407                 {
408                     maOut.println (sIndent + "child " + i + " :");
409                     showAccessibilityTree (xContext.getAccessibleChild(i),depth+1);
410                 }
411             }
412             else
413                 maOut.println ("Accessible object has no context");
414         }
415         catch (Exception e)
416         {
417             System.out.println (
418                 "caught exception in showAccessibleTree : " + e);
419             return false;
420         }
421 
422         return true;
423     }
424 
showProperties(XInterface xObject)425     public void showProperties (XInterface xObject)
426     {
427         XPropertySet xSet = (XPropertySet) UnoRuntime.queryInterface (
428             XPropertySet.class, xObject);
429         if (xSet == null)
430             maOut.println ("object does not support XPropertySet");
431         else
432         {
433             XPropertySetInfo xInfo = xSet.getPropertySetInfo ();
434             Property[] aProperties = xInfo.getProperties ();
435             int n = aProperties.length;
436             for (int i=0; i<n; i++)
437                 maOut.println (i + " : " + aProperties[i].Name +", " + aProperties[i].Type);
438         }
439     }
440 
441     private PrintStream maOut;
442 }
443