我目前正在为eclipse开发一个使用Zest显示树的插件。
我尝试向显示节点的图形添加自定义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();
}有人能为我的代码提供修复或变通方法吗?
发布于 2012-07-09 01:46:54
在Debug Visualization项目中,我们添加了一个双击侦听器,同时仍然支持拖动。
我们的代码在http://code.google.com/a/eclipselabs.org/p/debugvisualisation/source/browse/hu.cubussapiens.debugvisualisation/src/hu/cubussapiens/debugvisualisation/views/DebugVisualisationView.java中的第159行
// double click on nodes
graphViewer.getGraphControl().addMouseListener(new MouseAdapter() {
@Override
public void mouseDoubleClick(MouseEvent e) {
toggleOpen.run();
}
});您可以从MouseEvent中读取选定的节点(如果我没有记错的话),也可以检查当前的选择(这是我们在项目中采用的方法)。
https://stackoverflow.com/questions/11379765
复制相似问题