如何将子节点添加到RCP 3.x中TreeViewer的选定节点

时间:2017-02-07 16:38:24

标签: java eclipse-rcp jface rcp treeviewer

[Working]我有一个预定义的树形图,我可以动态地向树中添加节点,最后会附加到树上。

如果我选择一个节点并添加一个新节点,那么该新节点应作为子节点附加到所选节点。任何人都可以帮我吗?

我的工作代码片段

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

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

0 个答案:

没有答案