子图上的MouseMotionListener事件 - GEF

时间:2012-11-24 10:58:07

标签: java mouseevent eclipse-gef draw2d

我正在使用父draw2d图,其中包含多个子图(按钮。使用工具栏布局),注册到MouseMotionListener并在鼠标输入上执行一些操作(比如'enterAction')在鼠标出口处执行其他操作('exitAction “)点。

出于某种原因,在指向其中一个子按钮时调用'exitAction'。
我试图递归地将父图的所有子项注册到MouseMotionListener事件,虽然它“解决了我的问题”('exitAction'被解雇但我设法在按钮的'enterAction'中捕获它并处理它) - 另一个问题发生:按钮的'actionPerformed'事件(我已经添加了一个ActionListener按钮)没有被触发(或者是,但在某处我找不到......)。

有任何帮助吗? :\
谢谢!

1 个答案:

答案 0 :(得分:0)

这一般很难解决,因为在draw2d中鼠标事件不像html那样通过元素传播。

您可以将侦听器移动到画布上:

import org.eclipse.draw2d.ActionEvent;
import org.eclipse.draw2d.ActionListener;
import org.eclipse.draw2d.Border;
import org.eclipse.draw2d.Button;
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.FigureCanvas;
import org.eclipse.draw2d.Layer;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.draw2d.ScalableLayeredPane;
import org.eclipse.draw2d.XYLayout;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;

public class HHH {

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell();
    shell.setLayout(new FillLayout());

    final FigureCanvas canvas = new FigureCanvas(shell);
    final ScalableLayeredPane pane = new ScalableLayeredPane();
    pane.setScale(1.5f);

    canvas.setContents(pane);

    final Layer layer = new Layer();
    layer.setLayoutManager(new XYLayout());
    pane.add(layer);

    final Figure container = new Figure();
    container.setOpaque(true);
    container.setBackgroundColor(ColorConstants.lightBlue);
    container.setLayoutManager(new XYLayout());
    layer.add(container, new Rectangle(100, 100, 100, 100));

    final Button b = new Button("Push");
    b.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent event) {
            MessageBox box = new MessageBox(shell);
            box.setText("button pressed");
            box.open();
        }
    });
    container.add(b, new Rectangle(10, 10, -1, -1));

    canvas.addMouseMoveListener(new MouseMoveListener() {

        Border oldBorder = null;
        boolean over = false;

        @Override
        public void mouseMove(org.eclipse.swt.events.MouseEvent e) {
            final Rectangle containerBounds = container.getBounds().getCopy();
            container.translateToAbsolute(containerBounds);

            if (containerBounds.contains(e.x, e.y)) {
                // simulate enter container figure
                if (!over) {
                    oldBorder = container.getBorder();
                    container.setBorder(new LineBorder());
                    over = true;
                }
            } else {
                // simulate exit container figure
                if (over) {
                    container.setBorder(oldBorder);
                    oldBorder = null;
                    over = false;
                }
            }
        }

    });

    shell.setSize(500, 500);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }

}

}