KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > eclipse > console > utils > ProjectUtils


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     /**
18      * Add the given project nature to the given project (if it isn't already added).
19      * @return true if nature where added, false if not
20      * @throws OperationCanceledException if job were cancelled or CoreException if something went wrong.
21      */

22     public static boolean addProjectNature(IProject project, String JavaDoc 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 JavaDoc[] prevNatures= description.getNatureIds();
30             String JavaDoc[] newNatures= new String JavaDoc[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 JavaDoc 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 JavaDoc[] natures = description.getNatureIds();
51             String JavaDoc[] newNatures = new String JavaDoc[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