001    /*
002      GRANITE DATA SERVICES
003      Copyright (C) 2007-2010 ADEQUATE SYSTEMS SARL
004    
005      This file is part of Granite Data Services.
006    
007      Granite Data Services is free software; you can redistribute it and/or modify
008      it under the terms of the GNU Library General Public License as published by
009      the Free Software Foundation; either version 2 of the License, or (at your
010      option) any later version.
011    
012      Granite Data Services is distributed in the hope that it will be useful, but
013      WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014      FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015      for more details.
016    
017      You should have received a copy of the GNU Library General Public License
018      along with this library; if not, see <http://www.gnu.org/licenses/>.
019    */
020    
021    package org.granite.builder;
022    
023    import java.util.Iterator;
024    
025    import org.eclipse.core.resources.IProject;
026    import org.eclipse.core.resources.IProjectDescription;
027    import org.eclipse.core.runtime.CoreException;
028    import org.eclipse.core.runtime.IAdaptable;
029    import org.eclipse.jface.action.IAction;
030    import org.eclipse.jface.viewers.ISelection;
031    import org.eclipse.jface.viewers.IStructuredSelection;
032    import org.eclipse.ui.IObjectActionDelegate;
033    import org.eclipse.ui.IWorkbenchPart;
034    
035    /**
036     * @author Franck WOLFF
037     */
038    public class ToggleNatureAction implements IObjectActionDelegate {
039    
040        private ISelection selection;
041    
042        public void run(IAction action) {
043            if (selection instanceof IStructuredSelection) {
044                for (Iterator<?> it = ((IStructuredSelection)selection).iterator(); it.hasNext(); ) {
045                    Object element = it.next();
046                    
047                    IProject project = null;
048                    if (element instanceof IProject)
049                        project = (IProject)element;
050                    else if (element instanceof IAdaptable)
051                        project = (IProject)((IAdaptable)element).getAdapter(IProject.class);
052                    
053                    if (project != null)
054                        toggleNature(project);
055                }
056            }
057        }
058    
059        public void selectionChanged(IAction action, ISelection selection) {
060            this.selection = selection;
061        }
062    
063        public void setActivePart(IAction action, IWorkbenchPart targetPart) {
064        }
065    
066        public static void toggleNature(IProject project) {
067            try {
068                IProjectDescription description = project.getDescription();
069                String[] natures = description.getNatureIds();
070    
071                for (int i = 0; i < natures.length; ++i) {
072                    if (GraniteNature.NATURE_ID.equals(natures[i])) {
073                        // Remove granite nature
074                        String[] newNatures = new String[natures.length - 1];
075                        System.arraycopy(natures, 0, newNatures, 0, i);
076                        System.arraycopy(natures, i + 1, newNatures, i, natures.length - i - 1);
077                        description.setNatureIds(newNatures);
078                        project.setDescription(description, null);
079                        return;
080                    }
081                }
082    
083                // Add granite nature
084                String[] newNatures = new String[natures.length + 1];
085                System.arraycopy(natures, 0, newNatures, 0, natures.length);
086                newNatures[natures.length] = GraniteNature.NATURE_ID;
087                description.setNatureIds(newNatures);
088                project.setDescription(description, null);
089            } catch (CoreException e) {
090            }
091        }
092    }