package thahn.java.agui.ide.eclipse.project; import org.eclipse.core.resources.ICommand; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IProjectDescription; import org.eclipse.core.resources.IProjectNature; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.SubProgressMonitor; import thahn.java.agui.ide.eclipse.build.builder.ResourceBuilder; public class AguiNature implements IProjectNature { /** * ID of this project nature */ public static final String NATURE_ID = "thahn.java.agui.ide.eclipse.wizard.AguiNature"; private IProject project; /* * (non-Javadoc) * * @see org.eclipse.core.resources.IProjectNature#configure() */ public void configure() throws CoreException { IProjectDescription desc = project.getDescription(); ICommand[] commands = desc.getBuildSpec(); for (int i = 0; i < commands.length; ++i) { if (commands[i].getBuilderName().equals(ResourceBuilder.BUILDER_ID)) { return; } } ICommand[] newCommands = new ICommand[commands.length + 1]; System.arraycopy(commands, 0, newCommands, 0, commands.length); ICommand command = desc.newCommand(); command.setBuilderName(ResourceBuilder.BUILDER_ID); newCommands[newCommands.length - 1] = command; desc.setBuildSpec(newCommands); project.setDescription(desc, null); } public static void configureBuilder(IProject project) throws CoreException { IProjectDescription desc = project.getDescription(); ICommand[] commands = desc.getBuildSpec(); for (int i = 0; i < commands.length; ++i) { if (commands[i].getBuilderName().equals(ResourceBuilder.BUILDER_ID)) { return; } } ICommand[] newCommands = new ICommand[commands.length + 1]; System.arraycopy(commands, 0, newCommands, 0, commands.length); ICommand command = desc.newCommand(); command.setBuilderName(ResourceBuilder.BUILDER_ID); newCommands[newCommands.length - 1] = command; desc.setBuildSpec(newCommands); project.setDescription(desc, null); } public static void addNatureToProjectDescription(IProject project, String natureId, IProgressMonitor monitor) throws CoreException { if (!project.hasNature(natureId)) { IProjectDescription description = project.getDescription(); String[] natures = description.getNatureIds(); String[] newNatures = new String[natures.length + 1]; // Android natures always come first. if (natureId.equals(AguiNature.NATURE_ID)) { System.arraycopy(natures, 0, newNatures, 1, natures.length); newNatures[0] = natureId; } else { System.arraycopy(natures, 0, newNatures, 0, natures.length); newNatures[natures.length] = natureId; } description.setNatureIds(newNatures); project.setDescription(description, new SubProgressMonitor(monitor, 10)); // (FORCE, KEEP_HISTORY and AVOID_NATURE_CONFIG) } } /* * (non-Javadoc) * * @see org.eclipse.core.resources.IProjectNature#deconfigure() */ public void deconfigure() throws CoreException { IProjectDescription description = getProject().getDescription(); ICommand[] commands = description.getBuildSpec(); for (int i = 0; i < commands.length; ++i) { if (commands[i].getBuilderName().equals(ResourceBuilder.BUILDER_ID)) { ICommand[] newCommands = new ICommand[commands.length - 1]; System.arraycopy(commands, 0, newCommands, 0, i); System.arraycopy(commands, i + 1, newCommands, i, commands.length - i - 1); description.setBuildSpec(newCommands); project.setDescription(description, null); return; } } } /* * (non-Javadoc) * * @see org.eclipse.core.resources.IProjectNature#getProject() */ public IProject getProject() { return project; } /* * (non-Javadoc) * * @see org.eclipse.core.resources.IProjectNature#setProject(org.eclipse.core.resources.IProject) */ public void setProject(IProject project) { this.project = project; } public static boolean checkAguiProject(IProject project) { try { return project.hasNature(AguiNature.NATURE_ID); } catch (CoreException e) { e.printStackTrace(); } return false; } }