summaryrefslogtreecommitdiff
path: root/toolkit/test/accessibility/AccessibleSelectionHandler.java
blob: 62afb2546f9200f1baff8ae26e19a7419cc2a873 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289

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

import javax.swing.*;
import java.awt.*;
import java.util.Vector;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


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

    public AccessibleSelectionHandler()
    {
    }

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

    public AccessibleTreeNode createChild( AccessibleTreeNode aParent,
                                           int nIndex )
    {
        AccessibleTreeNode aChild = null;

        if( aParent instanceof AccTreeNode )
        {
            XAccessibleSelection xSelection =
                ((AccTreeNode)aParent).getSelection();
            if( xSelection != 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++ )
                            {
                                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 ) );
                                }
                            }
                            aChild = aVNode;
                        }
                        catch( IndexOutOfBoundsException e )
                        {
                            aChild = new StringNode( "IndexOutOfBounds",
                                                     aParent );
                        }
                    }
                    break;
                    default:
                        aChild = new StringNode( "ERROR", aParent );
                        break;
                }
            }
        }

        return aChild;
    }


    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];
    }

    public void performAction (AccessibleTreeNode aNode, int nIndex)
    {
        new SelectionDialog( (AccTreeNode)aNode ).show();
    }
}


//     virtual void SAL_CALL selectAccessibleChild(
//         sal_Int32 nChildIndex )
//         throw ( ::com::sun::star::lang::IndexOutOfBoundsException,
//                 ::com::sun::star::uno::RuntimeException );
//     virtual void SAL_CALL clearAccessibleSelection(  )
//         throw ( ::com::sun::star::uno::RuntimeException );
//     virtual void SAL_CALL selectAllAccessible(  )
//         throw ( ::com::sun::star::uno::RuntimeException );
//         throw ( ::com::sun::star::uno::RuntimeException );
//     virtual void SAL_CALL deselectSelectedAccessibleChild(



/**
 * Display a dialog with a list-box of children and select/deselect buttons
 */
class SelectionDialog extends JDialog
    implements ActionListener
{
    JList aChildren;
    XAccessibleSelection xSelection;

    public SelectionDialog( AccTreeNode aNode )
    {
        super( AccessibilityWorkBench.get() );

        xSelection = aNode.getSelection();

        XAccessibleContext xContext = aNode.getContext();
        int nCount = xContext.getAccessibleChildCount();
        Vector aChildVector = new Vector();
        for(int i = 0; i < nCount; i++)
        {
            try
            {
                aChildVector.add(
                    xContext.getAccessibleChild(i).
                    getAccessibleContext().getAccessibleDescription() );
            }
            catch( IndexOutOfBoundsException e )
            {
                aChildVector.add( "ERROR: IndexOutOfBoundsException" );
            }
        }

        aChildren = new JList( aChildVector );

        init();
    }

    /** build dialog */
    protected void init()
    {
        setTitle( "Select" );

        // vertical stacking of the elements
        Container aContent = getContentPane();

        // label with explanation
        aContent.add( new JLabel( "Select/Deselect child elements" ),
                      BorderLayout.NORTH );

        // the JListBox
        aContent.add( aChildren, BorderLayout.CENTER );
        aChildren.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );

        JPanel aButtons = new JPanel();
        aButtons.setLayout( new FlowLayout() );

        JButton aButton;

        aButton = new JButton( "Select" );
        aButton.setActionCommand( "Select" );
        aButton.addActionListener( this );
        aButtons.add( aButton );

        aButton = new JButton( "Deselect" );
        aButton.setActionCommand( "Deselect" );
        aButton.addActionListener( this );
        aButtons.add( aButton );

        aButton = new JButton( "Select all" );
        aButton.setActionCommand( "Select all" );
        aButton.addActionListener( this );
        aButtons.add( aButton );

        aButton = new JButton( "Clear Selection" );
        aButton.setActionCommand( "Clear Selection" );
        aButton.addActionListener( this );
        aButtons.add( aButton );

        aButton = new JButton( "Cancel" );
        aButton.setActionCommand( "Cancel" );
        aButton.addActionListener( this );
        aButtons.add( aButton );

        // add Panel with buttons
        aContent.add( aButtons, BorderLayout.SOUTH );

        setSize( getPreferredSize() );
    }

    void cancel()
    {
        hide();
        dispose();
    }

    void select()
    {
        try
        {
            xSelection.selectAccessibleChild( aChildren.getSelectedIndex() );
        }
        catch( IndexOutOfBoundsException e )
        {
            JOptionPane.showMessageDialog( AccessibilityWorkBench.get(),
                                           "Can't select: IndexOutofBounds",
                                           "Error in selectAccessibleChild",
                                           JOptionPane.ERROR_MESSAGE);
        }
    }

    void deselect()
    {
        try
        {
            xSelection.deselectSelectedAccessibleChild(
                aChildren.getSelectedIndex() );
        }
        catch( IndexOutOfBoundsException e )
        {
            JOptionPane.showMessageDialog( AccessibilityWorkBench.get(),
                                           "Can't deselect: IndexOutofBounds",
                                           "Error in deselectAccessibleChild",
                                           JOptionPane.ERROR_MESSAGE);
        }
    }

    void selectAll()
    {
        xSelection.selectAllAccessible();
    }

    void clearSelection()
    {
        xSelection.clearAccessibleSelection();
    }



    public void actionPerformed(ActionEvent e)
    {
        String sCommand = e.getActionCommand();

        if( "Cancel".equals( sCommand ) )
            cancel();
        else if ( "Select".equals( sCommand ) )
            select();
        else if ( "Deselect".equals( sCommand ) )
            deselect();
        else if ( "Clear Selection".equals( sCommand ) )
            clearSelection();
        else if ( "Select all".equals( sCommand ) )
            selectAll();
    }
}