1 /* 2 * This file is part of the LibreOffice project. 3 * 4 * This Source Code Form is subject to the terms of the Mozilla Public 5 * License, v. 2.0. If a copy of the MPL was not distributed with this 6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 * 8 * This file incorporates work covered by the following license notice: 9 * 10 * Licensed to the Apache Software Foundation (ASF) under one or more 11 * contributor license agreements. See the NOTICE file distributed 12 * with this work for additional information regarding copyright 13 * ownership. The ASF licenses this file to you under the Apache 14 * License, Version 2.0 (the "License"); you may not use this file 15 * except in compliance with the License. You may obtain a copy of 16 * the License at http://www.apache.org/licenses/LICENSE-2.0 . 17 */ 18 19 package ov; 20 21 import java.awt.GridBagConstraints; 22 import java.awt.GridBagLayout; 23 import java.awt.event.ActionListener; 24 import java.awt.event.ActionEvent; 25 26 import javax.swing.JButton; 27 import javax.swing.JLabel; 28 29 import com.sun.star.accessibility.AccessibleEventId; 30 import com.sun.star.accessibility.AccessibleEventObject; 31 import com.sun.star.accessibility.AccessibleStateType; 32 import com.sun.star.accessibility.XAccessibleComponent; 33 import com.sun.star.accessibility.XAccessibleContext; 34 import com.sun.star.accessibility.XAccessibleStateSet; 35 import com.sun.star.uno.UnoRuntime; 36 37 public class FocusView 38 extends ListeningObjectView 39 implements ActionListener 40 { 41 /** Create a FocusView when the given object supports the 42 XAccessibleComponent interface. 43 */ Create( ObjectViewContainer aContainer, XAccessibleContext xContext)44 public static ObjectView Create ( 45 ObjectViewContainer aContainer, 46 XAccessibleContext xContext) 47 { 48 XAccessibleComponent xComponent = UnoRuntime.queryInterface( 49 XAccessibleComponent.class, xContext); 50 if (xComponent != null) 51 return new FocusView (aContainer); 52 else 53 return null; 54 } 55 FocusView(ObjectViewContainer aContainer)56 private FocusView (ObjectViewContainer aContainer) 57 { 58 super (aContainer); 59 60 setLayout (new GridBagLayout()); 61 GridBagConstraints aConstraints = new GridBagConstraints (); 62 63 maFocused = new JLabel (); 64 aConstraints.gridy = 0; 65 aConstraints.weightx = 1; 66 aConstraints.fill = GridBagConstraints.HORIZONTAL; 67 add (maFocused, aConstraints); 68 69 maGrabFocus = new JButton ("grabFocus"); 70 aConstraints.gridy = 1; 71 aConstraints.fill = GridBagConstraints.NONE; 72 aConstraints.anchor = GridBagConstraints.WEST; 73 add (maGrabFocus, aConstraints); 74 75 maGrabFocus.addActionListener (this); 76 } 77 78 /** Additionally to the context store a reference to the 79 XAccessibleComponent interface. 80 */ 81 @Override SetObject(XAccessibleContext xObject)82 public void SetObject (XAccessibleContext xObject) 83 { 84 mxComponent = UnoRuntime.queryInterface( 85 XAccessibleComponent.class, xObject); 86 super.SetObject (xObject); 87 } 88 89 @Override Destroy()90 synchronized public void Destroy () 91 { 92 super.Destroy(); 93 maGrabFocus.removeActionListener (this); 94 } 95 96 @Override Update()97 synchronized public void Update () 98 { 99 if (mxContext == null) 100 { 101 maFocused.setText ("<null object>"); 102 maGrabFocus.setEnabled (false); 103 } 104 else 105 { 106 XAccessibleStateSet aStateSet = mxContext.getAccessibleStateSet(); 107 if (aStateSet.contains(AccessibleStateType.FOCUSED)) 108 maFocused.setText ("focused"); 109 else 110 maFocused.setText ("not focused"); 111 if (maGrabFocus != null) 112 maGrabFocus.setEnabled (true); 113 } 114 } 115 116 @Override GetTitle()117 public String GetTitle () 118 { 119 return "Focus"; 120 } 121 actionPerformed(ActionEvent aEvent)122 synchronized public void actionPerformed (ActionEvent aEvent) 123 { 124 if (aEvent.getActionCommand().equals("grabFocus")) 125 { 126 mxComponent.grabFocus(); 127 } 128 } 129 130 @Override notifyEvent(AccessibleEventObject aEvent)131 public void notifyEvent (AccessibleEventObject aEvent) 132 { 133 System.out.println (aEvent); 134 if (aEvent.EventId == AccessibleEventId.STATE_CHANGED) 135 Update (); 136 } 137 138 private final JLabel maFocused; 139 private final JButton maGrabFocus; 140 private XAccessibleComponent mxComponent; 141 } 142