2013-02-26 22 views
0

在EditorPalette中,模板使用可以拖放到mxGraphComponent中的JLabels表示,對吧?JGraphX中的JTree

不過,我想用通過JTree的層次結構,以增加這些模板的EditorPalette,節點不能被拖動並像普通模板GraphComponents下降

你能幫我提供的功能用於在組件左側添加JTree並在mxGraphComponent上拖放的添加模板?

回答

1

好的,我明白了。我的錯誤是,我必須創建一個mxCell提供幾何信息,所以不是

mxCell cell = new mxCell(component); 
在上面的代碼,我

mxCell cell = new mxCell(component, new mxGeometry(), ""); 

完整的方法來添加拖動功能看起來像此:

public void addComponentTabListeners() { 
    final JTree componentTree = view.componentTree; 

    DragGestureListener dragGestureListener = new DragGestureListener() { 
     @Override 
     public void dragGestureRecognized(DragGestureEvent arg0) { 
      TreePath path = componentTree.getSelectionPath(); 
      if ((path == null) || (path.getPathCount() <= 1)) { 
       return; 
      } 
      DefaultMutableTreeNode componentsNode = (DefaultMutableTreeNode) path.getLastPathComponent(); 
      I_Component component = null; 
      try { 
       component = ComponentHandler_KonsensApplication.getInstance().createInstance(
         componentsNode.toString(), "need unique Name here"); 
      } catch (ClassNotFoundException e) { 
       e.printStackTrace(); 
      } catch (InstantiationException e) { 
       e.printStackTrace(); 
      } catch (IllegalAccessException e) { 
       e.printStackTrace(); 
      } 
      mxCell cell = new mxCell(component, new mxGeometry(), ""); 

      cell.setVertex(true); 
      mxRectangle bounds = new mxRectangle(); 
      bounds.setHeight(80); 
      bounds.setWidth(80); 
      mxGraphTransferable t = new mxGraphTransferable(new Object[] { cell }, bounds); 
      arg0.startDrag(null, mxSwingConstants.EMPTY_IMAGE, new Point(), t, null); 
     } 
    }; 

    DragSource dragSource = new DragSource(); 
    dragSource.createDefaultDragGestureRecognizer(componentTree, DnDConstants.ACTION_COPY, 
      dragGestureListener); 
}