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 import com.sun.star.uno.UnoRuntime;
23 import com.sun.star.accessibility.XAccessible;
24 import com.sun.star.accessibility.XAccessibleContext;
25 import com.sun.star.accessibility.XAccessibleSelection;
26 import com.sun.star.lang.IndexOutOfBoundsException;
27 
28 import javax.swing.*;
29 import java.awt.*;
30 import java.util.Vector;
31 import java.awt.event.ActionListener;
32 import java.awt.event.ActionEvent;
33 
34 
35 
36 
37 /**
38  * Display a dialog with a list-box of children and select/deselect buttons
39  */
40 class SelectionDialog extends JDialog
41     implements ActionListener
42 {
SelectionDialog(AccTreeNode aNode)43     public SelectionDialog (AccTreeNode aNode)
44     {
45         super (AccessibilityWorkBench.Instance());
46 
47         maNode = aNode;
48 
49         Layout();
50     }
51 
52     /** build dialog */
Layout()53     protected void Layout ()
54     {
55         setTitle( "Select" );
56 
57         // vertical stacking of the elements
58         Container aContent = getContentPane();
59 
60         // label with explanation
61         aContent.add( new JLabel( "Select/Deselect child elements" ),
62                       BorderLayout.NORTH );
63 
64         // the JListBox
65         maChildrenSelector = new JList (GetChildrenList());
66         maChildrenSelector.setPreferredSize (new Dimension (500,300));
67         aContent.add (maChildrenSelector, BorderLayout.CENTER);
68         maChildrenSelector.setSelectionMode (ListSelectionModel.SINGLE_SELECTION);
69 
70         JPanel aButtons = new JPanel();
71         aButtons.setLayout( new FlowLayout() );
72 
73         JButton aButton;
74 
75         aButton = new JButton( "Select" );
76         aButton.setActionCommand( "Select" );
77         aButton.addActionListener( this );
78         aButtons.add( aButton );
79 
80         aButton = new JButton( "Deselect" );
81         aButton.setActionCommand( "Deselect" );
82         aButton.addActionListener( this );
83         aButtons.add( aButton );
84 
85         aButton = new JButton( "Select all" );
86         aButton.setActionCommand( "Select all" );
87         aButton.addActionListener( this );
88         aButtons.add( aButton );
89 
90         aButton = new JButton( "Clear Selection" );
91         aButton.setActionCommand( "Clear Selection" );
92         aButton.addActionListener( this );
93         aButtons.add( aButton );
94 
95         aButton = new JButton( "Close" );
96         aButton.setActionCommand( "Close" );
97         aButton.addActionListener( this );
98         aButtons.add( aButton );
99 
100         // add Panel with buttons
101         aContent.add( aButtons, BorderLayout.SOUTH );
102 
103         setSize( getPreferredSize() );
104     }
105 
106     /** Get a list of all children
107     */
GetChildrenList()108     private Vector GetChildrenList ()
109     {
110         mxSelection = maNode.getSelection();
111 
112         XAccessibleContext xContext = maNode.getContext();
113         int nCount = xContext.getAccessibleChildCount();
114         Vector aChildVector = new Vector();
115         for(int i = 0; i < nCount; i++)
116         {
117             try
118             {
119                 XAccessible xChild = xContext.getAccessibleChild(i);
120                 XAccessibleContext xChildContext = xChild.getAccessibleContext();
121                 aChildVector.add( i + " " + xChildContext.getAccessibleName());
122             }
123             catch( IndexOutOfBoundsException e )
124             {
125                 aChildVector.add( "ERROR: IndexOutOfBoundsException" );
126             }
127         }
128         return aChildVector;
129     }
130 
131 
close()132     void close ()
133     {
134         hide();
135         dispose();
136     }
137 
select()138     void select()
139     {
140         try
141         {
142             mxSelection.selectAccessibleChild (maChildrenSelector.getSelectedIndex());
143         }
144         catch( IndexOutOfBoundsException e )
145         {
146             JOptionPane.showMessageDialog( AccessibilityWorkBench.Instance(),
147                                            "Can't select: IndexOutofBounds",
148                                            "Error in selectAccessibleChild",
149                                            JOptionPane.ERROR_MESSAGE);
150         }
151     }
152 
deselect()153     void deselect()
154     {
155         try
156         {
157             mxSelection.deselectAccessibleChild(
158                 maChildrenSelector.getSelectedIndex());
159         }
160         catch( IndexOutOfBoundsException e )
161         {
162             JOptionPane.showMessageDialog( AccessibilityWorkBench.Instance(),
163                                            "Can't deselect: IndexOutofBounds",
164                                            "Error in deselectAccessibleChild",
165                                            JOptionPane.ERROR_MESSAGE);
166         }
167     }
168 
selectAll()169     void selectAll()
170     {
171         mxSelection.selectAllAccessibleChildren();
172     }
173 
clearSelection()174     void clearSelection()
175     {
176         mxSelection.clearAccessibleSelection();
177     }
178 
179 
180 
actionPerformed(ActionEvent e)181     public void actionPerformed(ActionEvent e)
182     {
183         String sCommand = e.getActionCommand();
184 
185         if( "Close".equals( sCommand ) )
186             close();
187         else if ( "Select".equals( sCommand ) )
188             select();
189         else if ( "Deselect".equals( sCommand ) )
190             deselect();
191         else if ( "Clear Selection".equals( sCommand ) )
192             clearSelection();
193         else if ( "Select all".equals( sCommand ) )
194             selectAll();
195     }
196 
197     private JList maChildrenSelector;
198     private XAccessibleSelection mxSelection;
199     private AccTreeNode maNode;
200 }
201