|
1 /* |
|
2 * Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: Definitions for the class BuilderUtil |
|
15 * |
|
16 */ |
|
17 |
|
18 package com.nokia.s60tools.analyzetool.builder; |
|
19 |
|
20 import org.eclipse.core.resources.IProject; |
|
21 import org.eclipse.core.resources.IProjectDescription; |
|
22 import org.eclipse.core.runtime.CoreException; |
|
23 |
|
24 /** |
|
25 * Add and removes AnalyzeTool build natures to project natures. |
|
26 * |
|
27 * @author kihe |
|
28 * |
|
29 */ |
|
30 public class BuilderUtil { |
|
31 |
|
32 /** |
|
33 * Constructor. |
|
34 */ |
|
35 public BuilderUtil() { |
|
36 // MethodDeclaration/Block[count(BlockStatement) = 0 and |
|
37 // @containsComment = 'false'] |
|
38 } |
|
39 |
|
40 /** |
|
41 * Adds AnalyzeTool build natures. |
|
42 * |
|
43 * @param project |
|
44 * Project reference |
|
45 * @return True no errors otherwise False |
|
46 */ |
|
47 private boolean addAnalysisNatures(final IProject project) { |
|
48 try { |
|
49 |
|
50 // get existing natures |
|
51 String[] natures = project.getDescription().getNatureIds(); |
|
52 |
|
53 // get project description |
|
54 IProjectDescription description = project.getDescription(); |
|
55 |
|
56 // create array for the new natures |
|
57 String[] newNatures = new String[natures.length + 2]; |
|
58 |
|
59 |
|
60 //if QT nature found we must adjust pre- and post natures to correct place |
|
61 if( description.hasNature(com.trolltech.qtcppproject.QtNature.QT_NATURE_ID) ) { |
|
62 |
|
63 //find QT nature location |
|
64 int qtNatureIndex = 0; |
|
65 for( int i=0; i<natures.length; i++ ) { |
|
66 if( natures[i].equals(com.trolltech.qtcppproject.QtNature.QT_NATURE_ID ) ) { |
|
67 qtNatureIndex = i; |
|
68 break; |
|
69 } |
|
70 } |
|
71 |
|
72 //QT nature id found and it is first nature=> now start to copy existing id and add AT id |
|
73 if( qtNatureIndex == 0 ) { |
|
74 //add natures |
|
75 newNatures[0] = natures[0]; |
|
76 newNatures[1] = PreNature.NATURE_ID; |
|
77 |
|
78 //copy rest of the existing natures |
|
79 System.arraycopy(natures, 1, newNatures, 2, natures.length-1); |
|
80 |
|
81 // add post-builder nature |
|
82 newNatures[natures.length + 1] = PostNature.NATURE_ID; |
|
83 } |
|
84 //QT nature id found but there are some other natures |
|
85 //before QT nature |
|
86 else { |
|
87 //copy existing natures |
|
88 System.arraycopy(natures, 0, newNatures, 0, qtNatureIndex+1); |
|
89 newNatures[qtNatureIndex+1] = PreNature.NATURE_ID; |
|
90 |
|
91 //copy rest of the existing natures |
|
92 System.arraycopy(natures, qtNatureIndex+1, newNatures, qtNatureIndex+2, natures.length-qtNatureIndex); |
|
93 |
|
94 // add post-builder nature |
|
95 newNatures[natures.length + 1] = PostNature.NATURE_ID; |
|
96 } |
|
97 } |
|
98 //no QT nature found just add pre nature first and post nature last |
|
99 else { |
|
100 |
|
101 // set pre-builder nature |
|
102 newNatures[0] = PreNature.NATURE_ID; |
|
103 |
|
104 // copy existing natures |
|
105 System.arraycopy(natures, 0, newNatures, 1, natures.length); |
|
106 |
|
107 // add post-builder nature |
|
108 newNatures[natures.length + 1] = PostNature.NATURE_ID; |
|
109 } |
|
110 |
|
111 // update project description |
|
112 description.setNatureIds(newNatures); |
|
113 project.setDescription(description, null); |
|
114 |
|
115 return true; |
|
116 } catch (CoreException ce) { |
|
117 ce.printStackTrace(); |
|
118 return false; |
|
119 } |
|
120 } |
|
121 |
|
122 |
|
123 |
|
124 /** |
|
125 * Disable AnalyzeTool build natures. |
|
126 * |
|
127 * @param project |
|
128 * Project reference |
|
129 */ |
|
130 public final void disableNatures(final IProject project) { |
|
131 try { |
|
132 // both natures found |
|
133 if (isNatureEnabled(project)) { |
|
134 removeNature(project, 2); |
|
135 } else { |
|
136 IProjectDescription description = project.getDescription(); |
|
137 |
|
138 // find natures |
|
139 boolean foundPreNature = description |
|
140 .hasNature(PreNature.NATURE_ID); |
|
141 boolean foundPostNature = description |
|
142 .hasNature(PostNature.NATURE_ID); |
|
143 |
|
144 //either pre- or post builder nature found => remove it |
|
145 if (foundPreNature || foundPostNature) { |
|
146 removeNature(project, 1); |
|
147 } |
|
148 |
|
149 } |
|
150 } catch (CoreException ce) { |
|
151 ce.printStackTrace(); |
|
152 } |
|
153 |
|
154 } |
|
155 |
|
156 /** |
|
157 * Add AnalyzeTool custom builder nature to project builder natures. |
|
158 * |
|
159 * @param project |
|
160 * Project reference |
|
161 * @return True if natures are added otherwise False |
|
162 */ |
|
163 public final boolean enableNatures(final IProject project) { |
|
164 try { |
|
165 // check is nature enable |
|
166 if( isNatureEnabled(project)) { |
|
167 return true; |
|
168 } |
|
169 // get project description |
|
170 IProjectDescription description = project.getDescription(); |
|
171 |
|
172 // find natures |
|
173 boolean foundPreNature = description |
|
174 .hasNature(PreNature.NATURE_ID); |
|
175 boolean foundPostNature = description |
|
176 .hasNature(PostNature.NATURE_ID); |
|
177 |
|
178 // only one analyzetool nature found => remove it |
|
179 if (foundPostNature || foundPreNature) { |
|
180 removeNature(project, 1); |
|
181 } |
|
182 |
|
183 // add right analysis natures |
|
184 return addAnalysisNatures(project); |
|
185 |
|
186 } catch (CoreException ce) { |
|
187 ce.printStackTrace(); |
|
188 return false; |
|
189 } |
|
190 } |
|
191 |
|
192 /** |
|
193 * Checks is AnalyzeTool custom nature enabled. |
|
194 * |
|
195 * @param projRef |
|
196 * Project reference |
|
197 * @return True both pre or post nature enabled otherwise False |
|
198 */ |
|
199 public final boolean isNatureEnabled(final IProject projRef) { |
|
200 boolean preNatureFound = false; |
|
201 boolean postNatureFound = false; |
|
202 |
|
203 //check project validity |
|
204 if (projRef == null || !projRef.isOpen()) { |
|
205 return false; |
|
206 } |
|
207 |
|
208 try { |
|
209 IProjectDescription description = projRef.getDescription(); |
|
210 preNatureFound = description.hasNature(PreNature.NATURE_ID); |
|
211 postNatureFound = description.hasNature(PostNature.NATURE_ID); |
|
212 } catch (CoreException e) { |
|
213 e.printStackTrace(); |
|
214 } |
|
215 if (preNatureFound && postNatureFound) { |
|
216 return true; |
|
217 } |
|
218 return false; |
|
219 |
|
220 } |
|
221 |
|
222 /** |
|
223 * Removes AnalyzeTool custom builder natures. |
|
224 * |
|
225 * @param project |
|
226 * Project reference |
|
227 * @param count |
|
228 * How many natures to remove |
|
229 */ |
|
230 private void removeNature(final IProject project, final int count) { |
|
231 try { |
|
232 IProjectDescription desc = project.getDescription(); |
|
233 String[] natures = desc.getNatureIds(); |
|
234 String[] newNatures = new String[natures.length - count]; |
|
235 |
|
236 int index = 0; |
|
237 |
|
238 //thru natures |
|
239 //if pre- or post nature found skip it |
|
240 for (int i = 0; i < natures.length; i++) { |
|
241 if (!natures[i].equals(PreNature.NATURE_ID) |
|
242 && !natures[i].equals(PostNature.NATURE_ID)) { |
|
243 newNatures[index] = natures[i]; |
|
244 index++; |
|
245 } |
|
246 } |
|
247 desc.setNatureIds(newNatures); |
|
248 project.setDescription(desc, null); |
|
249 |
|
250 } catch (CoreException ce) { |
|
251 ce.printStackTrace(); |
|
252 } |
|
253 |
|
254 } |
|
255 } |