/*******************************************************************************
 * Copyright (c) 2005, 2018 Red Hat, Inc.
 * 
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Red Hat - initial API and implementation
 *******************************************************************************/
package org.eclipse.linuxtools.rpm.core;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IProjectNature;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;

/**
 * The RPM project nature.
 *
 */
public class RPMProjectNature implements IProjectNature {

    private IProject project;

    /**
     * Adds the RPM project nature to a given workspace project.
     * @param project the project
     * @param mon a progress monitor, or <code>null</code> if progress monitoring
     * is not desired
     * @throws CoreException if adding the RPM project nature fails
     */
    public static void addRPMNature(IProject project, IProgressMonitor mon) throws CoreException {
        addNature(project, IRPMConstants.RPM_NATURE_ID, mon);
    }

    /**
     * Utility method for adding a nature to a project.
     *
     * @param proj
     *            the project to add the nature
     * @param natureId
     *            the id of the nature to assign to the project
     * @param monitor
     *            a progress monitor to indicate the duration of the operation,
     *            or <code>null</code> if progress reporting is not required.
     *
     */
    private static void addNature(IProject project, String natureId, IProgressMonitor monitor) throws CoreException {
        if(project.hasNature(natureId)) {
            return;
        }
        IProjectDescription description = project.getDescription();
        String[] prevNatures = description.getNatureIds();
        String[] newNatures = new String[prevNatures.length + 1];
        System.arraycopy(prevNatures, 0, newNatures, 0, prevNatures.length);
        newNatures[prevNatures.length] = natureId;
        description.setNatureIds(newNatures);
        project.setDescription(description, monitor);
    }

    @Override
    public void configure() {
    }

    @Override
    public void deconfigure() {
    }

    @Override
    public IProject getProject() {
        return project;
    }

    @Override
    public void setProject(IProject project) {
        this.project = project;
    }

}
