xref: /trunk/main/toolkit/test/accessibility/AccessibleSelectionHandler.java (revision 55adca3b5634862672eb3b8bd7f242c8984dfe41)
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 
23 import com.sun.star.uno.UnoRuntime;
24 import com.sun.star.accessibility.XAccessible;
25 import com.sun.star.accessibility.XAccessibleContext;
26 import com.sun.star.accessibility.XAccessibleSelection;
27 import com.sun.star.lang.IndexOutOfBoundsException;
28 
29 import javax.swing.*;
30 import java.awt.*;
31 import java.awt.event.ActionListener;
32 import java.awt.event.ActionEvent;
33 
34 
35 
36 class AccessibleSelectionHandler
37     extends NodeHandler
38 {
39     public NodeHandler createHandler( XAccessibleContext xContext )
40     {
41         XAccessibleSelection xSelection =
42             UnoRuntime.queryInterface(
43                 XAccessibleSelection.class, xContext);
44         return (xSelection == null) ? null :
45             new AccessibleSelectionHandler(xSelection);
46     }
47 
48     public AccessibleSelectionHandler()
49     {
50     }
51 
52     public AccessibleSelectionHandler( XAccessibleSelection xSelection )
53     {
54         if (xSelection != null)
55             maChildList.setSize( 2 );
56     }
57 
58     public AccessibleTreeNode createChild( AccessibleTreeNode aParent,
59                                            int nIndex )
60     {
61         AccessibleTreeNode aChild = null;
62 
63         if( aParent instanceof AccTreeNode )
64         {
65             XAccessibleSelection xSelection =
66                 ((AccTreeNode)aParent).getSelection();
67             if( xSelection != null )
68             {
69                 switch( nIndex )
70                 {
71                     case 0:
72                         aChild = new StringNode(
73                             "getSelectedAccessibleChildCount: " +
74                             xSelection.getSelectedAccessibleChildCount(),
75                             aParent );
76                         break;
77                     case 1:
78                     {
79                         VectorNode aVNode =
80                             new VectorNode( "Selected Children", aParent);
81                         int nSelected = 0;
82                         int nCount = ((AccTreeNode)aParent).getContext().
83                             getAccessibleChildCount();
84                         try
85                         {
86                             for( int i = 0; i < nCount; i++ )
87                             {
88                                 try
89                                 {
90                                     if( xSelection.isAccessibleChildSelected( i ) )
91                                     {
92                                         XAccessible xSelChild = xSelection.
93                                             getSelectedAccessibleChild(nSelected);
94                                         XAccessible xNChild =
95                                             ((AccTreeNode)aParent).
96                                             getContext().getAccessibleChild( i );
97                                         aVNode.addChild( new StringNode(
98                                             i + ": " +
99                                             xNChild.getAccessibleContext().
100                                             getAccessibleDescription() + " (" +
101                                             (xSelChild.equals(xNChild) ? "OK" : "XXX") +
102                                             ")", aParent ) );
103                                     }
104                                 }
105                                 catch (com.sun.star.lang.DisposedException e)
106                                 {
107                                     aVNode.addChild( new StringNode(
108                                         i + ": caught DisposedException while creating",
109                                         aParent ));
110                                 }
111                             }
112                             aChild = aVNode;
113                         }
114                         catch( IndexOutOfBoundsException e )
115                         {
116                             aChild = new StringNode( "IndexOutOfBounds",
117                                                      aParent );
118                         }
119                     }
120                     break;
121                     default:
122                         aChild = new StringNode( "ERROR", aParent );
123                         break;
124                 }
125             }
126         }
127 
128         return aChild;
129     }
130 
131 
132     public String[] getActions (AccessibleTreeNode aNode)
133     {
134         if( aNode instanceof AccTreeNode )
135         {
136             XAccessibleSelection xSelection =
137                 ((AccTreeNode)aNode).getSelection();
138             if( xSelection != null )
139             {
140                 return new String[] { "Select..." };
141             }
142         }
143         return new String[0];
144     }
145 
146     public void performAction (AccessibleTreeNode aNode, int nIndex)
147     {
148         new SelectionDialog( (AccTreeNode)aNode ).show();
149     }
150 }
151