xref: /AOO41X/main/toolkit/test/accessibility/CanvasShape.java (revision 1b0aaa91df402a6f20c4769a008573eb958e886d)
1*1b0aaa91SAndrew Rist /**************************************************************
2*1b0aaa91SAndrew Rist  *
3*1b0aaa91SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*1b0aaa91SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*1b0aaa91SAndrew Rist  * distributed with this work for additional information
6*1b0aaa91SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*1b0aaa91SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*1b0aaa91SAndrew Rist  * "License"); you may not use this file except in compliance
9*1b0aaa91SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*1b0aaa91SAndrew Rist  *
11*1b0aaa91SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*1b0aaa91SAndrew Rist  *
13*1b0aaa91SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*1b0aaa91SAndrew Rist  * software distributed under the License is distributed on an
15*1b0aaa91SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*1b0aaa91SAndrew Rist  * KIND, either express or implied.  See the License for the
17*1b0aaa91SAndrew Rist  * specific language governing permissions and limitations
18*1b0aaa91SAndrew Rist  * under the License.
19*1b0aaa91SAndrew Rist  *
20*1b0aaa91SAndrew Rist  *************************************************************/
21*1b0aaa91SAndrew Rist 
22cdf0e10cSrcweir import java.awt.*;
23cdf0e10cSrcweir import javax.swing.*;
24cdf0e10cSrcweir import javax.swing.tree.*;
25cdf0e10cSrcweir import java.awt.geom.Rectangle2D;
26cdf0e10cSrcweir 
27cdf0e10cSrcweir import com.sun.star.beans.XPropertyChangeListener;
28cdf0e10cSrcweir import com.sun.star.beans.PropertyChangeEvent;
29cdf0e10cSrcweir 
30cdf0e10cSrcweir import com.sun.star.accessibility.XAccessible;
31cdf0e10cSrcweir import com.sun.star.accessibility.XAccessibleContext;
32cdf0e10cSrcweir import com.sun.star.accessibility.XAccessibleComponent;
33cdf0e10cSrcweir import com.sun.star.accessibility.XAccessibleExtendedComponent;
34cdf0e10cSrcweir import com.sun.star.accessibility.XAccessibleText;
35cdf0e10cSrcweir import com.sun.star.accessibility.XAccessibleStateSet;
36cdf0e10cSrcweir import com.sun.star.accessibility.AccessibleStateType;
37cdf0e10cSrcweir 
38cdf0e10cSrcweir class CanvasShape
39cdf0e10cSrcweir {
40cdf0e10cSrcweir     public final Color maHighlightColor = Color.red;
41cdf0e10cSrcweir     public final Color maSelectionColor = Color.green;
42cdf0e10cSrcweir     public final Color maFocusColor = Color.blue;
43cdf0e10cSrcweir 
44cdf0e10cSrcweir     //    public AccessibleObject (XAccessibleContext xContext, TreePath aPath)
CanvasShape(AccTreeNode aNode)45cdf0e10cSrcweir     public CanvasShape (AccTreeNode aNode)
46cdf0e10cSrcweir     {
47cdf0e10cSrcweir         maNode = aNode;
48cdf0e10cSrcweir         mxContext = aNode.getContext();
49cdf0e10cSrcweir         msName = "name unknown";
50cdf0e10cSrcweir         msDescription = "description unknown";
51cdf0e10cSrcweir         maShape = new Rectangle2D.Double (-10,-10,10,10);
52cdf0e10cSrcweir         maPosition = new Point (-10,-10);
53cdf0e10cSrcweir         maSize = new Dimension (10,10);
54cdf0e10cSrcweir         maFgColor = java.awt.Color.black;
55cdf0e10cSrcweir         maBgColor = Color.blue;
56cdf0e10cSrcweir         mnRole = -1;
57cdf0e10cSrcweir         mbHighlighted = false;
58cdf0e10cSrcweir         mbSelected = false;
59cdf0e10cSrcweir         mbFocused = false;
60cdf0e10cSrcweir         mxComponent = aNode.getComponent();
61cdf0e10cSrcweir 
62cdf0e10cSrcweir         update ();
63cdf0e10cSrcweir     }
64cdf0e10cSrcweir 
65cdf0e10cSrcweir 
66cdf0e10cSrcweir 
67cdf0e10cSrcweir     /** Update the data obtained from the xAccessible.
68cdf0e10cSrcweir     */
update()69cdf0e10cSrcweir     public void update ()
70cdf0e10cSrcweir     {
71cdf0e10cSrcweir         if (mxContext != null)
72cdf0e10cSrcweir         {
73cdf0e10cSrcweir             msName = mxContext.getAccessibleName();
74cdf0e10cSrcweir             msDescription = mxContext.getAccessibleDescription();
75cdf0e10cSrcweir             mnRole = mxContext.getAccessibleRole();
76cdf0e10cSrcweir 
77cdf0e10cSrcweir             // Extract the selected and focused flag.
78cdf0e10cSrcweir             XAccessibleStateSet xStateSet = mxContext.getAccessibleStateSet ();
79cdf0e10cSrcweir             if (xStateSet != null)
80cdf0e10cSrcweir             {
81cdf0e10cSrcweir                 mbSelected = xStateSet.contains (AccessibleStateType.SELECTED);
82cdf0e10cSrcweir                 mbFocused = xStateSet.contains (AccessibleStateType.FOCUSED);
83cdf0e10cSrcweir             }
84cdf0e10cSrcweir         }
85cdf0e10cSrcweir 
86cdf0e10cSrcweir         updateGeometry ();
87cdf0e10cSrcweir         if (mxComponent != null)
88cdf0e10cSrcweir         {
89cdf0e10cSrcweir             // Note: alpha values in office 0..255 have to be mapped to
90cdf0e10cSrcweir             //       255..0 in Java
91cdf0e10cSrcweir             Color aCol = new Color (mxComponent.getForeground(), true);
92cdf0e10cSrcweir             maFgColor = new Color (aCol.getRed (),
93cdf0e10cSrcweir                                    aCol.getGreen (),
94cdf0e10cSrcweir                                    aCol.getBlue (),
95cdf0e10cSrcweir                                    0xff - aCol.getAlpha ());
96cdf0e10cSrcweir             aCol = new Color (mxComponent.getBackground(), true);
97cdf0e10cSrcweir             maBgColor = new Color (aCol.getRed (),
98cdf0e10cSrcweir                                    aCol.getGreen (),
99cdf0e10cSrcweir                                    aCol.getBlue (),
100cdf0e10cSrcweir                                    0xff - aCol.getAlpha ());
101cdf0e10cSrcweir         }
102cdf0e10cSrcweir     }
103cdf0e10cSrcweir 
updateGeometry()104cdf0e10cSrcweir     public void updateGeometry ()
105cdf0e10cSrcweir     {
106cdf0e10cSrcweir         if (mxComponent != null)
107cdf0e10cSrcweir         {
108cdf0e10cSrcweir             com.sun.star.awt.Point aLocationOnScreen = mxComponent.getLocationOnScreen();
109cdf0e10cSrcweir             com.sun.star.awt.Size aSizeOnScreen = mxComponent.getSize();
110cdf0e10cSrcweir             maPosition = new Point (
111cdf0e10cSrcweir                 aLocationOnScreen.X,
112cdf0e10cSrcweir                 aLocationOnScreen.Y);
113cdf0e10cSrcweir             maSize = new Dimension (
114cdf0e10cSrcweir                 aSizeOnScreen.Width,
115cdf0e10cSrcweir                 aSizeOnScreen.Height);
116cdf0e10cSrcweir         }
117cdf0e10cSrcweir     }
118cdf0e10cSrcweir 
119cdf0e10cSrcweir 
120cdf0e10cSrcweir     /** Paint the object into the specified canvas.  It is transformed
121cdf0e10cSrcweir         according to the specified offset and scale.
122cdf0e10cSrcweir     */
paint(Graphics2D g, double nXOffset, double nYOffset, double nScaleFactor, boolean bShowDescription, boolean bShowName, boolean bShowText)123cdf0e10cSrcweir     public void paint (Graphics2D g,
124cdf0e10cSrcweir         double nXOffset, double nYOffset, double nScaleFactor,
125cdf0e10cSrcweir         boolean bShowDescription, boolean bShowName, boolean bShowText)
126cdf0e10cSrcweir     {
127cdf0e10cSrcweir         try{
128cdf0e10cSrcweir             // Transform the object's position and size according to the
129cdf0e10cSrcweir             // specified offset and scale.
130cdf0e10cSrcweir             Point aLocation = new Point();
131cdf0e10cSrcweir             maShape = new Rectangle2D.Double (
132cdf0e10cSrcweir                 maPosition.x * nScaleFactor + nXOffset,
133cdf0e10cSrcweir                 maPosition.y * nScaleFactor + nYOffset,
134cdf0e10cSrcweir                 maSize.width * nScaleFactor,
135cdf0e10cSrcweir                 maSize.height * nScaleFactor);
136cdf0e10cSrcweir 
137cdf0e10cSrcweir             // Fill the object's bounding box with its background color if it
138cdf0e10cSrcweir             // has no children.
139cdf0e10cSrcweir             if (mxContext.getAccessibleChildCount() == 0)
140cdf0e10cSrcweir             {
141cdf0e10cSrcweir                 g.setColor (maBgColor);
142cdf0e10cSrcweir                 g.fill (maShape);
143cdf0e10cSrcweir             }
144cdf0e10cSrcweir 
145cdf0e10cSrcweir             // Remove alpha channel from color before drawing the frame.
146cdf0e10cSrcweir             Color color = maFgColor;
147cdf0e10cSrcweir             if (maFgColor.getAlpha()<128)
148cdf0e10cSrcweir                 color = new Color (maFgColor.getRed(), maFgColor.getGreen(), maFgColor.getBlue());
149cdf0e10cSrcweir             g.setColor (color);
150cdf0e10cSrcweir             g.draw (maShape);
151cdf0e10cSrcweir 
152cdf0e10cSrcweir             if (mbFocused)
153cdf0e10cSrcweir             {
154cdf0e10cSrcweir                 g.setColor (maFocusColor);
155cdf0e10cSrcweir                 for (int x=0; x<=2; x++)
156cdf0e10cSrcweir                     for (int y=0; y<=2; y++)
157cdf0e10cSrcweir                         g.fill (
158cdf0e10cSrcweir                             new Rectangle2D.Double (
159cdf0e10cSrcweir                                 maShape.x + x/2.0 * maShape.width-3,
160cdf0e10cSrcweir                                 maShape.y + y/2.0 * maShape.height-3,
161cdf0e10cSrcweir                                 6,
162cdf0e10cSrcweir                                 6));
163cdf0e10cSrcweir             }
164cdf0e10cSrcweir             if (mbSelected)
165cdf0e10cSrcweir             {
166cdf0e10cSrcweir                 g.setColor (maSelectionColor);
167cdf0e10cSrcweir                 for (int x=0; x<=2; x++)
168cdf0e10cSrcweir                     for (int y=0; y<=2; y++)
169cdf0e10cSrcweir                         g.draw (
170cdf0e10cSrcweir                             new Rectangle2D.Double (
171cdf0e10cSrcweir                                 maShape.x + x/2.0 * maShape.width-2,
172cdf0e10cSrcweir                                 maShape.y + y/2.0 * maShape.height-2,
173cdf0e10cSrcweir                                 4,
174cdf0e10cSrcweir                                 4));
175cdf0e10cSrcweir             }
176cdf0e10cSrcweir 
177cdf0e10cSrcweir             // Write the object's text OR name and description.
178cdf0e10cSrcweir             g.setColor (maFgColor);
179cdf0e10cSrcweir             if (bShowName)
180cdf0e10cSrcweir                 paintName (g);
181cdf0e10cSrcweir             if (bShowDescription)
182cdf0e10cSrcweir                 paintDescription (g);
183cdf0e10cSrcweir             if (bShowText)
184cdf0e10cSrcweir                 paintText (g);
185cdf0e10cSrcweir         }
186cdf0e10cSrcweir         catch (Exception e)
187cdf0e10cSrcweir         { // don't care
188cdf0e10cSrcweir         }
189cdf0e10cSrcweir     }
190cdf0e10cSrcweir 
paint_highlight(Graphics2D g, double nXOffset, double nYOffset, double nScaleFactor)191cdf0e10cSrcweir     public void paint_highlight (Graphics2D g,
192cdf0e10cSrcweir         double nXOffset, double nYOffset, double nScaleFactor)
193cdf0e10cSrcweir     {
194cdf0e10cSrcweir         if (mbHighlighted)
195cdf0e10cSrcweir             g.setColor (maHighlightColor);
196cdf0e10cSrcweir         else
197cdf0e10cSrcweir             g.setColor (maFgColor);
198cdf0e10cSrcweir         g.draw (maShape);
199cdf0e10cSrcweir     }
200cdf0e10cSrcweir 
201cdf0e10cSrcweir 
202cdf0e10cSrcweir 
203cdf0e10cSrcweir 
paintName(Graphics2D g)204cdf0e10cSrcweir     private void paintName (Graphics2D g)
205cdf0e10cSrcweir     {
206cdf0e10cSrcweir         g.drawString ("Name: " + msName,
207cdf0e10cSrcweir             (float)maShape.x+5,
208cdf0e10cSrcweir             (float)maShape.y+15);
209cdf0e10cSrcweir     }
210cdf0e10cSrcweir 
211cdf0e10cSrcweir 
212cdf0e10cSrcweir 
paintDescription(Graphics2D g)213cdf0e10cSrcweir     private void paintDescription (Graphics2D g)
214cdf0e10cSrcweir     {
215cdf0e10cSrcweir         g.drawString ("Description: " + msDescription,
216cdf0e10cSrcweir             (float)maShape.x+5,
217cdf0e10cSrcweir             (float)maShape.y+35);
218cdf0e10cSrcweir     }
219cdf0e10cSrcweir 
220cdf0e10cSrcweir 
221cdf0e10cSrcweir 
222cdf0e10cSrcweir 
paintText(Graphics2D g)223cdf0e10cSrcweir     private void paintText (Graphics2D g)
224cdf0e10cSrcweir     {
225cdf0e10cSrcweir         XAccessibleText xText = null;
226cdf0e10cSrcweir         // get XAccessibleText
227cdf0e10cSrcweir         xText = maNode.getText();
228cdf0e10cSrcweir 
229cdf0e10cSrcweir         // Draw every character in the text string.
230cdf0e10cSrcweir         if (xText != null)
231cdf0e10cSrcweir         {
232cdf0e10cSrcweir             String sText = xText.getText();
233cdf0e10cSrcweir             try
234cdf0e10cSrcweir             {
235cdf0e10cSrcweir                 for(int i = 0; i < sText.length(); i++)
236cdf0e10cSrcweir                 {
237cdf0e10cSrcweir                     com.sun.star.awt.Rectangle aRect =
238cdf0e10cSrcweir                         xText.getCharacterBounds(i);
239cdf0e10cSrcweir 
240cdf0e10cSrcweir                     double x = maShape.x + aRect.X;
241cdf0e10cSrcweir                     double y = maShape.y + aRect.Y + aRect.Height;
242cdf0e10cSrcweir 
243cdf0e10cSrcweir                     g.drawString(sText.substring(i, i+1), (float)x, (float)y);
244cdf0e10cSrcweir                 }
245cdf0e10cSrcweir             }
246cdf0e10cSrcweir             catch (com.sun.star.lang.IndexOutOfBoundsException e)
247cdf0e10cSrcweir             {}
248cdf0e10cSrcweir         }
249cdf0e10cSrcweir     }
250cdf0e10cSrcweir 
251cdf0e10cSrcweir 
252cdf0e10cSrcweir 
253cdf0e10cSrcweir 
254cdf0e10cSrcweir     /** Callback for disposing events.
255cdf0e10cSrcweir     */
disposing(com.sun.star.lang.EventObject e)256cdf0e10cSrcweir     public void disposing (com.sun.star.lang.EventObject e)
257cdf0e10cSrcweir     {
258cdf0e10cSrcweir         System.out.println ("Disposing");
259cdf0e10cSrcweir     }
260cdf0e10cSrcweir 
261cdf0e10cSrcweir 
262cdf0e10cSrcweir 
263cdf0e10cSrcweir 
264cdf0e10cSrcweir     /** Compute whether the specified point lies inside the object's
265cdf0e10cSrcweir         bounding box.
266cdf0e10cSrcweir     */
contains(int x, int y)267cdf0e10cSrcweir     public boolean contains (int x, int y)
268cdf0e10cSrcweir     {
269cdf0e10cSrcweir         return (maShape.contains (x,y));
270cdf0e10cSrcweir     }
271cdf0e10cSrcweir 
highlight()272cdf0e10cSrcweir     public void highlight ()
273cdf0e10cSrcweir     {
274cdf0e10cSrcweir         mbHighlighted = true;
275cdf0e10cSrcweir     }
276cdf0e10cSrcweir 
unhighlight()277cdf0e10cSrcweir     public void unhighlight ()
278cdf0e10cSrcweir     {
279cdf0e10cSrcweir         mbHighlighted = false;
280cdf0e10cSrcweir     }
281cdf0e10cSrcweir 
isHighlighted()282cdf0e10cSrcweir     public boolean isHighlighted ()
283cdf0e10cSrcweir     {
284cdf0e10cSrcweir         return mbHighlighted;
285cdf0e10cSrcweir     }
286cdf0e10cSrcweir 
getBBox()287cdf0e10cSrcweir     public Rectangle getBBox ()
288cdf0e10cSrcweir     {
289cdf0e10cSrcweir         return new Rectangle (maPosition, maSize);
290cdf0e10cSrcweir     }
291cdf0e10cSrcweir 
getOrigin()292cdf0e10cSrcweir     public Point getOrigin ()
293cdf0e10cSrcweir     {
294cdf0e10cSrcweir         return maPosition;
295cdf0e10cSrcweir     }
296cdf0e10cSrcweir 
getPath()297cdf0e10cSrcweir     public TreePath getPath ()
298cdf0e10cSrcweir     {
299cdf0e10cSrcweir         return new TreePath (maNode.createPath());
300cdf0e10cSrcweir     }
301cdf0e10cSrcweir 
getRole()302cdf0e10cSrcweir     public int getRole ()
303cdf0e10cSrcweir     {
304cdf0e10cSrcweir         return mnRole;
305cdf0e10cSrcweir     }
306cdf0e10cSrcweir 
getContext()307cdf0e10cSrcweir     public XAccessibleContext getContext ()
308cdf0e10cSrcweir     {
309cdf0e10cSrcweir         return mxContext;
310cdf0e10cSrcweir     }
311cdf0e10cSrcweir 
getComponent()312cdf0e10cSrcweir     public XAccessibleComponent getComponent ()
313cdf0e10cSrcweir     {
314cdf0e10cSrcweir         return mxComponent;
315cdf0e10cSrcweir     }
316cdf0e10cSrcweir 
toString()317cdf0e10cSrcweir     public String toString ()
318cdf0e10cSrcweir     {
319cdf0e10cSrcweir         return ">"+msName+", "+msDescription+" +"+maPosition.x+"+"+maPosition.y
320cdf0e10cSrcweir             +"x"+maSize.width+"x"+maSize.height+"<";
321cdf0e10cSrcweir     }
322cdf0e10cSrcweir 
323cdf0e10cSrcweir     private AccTreeNode
324cdf0e10cSrcweir         maNode;
325cdf0e10cSrcweir     private XAccessibleContext
326cdf0e10cSrcweir         mxContext;
327cdf0e10cSrcweir     private XAccessibleComponent
328cdf0e10cSrcweir         mxComponent;
329cdf0e10cSrcweir     private String
330cdf0e10cSrcweir         msDescription,
331cdf0e10cSrcweir         msName;
332cdf0e10cSrcweir     private Rectangle2D.Double
333cdf0e10cSrcweir         maShape;
334cdf0e10cSrcweir     private Point
335cdf0e10cSrcweir         maPosition;
336cdf0e10cSrcweir     private Dimension
337cdf0e10cSrcweir         maTransformedSize,
338cdf0e10cSrcweir         maSize;
339cdf0e10cSrcweir     private Color
340cdf0e10cSrcweir         maFgColor,
341cdf0e10cSrcweir         maBgColor;
342cdf0e10cSrcweir     private boolean
343cdf0e10cSrcweir         // Highlighting objects is an internal concept.  Corresponds to selection in the tree view.
344cdf0e10cSrcweir         mbHighlighted,
345cdf0e10cSrcweir         // Set when the accessible object is selected.
346cdf0e10cSrcweir         mbSelected,
347cdf0e10cSrcweir         // Set when the accessible object is focused.
348cdf0e10cSrcweir         mbFocused;
349cdf0e10cSrcweir     private int
350cdf0e10cSrcweir         mnRole;
351cdf0e10cSrcweir }
352