How to add the root node of TreePanel in another TreePanel via drag drop?

921 views Asked by At

I want to add the root node of one tree into another tree using drag drop. The example i am using for this task is at the following URL :

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

Please help me asap.

1

There are 1 answers

3
PVR On

In the same link. the code is given.

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