1 package net.mograsim.plugin.nature;
3 import java.util.Iterator;
4 import org.eclipse.core.commands.*;
5 import org.eclipse.core.resources.IProject;
6 import org.eclipse.core.resources.IProjectDescription;
7 import org.eclipse.core.runtime.CoreException;
8 import org.eclipse.core.runtime.IAdaptable;
9 import org.eclipse.jface.viewers.ISelection;
10 import org.eclipse.jface.viewers.IStructuredSelection;
11 import org.eclipse.ui.handlers.HandlerUtil;
13 public class AddRemoveMograsimNatureHandler extends AbstractHandler
16 public Object execute(ExecutionEvent event) throws ExecutionException
18 ISelection selection = HandlerUtil.getCurrentSelection(event);
20 if (selection instanceof IStructuredSelection)
22 for (Iterator<?> it = ((IStructuredSelection) selection).iterator(); it.hasNext();)
24 Object element = it.next();
25 IProject project = null;
26 if (element instanceof IProject)
28 project = (IProject) element;
29 } else if (element instanceof IAdaptable)
31 project = ((IAdaptable) element).getAdapter(IProject.class);
37 toggleNature(project);
39 catch (CoreException e)
42 throw new ExecutionException("Failed to toggle nature", e);
52 * Toggles sample nature on a project
54 * @param project to have sample nature added or removed
56 private void toggleNature(IProject project) throws CoreException
58 IProjectDescription description = project.getDescription();
59 String[] natures = description.getNatureIds();
61 for (int i = 0; i < natures.length; ++i)
63 if (MograsimNature.NATURE_ID.equals(natures[i]))
66 String[] newNatures = new String[natures.length - 1];
67 System.arraycopy(natures, 0, newNatures, 0, i);
68 System.arraycopy(natures, i + 1, newNatures, i, natures.length - i - 1);
69 description.setNatureIds(newNatures);
70 project.setDescription(description, null);
76 String[] newNatures = new String[natures.length + 1];
77 System.arraycopy(natures, 0, newNatures, 0, natures.length);
78 newNatures[natures.length] = MograsimNature.NATURE_ID;
79 description.setNatureIds(newNatures);
80 project.setDescription(description, null);