1 11 package org.eclipse.pde.internal.ui.wizards.plugin; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.Set ; 17 import java.util.TreeSet ; 18 19 import org.eclipse.core.resources.IFile; 20 import org.eclipse.core.resources.IFolder; 21 import org.eclipse.core.resources.IProject; 22 import org.eclipse.core.resources.ProjectScope; 23 import org.eclipse.core.runtime.CoreException; 24 import org.eclipse.core.runtime.IPath; 25 import org.eclipse.core.runtime.IProgressMonitor; 26 import org.eclipse.core.runtime.Path; 27 import org.eclipse.core.runtime.SubProgressMonitor; 28 import org.eclipse.core.runtime.preferences.IEclipsePreferences; 29 import org.eclipse.jdt.core.IClasspathEntry; 30 import org.eclipse.jdt.core.IJavaElement; 31 import org.eclipse.jdt.core.IJavaProject; 32 import org.eclipse.jdt.core.IPackageFragment; 33 import org.eclipse.jdt.core.IPackageFragmentRoot; 34 import org.eclipse.jdt.core.JavaCore; 35 import org.eclipse.jdt.core.JavaModelException; 36 import org.eclipse.jface.viewers.ISelection; 37 import org.eclipse.jface.viewers.StructuredSelection; 38 import org.eclipse.osgi.service.resolver.VersionRange; 39 import org.eclipse.pde.core.build.IBuildEntry; 40 import org.eclipse.pde.core.build.IBuildModelFactory; 41 import org.eclipse.pde.core.plugin.IFragment; 42 import org.eclipse.pde.core.plugin.IPlugin; 43 import org.eclipse.pde.core.plugin.IPluginBase; 44 import org.eclipse.pde.core.plugin.IPluginImport; 45 import org.eclipse.pde.core.plugin.IPluginLibrary; 46 import org.eclipse.pde.core.plugin.IPluginReference; 47 import org.eclipse.pde.internal.core.ICoreConstants; 48 import org.eclipse.pde.internal.core.PDECore; 49 import org.eclipse.pde.internal.core.build.WorkspaceBuildModel; 50 import org.eclipse.pde.internal.core.bundle.BundlePluginBase; 51 import org.eclipse.pde.internal.core.bundle.WorkspaceBundleFragmentModel; 52 import org.eclipse.pde.internal.core.bundle.WorkspaceBundlePluginModel; 53 import org.eclipse.pde.internal.core.bundle.WorkspaceBundlePluginModelBase; 54 import org.eclipse.pde.internal.core.ibundle.IBundle; 55 import org.eclipse.pde.internal.core.ibundle.IBundlePluginBase; 56 import org.eclipse.pde.internal.core.ibundle.IBundlePluginModelBase; 57 import org.eclipse.pde.internal.core.natures.PDE; 58 import org.eclipse.pde.internal.core.plugin.WorkspaceFragmentModel; 59 import org.eclipse.pde.internal.core.plugin.WorkspacePluginModel; 60 import org.eclipse.pde.internal.core.plugin.WorkspacePluginModelBase; 61 import org.eclipse.pde.internal.core.util.CoreUtility; 62 import org.eclipse.pde.internal.ui.PDEPlugin; 63 import org.eclipse.pde.internal.ui.PDEUIMessages; 64 import org.eclipse.pde.internal.ui.wizards.IProjectProvider; 65 import org.eclipse.pde.ui.IBundleContentWizard; 66 import org.eclipse.pde.ui.IFieldData; 67 import org.eclipse.pde.ui.IFragmentFieldData; 68 import org.eclipse.pde.ui.IPluginContentWizard; 69 import org.eclipse.pde.ui.IPluginFieldData; 70 import org.eclipse.ui.IWorkbenchPage; 71 import org.eclipse.ui.IWorkbenchPart; 72 import org.eclipse.ui.IWorkbenchWindow; 73 import org.eclipse.ui.PartInitException; 74 import org.eclipse.ui.actions.WorkspaceModifyOperation; 75 import org.eclipse.ui.ide.IDE; 76 import org.eclipse.ui.part.ISetSelectionTarget; 77 import org.osgi.framework.Constants; 78 import org.osgi.service.prefs.BackingStoreException; 79 80 public class NewProjectCreationOperation extends WorkspaceModifyOperation { 81 private IPluginContentWizard fContentWizard; 82 83 private IFieldData fData; 84 85 private PluginClassCodeGenerator fGenerator; 86 87 private WorkspacePluginModelBase fModel; 88 89 private IProjectProvider fProjectProvider; 90 91 private boolean fResult; 92 93 public NewProjectCreationOperation(IFieldData data, 94 IProjectProvider provider, IPluginContentWizard contentWizard) { 95 fData = data; 96 fProjectProvider = provider; 97 fContentWizard = contentWizard; 98 } 99 100 protected void adjustManifests(IProgressMonitor monitor, IProject project, IPluginBase bundle) 102 throws CoreException { 103 IPluginLibrary[] libs = fModel.getPluginBase().getLibraries(); 105 Set packages = new TreeSet (); 106 for (int i = 0; i < libs.length; i++) { 107 String [] filters = libs[i].getContentFilters(); 108 if (filters.length == 1 && filters[0].equals("**")) { addAllSourcePackages(project, packages); 111 break; 112 } 113 for (int j = 0; j < filters.length; j++) { 114 if (filters[j].endsWith(".*")) packages.add(filters[j].substring(0, filters[j].length() - 2)); 116 } 117 } 118 if (!packages.isEmpty()) { 119 IBundle iBundle = ((WorkspaceBundlePluginModelBase)fModel).getBundleModel().getBundle(); 120 iBundle.setHeader(Constants.EXPORT_PACKAGE, getCommaValueFromSet(packages)); 121 } 122 } 123 124 private void createBuildPropertiesFile(IProject project) 125 throws CoreException { 126 IFile file = project.getFile("build.properties"); if (!file.exists()) { 128 WorkspaceBuildModel model = new WorkspaceBuildModel(file); 129 IBuildModelFactory factory = model.getFactory(); 130 131 IBuildEntry binEntry = factory.createEntry(IBuildEntry.BIN_INCLUDES); 133 fillBinIncludes(project, binEntry); 134 createSourceOutputBuildEntries(model, factory); 135 model.getBuild().add(binEntry); 136 model.save(); 137 } 138 } 139 140 protected void createSourceOutputBuildEntries(WorkspaceBuildModel model, 141 IBuildModelFactory factory) throws CoreException { 142 String srcFolder = fData.getSourceFolderName(); 143 if (!fData.isSimple() && srcFolder != null) { 144 String libraryName = fData.getLibraryName(); 145 if (libraryName == null) 146 libraryName = "."; IBuildEntry entry = factory.createEntry(IBuildEntry.JAR_PREFIX 149 + libraryName); 150 if (srcFolder.length() > 0) 151 entry.addToken(new Path(srcFolder).addTrailingSeparator().toString()); 152 else 153 entry.addToken("."); model.getBuild().add(entry); 155 156 entry = factory.createEntry(IBuildEntry.OUTPUT_PREFIX + libraryName); 158 String outputFolder = fData.getOutputFolderName().trim(); 159 if (outputFolder.length() > 0) 160 entry.addToken(new Path(outputFolder).addTrailingSeparator().toString()); 161 else 162 entry.addToken("."); model.getBuild().add(entry); 164 } 165 } 166 167 protected void createContents(IProgressMonitor monitor, IProject project) 168 throws CoreException, JavaModelException, 169 InvocationTargetException , InterruptedException { 170 } 171 172 private void createManifest(IProject project) throws CoreException { 173 if (fData.hasBundleStructure()) { 174 if (fData instanceof IFragmentFieldData) { 175 fModel = new WorkspaceBundleFragmentModel(project.getFile(ICoreConstants.BUNDLE_FILENAME_DESCRIPTOR), 176 project.getFile(ICoreConstants.FRAGMENT_FILENAME_DESCRIPTOR)); 177 } else { 178 fModel = new WorkspaceBundlePluginModel(project.getFile(ICoreConstants.BUNDLE_FILENAME_DESCRIPTOR), 179 project.getFile(ICoreConstants.PLUGIN_FILENAME_DESCRIPTOR)); 180 } 181 } else { 182 if (fData instanceof IFragmentFieldData) { 183 fModel = new WorkspaceFragmentModel(project.getFile(ICoreConstants.FRAGMENT_FILENAME_DESCRIPTOR), false); 184 } else { 185 fModel = new WorkspacePluginModel(project.getFile(ICoreConstants.PLUGIN_FILENAME_DESCRIPTOR), false); 186 } 187 } 188 IPluginBase pluginBase = fModel.getPluginBase(); 189 String targetVersion = ((AbstractFieldData) fData).getTargetVersion(); 190 pluginBase.setSchemaVersion(Double.parseDouble(targetVersion) < 3.2 ? "3.0" : "3.2"); pluginBase.setId(fData.getId()); 192 pluginBase.setVersion(fData.getVersion()); 193 pluginBase.setName(fData.getName()); 194 pluginBase.setProviderName(fData.getProvider()); 195 if (fModel instanceof IBundlePluginModelBase) { 196 IBundlePluginModelBase bmodel = ((IBundlePluginModelBase)fModel); 197 ((IBundlePluginBase)bmodel.getPluginBase()).setTargetVersion(targetVersion); 198 bmodel.getBundleModel().getBundle().setHeader(Constants.BUNDLE_MANIFESTVERSION, "2"); } 200 if (pluginBase instanceof IFragment) { 201 IFragment fragment = (IFragment) pluginBase; 202 IFragmentFieldData data = (IFragmentFieldData) fData; 203 fragment.setPluginId(data.getPluginId()); 204 VersionRange version = new VersionRange(data.getPluginVersion()); 205 fragment.setPluginVersion(version.getMinimum().toString()); 206 fragment.setRule(data.getMatch()); 207 } else { 208 if (((IPluginFieldData) fData).doGenerateClass()) 209 ((IPlugin) pluginBase).setClassName(((IPluginFieldData) fData).getClassname()); 210 } 211 if (!fData.isSimple()) { 212 setPluginLibraries(fModel); 213 } 214 215 IPluginReference[] dependencies = getDependencies(); 216 for (int i = 0; i < dependencies.length; i++) { 217 IPluginReference ref = dependencies[i]; 218 IPluginImport iimport = fModel.getPluginFactory().createImport(); 219 iimport.setId(ref.getId()); 220 iimport.setVersion(ref.getVersion()); 221 iimport.setMatch(ref.getMatch()); 222 pluginBase.add(iimport); 223 } 224 if (pluginBase instanceof BundlePluginBase) { 226 IBundle bundle = ((BundlePluginBase)pluginBase).getBundle(); 227 if (fData instanceof AbstractFieldData) { 228 String framework = ((AbstractFieldData)fData).getOSGiFramework(); 229 if (framework != null) { 230 String value = getCommaValueFromSet(getImportPackagesSet()); 231 if (value.length() > 0) 232 bundle.setHeader(Constants.IMPORT_PACKAGE, value); 233 if (!framework.equals(ICoreConstants.EQUINOX)) 235 return; 236 } 237 } 238 if (fData instanceof IPluginFieldData && ((IPluginFieldData)fData).doGenerateClass()) { 239 if (targetVersion.equals("3.1")) bundle.setHeader(ICoreConstants.ECLIPSE_AUTOSTART, "true"); else 242 bundle.setHeader(ICoreConstants.ECLIPSE_LAZYSTART, "true"); } 244 if (fContentWizard != null) { 245 String [] newFiles = fContentWizard.getNewFiles(); 246 if (newFiles != null) 247 for (int i = 0; i < newFiles.length; i++) { 248 if ("plugin.properties".equals(newFiles[i])) { bundle.setHeader(Constants.BUNDLE_LOCALIZATION, "plugin"); break; 251 } 252 } 253 } 254 } 255 } 256 257 private IProject createProject() throws CoreException { 258 IProject project = fProjectProvider.getProject(); 259 if (!project.exists()) { 260 CoreUtility.createProject(project, fProjectProvider 261 .getLocationPath(), null); 262 project.open(null); 263 } 264 if (!project.hasNature(PDE.PLUGIN_NATURE)) 265 CoreUtility.addNatureToProject(project, PDE.PLUGIN_NATURE, null); 266 if (!fData.isSimple() && !project.hasNature(JavaCore.NATURE_ID)) 267 CoreUtility.addNatureToProject(project, JavaCore.NATURE_ID, null); 268 if (!fData.isSimple() && fData.getSourceFolderName() != null 269 && fData.getSourceFolderName().trim().length() > 0) { 270 IFolder folder = project.getFolder(fData.getSourceFolderName()); 271 if (!folder.exists()) 272 CoreUtility.createFolder(folder); 273 } 274 return project; 275 } 276 277 282 protected void execute(IProgressMonitor monitor) throws CoreException, 283 InvocationTargetException , InterruptedException { 284 285 monitor.beginTask(PDEUIMessages.NewProjectCreationOperation_creating, 287 getNumberOfWorkUnits()); 288 monitor.subTask(PDEUIMessages.NewProjectCreationOperation_project); 289 290 IProject project = createProject(); 292 monitor.worked(1); 293 createContents(monitor, project); 294 if (project.hasNature(JavaCore.NATURE_ID)) { 296 monitor.subTask(PDEUIMessages.NewProjectCreationOperation_setClasspath); 297 setClasspath(project, fData); 298 monitor.worked(1); 299 } 300 301 if (fData instanceof PluginFieldData) { 302 PluginFieldData data = (PluginFieldData) fData; 303 304 if (data.doGenerateClass()) { 306 generateTopLevelPluginClass(project, new SubProgressMonitor( 307 monitor, 1)); 308 } 309 } 310 monitor.subTask(PDEUIMessages.NewProjectCreationOperation_manifestFile); 312 createManifest(project); 313 monitor.worked(1); 314 315 monitor.subTask(PDEUIMessages.NewProjectCreationOperation_buildPropertiesFile); 317 createBuildPropertiesFile(project); 318 monitor.worked(1); 319 320 boolean contentWizardResult = true; 322 if (fContentWizard != null) { 323 contentWizardResult = fContentWizard.performFinish(project, fModel, 324 new SubProgressMonitor(monitor, 1)); 325 } 326 327 if (fData instanceof AbstractFieldData) { 328 String framework = ((AbstractFieldData)fData).getOSGiFramework(); 329 if (framework != null) { 330 IEclipsePreferences pref = new ProjectScope(project).getNode(PDECore.PLUGIN_ID); 331 if (pref != null) { 332 pref.putBoolean(ICoreConstants.RESOLVE_WITH_REQUIRE_BUNDLE, false); 333 pref.putBoolean(ICoreConstants.EXTENSIONS_PROPERTY, false); 334 if (!ICoreConstants.EQUINOX.equals(framework)) 335 pref.putBoolean(ICoreConstants.EQUINOX_PROPERTY, false); 336 try { 337 pref.flush(); 338 } catch (BackingStoreException e) { 339 PDEPlugin.logException(e); 340 } 341 } 342 } 343 } 344 345 if (fData.hasBundleStructure() && fModel instanceof WorkspaceBundlePluginModelBase) { 346 adjustManifests(new SubProgressMonitor(monitor, 1), project, 347 fModel.getPluginBase()); 348 } 349 350 fModel.save(); 351 openFile((IFile) fModel.getUnderlyingResource()); 352 monitor.worked(1); 353 354 fResult = contentWizardResult; 355 } 356 357 private Set getImportPackagesSet() { 358 TreeSet set = new TreeSet (); 359 if (fGenerator != null) { 360 String [] packages = fGenerator.getImportPackages(); 361 for (int i = 0; i < packages.length; i++) { 362 set.add(packages[i]); 363 } 364 } 365 if (fContentWizard instanceof IBundleContentWizard) { 366 String [] packages = ((IBundleContentWizard)fContentWizard).getImportPackages(); 367 for (int i = 0; i < packages.length; i++) { 368 set.add(packages[i]); 369 } 370 } 371 return set; 372 } 373 374 protected void fillBinIncludes(IProject project, IBuildEntry binEntry) 375 throws CoreException { 376 if ((!fData.hasBundleStructure() || fContentWizard != null) 377 && ((AbstractFieldData)fData).getOSGiFramework() == null) 378 binEntry.addToken(fData instanceof IFragmentFieldData ? ICoreConstants.FRAGMENT_FILENAME_DESCRIPTOR 379 : ICoreConstants.PLUGIN_FILENAME_DESCRIPTOR); 380 if (fData.hasBundleStructure()) 381 binEntry.addToken("META-INF/"); if (!fData.isSimple()) { 383 String libraryName = fData.getLibraryName(); 384 binEntry.addToken(libraryName == null ? "." : libraryName); } 386 if (fContentWizard != null) { 387 String [] files = fContentWizard.getNewFiles(); 388 for (int j = 0; j < files.length; j++) { 389 if (!binEntry.contains(files[j])) 390 binEntry.addToken(files[j]); 391 } 392 } 393 } 394 395 private void generateTopLevelPluginClass(IProject project, 396 IProgressMonitor monitor) throws CoreException { 397 PluginFieldData data = (PluginFieldData) fData; 398 fGenerator = new PluginClassCodeGenerator(project, data.getClassname(), data, fContentWizard != null); 399 fGenerator.generate(monitor); 400 monitor.done(); 401 } 402 403 private IClasspathEntry[] getClassPathEntries(IProject project, 404 IFieldData data) { 405 IClasspathEntry[] internalClassPathEntries = getInternalClassPathEntries( 406 project, data); 407 IClasspathEntry[] entries = new IClasspathEntry[internalClassPathEntries.length + 2]; 408 System.arraycopy(internalClassPathEntries, 0, entries, 0, internalClassPathEntries.length); 409 entries[entries.length - 2] = ClasspathComputer.createJREEntry(null); 410 entries[entries.length - 1] = ClasspathComputer.createContainerEntry(); 411 return entries; 412 } 413 414 private IPluginReference[] getDependencies() { 415 ArrayList result = new ArrayList (); 416 if (fGenerator != null) { 417 IPluginReference[] refs = fGenerator.getDependencies(); 418 for (int i = 0; i < refs.length; i++) { 419 result.add(refs[i]); 420 } 421 } 422 423 if (fContentWizard != null) { 424 IPluginReference[] refs = fContentWizard.getDependencies(fData 425 .isLegacy() ? null : "3.0"); for (int j = 0; j < refs.length; j++) { 427 if (!result.contains(refs[j])) 428 result.add(refs[j]); 429 } 430 } 431 return (IPluginReference[]) result.toArray(new IPluginReference[result 432 .size()]); 433 } 434 435 protected IClasspathEntry[] getInternalClassPathEntries(IProject project, 436 IFieldData data) { 437 if (data.getSourceFolderName() == null) { 438 return new IClasspathEntry[0]; 439 } 440 IClasspathEntry[] entries = new IClasspathEntry[1]; 441 IPath path = project.getFullPath().append(data.getSourceFolderName()); 442 entries[0] = JavaCore.newSourceEntry(path); 443 return entries; 444 } 445 446 protected int getNumberOfWorkUnits() { 447 int numUnits = 4; 448 if (fData.hasBundleStructure()) 449 numUnits++; 450 if (fData instanceof IPluginFieldData) { 451 IPluginFieldData data = (IPluginFieldData) fData; 452 if (data.doGenerateClass()) 453 numUnits++; 454 if (fContentWizard != null) 455 numUnits++; 456 } 457 return numUnits; 458 } 459 460 public boolean getResult() { 461 return fResult; 462 } 463 464 private void openFile(final IFile file) { 465 final IWorkbenchWindow ww = PDEPlugin.getActiveWorkbenchWindow(); 466 final IWorkbenchPage page = ww.getActivePage(); 467 if (page == null) 468 return; 469 final IWorkbenchPart focusPart = page.getActivePart(); 470 ww.getShell().getDisplay().asyncExec(new Runnable () { 471 public void run() { 472 if (focusPart instanceof ISetSelectionTarget) { 473 ISelection selection = new StructuredSelection(file); 474 ((ISetSelectionTarget) focusPart).selectReveal(selection); 475 } 476 try { 477 IDE.openEditor(page, file, true); 478 } catch (PartInitException e) { 479 } 480 } 481 }); 482 } 483 484 private void setClasspath(IProject project, IFieldData data) 485 throws JavaModelException, CoreException { 486 IJavaProject javaProject = JavaCore.create(project); 487 if (data.getOutputFolderName() != null) { 489 IPath path = project.getFullPath().append( 490 data.getOutputFolderName()); 491 javaProject.setOutputLocation(path, null); 492 } 493 IClasspathEntry[] entries = getClassPathEntries(project, data); 494 javaProject.setRawClasspath(entries, null); 495 } 496 497 protected void setPluginLibraries(WorkspacePluginModelBase model) 498 throws CoreException { 499 String libraryName = fData.getLibraryName(); 500 if (libraryName == null && !fData.hasBundleStructure()) { 501 libraryName = "."; } 503 if (libraryName != null) { 504 IPluginLibrary library = model.getPluginFactory().createLibrary(); 505 library.setName(libraryName); 506 library.setExported(!fData.hasBundleStructure()); 507 fModel.getPluginBase().add(library); 508 } 509 } 510 511 protected String getCommaValueFromSet(Set values) { 512 StringBuffer buffer = new StringBuffer (); 513 Iterator iter = values.iterator(); 514 while (iter.hasNext()) { 515 if (buffer.length() > 0) { 516 buffer.append(",\n "); } 518 buffer.append(iter.next().toString()); 519 } 520 return buffer.toString(); 521 } 522 523 private void addAllSourcePackages(IProject project, Set list) { 524 try { 525 IJavaProject javaProject = JavaCore.create(project); 526 IClasspathEntry[] classpath = javaProject.getRawClasspath(); 527 for (int i = 0; i < classpath.length; i++) { 528 IClasspathEntry entry = classpath[i]; 529 if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) { 530 IPath path = entry.getPath().removeFirstSegments(1); 531 if (path.segmentCount() > 0) { 532 IPackageFragmentRoot root = javaProject.getPackageFragmentRoot(project.getFolder(path)); 533 IJavaElement[] children = root.getChildren(); 534 for (int j = 0; j < children.length; j++) { 535 IPackageFragment frag = (IPackageFragment)children[j]; 536 if (frag.getChildren().length > 0 || frag.getNonJavaResources().length > 0) 537 list.add(children[j].getElementName()); 538 } 539 } 540 } 541 } 542 } catch (JavaModelException e) { 543 } 544 } 545 546 } 547 | Popular Tags |