001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.wizards.plugin;
011:
012: import java.io.ByteArrayInputStream;
013: import java.io.IOException;
014: import java.io.PrintWriter;
015: import java.io.StringWriter;
016: import java.util.ArrayList;
017:
018: import org.eclipse.core.resources.IFile;
019: import org.eclipse.core.resources.IProject;
020: import org.eclipse.core.runtime.CoreException;
021: import org.eclipse.core.runtime.IPath;
022: import org.eclipse.core.runtime.IProgressMonitor;
023: import org.eclipse.core.runtime.Path;
024: import org.eclipse.pde.core.plugin.IPluginReference;
025: import org.eclipse.pde.internal.core.util.CoreUtility;
026: import org.eclipse.pde.internal.ui.wizards.templates.PluginReference;
027:
028: public class PluginClassCodeGenerator {
029: private PluginFieldData fPluginData;
030: private IProject fProject;
031: private String fQualifiedClassName;
032: private boolean fGenerateTemplate;
033:
034: public PluginClassCodeGenerator(IProject project,
035: String qualifiedClassName, PluginFieldData data,
036: boolean generateTemplate) {
037: fProject = project;
038: fQualifiedClassName = qualifiedClassName;
039: fPluginData = data;
040: fGenerateTemplate = generateTemplate;
041: }
042:
043: public IFile generate(IProgressMonitor monitor)
044: throws CoreException {
045: int nameloc = fQualifiedClassName.lastIndexOf('.');
046: String packageName = (nameloc == -1) ? "" : fQualifiedClassName.substring(0, nameloc); //$NON-NLS-1$
047: String className = fQualifiedClassName.substring(nameloc + 1);
048:
049: IPath path = new Path(packageName.replace('.', '/'));
050: if (fPluginData.getSourceFolderName().trim().length() > 0)
051: path = new Path(fPluginData.getSourceFolderName())
052: .append(path);
053:
054: CoreUtility.createFolder(fProject.getFolder(path));
055:
056: IFile file = fProject.getFile(path.append(className + ".java")); //$NON-NLS-1$
057: StringWriter swriter = new StringWriter();
058: PrintWriter writer = new PrintWriter(swriter);
059: if (fPluginData.getOSGiFramework() != null) {
060: generateActivatorClass(packageName, className, writer);
061: } else {
062: generatePluginClass(packageName, className, writer);
063: }
064: writer.flush();
065: try {
066: swriter.close();
067: ByteArrayInputStream stream = new ByteArrayInputStream(
068: swriter.toString().getBytes(
069: fProject.getDefaultCharset()));
070: if (file.exists())
071: file.setContents(stream, false, true, monitor);
072: else
073: file.create(stream, false, monitor);
074: stream.close();
075: } catch (IOException e) {
076:
077: }
078: return file;
079: }
080:
081: private void generatePluginClass(String packageName,
082: String className, PrintWriter writer) {
083: if (!packageName.equals("")) { //$NON-NLS-1$
084: writer.println("package " + packageName + ";"); //$NON-NLS-1$ //$NON-NLS-2$
085: writer.println();
086: }
087: if (fPluginData.isUIPlugin()) {
088: if (fGenerateTemplate)
089: writer
090: .println("import org.eclipse.jface.resource.ImageDescriptor;"); //$NON-NLS-1$
091: writer
092: .println("import org.eclipse.ui.plugin.AbstractUIPlugin;"); //$NON-NLS-1$
093: } else {
094: writer.println("import org.eclipse.core.runtime.Plugin;"); //$NON-NLS-1$
095: }
096: writer.println("import org.osgi.framework.BundleContext;"); //$NON-NLS-1$
097: writer.println();
098: writer.println("/**"); //$NON-NLS-1$
099: writer
100: .println(" * The activator class controls the plug-in life cycle"); //$NON-NLS-1$
101: writer.println(" */"); //$NON-NLS-1$
102: if (fPluginData.isUIPlugin())
103: writer
104: .println("public class " + className + " extends AbstractUIPlugin {"); //$NON-NLS-1$ //$NON-NLS-2$
105: else
106: writer
107: .println("public class " + className + " extends Plugin {"); //$NON-NLS-1$ //$NON-NLS-2$
108: writer.println();
109: writer.println("\t// The plug-in ID"); //$NON-NLS-1$
110: writer
111: .println("\tpublic static final String PLUGIN_ID = \"" + fPluginData.getId() + "\";"); //$NON-NLS-1$ //$NON-NLS-2$
112: writer.println();
113: writer.println("\t// The shared instance"); //$NON-NLS-1$
114: writer.println("\tprivate static " + className + " plugin;"); //$NON-NLS-1$ //$NON-NLS-2$
115: writer.println("\t"); //$NON-NLS-1$
116: writer.println("\t/**"); //$NON-NLS-1$
117: writer.println("\t * The constructor"); //$NON-NLS-1$
118: writer.println("\t */"); //$NON-NLS-1$
119: writer.println("\tpublic " + className + "() {"); //$NON-NLS-1$ //$NON-NLS-2$
120: writer.println("\t}"); //$NON-NLS-1$
121: writer.println();
122:
123: writer.println("\t/*"); //$NON-NLS-1$
124: writer.println("\t * (non-Javadoc)"); //$NON-NLS-1$
125: if (fPluginData.isUIPlugin())
126: writer
127: .println("\t * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)"); //$NON-NLS-1$
128: else
129: writer
130: .println("\t * @see org.eclipse.core.runtime.Plugins#start(org.osgi.framework.BundleContext)"); //$NON-NLS-1$
131: writer.println("\t */"); //$NON-NLS-1$
132: writer
133: .println("\tpublic void start(BundleContext context) throws Exception {"); //$NON-NLS-1$
134: writer.println("\t\tsuper.start(context);"); //$NON-NLS-1$
135: writer.println("\t\tplugin = this;"); //$NON-NLS-1$
136: writer.println("\t}"); //$NON-NLS-1$
137: writer.println();
138:
139: writer.println("\t/*"); //$NON-NLS-1$
140: writer.println("\t * (non-Javadoc)"); //$NON-NLS-1$
141: if (fPluginData.isUIPlugin())
142: writer
143: .println("\t * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)"); //$NON-NLS-1$
144: else
145: writer
146: .println("\t * @see org.eclipse.core.runtime.Plugin#stop(org.osgi.framework.BundleContext)"); //$NON-NLS-1$
147: writer.println("\t */"); //$NON-NLS-1$
148: writer
149: .println("\tpublic void stop(BundleContext context) throws Exception {"); //$NON-NLS-1$
150: writer.println("\t\tplugin = null;"); //$NON-NLS-1$
151: writer.println("\t\tsuper.stop(context);"); //$NON-NLS-1$
152: writer.println("\t}"); //$NON-NLS-1$
153: writer.println();
154:
155: writer.println("\t/**"); //$NON-NLS-1$
156: writer.println("\t * Returns the shared instance"); //$NON-NLS-1$
157: writer.println("\t *"); //$NON-NLS-1$
158: writer.println("\t * @return the shared instance"); //$NON-NLS-1$
159: writer.println("\t */"); //$NON-NLS-1$
160: writer
161: .println("\tpublic static " + className + " getDefault() {"); //$NON-NLS-1$ //$NON-NLS-2$
162: writer.println("\t\treturn plugin;"); //$NON-NLS-1$
163: writer.println("\t}"); //$NON-NLS-1$
164: writer.println();
165: if (fPluginData.isUIPlugin() && fGenerateTemplate) {
166: writer.println("\t/**"); //$NON-NLS-1$
167: writer
168: .println("\t * Returns an image descriptor for the image file at the given"); //$NON-NLS-1$
169: writer.println("\t * plug-in relative path"); //$NON-NLS-1$
170: writer.println("\t *"); //$NON-NLS-1$
171: writer.println("\t * @param path the path"); //$NON-NLS-1$
172: writer.println("\t * @return the image descriptor"); //$NON-NLS-1$
173: writer.println("\t */"); //$NON-NLS-1$
174: writer
175: .println("\tpublic static ImageDescriptor getImageDescriptor(String path) {"); //$NON-NLS-1$
176: writer
177: .println("\t\treturn imageDescriptorFromPlugin(PLUGIN_ID, path);"); //$NON-NLS-1$ //$NON-NLS-2$
178: writer.println("\t}"); //$NON-NLS-1$
179: }
180: writer.println("}"); //$NON-NLS-1$
181: }
182:
183: private void generateActivatorClass(String packageName,
184: String className, PrintWriter writer) {
185: if (!packageName.equals("")) { //$NON-NLS-1$
186: writer.println("package " + packageName + ";"); //$NON-NLS-1$ //$NON-NLS-2$
187: writer.println();
188: }
189: writer.println("import org.osgi.framework.BundleActivator;"); //$NON-NLS-1$
190: writer.println("import org.osgi.framework.BundleContext;"); //$NON-NLS-1$
191: writer.println();
192: writer
193: .println("public class " + className + " implements BundleActivator {"); //$NON-NLS-1$ //$NON-NLS-2$
194: writer.println();
195: writer.println("\t/*"); //$NON-NLS-1$
196: writer.println("\t * (non-Javadoc)"); //$NON-NLS-1$
197: writer
198: .println("\t * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)"); //$NON-NLS-1$
199: writer.println("\t */"); //$NON-NLS-1$
200: writer
201: .println("\tpublic void start(BundleContext context) throws Exception {"); //$NON-NLS-1$
202: writer.println("\t}"); //$NON-NLS-1$
203: writer.println();
204: writer.println("\t/*"); //$NON-NLS-1$
205: writer.println("\t * (non-Javadoc)"); //$NON-NLS-1$
206: writer
207: .println("\t * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)"); //$NON-NLS-1$
208: writer.println("\t */"); //$NON-NLS-1$
209: writer
210: .println("\tpublic void stop(BundleContext context) throws Exception {"); //$NON-NLS-1$
211: writer.println("\t}"); //$NON-NLS-1$
212: writer.println();
213: writer.println("}"); //$NON-NLS-1$
214: }
215:
216: public IPluginReference[] getDependencies() {
217: ArrayList result = new ArrayList();
218: if (fPluginData.isUIPlugin())
219: result.add(new PluginReference("org.eclipse.ui", null, 0)); //$NON-NLS-1$
220: if (!fPluginData.isLegacy()
221: && fPluginData.getOSGiFramework() == null)
222: result.add(new PluginReference(
223: "org.eclipse.core.runtime", null, 0)); //$NON-NLS-1$
224: return (IPluginReference[]) result
225: .toArray(new IPluginReference[result.size()]);
226: }
227:
228: public String[] getImportPackages() {
229: return fPluginData.getOSGiFramework() != null ? new String[] { "org.osgi.framework;version=\"1.3.0\"" } //$NON-NLS-1$
230: : new String[0];
231: }
232:
233: }
|