diff --git a/core/org.eclipse.cdt.ui/ChangeLog b/core/org.eclipse.cdt.ui/ChangeLog index 9c74970aaf2..6c21c9d3a5e 100644 --- a/core/org.eclipse.cdt.ui/ChangeLog +++ b/core/org.eclipse.cdt.ui/ChangeLog @@ -1,3 +1,12 @@ +2004-02-28 Alain Magloire + + Provide an implementation of CView.selectReveal(). + The problem was that ITranslationUnit != IWorkingCopy + + * src/org/eclipse/cdt/internal/ui/cview/CView.java + * src/org/eclipse/cdt/internal/ui/cview/CViewElementComparer.java + * src/org/eclipse/cdt/ui/actions/ShowInCViewAction.java + 2004-02-28 Alain Magloire Fixing a NPE. diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/CView.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/CView.java index 557a0f0760b..7e0f35b7ccc 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/CView.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/CView.java @@ -523,6 +523,7 @@ public class CView extends ViewPart implements ISetSelectionTarget, IPropertyCha viewer = createViewer(parent); viewer.setUseHashlookup(true); + viewer.setComparer(new CViewElementComparer()); initContentProvider(viewer); initLabelProvider(viewer); CUIPlugin.getDefault().getProblemMarkerManager().addListener(viewer); diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/CViewElementComparer.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/CViewElementComparer.java new file mode 100644 index 00000000000..fc686738bae --- /dev/null +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/CViewElementComparer.java @@ -0,0 +1,67 @@ +/******************************************************************************* + * Copyright (c) 2000, 2003 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.internal.ui.cview; + +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; + +public class CViewElementComparer implements IElementComparer { + + 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; + + 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); + } + + 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(); + } +} diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/actions/ShowInCViewAction.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/actions/ShowInCViewAction.java index afd4164ebf7..ee2d09f7b38 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/actions/ShowInCViewAction.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/actions/ShowInCViewAction.java @@ -11,18 +11,16 @@ package org.eclipse.cdt.ui.actions; -import org.eclipse.cdt.core.model.CoreModel; +import org.eclipse.cdt.core.model.CModelException; import org.eclipse.cdt.core.model.ICElement; +import org.eclipse.cdt.internal.ui.cview.SelectionConverter; import org.eclipse.cdt.internal.ui.editor.CEditorMessages; import org.eclipse.cdt.ui.CUIPlugin; -import org.eclipse.core.resources.IFile; import org.eclipse.jface.text.ITextSelection; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.ISelectionProvider; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.viewers.StructuredSelection; -import org.eclipse.ui.IEditorInput; -import org.eclipse.ui.IFileEditorInput; import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.IWorkbenchPart; import org.eclipse.ui.IWorkbenchSite; @@ -90,15 +88,12 @@ public class ShowInCViewAction extends SelectionProviderAction { public void run(ITextEditor editor) { if (editor != null) { - IEditorInput input = editor.getEditorInput(); - if (input instanceof IFileEditorInput) { - IFileEditorInput fileInput = (IFileEditorInput) input; - IFile file = fileInput.getFile(); - CoreModel factory = CoreModel.getDefault(); - ICElement celement = factory.create(file); + try { + ICElement celement = SelectionConverter.getElementAtOffset(editor); if (celement != null) { run(new StructuredSelection(celement)); } + } catch (CModelException e) { } } }