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