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:
parent
ccd19b3db1
commit
c4af8be231
3 changed files with 130 additions and 53 deletions
|
@ -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
|
2004-02-15 Alain Magloire
|
||||||
|
|
||||||
Special working set for the C/C++ Celements
|
Special working set for the C/C++ Celements
|
||||||
|
|
|
@ -9,7 +9,7 @@
|
||||||
*
|
*
|
||||||
* Contributors:
|
* Contributors:
|
||||||
* QNX Software Systems - Initial API and implementation
|
* QNX Software Systems - Initial API and implementation
|
||||||
***********************************************************************/
|
***********************************************************************/
|
||||||
|
|
||||||
package org.eclipse.cdt.internal.ui.cview;
|
package org.eclipse.cdt.internal.ui.cview;
|
||||||
|
|
||||||
|
@ -17,12 +17,20 @@ import java.util.ArrayList;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.eclipse.cdt.core.model.CModelException;
|
||||||
import org.eclipse.cdt.core.model.ICElement;
|
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.resources.IResource;
|
||||||
import org.eclipse.core.runtime.IAdaptable;
|
import org.eclipse.core.runtime.IAdaptable;
|
||||||
|
import org.eclipse.jface.text.ITextSelection;
|
||||||
import org.eclipse.jface.viewers.ISelection;
|
import org.eclipse.jface.viewers.ISelection;
|
||||||
import org.eclipse.jface.viewers.IStructuredSelection;
|
import org.eclipse.jface.viewers.IStructuredSelection;
|
||||||
import org.eclipse.jface.viewers.StructuredSelection;
|
import org.eclipse.jface.viewers.StructuredSelection;
|
||||||
|
import org.eclipse.ui.IEditorInput;
|
||||||
|
import org.eclipse.ui.texteditor.ITextEditor;
|
||||||
|
|
||||||
public class SelectionConverter {
|
public class SelectionConverter {
|
||||||
|
|
||||||
|
@ -32,7 +40,9 @@ public class SelectionConverter {
|
||||||
Object[] elements = ((StructuredSelection) s).toArray();
|
Object[] elements = ((StructuredSelection) s).toArray();
|
||||||
for (int i = 0; i < elements.length; i++) {
|
for (int i = 0; i < elements.length; i++) {
|
||||||
Object e = elements[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);
|
ICElement c = (ICElement) ((IAdaptable) e).getAdapter(ICElement.class);
|
||||||
if (c != null) {
|
if (c != null) {
|
||||||
converted.add(c);
|
converted.add(c);
|
||||||
|
@ -49,7 +59,9 @@ public class SelectionConverter {
|
||||||
Object[] elements = ((StructuredSelection) s).toArray();
|
Object[] elements = ((StructuredSelection) s).toArray();
|
||||||
for (int i = 0; i < elements.length; i++) {
|
for (int i = 0; i < elements.length; i++) {
|
||||||
Object e = elements[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);
|
IResource r = (IResource) ((IAdaptable) e).getAdapter(IResource.class);
|
||||||
if (r != null) {
|
if (r != null) {
|
||||||
converted.add(r);
|
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
|
* @param selection
|
||||||
* the selection
|
* the selection
|
||||||
* @param resourceMask
|
* @param resourceMask
|
||||||
* resource mask formed by bitwise OR of resource type constants (defined on <code>IResource</code>)
|
* resource mask formed by bitwise OR of resource type constants
|
||||||
* @return IStructuredSelection or null if any of the entries are not adaptable.
|
* (defined on <code>IResource</code>)
|
||||||
|
* @return IStructuredSelection or null if any of the entries are not
|
||||||
|
* adaptable.
|
||||||
* @see IResource#getType()
|
* @see IResource#getType()
|
||||||
*/
|
*/
|
||||||
public static IStructuredSelection allResources(IStructuredSelection selection, int resourceMask) {
|
public static IStructuredSelection allResources(IStructuredSelection selection, int resourceMask) {
|
||||||
|
@ -95,7 +110,7 @@ public class SelectionConverter {
|
||||||
while (adaptables.hasNext()) {
|
while (adaptables.hasNext()) {
|
||||||
Object next = adaptables.next();
|
Object next = adaptables.next();
|
||||||
if (next instanceof IAdaptable) {
|
if (next instanceof IAdaptable) {
|
||||||
IResource resource = (IResource)((IAdaptable) next).getAdapter(IResource.class);
|
IResource resource = (IResource) ((IAdaptable) next).getAdapter(IResource.class);
|
||||||
if (resource == null) {
|
if (resource == null) {
|
||||||
return null;
|
return null;
|
||||||
} else if (resourceIsType(resource, resourceMask)) {
|
} else if (resourceIsType(resource, resourceMask)) {
|
||||||
|
@ -109,13 +124,45 @@ 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
|
* Returns whether the type of the given resource is among the specified
|
||||||
* resource types.
|
* resource types.
|
||||||
*
|
*
|
||||||
* @param resource the resource
|
* @param resource
|
||||||
* @param resourceMask resource mask formed by bitwise OR of resource type
|
* the resource
|
||||||
* constants (defined on <code>IResource</code>)
|
* @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
|
* @return <code>true</code> if the resources has a matching type, and
|
||||||
* <code>false</code> otherwise
|
* <code>false</code> otherwise
|
||||||
* @see IResource#getType()
|
* @see IResource#getType()
|
||||||
|
|
|
@ -11,14 +11,24 @@
|
||||||
|
|
||||||
package org.eclipse.cdt.ui.actions;
|
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.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.IWorkbenchPage;
|
||||||
import org.eclipse.ui.IWorkbenchPart;
|
import org.eclipse.ui.IWorkbenchPart;
|
||||||
import org.eclipse.ui.part.IShowInSource;
|
import org.eclipse.ui.PartInitException;
|
||||||
import org.eclipse.ui.part.IShowInTarget;
|
import org.eclipse.ui.actions.SelectionProviderAction;
|
||||||
|
import org.eclipse.ui.part.ISetSelectionTarget;
|
||||||
import org.eclipse.ui.texteditor.ITextEditor;
|
import org.eclipse.ui.texteditor.ITextEditor;
|
||||||
import org.eclipse.ui.texteditor.IUpdate;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class will open the C/C++ Projects view and highlight the
|
* 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
|
* accomplish this task so as to provide some additional portability
|
||||||
* and future proofing.
|
* and future proofing.
|
||||||
*/
|
*/
|
||||||
public class ShowInCViewAction extends Action implements IUpdate {
|
public class ShowInCViewAction extends SelectionProviderAction {
|
||||||
|
|
||||||
|
private IWorkbenchPage page;
|
||||||
|
|
||||||
private ITextEditor fEditor;
|
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() {
|
public ShowInCViewAction() {
|
||||||
this(null);
|
this(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ShowInCViewAction(ITextEditor editor) {
|
public ShowInCViewAction(ITextEditor editor) {
|
||||||
super(CEditorMessages.getString("ShowInCView.label")); //$NON-NLS-1$
|
this(editor.getEditorSite().getWorkbenchWindow().getActivePage(), editor.getSelectionProvider());
|
||||||
setToolTipText(CEditorMessages.getString("ShowInCView.tooltip")); //$NON-NLS-1$
|
fEditor = editor;
|
||||||
setDescription(CEditorMessages.getString("ShowInCView.description")); //$NON-NLS-1$
|
|
||||||
|
|
||||||
fEditor= editor;
|
|
||||||
//WorkbenchHelp.setHelp(this, new Object[] { IJavaHelpContextIds.ADD_IMPORT_ON_SELECTION_ACTION });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @see IAction#actionPerformed
|
* @see IAction#actionPerformed
|
||||||
*/
|
*/
|
||||||
public void run() {
|
public void run() {
|
||||||
if(fEditor == null) {
|
if(page == null) {
|
||||||
return;
|
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
|
//Locate a source and a target for us to use
|
||||||
IShowInTarget showInTarget;
|
|
||||||
IShowInSource showInSource;
|
|
||||||
try {
|
try {
|
||||||
IWorkbenchPage page = fEditor.getEditorSite().getWorkbenchWindow().getActivePage();
|
IWorkbenchPart part = page.showView(CUIPlugin.CVIEW_ID);
|
||||||
IWorkbenchPart part = page.showView(CVIEW_ID);
|
if (part instanceof ISetSelectionTarget) {
|
||||||
if(part instanceof IShowInTarget) {
|
((ISetSelectionTarget) part).selectReveal(selection);
|
||||||
showInTarget = (IShowInTarget)part;
|
}
|
||||||
} else {
|
} catch(PartInitException ex) {
|
||||||
showInTarget = (IShowInTarget)part.getAdapter(IShowInTarget.class);
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(fEditor instanceof IShowInSource) {
|
public void run(ITextEditor editor) {
|
||||||
showInSource = (IShowInSource)fEditor;
|
if (editor != null) {
|
||||||
} else {
|
IEditorInput input = editor.getEditorInput();
|
||||||
showInSource = (IShowInSource)fEditor.getAdapter(IShowInSource.class);
|
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch(Exception ex) {
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(showInTarget == null || showInSource == null) {
|
/*
|
||||||
return;
|
* (non-Javadoc)
|
||||||
}
|
* Method declared on SelectionProviderAction.
|
||||||
|
|
||||||
//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 update() {
|
public void selectionChanged(IStructuredSelection selection) {
|
||||||
|
setEnabled(!getSelection().isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue