Toggles sample nature on a project
new Job("Convert Project") {
@Override
protected IStatus run(IProgressMonitor monitor) {
try {
if (monitor != null) {
monitor.beginTask(String.format(
"Convert %1$s to Android", project.getName()), 5);
}
IProjectDescription description = project.getDescription();
String[] natures = description.getNatureIds();
// check if the project already has the android nature.
for (int i = 0; i < natures.length; ++i) {
if (AndroidConstants.NATURE.equals(natures[i])) {
// we shouldn't be here as the visibility of the item
// is dependent on the project.
return new Status(Status.WARNING, AdtPlugin.PLUGIN_ID,
"Project is already an Android project");
}
}
if (monitor != null) {
monitor.worked(1);
}
String[] newNatures = new String[natures.length + 1];
System.arraycopy(natures, 0, newNatures, 1, natures.length);
newNatures[0] = AndroidConstants.NATURE;
// set the new nature list in the project
description.setNatureIds(newNatures);
project.setDescription(description, null);
if (monitor != null) {
monitor.worked(1);
}
// Fix the classpath entries.
// get a java project
IJavaProject javaProject = JavaCore.create(project);
ProjectHelper.fixProjectClasspathEntries(javaProject);
if (monitor != null) {
monitor.worked(1);
}
return Status.OK_STATUS;
} catch (JavaModelException e) {
return e.getJavaModelStatus();
} catch (CoreException e) {
return e.getStatus();
} finally {
if (monitor != null) {
monitor.done();
}
}
}
}.schedule();