summaryrefslogtreecommitdiff
path: root/toolkit/test/accessibility/Canvas.java
blob: afacc026533055df5d009c8a547896b1fa1b7f71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import java.util.Vector;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
import drafts.com.sun.star.accessibility.XAccessibleContext;

/** This canvas displays accessible objects graphically.  Each accessible
    object with graphical representation is represented by an
    AccessibleObject object and has to be added by the
    <member>addAccessible</member> member function.
*/
class Canvas
    extends JPanel
    implements MouseListener, MouseMotionListener
{
    public MessageInterface maMessageDisplay;
    public final int nMaximumWidth = 1000;
    public final int nMaximumHeight = 1000;

    public static boolean bPaintText = false;

    public Canvas (MessageInterface aMessageDisplay, JTree aTree)
    {
        super (true);
        maObjects = new Vector ();
        maContexts = new Vector ();
        addMouseListener (this);
        addMouseMotionListener (this);
        maBoundingBox = new Rectangle (0,0,100,100);
        setPreferredSize (maBoundingBox.getSize());
        setSize (nMaximumWidth,nMaximumHeight);
        maMessageDisplay = aMessageDisplay;
        maTree = aTree;
        mnXOffset = 0;
        mnYOffset = 0;
        mnScaleFactor = 1;
    }

    public void addAccessible (AccessibleObject aObject)
    {
        if( maObjects.indexOf( aObject ) == -1 )
        {
            maObjects.add (aObject);
            maContexts.add (aObject.getContext());
        }
        maBoundingBox = maBoundingBox.union (aObject.getBBox());
    }

    public void removeAccessible (AccessibleObject aObject)
    {
        maObjects.remove (aObject);
        maContexts.remove (aObject.getContext());
    }


    public void addContext(XAccessibleContext xContext, TreePath aPath)
    {
        if( maContexts.indexOf( xContext ) == -1 )
            addAccessible( new AccessibleObject( xContext, aPath ) );
    }

    public void removeContext(XAccessibleContext xContext)
    {
        int i = maContexts.indexOf( xContext );
        if( i != -1 )
            removeAccessible( (AccessibleObject)maObjects.elementAt( i ) );
    }

    public void updateContext(XAccessibleContext aContext)
    {
        int i = maContexts.indexOf( aContext );
        if( i != -1 )
            ((AccessibleObject)maObjects.elementAt( i )).update();
    }

    public void clear ()
    {
        maObjects.clear();
        maContexts.clear();
    }

    public void paintComponent (Graphics g)
    {
        Rectangle r = g.getClipBounds();
        g.clearRect (r.x,r.y,r.width,r.height);

        // Recalculate scale and offset so that all accessible objects fit
        // into the area specified by nMaximum(Width,Height)
        double nXScale = 1,
            nYScale = 1;
        if (maBoundingBox.getWidth() > nMaximumWidth)
            nXScale = 1.0 * nMaximumWidth / maBoundingBox.getWidth();
        if (maBoundingBox.getHeight() > nMaximumHeight)
            nYScale = 1.0 * nMaximumHeight / maBoundingBox.getHeight();
        if (nXScale < nYScale)
            mnScaleFactor = nXScale;
        else
            mnScaleFactor = nYScale;

        int n = maObjects.size();
        for (int i=0; i<n; i++)
            ((AccessibleObject)(maObjects.elementAt(i))).paint (
                g,
                (int)-maBoundingBox.getX(),
                (int)-maBoundingBox.getY(),
                mnScaleFactor);
    }

    public void mouseClicked (MouseEvent e)
    {
    }

    public void mousePressed (MouseEvent e)
    {
        mnXAnchor = e.getX();
        mnYAnchor = e.getY();
        int n = maObjects.size();
        for (int i=0; i<n; i++)
        {
            AccessibleObject aObject = (AccessibleObject)(maObjects.elementAt(i));
            if (aObject != null && aObject.contains (e.getX(),e.getY()))
            {
                maActiveObject = aObject;
                maResizeFlag = aObject.getResizeFlag (e.getX(),e.getY());
            }
        }
    }

    public void mouseReleased (MouseEvent e)
    {
    }

    public void mouseEntered (MouseEvent e)
    {
    }

    public void mouseExited (MouseEvent e)
    {
        int n = maObjects.size();
        for (int i=0; i<n; i++)
        {
            AccessibleObject aObject = (AccessibleObject)(maObjects.elementAt(i));
            if (aObject != null)
                aObject.deselect ();
        }
        repaint ();
    }

    public void mouseDragged (MouseEvent e)
    {
        if (maActiveObject != null)
        {
            int dx = e.getX() - mnXAnchor;
            int dy = e.getY() - mnYAnchor;

            if ((e.getModifiers() & MouseEvent.BUTTON1_MASK) != 0)
            {
                maActiveObject.translate (dx,dy);
            }
            else if ((e.getModifiers() & MouseEvent.BUTTON2_MASK) != 0)
            {
                maActiveObject.resize (maResizeFlag, dx,dy);
            }

            mnXAnchor = e.getX();
            mnYAnchor = e.getY();

            repaint ();
        }
    }

    public void mouseMoved (MouseEvent e)
    {
        maActiveObject = null;
        for (int i=0; i<maObjects.size(); i++)
        {
            AccessibleObject aObject = (AccessibleObject)(maObjects.elementAt(i));
            if (aObject != null)
            {
                aObject.deselect ();
                if (aObject != null)
                    if (aObject.contains (e.getX(),e.getY()))
                    {
                        maActiveObject = aObject;
                    }
            }
        }
        if (maActiveObject != null)
        {
            maActiveObject.select ();
            maMessageDisplay.message ("mouse moved to " + e.getX() + "," + e.getY() + ": "
                +maActiveObject.toString());
            System.out.println ("path: " + maActiveObject.getPath());

            if (maTree != null)
            {
                maTree.scrollPathToVisible (maActiveObject.getPath());
                maTree.repaint ();
            }
        }
        repaint ();
    }

    protected int
        mnXAnchor,
        mnYAnchor,
        maResizeFlag,
        mnXOffset,
        mnYOffset;
    protected double
        mnScaleFactor;
    protected AccessibleObject
        maActiveObject;
    protected Vector
        maObjects,
        maContexts;
    protected Rectangle
        maBoundingBox;
    protected JTree
        maTree;
}