KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > bull > eclipse > jonas > utils > JDTUtil


1 package com.bull.eclipse.jonas.utils;
2
3 /*
4  * (c) Copyright Bull SA 2003.
5  * All Rights Reserved.
6  */

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 /**
14  * Utility class for JDT
15  * It might exist better way to implements those operations,
16  * or they might already exist in JDT
17  */

18
19 public class JDTUtil {
20
21     /**
22      * Adds a nature to a project
23      * (From BuildPathsBlock class)
24      */

25     
26     public static void addNatureToProject(IProject project, String JavaDoc natureId) throws CoreException {
27         IProject proj = project.getProject(); // Needed if project is a IJavaProject
28
IProjectDescription description = proj.getDescription();
29         String JavaDoc[] 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         // Add nature only if it is not already there
40
if(natureIndex == -1) {
41             String JavaDoc[] newNatures= new String JavaDoc[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     /**
51      * Remove a Nature to a Project
52      */

53     
54     public static void removeNatureToProject(IProject project, String JavaDoc natureId) throws CoreException {
55         IProject proj = project.getProject(); // Needed if project is a IJavaProject
56
IProjectDescription description = proj.getDescription();
57         String JavaDoc[] 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         // Remove nature only if it exists...
68
if(natureIndex != -1) {
69             String JavaDoc[] newNatures= new String JavaDoc[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