1 11 package org.eclipse.pde.internal.ui.wizards.plugin; 12 import java.io.ByteArrayInputStream ; 13 import java.io.IOException ; 14 import java.io.PrintWriter ; 15 import java.io.StringWriter ; 16 import java.util.ArrayList ; 17 18 import org.eclipse.core.resources.IFile; 19 import org.eclipse.core.resources.IProject; 20 import org.eclipse.core.runtime.CoreException; 21 import org.eclipse.core.runtime.IPath; 22 import org.eclipse.core.runtime.IProgressMonitor; 23 import org.eclipse.core.runtime.Path; 24 import org.eclipse.pde.core.plugin.IPluginReference; 25 import org.eclipse.pde.internal.core.util.CoreUtility; 26 import org.eclipse.pde.internal.ui.wizards.templates.PluginReference; 27 28 public class PluginClassCodeGenerator { 29 private PluginFieldData fPluginData; 30 private IProject fProject; 31 private String fQualifiedClassName; 32 private boolean fGenerateTemplate; 33 34 public PluginClassCodeGenerator(IProject project, String qualifiedClassName, 35 PluginFieldData data, boolean generateTemplate) { 36 fProject = project; 37 fQualifiedClassName = qualifiedClassName; 38 fPluginData = data; 39 fGenerateTemplate = generateTemplate; 40 } 41 42 public IFile generate(IProgressMonitor monitor) throws CoreException { 43 int nameloc = fQualifiedClassName.lastIndexOf('.'); 44 String packageName = (nameloc == -1) 45 ? "" : fQualifiedClassName.substring(0, nameloc); String className = fQualifiedClassName.substring(nameloc + 1); 47 48 IPath path = new Path(packageName.replace('.', '/')); 49 if (fPluginData.getSourceFolderName().trim().length() > 0) 50 path = new Path(fPluginData.getSourceFolderName()).append(path); 51 52 CoreUtility.createFolder(fProject.getFolder(path)); 53 54 IFile file = fProject.getFile(path.append(className + ".java")); StringWriter swriter = new StringWriter (); 56 PrintWriter writer = new PrintWriter (swriter); 57 if (fPluginData.getOSGiFramework() != null){ 58 generateActivatorClass(packageName, className, writer); 59 } else { 60 generatePluginClass(packageName, className, writer); 61 } 62 writer.flush(); 63 try { 64 swriter.close(); 65 ByteArrayInputStream stream = new ByteArrayInputStream (swriter.toString() 66 .getBytes(fProject.getDefaultCharset())); 67 if (file.exists()) 68 file.setContents(stream, false, true, monitor); 69 else 70 file.create(stream, false, monitor); 71 stream.close(); 72 } catch (IOException e) { 73 74 } 75 return file; 76 } 77 78 private void generatePluginClass(String packageName, String className, 79 PrintWriter writer) { 80 if (!packageName.equals("")) { writer.println("package " + packageName + ";"); writer.println(); 83 } 84 if (fPluginData.isUIPlugin()) { 85 if (fGenerateTemplate) 86 writer.println("import org.eclipse.jface.resource.ImageDescriptor;"); writer.println("import org.eclipse.ui.plugin.AbstractUIPlugin;"); } else { 89 writer.println("import org.eclipse.core.runtime.Plugin;"); } 91 writer.println("import org.osgi.framework.BundleContext;"); writer.println(); 93 writer.println("/**"); writer.println(" * The activator class controls the plug-in life cycle"); writer.println(" */"); if (fPluginData.isUIPlugin()) 97 writer.println("public class " + className + " extends AbstractUIPlugin {"); else 99 writer.println("public class " + className + " extends Plugin {"); writer.println(); 101 writer.println("\t// The plug-in ID"); writer.println("\tpublic static final String PLUGIN_ID = \"" + fPluginData.getId() + "\";"); writer.println(); 104 writer.println("\t// The shared instance"); writer.println("\tprivate static " + className + " plugin;"); writer.println("\t"); writer.println("\t/**"); writer.println("\t * The constructor"); writer.println("\t */"); writer.println("\tpublic " + className + "() {"); writer.println("\t}"); writer.println(); 113 114 writer.println("\t/*"); writer.println("\t * (non-Javadoc)"); if (fPluginData.isUIPlugin()) 117 writer.println("\t * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)"); else 119 writer.println("\t * @see org.eclipse.core.runtime.Plugins#start(org.osgi.framework.BundleContext)"); writer.println("\t */"); writer.println("\tpublic void start(BundleContext context) throws Exception {"); writer.println("\t\tsuper.start(context);"); writer.println("\t\tplugin = this;"); writer.println("\t}"); writer.println(); 126 127 writer.println("\t/*"); writer.println("\t * (non-Javadoc)"); if (fPluginData.isUIPlugin()) 130 writer.println("\t * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)"); else 132 writer.println("\t * @see org.eclipse.core.runtime.Plugin#stop(org.osgi.framework.BundleContext)"); writer.println("\t */"); writer.println("\tpublic void stop(BundleContext context) throws Exception {"); writer.println("\t\tplugin = null;"); writer.println("\t\tsuper.stop(context);"); writer.println("\t}"); writer.println(); 139 140 writer.println("\t/**"); writer.println("\t * Returns the shared instance"); writer.println("\t *"); writer.println("\t * @return the shared instance"); writer.println("\t */"); writer.println("\tpublic static " + className + " getDefault() {"); writer.println("\t\treturn plugin;"); writer.println("\t}"); writer.println(); 149 if (fPluginData.isUIPlugin() && fGenerateTemplate) { 150 writer.println("\t/**"); writer.println("\t * Returns an image descriptor for the image file at the given"); writer.println("\t * plug-in relative path"); writer.println("\t *"); writer.println("\t * @param path the path"); writer.println("\t * @return the image descriptor"); writer.println("\t */"); writer.println("\tpublic static ImageDescriptor getImageDescriptor(String path) {"); writer.println("\t\treturn imageDescriptorFromPlugin(PLUGIN_ID, path);"); writer.println("\t}"); } 161 writer.println("}"); } 163 164 private void generateActivatorClass(String packageName, String className, 165 PrintWriter writer) { 166 if (!packageName.equals("")) { writer.println("package " + packageName + ";"); writer.println(); 169 } 170 writer.println("import org.osgi.framework.BundleActivator;"); writer.println("import org.osgi.framework.BundleContext;"); writer.println(); 173 writer.println("public class " + className + " implements BundleActivator {"); writer.println(); 175 writer.println("\t/*"); writer.println("\t * (non-Javadoc)"); writer.println("\t * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)"); writer.println("\t */"); writer.println("\tpublic void start(BundleContext context) throws Exception {"); writer.println("\t}"); writer.println(); 182 writer.println("\t/*"); writer.println("\t * (non-Javadoc)"); writer.println("\t * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)"); writer.println("\t */"); writer.println("\tpublic void stop(BundleContext context) throws Exception {"); writer.println("\t}"); writer.println(); 189 writer.println("}"); } 191 192 public IPluginReference[] getDependencies() { 193 ArrayList result = new ArrayList (); 194 if (fPluginData.isUIPlugin()) 195 result.add(new PluginReference("org.eclipse.ui", null, 0)); if (!fPluginData.isLegacy() && fPluginData.getOSGiFramework() == null) 197 result.add(new PluginReference("org.eclipse.core.runtime", null, 0)); return (IPluginReference[]) result.toArray(new IPluginReference[result.size()]); 199 } 200 201 public String [] getImportPackages() { 202 return fPluginData.getOSGiFramework() != null 203 ? new String [] {"org.osgi.framework;version=\"1.3.0\""} : new String [0]; 205 } 206 207 } 208 | Popular Tags |