我目前正在开发一个使用 Zest 显示树的 eclipse 插件。
我尝试向显示节点的图形添加自定义 MouseListener
,因为我想添加双击功能,但这会覆盖允许节点被拖动的自然存在的功能。
我曾尝试添加基于 Draw2D 的拖动功能,但没有成功。这是我试过的代码:
private Point location;
public void mousePressed(MouseEvent me) {
location = me.getLocation();
me.consume();
}
public void mouseReleased(MouseEvent me) {
location = null;
me.consume();
}
public void mouseDragged(MouseEvent me) {
if (location == null) {
return;
}
Point moved= me.getLocation();
if (moved == null) {
return;
}
Dimension offset= moved.getDifference(location);
if (offset.width == 0 && offset.height == 0) {
return;
}
location= moved;
UpdateManager uMgr= figure.getUpdateManager();
LayoutManager lMgr= figure.getLayoutManager();
Rectangle bounds= figure.getBounds();
uMgr.addDirtyRegion(figure.getParent(), bounds);
bounds= bounds.getCopy().translate(offset.width, offset.height);
lMgr.setConstraint(figure, bounds);
figure.translate(offset.width, offset.height);
uMgr.addDirtyRegion(figure.getParent(), bounds);
me.consume();
}
任何人都可以为我的代码提供修复或解决方法吗?
最佳答案
在调试可视化项目中,我们添加了一个双击监听器,同时保留了拖动支持。
// double click on nodes
graphViewer.getGraphControl().addMouseListener(new MouseAdapter() {
@Override
public void mouseDoubleClick(MouseEvent e) {
toggleOpen.run();
}
});
您可以从 MouseEvent 中读取所选节点(如果我没记错的话),或者您可以检查当前选择(这是我们在项目中采用的方法)。
关于java - 将 MouseListener 添加到 Zest Graph,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11379765/