1 11 package org.eclipse.pde.internal.ui.wizards.feature; 12 13 import java.lang.reflect.InvocationTargetException ; 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 , InterruptedException { 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 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"); 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"); if (!file.exists()) { 132 WorkspaceBuildModel model = new WorkspaceBuildModel(file); 133 IBuildEntry ientry = model.getFactory().createEntry("bin.includes"); ientry.addToken("feature.xml"); String library = fFeatureData.library; 136 if (library != null) { 137 String source = fFeatureData.getSourceFolderName(); 138 if (source != null) { 139 IBuildEntry entry = model.getFactory().createEntry(IBuildEntry.JAR_PREFIX + library); 140 if (!source.endsWith("/")) source += "/"; entry.addToken(source); 143 ientry.addToken(library); 144 model.getBuild().add(entry); 145 } 146 String output = fFeatureData.getJavaBuildFolderName(); 147 if (output != null) { 148 IBuildEntry entry = model.getFactory().createEntry(IBuildPropertiesConstants.PROPERTY_OUTPUT_PREFIX + library); 149 if (!output.endsWith("/")) output += "/"; 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"); 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"); 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"); 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"); info.setDescription(PDEUIMessages.NewFeatureWizard_sampleDescriptionDesc); 194 195 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 final ISelection selection = new StructuredSelection(manifestFile); 208 final IWorkbenchPart activePart = page.getActivePart(); 209 210 if (activePart instanceof ISetSelectionTarget) { 211 fShell.getDisplay().asyncExec(new Runnable () { 212 public void run() { 213 ((ISetSelectionTarget) activePart).selectReveal(selection); 214 } 215 }); 216 } 217 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 |