Commits

Anonymous committed 67a0088

- Added ToggleNature action.
- Renamed LispProjectNature to LispNature

Comments (0)

Files changed (7)

          point="org.eclipse.core.resources.natures">
       <runtime>
          <run
-               class="org.lispdev.main.LispProjectNature">
+               class="org.lispdev.main.LispNature">
          </run>
       </runtime>
    </extension>
    <extension
+         point="org.eclipse.ui.ide.projectNatureImages">
+      <image
+            icon="icons/lisp-nature.gif"
+            id="org.lispdev.main.LispProjectNatureImage"
+            natureId="org.lispdev.main.LispProjectNature">
+      </image>
+   </extension>
+   <extension
          point="org.eclipse.ui.newWizards">
       <category
             id="org.lispdev.wizards"
                value="org.lispdev.lispNature">
          </filter>
       </objectContribution>
+      <objectContribution
+            id="org.lispdev.main.ToggleNatureAction.folder"
+            objectClass="org.eclipse.core.resources.IProject">
+         <action
+               class="org.lispdev.main.ToggleNatureAction"
+               enablesFor="+"
+               id="org.lispdev.main.ToggleNatureAction"
+               label="Add/Remove Lisp Nature"
+               menubarPath="group.add">
+         </action>
+      </objectContribution>
    </extension>
    <extension
          point="org.eclipse.ui.decorators">

src/org/lispdev/main/LispNature.java

+package org.lispdev.main;
+
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IProjectNature;
+import org.eclipse.core.runtime.CoreException;
+
+public class LispNature implements IProjectNature
+{
+
+  public static final String ID = "org.lispdev.lispNature";
+  private IProject project;
+
+  @Override
+  public void configure() throws CoreException
+  {
+    // TODO Auto-generated method stub
+
+  }
+
+  @Override
+  public void deconfigure() throws CoreException
+  {
+    // TODO Auto-generated method stub
+
+  }
+
+  @Override
+  public IProject getProject()
+  {
+    return project;
+  }
+
+  @Override
+  public void setProject(IProject project)
+  {
+    this.project = project;
+  }
+
+}

src/org/lispdev/main/LispProjectDecorator.java

     IProject p = (IProject)element;
     try
     {
-      if( p.getNature(LispProjectNature.ID) != null )
+      if( p.getNature(LispNature.ID) != null )
       {
         LispProcess pr = LispPlugin.get().getActiveProcess(p);
         decoration.addSuffix(" ["+(pr == null ? "" : pr.getLabel())+"]");

src/org/lispdev/main/LispProjectNature.java

-package org.lispdev.main;
-
-import org.eclipse.core.resources.IProject;
-import org.eclipse.core.resources.IProjectNature;
-import org.eclipse.core.runtime.CoreException;
-
-public class LispProjectNature implements IProjectNature
-{
-
-  public static final String ID = "org.lispdev.lispNature";
-  private IProject project;
-
-  @Override
-  public void configure() throws CoreException
-  {
-    // TODO Auto-generated method stub
-
-  }
-
-  @Override
-  public void deconfigure() throws CoreException
-  {
-    // TODO Auto-generated method stub
-
-  }
-
-  @Override
-  public IProject getProject()
-  {
-    return project;
-  }
-
-  @Override
-  public void setProject(IProject project)
-  {
-    this.project = project;
-  }
-
-}

src/org/lispdev/main/LoadProjectAction.java

       try
       {
         if(obj instanceof IProject
-            && ((IProject)obj).getNature(LispProjectNature.ID) != null )
+            && ((IProject)obj).getNature(LispNature.ID) != null )
         {
           project = (IProject)obj;
           file = null;

src/org/lispdev/main/NewProjectWizard.java

         String[] natures = pdesc.getNatureIds();
         String[] newNatures = new String[natures.length + 1];
         System.arraycopy(natures, 0, newNatures, 0, natures.length);
-        newNatures[natures.length] = LispProjectNature.ID;
+        newNatures[natures.length] = LispNature.ID;
         pdesc.setNatureIds(newNatures);
 
         newProject.setDescription(pdesc, IResource.FORCE, monitor);

src/org/lispdev/main/ToggleNatureAction.java

+package org.lispdev.main;
+
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IProjectDescription;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IAdaptable;
+import org.eclipse.jface.action.IAction;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.ui.IObjectActionDelegate;
+import org.eclipse.ui.IWorkbenchPart;
+
+public class ToggleNatureAction implements IObjectActionDelegate
+{
+  private IStructuredSelection selection;
+
+  @Override
+  public void run(IAction action)
+  {
+    if( selection != null )
+    {
+      for(Object obj : selection.toList())
+      {
+        IProject project = null;
+        if( obj instanceof IProject )
+        {
+          project = (IProject)obj;
+        }
+        else if( obj instanceof IAdaptable )
+        {
+          project = (IProject)((IAdaptable)obj).getAdapter(IProject.class);
+        }
+        if( project != null )
+        {
+          toggleNature(project);
+        }
+      }
+    }
+  }
+
+  private void toggleNature(IProject project) {
+    try
+    {
+      IProjectDescription description = project.getDescription();
+      String[] natures = description.getNatureIds();
+
+      for(int i = 0; i < natures.length; ++i)
+      {
+        if( LispNature.ID.equals(natures[i]) )
+        {
+          // Remove the nature
+          String[] newNatures = new String[natures.length - 1];
+          System.arraycopy(natures, 0, newNatures, 0, i);
+          System.arraycopy(natures, i + 1, newNatures, i, natures.length - i
+              - 1);
+          description.setNatureIds(newNatures);
+          project.setDescription(description, null);
+          return;
+        }
+      }
+
+      // Add the nature
+      String[] newNatures = new String[natures.length + 1];
+      System.arraycopy(natures, 0, newNatures, 0, natures.length);
+      newNatures[natures.length] = LispNature.ID;
+      description.setNatureIds(newNatures);
+      project.setDescription(description, null);
+    }
+    catch(CoreException e)
+    {}
+  }
+
+  @Override
+  public void selectionChanged(IAction action, ISelection selection)
+  {
+    if( selection instanceof IStructuredSelection )
+    {
+      this.selection = (IStructuredSelection)selection;
+    }
+    else
+    {
+      selection = null;
+    }
+  }
+
+  @Override
+  public void setActivePart(IAction action, IWorkbenchPart targetPart)
+  {}
+
+}