Renaming an item in a TreeView [message #986315] |
Mon, 19 November 2012 21:52 |
Ronnie Pottayya Messages: 24 Registered: November 2012 |
Junior Member |
|
|
Hello Guys,
I'm a student and very new in RCP development. I have actually a file explorer to develop and i'm a bit stuck in renaming a file.
I have a TreeView which shows me all the files and directories on my hard drive and i want to be able to rename a file or a directory. For this, i have set up a contextual menu and in it, i have a button named "Renommer". On click, i want to be able to edit the file or the directory directly on this TreeView.
I have followed lots of tutorials on the net but still i'm not able to achieve this.
Can someone please help me?
Below is a piece of code i have developped :
}else if (actionEnCours.compareTo("ActRen") == 0){
//on click on the menu Renommer
System.out.println("Rename");
viewer.getTree().setHeaderVisible(true);
//new RenameFile(viewer);
TreeViewerFocusCellManager focusCellManager = new TreeViewerFocusCellManager(viewer,new FocusCellOwnerDrawHighlighter(viewer));
ColumnViewerEditorActivationStrategy actSupport = new ColumnViewerEditorActivationStrategy(viewer) {
protected boolean isEditorActivationEvent(
ColumnViewerEditorActivationEvent event) {
return event.eventType == ColumnViewerEditorActivationEvent.TRAVERSAL
|| event.eventType == ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION
|| (event.eventType == ColumnViewerEditorActivationEvent.KEY_PRESSED && event.keyCode == SWT.CR)
|| event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC;
}
};
TreeViewerEditor.create(viewer, focusCellManager, actSupport, ColumnViewerEditor.TABBING_HORIZONTAL
| ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR
| ColumnViewerEditor.TABBING_VERTICAL | ColumnViewerEditor.KEYBOARD_ACTIVATION);
final TextCellEditor textCellEditor = new TextCellEditor(viewer.getTree());
TreeViewerColumn column = new TreeViewerColumn(viewer, SWT.NONE);
column.getColumn().setWidth(200);
column.getColumn().setMoveable(true);
column.getColumn().setText("Column 1");
column.setLabelProvider(new ColumnLabelProvider() {
public String getText(Object element) {
return "Column 1 => " + element.toString();
}
});
//adding the EditingSupport
column.setEditingSupport(new EditingSupport(viewer) {
@Override
protected void setValue(Object element, Object value) {
// TODO Auto-generated method stub
//not yet implemented
}
@Override
protected Object getValue(Object element) {
// TODO Auto-generated method stub
return ((TreeObject)element).getName();
}
@Override
protected CellEditor getCellEditor(Object element) {
// TODO Auto-generated method stub
return textCellEditor;
}
@Override
protected boolean canEdit(Object element) {
// TODO Auto-generated method stub
return true;
}
});
}
}
The problem with this piece of code is that, it is creating a column next to my original one.
|
|
|
|
|
|
|
|
Re: Renaming an item in a TreeView [message #986418 is a reply to message #986315] |
Tue, 20 November 2012 11:17 |
Thorsten Schlathölter Messages: 312 Registered: February 2012 Location: Düsseldorf |
Senior Member |
|
|
Just a note:
A TreeViewer by default has only one column. Note that your code above creates a new column in the viewer:
TreeViewerColumn column = new TreeViewerColumn(viewer, SWT.NONE);
column.getColumn().setWidth(200);
column.getColumn().setMoveable(true);
column.getColumn().setText("Column 1");
column.setLabelProvider(new ColumnLabelProvider() {
This is not what you want. Besides whenever you start renaming, you create yet another column.
Ever thought about the alternative to pop up a dialog for the new name? That's much easier. Eclipse does it the same way when renaming a file. Or is "editing in a viewer" the task to accomplish?
Regards,
Thorsten
[Updated on: Tue, 20 November 2012 11:19] Report message to a moderator
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|