From c4af8be23165add89dcfe8fe33f392576c4375d6 Mon Sep 17 00:00:00 2001 From: Alain Magloire Date: Mon, 16 Feb 2004 19:26:37 +0000 Subject: [PATCH] Use ITextEditor instead of CEditor for portability --- core/org.eclipse.cdt.ui/ChangeLog | 5 + .../internal/ui/cview/SelectionConverter.java | 73 +++++++++--- .../cdt/ui/actions/ShowInCViewAction.java | 105 +++++++++++------- 3 files changed, 130 insertions(+), 53 deletions(-) diff --git a/core/org.eclipse.cdt.ui/ChangeLog b/core/org.eclipse.cdt.ui/ChangeLog index 64c3b492cac..4265d20ee38 100644 --- a/core/org.eclipse.cdt.ui/ChangeLog +++ b/core/org.eclipse.cdt.ui/ChangeLog @@ -1,3 +1,8 @@ +2004-02-16 Alain Magloire + + Use ITextEditor instead of CEditor. + * src/org/eclipse/cdt/ui/actions/ShowInCViewAction.java + 2004-02-15 Alain Magloire Special working set for the C/C++ Celements diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/SelectionConverter.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/SelectionConverter.java index f9ad2894734..3d88b8bdc81 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/SelectionConverter.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/SelectionConverter.java @@ -9,7 +9,7 @@ * * Contributors: * QNX Software Systems - Initial API and implementation -***********************************************************************/ + ***********************************************************************/ package org.eclipse.cdt.internal.ui.cview; @@ -17,12 +17,20 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import org.eclipse.cdt.core.model.CModelException; import org.eclipse.cdt.core.model.ICElement; +import org.eclipse.cdt.core.model.ITranslationUnit; +import org.eclipse.cdt.core.model.IWorkingCopy; +import org.eclipse.cdt.ui.CUIPlugin; +import org.eclipse.cdt.ui.IWorkingCopyManager; import org.eclipse.core.resources.IResource; import org.eclipse.core.runtime.IAdaptable; +import org.eclipse.jface.text.ITextSelection; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.viewers.StructuredSelection; +import org.eclipse.ui.IEditorInput; +import org.eclipse.ui.texteditor.ITextEditor; public class SelectionConverter { @@ -32,7 +40,9 @@ public class SelectionConverter { Object[] elements = ((StructuredSelection) s).toArray(); for (int i = 0; i < elements.length; i++) { Object e = elements[i]; - if (e instanceof IAdaptable) { + if (e instanceof ICElement) { + converted.add((ICElement)e); + } else if (e instanceof IAdaptable) { ICElement c = (ICElement) ((IAdaptable) e).getAdapter(ICElement.class); if (c != null) { converted.add(c); @@ -49,7 +59,9 @@ public class SelectionConverter { Object[] elements = ((StructuredSelection) s).toArray(); for (int i = 0; i < elements.length; i++) { Object e = elements[i]; - if (e instanceof IAdaptable) { + if (e instanceof IResource) { + converted.add((IResource)e); + } else if (e instanceof IAdaptable) { IResource r = (IResource) ((IAdaptable) e).getAdapter(IResource.class); if (r != null) { converted.add(r); @@ -80,13 +92,16 @@ public class SelectionConverter { } /** - * Returns the selection adapted to IResource. Returns null if any of the entries are not adaptable. + * Returns the selection adapted to IResource. Returns null if any of the + * entries are not adaptable. * * @param selection * the selection * @param resourceMask - * resource mask formed by bitwise OR of resource type constants (defined on IResource) - * @return IStructuredSelection or null if any of the entries are not adaptable. + * resource mask formed by bitwise OR of resource type constants + * (defined on IResource) + * @return IStructuredSelection or null if any of the entries are not + * adaptable. * @see IResource#getType() */ public static IStructuredSelection allResources(IStructuredSelection selection, int resourceMask) { @@ -95,7 +110,7 @@ public class SelectionConverter { while (adaptables.hasNext()) { Object next = adaptables.next(); if (next instanceof IAdaptable) { - IResource resource = (IResource)((IAdaptable) next).getAdapter(IResource.class); + IResource resource = (IResource) ((IAdaptable) next).getAdapter(IResource.class); if (resource == null) { return null; } else if (resourceIsType(resource, resourceMask)) { @@ -109,19 +124,51 @@ public class SelectionConverter { } + public static ICElement getElementAtOffset(ITextEditor editor) throws CModelException { + return getElementAtOffset(getInput(editor), (ITextSelection) editor.getSelectionProvider().getSelection()); + } + + public static ICElement getElementAtOffset(ICElement input, ITextSelection selection) throws CModelException { + if (input instanceof ITranslationUnit) { + ITranslationUnit tunit = (ITranslationUnit) input; + if (tunit.isWorkingCopy()) { + synchronized (tunit) { + if (tunit instanceof IWorkingCopy) { + ((IWorkingCopy) tunit).reconcile(); + } + } + } + ICElement ref = tunit.getElementAtOffset(selection.getOffset()); + if (ref == null) + return input; + else + return ref; + } + return null; + } + + public static ICElement getInput(ITextEditor editor) { + if (editor == null) return null; + IEditorInput input = editor.getEditorInput(); + IWorkingCopyManager manager = CUIPlugin.getDefault().getWorkingCopyManager(); + return manager.getWorkingCopy(input); + } + /** * Returns whether the type of the given resource is among the specified * resource types. - * - * @param resource the resource - * @param resourceMask resource mask formed by bitwise OR of resource type - * constants (defined on IResource) + * + * @param resource + * the resource + * @param resourceMask + * resource mask formed by bitwise OR of resource type constants + * (defined on IResource) * @return true if the resources has a matching type, and - * false otherwise + * false otherwise * @see IResource#getType() */ public static boolean resourceIsType(IResource resource, int resourceMask) { - return (resource.getType() & resourceMask) != 0; + return (resource.getType() & resourceMask) != 0; } } 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 814efacb0a5..41cb30cb962 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,14 +11,24 @@ package org.eclipse.cdt.ui.actions; +import org.eclipse.cdt.core.model.CoreModel; +import org.eclipse.cdt.core.model.ICElement; import org.eclipse.cdt.internal.ui.editor.CEditorMessages; -import org.eclipse.jface.action.Action; +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.part.IShowInSource; -import org.eclipse.ui.part.IShowInTarget; +import org.eclipse.ui.PartInitException; +import org.eclipse.ui.actions.SelectionProviderAction; +import org.eclipse.ui.part.ISetSelectionTarget; import org.eclipse.ui.texteditor.ITextEditor; -import org.eclipse.ui.texteditor.IUpdate; /** * This class will open the C/C++ Projects view and highlight the @@ -27,64 +37,79 @@ import org.eclipse.ui.texteditor.IUpdate; * accomplish this task so as to provide some additional portability * and future proofing. */ -public class ShowInCViewAction extends Action implements IUpdate { +public class ShowInCViewAction extends SelectionProviderAction { + + private IWorkbenchPage page; + private ITextEditor fEditor; - final String CVIEW_ID = "org.eclipse.cdt.ui.CView"; + + public ShowInCViewAction(IWorkbenchPage page, ISelectionProvider viewer) { + super(viewer, CEditorMessages.getString("ShowInCView.label")); //$NON-NLS-1$ + setToolTipText(CEditorMessages.getString("ShowInCView.tooltip")); //$NON-NLS-1$ + setDescription(CEditorMessages.getString("ShowInCView.description")); //$NON-NLS-1$ + this.page = page; + setDescription(CEditorMessages.getString("ShowInCView.toolTip")); //$NON-NLS-1$ + //WorkbenchHelp.setHelp(this, ICHelpContextIds.SHOW_IN_CVIEW_ACTION); + } public ShowInCViewAction() { this(null); } public ShowInCViewAction(ITextEditor editor) { - super(CEditorMessages.getString("ShowInCView.label")); //$NON-NLS-1$ - setToolTipText(CEditorMessages.getString("ShowInCView.tooltip")); //$NON-NLS-1$ - setDescription(CEditorMessages.getString("ShowInCView.description")); //$NON-NLS-1$ - - fEditor= editor; - //WorkbenchHelp.setHelp(this, new Object[] { IJavaHelpContextIds.ADD_IMPORT_ON_SELECTION_ACTION }); + this(editor.getEditorSite().getWorkbenchWindow().getActivePage(), editor.getSelectionProvider()); + fEditor = editor; } /** * @see IAction#actionPerformed */ public void run() { - if(fEditor == null) { + if(page == null) { return; } - + ISelection selection = getSelection(); + if (selection instanceof ITextSelection) { + run(fEditor); + } else if (selection instanceof IStructuredSelection) { + run((IStructuredSelection)selection); + } + + } + + public void run(IStructuredSelection selection) { //Locate a source and a target for us to use - IShowInTarget showInTarget; - IShowInSource showInSource; try { - IWorkbenchPage page = fEditor.getEditorSite().getWorkbenchWindow().getActivePage(); - IWorkbenchPart part = page.showView(CVIEW_ID); - if(part instanceof IShowInTarget) { - showInTarget = (IShowInTarget)part; - } else { - showInTarget = (IShowInTarget)part.getAdapter(IShowInTarget.class); + IWorkbenchPart part = page.showView(CUIPlugin.CVIEW_ID); + if (part instanceof ISetSelectionTarget) { + ((ISetSelectionTarget) part).selectReveal(selection); } - - if(fEditor instanceof IShowInSource) { - showInSource = (IShowInSource)fEditor; - } else { - showInSource = (IShowInSource)fEditor.getAdapter(IShowInSource.class); - } - } catch(Exception ex) { - return; + } catch(PartInitException ex) { } - - if(showInTarget == null || showInSource == null) { - return; - } - - //Now go ahead and show it (assuming that you can!) - showInTarget.show(showInSource.getShowInContext()); } - /* (non-Javadoc) - * @see org.eclipse.ui.texteditor.IUpdate#update() + 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); + if (celement != null) { + run(new StructuredSelection(celement)); + } + } + } + } + + /* + * (non-Javadoc) + * Method declared on SelectionProviderAction. */ - public void update() { + public void selectionChanged(IStructuredSelection selection) { + setEnabled(!getSelection().isEmpty()); } + }