sysperfana/perfinvestigator/com.nokia.carbide.cpp.pi.core/src/com/nokia/carbide/cpp/pi/core/PIProjectNature.java
changeset 2 b9ab3b238396
child 12 ae255c9aa552
equal deleted inserted replaced
1:1050670c6980 2:b9ab3b238396
       
     1 /*
       
     2  * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of the License "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Nokia Corporation - initial contribution.
       
    11  *
       
    12  * Contributors:
       
    13  *
       
    14  * Description: 
       
    15  *
       
    16  */
       
    17 
       
    18 package com.nokia.carbide.cpp.pi.core;
       
    19 
       
    20 import java.util.ArrayList;
       
    21 import java.util.Arrays;
       
    22 import java.util.List;
       
    23 
       
    24 import org.eclipse.core.resources.IProject;
       
    25 import org.eclipse.core.resources.IProjectDescription;
       
    26 import org.eclipse.core.resources.IProjectNature;
       
    27 import org.eclipse.core.runtime.CoreException;
       
    28 import org.eclipse.core.runtime.IProgressMonitor;
       
    29 
       
    30 public class PIProjectNature implements IProjectNature {
       
    31 
       
    32 	public static final String PI_NATURE_ID = PICorePlugin.PLUGIN_ID + ".pinature";  //$NON-NLS-1$
       
    33 	
       
    34 	private IProject fProject;
       
    35 	
       
    36 	public void configure() throws CoreException {
       
    37 	}
       
    38 
       
    39 	public void deconfigure() throws CoreException {
       
    40 	}
       
    41 
       
    42 	public PIProjectNature() {
       
    43 	}
       
    44 	
       
    45 	public PIProjectNature(IProject project) {
       
    46 		setProject(project);
       
    47 	}
       
    48 
       
    49 	public IProject getProject() {
       
    50 		return fProject;
       
    51 	}
       
    52 
       
    53 	public void setProject(IProject project) {
       
    54 		fProject = project;
       
    55 	}
       
    56 
       
    57 	public static void addPINature(IProject project, IProgressMonitor mon) throws CoreException {
       
    58 		addNature(project, PI_NATURE_ID, mon);
       
    59 	}
       
    60 
       
    61 	public static void removePINature(IProject project, IProgressMonitor mon) throws CoreException {
       
    62 		removeNature(project, PI_NATURE_ID, mon);
       
    63 	}
       
    64 
       
    65 	private static void addNature(IProject project, String natureId, IProgressMonitor monitor) throws CoreException {
       
    66 		IProjectDescription description = project.getDescription();
       
    67 		String[] prevNatures = description.getNatureIds();
       
    68 		for (String prevNature : prevNatures) {
       
    69 			if (natureId.equals(prevNature)) {
       
    70 				return;
       
    71 			}
       
    72 		}
       
    73 		String[] newNatures = new String[prevNatures.length + 1];
       
    74 		System.arraycopy(prevNatures, 0, newNatures, 0, prevNatures.length);
       
    75 		newNatures[prevNatures.length] = natureId;
       
    76 		description.setNatureIds(newNatures);
       
    77 		project.setDescription(description, monitor);
       
    78 	}
       
    79 
       
    80 	private static void removeNature(IProject project, String natureId, IProgressMonitor monitor) throws CoreException {
       
    81 		IProjectDescription description = project.getDescription();
       
    82 		String[] prevNatures = description.getNatureIds();
       
    83 		List<String> newNatures = new ArrayList<String>(Arrays.asList(prevNatures));
       
    84 		newNatures.remove(natureId);
       
    85 		description.setNatureIds((String[]) newNatures.toArray(new String[newNatures.size()]));
       
    86 		project.setDescription(description, monitor);
       
    87 	}
       
    88 
       
    89 }