merged tag ooo/DEV300_m102
[LibreOffice.git] / toolkit / test / accessibility / ov / SelectionView.java
bloba71f242420ba0c5254784ab9e02c0ec85cb2439a
1 package ov;
3 import java.util.Vector;
5 import java.awt.event.ActionListener;
6 import java.awt.event.ActionEvent;
8 import java.awt.BorderLayout;
9 import java.awt.Dimension;
10 import java.awt.GridBagLayout;
11 import java.awt.GridBagConstraints;
13 import javax.swing.BoxLayout;
14 import javax.swing.ButtonGroup;
15 import javax.swing.JButton;
16 import javax.swing.JCheckBox;
17 import javax.swing.JLabel;
18 import javax.swing.JList;
19 import javax.swing.JPanel;
20 import javax.swing.JOptionPane;
21 import javax.swing.JRadioButton;
22 import javax.swing.JScrollPane;
23 import javax.swing.JToggleButton;
24 import javax.swing.ListSelectionModel;
27 import com.sun.star.accessibility.AccessibleEventId;
28 import com.sun.star.accessibility.AccessibleEventObject;
29 import com.sun.star.accessibility.AccessibleStateType;
30 import com.sun.star.accessibility.XAccessible;
31 import com.sun.star.accessibility.XAccessibleContext;
32 import com.sun.star.accessibility.XAccessibleSelection;
33 import com.sun.star.accessibility.XAccessibleStateSet;
35 import com.sun.star.uno.UnoRuntime;
36 import com.sun.star.lang.IndexOutOfBoundsException;
39 /** Display a list of children and select/deselect buttons
41 class SelectionView
42 extends ListeningObjectView
43 implements ActionListener
45 static public ObjectView Create (
46 ObjectViewContainer aContainer,
47 XAccessibleContext xContext)
49 XAccessibleSelection xSelection = (XAccessibleSelection)UnoRuntime.queryInterface(
50 XAccessibleSelection.class, xContext);
51 if (xSelection != null)
52 return new SelectionView(aContainer);
53 else
54 return null;
57 public SelectionView (ObjectViewContainer aContainer)
59 super (aContainer);
60 Layout();
63 public String GetTitle ()
65 return "Selection";
68 /** Create and arrange the widgets for this view.
70 private void Layout ()
72 setLayout (new GridBagLayout());
74 GridBagConstraints aConstraints = new GridBagConstraints();
76 // Label that shows wheter the selection is multi selectable.
77 aConstraints.gridx = 0;
78 aConstraints.gridy = 0;
79 aConstraints.anchor = GridBagConstraints.WEST;
80 maTypeLabel = new JLabel ();
81 add (maTypeLabel, aConstraints);
83 // the JListBox
84 maChildrenSelector = new JPanel ();
85 maChildrenSelector.setPreferredSize (new Dimension (100,100));
86 maChildrenSelector.setLayout (new BoxLayout (maChildrenSelector, BoxLayout.Y_AXIS));
88 aConstraints.gridx = 0;
89 aConstraints.gridwidth = 4;
90 aConstraints.gridy = 1;
91 aConstraints.fill = GridBagConstraints.HORIZONTAL;
92 add (new JScrollPane (maChildrenSelector,
93 JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
94 JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED),
95 aConstraints);
97 JButton aButton;
98 aButton = new JButton( "Select all" );
99 aButton.setActionCommand( "Select all" );
100 aButton.addActionListener( this );
101 aConstraints.gridx = 0;
102 aConstraints.gridwidth = 1;
103 aConstraints.gridy = 2;
104 aConstraints.fill = GridBagConstraints.NONE;
105 aConstraints.anchor = GridBagConstraints.WEST;
106 add (aButton, aConstraints);
108 aButton = new JButton( "Clear Selection" );
109 aButton.setActionCommand( "Clear Selection" );
110 aButton.addActionListener( this );
111 aConstraints.gridx = 1;
112 aConstraints.gridy = 2;
113 aConstraints.weightx = 1;
114 add (aButton, aConstraints);
116 setSize (getPreferredSize());
120 public void SetObject (XAccessibleContext xContext)
122 mxSelection = (XAccessibleSelection)UnoRuntime.queryInterface(
123 XAccessibleSelection.class, xContext);
124 super.SetObject (xContext);
128 public void Update ()
130 maChildrenSelector.removeAll ();
132 // Determine whether multi selection is possible.
133 XAccessibleStateSet aStateSet = mxContext.getAccessibleStateSet();
134 boolean bMultiSelectable = false;
135 ButtonGroup aButtonGroup = null;
136 if (aStateSet!=null && aStateSet.contains(AccessibleStateType.MULTI_SELECTABLE))
138 bMultiSelectable = true;
139 maTypeLabel.setText ("multi selectable");
141 else
143 maTypeLabel.setText ("single selectable");
144 aButtonGroup = new ButtonGroup ();
147 int nCount = mxContext.getAccessibleChildCount();
148 for (int i=0; i<nCount; i++)
152 XAccessible xChild = mxContext.getAccessibleChild(i);
153 XAccessibleContext xChildContext = xChild.getAccessibleContext();
155 String sName = i + " " + xChildContext.getAccessibleName();
156 JToggleButton aChild;
157 if (bMultiSelectable)
158 aChild = new JCheckBox (sName);
159 else
161 aChild = new JRadioButton (sName);
162 aButtonGroup.add (aChild);
165 XAccessibleStateSet aChildStateSet = mxContext.getAccessibleStateSet();
166 aChild.setSelected (aChildStateSet!=null
167 && aChildStateSet.contains(AccessibleStateType.SELECTED));
169 aChild.addActionListener (this);
170 maChildrenSelector.add (aChild);
173 catch (IndexOutOfBoundsException e)
180 void SelectAll()
182 mxSelection.selectAllAccessibleChildren();
185 void ClearSelection()
187 mxSelection.clearAccessibleSelection();
191 /** Call the function associated with the pressed button.
193 public void actionPerformed (ActionEvent aEvent)
195 String sCommand = aEvent.getActionCommand();
197 if (sCommand.equals ("Clear Selection"))
198 ClearSelection();
199 else if (sCommand.equals ("Select all"))
200 SelectAll();
201 else
203 // Extract the child index from the widget text.
204 String[] aWords = sCommand.split (" ");
205 int nIndex = Integer.parseInt(aWords[0]);
208 if (((JToggleButton)aEvent.getSource()).isSelected())
209 mxSelection.selectAccessibleChild (nIndex);
210 else
211 mxSelection.deselectAccessibleChild (nIndex);
213 catch (IndexOutOfBoundsException e)
215 System.err.println ("caught exception while changing selection: " + e);
221 public void notifyEvent (AccessibleEventObject aEvent)
223 if (aEvent.EventId == AccessibleEventId.SELECTION_CHANGED)
224 Update ();
227 private JPanel maChildrenSelector;
228 private XAccessibleSelection mxSelection;
229 private JLabel maTypeLabel;