2009-09-06 25 views
26

Có một plugin cho nhật thực cho phép tôi nhanh chóng tạo một lớp mới từ giao diện không?Tạo nhanh lớp từ giao diện trong nhật thực

Thay vì phải thực hiện gõ vào lớp mới thoại

Lý tưởng nhất cho tôi chọn một tên tiêu chuẩn như Impl cho nó để tạo ra

+1

Các "lớp mới" hộp thoại là không đủ nhanh cho bạn? – skaffman

+5

lười biếng trên quy mô sử thi – laurie

+2

Rõ ràng, nhưng tôi không thấy nó có thể nhanh như thế nào nữa ... bạn nhập tên lớp, chọn giao diện và trình hướng dẫn sẽ tạo ra triển khai mặc định của giao diện đó. .. làm thế nào mà có thể nhanh hơn? – skaffman

Trả lời

4

Tôi chưa thấy bất kỳ plugin nào thực hiện việc này nhưng có vẻ như là một lối tắt hợp lý đối với tôi.

Sau đây có thể tạo cơ sở cho plugin để tạo lớp trực tiếp từ giao diện đã chọn. Nó hoạt động trên hộp của tôi (TM).

Hiện tại giả định rằng lớp sẽ lấy tên giao diện được gắn với "Impl" và không thành công (ghi lại lý do) nếu loại đó đã tồn tại.

Một số cải tiến tôi có thể nghĩ:

  • cho phép lựa chọn nhiều giao diện
  • xác định một trang ưu tiên cho việc thực hiện hậu tố và gói tên
  • mở một cuộc đối thoại với các giá trị dân nếu "mặc định "triển khai đã tồn tại

Plugin thêm lệnh vào menu ngữ cảnh cho trình chỉnh sửa, chế độ xem và lựa chọn văn bản, tắt mục nếu chọn ection không giải quyết một giao diện. Nó cũng có thể được kích hoạt với ctrl-6 (bạn rõ ràng có thể thay đổi các ràng buộc khóa trong plugin.xml để phù hợp với tâm trạng của bạn).

Mã plugin là như sau:

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); 
    } 
} 

Các plugin.xml đóng góp lệnh là:

<?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> 

và manifest.mf trông như thế này:

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

alex: alex nhìn thấy bất cứ điều gì khác hơn là: kích chuột phải vào loại giao diện trong trình khám phá gói, chọn Mới-> Class và nó sẽ tự động triển khai giao diện đó. Bạn vẫn phải tự đặt tên cho lớp mới.

+0

Không hoạt động. Bạn sai rồi. – user710818

+30

@ user710818 Bạn có thể nhấp chuột phải vào tệp .java trong Package Explorer, không phải loại (tức là chỉ mở rộng tệp .java chứa giao diện và nhấp chuột phải vào loại giao diện, chọn Mới-> Lớp). Điều này hoạt động hoàn hảo ở Indigo và Helios ít nhất. – nos

+2

@nos Cảm ơn, nó hoạt động trên Luna cũng –

7

Nó đã thực sự hỏi càng sớm as 2002

Các refactoring nên trích xuất tất cả (công tắc cho "tất cả cộng đồng") phương pháp từ một lớp học, tạo ra một giao diện và đổi tên lớp cũ để classname Impl .

... và nhập như một feature request, "giải quyết" trong ticket 9798, vì New-> Class sẽ có tùy chọn 'phương pháp trừu tượng thừa kế' (ít nhất là từ Eclipse SDK 2.1 2003) để bạn lựa chọn để tự động thực hiện các phương thức trừu tượng công khai đó.

alt text

+0

Bạn có phải là người đóng góp không? Bạn thực sự dường như biết khá nhiều về nó :) – javamonkey79

+0

@ javamonkey79: không, nhưng một số người đóng góp thực tế thực sự trả lời câu hỏi SO: xem ví dụ câu trả lời này (và nhận xét của nó): http://stackoverflow.com/questions/1363584/eclipse-improve-debugging-and-display-vairable-values-on-mouseover/1363640 # 1363640 – VonC

+2

Điều tôi muốn thấy là hộp thoại Lớp mới được điền sẵn với giao diện đã chọn của tôi trong danh sách "triển khai". – Arkadiy

3

Nếu bạn tạo một lớp, hãy để nó triển khai giao diện.

Bạn gặp lỗi, bởi vì các phương pháp không được xác định. Chỉ cần Ctrl-1, hoặc phải clic, và bạn có thể tạo tất cả các phương thức, với các TODO, các chú thích javadoc và vv khi cần (tùy thuộc vào cách cấu hình Eclipse của bạn).

0

Phương pháp 1: Nhấp chuột phải vào tên lớp, sau đó chọn "Sửa nhanh", một d sau đó một menu nhỏ sẽ xuất hiện, trong đó bạn chọn: "Thêm các phương thức chưa được thực hiện".

Phương pháp 2: Nhấp chuột phải vào tên lớp, đi đến "Source", sau đó chọn "Override/Thực hiện phương pháp"

Các vấn đề liên quan