Note that there are some explanatory texts on larger screens.

plurals
  1. PORename a treeViewer Node with SWT
    primarykey
    data
    text
    <p>I have a treeViewer on which I have to implement editing for renaming that should be able to be invoked in two ways:</p> <ol> <li>by the <kbd>F2</kbd> key</li> <li><p>by a single mouse click if a node is selected.</p> <p><strong>More ever as Windows allows folder rename.</strong> For this, I have used <code>ICellModifier</code>, but it has not given the expected result.</p></li> </ol> <p>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 <kbd>F2</kbd> 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 <kbd>F2</kbd> key for renaming and how can the following code be optimized?</p> <pre><code>tree.addListener(SWT.Selection, new Listener() { public void handleEvent(Event event) { final TreeItem item = (TreeItem)event.item; if (item != null &amp;&amp; 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 &amp;&amp; newText.equals(oldText)) { item.setText(newText); composite.dispose(); break; } if (!focusOutOnce &amp;&amp; (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; } }); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload