2013-02-26 2 views
0

В редактореPalette шаблоны представлены с использованием JLabels, которые можно перетаскивать и удалять в mxGraphComponent, правильно?JTree in JGraphX ​​

Однако, я хочу, чтобы добавить эти шаблоны к EditorPalette с использованием иерархической структуры с помощью JTree, и узлы не могут быть перетаскивать в 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); 
}