From 7d383dd0ce466b110e238df435985cbad9876522 Mon Sep 17 00:00:00 2001 From: Chris Wiebe Date: Thu, 2 Sep 2004 22:00:11 +0000 Subject: [PATCH] 2004-09-02 Chris Wiebe fix editor/view selection problems * browser/org/eclipse/cdt/internal/ui/browser/cbrowsing/CBrowsingElementComparer.java * browser/org/eclipse/cdt/internal/ui/browser/cbrowsing/CBrowsingPart.java * browser/org/eclipse/cdt/internal/ui/browser/cbrowsing/MembersView.java * browser/org/eclipse/cdt/internal/ui/browser/cbrowsing/NamespacesView.java * browser/org/eclipse/cdt/internal/ui/browser/cbrowsing/TypesView.java --- .../browser/ChangeLog-browser | 9 ++ .../cbrowsing/CBrowsingElementComparer.java | 62 ++----------- .../ui/browser/cbrowsing/CBrowsingPart.java | 90 +++++++++---------- .../ui/browser/cbrowsing/MembersView.java | 20 ++--- .../ui/browser/cbrowsing/NamespacesView.java | 47 +++++++--- .../ui/browser/cbrowsing/TypesView.java | 27 ++++-- 6 files changed, 122 insertions(+), 133 deletions(-) diff --git a/core/org.eclipse.cdt.ui/browser/ChangeLog-browser b/core/org.eclipse.cdt.ui/browser/ChangeLog-browser index 20c315a091f..17bd7b94483 100644 --- a/core/org.eclipse.cdt.ui/browser/ChangeLog-browser +++ b/core/org.eclipse.cdt.ui/browser/ChangeLog-browser @@ -1,3 +1,12 @@ +2004-09-02 Chris Wiebe + + fix editor/view selection problems + * browser/org/eclipse/cdt/internal/ui/browser/cbrowsing/CBrowsingElementComparer.java + * browser/org/eclipse/cdt/internal/ui/browser/cbrowsing/CBrowsingPart.java + * browser/org/eclipse/cdt/internal/ui/browser/cbrowsing/MembersView.java + * browser/org/eclipse/cdt/internal/ui/browser/cbrowsing/NamespacesView.java + * browser/org/eclipse/cdt/internal/ui/browser/cbrowsing/TypesView.java + 2004-09-01 Chris Wiebe Fix for 68883 diff --git a/core/org.eclipse.cdt.ui/browser/org/eclipse/cdt/internal/ui/browser/cbrowsing/CBrowsingElementComparer.java b/core/org.eclipse.cdt.ui/browser/org/eclipse/cdt/internal/ui/browser/cbrowsing/CBrowsingElementComparer.java index f4c778b079c..1ca07ad6169 100644 --- a/core/org.eclipse.cdt.ui/browser/org/eclipse/cdt/internal/ui/browser/cbrowsing/CBrowsingElementComparer.java +++ b/core/org.eclipse.cdt.ui/browser/org/eclipse/cdt/internal/ui/browser/cbrowsing/CBrowsingElementComparer.java @@ -10,67 +10,17 @@ *******************************************************************************/ package org.eclipse.cdt.internal.ui.browser.cbrowsing; -import org.eclipse.cdt.core.browser.TypeUtil; -import org.eclipse.cdt.core.model.ICElement; -import org.eclipse.cdt.core.model.ITranslationUnit; -import org.eclipse.cdt.core.model.IWorkingCopy; -import org.eclipse.jface.viewers.IElementComparer; +import org.eclipse.cdt.internal.ui.cview.CViewElementComparer; -public class CBrowsingElementComparer implements IElementComparer { +public class CBrowsingElementComparer extends CViewElementComparer { public boolean equals(Object o1, Object o2) { - if (o1 == o2) // this handles also the case that both are null - return true; - if (o1 == null) - return false; // o2 != null if we reach this point - if (o1.equals(o2)) - return true; - - // Assume they are CElements - ICElement c1= (o1 instanceof ICElement) ? (ICElement)o1 : null; - ICElement c2= (o2 instanceof ICElement) ? (ICElement)o2 : null; - if (c1 == null || c2 == null) - return false; - - // compare identical elements across working copies - if (c1.getElementType() == c2.getElementType() - && c1.getElementName().equals(c2.getElementName())) { - if (TypeUtil.getFullyQualifiedName(c1).equals(TypeUtil.getFullyQualifiedName(c2))) { - return c1.getUnderlyingResource().equals(c2.getUnderlyingResource()); - } - } - - if (c1 instanceof ITranslationUnit) { - ITranslationUnit t1 = (ITranslationUnit)o1; - if (t1.isWorkingCopy()) { - c1 = ((IWorkingCopy)t1).getOriginalElement(); - } - } - if (c2 instanceof ITranslationUnit) { - ITranslationUnit t2 = (ITranslationUnit)o2; - if (t2.isWorkingCopy()) { - c2 = ((IWorkingCopy)t2).getOriginalElement(); - } - } - if (c1 == null || c2 == null) { - return false; - } - return c1.equals(c2); + //TODO compare ITypeInfos + return super.equals(o1, o2); } public int hashCode(Object o1) { - ICElement c1= (o1 instanceof ICElement) ? (ICElement)o1 : null; - if (c1 == null) - return o1.hashCode(); - if (c1 instanceof ITranslationUnit) { - ITranslationUnit t1= (ITranslationUnit)c1; - if (t1.isWorkingCopy()) { - c1= ((IWorkingCopy)t1).getOriginalElement(); - } - } - if (c1 == null) { - return o1.hashCode(); - } - return c1.hashCode(); + //TODO compare ITypeInfos + return super.hashCode(o1); } } diff --git a/core/org.eclipse.cdt.ui/browser/org/eclipse/cdt/internal/ui/browser/cbrowsing/CBrowsingPart.java b/core/org.eclipse.cdt.ui/browser/org/eclipse/cdt/internal/ui/browser/cbrowsing/CBrowsingPart.java index 6a8079aaaa0..72e696182d0 100644 --- a/core/org.eclipse.cdt.ui/browser/org/eclipse/cdt/internal/ui/browser/cbrowsing/CBrowsingPart.java +++ b/core/org.eclipse.cdt.ui/browser/org/eclipse/cdt/internal/ui/browser/cbrowsing/CBrowsingPart.java @@ -27,6 +27,7 @@ import org.eclipse.cdt.core.model.ICModel; import org.eclipse.cdt.core.model.ICProject; import org.eclipse.cdt.core.model.ISourceRoot; import org.eclipse.cdt.core.model.ITranslationUnit; +import org.eclipse.cdt.core.model.IWorkingCopy; import org.eclipse.cdt.core.resources.FileStorage; import org.eclipse.cdt.internal.ui.browser.opentype.OpenTypeMessages; import org.eclipse.cdt.internal.ui.editor.CEditor; @@ -660,24 +661,6 @@ public abstract class CBrowsingPart extends ViewPart implements IMenuListener, I return (resource instanceof IProject); } - protected boolean isValidNamespace(Object element) { - if (element instanceof ITypeInfo) { - ITypeInfo info = (ITypeInfo)element; - if (info.exists() && info.getCElementType() == ICElement.C_NAMESPACE) { - // make sure it has types other than namespaces - ITypeInfo[] types = info.getEnclosedTypes(); - if (types != null) { - for (int i = 0; i < types.length; ++i) { - if (types[i].getCElementType() != ICElement.C_NAMESPACE) { - return true; - } - } - } - } - } - return false; - } - /** * Answers if the given element is a valid * element for this part. @@ -686,20 +669,6 @@ public abstract class CBrowsingPart extends ViewPart implements IMenuListener, I * @return if the given element is a valid element */ abstract protected boolean isValidElement(Object element); -// if (element == null) -// return false; -//// element= getSuitableCElement(element); -//// if (element == null) -//// return false; -// Object input= getViewer().getInput(); -// if (input == null) -// return false; -// if (input instanceof Collection) -// return ((Collection)input).contains(element); -// else -// return input.equals(element); -// -// } private boolean isInputResetBy(Object newInput, Object input, IWorkbenchPart part) { if (newInput == null) @@ -1183,19 +1152,20 @@ public abstract class CBrowsingPart extends ViewPart implements IMenuListener, I } void adjustInputAndSetSelection(Object o) { - if (!(o instanceof ICElement) && !(o instanceof ITypeInfo)) { + Object element = getOriginalElement(o); + if (!(element instanceof ICElement) && !(element instanceof ITypeInfo)) { setSelection(StructuredSelection.EMPTY, true); return; } - - Object elementToSelect= getSuitableElement(findElementToSelect(o)); - Object newInput= findInputForElement(o); + + Object elementToSelect= getSuitableElement(findElementToSelect(element)); + Object newInput= findInputForElement(element); Object oldInput= null; Object viewerInput = getInput(); if (viewerInput instanceof ICElement || viewerInput instanceof ITypeInfo) oldInput = viewerInput; - if (elementToSelect == null && !isValidInput(newInput) && (newInput == null && !isAncestorOf(o, oldInput))) + if (elementToSelect == null && !isValidInput(newInput) && (newInput == null && !isAncestorOf(element, oldInput))) // Clear input setInput(null); else if (mustSetNewInput(elementToSelect, oldInput, newInput)) { @@ -1210,6 +1180,23 @@ public abstract class CBrowsingPart extends ViewPart implements IMenuListener, I else setSelection(StructuredSelection.EMPTY, true); } + + protected Object getOriginalElement(Object obj) { + if (obj instanceof ICElement) { + ICElement element = (ICElement)obj; + // Below is for children of TranslationUnits but we have to make sure + // we handle the case that the child comes from the a workingCopy in that + // case it should be equal as the original element. + ITranslationUnit t = (ITranslationUnit)element.getAncestor(ICElement.C_UNIT); + if (t != null && t.isWorkingCopy()) { + ICElement original = ((IWorkingCopy)t).getOriginal(element); + if (original != null) + return original; + } + } + return obj; + } + /** * Compute if a new input must be set. @@ -1218,9 +1205,18 @@ public abstract class CBrowsingPart extends ViewPart implements IMenuListener, I * @since 3.0 */ private boolean mustSetNewInput(Object elementToSelect, Object oldInput, Object newInput) { - return (newInput == null || !newInput.equals(oldInput)) - && (elementToSelect == null - || oldInput == null); + if (newInput == null || oldInput == null || !newInput.equals(oldInput)) { + return true; + } + if (elementToSelect == null) { + return false; + } +// return !findInputForElement(elementToSelect).equals(newInput); + return false; +// return !(inputContainsElement(newInput, elementToSelect)); +// return (newInput == null || !newInput.equals(oldInput)) +// && (elementToSelect == null +// || oldInput == null); // return (newInput == null || !newInput.equals(oldInput)) // && (elementToSelect == null // || oldInput == null @@ -1246,10 +1242,10 @@ public abstract class CBrowsingPart extends ViewPart implements IMenuListener, I abstract protected Object findElementToSelect(Object element); /** - * Converts the given Java element to one which is suitable for this + * Converts the given C element to one which is suitable for this * view. It takes into account wether the view shows working copies or not. * - * @param element the Java element to be converted + * @param element the C element to be converted * @return an element suitable for this view */ Object getSuitableElement(Object obj) { @@ -1381,17 +1377,17 @@ public abstract class CBrowsingPart extends ViewPart implements IMenuListener, I } if (ei instanceof IFileEditorInput) { IFile file= ((IFileEditorInput)ei).getFile(); - ICElement je= (ICElement)file.getAdapter(ICElement.class); - if (je == null) { + ICElement ce= (ICElement)file.getAdapter(ICElement.class); + if (ce == null) { IContainer container= ((IFileEditorInput)ei).getFile().getParent(); if (container != null) - je= (ICElement)container.getAdapter(ICElement.class); + ce= (ICElement)container.getAdapter(ICElement.class); } - if (je == null) { + if (ce == null) { setSelection(null, false); return; } - adjustInputAndSetSelection(je); + adjustInputAndSetSelection(ce); // } else if (ei instanceof IClassFileEditorInput) { // IClassFile cf= ((IClassFileEditorInput)ei).getClassFile(); // adjustInputAndSetSelection(cf); diff --git a/core/org.eclipse.cdt.ui/browser/org/eclipse/cdt/internal/ui/browser/cbrowsing/MembersView.java b/core/org.eclipse.cdt.ui/browser/org/eclipse/cdt/internal/ui/browser/cbrowsing/MembersView.java index 45cec9c7162..f8a86ad70db 100644 --- a/core/org.eclipse.cdt.ui/browser/org/eclipse/cdt/internal/ui/browser/cbrowsing/MembersView.java +++ b/core/org.eclipse.cdt.ui/browser/org/eclipse/cdt/internal/ui/browser/cbrowsing/MembersView.java @@ -117,7 +117,7 @@ public class MembersView extends CBrowsingPart implements IPropertyChangeListene protected boolean isValidInput(Object element) { if (element instanceof ITypeInfo) { ITypeInfo type= (ITypeInfo)element; - if (type.getCElementType() != ICElement.C_NAMESPACE && exists(type)) + if (type.getCElementType() != ICElement.C_NAMESPACE && type.exists()) return true; } return false; @@ -131,8 +131,11 @@ public class MembersView extends CBrowsingPart implements IPropertyChangeListene * @return if the given element is a valid element */ protected boolean isValidElement(Object element) { - if (element instanceof ICElement) { - if (element instanceof ICModel || element instanceof ICProject || element instanceof ISourceRoot) + if (element instanceof ICElement) { + if (element instanceof ICModel + || element instanceof ICProject + || element instanceof ISourceRoot + || element instanceof ITranslationUnit) return false; return true; } @@ -240,14 +243,9 @@ public class MembersView extends CBrowsingPart implements IPropertyChangeListene * @see org.eclipse.cdt.internal.ui.browser.cbrowsing.CBrowsingPart#findElementToSelect(java.lang.Object) */ protected Object findElementToSelect(Object element) { - if (element instanceof ICElement && !(element instanceof ITranslationUnit) - && TypeUtil.isDeclaredType((ICElement)element)) { - ICElement parent = TypeUtil.getDeclaringContainerType((ICElement)element); - if (parent != null) { - return element; - } - } - + if (isValidElement(element)) { + return element; + } return null; } } diff --git a/core/org.eclipse.cdt.ui/browser/org/eclipse/cdt/internal/ui/browser/cbrowsing/NamespacesView.java b/core/org.eclipse.cdt.ui/browser/org/eclipse/cdt/internal/ui/browser/cbrowsing/NamespacesView.java index 77834780921..d39bc04865c 100644 --- a/core/org.eclipse.cdt.ui/browser/org/eclipse/cdt/internal/ui/browser/cbrowsing/NamespacesView.java +++ b/core/org.eclipse.cdt.ui/browser/org/eclipse/cdt/internal/ui/browser/cbrowsing/NamespacesView.java @@ -24,6 +24,7 @@ import org.eclipse.cdt.internal.ui.ICHelpContextIds; import org.eclipse.cdt.ui.CUIPlugin; import org.eclipse.cdt.ui.PreferenceConstants; import org.eclipse.cdt.ui.browser.typeinfo.TypeInfoLabelProvider; +import org.eclipse.core.resources.IProject; import org.eclipse.jface.viewers.IContentProvider; import org.eclipse.jface.viewers.LabelProvider; import org.eclipse.jface.viewers.StructuredViewer; @@ -119,7 +120,21 @@ public class NamespacesView extends CBrowsingPart { * @return if the given element is a valid element */ protected boolean isValidElement(Object element) { - return isValidNamespace(element); + if (element instanceof ITypeInfo) { + ITypeInfo info = (ITypeInfo)element; + if (info.exists() && info.getCElementType() == ICElement.C_NAMESPACE) { + // make sure it has types other than namespaces + ITypeInfo[] types = info.getEnclosedTypes(); + if (types != null) { + for (int i = 0; i < types.length; ++i) { + if (types[i].getCElementType() != ICElement.C_NAMESPACE) { + return true; + } + } + } + } + } + return false; } /** @@ -170,7 +185,7 @@ public class NamespacesView extends CBrowsingPart { return cProject; } - if (element instanceof ICElement && !(element instanceof ITranslationUnit)) { + if (element instanceof ICElement) { ICElement cElem = (ICElement)element; ISourceRoot root = findSourceRoot(cElem); if (exists(root) && !isProjectSourceRoot(root)) @@ -191,19 +206,27 @@ public class NamespacesView extends CBrowsingPart { return null; } - if (element instanceof ICElement && !(element instanceof ITranslationUnit)) { - ICElement parent = (ICElement)element; - while (parent != null) { - if ((parent instanceof IStructure + if (element instanceof ICElement) { + ICElement celem = (ICElement)element; + if (celem instanceof ITranslationUnit) { + IProject project = celem.getCProject().getProject(); + return AllTypesCache.getGlobalNamespace(project); + } else if (celem.getElementType() == ICElement.C_NAMESPACE) { + return AllTypesCache.getTypeForElement(celem, true, true, null); + } else { + ICElement parent = (ICElement)element; + while (parent != null) { + if ((parent instanceof IStructure || parent instanceof IEnumeration || parent instanceof ITypeDef) && parent.exists()) { - ITypeInfo info = AllTypesCache.getTypeForElement(parent, true, true, null); - if (info != null) { - return info.getEnclosingNamespace(true); - } - } - parent = parent.getParent(); + ITypeInfo info = AllTypesCache.getTypeForElement(parent, true, true, null); + if (info != null) { + return info.getEnclosingNamespace(true); + } + } + parent = parent.getParent(); + } } return null; } diff --git a/core/org.eclipse.cdt.ui/browser/org/eclipse/cdt/internal/ui/browser/cbrowsing/TypesView.java b/core/org.eclipse.cdt.ui/browser/org/eclipse/cdt/internal/ui/browser/cbrowsing/TypesView.java index ae99e75c1a4..bf79946d5c1 100644 --- a/core/org.eclipse.cdt.ui/browser/org/eclipse/cdt/internal/ui/browser/cbrowsing/TypesView.java +++ b/core/org.eclipse.cdt.ui/browser/org/eclipse/cdt/internal/ui/browser/cbrowsing/TypesView.java @@ -25,6 +25,7 @@ import org.eclipse.cdt.internal.ui.ICHelpContextIds; import org.eclipse.cdt.ui.CUIPlugin; import org.eclipse.cdt.ui.PreferenceConstants; import org.eclipse.cdt.ui.browser.typeinfo.TypeInfoLabelProvider; +import org.eclipse.core.resources.IProject; import org.eclipse.jface.viewers.IContentProvider; import org.eclipse.jface.viewers.LabelProvider; import org.eclipse.jface.viewers.StructuredViewer; @@ -109,7 +110,11 @@ public class TypesView extends CBrowsingPart { * @return if the given element is a valid input */ protected boolean isValidInput(Object element) { - return isValidNamespace(element); + if (element instanceof ITypeInfo) { + ITypeInfo info = (ITypeInfo)element; + return (info.exists() && info.getCElementType() == ICElement.C_NAMESPACE); + } + return false; } /** @@ -166,12 +171,20 @@ public class TypesView extends CBrowsingPart { return null; } - if (element instanceof ICElement && !(element instanceof ITranslationUnit)) { - ICElement parent = TypeUtil.getDeclaringContainerType((ICElement)element); - if (parent != null) { - ITypeInfo info = AllTypesCache.getTypeForElement(parent, true, true, null); - if (info != null) - return info.getEnclosingNamespace(true); + if (element instanceof ICElement) { + ICElement celem = (ICElement) element; + if (celem instanceof ITranslationUnit) { + IProject project = celem.getCProject().getProject(); + return AllTypesCache.getGlobalNamespace(project); + } else if (celem.getElementType() == ICElement.C_NAMESPACE) { + return AllTypesCache.getTypeForElement(celem, true, true, null); + } else { + ICElement parent = TypeUtil.getDeclaringContainerType(celem); + if (parent != null) { + ITypeInfo info = AllTypesCache.getTypeForElement(parent, true, true, null); + if (info != null) + return info.getEnclosingNamespace(true); + } } return null; }