[英]Programmaitcally adding nature to existing project
When i adjust the project nature of a running 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()
I get an error " The resource tree is locked for modifications." 我收到一个错误“资源树已被锁定以进行修改”。 An exception stack trace is not available. 异常堆栈跟踪不可用。
I thought the schuduling should avoid the resource tree from being locked. 我以为应该避免资源树被锁定。 What can i do to prevent this?, is there an other way to add natures / convert projects 我可以做些什么来防止这种情况?还有其他方法可以添加自然/转换项目
This code is called from a menuitem using a class that extends AbstractHandler. 使用扩展AbstractHandler的类从菜单项中调用此代码。
Problem with size of array String[] newNatures
. 数组String[] newNatures
大小存在问题。
Try this: 尝试这个:
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.