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 package ov;
23 
24 import java.util.Vector;
25 
26 import java.awt.event.ActionListener;
27 import java.awt.event.ActionEvent;
28 
29 import java.awt.BorderLayout;
30 import java.awt.Dimension;
31 import java.awt.GridBagLayout;
32 import java.awt.GridBagConstraints;
33 
34 import javax.swing.BoxLayout;
35 import javax.swing.ButtonGroup;
36 import javax.swing.JButton;
37 import javax.swing.JCheckBox;
38 import javax.swing.JLabel;
39 import javax.swing.JList;
40 import javax.swing.JPanel;
41 import javax.swing.JOptionPane;
42 import javax.swing.JRadioButton;
43 import javax.swing.JScrollPane;
44 import javax.swing.JToggleButton;
45 import javax.swing.ListSelectionModel;
46 
47 
48 import com.sun.star.accessibility.AccessibleEventId;
49 import com.sun.star.accessibility.AccessibleEventObject;
50 import com.sun.star.accessibility.AccessibleStateType;
51 import com.sun.star.accessibility.XAccessible;
52 import com.sun.star.accessibility.XAccessibleContext;
53 import com.sun.star.accessibility.XAccessibleSelection;
54 import com.sun.star.accessibility.XAccessibleStateSet;
55 
56 import com.sun.star.uno.UnoRuntime;
57 import com.sun.star.lang.IndexOutOfBoundsException;
58 
59 
60 /** Display a list of children and select/deselect buttons
61 */
62 class SelectionView
63     extends ListeningObjectView
64     implements ActionListener
65 {
Create( ObjectViewContainer aContainer, XAccessibleContext xContext)66     static public ObjectView Create (
67         ObjectViewContainer aContainer,
68         XAccessibleContext xContext)
69     {
70         XAccessibleSelection xSelection = (XAccessibleSelection)UnoRuntime.queryInterface(
71                 XAccessibleSelection.class, xContext);
72         if (xSelection != null)
73             return new SelectionView(aContainer);
74         else
75             return null;
76     }
77 
SelectionView(ObjectViewContainer aContainer)78     public SelectionView (ObjectViewContainer aContainer)
79     {
80         super (aContainer);
81         Layout();
82     }
83 
GetTitle()84     public String GetTitle ()
85     {
86         return "Selection";
87     }
88 
89     /** Create and arrange the widgets for this view.
90     */
Layout()91     private void Layout ()
92     {
93         setLayout (new GridBagLayout());
94 
95         GridBagConstraints aConstraints = new GridBagConstraints();
96 
97         // Label that shows wheter the selection is multi selectable.
98         aConstraints.gridx = 0;
99         aConstraints.gridy = 0;
100         aConstraints.anchor = GridBagConstraints.WEST;
101         maTypeLabel = new JLabel ();
102         add (maTypeLabel, aConstraints);
103 
104         // the JListBox
105         maChildrenSelector = new JPanel ();
106         maChildrenSelector.setPreferredSize (new Dimension (100,100));
107         maChildrenSelector.setLayout (new BoxLayout (maChildrenSelector, BoxLayout.Y_AXIS));
108 
109         aConstraints.gridx = 0;
110         aConstraints.gridwidth = 4;
111         aConstraints.gridy = 1;
112         aConstraints.fill = GridBagConstraints.HORIZONTAL;
113         add (new JScrollPane (maChildrenSelector,
114                  JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
115                  JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED),
116             aConstraints);
117 
118         JButton aButton;
119         aButton = new JButton( "Select all" );
120         aButton.setActionCommand( "Select all" );
121         aButton.addActionListener( this );
122         aConstraints.gridx = 0;
123         aConstraints.gridwidth = 1;
124         aConstraints.gridy = 2;
125         aConstraints.fill = GridBagConstraints.NONE;
126         aConstraints.anchor = GridBagConstraints.WEST;
127         add (aButton, aConstraints);
128 
129         aButton = new JButton( "Clear Selection" );
130         aButton.setActionCommand( "Clear Selection" );
131         aButton.addActionListener( this );
132         aConstraints.gridx = 1;
133         aConstraints.gridy = 2;
134         aConstraints.weightx = 1;
135         add (aButton, aConstraints);
136 
137         setSize (getPreferredSize());
138     }
139 
140 
SetObject(XAccessibleContext xContext)141     public void SetObject (XAccessibleContext xContext)
142     {
143         mxSelection = (XAccessibleSelection)UnoRuntime.queryInterface(
144             XAccessibleSelection.class, xContext);
145         super.SetObject (xContext);
146     }
147 
148 
Update()149     public void Update ()
150     {
151         maChildrenSelector.removeAll ();
152 
153         // Determine whether multi selection is possible.
154         XAccessibleStateSet aStateSet = mxContext.getAccessibleStateSet();
155         boolean bMultiSelectable = false;
156         ButtonGroup aButtonGroup = null;
157         if (aStateSet!=null && aStateSet.contains(AccessibleStateType.MULTI_SELECTABLE))
158         {
159             bMultiSelectable = true;
160             maTypeLabel.setText ("multi selectable");
161         }
162         else
163         {
164             maTypeLabel.setText ("single selectable");
165             aButtonGroup = new ButtonGroup ();
166         }
167 
168         int nCount = mxContext.getAccessibleChildCount();
169         for (int i=0; i<nCount; i++)
170         {
171             try
172             {
173                 XAccessible xChild = mxContext.getAccessibleChild(i);
174                 XAccessibleContext xChildContext = xChild.getAccessibleContext();
175 
176                 String sName = i + " " + xChildContext.getAccessibleName();
177                 JToggleButton aChild;
178                 if (bMultiSelectable)
179                     aChild = new JCheckBox (sName);
180                 else
181                 {
182                     aChild = new JRadioButton (sName);
183                     aButtonGroup.add (aChild);
184                 }
185 
186                 XAccessibleStateSet aChildStateSet = mxContext.getAccessibleStateSet();
187                 aChild.setSelected (aChildStateSet!=null
188                     && aChildStateSet.contains(AccessibleStateType.SELECTED));
189 
190                 aChild.addActionListener (this);
191                 maChildrenSelector.add (aChild);
192 
193             }
194             catch (IndexOutOfBoundsException e)
195             {
196             }
197         }
198     }
199 
200 
SelectAll()201     void SelectAll()
202     {
203         mxSelection.selectAllAccessibleChildren();
204     }
205 
ClearSelection()206     void ClearSelection()
207     {
208         mxSelection.clearAccessibleSelection();
209     }
210 
211 
212     /** Call the function associated with the pressed button.
213     */
actionPerformed(ActionEvent aEvent)214     public void actionPerformed (ActionEvent aEvent)
215     {
216         String sCommand = aEvent.getActionCommand();
217 
218         if (sCommand.equals ("Clear Selection"))
219             ClearSelection();
220         else if (sCommand.equals ("Select all"))
221             SelectAll();
222         else
223         {
224             // Extract the child index from the widget text.
225             String[] aWords = sCommand.split (" ");
226             int nIndex = Integer.parseInt(aWords[0]);
227             try
228             {
229                 if (((JToggleButton)aEvent.getSource()).isSelected())
230                     mxSelection.selectAccessibleChild (nIndex);
231                 else
232                     mxSelection.deselectAccessibleChild (nIndex);
233             }
234             catch (IndexOutOfBoundsException e)
235             {
236                 System.err.println ("caught exception while changing selection: " + e);
237             }
238         }
239     }
240 
241 
notifyEvent(AccessibleEventObject aEvent)242     public void notifyEvent (AccessibleEventObject aEvent)
243     {
244         if (aEvent.EventId == AccessibleEventId.SELECTION_CHANGED)
245             Update ();
246     }
247 
248     private JPanel maChildrenSelector;
249     private XAccessibleSelection mxSelection;
250     private JLabel maTypeLabel;
251 }
252