KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > wizards > feature > AbstractCreateFeatureOperation


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.feature;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14
15 import org.eclipse.core.resources.IFile;
16 import org.eclipse.core.resources.IFolder;
17 import org.eclipse.core.resources.IProject;
18 import org.eclipse.core.resources.IProjectDescription;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IPath;
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.core.runtime.Path;
23 import org.eclipse.jdt.core.IClasspathEntry;
24 import org.eclipse.jdt.core.IJavaProject;
25 import org.eclipse.jdt.core.JavaCore;
26 import org.eclipse.jdt.launching.JavaRuntime;
27 import org.eclipse.jface.dialogs.MessageDialog;
28 import org.eclipse.jface.viewers.ISelection;
29 import org.eclipse.jface.viewers.StructuredSelection;
30 import org.eclipse.pde.core.build.IBuildEntry;
31 import org.eclipse.pde.internal.build.IBuildPropertiesConstants;
32 import org.eclipse.pde.internal.core.build.WorkspaceBuildModel;
33 import org.eclipse.pde.internal.core.feature.WorkspaceFeatureModel;
34 import org.eclipse.pde.internal.core.ifeature.IFeature;
35 import org.eclipse.pde.internal.core.ifeature.IFeatureInfo;
36 import org.eclipse.pde.internal.core.ifeature.IFeatureInstallHandler;
37 import org.eclipse.pde.internal.core.natures.PDE;
38 import org.eclipse.pde.internal.core.util.CoreUtility;
39 import org.eclipse.pde.internal.ui.IPDEUIConstants;
40 import org.eclipse.pde.internal.ui.PDEPlugin;
41 import org.eclipse.pde.internal.ui.PDEUIMessages;
42 import org.eclipse.swt.widgets.Shell;
43 import org.eclipse.ui.IWorkbenchPage;
44 import org.eclipse.ui.IWorkbenchPart;
45 import org.eclipse.ui.PartInitException;
46 import org.eclipse.ui.actions.WorkspaceModifyOperation;
47 import org.eclipse.ui.ide.IDE;
48 import org.eclipse.ui.part.FileEditorInput;
49 import org.eclipse.ui.part.ISetSelectionTarget;
50
51 public abstract class AbstractCreateFeatureOperation extends WorkspaceModifyOperation {
52     
53     protected IProject fProject;
54     protected IPath fLocation;
55     protected FeatureData fFeatureData;
56     private Shell fShell;
57     
58     public AbstractCreateFeatureOperation(IProject project, IPath location, FeatureData featureData, Shell shell) {
59         fProject = project;
60         fLocation = location;
61         fFeatureData = featureData;
62         fShell = shell;
63     }
64     
65     protected void execute(IProgressMonitor monitor) throws CoreException,
66             InvocationTargetException JavaDoc, InterruptedException JavaDoc {
67         try {
68             createFeature(monitor);
69         } catch (CoreException e) {
70             PDEPlugin.logException(e);
71         } finally {
72             monitor.done();
73         }
74     }
75     
76     protected void createFeature(IProgressMonitor monitor) throws CoreException {
77         monitor.beginTask(PDEUIMessages.NewFeatureWizard_creatingProject, 3);
78         IFile file;
79         if (shouldOverwriteFeature()) {
80             createProject(monitor);
81             monitor.worked(1);
82             createBuildProperties();
83             monitor.worked(1);
84             
85             // create feature.xml
86
monitor.subTask(PDEUIMessages.NewFeatureWizard_creatingManifest);
87             file = createFeature();
88             monitor.worked(1);
89         } else {
90             fProject.create(monitor);
91             fProject.open(monitor);
92             file = fProject.getFile("feature.xml"); //$NON-NLS-1$
93
monitor.worked(3);
94         }
95         if (file.exists())
96             openFeatureEditor(file);
97     }
98     
99     private void createProject(IProgressMonitor monitor) throws CoreException {
100         CoreUtility.createProject(fProject, fLocation, monitor);
101         fProject.open(monitor);
102         IProjectDescription desc = fProject.getWorkspace().newProjectDescription(fProject.getName());
103         desc.setLocation(fLocation);
104         if (!fProject.hasNature(PDE.FEATURE_NATURE))
105             CoreUtility.addNatureToProject(fProject, PDE.FEATURE_NATURE, monitor);
106
107         if (fFeatureData.hasCustomHandler()) {
108             if (!fProject.hasNature(JavaCore.NATURE_ID))
109                 CoreUtility.addNatureToProject(fProject, JavaCore.NATURE_ID, monitor);
110             
111             if (fFeatureData.getSourceFolderName() != null
112                     && fFeatureData.getSourceFolderName().trim().length() > 0) {
113                 IFolder folder = fProject.getFolder(fFeatureData.getSourceFolderName());
114                 if (!folder.exists())
115                     CoreUtility.createFolder(folder);
116             }
117             
118             IJavaProject jproject = JavaCore.create(fProject);
119             jproject.setOutputLocation(fProject.getFullPath().append(
120                     fFeatureData.getJavaBuildFolderName()), monitor);
121             jproject.setRawClasspath(
122                     new IClasspathEntry[] {
123                             JavaCore.newSourceEntry(fProject.getFullPath().append(fFeatureData.getSourceFolderName())),
124                             JavaCore.newContainerEntry(new Path(JavaRuntime.JRE_CONTAINER))
125                     }, monitor);
126         }
127     }
128     
129     protected void createBuildProperties() throws CoreException {
130         IFile file = fProject.getFile("build.properties"); //$NON-NLS-1$
131
if (!file.exists()) {
132             WorkspaceBuildModel model = new WorkspaceBuildModel(file);
133             IBuildEntry ientry = model.getFactory().createEntry("bin.includes"); //$NON-NLS-1$
134
ientry.addToken("feature.xml"); //$NON-NLS-1$
135
String JavaDoc library = fFeatureData.library;
136             if (library != null) {
137                 String JavaDoc source = fFeatureData.getSourceFolderName();
138                 if (source != null) {
139                     IBuildEntry entry = model.getFactory().createEntry(IBuildEntry.JAR_PREFIX + library);
140                     if (!source.endsWith("/")) //$NON-NLS-1$
141
source += "/"; //$NON-NLS-1$
142
entry.addToken(source);
143                     ientry.addToken(library);
144                     model.getBuild().add(entry);
145                 }
146                 String JavaDoc output = fFeatureData.getJavaBuildFolderName();
147                 if (output != null) {
148                     IBuildEntry entry = model.getFactory().createEntry(IBuildPropertiesConstants.PROPERTY_OUTPUT_PREFIX + library);
149                     if (!output.endsWith("/")) //$NON-NLS-1$
150
output += "/"; //$NON-NLS-1$
151
entry.addToken(output);
152                     model.getBuild().add(entry);
153                 }
154             }
155
156             model.getBuild().add(ientry);
157             model.save();
158         }
159         IDE.setDefaultEditor(file, IPDEUIConstants.BUILD_EDITOR_ID);
160     }
161     
162     protected IFile createFeature() throws CoreException {
163         IFile file = fProject.getFile("feature.xml"); //$NON-NLS-1$
164
WorkspaceFeatureModel model = new WorkspaceFeatureModel();
165         model.setFile(file);
166         IFeature feature = model.getFeature();
167         feature.setLabel(fFeatureData.name);
168         feature.setId(fFeatureData.id);
169         feature.setVersion(fFeatureData.version);
170         feature.setProviderName(fFeatureData.provider);
171         if (fFeatureData.hasCustomHandler())
172             feature.setInstallHandler(model.getFactory().createInstallHandler());
173
174         configureFeature(feature, model);
175         
176         IFeatureInstallHandler handler = feature.getInstallHandler();
177         if (handler != null)
178             handler.setLibrary(fFeatureData.library);
179
180         IFeatureInfo info = model.getFactory().createInfo(IFeature.INFO_COPYRIGHT);
181         feature.setFeatureInfo(info, IFeature.INFO_COPYRIGHT);
182         info.setURL("http://www.example.com/copyright"); //$NON-NLS-1$
183
info.setDescription(PDEUIMessages.NewFeatureWizard_sampleCopyrightDesc);
184
185         info = model.getFactory().createInfo(IFeature.INFO_LICENSE);
186         feature.setFeatureInfo(info, IFeature.INFO_LICENSE);
187         info.setURL("http://www.example.com/license"); //$NON-NLS-1$
188
info.setDescription(PDEUIMessages.NewFeatureWizard_sampleLicenseDesc);
189
190         info = model.getFactory().createInfo(IFeature.INFO_DESCRIPTION);
191         feature.setFeatureInfo(info, IFeature.INFO_DESCRIPTION);
192         info.setURL("http://www.example.com/description"); //$NON-NLS-1$
193
info.setDescription(PDEUIMessages.NewFeatureWizard_sampleDescriptionDesc);
194
195         // Save the model
196
model.save();
197         model.dispose();
198         IDE.setDefaultEditor(file, IPDEUIConstants.FEATURE_EDITOR_ID);
199         return file;
200     }
201     
202     protected abstract void configureFeature(IFeature feature, WorkspaceFeatureModel model) throws CoreException ;
203     
204     protected void openFeatureEditor(IFile manifestFile) {
205         IWorkbenchPage page = PDEPlugin.getActivePage();
206         // Reveal the file first
207
final ISelection selection = new StructuredSelection(manifestFile);
208         final IWorkbenchPart activePart = page.getActivePart();
209
210         if (activePart instanceof ISetSelectionTarget) {
211             fShell.getDisplay().asyncExec(new Runnable JavaDoc() {
212                 public void run() {
213                     ((ISetSelectionTarget) activePart).selectReveal(selection);
214                 }
215             });
216         }
217         // Open the editor
218
try {
219             page.openEditor(new FileEditorInput(manifestFile), IPDEUIConstants.FEATURE_EDITOR_ID);
220         } catch (PartInitException e) {
221             PDEPlugin.logException(e);
222         }
223     }
224     
225     protected boolean shouldOverwriteFeature() {
226         return
227             !fLocation.append(fProject.getName()).toFile().exists()
228                 ||
229             MessageDialog.openQuestion(
230                 PDEPlugin.getActiveWorkbenchShell(),
231                 this instanceof CreateFeaturePatchOperation ?
232                         PDEUIMessages.FeaturePatch_wtitle : PDEUIMessages.NewFeatureWizard_wtitle,
233                 PDEUIMessages.NewFeatureWizard_overwriteFeature);
234     }
235 }
236
Popular Tags