1616import org .eclipse .core .resources .IProjectDescription ;
1717import org .eclipse .core .resources .IProjectNature ;
1818import org .eclipse .core .runtime .CoreException ;
19+ import org .eclipse .jdt .core .JavaCore ;
1920
2021/**
2122 * @author zhou renjian
@@ -30,12 +31,14 @@ public class Java2ScriptProjectNature implements IProjectNature {
3031 */
3132 public void configure () throws CoreException {
3233 addToBuildSpec ("net.sf.j2s.core.java2scriptbuilder" );
34+ removeFromBuildSpec (JavaCore .BUILDER_ID );
3335 }
3436 /* (non-Javadoc)
3537 * @see org.eclipse.core.resources.IProjectNature#deconfigure()
3638 */
3739 public void deconfigure () throws CoreException {
3840 removeFromBuildSpec ("net.sf.j2s.core.java2scriptbuilder" );
41+ addToBuildSpec (JavaCore .BUILDER_ID );
3942 }
4043 /* (non-Javadoc)
4144 * @see org.eclipse.core.resources.IProjectNature#getProject()
@@ -53,19 +56,31 @@ public void setProject(IProject project) {
5356 public boolean hasNature () {
5457 try {
5558 IProjectDescription description = this .project .getDescription ();
56- int javaCommandIndex = getJavaCommandIndex (description .getBuildSpec ());
59+ int javaCommandIndex = getJava2ScriptCommandIndex (description .getBuildSpec ());
5760 return (javaCommandIndex != -1 );
5861 } catch (CoreException e ) {
5962 e .printStackTrace ();
6063 return false ;
6164 }
6265 }
63-
66+
6467 /**
6568 * Adds a builder to the build spec for the given project.
6669 */
6770 public void addToBuildSpec (String builderID ) throws CoreException {
68- //this.project.addToBuildSpec(builderID);
71+ if ("net.sf.j2s.core.java2scriptbuilder" .equals (builderID )) {
72+ IProjectDescription description = this .project .getDescription ();
73+ int javaCommandIndex = getJava2ScriptCommandIndex (description .getBuildSpec ());
74+
75+ if (javaCommandIndex == -1 ) {
76+
77+ // Add a Java command to the build spec
78+ ICommand command = description .newCommand ();
79+ command .setBuilderName (builderID );
80+ setJava2ScriptCommand (description , command );
81+ }
82+ return ;
83+ }
6984
7085 IProjectDescription description = this .project .getDescription ();
7186 int javaCommandIndex = getJavaCommandIndex (description .getBuildSpec ());
@@ -77,13 +92,13 @@ public void addToBuildSpec(String builderID) throws CoreException {
7792 command .setBuilderName (builderID );
7893 setJavaCommand (description , command );
7994 }
80- }
95+ }
8196
8297 /**
8398 * Update the Java command in the build spec (replace existing one if present,
8499 * add one first if none).
85100 */
86- private void setJavaCommand (
101+ private void setJava2ScriptCommand (
87102 IProjectDescription description ,
88103 ICommand newCommand )
89104 throws CoreException {
@@ -95,8 +110,36 @@ private void setJavaCommand(
95110 if (oldJavaCommandIndex == -1 ) {
96111 // Add a Java build spec before other builders (1FWJK7I)
97112 newCommands = new ICommand [oldBuildSpec .length + 1 ];
98- System .arraycopy (oldBuildSpec , 0 , newCommands , 0 , oldBuildSpec .length );
99- newCommands [oldBuildSpec .length ] = newCommand ;
113+ System .arraycopy (oldBuildSpec , 0 , newCommands , 1 , oldBuildSpec .length );
114+ newCommands [0 ] = newCommand ;
115+ } else {
116+ oldBuildSpec [oldJavaCommandIndex ] = newCommand ;
117+ newCommands = oldBuildSpec ;
118+ }
119+
120+ // Commit the spec change into the project
121+ description .setBuildSpec (newCommands );
122+ this .project .setDescription (description , null );
123+ }
124+
125+ /**
126+ * Update the Java command in the build spec (replace existing one if present,
127+ * add one first if none).
128+ */
129+ private void setJavaCommand (
130+ IProjectDescription description ,
131+ ICommand newCommand )
132+ throws CoreException {
133+
134+ ICommand [] oldBuildSpec = description .getBuildSpec ();
135+ int oldJavaCommandIndex = getJava2ScriptCommandIndex (oldBuildSpec );
136+ ICommand [] newCommands ;
137+
138+ if (oldJavaCommandIndex == -1 ) {
139+ // Add a Java build spec before other builders (1FWJK7I)
140+ newCommands = new ICommand [oldBuildSpec .length + 1 ];
141+ System .arraycopy (oldBuildSpec , 0 , newCommands , 1 , oldBuildSpec .length );
142+ newCommands [0 ] = newCommand ;
100143 } else {
101144 oldBuildSpec [oldJavaCommandIndex ] = newCommand ;
102145 newCommands = oldBuildSpec ;
@@ -111,7 +154,21 @@ private void setJavaCommand(
111154 * Find the specific Java command amongst the given build spec
112155 * and return its index or -1 if not found.
113156 */
114- private int getJavaCommandIndex (ICommand [] buildSpec ) {
157+ private static int getJavaCommandIndex (ICommand [] buildSpec ) {
158+
159+ for (int i = 0 ; i < buildSpec .length ; ++i ) {
160+ if (buildSpec [i ].getBuilderName ().equals (JavaCore .BUILDER_ID )) {
161+ return i ;
162+ }
163+ }
164+ return -1 ;
165+ }
166+
167+ /**
168+ * Find the specific Java2Script command amongst the given build spec
169+ * and return its index or -1 if not found.
170+ */
171+ private static int getJava2ScriptCommandIndex (ICommand [] buildSpec ) {
115172
116173 for (int i = 0 ; i < buildSpec .length ; ++i ) {
117174 if (buildSpec [i ].getBuilderName ().equals ("net.sf.j2s.core.java2scriptbuilder" )) {
@@ -139,4 +196,56 @@ public void removeFromBuildSpec(String builderID) throws CoreException {
139196 }
140197 }
141198 }
199+
200+ public static boolean hasJavaBuilder (IProject project ) {
201+ try {
202+ IProjectDescription description = project .getDescription ();
203+ int javaCommandIndex = getJavaCommandIndex (description .getBuildSpec ());
204+ return javaCommandIndex != -1 ;
205+ } catch (CoreException e ) {
206+ e .printStackTrace ();
207+ }
208+ return false ;
209+ }
210+
211+ public static boolean removeJavaBuilder (IProject project ) {
212+ boolean removed = false ;
213+ try {
214+ IProjectDescription description = project .getDescription ();
215+ ICommand [] commands = description .getBuildSpec ();
216+ for (int i = 0 ; i < commands .length ; ++i ) {
217+ if (commands [i ].getBuilderName ().equals (JavaCore .BUILDER_ID )) {
218+ ICommand [] newCommands = new ICommand [commands .length - 1 ];
219+ System .arraycopy (commands , 0 , newCommands , 0 , i );
220+ System .arraycopy (commands , i + 1 , newCommands , i , commands .length - i - 1 );
221+ description .setBuildSpec (newCommands );
222+ project .setDescription (description , null );
223+ removed = true ;
224+ break ;
225+ }
226+ }
227+ if (removed ) { // remove java2script builder, so later the builder can be the first builder
228+ for (int i = 0 ; i < commands .length ; ++i ) {
229+ if (commands [i ].getBuilderName ().equals ("net.sf.j2s.core.java2scriptbuilder" )) {
230+ ICommand [] newCommands = new ICommand [commands .length - 1 ];
231+ System .arraycopy (commands , 0 , newCommands , 0 , i );
232+ System .arraycopy (commands , i + 1 , newCommands , i , commands .length - i - 1 );
233+ description .setBuildSpec (newCommands );
234+ project .setDescription (description , null );
235+ break ;
236+ }
237+ }
238+ }
239+ } catch (CoreException e ) {
240+ e .printStackTrace ();
241+ }
242+ Java2ScriptProjectNature pn = new Java2ScriptProjectNature ();
243+ pn .setProject (project );
244+ try {
245+ pn .configure ();
246+ } catch (CoreException e ) {
247+ e .printStackTrace ();
248+ }
249+ return removed ;
250+ }
142251}
0 commit comments