1 package net.mograsim.plugin.nature;
3 import java.util.Iterator;
5 import org.eclipse.core.commands.AbstractHandler;
6 import org.eclipse.core.commands.ExecutionEvent;
7 import org.eclipse.core.commands.ExecutionException;
8 import org.eclipse.core.resources.IProject;
9 import org.eclipse.core.resources.IProjectDescription;
10 import org.eclipse.core.resources.IWorkspace;
11 import org.eclipse.core.resources.ResourcesPlugin;
12 import org.eclipse.core.runtime.CoreException;
13 import org.eclipse.core.runtime.IAdaptable;
14 import org.eclipse.core.runtime.IStatus;
15 import org.eclipse.core.runtime.MultiStatus;
16 import org.eclipse.jface.viewers.ISelection;
17 import org.eclipse.jface.viewers.IStructuredSelection;
18 import org.eclipse.ui.handlers.HandlerUtil;
20 public class AddMograsimNatureHandler extends AbstractHandler
23 public Object execute(ExecutionEvent event) throws ExecutionException
25 ISelection selection = HandlerUtil.getCurrentSelection(event);
28 MultiStatus ms = new MultiStatus("net.mograsim.plugin.core", 42, "MograsimNature Conversion", null);
30 if (selection instanceof IStructuredSelection)
32 for (Iterator<?> it = ((IStructuredSelection) selection).iterator(); it.hasNext();)
34 Object element = it.next();
35 IProject project = null;
36 if (element instanceof IProject)
38 project = (IProject) element;
39 } else if (element instanceof IAdaptable)
41 project = ((IAdaptable) element).getAdapter(IProject.class);
47 ms.add(toggleNature(project));
49 catch (CoreException e)
52 throw new ExecutionException("Failed to toggle nature", e);
58 return ms.getSeverity();
62 * Adds Mograsim nature on a project
64 * @param project to have Mograsim nature
67 private IStatus toggleNature(IProject project) throws CoreException
69 IProjectDescription description = project.getDescription();
70 String[] natures = description.getNatureIds();
73 String[] newNatures = new String[natures.length + 1];
74 System.arraycopy(natures, 0, newNatures, 0, natures.length);
75 newNatures[natures.length] = MograsimNature.NATURE_ID;
77 IWorkspace workspace = ResourcesPlugin.getWorkspace();
78 IStatus status = workspace.validateNatureSet(newNatures);
80 // only apply new nature, if the status is ok
81 if (status.getCode() == IStatus.OK)
83 description.setNatureIds(newNatures);
84 project.setDescription(description, null);