1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Use ITextEditor instead of CEditor for portability

This commit is contained in:
Alain Magloire 2004-02-16 19:26:37 +00:00
parent ccd19b3db1
commit c4af8be231
3 changed files with 130 additions and 53 deletions

View file

@ -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

View file

@ -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 <code>IResource</code>)
* @return IStructuredSelection or null if any of the entries are not adaptable.
* resource mask formed by bitwise OR of resource type constants
* (defined on <code>IResource</code>)
* @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 <code>IResource</code>)
*
* @param resource
* the resource
* @param resourceMask
* resource mask formed by bitwise OR of resource type constants
* (defined on <code>IResource</code>)
* @return <code>true</code> if the resources has a matching type, and
* <code>false</code> otherwise
* <code>false</code> otherwise
* @see IResource#getType()
*/
public static boolean resourceIsType(IResource resource, int resourceMask) {
return (resource.getType() & resourceMask) != 0;
return (resource.getType() & resourceMask) != 0;
}
}

View file

@ -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());
}
}