在 SWT 中编辑树后未反映的更改
Changes not reflecting after Editing tree in SWT
我在 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。 class 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)。现在,当我 expanding/collapsing 使用箭头的节点时,没有问题。它显示为 "DEF" 。
但是,当我通过双击 expanding/collapsing 时,它显示旧数据 "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) {
}
}
谢谢!
TreeViewer
的 update
方法需要来自内容提供商的对象 而不是 TreeItem
。类似于:
IStructuredSelection sel = treeViewer.getStructuredSelection();
Object selected = sel.getFirstElement();
((MyData)selected).setText(text.getText());
treeViewer.update(selected, null);
其中 MyData
是一个 class,您的内容提供商 returns 用于树元素并允许设置文本。
注意:如果您不使用 Eclipse Mars 或更高版本,第一行代码必须是:
IStructuredSelection sel = (IStructuredSelection)treeViewer.getSelection();
我在 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。 class 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)。现在,当我 expanding/collapsing 使用箭头的节点时,没有问题。它显示为 "DEF" 。 但是,当我通过双击 expanding/collapsing 时,它显示旧数据 "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) {
}
}
谢谢!
TreeViewer
的 update
方法需要来自内容提供商的对象 而不是 TreeItem
。类似于:
IStructuredSelection sel = treeViewer.getStructuredSelection();
Object selected = sel.getFirstElement();
((MyData)selected).setText(text.getText());
treeViewer.update(selected, null);
其中 MyData
是一个 class,您的内容提供商 returns 用于树元素并允许设置文本。
注意:如果您不使用 Eclipse Mars 或更高版本,第一行代码必须是:
IStructuredSelection sel = (IStructuredSelection)treeViewer.getSelection();