16
16
import org .eclipse .core .resources .IProjectDescription ;
17
17
import org .eclipse .core .resources .IProjectNature ;
18
18
import org .eclipse .core .runtime .CoreException ;
19
+ import org .eclipse .jdt .core .JavaCore ;
19
20
20
21
/**
21
22
* @author zhou renjian
@@ -30,12 +31,14 @@ public class Java2ScriptProjectNature implements IProjectNature {
30
31
*/
31
32
public void configure () throws CoreException {
32
33
addToBuildSpec ("net.sf.j2s.core.java2scriptbuilder" );
34
+ removeFromBuildSpec (JavaCore .BUILDER_ID );
33
35
}
34
36
/* (non-Javadoc)
35
37
* @see org.eclipse.core.resources.IProjectNature#deconfigure()
36
38
*/
37
39
public void deconfigure () throws CoreException {
38
40
removeFromBuildSpec ("net.sf.j2s.core.java2scriptbuilder" );
41
+ addToBuildSpec (JavaCore .BUILDER_ID );
39
42
}
40
43
/* (non-Javadoc)
41
44
* @see org.eclipse.core.resources.IProjectNature#getProject()
@@ -53,19 +56,31 @@ public void setProject(IProject project) {
53
56
public boolean hasNature () {
54
57
try {
55
58
IProjectDescription description = this .project .getDescription ();
56
- int javaCommandIndex = getJavaCommandIndex (description .getBuildSpec ());
59
+ int javaCommandIndex = getJava2ScriptCommandIndex (description .getBuildSpec ());
57
60
return (javaCommandIndex != -1 );
58
61
} catch (CoreException e ) {
59
62
e .printStackTrace ();
60
63
return false ;
61
64
}
62
65
}
63
-
66
+
64
67
/**
65
68
* Adds a builder to the build spec for the given project.
66
69
*/
67
70
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
+ }
69
84
70
85
IProjectDescription description = this .project .getDescription ();
71
86
int javaCommandIndex = getJavaCommandIndex (description .getBuildSpec ());
@@ -77,13 +92,13 @@ public void addToBuildSpec(String builderID) throws CoreException {
77
92
command .setBuilderName (builderID );
78
93
setJavaCommand (description , command );
79
94
}
80
- }
95
+ }
81
96
82
97
/**
83
98
* Update the Java command in the build spec (replace existing one if present,
84
99
* add one first if none).
85
100
*/
86
- private void setJavaCommand (
101
+ private void setJava2ScriptCommand (
87
102
IProjectDescription description ,
88
103
ICommand newCommand )
89
104
throws CoreException {
@@ -95,8 +110,36 @@ private void setJavaCommand(
95
110
if (oldJavaCommandIndex == -1 ) {
96
111
// Add a Java build spec before other builders (1FWJK7I)
97
112
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 ;
100
143
} else {
101
144
oldBuildSpec [oldJavaCommandIndex ] = newCommand ;
102
145
newCommands = oldBuildSpec ;
@@ -111,7 +154,21 @@ private void setJavaCommand(
111
154
* Find the specific Java command amongst the given build spec
112
155
* and return its index or -1 if not found.
113
156
*/
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 ) {
115
172
116
173
for (int i = 0 ; i < buildSpec .length ; ++i ) {
117
174
if (buildSpec [i ].getBuilderName ().equals ("net.sf.j2s.core.java2scriptbuilder" )) {
@@ -139,4 +196,56 @@ public void removeFromBuildSpec(String builderID) throws CoreException {
139
196
}
140
197
}
141
198
}
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
+ }
142
251
}
0 commit comments