1 package org.hibernate.eclipse.console.utils; 2 3 import org.eclipse.core.resources.IProject; 4 import org.eclipse.core.resources.IProjectDescription; 5 import org.eclipse.core.resources.IResource; 6 import org.eclipse.core.runtime.CoreException; 7 import org.eclipse.core.runtime.IProgressMonitor; 8 import org.eclipse.core.runtime.NullProgressMonitor; 9 import org.eclipse.core.runtime.OperationCanceledException; 10 11 public class ProjectUtils { 12 13 private ProjectUtils() { 14 15 } 16 17 22 public static boolean addProjectNature(IProject project, String nature, IProgressMonitor monitor) throws CoreException { 23 if (monitor != null && monitor.isCanceled()) { 24 throw new OperationCanceledException(); 25 } 26 27 if (!project.hasNature(nature)) { 28 IProjectDescription description = project.getDescription(); 29 String [] prevNatures= description.getNatureIds(); 30 String [] newNatures= new String [prevNatures.length + 1]; 31 System.arraycopy(prevNatures, 0, newNatures, 0, prevNatures.length); 32 newNatures[prevNatures.length]= nature; 33 description.setNatureIds(newNatures); 34 project.setDescription(description, monitor); 35 return true; 36 } else { 37 monitor.worked(1); 38 return false; 39 } 40 } 41 42 public static boolean removeProjectNature(IProject project, String nature, NullProgressMonitor monitor) throws CoreException { 43 if (monitor != null && monitor.isCanceled()) { 44 throw new OperationCanceledException(); 45 } 46 47 if (project.hasNature(nature)) { 48 IProjectDescription description = project.getDescription(); 49 50 String [] natures = description.getNatureIds(); 51 String [] newNatures = new String [natures.length - 1]; 52 for(int i = 0; i < natures.length; i++) { 53 if (!natures[i].equals(nature)) 54 newNatures[i] = natures[i]; 55 } 56 description.setNatureIds(newNatures); 57 project.setDescription(description, monitor); 58 return true; 59 } else { 60 monitor.worked(1); 61 return false; 62 } 63 } 64 65 } 66 | Popular Tags |