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