bump product version to 4.2.0.1
[LibreOffice.git] / toolkit / test / accessibility / ov / SelectionView.java
blobe6a06d7593c6c09400c3ead992468a8d9aa44027
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package ov;
21 import java.awt.event.ActionListener;
22 import java.awt.event.ActionEvent;
24 import java.awt.Dimension;
25 import java.awt.GridBagLayout;
26 import java.awt.GridBagConstraints;
28 import javax.swing.BoxLayout;
29 import javax.swing.ButtonGroup;
30 import javax.swing.JButton;
31 import javax.swing.JCheckBox;
32 import javax.swing.JLabel;
33 import javax.swing.JPanel;
34 import javax.swing.JRadioButton;
35 import javax.swing.JScrollPane;
36 import javax.swing.JToggleButton;
37 import com.sun.star.accessibility.AccessibleEventId;
38 import com.sun.star.accessibility.AccessibleEventObject;
39 import com.sun.star.accessibility.AccessibleStateType;
40 import com.sun.star.accessibility.XAccessible;
41 import com.sun.star.accessibility.XAccessibleContext;
42 import com.sun.star.accessibility.XAccessibleSelection;
43 import com.sun.star.accessibility.XAccessibleStateSet;
45 import com.sun.star.uno.UnoRuntime;
46 import com.sun.star.lang.IndexOutOfBoundsException;
49 /** Display a list of children and select/deselect buttons
51 class SelectionView
52 extends ListeningObjectView
53 implements ActionListener
55 static public ObjectView Create (
56 ObjectViewContainer aContainer,
57 XAccessibleContext xContext)
59 XAccessibleSelection xSelection = UnoRuntime.queryInterface(
60 XAccessibleSelection.class, xContext);
61 if (xSelection != null)
62 return new SelectionView(aContainer);
63 else
64 return null;
67 public SelectionView (ObjectViewContainer aContainer)
69 super (aContainer);
70 Layout();
73 public String GetTitle ()
75 return "Selection";
78 /** Create and arrange the widgets for this view.
80 private void Layout ()
82 setLayout (new GridBagLayout());
84 GridBagConstraints aConstraints = new GridBagConstraints();
86 // Label that shows wheter the selection is multi selectable.
87 aConstraints.gridx = 0;
88 aConstraints.gridy = 0;
89 aConstraints.anchor = GridBagConstraints.WEST;
90 maTypeLabel = new JLabel ();
91 add (maTypeLabel, aConstraints);
93 // the JListBox
94 maChildrenSelector = new JPanel ();
95 maChildrenSelector.setPreferredSize (new Dimension (100,100));
96 maChildrenSelector.setLayout (new BoxLayout (maChildrenSelector, BoxLayout.Y_AXIS));
98 aConstraints.gridx = 0;
99 aConstraints.gridwidth = 4;
100 aConstraints.gridy = 1;
101 aConstraints.fill = GridBagConstraints.HORIZONTAL;
102 add (new JScrollPane (maChildrenSelector,
103 JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
104 JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED),
105 aConstraints);
107 JButton aButton;
108 aButton = new JButton( "Select all" );
109 aButton.setActionCommand( "Select all" );
110 aButton.addActionListener( this );
111 aConstraints.gridx = 0;
112 aConstraints.gridwidth = 1;
113 aConstraints.gridy = 2;
114 aConstraints.fill = GridBagConstraints.NONE;
115 aConstraints.anchor = GridBagConstraints.WEST;
116 add (aButton, aConstraints);
118 aButton = new JButton( "Clear Selection" );
119 aButton.setActionCommand( "Clear Selection" );
120 aButton.addActionListener( this );
121 aConstraints.gridx = 1;
122 aConstraints.gridy = 2;
123 aConstraints.weightx = 1;
124 add (aButton, aConstraints);
126 setSize (getPreferredSize());
130 public void SetObject (XAccessibleContext xContext)
132 mxSelection = UnoRuntime.queryInterface(
133 XAccessibleSelection.class, xContext);
134 super.SetObject (xContext);
138 public void Update ()
140 maChildrenSelector.removeAll ();
142 // Determine whether multi selection is possible.
143 XAccessibleStateSet aStateSet = mxContext.getAccessibleStateSet();
144 boolean bMultiSelectable = false;
145 ButtonGroup aButtonGroup = null;
146 if (aStateSet!=null && aStateSet.contains(AccessibleStateType.MULTI_SELECTABLE))
148 bMultiSelectable = true;
149 maTypeLabel.setText ("multi selectable");
151 else
153 maTypeLabel.setText ("single selectable");
154 aButtonGroup = new ButtonGroup ();
157 int nCount = mxContext.getAccessibleChildCount();
158 for (int i=0; i<nCount; i++)
162 XAccessible xChild = mxContext.getAccessibleChild(i);
163 XAccessibleContext xChildContext = xChild.getAccessibleContext();
165 String sName = i + " " + xChildContext.getAccessibleName();
166 JToggleButton aChild;
167 if (bMultiSelectable)
168 aChild = new JCheckBox (sName);
169 else
171 aChild = new JRadioButton (sName);
172 aButtonGroup.add (aChild);
175 XAccessibleStateSet aChildStateSet = mxContext.getAccessibleStateSet();
176 aChild.setSelected (aChildStateSet!=null
177 && aChildStateSet.contains(AccessibleStateType.SELECTED));
179 aChild.addActionListener (this);
180 maChildrenSelector.add (aChild);
183 catch (IndexOutOfBoundsException e)
190 void SelectAll()
192 mxSelection.selectAllAccessibleChildren();
195 void ClearSelection()
197 mxSelection.clearAccessibleSelection();
201 /** Call the function associated with the pressed button.
203 public void actionPerformed (ActionEvent aEvent)
205 String sCommand = aEvent.getActionCommand();
207 if (sCommand.equals ("Clear Selection"))
208 ClearSelection();
209 else if (sCommand.equals ("Select all"))
210 SelectAll();
211 else
213 // Extract the child index from the widget text.
214 String[] aWords = sCommand.split (" ");
215 int nIndex = Integer.parseInt(aWords[0]);
218 if (((JToggleButton)aEvent.getSource()).isSelected())
219 mxSelection.selectAccessibleChild (nIndex);
220 else
221 mxSelection.deselectAccessibleChild (nIndex);
223 catch (IndexOutOfBoundsException e)
225 System.err.println ("caught exception while changing selection: " + e);
231 public void notifyEvent (AccessibleEventObject aEvent)
233 if (aEvent.EventId == AccessibleEventId.SELECTION_CHANGED)
234 Update ();
237 private JPanel maChildrenSelector;
238 private XAccessibleSelection mxSelection;
239 private JLabel maTypeLabel;