1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-10 01:35:39 +02:00

New static methods taken from JDT counterpart.

This commit is contained in:
Alain Magloire 2003-04-02 05:04:12 +00:00
parent bc176d3e56
commit c021d8a42e
2 changed files with 264 additions and 72 deletions

View file

@ -10,94 +10,278 @@ import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import org.eclipse.cdt.core.model.CModelException;
import org.eclipse.cdt.core.model.IBinary;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.ISourceRange;
import org.eclipse.cdt.core.model.ISourceReference;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.core.resources.FileStorage;
import org.eclipse.cdt.internal.core.model.IWorkingCopy;
import org.eclipse.cdt.internal.ui.editor.CEditor;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.utils.spawner.ProcessFactory;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IStorage;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.ui.IEditorDescriptor;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorRegistry;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.cdt.core.model.CModelException;
import org.eclipse.cdt.core.model.IBinary;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.resources.FileStorage;
import org.eclipse.cdt.internal.ui.editor.CEditor;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.utils.spawner.ProcessFactory;
import org.eclipse.ui.part.FileEditorInput;
import org.eclipse.ui.texteditor.ITextEditor;
public class EditorUtility {
private EditorUtility () {
}
public static IEditorPart openInEditor (IFile file) throws PartInitException {
IWorkbenchWindow window= CUIPlugin.getDefault().getActiveWorkbenchWindow();
if (window != null) {
IWorkbenchPage p= window.getActivePage();
if (p != null) {
return p.openEditor(file);
}
}
return null;
}
public static IEditorPart openInEditor (ICElement element) throws PartInitException {
IResource res = null;
/**
* Tests if a cu is currently shown in an editor
* @return the IEditorPart if shown, null if element is not open in an editor
*/
public static IEditorPart isOpenInEditor(Object inputElement) {
IEditorInput input = null;
try {
res = element.getUnderlyingResource();
} catch (CModelException e) {
input = getEditorInput(inputElement);
} catch (CModelException x) {
//CUIPlugin.log(x.getStatus());
}
// Treat binary differently
if (element instanceof IBinary) {
IStorage store = getStorage((IBinary)element);
if (store != null) {
return openInEditor(store, element.getElementName());
}
}
if (res != null && res instanceof IFile) {
IEditorPart editor = openInEditor((IFile)res);
if (editor instanceof CEditor) {
CEditor e = (CEditor)editor;
StructuredSelection selection = new StructuredSelection(element);
e.selectionChanged (new SelectionChangedEvent (e.getOutlinePage (), selection));
}
return editor;
}
return null;
}
public static IEditorPart openInEditor (IPath path) throws PartInitException {
IFile f = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
if (f == null) {
IStorage s = new FileStorage(path);
return openInEditor(s, path.lastSegment());
}
return openInEditor(f);
}
public static IEditorPart openInEditor (IStorage store, String name) throws PartInitException {
IEditorInput ei = new ExternalEditorInput(store);
IWorkbenchWindow window= CUIPlugin.getDefault().getActiveWorkbenchWindow();
if (window != null) {
IWorkbenchPage p = window.getActivePage();
if (input != null) {
IWorkbenchPage p= CUIPlugin.getActivePage();
if (p != null) {
return p.openEditor(ei, getEditorID(name));
return p.findEditor(input);
}
}
return null;
}
/**
* Opens a Java editor for an element such as <code>IJavaElement</code>, <code>IFile</code>, or <code>IStorage</code>.
* The editor is activated by default.
* @return the IEditorPart or null if wrong element type or opening failed
*/
public static IEditorPart openInEditor(Object inputElement) throws CModelException, PartInitException {
return openInEditor(inputElement, true);
}
/**
* Opens a Java editor for an element (IJavaElement, IFile, IStorage...)
* @return the IEditorPart or null if wrong element type or opening failed
*/
public static IEditorPart openInEditor(Object inputElement, boolean activate) throws CModelException, PartInitException {
if (inputElement instanceof IFile) {
return openInEditor((IFile) inputElement, activate);
}
IEditorInput input = getEditorInput(inputElement);
if (input instanceof IFileEditorInput) {
IFileEditorInput fileInput= (IFileEditorInput) input;
return openInEditor(fileInput.getFile(), activate);
}
if (input != null) {
return openInEditor(input, getEditorID(input, inputElement), activate);
}
return null;
}
/**
* Selects a C Element in an editor
*/
public static void revealInEditor(IEditorPart part, ICElement element) {
if (element != null && part instanceof CEditor) {
if (element instanceof ISourceReference) {
try {
ISourceRange range = ((ISourceReference) element).getSourceRange();
((CEditor) part).setSelection(range, true);
} catch (CModelException e) {
}
}
}
}
private static IEditorPart openInEditor(IFile file, boolean activate) throws PartInitException {
if (file != null) {
IWorkbenchPage p= CUIPlugin.getActivePage();
if (p != null) {
IEditorPart editorPart= p.openEditor(file, null, activate);
initializeHighlightRange(editorPart);
return editorPart;
}
}
return null;
}
private static IEditorPart openInEditor(IEditorInput input, String editorID, boolean activate) throws PartInitException {
if (input != null) {
IWorkbenchPage p= CUIPlugin.getActivePage();
if (p != null) {
IEditorPart editorPart= p.openEditor(input, editorID, activate);
initializeHighlightRange(editorPart);
return editorPart;
}
}
return null;
}
private static void initializeHighlightRange(IEditorPart editorPart) {
if (editorPart instanceof ITextEditor) {
//TogglePresentationAction toggleAction= new TogglePresentationAction();
// Initialize editor
//toggleAction.setEditor((ITextEditor)editorPart);
// Reset action
//toggleAction.setEditor(null);
}
}
private static IEditorInput getEditorInput(ICElement element) throws CModelException {
while (element != null) {
if (element instanceof IWorkingCopy && ((IWorkingCopy) element).isWorkingCopy())
element= ((IWorkingCopy) element).getOriginalElement();
if (element instanceof ISourceReference) {
element = ((ISourceReference)element).getTranslationUnit();
}
if (element instanceof ITranslationUnit) {
ITranslationUnit unit= (ITranslationUnit) element;
IResource resource= unit.getResource();
if (resource instanceof IFile)
return new FileEditorInput((IFile) resource);
}
if (element instanceof IBinary) {
//return new InternalClassFileEditorInput((IBinary) element);
return new ExternalEditorInput(getStorage((IBinary)element));
}
element= element.getParent();
}
return null;
}
public static IEditorInput getEditorInput(Object input) throws CModelException {
if (input instanceof ICElement) {
return getEditorInput((ICElement) input);
}
if (input instanceof IFile) {
return new FileEditorInput((IFile) input);
}
if (input instanceof IStorage) {
//return new JarEntryEditorInput((IStorage)input);
return new ExternalEditorInput((IStorage)input);
}
return null;
}
/**
* If the current active editor edits a c element return it, else
* return null
*/
public static ICElement getActiveEditorCInput() {
IWorkbenchPage page= CUIPlugin.getActivePage();
if (page != null) {
IEditorPart part= page.getActiveEditor();
if (part != null) {
IEditorInput editorInput= part.getEditorInput();
if (editorInput != null) {
return (ICElement)editorInput.getAdapter(ICElement.class);
}
}
}
return null;
}
/**
* Gets the working copy of an compilation unit opened in an editor
* @param part the editor part
* @param cu the original compilation unit (or another working copy)
* @return the working copy of the compilation unit, or null if not found
*/
// public static ITranslationUnit getWorkingCopy(ITranslationUnit cu) {
// if (cu == null)
// return null;
// if (cu.isWorkingCopy())
// return cu;
//
// return (ITranslationUnit)cu.findSharedWorkingCopy(CUIPlugin.getBufferFactory());
// }
/**
* Returns the translation unit for the given c element.
* @param element the c element whose compilation unit is searched for
* @return the compilation unit of the given java element
*/
private static ITranslationUnit getTranslationUnit(ICElement element) {
if (element == null)
return null;
int type= element.getElementType();
if (ICElement.C_UNIT == type) {
return (ITranslationUnit) element;
}
if (ICElement.C_BINARY == type) {
return null;
}
if (element instanceof ISourceReference) {
return ((ISourceReference) element).getTranslationUnit();
}
return getTranslationUnit(element.getParent());
}
// public static IEditorPart openInEditor (IFile file) throws PartInitException {
// IWorkbenchWindow window= CUIPlugin.getDefault().getActiveWorkbenchWindow();
// if (window != null) {
// IWorkbenchPage p= window.getActivePage();
// if (p != null) {
// return p.openEditor(file);
// }
// }
// return null;
// }
//
//
// public static IEditorPart openInEditor (IPath path) throws PartInitException {
// IFile f = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
// if (f == null) {
// IStorage s = new FileStorage(path);
// return openInEditor(s, path.lastSegment());
// }
// return openInEditor(f);
// }
//
// public static IEditorPart openInEditor (IStorage store, String name) throws PartInitException {
// IEditorInput ei = new ExternalEditorInput(store);
// IWorkbenchWindow window= CUIPlugin.getDefault().getActiveWorkbenchWindow();
// if (window != null) {
// IWorkbenchPage p = window.getActivePage();
// if (p != null) {
// return p.openEditor(ei, getEditorID(name));
// }
// }
// return null;
// }
//
public static String getEditorID(String name) {
IEditorRegistry registry = PlatformUI.getWorkbench().getEditorRegistry();
if (registry != null) {
@ -110,16 +294,11 @@ public class EditorUtility {
}
return null;
}
/**
* Selects a C Element in an editor
*/
public static void revealInEditor(IEditorPart part, ICElement element) {
if (element != null && part instanceof CEditor) {
//((CEditor) part).setSelection(element);
}
}
public static String getEditorID(IEditorInput input, Object inputObject) {
return getEditorID(input.getName());
}
public static IStorage getStorage(IBinary bin) {
IStorage store = null;
Process objdump = null;

View file

@ -38,6 +38,7 @@ import org.eclipse.core.runtime.Status;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.plugin.AbstractUIPlugin;
@ -109,6 +110,17 @@ public class CUIPlugin extends AbstractUIPlugin {
return getDefault().getWorkbench().getActiveWorkbenchWindow();
}
/**
* @return
*/
public static IWorkbenchPage getActivePage() {
IWorkbenchWindow window = getDefault().getActiveWorkbenchWindow();
if (window != null) {
return window.getActivePage();
}
return null;
}
public Shell getActiveWorkbenchShell() {
return getActiveWorkbenchWindow().getShell();
}
@ -274,4 +286,5 @@ public class CUIPlugin extends AbstractUIPlugin {
fProblemMarkerManager = new ProblemMarkerManager();
return fProblemMarkerManager;
}
}