summaryrefslogtreecommitdiff
path: root/accessibility/workben/org/openoffice/accessibility/awb/canvas/MouseObserver.java
blob: 3e7e2807906d66f7d92d5c27060bc389e9846652 (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
package org.openoffice.accessibility.awb.canvas;

import java.awt.Dimension;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Iterator;
import javax.swing.tree.TreePath;


/** Observe the mouse and highlight shapes of the canvas when clicked.
*/
public class MouseObserver
    implements MouseListener,
               MouseMotionListener
{
    public MouseObserver (Canvas aCanvas)
    {
        maCanvas = aCanvas;
        maCanvas.addMouseListener (this);
        maCanvas.addMouseMotionListener (this);
    }


    public void SetTree (javax.swing.JTree aTree)
    {
        maTree = aTree;
    }

    public void mouseClicked (MouseEvent e)
    {}

    public void mousePressed (MouseEvent e)
    {
        CanvasShape aObjectUnderMouse = FindCanvasShapeUnderMouse (e);
        maTree.clearSelection();
        if (aObjectUnderMouse != null)
        {
            TreePath aPath = aObjectUnderMouse.getNodePath();
            if ((e.getModifiers() & InputEvent.CTRL_MASK) != 0)
                maTree.expandPath (aPath);
            // Selecting the entry will eventually highlight the shape.
            maTree.setSelectionPath (aPath);
            maTree.makeVisible (aPath);
        }
    }

    public void mouseReleased (MouseEvent e)
    {}

    public void mouseEntered (MouseEvent e)
    {}

    public void mouseExited (MouseEvent e)
    {}

    public void mouseDragged (MouseEvent e)
    {
    }

    public void mouseMoved (MouseEvent e)
    {
        if ((e.getModifiers() & InputEvent.SHIFT_MASK) != 0)
            maCanvas.HighlightObject (FindCanvasShapeUnderMouse (e));
    }


    /** Search for the smallest shape that contains the mouse position.
    */
    protected CanvasShape FindCanvasShapeUnderMouse (MouseEvent e)
    {
        Dimension aSmallestSize = null;
        Iterator maShapeIterator = maCanvas.GetShapeIterator();
        CanvasShape aShapeUnderMouse = null;
        while (maShapeIterator.hasNext())
        {
            CanvasShape aShape = (CanvasShape)maShapeIterator.next();
            if (aShape != null)
                if (aShape.Contains (e.getX(),e.getY()))
                {
                    if (aShapeUnderMouse == null)
                    {
                        aSmallestSize = aShape.GetSize();
                        aShapeUnderMouse = aShape;
                    }
                    else
                    {
                        Dimension aSize = aShape.GetSize();
                        if (aSize.getWidth()<aSmallestSize.getWidth()
                            || aSize.getHeight()<aSmallestSize.getHeight())
                        {
                            aSmallestSize = aSize;
                            aShapeUnderMouse = aShape;
                        }
                    }
                }
        }
        return aShapeUnderMouse;
    }

    private Canvas maCanvas;
    private javax.swing.JTree maTree;
}