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