问题
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]);
}
}
});
}
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);
}
}
}
}
}
});
}
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;
}
}
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!
Thanks!
Edit: 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) {
}
}
Thanks!
回答1:
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);
where MyData
is a class that your content provider returns for the tree elements and which allows the text to be set.
Note: If you are not using Eclipse Mars or later the first line of code will have to be:
IStructuredSelection sel = (IStructuredSelection)treeViewer.getSelection();
来源:https://stackoverflow.com/questions/32717020/changes-not-reflecting-after-editing-tree-in-swt