package com.googlecode.jslint4java.eclipse.builder;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

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;

import com.googlecode.jslint4java.eclipse.JSLintLog;

public class ToggleNatureAction implements IObjectActionDelegate {

    private ISelection selection;

    /*
     * (non-Javadoc)
     *
     * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
     */
    public void run(IAction action) {
        if (selection instanceof IStructuredSelection) {
            for (Iterator<?> it = ((IStructuredSelection) selection).iterator(); it
                    .hasNext();) {
                Object element = it.next();
                IProject project = null;
                if (element instanceof IProject) {
                    project = (IProject) element;
                } else if (element instanceof IAdaptable) {
                    project = (IProject) ((IAdaptable) element)
                            .getAdapter(IProject.class);
                }
                if (project != null) {
                    toggleNature(project);
                }
            }
        }
    }

    /*
     * (non-Javadoc)
     *
     * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction,
     *      org.eclipse.jface.viewers.ISelection)
     */
    public void selectionChanged(IAction action, ISelection selection) {
        this.selection = selection;
    }

    /*
     * (non-Javadoc)
     *
     * @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction,
     *      org.eclipse.ui.IWorkbenchPart)
     */
    public void setActivePart(IAction action, IWorkbenchPart targetPart) {
    }

    /**
     * Toggles sample nature on a project
     *
     * @param project
     *                to have sample nature added or removed
     */
    private void toggleNature(IProject project) {
        try {
            IProjectDescription description = project.getDescription();
            List<String> natures = new ArrayList<String>(Arrays.asList(description.getNatureIds()));
            if (natures.contains(JSLintNature.NATURE_ID)) {
                // Remove the nature.
                natures.remove(JSLintNature.NATURE_ID);
            } else {
                // Add the nature.
                natures.add(JSLintNature.NATURE_ID);
            }
            description.setNatureIds(natures.toArray(new String[natures.size()]));
            project.setDescription(description, null);
        } catch (CoreException e) {
            JSLintLog.logError(e);
        }
    }

}

@@ -68,17 +68,20 @@
 il.Array
-s
+List
 ;%0Aimport
@@ -91,24 +91,22 @@
 va.util.
-Iterator
+Arrays
 ;%0Aimport
@@ -674,16 +674,26 @@
 ivate IS
+tructuredS
 election
@@ -879,217 +879,181 @@
     
-if (selection instanceof IStructuredSelection) %7B%0A            for (Iterator%3C?%3E it = ((IStructuredSelection) selection).iterator(); it%0A                    .hasNext();) %7B%0A                Object element = it.next(
+for (Object obj : selection.toArray()) %7B%0A            IProject project = projectFromSelectedItem(obj);%0A            if (project != null) %7B%0A                toggleNature(project
 );%0A 
@@ -1067,45 +1067,135 @@
     
+%7D%0A
     
-IProject project =
+    %7D%0A    %7D%0A%0A    /** Convert to a project, or return
  null
-;
+. */
 %0A    
-    
+private IProject projectFromSelectedItem(Object obj) %7B%0A
     
@@ -1198,31 +1198,27 @@
         if (
-element
+obj
  instanceof 
@@ -1245,54 +1245,31 @@
     
-    
-    project = (IProject) element;%0A        
+return (IProject) obj;%0A
     
@@ -1283,23 +1283,19 @@
 lse if (
-element
+obj
  instanc
@@ -1328,25 +1328,14 @@
     
-       
- project =
+return
  (IP
@@ -1360,128 +1360,55 @@
 le) 
-element)%0A                            .getAdapter(IProject.class);%0A                %7D%0A                if (project != null)
+obj).getAdapter(IProject.class);%0A        %7D else
  %7B%0A 
@@ -1422,70 +1422,20 @@
     
- 
-       toggleNature(project);%0A                %7D%0A            %7D
+return null;
 %0A   
@@ -1720,35 +1720,129 @@
     
-this.selection = selection;
+if (selection instanceof IStructuredSelection) %7B%0A            this.selection = (IStructuredSelection) selection;%0A        %7D
 %0A   

Tidy up run().