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