fdo#74697 Add Bluez 5 support for impress remote.
[LibreOffice.git] / toolkit / test / accessibility / CanvasShape.java
blobf9a036bc543abe9944f3f80a5701b76560e5ebe5
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.XAccessibleStateSet;
27 import com.sun.star.accessibility.AccessibleStateType;
29 class CanvasShape
31 public final Color maHighlightColor = Color.red;
32 public final Color maSelectionColor = Color.green;
33 public final Color maFocusColor = Color.blue;
35 // public AccessibleObject (XAccessibleContext xContext, TreePath aPath)
36 public CanvasShape (AccTreeNode aNode)
38 maNode = aNode;
39 mxContext = aNode.getContext();
40 msName = "name unknown";
41 msDescription = "description unknown";
42 maShape = new Rectangle2D.Double (-10,-10,10,10);
43 maPosition = new Point (-10,-10);
44 maSize = new Dimension (10,10);
45 maFgColor = java.awt.Color.black;
46 maBgColor = Color.blue;
47 mnRole = -1;
48 mbHighlighted = false;
49 mbSelected = false;
50 mbFocused = false;
51 mxComponent = aNode.getComponent();
53 update ();
58 /** Update the data obtained from the xAccessible.
60 public void update ()
62 if (mxContext != null)
64 msName = mxContext.getAccessibleName();
65 msDescription = mxContext.getAccessibleDescription();
66 mnRole = mxContext.getAccessibleRole();
68 // Extract the selected and focused flag.
69 XAccessibleStateSet xStateSet = mxContext.getAccessibleStateSet ();
70 if (xStateSet != null)
72 mbSelected = xStateSet.contains (AccessibleStateType.SELECTED);
73 mbFocused = xStateSet.contains (AccessibleStateType.FOCUSED);
77 updateGeometry ();
78 if (mxComponent != null)
80 // Note: alpha values in office 0..255 have to be mapped to
81 // 255..0 in Java
82 Color aCol = new Color (mxComponent.getForeground(), true);
83 maFgColor = new Color (aCol.getRed (),
84 aCol.getGreen (),
85 aCol.getBlue (),
86 0xff - aCol.getAlpha ());
87 aCol = new Color (mxComponent.getBackground(), true);
88 maBgColor = new Color (aCol.getRed (),
89 aCol.getGreen (),
90 aCol.getBlue (),
91 0xff - aCol.getAlpha ());
95 public void updateGeometry ()
97 if (mxComponent != null)
99 com.sun.star.awt.Point aLocationOnScreen = mxComponent.getLocationOnScreen();
100 com.sun.star.awt.Size aSizeOnScreen = mxComponent.getSize();
101 maPosition = new Point (
102 aLocationOnScreen.X,
103 aLocationOnScreen.Y);
104 maSize = new Dimension (
105 aSizeOnScreen.Width,
106 aSizeOnScreen.Height);
111 /** Paint the object into the specified canvas. It is transformed
112 according to the specified offset and scale.
114 public void paint (Graphics2D g,
115 double nXOffset, double nYOffset, double nScaleFactor,
116 boolean bShowDescription, boolean bShowName, boolean bShowText)
118 try{
119 // Transform the object's position and size according to the
120 // specified offset and scale.
121 Point aLocation = new Point();
122 maShape = new Rectangle2D.Double (
123 maPosition.x * nScaleFactor + nXOffset,
124 maPosition.y * nScaleFactor + nYOffset,
125 maSize.width * nScaleFactor,
126 maSize.height * nScaleFactor);
128 // Fill the object's bounding box with its background color if it
129 // has no children.
130 if (mxContext.getAccessibleChildCount() == 0)
132 g.setColor (maBgColor);
133 g.fill (maShape);
136 // Remove alpha channel from color before drawing the frame.
137 Color color = maFgColor;
138 if (maFgColor.getAlpha()<128)
139 color = new Color (maFgColor.getRed(), maFgColor.getGreen(), maFgColor.getBlue());
140 g.setColor (color);
141 g.draw (maShape);
143 if (mbFocused)
145 g.setColor (maFocusColor);
146 for (int x=0; x<=2; x++)
147 for (int y=0; y<=2; y++)
148 g.fill (
149 new Rectangle2D.Double (
150 maShape.x + x/2.0 * maShape.width-3,
151 maShape.y + y/2.0 * maShape.height-3,
153 6));
155 if (mbSelected)
157 g.setColor (maSelectionColor);
158 for (int x=0; x<=2; x++)
159 for (int y=0; y<=2; y++)
160 g.draw (
161 new Rectangle2D.Double (
162 maShape.x + x/2.0 * maShape.width-2,
163 maShape.y + y/2.0 * maShape.height-2,
165 4));
168 // Write the object's text OR name and description.
169 g.setColor (maFgColor);
170 if (bShowName)
171 paintName (g);
172 if (bShowDescription)
173 paintDescription (g);
174 if (bShowText)
175 paintText (g);
177 catch (Exception e)
178 { // don't care
182 public void paint_highlight (Graphics2D g,
183 double nXOffset, double nYOffset, double nScaleFactor)
185 if (mbHighlighted)
186 g.setColor (maHighlightColor);
187 else
188 g.setColor (maFgColor);
189 g.draw (maShape);
195 private void paintName (Graphics2D g)
197 g.drawString ("Name: " + msName,
198 (float)maShape.x+5,
199 (float)maShape.y+15);
204 private void paintDescription (Graphics2D g)
206 g.drawString ("Description: " + msDescription,
207 (float)maShape.x+5,
208 (float)maShape.y+35);
214 private void paintText (Graphics2D g)
216 XAccessibleText xText = null;
217 // get XAccessibleText
218 xText = maNode.getText();
220 // Draw every character in the text string.
221 if (xText != null)
223 String sText = xText.getText();
226 for(int i = 0; i < sText.length(); i++)
228 com.sun.star.awt.Rectangle aRect =
229 xText.getCharacterBounds(i);
231 double x = maShape.x + aRect.X;
232 double y = maShape.y + aRect.Y + aRect.Height;
234 g.drawString(sText.substring(i, i+1), (float)x, (float)y);
237 catch (com.sun.star.lang.IndexOutOfBoundsException e)
245 /** Callback for disposing events.
247 public void disposing (com.sun.star.lang.EventObject e)
249 System.out.println ("Disposing");
255 /** Compute whether the specified point lies inside the object's
256 bounding box.
258 public boolean contains (int x, int y)
260 return (maShape.contains (x,y));
263 public void highlight ()
265 mbHighlighted = true;
268 public void unhighlight ()
270 mbHighlighted = false;
273 public boolean isHighlighted ()
275 return mbHighlighted;
278 public Rectangle getBBox ()
280 return new Rectangle (maPosition, maSize);
283 public Point getOrigin ()
285 return maPosition;
288 public TreePath getPath ()
290 return new TreePath (maNode.createPath());
293 public int getRole ()
295 return mnRole;
298 public XAccessibleContext getContext ()
300 return mxContext;
303 public XAccessibleComponent getComponent ()
305 return mxComponent;
308 public String toString ()
310 return ">"+msName+", "+msDescription+" +"+maPosition.x+"+"+maPosition.y
311 +"x"+maSize.width+"x"+maSize.height+"<";
314 private AccTreeNode
315 maNode;
316 private XAccessibleContext
317 mxContext;
318 private XAccessibleComponent
319 mxComponent;
320 private String
321 msDescription,
322 msName;
323 private Rectangle2D.Double
324 maShape;
325 private Point
326 maPosition;
327 private Dimension
328 maTransformedSize,
329 maSize;
330 private Color
331 maFgColor,
332 maBgColor;
333 private boolean
334 // Highlighting objects is an internal concept. Corresponds to selection in the tree view.
335 mbHighlighted,
336 // Set when the accessible object is selected.
337 mbSelected,
338 // Set when the accessible object is focused.
339 mbFocused;
340 private int
341 mnRole;