[Bugs 9201 and 9031] Handle nested source roots correctly
authordadubrow
Fri, 05 Jun 2009 10:43:19 -0500
changeset 18 fad952f32a04
parent 17 09ccf47b690d
child 19 89d3c8eb71dd
child 20 f2052eb98ec7
[Bugs 9201 and 9031] Handle nested source roots correctly
cdt/cdt_5_0_x/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/BaseCElementContentProvider.java
cdt/cdt_5_0_x/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/viewsupport/CElementImageProvider.java
--- a/cdt/cdt_5_0_x/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/BaseCElementContentProvider.java	Wed Jun 03 17:30:37 2009 -0500
+++ b/cdt/cdt_5_0_x/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/BaseCElementContentProvider.java	Fri Jun 05 10:43:19 2009 -0500
@@ -12,6 +12,7 @@
 package org.eclipse.cdt.internal.ui;
 
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -426,7 +427,11 @@
 
 	protected Object[] getCResources(ICContainer container) throws CModelException {
 		Object[] objects = null;
-		Object[] children = container.getChildren();
+		ICElement[] children = container.getChildren();
+		List<ICElement> missingElements = Collections.emptyList();
+		if (!CCorePlugin.showSourceRootsAtTopOfProject()) {
+			missingElements = getMissingElements(container, children);
+		}
 		try {
 			objects = container.getNonCResources();
 			if (objects.length > 0) {
@@ -437,7 +442,38 @@
 		if (objects == null || objects.length == 0) {
 			return children;
 		}
-		return concatenate(children, objects);
+		Object[] result = concatenate(children, objects);
+		return concatenate(result, missingElements.toArray());
+	}
+
+	private List<ICElement> getMissingElements(ICContainer container, ICElement[] elements) {
+		// nested source roots may be filtered out below the project root, 
+		// we need to find them to add them back in
+		List<ICElement> missingElements = new ArrayList<ICElement>();
+		try {
+			List<IResource> missingContainers = new ArrayList<IResource>();
+			IResource[] allChildren = ((IContainer) container.getResource()).members();
+			for (IResource child : allChildren) {
+				if (!(child instanceof IContainer))
+					continue;
+				boolean found = false;
+				for (ICElement element : elements) {
+					if (element.getResource().equals(child)) {
+						found = true;
+						break;
+					}
+				}
+				if (!found)
+					missingContainers.add(child);
+			}
+			for (IResource resource : missingContainers) {
+				ICElement element = container.getCProject().findElement(resource.getFullPath());
+				if (element != null)
+					missingElements.add(element);
+			}
+		} catch (CoreException e1) {
+		}
+		return missingElements;
 	}
 
 	protected Object[] getResources(IProject project) {
--- a/cdt/cdt_5_0_x/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/viewsupport/CElementImageProvider.java	Wed Jun 03 17:30:37 2009 -0500
+++ b/cdt/cdt_5_0_x/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/viewsupport/CElementImageProvider.java	Fri Jun 05 10:43:19 2009 -0500
@@ -16,10 +16,8 @@
 import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.IFolder;
 import org.eclipse.core.resources.IProject;
-import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IAdaptable;
 import org.eclipse.core.runtime.IPath;
-import org.eclipse.core.runtime.Path;
 import org.eclipse.jface.resource.ImageDescriptor;
 import org.eclipse.swt.graphics.Image;
 import org.eclipse.swt.graphics.Point;
@@ -34,6 +32,7 @@
 import org.eclipse.cdt.core.model.IBinary;
 import org.eclipse.cdt.core.model.IBinaryContainer;
 import org.eclipse.cdt.core.model.IBinaryModule;
+import org.eclipse.cdt.core.model.ICContainer;
 import org.eclipse.cdt.core.model.ICElement;
 import org.eclipse.cdt.core.model.ICProject;
 import org.eclipse.cdt.core.model.IContributedCElement;
@@ -50,7 +49,6 @@
 import org.eclipse.cdt.ui.CElementImageDescriptor;
 import org.eclipse.cdt.ui.CUIPlugin;
 
-import org.eclipse.cdt.internal.core.model.CModel;
 import org.eclipse.cdt.internal.core.model.CModelManager;
 
 import org.eclipse.cdt.internal.ui.CPluginImages;
@@ -127,7 +125,11 @@
 	public Image getImageLabel(Object element, int flags) {
 		ImageDescriptor descriptor= null;
 		if (element instanceof ICElement) {
-			descriptor= getCImageDescriptor((ICElement) element, flags);
+			if (!CCorePlugin.showSourceRootsAtTopOfProject() &&
+				element instanceof ICContainer && isParentOfSourceRoot(element))
+				descriptor = CPluginImages.DESC_OBJS_SOURCE2_ROOT;
+			else
+				descriptor= getCImageDescriptor((ICElement) element, flags);
 		} else if (element instanceof IFile) {
 			// Check for Non Translation Unit.
 			IFile file = (IFile)element;
@@ -159,13 +161,23 @@
 	}
 
 	private boolean isParentOfSourceRoot(Object element) {
-		IFolder folder = (IFolder)element;
+		// we want to return true for parents of source roots which are not themselves source roots
+		// so we can distinguish the two and return the source root icon or the parent of source root icon
+		IFolder folder = null;
+		if (element instanceof ICContainer && !(element instanceof ISourceRoot))
+			folder = (IFolder) ((ICContainer) element).getResource();
+		else if (element instanceof IFolder)
+			folder = (IFolder) element;
+		if (folder == null)
+			return false;
+		
 		ICProject cproject = CModelManager.getDefault().getCModel().findCProject(folder.getProject());
 		if (cproject != null) {
 			try {
 				IPath folderPath = folder.getFullPath();
 				for (ICElement sourceRoot : cproject.getSourceRoots()) {
-					if (folderPath.isPrefixOf(sourceRoot.getPath())) {
+					IPath sourceRootPath = sourceRoot.getPath();
+					if (folderPath.isPrefixOf(sourceRootPath)) {
 						return true;
 					}
 				}