2012-03-29 4 views
1

でtreeViewerノードの名前を変更します:F2によっては、私は私の2つの方法で呼び出すことができるようにする必要があり改名するための編集を実装する必要がどのtreeViewerを持ってSWT

  1. キー
  2. でノードが選択されている場合、1回のマウスクリック。

    Windowsではフォルダの名前を変更できます。 ICellModifierを使用しましたが、予期した結果が得られていません。

ノードが選択されている場合、ダブルクリックでエディタを開く際に問題が発生しますが、次のコードでは2点に達しました。主な関心事は、依然として保留中である名前の変更にF2キーを許可することです。私は、以下でkeyListenerで書いたのと同じコードを使用する必要がありますが、うまくいきません。このコードは最適化されたソリューションだとは本当に思いませんが、うまくいきます。 2番目のオプションの場合は、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

実際にjfaceのTreeViewerを使用していますか?コードスニペットは、単純なSWT Treeウィジェットを使用していることを示しています。したがって、コードの多くは定型コードです。 実際に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

関連する問題