001/*
002  GRANITE DATA SERVICES
003  Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
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
021package org.granite.builder;
022
023import java.util.Iterator;
024
025import org.eclipse.core.resources.IProject;
026import org.eclipse.core.resources.IProjectDescription;
027import org.eclipse.core.runtime.CoreException;
028import org.eclipse.core.runtime.IAdaptable;
029import org.eclipse.jface.action.IAction;
030import org.eclipse.jface.viewers.ISelection;
031import org.eclipse.jface.viewers.IStructuredSelection;
032import org.eclipse.ui.IObjectActionDelegate;
033import org.eclipse.ui.IWorkbenchPart;
034
035/**
036 * @author Franck WOLFF
037 */
038public class ToggleNatureAction implements IObjectActionDelegate {
039
040    private ISelection selection;
041
042    @Override
043        public void run(IAction action) {
044        if (selection instanceof IStructuredSelection) {
045            for (Iterator<?> it = ((IStructuredSelection)selection).iterator(); it.hasNext(); ) {
046                Object element = it.next();
047                
048                IProject project = null;
049                if (element instanceof IProject)
050                    project = (IProject)element;
051                else if (element instanceof IAdaptable)
052                    project = (IProject)((IAdaptable)element).getAdapter(IProject.class);
053                
054                if (project != null)
055                    toggleNature(project);
056            }
057        }
058    }
059
060    @Override
061        public void selectionChanged(IAction action, ISelection selection) {
062        this.selection = selection;
063    }
064
065    @Override
066        public void setActivePart(IAction action, IWorkbenchPart targetPart) {
067    }
068
069    public static void toggleNature(IProject project) {
070        try {
071            IProjectDescription description = project.getDescription();
072            String[] natures = description.getNatureIds();
073
074            for (int i = 0; i < natures.length; ++i) {
075                if (GraniteNature.NATURE_ID.equals(natures[i])) {
076                    // Remove granite nature
077                    String[] newNatures = new String[natures.length - 1];
078                    System.arraycopy(natures, 0, newNatures, 0, i);
079                    System.arraycopy(natures, i + 1, newNatures, i, natures.length - i - 1);
080                    description.setNatureIds(newNatures);
081                    project.setDescription(description, null);
082                    return;
083                }
084            }
085
086            // Add granite nature
087            String[] newNatures = new String[natures.length + 1];
088            System.arraycopy(natures, 0, newNatures, 0, natures.length);
089            newNatures[natures.length] = GraniteNature.NATURE_ID;
090            description.setNatureIds(newNatures);
091            project.setDescription(description, null);
092        } catch (CoreException e) {
093        }
094    }
095}