xref: /AOO41X/main/toolkit/test/accessibility/AccessibleRelationHandler.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
2*cdf0e10cSrcweir import com.sun.star.accessibility.XAccessible;
3*cdf0e10cSrcweir import com.sun.star.accessibility.XAccessibleContext;
4*cdf0e10cSrcweir import com.sun.star.accessibility.AccessibleRelation;
5*cdf0e10cSrcweir import com.sun.star.accessibility.XAccessibleRelationSet;
6*cdf0e10cSrcweir import com.sun.star.accessibility.AccessibleRelationType;
7*cdf0e10cSrcweir import com.sun.star.lang.IndexOutOfBoundsException;
8*cdf0e10cSrcweir 
9*cdf0e10cSrcweir import tools.NameProvider;
10*cdf0e10cSrcweir 
11*cdf0e10cSrcweir class AccessibleRelationHandler
12*cdf0e10cSrcweir     extends NodeHandler
13*cdf0e10cSrcweir {
14*cdf0e10cSrcweir     public NodeHandler createHandler( XAccessibleContext xContext )
15*cdf0e10cSrcweir     {
16*cdf0e10cSrcweir         AccessibleRelationHandler aHandler = null;
17*cdf0e10cSrcweir         if (xContext != null)
18*cdf0e10cSrcweir         {
19*cdf0e10cSrcweir             XAccessibleRelationSet xRelation = xContext.getAccessibleRelationSet();
20*cdf0e10cSrcweir             if (xRelation != null)
21*cdf0e10cSrcweir                 aHandler = new AccessibleRelationHandler(xContext);
22*cdf0e10cSrcweir         }
23*cdf0e10cSrcweir         return aHandler;
24*cdf0e10cSrcweir     }
25*cdf0e10cSrcweir 
26*cdf0e10cSrcweir     public AccessibleRelationHandler()
27*cdf0e10cSrcweir     {
28*cdf0e10cSrcweir     }
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir     public AccessibleRelationHandler( XAccessibleContext xContext )
31*cdf0e10cSrcweir     {
32*cdf0e10cSrcweir         XAccessibleRelationSet xRelation = xContext.getAccessibleRelationSet();
33*cdf0e10cSrcweir         if (xRelation != null)
34*cdf0e10cSrcweir             maChildList.setSize( 1 );
35*cdf0e10cSrcweir     }
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir     public AccessibleTreeNode createChild( AccessibleTreeNode aParent,
38*cdf0e10cSrcweir                                            int nIndex )
39*cdf0e10cSrcweir     {
40*cdf0e10cSrcweir         XAccessibleRelationSet xRelation = null;
41*cdf0e10cSrcweir         AccessibleTreeNode aChild = null;
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir         if( aParent instanceof AccTreeNode )
44*cdf0e10cSrcweir         {
45*cdf0e10cSrcweir             xRelation =
46*cdf0e10cSrcweir                 ((AccTreeNode)aParent).getContext().getAccessibleRelationSet();
47*cdf0e10cSrcweir         }
48*cdf0e10cSrcweir         if( xRelation == null )
49*cdf0e10cSrcweir             return aChild;
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir         VectorNode aVNode = new VectorNode( "RelationSet", aParent);
53*cdf0e10cSrcweir         int nCount = xRelation.getRelationCount();
54*cdf0e10cSrcweir         try
55*cdf0e10cSrcweir         {
56*cdf0e10cSrcweir             for( int i = 0; i < nCount; i++ )
57*cdf0e10cSrcweir             {
58*cdf0e10cSrcweir                 AccessibleRelation aRelation = xRelation.getRelation( i );
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir                 StringBuffer aBuffer = new StringBuffer();
61*cdf0e10cSrcweir                 aBuffer.append (NameProvider.getRelationName (aRelation.RelationType));
62*cdf0e10cSrcweir                 aBuffer.append( ": " );
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir                 for( int j = 0; j < aRelation.TargetSet.length; j++ )
65*cdf0e10cSrcweir                 {
66*cdf0e10cSrcweir                     Object aTarget = aRelation.TargetSet[j];
67*cdf0e10cSrcweir                     XAccessible xAccTarget =
68*cdf0e10cSrcweir                         (XAccessible)UnoRuntime.queryInterface(
69*cdf0e10cSrcweir                              XAccessible.class, aTarget );
70*cdf0e10cSrcweir                     if( xAccTarget == null )
71*cdf0e10cSrcweir                     {
72*cdf0e10cSrcweir                         aBuffer.append( aTarget.toString() );
73*cdf0e10cSrcweir                     }
74*cdf0e10cSrcweir                     else
75*cdf0e10cSrcweir                     {
76*cdf0e10cSrcweir                         aBuffer.append( xAccTarget.getAccessibleContext().
77*cdf0e10cSrcweir                                          getAccessibleName() );
78*cdf0e10cSrcweir                     }
79*cdf0e10cSrcweir                     aBuffer.append( ", " );
80*cdf0e10cSrcweir                 }
81*cdf0e10cSrcweir                 aBuffer.delete( aBuffer.length() - 2, aBuffer.length() );
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir                 aVNode.addChild( new StringNode( aBuffer.toString(),
84*cdf0e10cSrcweir                                                  aParent ) );
85*cdf0e10cSrcweir             }
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir             aChild = aVNode;
88*cdf0e10cSrcweir         }
89*cdf0e10cSrcweir         catch( IndexOutOfBoundsException e )
90*cdf0e10cSrcweir         {
91*cdf0e10cSrcweir             aChild = new StringNode( "IndexOutOfBounds", aParent );
92*cdf0e10cSrcweir         }
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir         return aChild;
95*cdf0e10cSrcweir     }
96*cdf0e10cSrcweir }
97