1 package ov; 2 3 import java.awt.Color; 4 import java.awt.GridBagConstraints; 5 import java.awt.GridBagLayout; 6 import java.awt.event.ActionListener; 7 import java.awt.event.ActionEvent; 8 9 import javax.swing.JButton; 10 import javax.swing.JLabel; 11 12 import com.sun.star.accessibility.AccessibleEventId; 13 import com.sun.star.accessibility.AccessibleEventObject; 14 import com.sun.star.accessibility.AccessibleStateType; 15 import com.sun.star.accessibility.XAccessibleComponent; 16 import com.sun.star.accessibility.XAccessibleContext; 17 import com.sun.star.accessibility.XAccessibleStateSet; 18 import com.sun.star.uno.UnoRuntime; 19 20 public class FocusView 21 extends ListeningObjectView 22 implements ActionListener 23 { 24 /** Create a FocusView when the given object supports the 25 XAccessibleComponent interface. 26 */ 27 static public ObjectView Create ( 28 ObjectViewContainer aContainer, 29 XAccessibleContext xContext) 30 { 31 XAccessibleComponent xComponent = (XAccessibleComponent)UnoRuntime.queryInterface( 32 XAccessibleComponent.class, xContext); 33 if (xComponent != null) 34 return new FocusView (aContainer); 35 else 36 return null; 37 } 38 39 public FocusView (ObjectViewContainer aContainer) 40 { 41 super (aContainer); 42 43 setLayout (new GridBagLayout()); 44 GridBagConstraints aConstraints = new GridBagConstraints (); 45 46 maFocused = new JLabel (); 47 aConstraints.gridy = 0; 48 aConstraints.weightx = 1; 49 aConstraints.fill = GridBagConstraints.HORIZONTAL; 50 add (maFocused, aConstraints); 51 52 maGrabFocus = new JButton ("grabFocus"); 53 aConstraints.gridy = 1; 54 aConstraints.fill = GridBagConstraints.NONE; 55 aConstraints.anchor = GridBagConstraints.WEST; 56 add (maGrabFocus, aConstraints); 57 58 maGrabFocus.addActionListener (this); 59 } 60 61 /** Additionally to the context store a reference to the 62 XAccessibleComponent interface. 63 */ 64 public void SetObject (XAccessibleContext xObject) 65 { 66 mxComponent = (XAccessibleComponent)UnoRuntime.queryInterface( 67 XAccessibleComponent.class, xObject); 68 super.SetObject (xObject); 69 } 70 71 synchronized public void Destroy () 72 { 73 super.Destroy(); 74 maGrabFocus.removeActionListener (this); 75 } 76 77 synchronized public void Update () 78 { 79 if (mxContext == null) 80 { 81 maFocused.setText ("<null object>"); 82 maGrabFocus.setEnabled (false); 83 } 84 else 85 { 86 XAccessibleStateSet aStateSet = mxContext.getAccessibleStateSet(); 87 if (aStateSet.contains(AccessibleStateType.FOCUSED)) 88 maFocused.setText ("focused"); 89 else 90 maFocused.setText ("not focused"); 91 if (maGrabFocus != null) 92 maGrabFocus.setEnabled (true); 93 } 94 } 95 96 public String GetTitle () 97 { 98 return ("Focus"); 99 } 100 101 synchronized public void actionPerformed (ActionEvent aEvent) 102 { 103 if (aEvent.getActionCommand().equals("grabFocus")) 104 { 105 mxComponent.grabFocus(); 106 } 107 } 108 109 public void notifyEvent (AccessibleEventObject aEvent) 110 { 111 System.out.println (aEvent); 112 if (aEvent.EventId == AccessibleEventId.STATE_CHANGED) 113 Update (); 114 } 115 116 private JLabel maFocused; 117 private JButton maGrabFocus; 118 private XAccessibleComponent mxComponent; 119 } 120