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 .
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
;
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
)
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
;
48 mbHighlighted
= false;
51 mxComponent
= aNode
.getComponent();
58 /** Update the data obtained from the xAccessible.
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
);
78 if (mxComponent
!= null)
80 // Note: alpha values in office 0..255 have to be mapped to
82 Color aCol
= new Color (mxComponent
.getForeground(), true);
83 maFgColor
= new Color (aCol
.getRed (),
86 0xff - aCol
.getAlpha ());
87 aCol
= new Color (mxComponent
.getBackground(), true);
88 maBgColor
= new Color (aCol
.getRed (),
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 (
103 aLocationOnScreen
.Y
);
104 maSize
= new Dimension (
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
)
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
130 if (mxContext
.getAccessibleChildCount() == 0)
132 g
.setColor (maBgColor
);
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());
145 g
.setColor (maFocusColor
);
146 for (int x
=0; x
<=2; x
++)
147 for (int y
=0; y
<=2; y
++)
149 new Rectangle2D
.Double (
150 maShape
.x
+ x
/2.0 * maShape
.width
-3,
151 maShape
.y
+ y
/2.0 * maShape
.height
-3,
157 g
.setColor (maSelectionColor
);
158 for (int x
=0; x
<=2; x
++)
159 for (int y
=0; y
<=2; y
++)
161 new Rectangle2D
.Double (
162 maShape
.x
+ x
/2.0 * maShape
.width
-2,
163 maShape
.y
+ y
/2.0 * maShape
.height
-2,
168 // Write the object's text OR name and description.
169 g
.setColor (maFgColor
);
172 if (bShowDescription
)
173 paintDescription (g
);
182 public void paint_highlight (Graphics2D g
,
183 double nXOffset
, double nYOffset
, double nScaleFactor
)
186 g
.setColor (maHighlightColor
);
188 g
.setColor (maFgColor
);
195 private void paintName (Graphics2D g
)
197 g
.drawString ("Name: " + msName
,
199 (float)maShape
.y
+15);
204 private void paintDescription (Graphics2D g
)
206 g
.drawString ("Description: " + msDescription
,
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.
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
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 ()
288 public TreePath
getPath ()
290 return new TreePath (maNode
.createPath());
293 public int getRole ()
298 public XAccessibleContext
getContext ()
303 public XAccessibleComponent
getComponent ()
308 public String
toString ()
310 return ">"+msName
+", "+msDescription
+" +"+maPosition
.x
+"+"+maPosition
.y
311 +"x"+maSize
.width
+"x"+maSize
.height
+"<";
316 private XAccessibleContext
318 private XAccessibleComponent
323 private Rectangle2D
.Double
334 // Highlighting objects is an internal concept. Corresponds to selection in the tree view.
336 // Set when the accessible object is selected.
338 // Set when the accessible object is focused.