merged tag ooo/DEV300_m102
[LibreOffice.git] / toolkit / test / accessibility / SelectionDialog.java
bloba632e9f77a0c5856486f31af926ce2b13adf2183
1 import com.sun.star.uno.UnoRuntime;
2 import com.sun.star.accessibility.XAccessible;
3 import com.sun.star.accessibility.XAccessibleContext;
4 import com.sun.star.accessibility.XAccessibleSelection;
5 import com.sun.star.lang.IndexOutOfBoundsException;
7 import javax.swing.*;
8 import java.awt.*;
9 import java.util.Vector;
10 import java.awt.event.ActionListener;
11 import java.awt.event.ActionEvent;
16 /**
17 * Display a dialog with a list-box of children and select/deselect buttons
19 class SelectionDialog extends JDialog
20 implements ActionListener
22 public SelectionDialog (AccTreeNode aNode)
24 super (AccessibilityWorkBench.Instance());
26 maNode = aNode;
28 Layout();
31 /** build dialog */
32 protected void Layout ()
34 setTitle( "Select" );
36 // vertical stacking of the elements
37 Container aContent = getContentPane();
39 // label with explanation
40 aContent.add( new JLabel( "Select/Deselect child elements" ),
41 BorderLayout.NORTH );
43 // the JListBox
44 maChildrenSelector = new JList (GetChildrenList());
45 maChildrenSelector.setPreferredSize (new Dimension (500,300));
46 aContent.add (maChildrenSelector, BorderLayout.CENTER);
47 maChildrenSelector.setSelectionMode (ListSelectionModel.SINGLE_SELECTION);
49 JPanel aButtons = new JPanel();
50 aButtons.setLayout( new FlowLayout() );
52 JButton aButton;
54 aButton = new JButton( "Select" );
55 aButton.setActionCommand( "Select" );
56 aButton.addActionListener( this );
57 aButtons.add( aButton );
59 aButton = new JButton( "Deselect" );
60 aButton.setActionCommand( "Deselect" );
61 aButton.addActionListener( this );
62 aButtons.add( aButton );
64 aButton = new JButton( "Select all" );
65 aButton.setActionCommand( "Select all" );
66 aButton.addActionListener( this );
67 aButtons.add( aButton );
69 aButton = new JButton( "Clear Selection" );
70 aButton.setActionCommand( "Clear Selection" );
71 aButton.addActionListener( this );
72 aButtons.add( aButton );
74 aButton = new JButton( "Close" );
75 aButton.setActionCommand( "Close" );
76 aButton.addActionListener( this );
77 aButtons.add( aButton );
79 // add Panel with buttons
80 aContent.add( aButtons, BorderLayout.SOUTH );
82 setSize( getPreferredSize() );
85 /** Get a list of all children
87 private Vector GetChildrenList ()
89 mxSelection = maNode.getSelection();
91 XAccessibleContext xContext = maNode.getContext();
92 int nCount = xContext.getAccessibleChildCount();
93 Vector aChildVector = new Vector();
94 for(int i = 0; i < nCount; i++)
96 try
98 XAccessible xChild = xContext.getAccessibleChild(i);
99 XAccessibleContext xChildContext = xChild.getAccessibleContext();
100 aChildVector.add( i + " " + xChildContext.getAccessibleName());
102 catch( IndexOutOfBoundsException e )
104 aChildVector.add( "ERROR: IndexOutOfBoundsException" );
107 return aChildVector;
111 void close ()
113 hide();
114 dispose();
117 void select()
121 mxSelection.selectAccessibleChild (maChildrenSelector.getSelectedIndex());
123 catch( IndexOutOfBoundsException e )
125 JOptionPane.showMessageDialog( AccessibilityWorkBench.Instance(),
126 "Can't select: IndexOutofBounds",
127 "Error in selectAccessibleChild",
128 JOptionPane.ERROR_MESSAGE);
132 void deselect()
136 mxSelection.deselectAccessibleChild(
137 maChildrenSelector.getSelectedIndex());
139 catch( IndexOutOfBoundsException e )
141 JOptionPane.showMessageDialog( AccessibilityWorkBench.Instance(),
142 "Can't deselect: IndexOutofBounds",
143 "Error in deselectAccessibleChild",
144 JOptionPane.ERROR_MESSAGE);
148 void selectAll()
150 mxSelection.selectAllAccessibleChildren();
153 void clearSelection()
155 mxSelection.clearAccessibleSelection();
160 public void actionPerformed(ActionEvent e)
162 String sCommand = e.getActionCommand();
164 if( "Close".equals( sCommand ) )
165 close();
166 else if ( "Select".equals( sCommand ) )
167 select();
168 else if ( "Deselect".equals( sCommand ) )
169 deselect();
170 else if ( "Clear Selection".equals( sCommand ) )
171 clearSelection();
172 else if ( "Select all".equals( sCommand ) )
173 selectAll();
176 private JList maChildrenSelector;
177 private XAccessibleSelection mxSelection;
178 private AccTreeNode maNode;