Rename a treeViewer Node with SWT

假如想象 提交于 2019-12-22 00:27:12

问题


I have a treeViewer on which I have to implement editing for renaming that should be able to be invoked in two ways:

  1. by the F2 key
  2. 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;
        }
    });

回答1:


Are you really using jface's TreeViewer? Your code snippet does show that you are using the simple SWT Tree widget. Hence, much of your code is boilerplate code. If you are really using TreeViewer, then have a look at jface's EditingSupport which is very convenient and flexible allowing you to achieve what you are trying to solve manually.

To make it short, have a look at this snippet:

http://git.eclipse.org/c/platform/eclipse.platform.ui.git/tree/examples/org.eclipse.jface.snippets/Eclipse%20JFace%20Snippets/org/eclipse/jface/snippets/viewers/Snippet026TreeViewerTabEditing.java



来源:https://stackoverflow.com/questions/9921560/rename-a-treeviewer-node-with-swt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!