Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[cdt-patch] cdt.ui Browsing binary functions.

Index: ChangeLog
===================================================================
RCS file: /home/tools/org.eclipse.cdt.ui/ChangeLog,v
retrieving revision 1.77
diff -u -r1.77 ChangeLog
--- ChangeLog	1 Apr 2003 18:42:03 -0000	1.77
+++ ChangeLog	2 Apr 2003 04:48:01 -0000
@@ -1,5 +1,16 @@
 2003-04-01 Alain Magloire
 
+	Support to open a child in an IBinary if we know the TranslationUnit.
+	
+	* src/org/eclipse/internal/ui/editor/CEditor.java:
+	setSelection() make the method visible.
+	* src/org/eclipse/internal/ui/util/EditorUtility.java:
+	More static method taken from the JDT.
+	* src/org/eclipse/internal/ui/cview/CView.java:
+	handleDoubleClick(), for IBinary.
+
+2003-04-01 Alain Magloire
+
 	Patch from Hoda Amer.
 	New icons for enumerations, enumerators and typedefs.
 	Small fix for CElementLabelProvider.getText().
Index: src/org/eclipse/cdt/internal/ui/cview/CView.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/CView.java,v
retrieving revision 1.17
diff -u -r1.17 CView.java
--- src/org/eclipse/cdt/internal/ui/cview/CView.java	31 Mar 2003 03:52:44 -0000	1.17
+++ src/org/eclipse/cdt/internal/ui/cview/CView.java	2 Apr 2003 04:48:06 -0000
@@ -291,32 +291,23 @@
 	*/
 	protected void handleDoubleClick(DoubleClickEvent event) {
 		IStructuredSelection s = (IStructuredSelection)event.getSelection();
-		//Object element = s.getFirstElement();
 		IAdaptable element = (IAdaptable)s.getFirstElement();
-		IResource resource = null;
-
-		if (element instanceof IAdaptable) {
-			resource = (IResource)((IAdaptable)element).getAdapter(IResource.class);
-		}
-
-		if (resource == null)
-			return;
-
+		IEditorPart part = null;
 		//System.out.println ("Double click on " + element);
-		if (resource instanceof IFile) {
-			if (element instanceof ICElement) {
-				try {
-					EditorUtility.openInEditor((ICElement)element);
-				} catch (Exception e) {
+
+		try {
+			part = EditorUtility.openInEditor(element);
+			if (part != null) {
+				IWorkbenchPage page = getSite().getPage();
+				page.bringToTop(part);
+				if (element instanceof ISourceReference) {
+					EditorUtility.revealInEditor(part, (ICElement)element);
 				}
-			} else {
-				openFileAction.selectionChanged(s);
-				openFileAction.run();
-			}
-		} else {
-			if (viewer.isExpandable(element)) {
-				viewer.setExpandedState(element, !viewer.getExpandedState(element));
 			}
+		} catch (Exception e) {
+		}		
+		if (part == null && viewer.isExpandable(element)) {
+			viewer.setExpandedState(element, !viewer.getExpandedState(element));
 		}
 	}
 
Index: src/org/eclipse/cdt/internal/ui/editor/CEditor.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CEditor.java,v
retrieving revision 1.17
diff -u -r1.17 CEditor.java
--- src/org/eclipse/cdt/internal/ui/editor/CEditor.java	1 Apr 2003 18:41:24 -0000	1.17
+++ src/org/eclipse/cdt/internal/ui/editor/CEditor.java	2 Apr 2003 04:48:11 -0000
@@ -583,7 +583,7 @@
 	 * @param element the source range to be shown in the editor, can be null.
 	 * @param moveCursor if true the editor is scrolled to show the range.
 	 */
-	private void setSelection(ISourceRange element, boolean moveCursor) {
+	public void setSelection(ISourceRange element, boolean moveCursor) {
 		if (element != null) {
 			try {
 				IRegion alternateRegion = null;
Index: src/org/eclipse/cdt/internal/ui/util/EditorUtility.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/util/EditorUtility.java,v
retrieving revision 1.6
diff -u -r1.6 EditorUtility.java
--- src/org/eclipse/cdt/internal/ui/util/EditorUtility.java	27 Mar 2003 17:40:15 -0000	1.6
+++ src/org/eclipse/cdt/internal/ui/util/EditorUtility.java	2 Apr 2003 04:48:13 -0000
@@ -10,94 +10,278 @@
 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();
+	/** 
+	 * 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 {
+			input = getEditorInput(inputElement);
+		} catch (CModelException x) {
+			//CUIPlugin.log(x.getStatus());
+		}
+                
+		if (input != null) {
+			IWorkbenchPage p= CUIPlugin.getActivePage();
 			if (p != null) {
-				return p.openEditor(file);
+				return p.findEditor(input);
 			}
 		}
+                
 		return null;
 	}
 
-	public static IEditorPart openInEditor (ICElement element) throws PartInitException {
-		IResource res = null;
-		try {
-			res = element.getUnderlyingResource();
-		} catch (CModelException e) {
+
+	/**
+	 * 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;
+	}
 
-		// Treat binary differently
-		if (element instanceof IBinary) {
-			IStorage store = getStorage((IBinary)element);
-			if (store != null) {
-				return openInEditor(store, element.getElementName());
+	/** 
+	 * 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) {
+				}
 			}
 		}
+	}
 
-		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));
+	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 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());
+	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 openInEditor(f);
+		return null;
 	}
 
-	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));
+	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 @@
 		}
 		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;
Index: src/org/eclipse/cdt/ui/CUIPlugin.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/CUIPlugin.java,v
retrieving revision 1.8
diff -u -r1.8 CUIPlugin.java
--- src/org/eclipse/cdt/ui/CUIPlugin.java	27 Mar 2003 16:11:56 -0000	1.8
+++ src/org/eclipse/cdt/ui/CUIPlugin.java	2 Apr 2003 04:48:14 -0000
@@ -38,6 +38,7 @@
 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 @@
 		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 @@
 			fProblemMarkerManager = new ProblemMarkerManager();
 		return fProblemMarkerManager;
 	}
+
 }



Back to the top