在SWT中编辑树后更改未反映 [英] Changes not reflecting after Editing tree in SWT

查看:67
本文介绍了在SWT中编辑树后更改未反映的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SWT中有一棵树,在其中添加了一个编辑侦听器,如下所示-

I have a tree in SWT, to which I have added a edit listener as below -

    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,单击后将扩展(如果节点有子节点)。代码如下-

I have also implemented a doubleClickListener for the nodes of the tree which on click will expand(if the node has children). The code is as follows-

    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类是这样的-

Please note, I have created my own data structure for tree here, which is TreeStructure. The class TreeStructure goes like this -

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。
请帮助!

Now, my problem is - Suppose, there is a node "ABC". I have edited it and renamed it as "DEF". I have done treeviewer.update(itemEdited,null). Now, when I am expanding/collapsing the node using arrows, there is no problem. It displays as "DEF" . But, when I am expanding/collapsing by doing double click, it displays the old data which is "ABC". Please help!

谢谢!

编辑:我的树查看器内容提供商看起来像这样-

My content provider for the tree viewer looks like this-

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

谢谢!

推荐答案

TreeViewer update 方法需要内容提供商提供的对象不是 TreeItem 。像这样的东西:

The update method of TreeViewer requires the object from your content provider not the TreeItem. Something like:

IStructuredSelection sel = treeViewer.getStructuredSelection();

Object selected = sel.getFirstElement();

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

treeViewer.update(selected, null);

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

where MyData is a class that your content provider returns for the tree elements and which allows the text to be set.

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

Note: If you are not using Eclipse Mars or later the first line of code will have to be:

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

这篇关于在SWT中编辑树后更改未反映的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆