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
.AccessibleStateType
;
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
)
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;
48 mxComponent
= aNode
.getComponent();
55 /** Update the data obtained from the xAccessible.
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;
71 if (mxComponent
!= null)
73 // Note: alpha values in office 0..255 have to be mapped to
75 Color aCol
= new Color (mxComponent
.getForeground(), true);
76 maFgColor
= new Color (aCol
.getRed (),
79 0xff - aCol
.getAlpha ());
80 aCol
= new Color (mxComponent
.getBackground(), true);
81 maBgColor
= new Color (aCol
.getRed (),
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 (
97 maSize
= new Dimension (
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
)
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
122 if (mxContext
.getAccessibleChildCount() == 0)
124 g
.setColor (maBgColor
);
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());
137 g
.setColor (maFocusColor
);
138 for (int x
=0; x
<=2; x
++)
139 for (int y
=0; y
<=2; y
++)
141 new Rectangle2D
.Double (
142 maShape
.x
+ x
/2.0 * maShape
.width
-3,
143 maShape
.y
+ y
/2.0 * maShape
.height
-3,
149 g
.setColor (maSelectionColor
);
150 for (int x
=0; x
<=2; x
++)
151 for (int y
=0; y
<=2; y
++)
153 new Rectangle2D
.Double (
154 maShape
.x
+ x
/2.0 * maShape
.width
-2,
155 maShape
.y
+ y
/2.0 * maShape
.height
-2,
160 // Write the object's text OR name and description.
161 g
.setColor (maFgColor
);
164 if (bShowDescription
)
165 paintDescription (g
);
174 public void paint_highlight (Graphics2D g
)
177 g
.setColor (maHighlightColor
);
179 g
.setColor (maFgColor
);
186 private void paintName (Graphics2D g
)
188 g
.drawString ("Name: " + msName
,
190 (float)maShape
.y
+15);
195 private void paintDescription (Graphics2D g
)
197 g
.drawString ("Description: " + msDescription
,
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.
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
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());
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
;
280 // Highlighting objects is an internal concept. Corresponds to selection in the tree view.
282 // Set when the accessible object is selected.
284 // Set when the accessible object is focused.