使用SWT重命名treeViewer节点

时间:2012-03-29 08:17:43

标签: java eclipse eclipse-plugin swt treeviewer

我有一个treeViewer,我必须在其上实现重命名编辑,应该能够以两种方式调用:

  1. 通过 F2
  2. 如果选择了某个节点,只需单击鼠标即可。

    Windows允许文件夹重命名。 为此,我使用了ICellModifier,但没有给出预期的结果。

  3. 通过以下代码,我已经实现了第2点,但是如果选择了一个节点,则在双击时打开编辑器会产生问题。主要关注的是允许 F2 键进行重命名,这仍然是未决的。我必须在keyListener中使用我在下面编写的相同代码,但它不起作用...我真的不认为以下代码是一个优化的解决方案,但它的工作原理。对于第二个选项,是否有允许 F2 键进行重命名的解决方案以及如何优化以下代码?

    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 个答案:

答案 0 :(得分:2)

你真的使用jface的TreeViewer吗?您的代码段确实显示您正在使用简单的SWT树小部件。因此,您的大部分代码都是样板代码。 如果您真的使用的是TreeViewer,那么请看一下jface的EditingSupport,它非常方便灵活,可以让您实现手动解决的问题。

为了简短起见,请看一下这个片段:

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