/** {@inheritDoc} */
  public Object execute(CommandLine commandLine) throws Exception {
    String name = commandLine.getValue(Options.PROJECT_OPTION);
    IProject project = ProjectUtils.getProject(name);
    String[] aliases = StringUtils.split(commandLine.getValue(Options.NATURE_OPTION), ',');

    IProjectDescription desc = project.getDescription();
    String[] natureIds = desc.getNatureIds();
    ArrayList<String> modified = new ArrayList<String>();
    ArrayList<String> newNatures = new ArrayList<String>();
    CollectionUtils.addAll(modified, natureIds);
    for (String alias : aliases) {
      String natureId = ProjectNatureFactory.getNatureForAlias(alias);
      if (natureId != null && !modified.contains(natureId)) {
        modified.add(natureId);
        newNatures.add(natureId);
      }
    }

    desc.setNatureIds((String[]) modified.toArray(new String[modified.size()]));
    project.setDescription(desc, new NullProgressMonitor());

    for (String nature : newNatures) {
      ProjectManager manager = ProjectManagement.getProjectManager(nature);
      if (manager != null) {
        manager.create(project, commandLine);
      }
    }

    return Services.getMessage("project.nature.added");
  }
Example #2
0
  /**
   * Gets a java project from the supplied IProject.
   *
   * @param project The IProject.
   * @return The java project.
   */
  public static IJavaProject getJavaProject(IProject project) throws Exception {
    if (ProjectUtils.getPath(project) == null) {
      throw new IllegalArgumentException(
          Services.getMessage("project.location.null", project.getName()));
    }

    if (!project.hasNature(PluginResources.NATURE)) {
      throw new IllegalArgumentException(
          Services.getMessage(
              "project.missing.nature",
              project.getName(),
              ProjectNatureFactory.getAliasForNature(PluginResources.NATURE)));
    }

    IJavaProject javaProject = JavaCore.create(project);
    if (javaProject == null || !javaProject.exists()) {
      throw new IllegalArgumentException(Services.getMessage("project.not.found", project));
    }

    return javaProject;
  }