2012-07-08 67 views
1

我目前正在開發一個eclipse插件,它使用Zest顯示一棵樹。添加MouseListener到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(); 
} 

任何人都可以提供我的代碼或解決方法的修復?

回答

3

在調試可視化項目中,我們添加了雙擊偵聽器,同時拖動支持仍然存在。

我們的代碼是行159 http://code.google.com/a/eclipselabs.org/p/debugvisualisation/source/browse/hu.cubussapiens.debugvisualisation/src/hu/cubussapiens/debugvisualisation/views/DebugVisualisationView.java

// double click on nodes 
    graphViewer.getGraphControl().addMouseListener(new MouseAdapter() { 

      @Override 
      public void mouseDoubleClick(MouseEvent e) { 
       toggleOpen.run(); 
      } 
    }); 

您既可以讀取的MouseEvent選擇的節點(如果我沒有記錯的話),或者你可以檢查當前的選擇(也就是我們還沒有參與這個項目)。

相關問題