author | timkelly |
Thu, 06 Aug 2009 12:01:58 -0500 | |
changeset 52 | 42077b7eab6e |
parent 51 | 49c226a8748e |
child 74 | 30678d98b200 |
permissions | -rw-r--r-- |
37 | 1 |
/******************************************************************************* |
2 |
* Copyright (c) 2005, 2009 IBM Corporation and others. |
|
3 |
* All rights reserved. This program and the accompanying materials |
|
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
|
5 |
* which accompanies this distribution, and is available at |
|
6 |
* http://www.eclipse.org/legal/epl-v10.html |
|
7 |
* |
|
8 |
* Contributors: |
|
9 |
* IBM Corporation - initial API and implementation |
|
10 |
* QNX Software System |
|
11 |
*******************************************************************************/ |
|
12 |
package org.eclipse.cdt.internal.ui; |
|
13 |
||
14 |
import java.util.ArrayList; |
|
52
42077b7eab6e
Second merge to proper cdt_6_0 branch for CDT bugs 181020, 279844, & Carbide bugs 6152 & 9031.
timkelly
parents:
51
diff
changeset
|
15 |
import java.util.Collections; |
37 | 16 |
import java.util.HashMap; |
17 |
import java.util.List; |
|
18 |
import java.util.Map; |
|
19 |
||
20 |
import org.eclipse.core.resources.IContainer; |
|
21 |
import org.eclipse.core.resources.IFile; |
|
22 |
import org.eclipse.core.resources.IFolder; |
|
23 |
import org.eclipse.core.resources.IProject; |
|
24 |
import org.eclipse.core.resources.IResource; |
|
25 |
import org.eclipse.core.runtime.CoreException; |
|
26 |
import org.eclipse.jface.viewers.ITreeContentProvider; |
|
27 |
import org.eclipse.jface.viewers.Viewer; |
|
28 |
import org.eclipse.ui.model.IWorkbenchAdapter; |
|
29 |
||
52
42077b7eab6e
Second merge to proper cdt_6_0 branch for CDT bugs 181020, 279844, & Carbide bugs 6152 & 9031.
timkelly
parents:
51
diff
changeset
|
30 |
import org.eclipse.cdt.core.CCorePlugin; |
37 | 31 |
import org.eclipse.cdt.core.model.CModelException; |
32 |
import org.eclipse.cdt.core.model.CoreModel; |
|
33 |
import org.eclipse.cdt.core.model.IArchive; |
|
34 |
import org.eclipse.cdt.core.model.IArchiveContainer; |
|
35 |
import org.eclipse.cdt.core.model.IBinary; |
|
36 |
import org.eclipse.cdt.core.model.IBinaryContainer; |
|
37 |
import org.eclipse.cdt.core.model.IBinaryModule; |
|
38 |
import org.eclipse.cdt.core.model.ICContainer; |
|
39 |
import org.eclipse.cdt.core.model.ICElement; |
|
40 |
import org.eclipse.cdt.core.model.ICModel; |
|
41 |
import org.eclipse.cdt.core.model.ICProject; |
|
42 |
import org.eclipse.cdt.core.model.IInclude; |
|
43 |
import org.eclipse.cdt.core.model.IMember; |
|
44 |
import org.eclipse.cdt.core.model.INamespace; |
|
45 |
import org.eclipse.cdt.core.model.IParent; |
|
46 |
import org.eclipse.cdt.core.model.ISourceReference; |
|
47 |
import org.eclipse.cdt.core.model.ISourceRoot; |
|
48 |
import org.eclipse.cdt.core.model.ITranslationUnit; |
|
49 |
import org.eclipse.cdt.core.model.IWorkingCopy; |
|
50 |
import org.eclipse.cdt.ui.CElementGrouping; |
|
51 |
import org.eclipse.cdt.ui.CUIPlugin; |
|
52 |
import org.eclipse.cdt.ui.IncludesGrouping; |
|
53 |
import org.eclipse.cdt.ui.NamespacesGrouping; |
|
54 |
||
55 |
/** |
|
56 |
* A base content provider for C elements. It provides access to the |
|
57 |
* C element hierarchy without listening to changes in the C model. |
|
58 |
* Use this class when you want to present the C elements |
|
59 |
* in a modal dialog or wizard. |
|
60 |
* <p> |
|
61 |
* The following C element hierarchy is surfaced by this content provider: |
|
62 |
* <p> |
|
63 |
* <pre> |
|
64 |
C model (<code>ICModel</code>)<br> |
|
65 |
C project (<code>ICProject</code>)<br> |
|
66 |
Source root (<code>ISourceRoot</code>)<br> |
|
67 |
C Container(folders) (<code>ICContainer</code>)<br> |
|
68 |
Translation unit (<code>ITranslationUnit</code>)<br> |
|
69 |
Binary file (<code>IBinary</code>)<br> |
|
70 |
Archive file (<code>IArchive</code>)<br> |
|
71 |
Non C Resource file (<code>Object</code>)<br> |
|
72 |
||
73 |
* </pre> |
|
74 |
*/ |
|
75 |
public class BaseCElementContentProvider implements ITreeContentProvider { |
|
76 |
||
77 |
protected static final Object[] NO_CHILDREN= new Object[0]; |
|
78 |
||
79 |
protected boolean fProvideMembers= false; |
|
80 |
protected boolean fProvideWorkingCopy= false; |
|
81 |
protected boolean fIncludesGrouping= false; |
|
82 |
protected boolean fNamespacesGrouping= false; |
|
83 |
protected boolean fMemberGrouping= false; |
|
84 |
||
85 |
public BaseCElementContentProvider() { |
|
86 |
this(false, false); |
|
87 |
} |
|
88 |
||
89 |
public BaseCElementContentProvider(boolean provideMembers, boolean provideWorkingCopy) { |
|
90 |
fProvideMembers= provideMembers; |
|
91 |
fProvideWorkingCopy= provideWorkingCopy; |
|
92 |
} |
|
93 |
||
94 |
/** |
|
95 |
* Returns whether the members are provided when asking |
|
96 |
* for a TU's children. |
|
97 |
*/ |
|
98 |
public boolean getProvideMembers() { |
|
99 |
return fProvideMembers; |
|
100 |
} |
|
101 |
||
102 |
/** |
|
103 |
* Returns whether the members are provided when asking |
|
104 |
* for a TU's children. |
|
105 |
*/ |
|
106 |
public void setProvideMembers(boolean b) { |
|
107 |
fProvideMembers= b; |
|
108 |
} |
|
109 |
||
110 |
/** |
|
111 |
* Sets whether the members are provided from |
|
112 |
* a working copy of a compilation unit |
|
113 |
*/ |
|
114 |
public void setProvideWorkingCopy(boolean b) { |
|
115 |
fProvideWorkingCopy= b; |
|
116 |
} |
|
117 |
||
118 |
/** |
|
119 |
* Returns whether the members are provided |
|
120 |
* from a working copy a compilation unit. |
|
121 |
*/ |
|
122 |
public boolean getProvideWorkingCopy() { |
|
123 |
return fProvideWorkingCopy; |
|
124 |
} |
|
125 |
||
126 |
/** |
|
127 |
* Can elements be group. |
|
128 |
*/ |
|
129 |
public boolean areIncludesGroup() { |
|
130 |
return fIncludesGrouping; |
|
131 |
} |
|
132 |
||
133 |
/** |
|
134 |
* Allow Elements to be group. |
|
135 |
* @param b |
|
136 |
*/ |
|
137 |
public void setIncludesGrouping(boolean b) { |
|
138 |
fIncludesGrouping = b; |
|
139 |
} |
|
140 |
||
141 |
/** |
|
142 |
* Can elements be group. |
|
143 |
*/ |
|
144 |
public boolean areNamespacesGroup() { |
|
145 |
return fNamespacesGrouping; |
|
146 |
} |
|
147 |
||
148 |
/** |
|
149 |
* Allow Elements to be group. |
|
150 |
* @param b |
|
151 |
*/ |
|
152 |
public void setNamespacesGrouping(boolean b) { |
|
153 |
fNamespacesGrouping = b; |
|
154 |
} |
|
155 |
||
156 |
/** |
|
157 |
* @return whether grouping of members is enabled |
|
158 |
*/ |
|
159 |
public boolean isMemberGroupingEnabled() { |
|
160 |
return fMemberGrouping; |
|
161 |
} |
|
162 |
||
163 |
/** |
|
164 |
* Enable/disable member grouping by common namespace. |
|
165 |
* @param enable |
|
166 |
*/ |
|
167 |
public void setMemberGrouping(boolean enable) { |
|
168 |
fMemberGrouping = enable; |
|
169 |
} |
|
170 |
||
171 |
/* (non-Cdoc) |
|
172 |
* Method declared on IContentProvider. |
|
173 |
*/ |
|
174 |
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { |
|
175 |
} |
|
176 |
||
177 |
/* (non-Cdoc) |
|
178 |
* Method declared on IContentProvider. |
|
179 |
*/ |
|
180 |
public void dispose() { |
|
181 |
} |
|
182 |
||
183 |
/* (non-Cdoc) |
|
184 |
* Method declared on IStructuredContentProvider. |
|
185 |
*/ |
|
186 |
public Object[] getElements(Object parent) { |
|
187 |
return getChildren(parent); |
|
188 |
} |
|
189 |
||
190 |
/* (non-Cdoc) |
|
191 |
* Method declared on ITreeContentProvider. |
|
192 |
*/ |
|
193 |
public Object[] getChildren(Object element) { |
|
194 |
if (!exists(element)) |
|
195 |
return NO_CHILDREN; |
|
196 |
||
197 |
try { |
|
198 |
if (element instanceof ICModel) { |
|
199 |
return getCProjects((ICModel)element); |
|
200 |
} else if (element instanceof ICProject ) { |
|
201 |
return getSourceRoots((ICProject)element); |
|
202 |
} else if (element instanceof ICContainer) { |
|
203 |
return getCResources((ICContainer)element); |
|
204 |
} else if (element instanceof ITranslationUnit) { |
|
205 |
// if we want to get the children of a translation unit |
|
206 |
if (fProvideMembers) { |
|
207 |
// if we want to use the working copy of it |
|
208 |
ITranslationUnit tu = (ITranslationUnit)element; |
|
209 |
if (fProvideWorkingCopy){ |
|
210 |
// if it is not already a working copy |
|
211 |
if (!(element instanceof IWorkingCopy)){ |
|
212 |
// if it has a valid working copy |
|
213 |
IWorkingCopy copy = tu.findSharedWorkingCopy(CUIPlugin.getDefault().getBufferFactory()); |
|
214 |
if (copy != null) { |
|
215 |
tu = copy; |
|
216 |
} |
|
217 |
} |
|
218 |
} |
|
219 |
return getTranslationUnitChildren(tu); |
|
220 |
} |
|
221 |
} else if (element instanceof IBinary) { |
|
222 |
return ((IBinary)element).getChildren(); |
|
223 |
} else if (element instanceof IArchive) { |
|
224 |
return ((IArchive)element).getChildren(); |
|
225 |
} else if (element instanceof IBinaryModule) { |
|
226 |
return ((IBinaryModule)element).getChildren(); |
|
227 |
} else if (element instanceof INamespace) { |
|
228 |
return getNamespaceChildren((INamespace) element); |
|
229 |
} else if (element instanceof ISourceReference && element instanceof IParent) { |
|
230 |
return ((IParent)element).getChildren(); |
|
231 |
} else if (element instanceof IProject) { |
|
232 |
return getResources((IProject)element); |
|
233 |
} else if (element instanceof IFolder) { |
|
234 |
return getResources((IFolder)element); |
|
235 |
} else if (element instanceof CElementGrouping) { |
|
236 |
return ((CElementGrouping)element).getChildren(element); |
|
237 |
} |
|
238 |
} catch (CModelException e) { |
|
239 |
//CUIPlugin.log(e); |
|
240 |
return NO_CHILDREN; |
|
241 |
} |
|
242 |
return NO_CHILDREN; |
|
243 |
} |
|
244 |
||
245 |
/* (non-Cdoc) |
|
246 |
* |
|
247 |
* @see ITreeContentProvider |
|
248 |
*/ |
|
249 |
public boolean hasChildren(Object element) { |
|
250 |
if (fProvideMembers) { |
|
251 |
// assume TUs and binary files are never empty |
|
252 |
if (element instanceof IBinary || element instanceof ITranslationUnit || element instanceof IArchive) { |
|
253 |
return true; |
|
254 |
} |
|
255 |
} else { |
|
256 |
// don't allow to drill down into a compilation unit or class file |
|
257 |
if (element instanceof ITranslationUnit || element instanceof IBinary || element instanceof IArchive |
|
258 |
|| element instanceof IFile) { |
|
259 |
return false; |
|
260 |
} |
|
261 |
} |
|
262 |
||
263 |
if (element instanceof ICProject) { |
|
264 |
ICProject cp= (ICProject)element; |
|
265 |
if (!cp.getProject().isOpen()) { |
|
266 |
return false; |
|
267 |
} |
|
268 |
return true; |
|
269 |
} |
|
270 |
||
271 |
if (element instanceof ICContainer) { |
|
272 |
ICContainer container= (ICContainer)element; |
|
273 |
IResource resource= container.getResource(); |
|
274 |
if (resource instanceof IContainer) { |
|
275 |
try { |
|
276 |
return ((IContainer)resource).members().length > 0; |
|
277 |
} catch (CoreException exc) { |
|
278 |
return false; |
|
279 |
} |
|
280 |
} |
|
281 |
} |
|
282 |
||
283 |
if (element instanceof IParent) { |
|
284 |
// when we have C children return true, else we fetch all the children |
|
285 |
if (((IParent)element).hasChildren()) { |
|
286 |
return true; |
|
287 |
} |
|
288 |
} |
|
289 |
||
290 |
if (element instanceof CElementGrouping) { |
|
291 |
return true; |
|
292 |
} |
|
293 |
||
294 |
Object[] children= getChildren(element); |
|
295 |
return (children != null) && children.length > 0; |
|
296 |
} |
|
297 |
||
298 |
/* (non-Cdoc) |
|
299 |
* Method declared on ITreeContentProvider. |
|
300 |
*/ |
|
301 |
public Object getParent(Object element) { |
|
302 |
if (!exists(element)) { |
|
303 |
return null; |
|
304 |
} |
|
305 |
return internalGetParent(element); |
|
306 |
} |
|
307 |
||
308 |
public Object internalGetParent(Object element) { |
|
309 |
if (element instanceof IResource) { |
|
310 |
IResource parent= ((IResource)element).getParent(); |
|
311 |
if (parent != null && parent.isAccessible()) { |
|
312 |
ICElement cParent= CoreModel.getDefault().create(parent); |
|
313 |
if (cParent != null && cParent.exists()) { |
|
314 |
return cParent; |
|
315 |
} |
|
316 |
} |
|
317 |
return parent; |
|
318 |
} |
|
319 |
Object parent = null; |
|
320 |
if (element instanceof ICElement) { |
|
321 |
parent = ((ICElement)element).getParent(); |
|
322 |
// translate working copy parent to original TU, |
|
323 |
// because working copies are never returned by getChildren |
|
324 |
// this is necessary for proper show-in-target support |
|
325 |
if (parent instanceof IWorkingCopy) { |
|
326 |
parent= ((IWorkingCopy)parent).getOriginalElement(); |
|
327 |
} |
|
328 |
} else if (element instanceof IWorkbenchAdapter) { |
|
329 |
parent = ((IWorkbenchAdapter)element).getParent(element); |
|
330 |
} |
|
331 |
||
332 |
// if the parent is the default ISourceRoot == ICProject return the project |
|
333 |
if (parent instanceof ISourceRoot) { |
|
334 |
if (isProjectSourceRoot((ISourceRoot)parent)) { |
|
335 |
parent = ((ISourceRoot)parent).getCProject(); |
|
336 |
} |
|
337 |
} else if (parent instanceof IBinaryContainer || parent instanceof IArchiveContainer) { |
|
338 |
// If the virtual container is the parent we must find the legitimate parent. |
|
339 |
if (element instanceof ICElement) { |
|
340 |
IResource res = ((ICElement)element).getResource(); |
|
341 |
if (res != null) { |
|
342 |
parent = internalGetParent(res); |
|
343 |
} |
|
344 |
} |
|
345 |
} |
|
346 |
if (parent instanceof INamespace && fNamespacesGrouping) { |
|
347 |
final INamespace namespace = (INamespace)parent; |
|
348 |
final NamespacesGrouping grouping = new NamespacesGrouping(namespace.getTranslationUnit(), namespace, fMemberGrouping); |
|
349 |
if (grouping.getNamespaces().length > 2) { |
|
350 |
parent = grouping; |
|
351 |
} |
|
352 |
} |
|
353 |
if (parent instanceof IMember && fMemberGrouping) { |
|
354 |
final IMember member = (IMember)parent; |
|
355 |
final String ns = getElementNamespace(member); |
|
356 |
if (ns != null) { |
|
357 |
Object parentParent = member.getParent(); |
|
358 |
if (parentParent instanceof INamespace && fNamespacesGrouping) { |
|
359 |
final INamespace namespace = (INamespace)parent; |
|
360 |
final NamespacesGrouping grouping = new NamespacesGrouping(namespace.getTranslationUnit(), namespace); |
|
361 |
if (grouping.getNamespaces().length > 2) { |
|
362 |
parentParent = grouping; |
|
363 |
} |
|
364 |
} |
|
365 |
return new MembersGrouping(parentParent, ns); |
|
366 |
} |
|
367 |
} |
|
368 |
// if we are doing grouping for the includes return the grouping container. |
|
369 |
if (element instanceof IInclude && fIncludesGrouping) { |
|
370 |
parent = new IncludesGrouping(((IInclude)element).getTranslationUnit()); |
|
371 |
} |
|
372 |
return parent; |
|
373 |
} |
|
374 |
||
375 |
protected Object[] getCProjects(ICModel cModel) throws CModelException { |
|
376 |
Object[] objects = cModel.getCProjects(); |
|
377 |
try { |
|
378 |
Object[] nonC = cModel.getNonCResources(); |
|
379 |
if (nonC.length > 0) { |
|
380 |
objects = concatenate(objects, nonC); |
|
381 |
} |
|
382 |
} catch (CModelException e) { |
|
383 |
// |
|
384 |
} |
|
385 |
return objects; |
|
386 |
} |
|
387 |
||
388 |
protected Object[] getSourceRoots(ICProject cproject) throws CModelException { |
|
389 |
if (!cproject.getProject().isOpen()) |
|
390 |
return NO_CHILDREN; |
|
391 |
||
392 |
List<ICElement> list= new ArrayList<ICElement>(); |
|
393 |
ICElement[] children = cproject.getChildren(); |
|
394 |
for (int i= 0; i < children.length; i++) { |
|
395 |
ICElement child = children[i]; |
|
396 |
if (child instanceof ISourceRoot && child.getResource().getType() == IResource.PROJECT) { |
|
397 |
// Was a source root at the project, get the children of this element |
|
398 |
ICElement[] c2 = ((ISourceRoot)child).getChildren(); |
|
399 |
for (int k = 0; k < c2.length; ++k) |
|
400 |
list.add(c2[k]); |
|
52
42077b7eab6e
Second merge to proper cdt_6_0 branch for CDT bugs 181020, 279844, & Carbide bugs 6152 & 9031.
timkelly
parents:
51
diff
changeset
|
401 |
} else if (CCorePlugin.showSourceRootsAtTopOfProject()) { |
37 | 402 |
list.add(child); |
52
42077b7eab6e
Second merge to proper cdt_6_0 branch for CDT bugs 181020, 279844, & Carbide bugs 6152 & 9031.
timkelly
parents:
51
diff
changeset
|
403 |
} else if (child instanceof ISourceRoot && |
42077b7eab6e
Second merge to proper cdt_6_0 branch for CDT bugs 181020, 279844, & Carbide bugs 6152 & 9031.
timkelly
parents:
51
diff
changeset
|
404 |
child.getResource().getParent().equals(cproject.getProject())) { |
42077b7eab6e
Second merge to proper cdt_6_0 branch for CDT bugs 181020, 279844, & Carbide bugs 6152 & 9031.
timkelly
parents:
51
diff
changeset
|
405 |
list.add(child); |
42077b7eab6e
Second merge to proper cdt_6_0 branch for CDT bugs 181020, 279844, & Carbide bugs 6152 & 9031.
timkelly
parents:
51
diff
changeset
|
406 |
} |
37 | 407 |
} |
408 |
||
409 |
Object[] objects = list.toArray(); |
|
410 |
Object[] nonC = cproject.getNonCResources(); |
|
411 |
if (nonC != null && nonC.length > 0) { |
|
412 |
nonC = filterNonCResources(nonC, cproject); |
|
413 |
objects = concatenate(objects, nonC); |
|
414 |
} |
|
415 |
||
416 |
return objects; |
|
417 |
} |
|
418 |
||
419 |
protected Object[] getTranslationUnitChildren(ITranslationUnit unit) throws CModelException { |
|
420 |
Object[] children = unit.getChildren(); |
|
421 |
if (fIncludesGrouping) { |
|
422 |
boolean hasInclude = false; |
|
423 |
ArrayList<Object> list = new ArrayList<Object>(children.length); |
|
424 |
for (int i = 0; i < children.length; i++) { |
|
425 |
if (!(children[i] instanceof IInclude)) { |
|
426 |
list.add(children[i]); |
|
427 |
} else { |
|
428 |
hasInclude = true; |
|
429 |
} |
|
430 |
} |
|
431 |
if (hasInclude) { |
|
432 |
list.add (0, new IncludesGrouping(unit)); |
|
433 |
} |
|
434 |
children = list.toArray(); |
|
435 |
} |
|
436 |
Map<String, NamespacesGrouping> nsmap = new HashMap<String, NamespacesGrouping>(); |
|
437 |
if (fNamespacesGrouping) { |
|
438 |
// check if there is another namespace with the same name for the same parent |
|
439 |
List<Object> list = new ArrayList<Object>(children.length); |
|
440 |
for (int i = 0; i < children.length; ++i) { |
|
441 |
if (children[i] instanceof INamespace) { |
|
442 |
INamespace n1 = (INamespace)children[i]; |
|
443 |
NamespacesGrouping namespacesGrouping = nsmap.get(n1.getElementName()); |
|
444 |
if (namespacesGrouping == null) { |
|
445 |
namespacesGrouping = new NamespacesGrouping(unit, n1, fMemberGrouping); |
|
446 |
if (namespacesGrouping.getNamespaces().length > 1) { |
|
447 |
nsmap.put(n1.getElementName(), namespacesGrouping); |
|
448 |
list.add(namespacesGrouping); |
|
449 |
} else { |
|
450 |
list.add(children[i]); |
|
451 |
} |
|
452 |
} |
|
453 |
} else { |
|
454 |
list.add(children[i]); |
|
455 |
} |
|
456 |
} |
|
457 |
children = list.toArray(); |
|
458 |
} |
|
459 |
if (fMemberGrouping) { |
|
460 |
// check if there is another member with the same namespace for the same parent |
|
461 |
List<Object> list = new ArrayList<Object>(children.length); |
|
462 |
Map<String, MembersGrouping> map = new HashMap<String, MembersGrouping>(); |
|
463 |
for (int i = 0; i < children.length; ++i) { |
|
464 |
if (children[i] instanceof IMember) { |
|
465 |
final ICElement member = (ICElement)children[i]; |
|
466 |
String namespace = getElementNamespace(member); |
|
467 |
MembersGrouping memberGrouping = map.get(namespace); |
|
468 |
if (memberGrouping == null) { |
|
469 |
memberGrouping = new MembersGrouping(unit, namespace); |
|
470 |
map.put(namespace, memberGrouping); |
|
471 |
list.add(memberGrouping); |
|
472 |
} |
|
473 |
} else if (fNamespacesGrouping && children[i] instanceof INamespace) { |
|
474 |
if (!nsmap.containsKey(((INamespace) children[i]).getElementName())) { |
|
475 |
list.add(children[i]); |
|
476 |
} |
|
477 |
} else { |
|
478 |
list.add(children[i]); |
|
479 |
} |
|
480 |
} |
|
481 |
children = list.toArray(); |
|
482 |
} |
|
483 |
return children; |
|
484 |
} |
|
485 |
||
486 |
protected Object[] getNamespaceChildren(IParent element) throws CModelException { |
|
487 |
Object[] children = element.getChildren(); |
|
488 |
if (fMemberGrouping) { |
|
489 |
// check if there is another member with the same namespace for the same parent |
|
490 |
List<Object> list = new ArrayList<Object>(children.length); |
|
491 |
Map<String, MembersGrouping> map = new HashMap<String, MembersGrouping>(); |
|
492 |
for (int i = 0; i < children.length; ++i) { |
|
493 |
if (children[i] instanceof IMember) { |
|
494 |
final ICElement member = (ICElement)children[i]; |
|
495 |
String namespace = getElementNamespace(member); |
|
496 |
MembersGrouping memberGrouping = map.get(namespace); |
|
497 |
if (memberGrouping == null) { |
|
498 |
memberGrouping = new MembersGrouping(element, namespace); |
|
499 |
map.put(namespace, memberGrouping); |
|
500 |
list.add(memberGrouping); |
|
501 |
} |
|
502 |
} else { |
|
503 |
list.add(children[i]); |
|
504 |
} |
|
505 |
} |
|
506 |
children = list.toArray(); |
|
507 |
} |
|
508 |
return children; |
|
509 |
} |
|
510 |
||
511 |
private static String getElementNamespace(ICElement member) { |
|
512 |
String name = member.getElementName(); |
|
513 |
int idx = name.lastIndexOf("::"); //$NON-NLS-1$ |
|
514 |
if (idx < 0) { |
|
515 |
return null; |
|
516 |
} |
|
517 |
return name.substring(0, idx); |
|
518 |
} |
|
519 |
||
520 |
protected Object[] getCResources(ICContainer container) throws CModelException { |
|
521 |
Object[] objects = null; |
|
52
42077b7eab6e
Second merge to proper cdt_6_0 branch for CDT bugs 181020, 279844, & Carbide bugs 6152 & 9031.
timkelly
parents:
51
diff
changeset
|
522 |
ICElement[] children = container.getChildren(); |
42077b7eab6e
Second merge to proper cdt_6_0 branch for CDT bugs 181020, 279844, & Carbide bugs 6152 & 9031.
timkelly
parents:
51
diff
changeset
|
523 |
List<ICElement> missingElements = Collections.emptyList(); |
42077b7eab6e
Second merge to proper cdt_6_0 branch for CDT bugs 181020, 279844, & Carbide bugs 6152 & 9031.
timkelly
parents:
51
diff
changeset
|
524 |
if (!CCorePlugin.showSourceRootsAtTopOfProject()) { |
42077b7eab6e
Second merge to proper cdt_6_0 branch for CDT bugs 181020, 279844, & Carbide bugs 6152 & 9031.
timkelly
parents:
51
diff
changeset
|
525 |
missingElements = getMissingElements(container, children); |
42077b7eab6e
Second merge to proper cdt_6_0 branch for CDT bugs 181020, 279844, & Carbide bugs 6152 & 9031.
timkelly
parents:
51
diff
changeset
|
526 |
} |
37 | 527 |
try { |
528 |
objects = container.getNonCResources(); |
|
529 |
if (objects.length > 0) { |
|
530 |
objects = filterNonCResources(objects, container.getCProject()); |
|
531 |
} |
|
532 |
} catch (CModelException e) { |
|
533 |
} |
|
534 |
if (objects == null || objects.length == 0) { |
|
535 |
return children; |
|
536 |
} |
|
52
42077b7eab6e
Second merge to proper cdt_6_0 branch for CDT bugs 181020, 279844, & Carbide bugs 6152 & 9031.
timkelly
parents:
51
diff
changeset
|
537 |
Object[] result = concatenate(children, objects); |
42077b7eab6e
Second merge to proper cdt_6_0 branch for CDT bugs 181020, 279844, & Carbide bugs 6152 & 9031.
timkelly
parents:
51
diff
changeset
|
538 |
return concatenate(result, missingElements.toArray()); |
42077b7eab6e
Second merge to proper cdt_6_0 branch for CDT bugs 181020, 279844, & Carbide bugs 6152 & 9031.
timkelly
parents:
51
diff
changeset
|
539 |
} |
42077b7eab6e
Second merge to proper cdt_6_0 branch for CDT bugs 181020, 279844, & Carbide bugs 6152 & 9031.
timkelly
parents:
51
diff
changeset
|
540 |
|
42077b7eab6e
Second merge to proper cdt_6_0 branch for CDT bugs 181020, 279844, & Carbide bugs 6152 & 9031.
timkelly
parents:
51
diff
changeset
|
541 |
private List<ICElement> getMissingElements(ICContainer container, ICElement[] elements) { |
42077b7eab6e
Second merge to proper cdt_6_0 branch for CDT bugs 181020, 279844, & Carbide bugs 6152 & 9031.
timkelly
parents:
51
diff
changeset
|
542 |
// nested source roots may be filtered out below the project root, |
42077b7eab6e
Second merge to proper cdt_6_0 branch for CDT bugs 181020, 279844, & Carbide bugs 6152 & 9031.
timkelly
parents:
51
diff
changeset
|
543 |
// we need to find them to add them back in |
42077b7eab6e
Second merge to proper cdt_6_0 branch for CDT bugs 181020, 279844, & Carbide bugs 6152 & 9031.
timkelly
parents:
51
diff
changeset
|
544 |
List<ICElement> missingElements = new ArrayList<ICElement>(); |
42077b7eab6e
Second merge to proper cdt_6_0 branch for CDT bugs 181020, 279844, & Carbide bugs 6152 & 9031.
timkelly
parents:
51
diff
changeset
|
545 |
try { |
42077b7eab6e
Second merge to proper cdt_6_0 branch for CDT bugs 181020, 279844, & Carbide bugs 6152 & 9031.
timkelly
parents:
51
diff
changeset
|
546 |
List<IResource> missingContainers = new ArrayList<IResource>(); |
42077b7eab6e
Second merge to proper cdt_6_0 branch for CDT bugs 181020, 279844, & Carbide bugs 6152 & 9031.
timkelly
parents:
51
diff
changeset
|
547 |
IResource[] allChildren = ((IContainer) container.getResource()).members(); |
42077b7eab6e
Second merge to proper cdt_6_0 branch for CDT bugs 181020, 279844, & Carbide bugs 6152 & 9031.
timkelly
parents:
51
diff
changeset
|
548 |
for (IResource child : allChildren) { |
42077b7eab6e
Second merge to proper cdt_6_0 branch for CDT bugs 181020, 279844, & Carbide bugs 6152 & 9031.
timkelly
parents:
51
diff
changeset
|
549 |
if (!(child instanceof IContainer)) |
42077b7eab6e
Second merge to proper cdt_6_0 branch for CDT bugs 181020, 279844, & Carbide bugs 6152 & 9031.
timkelly
parents:
51
diff
changeset
|
550 |
continue; |
42077b7eab6e
Second merge to proper cdt_6_0 branch for CDT bugs 181020, 279844, & Carbide bugs 6152 & 9031.
timkelly
parents:
51
diff
changeset
|
551 |
boolean found = false; |
42077b7eab6e
Second merge to proper cdt_6_0 branch for CDT bugs 181020, 279844, & Carbide bugs 6152 & 9031.
timkelly
parents:
51
diff
changeset
|
552 |
for (ICElement element : elements) { |
42077b7eab6e
Second merge to proper cdt_6_0 branch for CDT bugs 181020, 279844, & Carbide bugs 6152 & 9031.
timkelly
parents:
51
diff
changeset
|
553 |
if (element.getResource().equals(child)) { |
42077b7eab6e
Second merge to proper cdt_6_0 branch for CDT bugs 181020, 279844, & Carbide bugs 6152 & 9031.
timkelly
parents:
51
diff
changeset
|
554 |
found = true; |
42077b7eab6e
Second merge to proper cdt_6_0 branch for CDT bugs 181020, 279844, & Carbide bugs 6152 & 9031.
timkelly
parents:
51
diff
changeset
|
555 |
break; |
42077b7eab6e
Second merge to proper cdt_6_0 branch for CDT bugs 181020, 279844, & Carbide bugs 6152 & 9031.
timkelly
parents:
51
diff
changeset
|
556 |
} |
42077b7eab6e
Second merge to proper cdt_6_0 branch for CDT bugs 181020, 279844, & Carbide bugs 6152 & 9031.
timkelly
parents:
51
diff
changeset
|
557 |
} |
42077b7eab6e
Second merge to proper cdt_6_0 branch for CDT bugs 181020, 279844, & Carbide bugs 6152 & 9031.
timkelly
parents:
51
diff
changeset
|
558 |
if (!found) |
42077b7eab6e
Second merge to proper cdt_6_0 branch for CDT bugs 181020, 279844, & Carbide bugs 6152 & 9031.
timkelly
parents:
51
diff
changeset
|
559 |
missingContainers.add(child); |
42077b7eab6e
Second merge to proper cdt_6_0 branch for CDT bugs 181020, 279844, & Carbide bugs 6152 & 9031.
timkelly
parents:
51
diff
changeset
|
560 |
} |
42077b7eab6e
Second merge to proper cdt_6_0 branch for CDT bugs 181020, 279844, & Carbide bugs 6152 & 9031.
timkelly
parents:
51
diff
changeset
|
561 |
for (IResource resource : missingContainers) { |
42077b7eab6e
Second merge to proper cdt_6_0 branch for CDT bugs 181020, 279844, & Carbide bugs 6152 & 9031.
timkelly
parents:
51
diff
changeset
|
562 |
ICElement element = container.getCProject().findElement(resource.getFullPath()); |
42077b7eab6e
Second merge to proper cdt_6_0 branch for CDT bugs 181020, 279844, & Carbide bugs 6152 & 9031.
timkelly
parents:
51
diff
changeset
|
563 |
if (element != null) |
42077b7eab6e
Second merge to proper cdt_6_0 branch for CDT bugs 181020, 279844, & Carbide bugs 6152 & 9031.
timkelly
parents:
51
diff
changeset
|
564 |
missingElements.add(element); |
42077b7eab6e
Second merge to proper cdt_6_0 branch for CDT bugs 181020, 279844, & Carbide bugs 6152 & 9031.
timkelly
parents:
51
diff
changeset
|
565 |
} |
42077b7eab6e
Second merge to proper cdt_6_0 branch for CDT bugs 181020, 279844, & Carbide bugs 6152 & 9031.
timkelly
parents:
51
diff
changeset
|
566 |
} catch (CoreException e1) { |
42077b7eab6e
Second merge to proper cdt_6_0 branch for CDT bugs 181020, 279844, & Carbide bugs 6152 & 9031.
timkelly
parents:
51
diff
changeset
|
567 |
} |
42077b7eab6e
Second merge to proper cdt_6_0 branch for CDT bugs 181020, 279844, & Carbide bugs 6152 & 9031.
timkelly
parents:
51
diff
changeset
|
568 |
return missingElements; |
37 | 569 |
} |
570 |
||
571 |
protected Object[] getResources(IProject project) { |
|
572 |
try { |
|
573 |
return project.members(); |
|
574 |
} catch (CoreException e) { |
|
575 |
} |
|
576 |
return NO_CHILDREN; |
|
577 |
} |
|
578 |
||
579 |
protected Object[] getResources(IFolder folder) throws CModelException { |
|
580 |
ICProject cproject = CoreModel.getDefault().create(folder.getProject()); |
|
581 |
Object[] members = null; |
|
582 |
try { |
|
583 |
members = folder.members(); |
|
584 |
} catch (CoreException e) { |
|
585 |
// |
|
586 |
} |
|
587 |
if (members == null || members.length == 0) { |
|
588 |
return NO_CHILDREN; |
|
589 |
} |
|
590 |
return filterNonCResources(members, cproject); |
|
591 |
} |
|
592 |
||
593 |
private Object[] filterNonCResources(Object[] objects, ICProject cproject) throws CModelException { |
|
594 |
ICElement[] binaries = null; |
|
595 |
ICElement[] archives = null; |
|
596 |
try { |
|
597 |
binaries = getBinaries(cproject); |
|
598 |
archives = getArchives(cproject); |
|
599 |
} catch (CModelException e) { |
|
600 |
archives = binaries = new ICElement[0]; |
|
601 |
} |
|
602 |
ISourceRoot[] roots = null; |
|
603 |
try { |
|
604 |
roots = cproject.getSourceRoots(); |
|
605 |
} catch (CModelException e) { |
|
606 |
roots = new ISourceRoot[0]; |
|
607 |
} |
|
608 |
List<Object> nonCResources = new ArrayList<Object>(objects.length); |
|
609 |
for (int i= 0; i < objects.length; i++) { |
|
610 |
Object o= objects[i]; |
|
611 |
// A folder can also be a source root in the following case |
|
612 |
// Project |
|
613 |
// + src <- source folder |
|
614 |
// + excluded <- excluded from class path |
|
615 |
// + included <- a new source folder. |
|
616 |
// Included is a member of excluded, but since it is rendered as a source |
|
617 |
// folder we have to exclude it as a normal child. |
|
618 |
if (o instanceof IFolder) { |
|
619 |
IFolder folder = (IFolder)o; |
|
52
42077b7eab6e
Second merge to proper cdt_6_0 branch for CDT bugs 181020, 279844, & Carbide bugs 6152 & 9031.
timkelly
parents:
51
diff
changeset
|
620 |
ISourceRoot root = null; |
37 | 621 |
for (int j = 0; j < roots.length; j++) { |
622 |
if (roots[j].getPath().equals(folder.getFullPath())) { |
|
52
42077b7eab6e
Second merge to proper cdt_6_0 branch for CDT bugs 181020, 279844, & Carbide bugs 6152 & 9031.
timkelly
parents:
51
diff
changeset
|
623 |
root = roots[j]; |
37 | 624 |
break; |
625 |
} |
|
626 |
} |
|
627 |
// it is a sourceRoot skip it. |
|
52
42077b7eab6e
Second merge to proper cdt_6_0 branch for CDT bugs 181020, 279844, & Carbide bugs 6152 & 9031.
timkelly
parents:
51
diff
changeset
|
628 |
if (root != null) { |
42077b7eab6e
Second merge to proper cdt_6_0 branch for CDT bugs 181020, 279844, & Carbide bugs 6152 & 9031.
timkelly
parents:
51
diff
changeset
|
629 |
if (CCorePlugin.showSourceRootsAtTopOfProject()) |
42077b7eab6e
Second merge to proper cdt_6_0 branch for CDT bugs 181020, 279844, & Carbide bugs 6152 & 9031.
timkelly
parents:
51
diff
changeset
|
630 |
continue; |
42077b7eab6e
Second merge to proper cdt_6_0 branch for CDT bugs 181020, 279844, & Carbide bugs 6152 & 9031.
timkelly
parents:
51
diff
changeset
|
631 |
else |
42077b7eab6e
Second merge to proper cdt_6_0 branch for CDT bugs 181020, 279844, & Carbide bugs 6152 & 9031.
timkelly
parents:
51
diff
changeset
|
632 |
o = root; |
37 | 633 |
} |
634 |
} else if (o instanceof IFile){ |
|
635 |
boolean found = false; |
|
636 |
for (int j = 0; j < binaries.length; j++) { |
|
637 |
IResource res = binaries[j].getResource(); |
|
638 |
if (o.equals(res)) { |
|
639 |
o = binaries[j]; |
|
640 |
found = true; |
|
641 |
break; |
|
642 |
} |
|
643 |
} |
|
644 |
if (!found) { |
|
645 |
for (int j = 0; j < archives.length; j++) { |
|
646 |
IResource res = archives[j].getResource(); |
|
647 |
if (o.equals(res)) { |
|
648 |
o = archives[j]; |
|
649 |
break; |
|
650 |
} |
|
651 |
} |
|
652 |
} |
|
653 |
} |
|
654 |
nonCResources.add(o); |
|
655 |
} |
|
656 |
return nonCResources.toArray(); |
|
657 |
} |
|
658 |
||
659 |
/** |
|
660 |
* Note: This method is for internal use only. Clients should not call this method. |
|
661 |
*/ |
|
662 |
protected boolean isProjectSourceRoot(ISourceRoot root) { |
|
663 |
IResource resource= root.getResource(); |
|
664 |
return (resource instanceof IProject); |
|
665 |
} |
|
666 |
||
667 |
protected boolean exists(Object element) { |
|
668 |
if (element == null) { |
|
669 |
return false; |
|
670 |
} |
|
671 |
if (element instanceof IResource) { |
|
672 |
return ((IResource)element).exists(); |
|
673 |
} |
|
674 |
if (element instanceof ICElement) { |
|
675 |
return ((ICElement)element).exists(); |
|
676 |
} |
|
677 |
return true; |
|
678 |
} |
|
679 |
||
680 |
protected IBinary[] getBinaries(ICProject cproject) throws CModelException { |
|
681 |
IBinaryContainer container = cproject.getBinaryContainer(); |
|
682 |
return getBinaries(container); |
|
683 |
} |
|
684 |
||
685 |
protected IBinary[] getBinaries(IBinaryContainer container) throws CModelException { |
|
686 |
ICElement[] celements = container.getChildren(); |
|
687 |
ArrayList<IBinary> list = new ArrayList<IBinary>(celements.length); |
|
688 |
for (int i = 0; i < celements.length; i++) { |
|
689 |
if (celements[i] instanceof IBinary) { |
|
690 |
IBinary bin = (IBinary)celements[i]; |
|
691 |
list.add(bin); |
|
692 |
} |
|
693 |
} |
|
694 |
IBinary[] bins = new IBinary[list.size()]; |
|
695 |
list.toArray(bins); |
|
696 |
return bins; |
|
697 |
} |
|
698 |
||
699 |
protected IArchive[] getArchives(ICProject cproject) throws CModelException { |
|
700 |
IArchiveContainer container = cproject.getArchiveContainer(); |
|
701 |
return getArchives(container); |
|
702 |
} |
|
703 |
||
704 |
protected IArchive[] getArchives(IArchiveContainer container) throws CModelException { |
|
705 |
ICElement[] celements = container.getChildren(); |
|
706 |
ArrayList<IArchive> list = new ArrayList<IArchive>(celements.length); |
|
707 |
for (int i = 0; i < celements.length; i++) { |
|
708 |
if (celements[i] instanceof IArchive) { |
|
709 |
IArchive ar = (IArchive)celements[i]; |
|
710 |
list.add(ar); |
|
711 |
} |
|
712 |
} |
|
713 |
IArchive[] ars = new IArchive[list.size()]; |
|
714 |
list.toArray(ars); |
|
715 |
return ars; |
|
716 |
} |
|
717 |
||
718 |
/** |
|
719 |
* Note: This method is for internal use only. Clients should not call this method. |
|
720 |
*/ |
|
721 |
protected static Object[] concatenate(Object[] a1, Object[] a2) { |
|
722 |
int a1Len = a1.length; |
|
723 |
int a2Len = a2.length; |
|
724 |
Object[] res = new Object[a1Len + a2Len]; |
|
725 |
System.arraycopy(a1, 0, res, 0, a1Len); |
|
726 |
System.arraycopy(a2, 0, res, a1Len, a2Len); |
|
727 |
return res; |
|
728 |
} |
|
729 |
||
730 |
} |