581b4a6f96dbd23aef2e73205e961edbb069ced5
[Mograsim.git] / net.mograsim.plugin.core / src / net / mograsim / plugin / nature / AddRemoveMograsimNatureHandler.java
1 package net.mograsim.plugin.nature;
2
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;
12
13 public class AddRemoveMograsimNatureHandler extends AbstractHandler
14 {
15
16         public Object execute(ExecutionEvent event) throws ExecutionException
17         {
18                 ISelection selection = HandlerUtil.getCurrentSelection(event);
19                 //
20                 if (selection instanceof IStructuredSelection)
21                 {
22                         for (Iterator<?> it = ((IStructuredSelection) selection).iterator(); it.hasNext();)
23                         {
24                                 Object element = it.next();
25                                 IProject project = null;
26                                 if (element instanceof IProject)
27                                 {
28                                         project = (IProject) element;
29                                 } else if (element instanceof IAdaptable)
30                                 {
31                                         project = ((IAdaptable) element).getAdapter(IProject.class);
32                                 }
33                                 if (project != null)
34                                 {
35                                         try
36                                         {
37                                                 toggleNature(project);
38                                         }
39                                         catch (CoreException e)
40                                         {
41                                                 // TODO log something
42                                                 throw new ExecutionException("Failed to toggle nature", e);
43                                         }
44                                 }
45                         }
46                 }
47
48                 return null;
49         }
50
51         /**
52          * Toggles sample nature on a project
53          *
54          * @param project to have sample nature added or removed
55          */
56         private void toggleNature(IProject project) throws CoreException
57         {
58                 IProjectDescription description = project.getDescription();
59                 String[] natures = description.getNatureIds();
60
61                 for (int i = 0; i < natures.length; ++i)
62                 {
63                         if (MograsimNature.NATURE_ID.equals(natures[i]))
64                         {
65                                 // Remove the nature
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);
71                                 return;
72                         }
73                 }
74
75                 // Add the nature
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);
81         }
82 }