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