KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > wizards > plugin > PluginClassCodeGenerator


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.wizards.plugin;
12 import java.io.ByteArrayInputStream JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.io.PrintWriter JavaDoc;
15 import java.io.StringWriter JavaDoc;
16 import java.util.ArrayList JavaDoc;
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 JavaDoc fQualifiedClassName;
32     private boolean fGenerateTemplate;
33
34     public PluginClassCodeGenerator(IProject project, String JavaDoc 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 JavaDoc packageName = (nameloc == -1)
45                 ? "" : fQualifiedClassName.substring(0, nameloc); //$NON-NLS-1$
46
String JavaDoc 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")); //$NON-NLS-1$
55
StringWriter JavaDoc swriter = new StringWriter JavaDoc();
56         PrintWriter JavaDoc writer = new PrintWriter JavaDoc(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 JavaDoc stream = new ByteArrayInputStream JavaDoc(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 JavaDoc e) {
73
74         }
75         return file;
76     }
77
78     private void generatePluginClass(String JavaDoc packageName, String JavaDoc className,
79             PrintWriter JavaDoc writer) {
80         if (!packageName.equals("")) { //$NON-NLS-1$
81
writer.println("package " + packageName + ";"); //$NON-NLS-1$ //$NON-NLS-2$
82
writer.println();
83         }
84         if (fPluginData.isUIPlugin()) {
85             if (fGenerateTemplate)
86                 writer.println("import org.eclipse.jface.resource.ImageDescriptor;"); //$NON-NLS-1$
87
writer.println("import org.eclipse.ui.plugin.AbstractUIPlugin;"); //$NON-NLS-1$
88
} else {
89             writer.println("import org.eclipse.core.runtime.Plugin;"); //$NON-NLS-1$
90
}
91         writer.println("import org.osgi.framework.BundleContext;"); //$NON-NLS-1$
92
writer.println();
93         writer.println("/**"); //$NON-NLS-1$
94
writer.println(" * The activator class controls the plug-in life cycle"); //$NON-NLS-1$
95
writer.println(" */"); //$NON-NLS-1$
96
if (fPluginData.isUIPlugin())
97             writer.println("public class " + className + " extends AbstractUIPlugin {"); //$NON-NLS-1$ //$NON-NLS-2$
98
else
99             writer.println("public class " + className + " extends Plugin {"); //$NON-NLS-1$ //$NON-NLS-2$
100
writer.println();
101         writer.println("\t// The plug-in ID"); //$NON-NLS-1$
102
writer.println("\tpublic static final String PLUGIN_ID = \"" + fPluginData.getId() + "\";"); //$NON-NLS-1$ //$NON-NLS-2$
103
writer.println();
104         writer.println("\t// The shared instance"); //$NON-NLS-1$
105
writer.println("\tprivate static " + className + " plugin;"); //$NON-NLS-1$ //$NON-NLS-2$
106
writer.println("\t"); //$NON-NLS-1$
107
writer.println("\t/**"); //$NON-NLS-1$
108
writer.println("\t * The constructor"); //$NON-NLS-1$
109
writer.println("\t */"); //$NON-NLS-1$
110
writer.println("\tpublic " + className + "() {"); //$NON-NLS-1$ //$NON-NLS-2$
111
writer.println("\t}"); //$NON-NLS-1$
112
writer.println();
113
114         writer.println("\t/*"); //$NON-NLS-1$
115
writer.println("\t * (non-Javadoc)"); //$NON-NLS-1$
116
if (fPluginData.isUIPlugin())
117             writer.println("\t * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)"); //$NON-NLS-1$
118
else
119             writer.println("\t * @see org.eclipse.core.runtime.Plugins#start(org.osgi.framework.BundleContext)"); //$NON-NLS-1$
120
writer.println("\t */"); //$NON-NLS-1$
121
writer.println("\tpublic void start(BundleContext context) throws Exception {"); //$NON-NLS-1$
122
writer.println("\t\tsuper.start(context);"); //$NON-NLS-1$
123
writer.println("\t\tplugin = this;"); //$NON-NLS-1$
124
writer.println("\t}"); //$NON-NLS-1$
125
writer.println();
126
127         writer.println("\t/*"); //$NON-NLS-1$
128
writer.println("\t * (non-Javadoc)"); //$NON-NLS-1$
129
if (fPluginData.isUIPlugin())
130             writer.println("\t * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)"); //$NON-NLS-1$
131
else
132             writer.println("\t * @see org.eclipse.core.runtime.Plugin#stop(org.osgi.framework.BundleContext)"); //$NON-NLS-1$
133
writer.println("\t */"); //$NON-NLS-1$
134
writer.println("\tpublic void stop(BundleContext context) throws Exception {"); //$NON-NLS-1$
135
writer.println("\t\tplugin = null;"); //$NON-NLS-1$
136
writer.println("\t\tsuper.stop(context);"); //$NON-NLS-1$
137
writer.println("\t}"); //$NON-NLS-1$
138
writer.println();
139
140         writer.println("\t/**"); //$NON-NLS-1$
141
writer.println("\t * Returns the shared instance"); //$NON-NLS-1$
142
writer.println("\t *"); //$NON-NLS-1$
143
writer.println("\t * @return the shared instance"); //$NON-NLS-1$
144
writer.println("\t */"); //$NON-NLS-1$
145
writer.println("\tpublic static " + className + " getDefault() {"); //$NON-NLS-1$ //$NON-NLS-2$
146
writer.println("\t\treturn plugin;"); //$NON-NLS-1$
147
writer.println("\t}"); //$NON-NLS-1$
148
writer.println();
149         if (fPluginData.isUIPlugin() && fGenerateTemplate) {
150             writer.println("\t/**"); //$NON-NLS-1$
151
writer.println("\t * Returns an image descriptor for the image file at the given"); //$NON-NLS-1$
152
writer.println("\t * plug-in relative path"); //$NON-NLS-1$
153
writer.println("\t *"); //$NON-NLS-1$
154
writer.println("\t * @param path the path"); //$NON-NLS-1$
155
writer.println("\t * @return the image descriptor"); //$NON-NLS-1$
156
writer.println("\t */"); //$NON-NLS-1$
157
writer.println("\tpublic static ImageDescriptor getImageDescriptor(String path) {"); //$NON-NLS-1$
158
writer.println("\t\treturn imageDescriptorFromPlugin(PLUGIN_ID, path);"); //$NON-NLS-1$ //$NON-NLS-2$
159
writer.println("\t}"); //$NON-NLS-1$
160
}
161         writer.println("}"); //$NON-NLS-1$
162
}
163
164     private void generateActivatorClass(String JavaDoc packageName, String JavaDoc className,
165             PrintWriter JavaDoc writer) {
166         if (!packageName.equals("")) { //$NON-NLS-1$
167
writer.println("package " + packageName + ";"); //$NON-NLS-1$ //$NON-NLS-2$
168
writer.println();
169         }
170         writer.println("import org.osgi.framework.BundleActivator;"); //$NON-NLS-1$
171
writer.println("import org.osgi.framework.BundleContext;"); //$NON-NLS-1$
172
writer.println();
173         writer.println("public class " + className + " implements BundleActivator {"); //$NON-NLS-1$ //$NON-NLS-2$
174
writer.println();
175         writer.println("\t/*"); //$NON-NLS-1$
176
writer.println("\t * (non-Javadoc)"); //$NON-NLS-1$
177
writer.println("\t * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)"); //$NON-NLS-1$
178
writer.println("\t */"); //$NON-NLS-1$
179
writer.println("\tpublic void start(BundleContext context) throws Exception {"); //$NON-NLS-1$
180
writer.println("\t}"); //$NON-NLS-1$
181
writer.println();
182         writer.println("\t/*"); //$NON-NLS-1$
183
writer.println("\t * (non-Javadoc)"); //$NON-NLS-1$
184
writer.println("\t * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)"); //$NON-NLS-1$
185
writer.println("\t */"); //$NON-NLS-1$
186
writer.println("\tpublic void stop(BundleContext context) throws Exception {"); //$NON-NLS-1$
187
writer.println("\t}"); //$NON-NLS-1$
188
writer.println();
189         writer.println("}"); //$NON-NLS-1$
190
}
191
192     public IPluginReference[] getDependencies() {
193         ArrayList JavaDoc result = new ArrayList JavaDoc();
194         if (fPluginData.isUIPlugin())
195             result.add(new PluginReference("org.eclipse.ui", null, 0)); //$NON-NLS-1$
196
if (!fPluginData.isLegacy() && fPluginData.getOSGiFramework() == null)
197             result.add(new PluginReference("org.eclipse.core.runtime", null, 0)); //$NON-NLS-1$
198
return (IPluginReference[]) result.toArray(new IPluginReference[result.size()]);
199     }
200     
201     public String JavaDoc[] getImportPackages() {
202         return fPluginData.getOSGiFramework() != null
203                     ? new String JavaDoc[] {"org.osgi.framework;version=\"1.3.0\""} //$NON-NLS-1$
204
: new String JavaDoc[0];
205     }
206
207 }
208
Popular Tags