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