I have a treeViewer on which I have to implement editing for renaming that should be able to be invoked in two ways:
- by the F2 key
by a single mouse click if a node is selected.
More ever as Windows allows folder rename. For this, I have used
ICellModifier
, but it has not given the expected result.
By the following code, I have achieved point number 2 though it is creating a problem for opening the editor on a double click if a node is selected. The main concern is to allow the F2 key for renaming which is still pending. I have to use the same code that I have written in the following in a keyListener, but it does't work... I really don't think this following code is an optimized solution, but it works. For the second option, is there a solution to allow the F2 key for renaming and how can the following code be optimized?
tree.addListener(SWT.Selection, new Listener()
{
public void handleEvent(Event event)
{
final TreeItem item = (TreeItem)event.item;
if (item != null && item == lastItem[0])
{
boolean showBorder = true;
//it will not allow to rename root
if (item.getParentItem() == null)
return;
final Composite composite = new Composite(tree, SWT.NONE);
if (showBorder)
composite.setBackground(black);
final Text text = new Text(composite, SWT.NONE);
final int inset = showBorder ? 1 : 0;
composite.addListener(SWT.Resize, new Listener()
{
public void handleEvent(Event e)
{
Rectangle rect = composite.getClientArea();
text.setBounds(rect.x + inset, rect.y + inset,
rect.width - inset * 2, rect.height - inset * 2);
}
});
textListener = new Listener()
{
boolean focusOutOnce = false;
public void handleEvent(final Event e)
{
String newText ;
Model data = (Model)item.getData();
boolean caseType = false;
//if nodeType is case
if(data.getNodeType() == Model.TestType.CASE)
caseType = true;
String oldText = item.getText();
switch (e.type)
{
case SWT.FocusOut:
//validate the renamed text and update
//model to get dump in to file.
newText = text.getText();
if(Utils.length(newText) != 0)
newText = newText.trim();
if(!focusOutOnce && newText.equals(oldText))
{
item.setText(newText);
composite.dispose();
break;
}
if (!focusOutOnce &&
(Model.containsAction(newText) || Model.containsCase(newText) ) )
{
composite.dispose();
break;
}
if (!focusOutOnce )
{
//action edit name
}
else if(!focusOutOnce)
{
}
composite.dispose();
break;
case SWT.Verify:
newText = text.getText();
String leftText = newText.substring(0, e.start);
String rightText =
newText.substring(e.end,
newText.length());
GC gc = new GC(text);
Point size =
gc.textExtent(leftText + e.text
+ rightText);
gc.dispose();
size = text.computeSize(size.x, SWT.DEFAULT);
editor.horizontalAlignment = SWT.LEFT;
Rectangle itemRect = item.getBounds(),
rect = tree.getClientArea();
editor.minimumWidth =
Math.max(size.x, itemRect.width)
+ inset * 2;
int left = itemRect.x,
right = rect.x + rect.width;
editor.minimumWidth =
Math.min(editor.minimumWidth, right
- left);
editor.minimumHeight = size.y + inset * 2;
editor.layout();
break;
case SWT.Traverse:
switch (e.detail)
{
case SWT.TRAVERSE_RETURN:
composite.dispose();
break;
case SWT.TRAVERSE_ESCAPE:
composite.dispose();
e.doit = false;
}
break;
}
}
};
text.addListener(SWT.Verify, textListener);
text.addListener(SWT.Traverse, textListener);
text.addListener(SWT.FocusOut, textListener);
editor.setEditor(composite, item);
text.setText(item.getText());
text.selectAll();
text.setFocus();
}
lastItem[0] = item;
}
});