在SWT中编辑树后未反映的更改

时间:2015-09-22 12:34:54

标签: java tree treeview swt eclipse-rcp

我在SWT中有一棵树,我在其中添加了一个编辑监听器 -

    private void addEditListener() {
    final TreeEditor editor = new TreeEditor(tree);
    editor.horizontalAlignment = SWT.LEFT;
    editor.grabHorizontal = true;
    tree.addKeyListener(new KeyAdapter() {
        public void keyPressed(KeyEvent event) {

            // Make sure one and only one item is selected when F2 is
            // pressed
            if (event.keyCode == SWT.F2 && tree.getSelectionCount() == 1) {

                // Create a text field to do the editing
                final Text text = new Text(tree, SWT.NONE);
                text.setText(tree.getSelection()[0].getText());
                text.selectAll();
                text.setFocus();

                // If they hit Enter, set the text into the tree and end the
                // editing
                // session. If they hit Escape, ignore the text and end the
                // editing session
                text.addKeyListener(new KeyAdapter() {
                    public void keyPressed(KeyEvent event) {
                        switch (event.keyCode) {
                        case SWT.CR:
                            final TreeItem item = tree.getSelection()[0];
                            item.setText(text.getText());
                            treeViewer.update(item, null);
                        case SWT.ESC:
                            text.dispose();
                            break;
                        }
                    }
                });

                // Set the text field into the editor
                editor.setEditor(text, tree.getSelection()[0]); 
            }
        }
    });
}

我还为树的节点实现了一个doubleClickListener,点击它将展开(如果节点有子节点)。代码如下 -

    private void addDoubleClickListener(Display d, TreeView treeView) {
    treeViewer.addDoubleClickListener(new IDoubleClickListener() {

        @Override
        public void doubleClick(DoubleClickEvent e) {
            ISelection selection = e.getSelection();
            if (selection instanceof IStructuredSelection) {
                Object item = ((IStructuredSelection) selection)
                        .getFirstElement();
                if (item == null) {
                    return;
                } else {
                    TreeItem treeItem = tree.getSelection()[0];
                    System.out.println();
                    if (treeItem.getItemCount() == 0) {
                        styledText.setText(tree.getSelection()[0].getText());

                    } else {
                        if (treeViewer.getExpandedState(item)) {
                            treeViewer.collapseToLevel(item,
                                    AbstractTreeViewer.ALL_LEVELS);
                        } else {
                            treeViewer.expandToLevel(item, 1);
                            treeView.addIcons(treeItem, d);
                        }
                    }

                }
            }
        }
    });

}

请注意,我在这里为树创建了自己的数据结构,即 TreeStructure 。 TreeStructure类是这样的 -

public class TreeStructure {
public String name;
private List children = new ArrayList();
private TreeStructure parent;
private String filepath;

public String value;

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

public Object getParent() {
    return parent;
}

public TreeStructure addChild(TreeStructure child, String filepath) {
    children.add(child);
    child.parent = this;
    child.filepath = filepath;
    child.name = child.name;
    return this;
}

public List getChildren() {
    return children;
}

public String toString() {
    return name;
}

public void removeChildren(TreeStructure parent) {
    parent.children.removeAll(children);
}

public String getFilepath() {
    return filepath;
}

}

现在,我的问题是 - 假设有一个节点“ABC”。我编辑了它并将其重命名为“DEF”。我已经完成了treeviewer.update(itemEdited,null)。现在,当我使用箭头展开/折叠节点时,没有问题。它显示为“DEF”。 但是,当我通过双击进行扩展/折叠时,它会显示“ABC”的旧数据。 请帮忙!

谢谢!

编辑:树查看器的我的内容提供程序如下所示 -

public class ProjectContentProvider implements ITreeContentProvider {
public Object[] getChildren(Object parentElement) {
    return ((TreeStructure) parentElement).getChildren().toArray();
}

public Object getParent(Object element) {
    return ((TreeStructure) element).getParent();
}

public boolean hasChildren(Object element) {
    return ((TreeStructure) element).getChildren().size() > 0;
}

public Object[] getElements(Object inputElement) {
    return ((TreeStructure) inputElement).getChildren().toArray();
}

public void dispose() {
}

public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
}
}

谢谢!

1 个答案:

答案 0 :(得分:2)

update TreeViewer方法需要您的内容提供商 TreeItem的对象。类似的东西:

IStructuredSelection sel = treeViewer.getStructuredSelection();

Object selected = sel.getFirstElement();

((MyData)selected).setText(text.getText());

treeViewer.update(selected, null);

其中MyData是您的内容提供者为树元素返回的类,并允许设置文本。

注意:如果您不使用Eclipse Mars或更高版本,则第一行代码必须是:

IStructuredSelection sel = (IStructuredSelection)treeViewer.getSelection();