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