1*1b0aaa91SAndrew Rist /**************************************************************
2*1b0aaa91SAndrew Rist  *
3*1b0aaa91SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*1b0aaa91SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*1b0aaa91SAndrew Rist  * distributed with this work for additional information
6*1b0aaa91SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*1b0aaa91SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*1b0aaa91SAndrew Rist  * "License"); you may not use this file except in compliance
9*1b0aaa91SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*1b0aaa91SAndrew Rist  *
11*1b0aaa91SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*1b0aaa91SAndrew Rist  *
13*1b0aaa91SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*1b0aaa91SAndrew Rist  * software distributed under the License is distributed on an
15*1b0aaa91SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*1b0aaa91SAndrew Rist  * KIND, either express or implied.  See the License for the
17*1b0aaa91SAndrew Rist  * specific language governing permissions and limitations
18*1b0aaa91SAndrew Rist  * under the License.
19*1b0aaa91SAndrew Rist  *
20*1b0aaa91SAndrew Rist  *************************************************************/
21*1b0aaa91SAndrew Rist 
22cdf0e10cSrcweir 
23cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
24cdf0e10cSrcweir import com.sun.star.accessibility.XAccessible;
25cdf0e10cSrcweir import com.sun.star.accessibility.XAccessibleContext;
26cdf0e10cSrcweir import com.sun.star.accessibility.XAccessibleSelection;
27cdf0e10cSrcweir import com.sun.star.lang.IndexOutOfBoundsException;
28cdf0e10cSrcweir 
29cdf0e10cSrcweir import javax.swing.*;
30cdf0e10cSrcweir import java.awt.*;
31cdf0e10cSrcweir import java.util.Vector;
32cdf0e10cSrcweir import java.awt.event.ActionListener;
33cdf0e10cSrcweir import java.awt.event.ActionEvent;
34cdf0e10cSrcweir 
35cdf0e10cSrcweir 
36cdf0e10cSrcweir 
37cdf0e10cSrcweir class AccessibleSelectionHandler
38cdf0e10cSrcweir     extends NodeHandler
39cdf0e10cSrcweir {
createHandler( XAccessibleContext xContext )40cdf0e10cSrcweir     public NodeHandler createHandler( XAccessibleContext xContext )
41cdf0e10cSrcweir     {
42cdf0e10cSrcweir         XAccessibleSelection xSelection =
43cdf0e10cSrcweir             (XAccessibleSelection) UnoRuntime.queryInterface(
44cdf0e10cSrcweir                 XAccessibleSelection.class, xContext);
45cdf0e10cSrcweir         return (xSelection == null) ? null :
46cdf0e10cSrcweir             new AccessibleSelectionHandler(xSelection);
47cdf0e10cSrcweir     }
48cdf0e10cSrcweir 
AccessibleSelectionHandler()49cdf0e10cSrcweir     public AccessibleSelectionHandler()
50cdf0e10cSrcweir     {
51cdf0e10cSrcweir     }
52cdf0e10cSrcweir 
AccessibleSelectionHandler( XAccessibleSelection xSelection )53cdf0e10cSrcweir     public AccessibleSelectionHandler( XAccessibleSelection xSelection )
54cdf0e10cSrcweir     {
55cdf0e10cSrcweir         if (xSelection != null)
56cdf0e10cSrcweir             maChildList.setSize( 2 );
57cdf0e10cSrcweir     }
58cdf0e10cSrcweir 
createChild( AccessibleTreeNode aParent, int nIndex )59cdf0e10cSrcweir     public AccessibleTreeNode createChild( AccessibleTreeNode aParent,
60cdf0e10cSrcweir                                            int nIndex )
61cdf0e10cSrcweir     {
62cdf0e10cSrcweir         AccessibleTreeNode aChild = null;
63cdf0e10cSrcweir 
64cdf0e10cSrcweir         if( aParent instanceof AccTreeNode )
65cdf0e10cSrcweir         {
66cdf0e10cSrcweir             XAccessibleSelection xSelection =
67cdf0e10cSrcweir                 ((AccTreeNode)aParent).getSelection();
68cdf0e10cSrcweir             if( xSelection != null )
69cdf0e10cSrcweir             {
70cdf0e10cSrcweir                 switch( nIndex )
71cdf0e10cSrcweir                 {
72cdf0e10cSrcweir                     case 0:
73cdf0e10cSrcweir                         aChild = new StringNode(
74cdf0e10cSrcweir                             "getSelectedAccessibleChildCount: " +
75cdf0e10cSrcweir                             xSelection.getSelectedAccessibleChildCount(),
76cdf0e10cSrcweir                             aParent );
77cdf0e10cSrcweir                         break;
78cdf0e10cSrcweir                     case 1:
79cdf0e10cSrcweir                     {
80cdf0e10cSrcweir                         VectorNode aVNode =
81cdf0e10cSrcweir                             new VectorNode( "Selected Children", aParent);
82cdf0e10cSrcweir                         int nSelected = 0;
83cdf0e10cSrcweir                         int nCount = ((AccTreeNode)aParent).getContext().
84cdf0e10cSrcweir                             getAccessibleChildCount();
85cdf0e10cSrcweir                         try
86cdf0e10cSrcweir                         {
87cdf0e10cSrcweir                             for( int i = 0; i < nCount; i++ )
88cdf0e10cSrcweir                             {
89cdf0e10cSrcweir                                 try
90cdf0e10cSrcweir                                 {
91cdf0e10cSrcweir                                     if( xSelection.isAccessibleChildSelected( i ) )
92cdf0e10cSrcweir                                     {
93cdf0e10cSrcweir                                         XAccessible xSelChild = xSelection.
94cdf0e10cSrcweir                                             getSelectedAccessibleChild(nSelected);
95cdf0e10cSrcweir                                         XAccessible xNChild =
96cdf0e10cSrcweir                                             ((AccTreeNode)aParent).
97cdf0e10cSrcweir                                             getContext().getAccessibleChild( i );
98cdf0e10cSrcweir                                         aVNode.addChild( new StringNode(
99cdf0e10cSrcweir                                             i + ": " +
100cdf0e10cSrcweir                                             xNChild.getAccessibleContext().
101cdf0e10cSrcweir                                             getAccessibleDescription() + " (" +
102cdf0e10cSrcweir                                             (xSelChild.equals(xNChild) ? "OK" : "XXX") +
103cdf0e10cSrcweir                                             ")", aParent ) );
104cdf0e10cSrcweir                                     }
105cdf0e10cSrcweir                                 }
106cdf0e10cSrcweir                                 catch (com.sun.star.lang.DisposedException e)
107cdf0e10cSrcweir                                 {
108cdf0e10cSrcweir                                     aVNode.addChild( new StringNode(
109cdf0e10cSrcweir                                         i + ": caught DisposedException while creating",
110cdf0e10cSrcweir                                         aParent ));
111cdf0e10cSrcweir                                 }
112cdf0e10cSrcweir                             }
113cdf0e10cSrcweir                             aChild = aVNode;
114cdf0e10cSrcweir                         }
115cdf0e10cSrcweir                         catch( IndexOutOfBoundsException e )
116cdf0e10cSrcweir                         {
117cdf0e10cSrcweir                             aChild = new StringNode( "IndexOutOfBounds",
118cdf0e10cSrcweir                                                      aParent );
119cdf0e10cSrcweir                         }
120cdf0e10cSrcweir                     }
121cdf0e10cSrcweir                     break;
122cdf0e10cSrcweir                     default:
123cdf0e10cSrcweir                         aChild = new StringNode( "ERROR", aParent );
124cdf0e10cSrcweir                         break;
125cdf0e10cSrcweir                 }
126cdf0e10cSrcweir             }
127cdf0e10cSrcweir         }
128cdf0e10cSrcweir 
129cdf0e10cSrcweir         return aChild;
130cdf0e10cSrcweir     }
131cdf0e10cSrcweir 
132cdf0e10cSrcweir 
getActions(AccessibleTreeNode aNode)133cdf0e10cSrcweir     public String[] getActions (AccessibleTreeNode aNode)
134cdf0e10cSrcweir     {
135cdf0e10cSrcweir         if( aNode instanceof AccTreeNode )
136cdf0e10cSrcweir         {
137cdf0e10cSrcweir             XAccessibleSelection xSelection =
138cdf0e10cSrcweir                 ((AccTreeNode)aNode).getSelection();
139cdf0e10cSrcweir             if( xSelection != null )
140cdf0e10cSrcweir             {
141cdf0e10cSrcweir                 return new String[] { "Select..." };
142cdf0e10cSrcweir             }
143cdf0e10cSrcweir         }
144cdf0e10cSrcweir         return new String[0];
145cdf0e10cSrcweir     }
146cdf0e10cSrcweir 
performAction(AccessibleTreeNode aNode, int nIndex)147cdf0e10cSrcweir     public void performAction (AccessibleTreeNode aNode, int nIndex)
148cdf0e10cSrcweir     {
149cdf0e10cSrcweir         new SelectionDialog( (AccTreeNode)aNode ).show();
150cdf0e10cSrcweir     }
151cdf0e10cSrcweir }
152