1 /*************************************************************************
3 * $RCSfile: TextualDisplay.java,v $
7 * last change: $Author: hr $ $Date: 2003-06-30 15:07:53 $
9 * The Contents of this file are made available subject to the terms of
12 * Copyright (c) 2003 by Sun Microsystems, Inc.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *************************************************************************/
41 import java
.awt
.Color
;
43 import java
.awt
.Dimension
;
44 import javax
.swing
.JTextArea
;
45 import javax
.swing
.JScrollPane
;
46 import java
.util
.Vector
;
48 import com
.sun
.star
.accessibility
.XAccessible
;
49 import com
.sun
.star
.accessibility
.XAccessibleContext
;
50 import com
.sun
.star
.accessibility
.XAccessibleComponent
;
51 import com
.sun
.star
.accessibility
.XAccessibleStateSet
;
53 import com
.sun
.star
.uno
.AnyConverter
;
54 import com
.sun
.star
.uno
.UnoRuntime
;
56 import com
.sun
.star
.awt
.Point
;
57 import com
.sun
.star
.awt
.Size
;
59 /** Display textual information for a given accessible object. This
60 includes the names of that object of its ancestors as well as some
61 information retrieved from the XAccessibleContext and
62 XAccessibleComponent interfaces.
66 implements IAccessibleObjectDisplay
68 /** Create a new scroll pane that contains a text widget which display
69 information about given accessible objects.
71 public TextualDisplay ()
73 // Create a text widget for displaying the text information...
74 maText
= new JTextArea (80,10);
75 maText
.setBackground (new Color (250,240,230));
76 maText
.setFont (new Font ("Courier", Font
.PLAIN
, 11));
78 // ...and set-up the scroll pane to show this widget.
79 setViewportView (maText
);
80 setVerticalScrollBarPolicy (JScrollPane
.VERTICAL_SCROLLBAR_ALWAYS
);
81 setHorizontalScrollBarPolicy (JScrollPane
.HORIZONTAL_SCROLLBAR_ALWAYS
);
87 /** Set the accessible object to display. Call this method e.g. when a
88 new object has been focused.
90 public synchronized void setAccessibleObject (XAccessibleContext xContext
)
92 // First clear the display area.
93 msTextContent
= new String ();
97 String sIndentation
= showParents (xContext
);
98 showContextInfo (xContext
, sIndentation
);
99 showComponentInfo (xContext
, sIndentation
);
102 maText
.setText (msTextContent
);
108 public synchronized void updateAccessibleObject (XAccessibleContext xContext
)
110 setAccessibleObject (xContext
);
115 /** Show some of the information available over the given object's
116 XAccessibleContext interface.
118 private void showContextInfo (XAccessibleContext xContext
, String sIndentation
)
120 // Show the description.
121 msTextContent
+= sIndentation
+ "Description: "
122 + xContext
.getAccessibleDescription() + "\n";
124 showStates (xContext
, sIndentation
);
130 /** Show a list of all of the the given object's states. Use the
131 NameConverter class to transform the numerical state ids into human
134 The accessible context for which to show the state names.
136 private void showStates (XAccessibleContext xContext
, String sIndentation
)
138 // Get the state set object...
139 XAccessibleStateSet xStateSet
= xContext
.getAccessibleStateSet();
140 // ...and retrieve an array of numerical ids.
141 short aStates
[] = xStateSet
.getStates();
143 // Iterate over the array and print the names of the states.
144 msTextContent
+= sIndentation
+ "States : ";
145 for (int i
=0; i
<aStates
.length
; i
++)
148 msTextContent
+= ", ";
149 msTextContent
+= NameProvider
.getStateName(aStates
[i
]);
151 msTextContent
+= "\n";
157 /** When the given object supports the XAccessibleComponent interface then
158 show its size and location on the screen.
160 private void showComponentInfo (XAccessibleContext xContext
, String sIndentation
)
162 // Try to cast the given accessible context to the
163 // XAccessibleComponent interface.
164 XAccessibleComponent xComponent
=
165 (XAccessibleComponent
)UnoRuntime
.queryInterface(
166 XAccessibleComponent
.class, xContext
);
167 if (xComponent
!= null)
169 Point aLocation
= xComponent
.getLocationOnScreen();
170 msTextContent
+= sIndentation
+ "Position : "
171 + aLocation
.X
+ ", " + aLocation
.Y
+ "\n";
173 Size aSize
= xComponent
.getSize();
174 msTextContent
+= sIndentation
+ "Size : "
175 + aSize
.Width
+ ", " + aSize
.Height
+ "\n";
183 /** Print the names of the given object and its parents and return an
184 indentation string that can be used to print further information
187 private String
showParents (XAccessibleContext xContext
)
189 // Create the path from the given object to its tree's root.
190 Vector aPathToRoot
= new Vector();
191 while (xContext
!= null)
193 aPathToRoot
.add (xContext
);
194 // Go up the hierarchy one level to the object's parent.
197 XAccessible xParent
= xContext
.getAccessibleParent();
199 xContext
= xParent
.getAccessibleContext();
205 System
.err
.println ("caught exception " + e
+ " while getting path to root");
209 // Print the path in the accessibility tree from the given context to
211 String sIndentation
= new String ();
212 for (int i
=aPathToRoot
.size()-1; i
>=0; i
--)
214 XAccessibleContext xParentContext
= (XAccessibleContext
)aPathToRoot
.get(i
);
215 String sParentName
= xParentContext
.getAccessibleName();
216 if (sParentName
.length() == 0)
217 sParentName
= "<unnamed> / Role "
218 + NameProvider
.getRoleName(xParentContext
.getAccessibleRole());
219 msTextContent
+= sIndentation
+ sParentName
+ "\n";
220 sIndentation
+= msIndentation
;
228 /// The text widget that is used for the actual text display.
229 private JTextArea maText
;
231 /// The indentation with which an object's child is indented.
232 private final String msIndentation
= new String(" ");
234 /// The text content displayed by this object.
235 private String msTextContent
= new String ();