如何在eclipse rcp中删除项目性质

时间:2016-12-07 06:33:55

标签: eclipse eclipse-plugin eclipse-rcp

我想删除在使用扩展点(org.eclipse.core.resources.nature)之前添加的项目性质。

请帮我提供示例代码。

1 个答案:

答案 0 :(得分:0)

您可以使用以下内容从项目中删除自然ID:

void removeNatureId(IProject project, String removeId) throws CoreException
{
  IProjectDescription description = project.getDescription();

  String [] natureIds = description.getNatureIds();

  for (int i = 0; i < natureIds.length; ++i) {
    if (natureIds[i].equals(removeId)) {
      String [] newIds = new String [natureIds.length - 1];

      System.arraycopy(natureIds, 0, newIds, 0, i);
      System.arraycopy(natureIds, i + 1, newIds, i, natureIds.length - i - 1);

      description.setNatureIds(newIds);

      project.setDescription(description, null);

      break;
    }
  }
}

如果您需要延迟运行,可以使用WorkspaceJob

class RemoveJob extends WorkspaceJob
{
  RemoveJob()
  {
    super("Remove Job");

    setSystem(true);

    // Use scheduling rule to make sure the job doesn't run until workspace is unlocked
    setRule(ResourcesPlugin.getWorkspace().getRoot());
  }

  @Override
  public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException
  {
    // TODO do the remove

    return Status.OK_STATUS;
  }
}

创建并安排:

Job job = new RemoveJob();

job.schedule();