0

[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;
}
}
Sudeep
  • 153
  • 1
  • 1
  • 12
  • Changes to the structure must be done in the viewer's Content Provider – greg-449 Feb 07 '17 at 17:05
  • I would use a combination of EditingSupport and ICellEditorListener. It would place a cell editor in the selected node, user types anything and then you can process input anyway you want – bichito Feb 07 '17 at 17:05
  • The treeviewer is the classic MVC design pattern's View. Add the child to the Model. – nitind Feb 07 '17 at 17:29

0 Answers0