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