001: /*******************************************************************************
002: * Copyright (c) 2005, 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.feature;
011:
012: import java.lang.reflect.InvocationTargetException;
013:
014: import org.eclipse.core.resources.IFile;
015: import org.eclipse.core.resources.IFolder;
016: import org.eclipse.core.resources.IProject;
017: import org.eclipse.core.resources.IProjectDescription;
018: import org.eclipse.core.runtime.CoreException;
019: import org.eclipse.core.runtime.IPath;
020: import org.eclipse.core.runtime.IProgressMonitor;
021: import org.eclipse.core.runtime.Path;
022: import org.eclipse.jdt.core.IClasspathEntry;
023: import org.eclipse.jdt.core.IJavaProject;
024: import org.eclipse.jdt.core.JavaCore;
025: import org.eclipse.jdt.launching.JavaRuntime;
026: import org.eclipse.jface.dialogs.MessageDialog;
027: import org.eclipse.jface.viewers.ISelection;
028: import org.eclipse.jface.viewers.StructuredSelection;
029: import org.eclipse.pde.core.build.IBuildEntry;
030: import org.eclipse.pde.internal.build.IBuildPropertiesConstants;
031: import org.eclipse.pde.internal.core.build.WorkspaceBuildModel;
032: import org.eclipse.pde.internal.core.feature.WorkspaceFeatureModel;
033: import org.eclipse.pde.internal.core.ifeature.IFeature;
034: import org.eclipse.pde.internal.core.ifeature.IFeatureInfo;
035: import org.eclipse.pde.internal.core.ifeature.IFeatureInstallHandler;
036: import org.eclipse.pde.internal.core.natures.PDE;
037: import org.eclipse.pde.internal.core.util.CoreUtility;
038: import org.eclipse.pde.internal.ui.IPDEUIConstants;
039: import org.eclipse.pde.internal.ui.PDEPlugin;
040: import org.eclipse.pde.internal.ui.PDEUIMessages;
041: import org.eclipse.swt.widgets.Shell;
042: import org.eclipse.ui.IWorkbenchPage;
043: import org.eclipse.ui.IWorkbenchPart;
044: import org.eclipse.ui.PartInitException;
045: import org.eclipse.ui.actions.WorkspaceModifyOperation;
046: import org.eclipse.ui.ide.IDE;
047: import org.eclipse.ui.part.FileEditorInput;
048: import org.eclipse.ui.part.ISetSelectionTarget;
049:
050: public abstract class AbstractCreateFeatureOperation extends
051: WorkspaceModifyOperation {
052:
053: protected IProject fProject;
054: protected IPath fLocation;
055: protected FeatureData fFeatureData;
056: private Shell fShell;
057:
058: public AbstractCreateFeatureOperation(IProject project,
059: IPath location, FeatureData featureData, Shell shell) {
060: fProject = project;
061: fLocation = location;
062: fFeatureData = featureData;
063: fShell = shell;
064: }
065:
066: protected void execute(IProgressMonitor monitor)
067: throws CoreException, InvocationTargetException,
068: InterruptedException {
069: try {
070: createFeature(monitor);
071: } catch (CoreException e) {
072: PDEPlugin.logException(e);
073: } finally {
074: monitor.done();
075: }
076: }
077:
078: protected void createFeature(IProgressMonitor monitor)
079: throws CoreException {
080: monitor.beginTask(
081: PDEUIMessages.NewFeatureWizard_creatingProject, 3);
082: IFile file;
083: if (shouldOverwriteFeature()) {
084: createProject(monitor);
085: monitor.worked(1);
086: createBuildProperties();
087: monitor.worked(1);
088:
089: // create feature.xml
090: monitor
091: .subTask(PDEUIMessages.NewFeatureWizard_creatingManifest);
092: file = createFeature();
093: monitor.worked(1);
094: } else {
095: fProject.create(monitor);
096: fProject.open(monitor);
097: file = fProject.getFile("feature.xml"); //$NON-NLS-1$
098: monitor.worked(3);
099: }
100: if (file.exists())
101: openFeatureEditor(file);
102: }
103:
104: private void createProject(IProgressMonitor monitor)
105: throws CoreException {
106: CoreUtility.createProject(fProject, fLocation, monitor);
107: fProject.open(monitor);
108: IProjectDescription desc = fProject.getWorkspace()
109: .newProjectDescription(fProject.getName());
110: desc.setLocation(fLocation);
111: if (!fProject.hasNature(PDE.FEATURE_NATURE))
112: CoreUtility.addNatureToProject(fProject,
113: PDE.FEATURE_NATURE, monitor);
114:
115: if (fFeatureData.hasCustomHandler()) {
116: if (!fProject.hasNature(JavaCore.NATURE_ID))
117: CoreUtility.addNatureToProject(fProject,
118: JavaCore.NATURE_ID, monitor);
119:
120: if (fFeatureData.getSourceFolderName() != null
121: && fFeatureData.getSourceFolderName().trim()
122: .length() > 0) {
123: IFolder folder = fProject.getFolder(fFeatureData
124: .getSourceFolderName());
125: if (!folder.exists())
126: CoreUtility.createFolder(folder);
127: }
128:
129: IJavaProject jproject = JavaCore.create(fProject);
130: jproject.setOutputLocation(fProject.getFullPath().append(
131: fFeatureData.getJavaBuildFolderName()), monitor);
132: jproject
133: .setRawClasspath(
134: new IClasspathEntry[] {
135: JavaCore
136: .newSourceEntry(fProject
137: .getFullPath()
138: .append(
139: fFeatureData
140: .getSourceFolderName())),
141: JavaCore
142: .newContainerEntry(new Path(
143: JavaRuntime.JRE_CONTAINER)) },
144: monitor);
145: }
146: }
147:
148: protected void createBuildProperties() throws CoreException {
149: IFile file = fProject.getFile("build.properties"); //$NON-NLS-1$
150: if (!file.exists()) {
151: WorkspaceBuildModel model = new WorkspaceBuildModel(file);
152: IBuildEntry ientry = model.getFactory().createEntry(
153: "bin.includes"); //$NON-NLS-1$
154: ientry.addToken("feature.xml"); //$NON-NLS-1$
155: String library = fFeatureData.library;
156: if (library != null) {
157: String source = fFeatureData.getSourceFolderName();
158: if (source != null) {
159: IBuildEntry entry = model.getFactory().createEntry(
160: IBuildEntry.JAR_PREFIX + library);
161: if (!source.endsWith("/")) //$NON-NLS-1$
162: source += "/"; //$NON-NLS-1$
163: entry.addToken(source);
164: ientry.addToken(library);
165: model.getBuild().add(entry);
166: }
167: String output = fFeatureData.getJavaBuildFolderName();
168: if (output != null) {
169: IBuildEntry entry = model
170: .getFactory()
171: .createEntry(
172: IBuildPropertiesConstants.PROPERTY_OUTPUT_PREFIX
173: + library);
174: if (!output.endsWith("/")) //$NON-NLS-1$
175: output += "/"; //$NON-NLS-1$
176: entry.addToken(output);
177: model.getBuild().add(entry);
178: }
179: }
180:
181: model.getBuild().add(ientry);
182: model.save();
183: }
184: IDE.setDefaultEditor(file, IPDEUIConstants.BUILD_EDITOR_ID);
185: }
186:
187: protected IFile createFeature() throws CoreException {
188: IFile file = fProject.getFile("feature.xml"); //$NON-NLS-1$
189: WorkspaceFeatureModel model = new WorkspaceFeatureModel();
190: model.setFile(file);
191: IFeature feature = model.getFeature();
192: feature.setLabel(fFeatureData.name);
193: feature.setId(fFeatureData.id);
194: feature.setVersion(fFeatureData.version);
195: feature.setProviderName(fFeatureData.provider);
196: if (fFeatureData.hasCustomHandler())
197: feature.setInstallHandler(model.getFactory()
198: .createInstallHandler());
199:
200: configureFeature(feature, model);
201:
202: IFeatureInstallHandler handler = feature.getInstallHandler();
203: if (handler != null)
204: handler.setLibrary(fFeatureData.library);
205:
206: IFeatureInfo info = model.getFactory().createInfo(
207: IFeature.INFO_COPYRIGHT);
208: feature.setFeatureInfo(info, IFeature.INFO_COPYRIGHT);
209: info.setURL("http://www.example.com/copyright"); //$NON-NLS-1$
210: info
211: .setDescription(PDEUIMessages.NewFeatureWizard_sampleCopyrightDesc);
212:
213: info = model.getFactory().createInfo(IFeature.INFO_LICENSE);
214: feature.setFeatureInfo(info, IFeature.INFO_LICENSE);
215: info.setURL("http://www.example.com/license"); //$NON-NLS-1$
216: info
217: .setDescription(PDEUIMessages.NewFeatureWizard_sampleLicenseDesc);
218:
219: info = model.getFactory().createInfo(IFeature.INFO_DESCRIPTION);
220: feature.setFeatureInfo(info, IFeature.INFO_DESCRIPTION);
221: info.setURL("http://www.example.com/description"); //$NON-NLS-1$
222: info
223: .setDescription(PDEUIMessages.NewFeatureWizard_sampleDescriptionDesc);
224:
225: // Save the model
226: model.save();
227: model.dispose();
228: IDE.setDefaultEditor(file, IPDEUIConstants.FEATURE_EDITOR_ID);
229: return file;
230: }
231:
232: protected abstract void configureFeature(IFeature feature,
233: WorkspaceFeatureModel model) throws CoreException;
234:
235: protected void openFeatureEditor(IFile manifestFile) {
236: IWorkbenchPage page = PDEPlugin.getActivePage();
237: // Reveal the file first
238: final ISelection selection = new StructuredSelection(
239: manifestFile);
240: final IWorkbenchPart activePart = page.getActivePart();
241:
242: if (activePart instanceof ISetSelectionTarget) {
243: fShell.getDisplay().asyncExec(new Runnable() {
244: public void run() {
245: ((ISetSelectionTarget) activePart)
246: .selectReveal(selection);
247: }
248: });
249: }
250: // Open the editor
251: try {
252: page.openEditor(new FileEditorInput(manifestFile),
253: IPDEUIConstants.FEATURE_EDITOR_ID);
254: } catch (PartInitException e) {
255: PDEPlugin.logException(e);
256: }
257: }
258:
259: protected boolean shouldOverwriteFeature() {
260: return !fLocation.append(fProject.getName()).toFile().exists()
261: || MessageDialog
262: .openQuestion(
263: PDEPlugin.getActiveWorkbenchShell(),
264: this instanceof CreateFeaturePatchOperation ? PDEUIMessages.FeaturePatch_wtitle
265: : PDEUIMessages.NewFeatureWizard_wtitle,
266: PDEUIMessages.NewFeatureWizard_overwriteFeature);
267: }
268: }
|