Merge CDT bug 181020 (from CDT patch, not from patch from Carbide Bug 9031)
authortimkelly
Thu, 30 Jul 2009 14:41:10 -0500
changeset 44 b0f794238767
parent 43 3c5a41c680a8
child 45 70f756c14af3
Merge CDT bug 181020 (from CDT patch, not from patch from Carbide Bug 9031)
cdt/cdt_6_0_x/org.eclipse.cdt.core/src/org/eclipse/cdt/core/CCorePlugin.java
cdt/cdt_6_0_x/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/BaseCElementContentProvider.java
cdt/cdt_6_0_x/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/AppearancePreferencePage.java
cdt/cdt_6_0_x/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/viewsupport/CElementImageProvider.java
--- a/cdt/cdt_6_0_x/org.eclipse.cdt.core/src/org/eclipse/cdt/core/CCorePlugin.java	Thu Jul 30 14:34:06 2009 -0500
+++ b/cdt/cdt_6_0_x/org.eclipse.cdt.core/src/org/eclipse/cdt/core/CCorePlugin.java	Thu Jul 30 14:41:10 2009 -0500
@@ -1170,15 +1170,7 @@
 	public static IUserVarSupplier getUserVarSupplier() {
 		return UserVarSupplier.getInstance();
 	}
-	/**
-	 * Returns preference controlling whether source roots are shown at the top of projects
-	 * or embedded within the resource tree of projects when they are not top level folders.
-	 * 
-	 * @return boolean preference value
-	 */
-	public static boolean showSourceRootsAtTopOfProject() {
-		return getDefault().getPluginPreferences().getBoolean( CCorePreferenceConstants.SHOW_SOURCE_ROOTS_AT_TOP_LEVEL_OF_PROJECT) == true;
-	}
+	
 	// NON-API
 
 	/**
@@ -1275,4 +1267,14 @@
 	public static IPDOMManager getPDOMManager() {
 		return getDefault().pdomManager;
 	}
+
+	/**
+	 * Returns preference controlling whether source roots are shown at the top of projects
+	 * or embedded within the resource tree of projects when they are not top level folders.
+	 * 
+	 * @return boolean preference value
+	 */
+	public static boolean showSourceRootsAtTopOfProject() {
+		return getDefault().getPluginPreferences().getBoolean( CCorePreferenceConstants.SHOW_SOURCE_ROOTS_AT_TOP_LEVEL_OF_PROJECT);
+	}
 }
\ No newline at end of file
--- a/cdt/cdt_6_0_x/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/BaseCElementContentProvider.java	Thu Jul 30 14:34:06 2009 -0500
+++ b/cdt/cdt_6_0_x/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/BaseCElementContentProvider.java	Thu Jul 30 14:41:10 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;
@@ -390,7 +391,6 @@
 			
 		List<ICElement> list= new ArrayList<ICElement>();
 		ICElement[] children = cproject.getChildren();
-
 		for (int i= 0; i < children.length; i++) {
 			ICElement child = children[i];
 			if (child instanceof ISourceRoot && child.getResource().getType() == IResource.PROJECT) {
@@ -519,7 +519,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) {
@@ -530,7 +534,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_6_0_x/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/AppearancePreferencePage.java	Thu Jul 30 14:34:06 2009 -0500
+++ b/cdt/cdt_6_0_x/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/AppearancePreferencePage.java	Thu Jul 30 14:41:10 2009 -0500
@@ -28,7 +28,6 @@
 import org.eclipse.ui.IWorkbenchPreferencePage;
 import org.eclipse.ui.PlatformUI;
 import org.osgi.service.prefs.BackingStoreException;
-import org.eclipse.ui.navigator.CommonNavigator;
 
 import org.eclipse.cdt.core.CCorePlugin;
 import org.eclipse.cdt.core.CCorePreferenceConstants;
@@ -50,8 +49,8 @@
 	private SelectionButtonDialogField fOutlineGroupNamespaces;
 	private SelectionButtonDialogField fCViewGroupIncludes;
 	private SelectionButtonDialogField fCViewSeparateHeaderAndSource;
+	private SelectionButtonDialogField fOutlineGroupMembers;
 	private SelectionButtonDialogField fShowSourceRootsAtTopOfProject;
-	private SelectionButtonDialogField fOutlineGroupMembers;
 	
 	public AppearancePreferencePage() {
 		setPreferenceStore(PreferenceConstants.getPreferenceStore());
@@ -101,9 +100,9 @@
 		fCViewSeparateHeaderAndSource.setSelection(prefs.getBoolean(PreferenceConstants.CVIEW_SEPARATE_HEADER_AND_SOURCE));
 		fOutlineGroupIncludes.setSelection(prefs.getBoolean(PreferenceConstants.OUTLINE_GROUP_INCLUDES));
 		fOutlineGroupNamespaces.setSelection(prefs.getBoolean(PreferenceConstants.OUTLINE_GROUP_NAMESPACES));
+		fOutlineGroupMembers.setSelection(prefs.getBoolean(PreferenceConstants.OUTLINE_GROUP_MEMBERS));
 		boolean showSourceRootsAtTopOfProject = CCorePlugin.showSourceRootsAtTopOfProject();
 		fShowSourceRootsAtTopOfProject.setSelection(showSourceRootsAtTopOfProject);
-		fOutlineGroupMembers.setSelection(prefs.getBoolean(PreferenceConstants.OUTLINE_GROUP_MEMBERS));
 	}
 	
 	/*
@@ -139,6 +138,7 @@
 		new Separator().doFillIntoGrid(result, nColumns);
 		
 		fCViewSeparateHeaderAndSource.doFillIntoGrid(result, nColumns);
+		
 		String noteTitle= PreferencesMessages.AppearancePreferencePage_note;
 		String noteMessage= PreferencesMessages.AppearancePreferencePage_preferenceOnlyForNewViews; 
 		Composite noteControl= createNoteComposite(JFaceResources.getDialogFont(), result, noteTitle, noteMessage);
@@ -187,13 +187,13 @@
 		prefs.setValue(PreferenceConstants.OUTLINE_GROUP_INCLUDES, fOutlineGroupIncludes.isSelected());
 		prefs.setValue(PreferenceConstants.OUTLINE_GROUP_NAMESPACES, fOutlineGroupNamespaces.isSelected());
 		prefs.setValue(PreferenceConstants.OUTLINE_GROUP_MEMBERS, fOutlineGroupMembers.isSelected());
-		CCorePlugin.getDefault().getPluginPreferences().setValue(CCorePreferenceConstants.SHOW_SOURCE_ROOTS_AT_TOP_LEVEL_OF_PROJECT, fShowSourceRootsAtTopOfProject.isSelected());
-		CCorePlugin.getDefault().savePluginPreferences();
 		try {
 			new InstanceScope().getNode(CUIPlugin.PLUGIN_ID).flush();
 		} catch (BackingStoreException exc) {
 			CUIPlugin.log(exc);
 		}
+		CCorePlugin.getDefault().getPluginPreferences().setValue(CCorePreferenceConstants.SHOW_SOURCE_ROOTS_AT_TOP_LEVEL_OF_PROJECT, fShowSourceRootsAtTopOfProject.isSelected());
+		CCorePlugin.getDefault().savePluginPreferences();
 		return super.performOk();
 	}
 	
@@ -203,14 +203,14 @@
 	@Override
 	protected void performDefaults() {
 		IPreferenceStore prefs= getPreferenceStore();
-		Preferences corePrefs = CCorePlugin.getDefault().getPluginPreferences();
-		fShowSourceRootsAtTopOfProject.setSelection(corePrefs.getDefaultBoolean(CCorePreferenceConstants.SHOW_SOURCE_ROOTS_AT_TOP_LEVEL_OF_PROJECT));
 		fShowTUChildren.setSelection(prefs.getDefaultBoolean(PreferenceConstants.PREF_SHOW_CU_CHILDREN));
 		fCViewGroupIncludes.setSelection(prefs.getDefaultBoolean(PreferenceConstants.CVIEW_GROUP_INCLUDES));
 		fCViewSeparateHeaderAndSource.setSelection(prefs.getDefaultBoolean(PreferenceConstants.CVIEW_SEPARATE_HEADER_AND_SOURCE));
 		fOutlineGroupIncludes.setSelection(prefs.getDefaultBoolean(PreferenceConstants.OUTLINE_GROUP_INCLUDES));
 		fOutlineGroupNamespaces.setSelection(prefs.getDefaultBoolean(PreferenceConstants.OUTLINE_GROUP_NAMESPACES));
 		fOutlineGroupMembers.setSelection(prefs.getDefaultBoolean(PreferenceConstants.OUTLINE_GROUP_MEMBERS));
+		Preferences corePrefs = CCorePlugin.getDefault().getPluginPreferences();
+		fShowSourceRootsAtTopOfProject.setSelection(corePrefs.getDefaultBoolean(CCorePreferenceConstants.SHOW_SOURCE_ROOTS_AT_TOP_LEVEL_OF_PROJECT));
 		super.performDefaults();
 	}
 }
--- a/cdt/cdt_6_0_x/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/viewsupport/CElementImageProvider.java	Thu Jul 30 14:34:06 2009 -0500
+++ b/cdt/cdt_6_0_x/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/viewsupport/CElementImageProvider.java	Thu Jul 30 14:41:10 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;
@@ -51,7 +50,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;
@@ -128,7 +126,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;
@@ -160,13 +162,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;
 					}
 				}