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.XAccessibleText;
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 TextView
42     extends ListeningObjectView
43 {
44     /** Create a TextView when the given object supports the
45         XAccessibleText interface.
46     */
Create( ObjectViewContainer aContainer, XAccessibleContext xContext)47     static public ObjectView Create (
48         ObjectViewContainer aContainer,
49         XAccessibleContext xContext)
50     {
51         XAccessibleText xText = (XAccessibleText)UnoRuntime.queryInterface(
52                 XAccessibleText.class, xContext);
53         if (xText != null)
54             return new TextView (aContainer);
55         else
56             return null;
57     }
58 
59 
TextView(ObjectViewContainer aContainer)60     public TextView (ObjectViewContainer aContainer)
61     {
62         super (aContainer);
63 
64         setLayout (new GridBagLayout());
65         GridBagConstraints aConstraints = new GridBagConstraints ();
66 
67         JLabel aLabel = new JLabel ("Text:");
68         aConstraints.gridy = 0;
69         aConstraints.weightx = 1;
70         aConstraints.fill = GridBagConstraints.HORIZONTAL;
71         add (aLabel, aConstraints);
72 
73         maTextLabel = new JLabel ("");
74         aConstraints.gridx = 1;
75         aConstraints.fill = GridBagConstraints.NONE;
76         aConstraints.anchor = GridBagConstraints.WEST;
77         add (maTextLabel, aConstraints);
78 
79         aLabel = new JLabel ("Caret position:");
80         aConstraints.gridx = 0;
81         aConstraints.gridy = 1;
82         aConstraints.weightx = 1;
83         aConstraints.fill = GridBagConstraints.HORIZONTAL;
84         add (aLabel, aConstraints);
85 
86         maCaretPositionLabel = new JLabel ("");
87         aConstraints.gridx = 1;
88         aConstraints.fill = GridBagConstraints.NONE;
89         aConstraints.anchor = GridBagConstraints.WEST;
90         add (maCaretPositionLabel, aConstraints);
91     }
92 
93 
94     /** Additionally to the context store a reference to the
95         XAccessibleText interface.
96     */
SetObject(XAccessibleContext xObject)97     public void SetObject (XAccessibleContext xObject)
98     {
99         mxText = (XAccessibleText)UnoRuntime.queryInterface(
100             XAccessibleText.class, xObject);
101         super.SetObject (xObject);
102     }
103 
Destroy()104     synchronized public void Destroy ()
105     {
106         super.Destroy();
107     }
108 
Update()109     synchronized public void Update ()
110     {
111         if (mxText == null)
112         {
113             maTextLabel.setText ("<null object>");
114             maCaretPositionLabel.setText ("<null object>");
115         }
116         else
117         {
118             maTextLabel.setText (mxText.getText());
119             maCaretPositionLabel.setText (Integer.toString(mxText.getCaretPosition()));
120         }
121     }
122 
GetTitle()123     public String GetTitle ()
124     {
125         return ("Text");
126     }
127 
notifyEvent(AccessibleEventObject aEvent)128     public void notifyEvent (AccessibleEventObject aEvent)
129     {
130         System.out.println (aEvent);
131         switch (aEvent.EventId)
132         {
133             case AccessibleEventId.TEXT_CHANGED :
134             case AccessibleEventId.CARET_CHANGED :
135                 Update ();
136                 break;
137         }
138     }
139 
140     private JLabel
141         maTextLabel,
142         maCaretPositionLabel;
143     private XAccessibleText mxText;
144 }
145