2009-09-06 82 views
26

是否有一個eclipse插件,它允許我從界面快速生成一個新類?從eclipse中的界面快速創建類

而不是必須做打字的新一類的對話框

理想讓我選擇一個標準名稱,如默認地將Impl才能產生

+1

「新階級」對話框不夠快的嗎? – skaffman 2009-09-06 18:53:19

+5

以史詩級別懶惰 – laurie 2009-09-06 20:21:16

+2

很明顯,但我不明白它是如何比現在更快......您鍵入類名稱,選擇接口,然後嚮導將生成該接口的默認實現。 ..怎麼可能更快? – skaffman 2009-09-06 20:35:19

回答

4

我還沒有看到這樣做的任何插件,但它似乎是一個合理的捷徑。

以下內容可以構成插件直接從選定界面生成類的基礎。它適用於我的盒子(TM)。

它目前假定該類將接口名稱後綴爲「Impl」,如果該類型已存在,則會失敗(記錄原因)。

一些改進,我能想到的:

  • 允許多個接口
  • 選擇定義一個首選項頁面的執行後綴和包名
  • 打開使用,如果「默認填充值的對話「實現已經存在

該插件爲編輯器,視圖和文本選擇的上下文菜單添加一個命令,禁用該項目ection不解析爲接口。它也可以用ctrl-6激活(你可以很明顯地改變plugin.xml中的鍵綁定以適合你的心情)。

插件代碼如下:

package name.seller.rich.classwizard.actions; 

import java.util.Collections; 

import org.eclipse.core.commands.AbstractHandler; 
import org.eclipse.core.commands.ExecutionEvent; 
import org.eclipse.core.commands.ExecutionException; 
import org.eclipse.core.expressions.EvaluationContext; 
import org.eclipse.core.resources.IFile; 
import org.eclipse.core.resources.IResource; 
import org.eclipse.core.runtime.CoreException; 
import org.eclipse.core.runtime.NullProgressMonitor; 
import org.eclipse.jdt.core.ICompilationUnit; 
import org.eclipse.jdt.core.IJavaElement; 
import org.eclipse.jdt.core.IType; 
import org.eclipse.jdt.core.JavaModelException; 
import org.eclipse.jdt.internal.ui.JavaPlugin; 
import org.eclipse.jdt.internal.ui.actions.SelectionConverter; 
import org.eclipse.jdt.ui.wizards.NewClassWizardPage; 
import org.eclipse.jface.viewers.IStructuredSelection; 
import org.eclipse.swt.widgets.Display; 
import org.eclipse.ui.IWorkbenchPage; 
import org.eclipse.ui.IWorkbenchPart; 
import org.eclipse.ui.IWorkbenchWindow; 
import org.eclipse.ui.PartInitException; 
import org.eclipse.ui.handlers.HandlerUtil; 
import org.eclipse.ui.ide.IDE; 
import org.eclipse.ui.wizards.newresource.BasicNewResourceWizard; 

public class GenerateClassHandler extends AbstractHandler { 

    public GenerateClassHandler() { 
    } 

    public Object execute(ExecutionEvent event) throws ExecutionException { 
     NewClassWizardPage page = new NewClassWizardPage(); 

     EvaluationContext evaluationContext = (EvaluationContext) event 
       .getApplicationContext(); 

     IWorkbenchPart activePart = (IWorkbenchPart) evaluationContext 
       .getVariable("activePart"); 
     try { 
      IStructuredSelection selection = SelectionConverter 
        .getStructuredSelection(activePart); 

      IType type = getFirstType(selection); 

      if (type != null && type.exists() && type.isInterface()) { 
       page.init(selection); 

       String typeName = type.getElementName() + "Impl"; 
       // TODO handle existing type 
       page.setTypeName(typeName, true); 

       // generate constructors and methods, allow modification 
       page.setMethodStubSelection(false, true, true, true); 

       page.setSuperInterfaces(Collections.singletonList(type 
         .getFullyQualifiedName()), true); 
       try { 
        page.createType(new NullProgressMonitor()); 

        IResource resource = page.getModifiedResource(); 
        if (resource != null) { 
         IWorkbenchWindow window = HandlerUtil 
           .getActiveWorkbenchWindowChecked(event); 
         BasicNewResourceWizard 
           .selectAndReveal(resource, window); 
         openResource((IFile) resource, window); 
        } 
       } catch (CoreException e) { 
        // TODO if we get this the type already exists, open a 
        // dialogue to allow the type name to be modified or give 
        // up? 
        logException(e); 
       } 

      } 
     } catch (JavaModelException e) { 
      logException(e); 
     } catch (InterruptedException e) { 
      logException(e); 
     } 
     return null; 
    } 

    protected void openResource(final IFile resource, 
      IWorkbenchWindow window) { 
     final IWorkbenchPage activePage = window.getActivePage(); 
     if (activePage != null) { 
      final Display display = window.getShell().getDisplay(); 
      if (display != null) { 
       display.asyncExec(new Runnable() { 
        public void run() { 
         try { 
          IDE.openEditor(activePage, resource, true); 
         } catch (PartInitException e) { 
          logException(e); 
         } 
        } 
       }); 
      } 
     } 
    } 

    @Override 
    public void setEnabled(Object context) { 
     if (context != null && context instanceof EvaluationContext) { 
      EvaluationContext evaluationContext = (EvaluationContext) context; 

      IWorkbenchPart activePart = (IWorkbenchPart) evaluationContext 
        .getVariable("activePart"); 

      try { 
       IStructuredSelection selection = SelectionConverter 
         .getStructuredSelection(activePart); 

       IType type = getFirstType(selection); 

       if (type != null) { 
        setBaseEnabled(type.isInterface()); 
        return; 
       } 
      } catch (JavaModelException e) { 
       logException(e); 
      } 
     } 

     setBaseEnabled(false); 
    } 

    private IType getFirstType(IStructuredSelection selection) { 
     IJavaElement[] elements = SelectionConverter.getElements(selection); 

     if (elements != null && elements.length > 0) { 
      if (elements[0] != null && elements[0] instanceof IType) { 
       return (IType) elements[0]; 
      } 

      try { 
       if (elements[0] != null 
         && elements[0] instanceof ICompilationUnit) { 
        IType[] types = ((ICompilationUnit) elements[0]) 
          .getAllTypes(); 

        if (types != null && types.length > 0) { 
         return types[0]; 
        } 
       } 
      } catch (JavaModelException e) { 
       logException(e); 
      } 
     } 
     return null; 
    } 

    protected void logException(Exception e) { 
     JavaPlugin.log(e); 
    } 
} 

有助於該命令的plugin.xml的是:

<?xml version="1.0" encoding="UTF-8"?> 
<?eclipse version="3.0"?> 
<plugin> 
    <extension 
    point="org.eclipse.ui.commands"> 
     <command 
     name="Generate Class" 
     categoryId="name.seller.rich.classwizard.category" 
     id="name.seller.rich.classwizard.generateClassCommand"> 
     </command> 
    </extension> 
    <extension 
    point="org.eclipse.ui.handlers"> 
     <handler 
     commandId="name.seller.rich.classwizard.generateClassCommand" 
     class="name.seller.rich.classwizard.actions.GenerateClassHandler"> 
     </handler> 
    </extension> 
    <extension 
    point="org.eclipse.ui.bindings"> 
     <key 
     commandId="name.seller.rich.classwizard.generateClassCommand" 
     contextId="org.eclipse.ui.contexts.window" 
     sequence="M1+6" 
     schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"> 
     </key> 
    </extension> 
    <extension 
    point="org.eclipse.ui.menus"> 
     <menuContribution 
     locationURI="popup:org.eclipse.ui.popup.any?after=additions"> 
    <command 
      commandId="name.seller.rich.classwizard.generateClassCommand" 
      mnemonic="G"> 
    </command> 
     </menuContribution> 
    </extension> 
</plugin> 

和MANIFEST.MF看起來像這樣:

Manifest-Version: 1.0 
Bundle-ManifestVersion: 2 
Bundle-Name: Classwizard 
Bundle-SymbolicName: name.seller.rich.classwizard; singleton:=true 
Bundle-Version: 1.0.0 
Require-Bundle: org.eclipse.ui, 
org.eclipse.core.runtime, 
org.eclipse.jdt.core;bundle-version="3.5.0", 
org.eclipse.core.expressions;bundle-version="3.4.100", 
org.eclipse.jface.text;bundle-version="3.5.0", 
org.eclipse.jdt.ui;bundle-version="3.5.0", 
org.eclipse.ui.ide;bundle-version="3.5.0", 
org.eclipse.ui.editors;bundle-version="3.5.0", 
org.eclipse.core.resources;bundle-version="3.5.0" 
Eclipse-AutoStart: true 
Bundle-RequiredExecutionEnvironment: JavaSE-1.6 
36

沒有帶其他見過比:右鍵單擊接口類型在包瀏覽器中,選擇New-> Class,它將自動實現該接口。你仍然必須自己命名新課程。

+0

不起作用。你錯了。 – user710818 2011-11-02 06:42:25

+30

@ user710818您可能是右鍵單擊Package Explorer中的.java文件,而不是類型(即只展開包含該接口的.java文件,並右鍵單擊接口類型,選擇New-> Class)。至少在Indigo和Helios中這是完美的。 – nos 2011-11-02 08:32:06

+2

@nos謝謝,它也適用於Luna – 2015-04-10 09:00:09

7

實際上,它是儘快問as 2002

重構應提取所有(開關「全部公開」)從一個類的方法 ,創建一個接口和老班 重命名爲類名默認地將Impl

...並輸入爲feature request,「解決」,在ticket 9798,因爲新建 - >類將有選擇‘繼承抽象方法’(因爲至少Eclipse SDK 2.1 2003)供您才能選擇自動實現這些公共抽象方法。

alt text

+0

你是貢獻者嗎?你真的好像對此非常瞭解:) – javamonkey79 2009-09-07 00:17:31

+0

@ javamonkey79:nope,但是一些真正的eclipse貢獻者會回答這個問題:請參閱這個答案(及其評論):http://stackoverflow.com/questions/1363584/eclipse-improve-debugging-display-vairable-values-on-mouseover/1363640#1363640 – VonC 2009-09-07 04:05:39

+2

我想看到的是在「實施」列表中預填充了我選擇的界面的新建類對話框。 – Arkadiy 2011-11-23 20:50:33

3

如果你創建一個類,讓它實現一個接口。

您會收到錯誤,因爲方法未定義。只需按Ctrl-1或右鍵,就可以根據需要創建所有方法,包括TODO,javadoc註釋等等(取決於Eclipse的配置方式)。

0

方法1:右鍵單擊班級名稱,然後選擇「快速修復」,一個然後會出現一個小菜單,您可以在其中選擇:「添加未實現的方法」。

方法2:右鍵單擊類名,進入「源」,然後選擇「覆蓋/實現方法」