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);
27 MultiStatus ms = new MultiStatus("net.mograsim.plugin.core", 42, "MograsimNature Conversion", null);
29 if (selection instanceof IStructuredSelection)
31 for (Iterator<?> it = ((IStructuredSelection) selection).iterator(); it.hasNext();)
33 Object element = it.next();
34 IProject project = null;
35 if (element instanceof IProject)
37 project = (IProject) element;
38 } else if (element instanceof IAdaptable)
40 project = ((IAdaptable) element).getAdapter(IProject.class);
46 ms.add(addNature(project));
48 catch (CoreException e)
51 throw new ExecutionException("Failed to toggle nature", e);
57 return ms.getSeverity();
61 * Adds Mograsim nature on a project
63 * @param project to have Mograsim nature
66 public static IStatus addNature(IProject project) throws CoreException
68 IProjectDescription description = project.getDescription();
69 String[] natures = description.getNatureIds();
72 String[] newNatures = new String[natures.length + 1];
73 System.arraycopy(natures, 0, newNatures, 0, natures.length);
74 newNatures[natures.length] = MograsimNature.NATURE_ID;
76 IWorkspace workspace = ResourcesPlugin.getWorkspace();
77 IStatus status = workspace.validateNatureSet(newNatures);
79 // only apply new nature, if the status is ok
80 if (status.getCode() == IStatus.OK)
82 description.setNatureIds(newNatures);
83 project.setDescription(description, null);