merge the formfield patch from ooo-build
[ooovba.git] / odk / examples / DevelopersGuide / Accessibility / GraphicalDisplay.java
blob302826a06cc7d150d3dd31af285e00632119585c
1 /*************************************************************************
3 * $RCSfile: GraphicalDisplay.java,v $
5 * $Revision: 1.3 $
7 * last change: $Author: hr $ $Date: 2003-06-30 15:06:08 $
9 * The Contents of this file are made available subject to the terms of
10 * the BSD license.
12 * Copyright (c) 2003 by Sun Microsystems, Inc.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *************************************************************************/
41 import javax.swing.*;
42 import java.awt.Dimension;
43 import java.awt.*;
44 import java.awt.geom.*;
46 import com.sun.star.accessibility.XAccessible;
47 import com.sun.star.accessibility.XAccessibleContext;
48 import com.sun.star.accessibility.XAccessibleComponent;
50 import com.sun.star.awt.Point;
51 import com.sun.star.awt.Size;
52 import com.sun.star.uno.UnoRuntime;
55 /** Display the currently focused accessible object graphically.
57 public class GraphicalDisplay
58 extends JPanel
59 implements IAccessibleObjectDisplay
61 /** Create a new graphical widget the displays some of the geometrical
62 information availbable from accessible objects.
64 public GraphicalDisplay ()
66 setPreferredSize (new Dimension (300,200));
70 /** Paint some or all of the area of this widget with the outlines of
71 the currently focues object and its ancestors.
73 public synchronized void paintComponent (Graphics g)
75 super.paintComponent (g);
77 setupTransformation ();
79 // Draw the screen representation to give a hint of the location of the
80 // accessible object on the screen.
81 Dimension aScreenSize = Toolkit.getDefaultToolkit().getScreenSize();
82 // Fill the screen rectangle.
83 g.setColor (new Color (250,240,230));
84 g.fillRect (
85 (int)(mnHOffset+0.5),
86 (int)(mnVOffset+0.5),
87 (int)(mnScale*aScreenSize.getWidth()),
88 (int)(mnScale*aScreenSize.getHeight()));
89 // Draw a frame arround the screen rectangle to increase its visibility.
90 g.setColor (Color.BLACK);
91 g.drawRect (
92 (int)(mnHOffset+0.5),
93 (int)(mnVOffset+0.5),
94 (int)(mnScale*aScreenSize.getWidth()),
95 (int)(mnScale*aScreenSize.getHeight()));
97 // Now do the actual display of the accessible object.
98 drawAccessibleObject (g, mxContext, Color.GREEN);
100 public synchronized void paintChildren (Graphics g)
103 public synchronized void paintBorder (Graphics g)
110 /** Draw a simple representation of the given accessible object in the
111 specified color.
113 public void drawAccessibleObject (Graphics g, XAccessibleContext xContext, Color aColor)
115 if (xContext != null)
117 // First draw our parent.
118 XAccessible xParent = xContext.getAccessibleParent();
119 if (xParent != null)
120 drawAccessibleObject (g, xParent.getAccessibleContext(), Color.GRAY);
122 // When the context supports the XAccessibleComponent interface
123 // then draw its outline.
124 XAccessibleComponent xComponent =
125 (XAccessibleComponent)UnoRuntime.queryInterface(
126 XAccessibleComponent.class, xContext);
127 if (xComponent != null)
129 // Get size and location on screen and transform them to fit
130 // everything inside this widget.
131 Point aLocation = xComponent.getLocationOnScreen();
132 Size aSize = xComponent.getSize();
133 g.setColor (aColor);
134 g.drawRect (
135 (int)(mnHOffset + mnScale*aLocation.X+0.5),
136 (int)(mnVOffset + mnScale*aLocation.Y+0.5),
137 (int)(mnScale*aSize.Width),
138 (int)(mnScale*aSize.Height));
144 public synchronized void setAccessibleObject (XAccessibleContext xContext)
146 mxContext = xContext;
147 repaint ();
150 public synchronized void updateAccessibleObject (XAccessibleContext xContext)
152 repaint ();
156 /** Set up the transformation so that the graphical display can show a
157 centered representation of the whole screen.
159 private void setupTransformation ()
161 Dimension aScreenSize = Toolkit.getDefaultToolkit().getScreenSize();
162 Dimension aWidgetSize = getSize();
163 if ((aScreenSize.getWidth() > 0) && (aScreenSize.getHeight() > 0))
165 // Calculate the scales that would map the screen onto the
166 // widget in both of the coordinate axes and select the smaller
167 // of the two: it maps the screen onto the widget in both axes
168 // at the same time.
169 double nHScale = (aWidgetSize.getWidth() - 10) / aScreenSize.getWidth();
170 double nVScale = (aWidgetSize.getHeight() - 10) / aScreenSize.getHeight();
171 if (nHScale < nVScale)
172 mnScale = nHScale;
173 else
174 mnScale = nVScale;
176 // Calculate offsets that center the scaled screen inside the widget.
177 mnHOffset = (aWidgetSize.getWidth() - mnScale*aScreenSize.getWidth()) / 2.0;
178 mnVOffset = (aWidgetSize.getHeight() - mnScale*aScreenSize.getHeight()) / 2.0;
180 else
182 // In case of a degenerate (not yet initialized?) screen size
183 // use some meaningless default values.
184 mnScale = 1;
185 mnHOffset = 0;
186 mnVOffset = 0;
191 private XAccessibleContext mxContext;
192 private double mnScale;
193 private double mnHOffset;
194 private double mnVOffset;