0

在 EditorPalette 中,模板使用 JLabels 表示,可以在 mxGraphComponent 中拖放,对吧?

但是,我想通过JTree使用层次结构将这些模板添加到EditorPalette中,并且节点不能像普通模板那样在GraphComponents中拖放

您能否通过提供添加模板的功能来帮助我,以便在组件左侧添加 JTree 并在 mxGraphComponent 上拖放?

4

1 回答 1

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);
}
于 2013-08-30T18:34:49.390 回答