Sudeep
Sudeep

Reputation: 173

How to add a child node to the selected node of a TreeViewer in RCP 3.x

[Working] I have a pre-defined treeviewer, where i am able to add nodes to the tree dynamically, which gets appended at the end.

If i select a node and add a new node, that new node should be appended as a child to the selected node. Can anyone help me with it?

Snippet of my working code

add.addSelectionListener(new SelectionAdapter() { 
        @Override
        public void widgetSelected(SelectionEvent e) {

            Shell dShell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
            ShowDialog1 dialog = new ShowDialog1(dShell);
            dialog.create();
            if (dialog.open() == Window.OK) {
                first = dialog.getFirstName();
                treeViewer.setInput(getRootNode(first));

            }
        }
    });

getRootNode()

public static ProjectTree mc = new ProjectTree("root");
private static ProjectTree getRootNode(String first) {
    ProjectTree node1 = new ProjectTree(first);
    mc.addChild(node1, "");
    return mc;
}

class ProjectTree

public class ProjectTree {
private String name;
private ArrayList<ProjectTree> children = new ArrayList<ProjectTree>();
private ProjectTree parent;
private String filepath;

public ProjectTree(String n) {
    name = n;
}

public ProjectTree addChild(ProjectTree child, String filepath) {
    children.add(child);
    child.parent = this;
    child.filepath = filepath;
    child.name = child.name;
    System.out.println("Children : " + children);
    return this;
}
}

Upvotes: 0

Views: 242

Answers (0)

Related Questions