首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Eclipse开发 >

施用代码生成插件工程,脱离eclipse本身的新建工程向导(3)

2013-09-11 
使用代码生成插件工程,脱离eclipse本身的新建工程向导(3)在第二步中需要有个PluginClassCodeGenerator类,

使用代码生成插件工程,脱离eclipse本身的新建工程向导(3)
    在第二步中需要有个PluginClassCodeGenerator类,是用来生产插件工程的启动类Activator,在eclipse源码中,也需要依赖向导中保存的一些上下文信息,也需要进行改造,改造后如下。

import java.io.ByteArrayInputStream;import java.io.IOException;import java.io.PrintWriter;import java.io.StringWriter;import java.util.ArrayList;import org.eclipse.core.resources.IFile;import org.eclipse.core.resources.IProject;import org.eclipse.core.runtime.CoreException;import org.eclipse.core.runtime.IPath;import org.eclipse.core.runtime.Path;import org.eclipse.pde.core.plugin.IPluginReference;import org.eclipse.pde.internal.build.IPDEBuildConstants;import org.eclipse.pde.internal.core.util.CoreUtility;import org.eclipse.pde.ui.templates.PluginReference;/** *  * @author aquarion * @version 1.0 * */@SuppressWarnings("restriction")public class PluginClassCodeGenerator {private IProject fProject;private String fQualifiedClassName;private String id;public PluginClassCodeGenerator(IProject project,String qualifiedClassName, String id) {fProject = project;fQualifiedClassName = qualifiedClassName;this.id = id;}public IFile generate() throws CoreException {int nameloc = fQualifiedClassName.lastIndexOf('.');String packageName = (nameloc == -1) ? "" : fQualifiedClassName.substring(0, nameloc); //$NON-NLS-1$String className = fQualifiedClassName.substring(nameloc + 1);IPath path = new Path(packageName.replace('.', '/'));path = new Path("src").append(path);CoreUtility.createFolder(fProject.getFolder(path));IFile file = fProject.getFile(path.append(className + ".java")); //$NON-NLS-1$StringWriter swriter = new StringWriter();PrintWriter writer = new PrintWriter(swriter);// only generate/extend plug-in classes if it's a IU plug-ingeneratePluginClass(packageName, className, writer);writer.flush();try {swriter.close();ByteArrayInputStream stream = new ByteArrayInputStream(swriter.toString().getBytes(fProject.getDefaultCharset()));if (file.exists())file.setContents(stream, false, true, null);elsefile.create(stream, false, null);stream.close();} catch (IOException e) {}return file;}private void generatePluginClass(String packageName, String className,PrintWriter writer) {if (!packageName.equals("")) { //$NON-NLS-1$writer.println("package " + packageName + ";"); //$NON-NLS-1$ //$NON-NLS-2$writer.println();}writer.println("import org.eclipse.ui.plugin.AbstractUIPlugin;"); //$NON-NLS-1$writer.println("import org.osgi.framework.BundleContext;"); //$NON-NLS-1$writer.println();writer.println("/**"); //$NON-NLS-1$writer.println(" * The activator class controls the plug-in life cycle"); //$NON-NLS-1$writer.println(" */"); //$NON-NLS-1$writer.println("public class " + className + " extends AbstractUIPlugin {"); //$NON-NLS-1$ //$NON-NLS-2$writer.println();writer.println("\t// The plug-in ID"); //$NON-NLS-1$writer.println("\tpublic static final String PLUGIN_ID = "" + id + ""; //$NON-NLS-1$"); //$NON-NLS-1$ //$NON-NLS-2$writer.println();writer.println("\t// The shared instance"); //$NON-NLS-1$writer.println("\tprivate static " + className + " plugin;"); //$NON-NLS-1$ //$NON-NLS-2$writer.println("\t"); //$NON-NLS-1$writer.println("\t/**"); //$NON-NLS-1$writer.println("\t * The constructor"); //$NON-NLS-1$writer.println("\t */"); //$NON-NLS-1$writer.println("\tpublic " + className + "() {"); //$NON-NLS-1$ //$NON-NLS-2$writer.println("\t}"); //$NON-NLS-1$writer.println();writer.println("\t/*"); //$NON-NLS-1$writer.println("\t * (non-Javadoc)"); //$NON-NLS-1$writer.println("\t * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)"); //$NON-NLS-1$writer.println("\t */"); //$NON-NLS-1$writer.println("\tpublic void start(BundleContext context) throws Exception {"); //$NON-NLS-1$writer.println("\t\tsuper.start(context);"); //$NON-NLS-1$writer.println("\t\tplugin = this;"); //$NON-NLS-1$writer.println("\t}"); //$NON-NLS-1$writer.println();writer.println("\t/*"); //$NON-NLS-1$writer.println("\t * (non-Javadoc)"); //$NON-NLS-1$writer.println("\t * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)"); //$NON-NLS-1$writer.println("\t */"); //$NON-NLS-1$writer.println("\tpublic void stop(BundleContext context) throws Exception {"); //$NON-NLS-1$writer.println("\t\tplugin = null;"); //$NON-NLS-1$writer.println("\t\tsuper.stop(context);"); //$NON-NLS-1$writer.println("\t}"); //$NON-NLS-1$writer.println();writer.println("\t/**"); //$NON-NLS-1$writer.println("\t * Returns the shared instance"); //$NON-NLS-1$writer.println("\t *"); //$NON-NLS-1$writer.println("\t * @return the shared instance"); //$NON-NLS-1$writer.println("\t */"); //$NON-NLS-1$writer.println("\tpublic static " + className + " getDefault() {"); //$NON-NLS-1$ //$NON-NLS-2$writer.println("\t\treturn plugin;"); //$NON-NLS-1$writer.println("\t}"); //$NON-NLS-1$writer.println();writer.println("}"); //$NON-NLS-1$}@SuppressWarnings({ "rawtypes", "unchecked", "restriction" })public IPluginReference[] getDependencies() {ArrayList result = new ArrayList();result.add(new PluginReference("org.eclipse.ui", null, 0)); //$NON-NLS-1$result.add(new PluginReference(IPDEBuildConstants.BUNDLE_CORE_RUNTIME,null, 0));return (IPluginReference[]) result.toArray(new IPluginReference[result.size()]);}// public String[] getImportPackages() {//return fPluginData.getOSGiFramework() != null ? new String[] { "org.osgi.framework;version="1.3.0"" } //$NON-NLS-1$// : new String[0];// }}


    经过这三步,能生产最基础的插件工程,用处其实不大,需要进一步的定制特别的插件工程内容来满足需求。

热点排行