1 package ov; 2 3 import java.util.Vector; 4 5 import java.awt.event.ActionListener; 6 import java.awt.event.ActionEvent; 7 8 import java.awt.BorderLayout; 9 import java.awt.Dimension; 10 import java.awt.GridBagLayout; 11 import java.awt.GridBagConstraints; 12 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; 25 26 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; 34 35 import com.sun.star.uno.UnoRuntime; 36 import com.sun.star.lang.IndexOutOfBoundsException; 37 38 39 /** Display a list of children and select/deselect buttons 40 */ 41 class SelectionView 42 extends ListeningObjectView 43 implements ActionListener 44 { 45 static public ObjectView Create ( 46 ObjectViewContainer aContainer, 47 XAccessibleContext xContext) 48 { 49 XAccessibleSelection xSelection = (XAccessibleSelection)UnoRuntime.queryInterface( 50 XAccessibleSelection.class, xContext); 51 if (xSelection != null) 52 return new SelectionView(aContainer); 53 else 54 return null; 55 } 56 57 public SelectionView (ObjectViewContainer aContainer) 58 { 59 super (aContainer); 60 Layout(); 61 } 62 63 public String GetTitle () 64 { 65 return "Selection"; 66 } 67 68 /** Create and arrange the widgets for this view. 69 */ 70 private void Layout () 71 { 72 setLayout (new GridBagLayout()); 73 74 GridBagConstraints aConstraints = new GridBagConstraints(); 75 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); 82 83 // the JListBox 84 maChildrenSelector = new JPanel (); 85 maChildrenSelector.setPreferredSize (new Dimension (100,100)); 86 maChildrenSelector.setLayout (new BoxLayout (maChildrenSelector, BoxLayout.Y_AXIS)); 87 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); 96 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); 107 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); 115 116 setSize (getPreferredSize()); 117 } 118 119 120 public void SetObject (XAccessibleContext xContext) 121 { 122 mxSelection = (XAccessibleSelection)UnoRuntime.queryInterface( 123 XAccessibleSelection.class, xContext); 124 super.SetObject (xContext); 125 } 126 127 128 public void Update () 129 { 130 maChildrenSelector.removeAll (); 131 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)) 137 { 138 bMultiSelectable = true; 139 maTypeLabel.setText ("multi selectable"); 140 } 141 else 142 { 143 maTypeLabel.setText ("single selectable"); 144 aButtonGroup = new ButtonGroup (); 145 } 146 147 int nCount = mxContext.getAccessibleChildCount(); 148 for (int i=0; i<nCount; i++) 149 { 150 try 151 { 152 XAccessible xChild = mxContext.getAccessibleChild(i); 153 XAccessibleContext xChildContext = xChild.getAccessibleContext(); 154 155 String sName = i + " " + xChildContext.getAccessibleName(); 156 JToggleButton aChild; 157 if (bMultiSelectable) 158 aChild = new JCheckBox (sName); 159 else 160 { 161 aChild = new JRadioButton (sName); 162 aButtonGroup.add (aChild); 163 } 164 165 XAccessibleStateSet aChildStateSet = mxContext.getAccessibleStateSet(); 166 aChild.setSelected (aChildStateSet!=null 167 && aChildStateSet.contains(AccessibleStateType.SELECTED)); 168 169 aChild.addActionListener (this); 170 maChildrenSelector.add (aChild); 171 172 } 173 catch (IndexOutOfBoundsException e) 174 { 175 } 176 } 177 } 178 179 180 void SelectAll() 181 { 182 mxSelection.selectAllAccessibleChildren(); 183 } 184 185 void ClearSelection() 186 { 187 mxSelection.clearAccessibleSelection(); 188 } 189 190 191 /** Call the function associated with the pressed button. 192 */ 193 public void actionPerformed (ActionEvent aEvent) 194 { 195 String sCommand = aEvent.getActionCommand(); 196 197 if (sCommand.equals ("Clear Selection")) 198 ClearSelection(); 199 else if (sCommand.equals ("Select all")) 200 SelectAll(); 201 else 202 { 203 // Extract the child index from the widget text. 204 String[] aWords = sCommand.split (" "); 205 int nIndex = Integer.parseInt(aWords[0]); 206 try 207 { 208 if (((JToggleButton)aEvent.getSource()).isSelected()) 209 mxSelection.selectAccessibleChild (nIndex); 210 else 211 mxSelection.deselectAccessibleChild (nIndex); 212 } 213 catch (IndexOutOfBoundsException e) 214 { 215 System.err.println ("caught exception while changing selection: " + e); 216 } 217 } 218 } 219 220 221 public void notifyEvent (AccessibleEventObject aEvent) 222 { 223 if (aEvent.EventId == AccessibleEventId.SELECTION_CHANGED) 224 Update (); 225 } 226 227 private JPanel maChildrenSelector; 228 private XAccessibleSelection mxSelection; 229 private JLabel maTypeLabel; 230 } 231