繁体   English   中英

以编程方式为现有项目增加性质

[英]Programmaitcally adding nature to existing project

当我调整正在运行的项目的项目性质时

WorkspaceJob job = new WorkspaceJob("AddingNature") {

        @Override
        public IStatus runInWorkspace(IProgressMonitor monitor)
                throws CoreException {
try {

       IProjectDescription description = activeProject
                        .getDescription();
       String[] prevNatures = description.getNatureIds();
       String[] newNatures = new String[prevNatures.length + 1];
       System.arraycopy(prevNatures, 0, newNatures, 0,
            prevNatures.length);
       newNatures[prevNatures.length] = ID;

       description.setNatureIds(newNatures);
       activeProject.setDescription(description,
          new NullProgressMonitor());
       return Status.OK_STATUS;
   } catch (CoreException e) {
       LOGGER.log(Level.SEVERE, WARNING_NATURE_FAIL, e.getMessage());          
       return Status.CANCEL_STATUS;
    }  
}
job.schedule()

我收到一个错误“资源树已被锁定以进行修改”。 异常堆栈跟踪不可用。

我以为应该避免资源树被锁定。 我可以做些什么来防止这种情况?还有其他方法可以添加自然/转换项目

使用扩展AbstractHandler的类从菜单项中调用此代码。

数组String[] newNatures大小存在问题。

尝试这个:

public static void addProjectNature(IProject project, String natureID, boolean addFirst) {
    try {
        IProjectDescription description = project.getDescription();
        if (description.hasNature(natureID)) {
            return;
        }
        String[] natures = description.getNatureIds();
        String[] newNatures = new String[natures.length + 1];

        if (addFirst) {
            System.arraycopy(natures, 0, newNatures, 1, natures.length);
            newNatures[0] = natureID;
        } else {
            System.arraycopy(natures, 0, newNatures, 0, natures.length);
            newNatures[natures.length] = natureID;
        }
        IStatus status = project.getWorkspace().validateNatureSet(newNatures);
        if (status.getCode() == IStatus.OK) {
            description.setNatureIds(newNatures);
            project.setDescription(description, null);
        } else {
            //Error message
        }
    } catch (CoreException e) {
         e.printStackTrace();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM