3941b1d2c850a016271ee5fe48928abb508cd17d
[Mograsim.git] / plugins / net.mograsim.plugin.core / src / net / mograsim / plugin / nature / AddMograsimNatureHandler.java
1 package net.mograsim.plugin.nature;
2
3 import java.util.Iterator;
4
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;
19
20 public class AddMograsimNatureHandler extends AbstractHandler
21 {
22         @Override
23         public Object execute(ExecutionEvent event) throws ExecutionException
24         {
25                 ISelection selection = HandlerUtil.getCurrentSelection(event);
26                 //
27
28                 MultiStatus ms = new MultiStatus("net.mograsim.plugin.core", 42, "MograsimNature Conversion", null);
29
30                 if (selection instanceof IStructuredSelection)
31                 {
32                         for (Iterator<?> it = ((IStructuredSelection) selection).iterator(); it.hasNext();)
33                         {
34                                 Object element = it.next();
35                                 IProject project = null;
36                                 if (element instanceof IProject)
37                                 {
38                                         project = (IProject) element;
39                                 } else if (element instanceof IAdaptable)
40                                 {
41                                         project = ((IAdaptable) element).getAdapter(IProject.class);
42                                 }
43                                 if (project != null)
44                                 {
45                                         try
46                                         {
47                                                 ms.add(toggleNature(project));
48                                         }
49                                         catch (CoreException e)
50                                         {
51                                                 // TODO log something
52                                                 throw new ExecutionException("Failed to toggle nature", e);
53                                         }
54                                 }
55                         }
56                 }
57
58                 return ms.getSeverity();
59         }
60
61         /**
62          * Adds Mograsim nature on a project
63          *
64          * @param project to have Mograsim nature
65          * @return
66          */
67         private IStatus toggleNature(IProject project) throws CoreException
68         {
69                 IProjectDescription description = project.getDescription();
70                 String[] natures = description.getNatureIds();
71
72                 // Add the nature
73                 String[] newNatures = new String[natures.length + 1];
74                 System.arraycopy(natures, 0, newNatures, 0, natures.length);
75                 newNatures[natures.length] = MograsimNature.NATURE_ID;
76
77                 IWorkspace workspace = ResourcesPlugin.getWorkspace();
78                 IStatus status = workspace.validateNatureSet(newNatures);
79
80                 // only apply new nature, if the status is ok
81                 if (status.getCode() == IStatus.OK)
82                 {
83                         description.setNatureIds(newNatures);
84                         project.setDescription(description, null);
85                 }
86
87                 return status;
88         }
89 }