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