如何通过拖放将TreePanel的根节点添加到另一个TreePanel中?

时间:2012-01-16 16:00:48

标签: java gwt-ext treepanel

我想使用拖放将一棵树的根节点添加到另一棵树中。我用于此任务的示例位于以下URL:

http://gwt-ext.com/demo/#twoTrees

请尽快帮助我。

1 个答案:

答案 0 :(得分:1)

在同一个链接中。代码已经给出。

public class TwoTreesSample implements EntryPoint {  

    public void onModuleLoad() {  
        Panel panel = new Panel();  
        panel.setBorder(false);  
        panel.setPaddings(15);  

        //create form for nody drop style  
        FormPanel formPanel = new FormPanel();  
        formPanel.setBorder(false);  
        formPanel.setHideLabels(true);  

        FieldSet fieldSet = new FieldSet("Drop style");  
        fieldSet.setWidth(420);  
        formPanel.add(fieldSet);  

        final Radio moveRadio = new Radio();  
        moveRadio.setName("dropstyle");  
        moveRadio.setBoxLabel("Move");  
        moveRadio.setChecked(true);  
        fieldSet.add(moveRadio);  

        final Radio copyRadio = new Radio();  
        copyRadio.setName("dropstyle");  
        copyRadio.setBoxLabel("Copy");  
        fieldSet.add(copyRadio);  

        //create source countries tree  
        final TreePanel treePanel = new TreePanel();  
        treePanel.setTitle("Countries");  
        treePanel.setAnimate(true);  
        treePanel.setEnableDD(true);  
        treePanel.setContainerScroll(true);  
        treePanel.setRootVisible(true);  
        treePanel.setWidth(200);  
        treePanel.setHeight(400);  
        treePanel.setSelectionModel(new MultiSelectionModel());  

        final XMLTreeLoader loader = new XMLTreeLoader();  
        loader.setDataUrl("data/countries-grouped.xml");  
        loader.setMethod(Connection.GET);  
        loader.setRootTag("countries");  
        loader.setFolderTitleMapping("@title");  
        loader.setFolderTag("continent");  
        loader.setLeafTitleMapping("@title");  
        loader.setLeafTag("country");  
        loader.setQtipMapping("@qtip");  
        loader.setIconMapping("@icon");  

        AsyncTreeNode root = new AsyncTreeNode("Countries", loader);  
        treePanel.setRootNode(root);  
        root.expand();  
        treePanel.expandAll();  

        //create target vacation tree  
        final TreePanel tripTreePanel = new TreePanel();  
        tripTreePanel.setTitle("Trip Planner");  
        tripTreePanel.setAnimate(true);  
        tripTreePanel.setEnableDD(true);  
        tripTreePanel.setContainerScroll(true);  
        tripTreePanel.setRootVisible(true);  
        tripTreePanel.setWidth(200);  
        tripTreePanel.setHeight(400);  

        final XMLTreeLoader tripLoader = new XMLTreeLoader();  
        tripLoader.setDataUrl("data/trip.xml");  
        tripLoader.setMethod(Connection.GET);  
        tripLoader.setRootTag("vacations");  
        tripLoader.setFolderIdMapping("@title");  
        tripLoader.setFolderTag("trip");  
        tripLoader.setQtipMapping("@qtip");  
        tripLoader.setIconMapping("@icon");  
        tripLoader.setAttributeMappings(new String[]{"@trip"});  

        final AsyncTreeNode tripRoot = new AsyncTreeNode("Places to Visit", tripLoader);  

        tripTreePanel.setRootNode(tripRoot);  

        //add trip tree listener that handles move / copy logic  
        tripTreePanel.addListener(new TreePanelListenerAdapter() {  
            public void onRender(Component component) {  
                tripRoot.expand();  
                tripTreePanel.expandAll();  
            }  

            public boolean doBeforeNodeDrop(TreePanel treePanel, TreeNode target, DragData dragData,  
                                            String point, DragDrop source, TreeNode dropNode,  
                                            DropNodeCallback dropDropNodeCallback) {  
                if ("true".equals(target.getAttribute("trip"))) {  
                    if (copyRadio.getValue()) {  
                        TreeNode copyNode = dropNode.cloneNode();  
                        Node[] children = copyNode.getChildNodes();  
                        for (int i = 0; i < children.length; i++) {  
                            Node child = children[i];  
                            copyNode.removeChild(child);  
                        }  
                        dropDropNodeCallback.setDropNode(copyNode);  
                    }  
                }  
                return true;  
            }  
        });  

        Panel horizontalPanel = new Panel();  
        horizontalPanel.setLayout(new HorizontalLayout(20));  
        horizontalPanel.add(treePanel);  
        horizontalPanel.add(tripTreePanel);  

        Panel verticalPanel = new Panel();  
        verticalPanel.setLayout(new VerticalLayout(15));  

        verticalPanel.add(formPanel);  
        verticalPanel.add(horizontalPanel);  

        panel.add(verticalPanel);  

        RootPanel.get().add(panel);  
    }  
}