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