summaryrefslogtreecommitdiff
path: root/toolkit/test/accessibility/AccessibleSelectionHandler.java
blob: 4a8cd24ba9c9fda606589077c8c8c4bac212a51d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

import com.sun.star.uno.UnoRuntime;
import com.sun.star.accessibility.XAccessible;
import com.sun.star.accessibility.XAccessibleContext;
import com.sun.star.accessibility.XAccessibleSelection;
import com.sun.star.lang.IndexOutOfBoundsException;



class AccessibleSelectionHandler
    extends NodeHandler
{
    @Override
    public NodeHandler createHandler( XAccessibleContext xContext )
    {
        XAccessibleSelection xSelection =
            UnoRuntime.queryInterface(
            XAccessibleSelection.class, xContext);
        return (xSelection == null) ? null :
            new AccessibleSelectionHandler(xSelection);
    }

    public AccessibleSelectionHandler()
    {
    }

    private AccessibleSelectionHandler( XAccessibleSelection xSelection )
    {
        if (xSelection != null)
            maChildList.setSize( 2 );
    }

    @Override
    public AccessibleTreeNode createChild( AccessibleTreeNode aParent,
                                           int nIndex )
    {
        if( !(aParent instanceof AccTreeNode) )
            return null;

        XAccessibleSelection xSelection = ((AccTreeNode)aParent).getSelection();
        if( xSelection == null )
            return null;

        AccessibleTreeNode aChild = null;

        switch( nIndex )
        {
            case 0:
                aChild = new StringNode(
                    "getSelectedAccessibleChildCount: " +
                    xSelection.getSelectedAccessibleChildCount(),
                    aParent );
                break;
            case 1:
            {
                VectorNode aVNode =
                    new VectorNode( "Selected Children", aParent);
                int nSelected = 0;
                int nCount = ((AccTreeNode)aParent).getContext().
                    getAccessibleChildCount();
                try
                {
                    for( int i = 0; i < nCount; i++ )
                    {
                        try
                        {
                            if( xSelection.isAccessibleChildSelected( i ) )
                            {
                                XAccessible xSelChild = xSelection.
                                    getSelectedAccessibleChild(nSelected);
                                XAccessible xNChild =
                                    ((AccTreeNode)aParent).
                                    getContext().getAccessibleChild( i );
                                aVNode.addChild( new StringNode(
                                    i + ": " +
                                    xNChild.getAccessibleContext().
                                    getAccessibleDescription() + " (" +
                                    (xSelChild.equals(xNChild) ? "OK" : "XXX") +
                                    ")", aParent ) );
                            }
                        }
                        catch (com.sun.star.lang.DisposedException e)
                        {
                            aVNode.addChild( new StringNode(
                                i + ": caught DisposedException while creating",
                                aParent ));
                        }
                    }
                    aChild = aVNode;
                }
                catch( IndexOutOfBoundsException e )
                {
                    aChild = new StringNode( "IndexOutOfBounds",
                                             aParent );
                }
            }
            break;
            default:
                aChild = new StringNode( "ERROR", aParent );
                break;
        }

        return aChild;
    }


    @Override
    public String[] getActions (AccessibleTreeNode aNode)
    {
        if( aNode instanceof AccTreeNode )
        {
            XAccessibleSelection xSelection =
                ((AccTreeNode)aNode).getSelection();
            if( xSelection != null )
            {
                return new String[] { "Select..." };
            }
        }
        return new String[0];
    }

    @Override
    public void performAction (AccessibleTreeNode aNode, int nIndex)
    {
        SelectionDialog selectionDialog = new SelectionDialog( (AccTreeNode)aNode );
        selectionDialog.setVisible(true);
    }
}