1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.seasar.eclipse.common.util; |
17 | |
|
18 | |
import java.util.ArrayList; |
19 | |
import java.util.List; |
20 | |
|
21 | |
import org.eclipse.core.resources.ICommand; |
22 | |
import org.eclipse.core.resources.IProject; |
23 | |
import org.eclipse.core.resources.IProjectDescription; |
24 | |
import org.eclipse.core.resources.IProjectNature; |
25 | |
import org.eclipse.core.resources.IResource; |
26 | |
import org.eclipse.core.resources.IWorkspace; |
27 | |
import org.eclipse.core.resources.IWorkspaceRoot; |
28 | |
import org.eclipse.core.resources.ProjectScope; |
29 | |
import org.eclipse.core.resources.ResourcesPlugin; |
30 | |
import org.eclipse.core.runtime.CoreException; |
31 | |
import org.eclipse.core.runtime.IPath; |
32 | |
import org.eclipse.core.runtime.Platform; |
33 | |
import org.eclipse.core.runtime.preferences.IScopeContext; |
34 | |
import org.eclipse.core.runtime.preferences.InstanceScope; |
35 | |
import org.eclipse.jdt.core.IClasspathEntry; |
36 | |
import org.eclipse.jdt.core.IJavaModel; |
37 | |
import org.eclipse.jdt.core.IJavaProject; |
38 | |
import org.eclipse.jdt.core.IPackageFragmentRoot; |
39 | |
import org.eclipse.jdt.core.JavaCore; |
40 | |
import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants; |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | 0 | public class ProjectUtil { |
47 | |
|
48 | |
private static List<ICommand> getCommands(IProjectDescription desc, String[] ignores) { |
49 | 0 | List<ICommand> newCommands = new ArrayList<ICommand>(); |
50 | 0 | for (ICommand command : desc.getBuildSpec()) { |
51 | 0 | boolean flag = true; |
52 | 0 | for (String ignore : ignores) { |
53 | 0 | if (command.getBuilderName().equals(ignore)) { |
54 | 0 | flag = false; |
55 | 0 | break; |
56 | |
} |
57 | |
} |
58 | 0 | if (flag) { |
59 | 0 | newCommands.add(command); |
60 | |
} else { |
61 | 0 | flag = true; |
62 | |
} |
63 | |
} |
64 | 0 | return newCommands; |
65 | |
} |
66 | |
|
67 | |
private static void setCommands(IProjectDescription desc, List<ICommand> newCommands) { |
68 | 0 | desc.setBuildSpec(newCommands.toArray(new ICommand[newCommands.size()])); |
69 | 0 | } |
70 | |
|
71 | |
public static void addBuilders(IProject project, String[] ids) |
72 | |
throws CoreException { |
73 | 0 | IProjectDescription desc = project.getDescription(); |
74 | 0 | List<ICommand> newCommands = getCommands(desc, ids); |
75 | 0 | for (String id : ids) { |
76 | 0 | ICommand command = desc.newCommand(); |
77 | 0 | command.setBuilderName(id); |
78 | 0 | newCommands.add(command); |
79 | |
} |
80 | 0 | setCommands(desc, newCommands); |
81 | 0 | project.setDescription(desc, null); |
82 | 0 | } |
83 | |
|
84 | |
public static void removeBuilders(IProject project, String[] id) |
85 | |
throws CoreException { |
86 | 0 | IProjectDescription desc = project.getDescription(); |
87 | 0 | List<ICommand> newCommands = getCommands(desc, id); |
88 | 0 | setCommands(desc, newCommands); |
89 | 0 | project.setDescription(desc, null); |
90 | 0 | } |
91 | |
|
92 | |
public static void addNature(IProject project, String natureID) |
93 | |
throws CoreException { |
94 | 0 | if ((project != null) && project.isAccessible()) { |
95 | 0 | IProjectDescription desc = project.getDescription(); |
96 | 0 | String[] natureIDs = desc.getNatureIds(); |
97 | 0 | int length = natureIDs.length; |
98 | 0 | String[] newIDs = new String[length + 1]; |
99 | 0 | for (int i = 0; i < length; i++) { |
100 | 0 | if (natureIDs[i].equals(natureID)) { |
101 | 0 | return; |
102 | |
} |
103 | 0 | newIDs[i] = natureIDs[i]; |
104 | |
} |
105 | 0 | newIDs[length] = natureID; |
106 | 0 | desc.setNatureIds(newIDs); |
107 | 0 | project.setDescription(desc, null); |
108 | |
} |
109 | 0 | } |
110 | |
|
111 | |
public static void removeNature(IProject project, String natureID) |
112 | |
throws CoreException { |
113 | 0 | if ((project != null) && project.isAccessible()) { |
114 | 0 | IProjectDescription desc = project.getDescription(); |
115 | 0 | String[] natureIDs = desc.getNatureIds(); |
116 | 0 | int length = natureIDs.length; |
117 | 0 | for (int i = 0; i < length; i++) { |
118 | 0 | if (natureIDs[i].equals(natureID)) { |
119 | 0 | String[] newIDs = new String[length - 1]; |
120 | 0 | System.arraycopy(natureIDs, 0, newIDs, 0, i); |
121 | 0 | System.arraycopy(natureIDs, i + 1, newIDs, i, length - i |
122 | |
- 1); |
123 | 0 | desc.setNatureIds(newIDs); |
124 | 0 | project.setDescription(desc, null); |
125 | 0 | return; |
126 | |
} |
127 | |
} |
128 | |
} |
129 | 0 | } |
130 | |
|
131 | |
public static IProjectNature getNature(IProject project, String natureID) |
132 | |
throws CoreException { |
133 | 0 | if ((project != null) && (project.isOpen())) { |
134 | 0 | return project.getNature(natureID); |
135 | |
} |
136 | 0 | return null; |
137 | |
} |
138 | |
|
139 | |
public static boolean hasNature(IProject project, String natureID) { |
140 | |
try { |
141 | 0 | return getNature(project, natureID) != null; |
142 | 0 | } catch (CoreException e) { |
143 | 0 | return false; |
144 | |
} |
145 | |
} |
146 | |
|
147 | |
public static String[] getNatureIds(IProject project) { |
148 | |
try { |
149 | 0 | return project.getDescription().getNatureIds(); |
150 | 0 | } catch (CoreException e) { |
151 | 0 | return new String[0]; |
152 | |
} |
153 | |
} |
154 | |
|
155 | |
public static IWorkspace getWorkspace() { |
156 | 0 | return ResourcesPlugin.getWorkspace(); |
157 | |
} |
158 | |
|
159 | |
public static IWorkspaceRoot getWorkspaceRoot() { |
160 | 0 | return getWorkspace().getRoot(); |
161 | |
} |
162 | |
|
163 | |
public static IProject[] getAllProjects() { |
164 | 0 | return getWorkspaceRoot().getProjects(); |
165 | |
} |
166 | |
|
167 | |
public static IProject getProject(String projectName) { |
168 | 0 | return getWorkspaceRoot().getProject(projectName); |
169 | |
} |
170 | |
|
171 | |
public static IJavaProject getJavaProject(String projectName) { |
172 | 0 | return JavaCore.create(getProject(projectName)); |
173 | |
} |
174 | |
|
175 | |
public static IJavaProject getJavaProject(IResource resource) { |
176 | 0 | return JavaCore.create(resource.getProject()); |
177 | |
} |
178 | |
|
179 | |
public static IJavaProject[] getJavaProjects() throws CoreException { |
180 | 0 | return getJavaModel().getJavaProjects(); |
181 | |
} |
182 | |
|
183 | |
public static IJavaModel getJavaModel() { |
184 | 0 | IWorkspace workspace = ResourcesPlugin.getWorkspace(); |
185 | 0 | return JavaCore.create(workspace.getRoot()); |
186 | |
} |
187 | |
|
188 | |
public static IJavaProject getJavaProject(IPath path) { |
189 | 0 | IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject( |
190 | |
path.segment(0)); |
191 | 0 | return JavaCore.create(project); |
192 | |
} |
193 | |
|
194 | |
public static String createIndentString(int indentationUnits, |
195 | |
IJavaProject project) { |
196 | 0 | final String tabChar = getCoreOption(project, |
197 | |
DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR); |
198 | |
final int tabs, spaces; |
199 | 0 | if (JavaCore.SPACE.equals(tabChar)) { |
200 | 0 | tabs = 0; |
201 | 0 | spaces = indentationUnits * getIndentWidth(project); |
202 | 0 | } else if (JavaCore.TAB.equals(tabChar)) { |
203 | |
|
204 | 0 | tabs = indentationUnits; |
205 | 0 | spaces = 0; |
206 | 0 | } else if (DefaultCodeFormatterConstants.MIXED.equals(tabChar)) { |
207 | 0 | int tabWidth = getTabWidth(project); |
208 | 0 | int spaceEquivalents = indentationUnits * getIndentWidth(project); |
209 | 0 | if (tabWidth > 0) { |
210 | 0 | tabs = spaceEquivalents / tabWidth; |
211 | 0 | spaces = spaceEquivalents % tabWidth; |
212 | |
} else { |
213 | 0 | tabs = 0; |
214 | 0 | spaces = spaceEquivalents; |
215 | |
} |
216 | 0 | } else { |
217 | 0 | return null; |
218 | |
} |
219 | |
|
220 | 0 | StringBuffer buffer = new StringBuffer(tabs + spaces); |
221 | 0 | for (int i = 0; i < tabs; i++) { |
222 | 0 | buffer.append('\t'); |
223 | |
} |
224 | 0 | for (int i = 0; i < spaces; i++) { |
225 | 0 | buffer.append(' '); |
226 | |
} |
227 | 0 | return buffer.toString(); |
228 | |
} |
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
public static int getTabWidth(IJavaProject project) { |
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | |
|
245 | |
String key; |
246 | 0 | if (JavaCore.SPACE.equals(getCoreOption(project, |
247 | |
DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR))) { |
248 | 0 | key = DefaultCodeFormatterConstants.FORMATTER_INDENTATION_SIZE; |
249 | |
} else { |
250 | 0 | key = DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE; |
251 | |
} |
252 | |
|
253 | 0 | return getCoreOption(project, key, 4); |
254 | |
} |
255 | |
|
256 | |
public static int getIndentWidth(IJavaProject project) { |
257 | |
String key; |
258 | 0 | if (DefaultCodeFormatterConstants.MIXED.equals(getCoreOption(project, |
259 | |
DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR))) { |
260 | 0 | key = DefaultCodeFormatterConstants.FORMATTER_INDENTATION_SIZE; |
261 | |
} else { |
262 | 0 | key = DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE; |
263 | |
} |
264 | |
|
265 | 0 | return getCoreOption(project, key, 4); |
266 | |
} |
267 | |
|
268 | |
public static int getCoreOption(IJavaProject project, String key, int def) { |
269 | |
try { |
270 | 0 | return Integer.parseInt(getCoreOption(project, key)); |
271 | 0 | } catch (NumberFormatException e) { |
272 | 0 | return def; |
273 | |
} |
274 | |
} |
275 | |
|
276 | |
public static String getCoreOption(IJavaProject project, String key) { |
277 | 0 | if (project == null) { |
278 | 0 | return JavaCore.getOption(key); |
279 | |
} |
280 | 0 | return project.getOption(key, true); |
281 | |
} |
282 | |
|
283 | |
public static String getProjectLineDelimiter(IJavaProject javaProject) { |
284 | 0 | IProject project = null; |
285 | 0 | if (javaProject != null) { |
286 | 0 | project = javaProject.getProject(); |
287 | |
} |
288 | |
|
289 | 0 | String lineDelimiter = getLineDelimiterPreference(project); |
290 | 0 | if (lineDelimiter != null) { |
291 | 0 | return lineDelimiter; |
292 | |
} |
293 | |
|
294 | 0 | return System.getProperty("line.separator", "\n"); |
295 | |
} |
296 | |
|
297 | |
public static String getLineDelimiterPreference(IProject project) { |
298 | |
IScopeContext[] scopeContext; |
299 | 0 | if (project != null) { |
300 | |
|
301 | 0 | scopeContext = new IScopeContext[] { new ProjectScope(project) }; |
302 | 0 | String lineDelimiter = Platform.getPreferencesService().getString( |
303 | |
Platform.PI_RUNTIME, Platform.PREF_LINE_SEPARATOR, null, |
304 | |
scopeContext); |
305 | 0 | if (lineDelimiter != null) { |
306 | 0 | return lineDelimiter; |
307 | |
} |
308 | |
} |
309 | |
|
310 | 0 | scopeContext = new IScopeContext[] { new InstanceScope() }; |
311 | 0 | String platformDefault = System.getProperty("line.separator", "\n"); |
312 | 0 | return Platform.getPreferencesService().getString(Platform.PI_RUNTIME, |
313 | |
Platform.PREF_LINE_SEPARATOR, platformDefault, scopeContext); |
314 | |
} |
315 | |
|
316 | |
public static IPackageFragmentRoot getFirstSrcPackageFragmentRoot( |
317 | |
IJavaProject javap) throws CoreException { |
318 | 0 | IPackageFragmentRoot[] roots = javap.getPackageFragmentRoots(); |
319 | 0 | for (int i = 0; roots != null && i < roots.length; i++) { |
320 | 0 | IPackageFragmentRoot root = roots[i]; |
321 | 0 | if (root.getKind() == IPackageFragmentRoot.K_SOURCE) { |
322 | 0 | return root; |
323 | |
} |
324 | |
} |
325 | 0 | return null; |
326 | |
} |
327 | |
|
328 | |
public static IPackageFragmentRoot[] getSrcPackageFragmentRoot( |
329 | |
IJavaProject javap) throws CoreException { |
330 | 0 | List<IPackageFragmentRoot> result = new ArrayList<IPackageFragmentRoot>(); |
331 | 0 | IPackageFragmentRoot[] roots = javap.getPackageFragmentRoots(); |
332 | 0 | for (int i = 0; roots != null && i < roots.length; i++) { |
333 | 0 | IPackageFragmentRoot root = roots[i]; |
334 | 0 | if (root.getKind() == IPackageFragmentRoot.K_SOURCE) { |
335 | 0 | result.add(root); |
336 | |
} |
337 | |
} |
338 | 0 | return result.toArray(new IPackageFragmentRoot[result.size()]); |
339 | |
} |
340 | |
|
341 | |
public static IPath[] getOutputLocations(IJavaProject project) |
342 | |
throws CoreException { |
343 | 0 | List<IPath> result = new ArrayList<IPath>(); |
344 | 0 | result.add(project.getOutputLocation()); |
345 | 0 | IClasspathEntry[] entries = project.getRawClasspath(); |
346 | 0 | for (IClasspathEntry entry : entries) { |
347 | 0 | if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) { |
348 | 0 | result.add(entry.getOutputLocation()); |
349 | |
} |
350 | |
} |
351 | |
|
352 | 0 | return result.toArray(new IPath[result.size()]); |
353 | |
} |
354 | |
|
355 | |
public static IProject getCurrentSelectedProject() { |
356 | 0 | return AdaptableUtil.toProject(ResouceUtil.getCurrentSelectedResouce()); |
357 | |
} |
358 | |
} |