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