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