1 package com.bull.eclipse.jonas.utils; 2 3 7 8 9 import org.eclipse.core.resources.IProject; 10 import org.eclipse.core.resources.IProjectDescription; 11 import org.eclipse.core.runtime.CoreException; 12 13 18 19 public class JDTUtil { 20 21 25 26 public static void addNatureToProject(IProject project, String natureId) throws CoreException { 27 IProject proj = project.getProject(); IProjectDescription description = proj.getDescription(); 29 String [] prevNatures= description.getNatureIds(); 30 31 int natureIndex = -1; 32 for (int i=0; i<prevNatures.length; i++) { 33 if(prevNatures[i].equals(natureId)) { 34 natureIndex = i; 35 i = prevNatures.length; 36 } 37 } 38 39 if(natureIndex == -1) { 41 String [] newNatures= new String [prevNatures.length + 1]; 42 System.arraycopy(prevNatures, 0, newNatures, 0, prevNatures.length); 43 newNatures[prevNatures.length]= natureId; 44 description.setNatureIds(newNatures); 45 proj.setDescription(description, null); 46 } 47 } 48 49 50 53 54 public static void removeNatureToProject(IProject project, String natureId) throws CoreException { 55 IProject proj = project.getProject(); IProjectDescription description = proj.getDescription(); 57 String [] prevNatures= description.getNatureIds(); 58 59 int natureIndex = -1; 60 for (int i=0; i<prevNatures.length; i++) { 61 if(prevNatures[i].equals(natureId)) { 62 natureIndex = i; 63 i = prevNatures.length; 64 } 65 } 66 67 if(natureIndex != -1) { 69 String [] newNatures= new String [prevNatures.length - 1]; 70 System.arraycopy(prevNatures, 0, newNatures, 0, natureIndex); 71 System.arraycopy(prevNatures, natureIndex+1, newNatures, natureIndex, prevNatures.length - (natureIndex+1)); 72 description.setNatureIds(newNatures); 73 proj.setDescription(description, null); 74 } 75 } 76 77 } 78 | Popular Tags |