Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / toolkit / test / accessibility / CanvasShape.java
blob71ae17d68887ad87658607ba8f69345d896e64df
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 import java.awt.*;
20 import javax.swing.tree.*;
21 import java.awt.geom.Rectangle2D;
23 import com.sun.star.accessibility.XAccessibleContext;
24 import com.sun.star.accessibility.XAccessibleComponent;
25 import com.sun.star.accessibility.XAccessibleText;
26 import com.sun.star.accessibility.AccessibleStateType;
28 class CanvasShape
30 private final Color maHighlightColor = Color.red;
31 private final Color maSelectionColor = Color.green;
32 private final Color maFocusColor = Color.blue;
34 public CanvasShape (AccTreeNode aNode)
36 maNode = aNode;
37 mxContext = aNode.getContext();
38 msName = "name unknown";
39 msDescription = "description unknown";
40 maShape = new Rectangle2D.Double (-10,-10,10,10);
41 maPosition = new Point (-10,-10);
42 maSize = new Dimension (10,10);
43 maFgColor = java.awt.Color.black;
44 maBgColor = Color.blue;
45 mbHighlighted = false;
46 mbSelected = false;
47 mbFocused = false;
48 mxComponent = aNode.getComponent();
50 update ();
55 /** Update the data obtained from the xAccessible.
57 public void update ()
59 if (mxContext != null)
61 msName = mxContext.getAccessibleName();
62 msDescription = mxContext.getAccessibleDescription();
64 // Extract the selected and focused flag.
65 long nStateSet = mxContext.getAccessibleStateSet ();
66 mbSelected = (nStateSet & AccessibleStateType.SELECTED) != 0;
67 mbFocused = (nStateSet & AccessibleStateType.FOCUSED) != 0;
70 updateGeometry ();
71 if (mxComponent != null)
73 // Note: alpha values in office 0..255 have to be mapped to
74 // 255..0 in Java
75 Color aCol = new Color (mxComponent.getForeground(), true);
76 maFgColor = new Color (aCol.getRed (),
77 aCol.getGreen (),
78 aCol.getBlue (),
79 0xff - aCol.getAlpha ());
80 aCol = new Color (mxComponent.getBackground(), true);
81 maBgColor = new Color (aCol.getRed (),
82 aCol.getGreen (),
83 aCol.getBlue (),
84 0xff - aCol.getAlpha ());
88 public void updateGeometry ()
90 if (mxComponent != null)
92 com.sun.star.awt.Point aLocationOnScreen = mxComponent.getLocationOnScreen();
93 com.sun.star.awt.Size aSizeOnScreen = mxComponent.getSize();
94 maPosition = new Point (
95 aLocationOnScreen.X,
96 aLocationOnScreen.Y);
97 maSize = new Dimension (
98 aSizeOnScreen.Width,
99 aSizeOnScreen.Height);
104 /** Paint the object into the specified canvas. It is transformed
105 according to the specified offset and scale.
107 public void paint (Graphics2D g,
108 double nXOffset, double nYOffset, double nScaleFactor,
109 boolean bShowDescription, boolean bShowName, boolean bShowText)
111 try{
112 // Transform the object's position and size according to the
113 // specified offset and scale.
114 maShape = new Rectangle2D.Double (
115 maPosition.x * nScaleFactor + nXOffset,
116 maPosition.y * nScaleFactor + nYOffset,
117 maSize.width * nScaleFactor,
118 maSize.height * nScaleFactor);
120 // Fill the object's bounding box with its background color if it
121 // has no children.
122 if (mxContext.getAccessibleChildCount() == 0)
124 g.setColor (maBgColor);
125 g.fill (maShape);
128 // Remove alpha channel from color before drawing the frame.
129 Color color = maFgColor;
130 if (maFgColor.getAlpha()<128)
131 color = new Color (maFgColor.getRed(), maFgColor.getGreen(), maFgColor.getBlue());
132 g.setColor (color);
133 g.draw (maShape);
135 if (mbFocused)
137 g.setColor (maFocusColor);
138 for (int x=0; x<=2; x++)
139 for (int y=0; y<=2; y++)
140 g.fill (
141 new Rectangle2D.Double (
142 maShape.x + x/2.0 * maShape.width-3,
143 maShape.y + y/2.0 * maShape.height-3,
145 6));
147 if (mbSelected)
149 g.setColor (maSelectionColor);
150 for (int x=0; x<=2; x++)
151 for (int y=0; y<=2; y++)
152 g.draw (
153 new Rectangle2D.Double (
154 maShape.x + x/2.0 * maShape.width-2,
155 maShape.y + y/2.0 * maShape.height-2,
157 4));
160 // Write the object's text OR name and description.
161 g.setColor (maFgColor);
162 if (bShowName)
163 paintName (g);
164 if (bShowDescription)
165 paintDescription (g);
166 if (bShowText)
167 paintText (g);
169 catch (Exception e)
170 { // don't care
174 public void paint_highlight (Graphics2D g)
176 if (mbHighlighted)
177 g.setColor (maHighlightColor);
178 else
179 g.setColor (maFgColor);
180 g.draw (maShape);
186 private void paintName (Graphics2D g)
188 g.drawString ("Name: " + msName,
189 (float)maShape.x+5,
190 (float)maShape.y+15);
195 private void paintDescription (Graphics2D g)
197 g.drawString ("Description: " + msDescription,
198 (float)maShape.x+5,
199 (float)maShape.y+35);
205 private void paintText (Graphics2D g)
207 XAccessibleText xText = null;
208 // get XAccessibleText
209 xText = maNode.getText();
211 // Draw every character in the text string.
212 if (xText != null)
214 String sText = xText.getText();
217 for(int i = 0; i < sText.length(); i++)
219 com.sun.star.awt.Rectangle aRect =
220 xText.getCharacterBounds(i);
222 double x = maShape.x + aRect.X;
223 double y = maShape.y + aRect.Y + aRect.Height;
225 g.drawString(sText.substring(i, i+1), (float)x, (float)y);
228 catch (com.sun.star.lang.IndexOutOfBoundsException e)
236 /** Compute whether the specified point lies inside the object's
237 bounding box.
239 public boolean contains (int x, int y)
241 return maShape.contains (x,y);
244 public void highlight ()
246 mbHighlighted = true;
249 public void unhighlight ()
251 mbHighlighted = false;
254 public Rectangle getBBox ()
256 return new Rectangle (maPosition, maSize);
259 public TreePath getPath ()
261 return new TreePath (maNode.createPath());
264 @Override
265 public String toString ()
267 return ">"+msName+", "+msDescription+" +"+maPosition.x+"+"+maPosition.y
268 +"x"+maSize.width+"x"+maSize.height+"<";
271 private final AccTreeNode maNode;
272 private final XAccessibleContext mxContext;
273 private final XAccessibleComponent mxComponent;
274 private String msDescription, msName;
275 private Rectangle2D.Double maShape;
276 private Point maPosition;
277 private Dimension maSize;
278 private Color maFgColor, maBgColor;
279 private boolean
280 // Highlighting objects is an internal concept. Corresponds to selection in the tree view.
281 mbHighlighted,
282 // Set when the accessible object is selected.
283 mbSelected,
284 // Set when the accessible object is focused.
285 mbFocused;