从Eclipse中的界面快速创建类 [英] Quickly create class from an interface in eclipse
问题描述
而不是在新的类对话框中进行输入, / p>
理想情况下,让我选择像Impl这样的标准名称来生成
p>我没有看到任何这样做的插件,但这对我来说似乎是一个合理的捷径。
以下内容可以形成插件生成类的基础直接从选定的界面。它在我的框(TM)上工作。
它目前假定类将采用后缀为Impl的接口名称,如果该类型已经存在,则失败(记录原因)存在
我可以想到的一些增强功能:
- 允许选择多个界面
- 为实现后缀和程序包名称定义首选项页面
- 如果默认实现已存在,则打开一个填充值的对话框
该插件在编辑器,视图和文本选择的上下文菜单中添加一个命令,如果选择不能解析,则禁用该项一个接口它也可以使用 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处理现有类型
page.setTypeName(typeName,true);
//生成构造函数和方法,允许修改
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)资源,窗口);
}
} catch(CoreException e){
// TODO如果我们得到这个类型已经存在,打开一个
//对话框允许类型名称被修改或给
// up?
logException(e);
}
}
} catch(JavaModelException e){
logException(e);
} catch(InterruptedException e){
logException(e);
}
返回null;
}
protected void openResource(最终的IFile资源,
IWorkbenchWindow窗口){
final IWorkbenchPage activePage = window.getActivePage();
if(activePage!= null){
final显示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&& ; EvaluationContext的上下文instanceof){
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&&&∈ {
return(IType)elements [0];
}
try {
if(elements [0]!= null
&& element [0] instanceof ICompilationUnit){
IType [ ] types =((ICompilationUnit)elements [0])
.getAllTypes();
if(types!= null&& types.length> 0){
return types [0];
}
}
} catch(JavaModelException e){
logException(e);
}
}
返回null;
}
protected void logException(Exception e){
JavaPlugin.log(e);
}
}
提交命令的plugin.xml是: / p>
<?xml version =1.0encoding =UTF-8?>
<?eclipse version =3.0?>
< plugin>
< extension
point =org.eclipse.ui.commands>
< command
name =生成类
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 = addedions>
< 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
需求包: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
Is there a plugin for eclipse which allows me to quickly generate a new class from an interface?
Rather than having to do the typing in the new class dialog
Ideally letting me choose a standard name like Impl for it to generate
I've not seen any plugins that do this, but it seems a reasonable shortcut to me.
The following could form the basis for a plugin to generate a class directly from a selected interface. It works on my box(TM).
It currently assumes the class will take the interface name suffixed with "Impl" and fails (logging the reason) if that type already exists.
Some enhancements I can think of:
- allow selection of multiple interfaces
- define a preference page for the implementation suffix and package name
- open a dialogue with the values populated if the "default" implementation already exists
The plugin adds a command to the context menu for editors, views and text selections, disabling the item if the selection doesn't resolve to an interface. It can also be activated with ctrl-6 (you can obviously change the key-bindings in the plugin.xml to suit your mood).
The plugin code is as follows:
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);
}
}
The plugin.xml to contribute the command is:
<?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>
and the manifest.mf looks like this:
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
这篇关于从Eclipse中的界面快速创建类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!