0

In the EditorPalette, the templates are represented using JLabels which can be dragged and dropped in the mxGraphComponent, right ?

However, I want to add these templates to the EditorPalette using a hierarchical structure via JTree, and the nodes can't be dragged and dropped in the GraphComponents like the ordinary templates

Can you help me by providing function of a add template for adding JTree on the left side of a component and drag and dropping on mxGraphComponent?

Frodo Baggins
  • 8,290
  • 6
  • 45
  • 55

1 Answers1

1

Okay, I got it. My mistake was, that I have to create an mxCell providing geometry information, so instead of

mxCell cell = new mxCell(component);

in the upper code I have to write

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

The complete method to add the drag functionality looks like this:

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);
}