diff --git a/core/org.eclipse.cdt.ui/browser/org/eclipse/cdt/internal/ui/browser/opentype/OpenTypeAction.java b/core/org.eclipse.cdt.ui/browser/org/eclipse/cdt/internal/ui/browser/opentype/OpenTypeAction.java index bb7f8b8d69f..85f2ca11c6f 100644 --- a/core/org.eclipse.cdt.ui/browser/org/eclipse/cdt/internal/ui/browser/opentype/OpenTypeAction.java +++ b/core/org.eclipse.cdt.ui/browser/org/eclipse/cdt/internal/ui/browser/opentype/OpenTypeAction.java @@ -10,16 +10,6 @@ *******************************************************************************/ package org.eclipse.cdt.internal.ui.browser.opentype; -import org.eclipse.cdt.core.browser.AllTypesCache; -import org.eclipse.cdt.core.browser.ITypeInfo; -import org.eclipse.cdt.core.browser.ITypeReference; -import org.eclipse.cdt.core.model.CModelException; -import org.eclipse.cdt.core.model.ITranslationUnit; -import org.eclipse.cdt.core.resources.FileStorage; -import org.eclipse.cdt.internal.ui.util.EditorUtility; -import org.eclipse.cdt.ui.CUIPlugin; -import org.eclipse.core.resources.IStorage; -import org.eclipse.core.runtime.IPath; import org.eclipse.jface.action.IAction; import org.eclipse.jface.dialogs.IDialogConstants; import org.eclipse.jface.dialogs.MessageDialog; @@ -33,6 +23,15 @@ import org.eclipse.ui.IWorkbenchWindowActionDelegate; import org.eclipse.ui.PartInitException; import org.eclipse.ui.texteditor.ITextEditor; +import org.eclipse.cdt.core.browser.AllTypesCache; +import org.eclipse.cdt.core.browser.ITypeInfo; +import org.eclipse.cdt.core.browser.ITypeReference; +import org.eclipse.cdt.core.model.CModelException; +import org.eclipse.cdt.core.model.ITranslationUnit; +import org.eclipse.cdt.ui.CUIPlugin; + +import org.eclipse.cdt.internal.ui.util.EditorUtility; + public class OpenTypeAction implements IWorkbenchWindowActionDelegate { public OpenTypeAction() { @@ -96,11 +95,7 @@ public class OpenTypeAction implements IWorkbenchWindowActionDelegate { editorPart = EditorUtility.openInEditor(unit); if (editorPart == null) { // open as external file - IPath path = location.getLocation(); - if (path != null) { - IStorage storage = new FileStorage(path); - editorPart = EditorUtility.openInEditor(storage); - } + editorPart = EditorUtility.openInEditor(location.getLocation(), null); } // highlight the type in the editor diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/dnd/TextEditorDropAdapter.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/dnd/TextEditorDropAdapter.java index d8fd34394ad..86bb3dd37f5 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/dnd/TextEditorDropAdapter.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/dnd/TextEditorDropAdapter.java @@ -38,8 +38,6 @@ import org.eclipse.swt.graphics.Point; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.widgets.Caret; import org.eclipse.swt.widgets.Display; -import org.eclipse.ui.IEditorDescriptor; -import org.eclipse.ui.IEditorInput; import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.IWorkbenchPart; import org.eclipse.ui.PartInitException; @@ -360,10 +358,7 @@ public class TextEditorDropAdapter extends DropTargetAdapter implements null)); } if (file.canRead()) { - IEditorInput editorInput= EditorUtility.getEditorInputForLocation(path); - IEditorDescriptor desc= IDE.getEditorDescriptor(file.getName()); - String editorId= desc.getId(); - IDE.openEditor(getPage(), editorInput, editorId); + EditorUtility.openInEditor(path, null); } else { throw new CoreException(new Status(IStatus.ERROR, CUIPlugin .getPluginId(), 0, CUIMessages.getFormattedString( diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CDocumentProvider.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CDocumentProvider.java index 8f52f0d4988..7bb6e85c2b4 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CDocumentProvider.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CDocumentProvider.java @@ -6,8 +6,8 @@ * http://www.eclipse.org/legal/epl-v10.html * * Contributors: - * QNX Software Systems - Initial API and implementation - * Anton Leherbauer (Wind River Systems) + * QNX Software Systems - Initial API and implementation + * Anton Leherbauer (Wind River Systems) *******************************************************************************/ package org.eclipse.cdt.internal.ui.editor; @@ -73,6 +73,7 @@ import org.eclipse.cdt.internal.core.model.IBufferFactory; import org.eclipse.cdt.internal.ui.CFileElementWorkingCopy; import org.eclipse.cdt.internal.ui.CPluginImages; import org.eclipse.cdt.internal.ui.text.IProblemRequestorExtension; +import org.eclipse.cdt.internal.ui.util.ExternalEditorInput; /** * CDocumentProvider2 @@ -863,6 +864,7 @@ public class CDocumentProvider extends TextFileDocumentProvider { if (fileBufferAnnotationModel != null) { ((AnnotationModel)tuInfo.fModel).addAnnotationModel("secondaryModel", fileBufferAnnotationModel); //$NON-NLS-1$ } + tuInfo.fCachedReadOnlyState= true; } if (tuInfo.fModel instanceof TranslationUnitAnnotationModel) { TranslationUnitAnnotationModel model= (TranslationUnitAnnotationModel) tuInfo.fModel; @@ -877,6 +879,30 @@ public class CDocumentProvider extends TextFileDocumentProvider { return tuInfo; } + /* + * @see org.eclipse.ui.editors.text.TextFileDocumentProvider#isReadOnly(java.lang.Object) + */ + public boolean isReadOnly(Object element) { + // external editor input must not be modified + // because of missing functionality in CFileElementWorkingCopy + if (element instanceof ExternalEditorInput) { + return true; + } + return super.isReadOnly(element); + } + + /* + * @see org.eclipse.ui.editors.text.TextFileDocumentProvider#isModifiable(java.lang.Object) + */ + public boolean isModifiable(Object element) { + // external editor input must not be modified + // because of missing functionality in CFileElementWorkingCopy + if (element instanceof ExternalEditorInput) { + return false; + } + return super.isModifiable(element); + } + /* * @see org.eclipse.ui.editors.text.TextFileDocumentProvider#disposeFileInfo(java.lang.Object, * org.eclipse.ui.editors.text.TextFileDocumentProvider.FileInfo) @@ -1053,20 +1079,6 @@ public class CDocumentProvider extends TextFileDocumentProvider { disconnect(e.next()); } - /** - * Returns the underlying resource for the given element. - * - * @param element the element - * @return the underlying resource of the given element - */ - public IResource getUnderlyingResource(Object element) { - if (element instanceof IFileEditorInput) { - IFileEditorInput input= (IFileEditorInput) element; - return input.getFile(); - } - return null; - } - public ILineTracker createLineTracker(Object element) { return new DefaultLineTracker(); } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/ExternalSearchAnnotationModel.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/ExternalSearchAnnotationModel.java index e0f7bfca2da..9de435b7f38 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/ExternalSearchAnnotationModel.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/ExternalSearchAnnotationModel.java @@ -1,5 +1,5 @@ /********************************************************************** - * Copyright (c) 2002, 2005 IBM Corporation and others. + * Copyright (c) 2002, 2006 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -7,16 +7,15 @@ * * Contributors: * IBM Rational Software - Initial Contribution + * Norbert Ploett (Siemens AG) + * Anton Leherbauer (Wind River Systems) **********************************************************************/ package org.eclipse.cdt.internal.ui.editor; import org.eclipse.core.resources.IMarker; import org.eclipse.core.resources.IResource; -import org.eclipse.core.resources.IResourceChangeEvent; -import org.eclipse.core.resources.IResourceChangeListener; import org.eclipse.core.resources.IStorage; -import org.eclipse.core.resources.IWorkspace; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.Path; @@ -24,35 +23,54 @@ import org.eclipse.ui.texteditor.ResourceMarkerAnnotationModel; import org.eclipse.cdt.core.model.ICModelMarker; -public class ExternalSearchAnnotationModel extends - ResourceMarkerAnnotationModel implements IResourceChangeListener { +public class ExternalSearchAnnotationModel extends ResourceMarkerAnnotationModel { - protected IWorkspace fWorkspace; - protected IStorage fStorage; - protected boolean fChangesApplied; + private final IStorage fStorage; + private final int fDepth; + private final String fLocationAttribute; + + /** + * @param markerResource + * @param storage + */ + public ExternalSearchAnnotationModel(IResource markerResource, IStorage storage) { + this(markerResource, storage, IResource.DEPTH_ZERO, ICModelMarker.C_MODEL_MARKER_EXTERNAL_LOCATION); + } /** - * @param resource + * @param markerResource + * @param storage + * @param depth */ - public ExternalSearchAnnotationModel(IResource resource, IStorage storage) { - super(resource); - this.fWorkspace = resource.getWorkspace(); - this.fStorage = storage; + ExternalSearchAnnotationModel(IResource markerResource, IStorage storage, int depth) { + this(markerResource, storage, depth, ICModelMarker.C_MODEL_MARKER_EXTERNAL_LOCATION); + } + + /** + * @param markerResource + * @param storage + * @param depth + * @param locationAttribute + */ + ExternalSearchAnnotationModel(IResource markerResource, + IStorage storage, int depth, String locationAttribute) { + super(markerResource); + fStorage= storage; + fDepth= depth; + fLocationAttribute= locationAttribute; } protected IMarker[] retrieveMarkers() throws CoreException { IMarker[] markers = null; if (getResource() != null) { - markers = getResource().findMarkers(IMarker.MARKER, true, - IResource.DEPTH_ZERO); + markers = getResource().findMarkers(IMarker.MARKER, true, fDepth); } return markers; } protected boolean isAcceptable(IMarker marker) { boolean acceptable = false; - String externalFileName = marker.getAttribute( - ICModelMarker.C_MODEL_MARKER_EXTERNAL_LOCATION, null); + String externalFileName = marker.getAttribute(fLocationAttribute, null); if (externalFileName != null) { // Only accept markers with external // paths set IPath externalPath = new Path(externalFileName); @@ -66,13 +84,4 @@ public class ExternalSearchAnnotationModel extends return acceptable; } - /* - * (non-Javadoc) - * - * @see org.eclipse.core.resources.IResourceChangeListener#resourceChanged(org.eclipse.core.resources.IResourceChangeEvent) - */ - public void resourceChanged(IResourceChangeEvent event) { - - } - } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/ExternalSearchDocumentProvider.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/ExternalSearchDocumentProvider.java index bf9958a80c2..0b2d66edafa 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/ExternalSearchDocumentProvider.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/ExternalSearchDocumentProvider.java @@ -7,93 +7,98 @@ * * Contributors: * IBM Corporation - initial API and implementation + * Norbert Ploett (Siemens AG) + * Anton Leherbauer (Wind River Systems) *******************************************************************************/ package org.eclipse.cdt.internal.ui.editor; -import org.eclipse.cdt.internal.ui.text.CTextTools; -import org.eclipse.cdt.internal.ui.util.ExternalEditorInput; -import org.eclipse.cdt.ui.CUIPlugin; -import org.eclipse.core.resources.IFile; -import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.IStorage; import org.eclipse.core.runtime.CoreException; -import org.eclipse.jface.text.IDocument; +import org.eclipse.core.runtime.IAdaptable; +import org.eclipse.core.runtime.IPath; +import org.eclipse.jface.text.source.AnnotationModel; import org.eclipse.jface.text.source.IAnnotationModel; -import org.eclipse.ui.editors.text.FileDocumentProvider; +import org.eclipse.ui.IPathEditorInput; +import org.eclipse.ui.IStorageEditorInput; +import org.eclipse.ui.editors.text.ILocationProvider; -public class ExternalSearchDocumentProvider extends FileDocumentProvider { +import org.eclipse.cdt.core.resources.FileStorage; +import org.eclipse.cdt.ui.CUIPlugin; + +import org.eclipse.cdt.internal.ui.util.ExternalEditorInput; + +public class ExternalSearchDocumentProvider extends CStorageDocumentProvider { + /** Location attribute for breakpoints. See ICBreakpoint.SOURCE_HANDLE */ + private static final String DEBUG_SOURCE_HANDLE = "org.eclipse.cdt.debug.core.sourceHandle"; //$NON-NLS-1$ + public ExternalSearchDocumentProvider(){ super(); } /* - * @see AbstractDocumentProvider#createElementInfo(Object) + * @see org.eclipse.ui.editors.text.StorageDocumentProvider#createAnnotationModel(java.lang.Object) */ - protected ElementInfo createElementInfo(Object element) throws CoreException { - + protected IAnnotationModel createAnnotationModel(Object element) throws CoreException { if (element instanceof ExternalEditorInput) { - - ExternalEditorInput externalInput = (ExternalEditorInput) element; - - IDocument d = createDocument(externalInput); - IAnnotationModel m= createExternalSearchAnnotationModel(externalInput); - - FileInfo info= new FileInfo(d, m, null); - return info; + return createExternalSearchAnnotationModel((ExternalEditorInput)element); } - return super.createElementInfo(element); + if (element instanceof IStorageEditorInput) { + IStorage storage= ((IStorageEditorInput)element).getStorage(); + if (storage.getFullPath() != null) { + return createExternalSearchAnnotationModel(storage, null); + } + } + if (element instanceof IPathEditorInput) { + IPath path= ((IPathEditorInput)element).getPath(); + IStorage storage= new FileStorage(path); + return createExternalSearchAnnotationModel(storage, null); + } + if (element instanceof IAdaptable) { + IAdaptable adaptable= (IAdaptable) element; + ILocationProvider provider= (ILocationProvider) adaptable.getAdapter(ILocationProvider.class); + if (provider != null) { + IPath path= provider.getPath(element); + IStorage storage= new FileStorage(path); + return createExternalSearchAnnotationModel(storage, null); + } + } + return super.createAnnotationModel(element); + } + + /** + * Create an annotation model for the given {@link ExternalEditorInput}. + * + * @param externalInput + * @return a new annotation model for the external editor input + */ + private IAnnotationModel createExternalSearchAnnotationModel(ExternalEditorInput externalInput) { + IStorage storage = externalInput.getStorage(); + IResource markerResource = externalInput.getMarkerResource(); + return createExternalSearchAnnotationModel(storage, markerResource); } /** - * @param externalInput - * @return a new annotation model for the editor input + * Create an annotation model for the given file and associated resource marker. + * + * @param storage the file in the form of a IStorage + * @param markerResource the resource to retrieve markers from, may be null + * @return a new annotation model for the file */ - private IAnnotationModel createExternalSearchAnnotationModel(ExternalEditorInput externalInput) { - - IStorage storage = externalInput.getStorage(); - - IResource resourceToUseForMarker = null; - - IFile resourceFile = CUIPlugin.getWorkspace().getRoot().getFileForLocation(storage.getFullPath()); - - if (resourceFile == null){ - - resourceToUseForMarker = externalInput.getMarkerResource(); - - if (null==resourceToUseForMarker) { - IProject[] proj = CUIPlugin.getWorkspace().getRoot() - .getProjects(); - for (int i = 0; i < proj.length; i++) { - if (proj[i].isOpen()) { - resourceToUseForMarker = proj[i]; - break; - } - } - } + private IAnnotationModel createExternalSearchAnnotationModel(IStorage storage, IResource markerResource) { + AnnotationModel model= null; + if (markerResource != null){ + model = new ExternalSearchAnnotationModel(markerResource, storage); + } else { + // no marker resource available - search workspace root and all project resources (depth one) + markerResource= CUIPlugin.getWorkspace().getRoot(); + model = new ExternalSearchAnnotationModel(markerResource, storage, IResource.DEPTH_ONE); } - else { - resourceToUseForMarker = resourceFile.getProject(); - } - - if (resourceToUseForMarker != null){ - ExternalSearchAnnotationModel model = new ExternalSearchAnnotationModel(resourceToUseForMarker, storage); - return model; - } - return null; + // attach annotation model for C breakpoints + model.addAnnotationModel("debugMarkerModel", new ExternalSearchAnnotationModel(markerResource, storage, IResource.DEPTH_ZERO, DEBUG_SOURCE_HANDLE)); //$NON-NLS-1$ + return model; } - /* - * @see AbstractDocumentProvider#createDocument(Object) - */ - protected IDocument createDocument(Object element) throws CoreException { - IDocument document= super.createDocument(element); - if (document != null){ - CTextTools textTools = CUIPlugin.getDefault().getTextTools(); - textTools.setupCDocument(document); - } - return document; - } } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/OpenIncludeAction.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/OpenIncludeAction.java index 71e173fb1e2..76c47a3e6f8 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/OpenIncludeAction.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/OpenIncludeAction.java @@ -17,20 +17,7 @@ import java.util.ArrayList; import java.util.HashSet; import java.util.List; -import org.eclipse.cdt.core.CCorePlugin; -import org.eclipse.cdt.core.model.CModelException; -import org.eclipse.cdt.core.model.CoreModel; -import org.eclipse.cdt.core.model.ICElement; -import org.eclipse.cdt.core.model.ICProject; -import org.eclipse.cdt.core.model.ITranslationUnit; -import org.eclipse.cdt.core.parser.IScannerInfo; -import org.eclipse.cdt.core.parser.IScannerInfoProvider; -import org.eclipse.cdt.internal.ui.CPluginImages; -import org.eclipse.cdt.internal.ui.dialogs.ElementListSelectionDialog; -import org.eclipse.cdt.internal.ui.util.EditorUtility; -import org.eclipse.cdt.ui.CUIPlugin; import org.eclipse.core.resources.IContainer; -import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.IResourceProxy; @@ -52,6 +39,17 @@ import org.eclipse.ui.IEditorDescriptor; import org.eclipse.ui.IEditorRegistry; import org.eclipse.ui.PlatformUI; +import org.eclipse.cdt.core.CCorePlugin; +import org.eclipse.cdt.core.model.CModelException; +import org.eclipse.cdt.core.model.ICElement; +import org.eclipse.cdt.core.parser.IScannerInfo; +import org.eclipse.cdt.core.parser.IScannerInfoProvider; +import org.eclipse.cdt.ui.CUIPlugin; + +import org.eclipse.cdt.internal.ui.CPluginImages; +import org.eclipse.cdt.internal.ui.dialogs.ElementListSelectionDialog; +import org.eclipse.cdt.internal.ui.util.EditorUtility; + public class OpenIncludeAction extends Action { @@ -117,25 +115,7 @@ public class OpenIncludeAction extends Action { } if (fileToOpen != null) { - IFile file = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(fileToOpen); - if (file != null) { - EditorUtility.openInEditor(file); - } else { - ICProject cproject = include.getCProject(); - ITranslationUnit unit = CoreModel.getDefault().createTranslationUnitFrom(cproject, fileToOpen); - if (unit != null) { - EditorUtility.openInEditor(unit); - } else { - // try linked files - IFile[] files = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocation(fileToOpen); - for(int i=0; i 0) { - IEditorPart editor = IDE.openEditor(CUIPlugin.getActivePage(), files[0]); - try { - IMarker marker = files[0].createMarker(NewSearchUI.SEARCH_MARKER); - marker.setAttribute(IMarker.CHAR_START, currentOffset); - marker.setAttribute(IMarker.CHAR_END, currentOffset + currentLength); - IDE.gotoMarker(editor, marker); - marker.delete(); - } catch (CoreException e) { - CUIPlugin.getDefault().log(e); - } + protected void open(IPath path, int currentOffset, int currentLength) throws CoreException { + IEditorPart editor = EditorUtility.openInEditor(path, fEditor.getInputCElement()); + if (editor instanceof ITextEditor) { + ITextEditor textEditor = (ITextEditor)editor; + textEditor.selectAndReveal(currentOffset, currentLength); } else { - // external file - IEditorInput input = new ExternalEditorInput(new FileStorage(path)); - IEditorPart editor = CUIPlugin.getActivePage().openEditor(input, ExternalSearchEditor.EDITOR_ID); - if (editor instanceof ITextEditor) { - ITextEditor textEditor = (ITextEditor)editor; - textEditor.selectAndReveal(currentOffset, currentLength); - } + // TODO: report error } } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/util/EditorUtility.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/util/EditorUtility.java index 6bb4e8be175..fbd6135f0fe 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/util/EditorUtility.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/util/EditorUtility.java @@ -8,29 +8,16 @@ * Contributors: * QNX Software Systems - Initial API and implementation * Markus Schorn (Wind River Systems) + * Norbert Ploett (Siemens AG) + * Anton Leherbauer (Wind River Systems) *******************************************************************************/ package org.eclipse.cdt.internal.ui.util; - import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.text.MessageFormat; -import org.eclipse.cdt.core.model.CModelException; -import org.eclipse.cdt.core.model.CoreModel; -import org.eclipse.cdt.core.model.IBinary; -import org.eclipse.cdt.core.model.IBuffer; -import org.eclipse.cdt.core.model.ICElement; -import org.eclipse.cdt.core.model.ISourceReference; -import org.eclipse.cdt.core.model.ITranslationUnit; -import org.eclipse.cdt.core.model.IWorkingCopy; -import org.eclipse.cdt.core.resources.FileStorage; -import org.eclipse.cdt.internal.ui.editor.CEditor; -import org.eclipse.cdt.internal.ui.editor.CEditorMessages; -import org.eclipse.cdt.internal.ui.editor.ITranslationUnitEditorInput; -import org.eclipse.cdt.ui.CUIPlugin; - import org.eclipse.core.filebuffers.FileBuffers; import org.eclipse.core.resources.IContainer; import org.eclipse.core.resources.IFile; @@ -41,6 +28,8 @@ import org.eclipse.core.resources.IStorage; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.Path; +import org.eclipse.core.runtime.Platform; +import org.eclipse.core.runtime.content.IContentType; import org.eclipse.jface.action.Action; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.MessageBox; @@ -52,15 +41,31 @@ import org.eclipse.ui.IFileEditorInput; import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.PartInitException; import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.editors.text.EditorsUI; import org.eclipse.ui.part.FileEditorInput; -import org.eclipse.ui.texteditor.ITextEditor; + +import org.eclipse.cdt.core.model.CModelException; +import org.eclipse.cdt.core.model.CoreModel; +import org.eclipse.cdt.core.model.IBinary; +import org.eclipse.cdt.core.model.IBuffer; +import org.eclipse.cdt.core.model.ICElement; +import org.eclipse.cdt.core.model.ICProject; +import org.eclipse.cdt.core.model.ISourceReference; +import org.eclipse.cdt.core.model.ITranslationUnit; +import org.eclipse.cdt.core.model.IWorkingCopy; +import org.eclipse.cdt.core.resources.FileStorage; +import org.eclipse.cdt.ui.CUIPlugin; + +import org.eclipse.cdt.internal.ui.editor.CEditor; +import org.eclipse.cdt.internal.ui.editor.CEditorMessages; +import org.eclipse.cdt.internal.ui.editor.ITranslationUnitEditorInput; public class EditorUtility { /** * The ID of the default text editor */ - public static final String DEFAULT_TEXT_EDITOR_ID = "org.eclipse.ui.DefaultTextEditor"; //$NON-NLS-1$ + public static final String DEFAULT_TEXT_EDITOR_ID = EditorsUI.DEFAULT_TEXT_EDITOR_ID; private EditorUtility () { } @@ -88,18 +93,18 @@ public class EditorUtility { return null; } - /** - * Opens a Java editor for an element such as IJavaElement, IFile, or IStorage. + * Opens an editor for an element such as ICElement, + * IFile, or IStorage. * 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...) + * Opens an editor for an element (ICElement, 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 { @@ -109,15 +114,11 @@ public class EditorUtility { } 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; } @@ -184,8 +185,8 @@ public class EditorUtility { } /** + * Open error dialog about closed project. * @param project - * */ private static void closedProject(IProject project) { MessageBox errorMsg = new MessageBox(CUIPlugin.getActiveWorkbenchShell(), SWT.ICON_ERROR | SWT.OK); @@ -201,23 +202,12 @@ public class EditorUtility { 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()) @@ -253,11 +243,9 @@ public class EditorUtility { if (input instanceof ICElement) { return getEditorInput((ICElement) input); } - if (input instanceof IFile) { return new FileEditorInput((IFile) input); } - if (input instanceof IStorage) { return new ExternalEditorInput((IStorage)input); } @@ -270,16 +258,65 @@ public class EditorUtility { * is returned, otherwise, the input is an IStorageEditorInput * assuming the location points to an existing file in the file system. * - * @param location a valid file system location + * @param location a valid file system location * @return an editor input + * @deprecated Use {@link #getEditorInputForLocation(IPath, ICElement)} instead */ public static IEditorInput getEditorInputForLocation(IPath location) { + return getEditorInputForLocation(location, null); + } + + /** + * Utility method to open an editor for the given file system location + * using {@link #getEditorInputForLocation(IPath, ICElement)} to create + * the editor input. + * + * @param location a file system location + * @param element an element related to the target file, may be null + * @throws PartInitException + */ + public static IEditorPart openInEditor(IPath location, ICElement element) throws PartInitException { + IEditorInput input= getEditorInputForLocation(location, element); + return EditorUtility.openInEditor(input, getEditorID(input, element), true); + } + + /** + * Utility method to get an editor input for the given file system location. + * If the location denotes a workspace file, a FileEditorInput + * is returned, otherwise, the input is an IStorageEditorInput + * assuming the location points to an existing file in the file system. + * The ICElement is used to determine the associated project + * in case the location can not be resolved to a workspace IFile. + * + * @param location a valid file system location + * @param element an element related to the target file, may be null + * @return an editor input + */ + public static IEditorInput getEditorInputForLocation(IPath location, ICElement element) { IFile resource= FileBuffers.getWorkspaceFileAtLocation(location); if (resource != null) { return new FileEditorInput(resource); - } else { - return new ExternalEditorInput(new FileStorage(location)); + } else if (element != null) { + // try to get a translation unit from the location and associated element + ICProject cproject= element.getCProject(); + if (cproject != null) { + ITranslationUnit unit = CoreModel.getDefault().createTranslationUnitFrom(cproject, location); + if (unit != null) { + return new ExternalEditorInput(unit, new FileStorage(location)); + } + } + // no translation unit - still try to get a sensible marker resource + // from the associated element + if (element instanceof IWorkingCopy && ((IWorkingCopy) element).isWorkingCopy()) { + element= ((IWorkingCopy) element).getOriginalElement(); + } + IResource markerResource= null; + if (element != null) { + markerResource= element.getUnderlyingResource(); + } + return new ExternalEditorInput(new FileStorage(location), markerResource); } + return new ExternalEditorInput(new FileStorage(location)); } @@ -316,30 +353,13 @@ public class EditorUtility { return cu.findSharedWorkingCopy(CUIPlugin.getDefault().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()); -// } - + /** + * Determine the editor id from the given file name using + * the workspace-wide content-type definitions. + * + * @param name the file name + * @return a valid editor id, never null + */ public static String getEditorID(String name) { IEditorRegistry registry = PlatformUI.getWorkbench().getEditorRegistry(); if (registry != null) { @@ -347,27 +367,27 @@ public class EditorUtility { if (descriptor != null) { return descriptor.getId(); } - return registry.findEditor(DEFAULT_TEXT_EDITOR_ID).getId(); } - return null; + return DEFAULT_TEXT_EDITOR_ID; } + /** + * Determine the editor id from the given editor input and optional input object. + * When a translation unit can be obtained, the project-specific content-type + * mechanism is used to determine the correct editor id. + * If that fails, the editor id is determined by file name and extension using + * the workspace-wide content-type definitions. + * + * @param input the editor input + * @param inputObject the input object (used to create the editor input) or null + * @return a valid editor id, never null + */ public static String getEditorID(IEditorInput input, Object inputObject) { - String ID = getEditorID(input.getName()); - - if (!"org.eclipse.ui.DefaultTextEditor".equals(ID)) { //$NON-NLS-1$ - return ID; - } - - // TODO:FIXME:HACK etc ... - // Unfortunately unless specifying all of possible headers in the plugin.xml - // and it is not possible(for example filenames: iostream, cstdlib, etc ... - // We try this hack here. This is to be remove when the Eclipse Platform - // implement there contentious IContentType ITranslationUnit tunit = null; - - if (input instanceof IFileEditorInput) { + if (inputObject instanceof ITranslationUnit) { + tunit= (ITranslationUnit)inputObject; + } else if (input instanceof IFileEditorInput) { IFileEditorInput editorInput = (IFileEditorInput)input; IFile file = editorInput.getFile(); ICElement celement = CoreModel.getDefault().create(file); @@ -379,20 +399,31 @@ public class EditorUtility { tunit = editorInput.getTranslationUnit(); } - // Choose an a file base on the extension. if (tunit != null) { + // Choose an editor based on the content type + String contentTypeId= tunit.getContentTypeId(); + if (contentTypeId != null) { + IContentType contentType= Platform.getContentTypeManager().getContentType(contentTypeId); + IEditorRegistry registry = PlatformUI.getWorkbench().getEditorRegistry(); + IEditorDescriptor desc= registry.getDefaultEditor(input.getName(), contentType); + if (desc != null) { + return desc.getId(); + } + } + // Choose an editor based on the language (obsolete?) if (tunit.isCLanguage()) { - return "org.eclipse.cdt.ui.editor.CEditor"; //$NON-NLS-1$ - //return getEditorID("No_ExIsTeNt_FiLe.c");//$NON-NLS-1$ + return CUIPlugin.EDITOR_ID; } else if (tunit.isCXXLanguage()) { - return "org.eclipse.cdt.ui.editor.CEditor"; //$NON-NLS-1$ - //return getEditorID("No_ExIsTeNt_FiLe.cpp");//$NON-NLS-1$ + return CUIPlugin.EDITOR_ID; } else if (tunit.isASMLanguage()) { return "org.eclipse.cdt.ui.editor.asm.AsmEditor"; //$NON-NLS-1$ } } - return ID; + // Choose an editor based on filename/extension + String editorId = getEditorID(input.getName()); + + return editorId; } /** diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/util/ExternalEditorInput.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/util/ExternalEditorInput.java index b158fe0cb58..2ee18be36db 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/util/ExternalEditorInput.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/util/ExternalEditorInput.java @@ -134,6 +134,7 @@ public class ExternalEditorInput implements ITranslationUnitEditorInput { public ExternalEditorInput(ITranslationUnit unit, IStorage exFile) { this(exFile, exFile.getFullPath()); this.unit = unit; + markerResource= unit.getCProject().getProject(); } public ExternalEditorInput(IStorage exFile) { diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/util/OpenExternalProblemAction.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/util/OpenExternalProblemAction.java index 1cf835da28c..40d3235309a 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/util/OpenExternalProblemAction.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/util/OpenExternalProblemAction.java @@ -1,12 +1,13 @@ /******************************************************************************* - * Copyright (c) 2006 Siemens AG. + * Copyright (c) 2006 Siemens AG and others. * All rights reserved. This content and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: - * Norbert Ploett - Initial implementation + * Norbert Ploett (Siemens AG) + * Anton Leherbauer (Wind River Systems) *******************************************************************************/ package org.eclipse.cdt.internal.ui.util; @@ -17,31 +18,22 @@ import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.Path; import org.eclipse.jface.action.IAction; -import org.eclipse.jface.text.BadLocationException; -import org.eclipse.jface.text.IDocument; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.swt.widgets.Event; -import org.eclipse.ui.IEditorInput; import org.eclipse.ui.IEditorPart; import org.eclipse.ui.IObjectActionDelegate; import org.eclipse.ui.IWorkbenchPart; import org.eclipse.ui.actions.ActionDelegate; -import org.eclipse.ui.texteditor.ITextEditor; +import org.eclipse.ui.ide.IDE; -import org.eclipse.cdt.core.model.CoreModel; import org.eclipse.cdt.core.model.ICModelMarker; import org.eclipse.cdt.core.model.ICProject; -import org.eclipse.cdt.core.model.ITranslationUnit; -import org.eclipse.cdt.core.resources.FileStorage; import org.eclipse.cdt.ui.CUIPlugin; import org.eclipse.cdt.internal.core.model.CModelManager; -import org.eclipse.cdt.internal.ui.editor.ExternalSearchEditor; - -public class OpenExternalProblemAction extends ActionDelegate implements IObjectActionDelegate - { +public class OpenExternalProblemAction extends ActionDelegate implements IObjectActionDelegate { IStructuredSelection selection ; @@ -59,38 +51,16 @@ public class OpenExternalProblemAction extends ActionDelegate implements IObject IMarker marker = (IMarker) object; Object attributeObject = marker.getAttribute(ICModelMarker.C_MODEL_MARKER_EXTERNAL_LOCATION); if (attributeObject instanceof String) { - String externalLocation = (String) attributeObject ; + String externalLocation = (String) attributeObject; IPath externalPath = new Path(externalLocation); - IEditorPart editor = null ; - // Try to open a C editor with the project and the path - ICProject cproject = getCProject(marker); - if (null!=cproject) { - ITranslationUnit tu = CoreModel.getDefault() - .createTranslationUnitFrom(cproject, - externalPath); - if (null!=tu) { - editor = EditorUtility.openInEditor(tu); - } - } else { - // Open in plain external editor - IEditorInput input = new ExternalEditorInput(new FileStorage(externalPath), marker.getResource()); - editor = CUIPlugin.getActivePage().openEditor(input, ExternalSearchEditor.EDITOR_ID); - } - if (editor instanceof ITextEditor) { - int lineNumber = marker.getAttribute(IMarker.LINE_NUMBER, 0); - int currentOffset = 0 ; - int currentLength = 0; - ITextEditor textEditor = (ITextEditor) editor; - IEditorInput ediinput = textEditor.getEditorInput(); - IDocument document = textEditor.getDocumentProvider().getDocument(ediinput); - try { - currentOffset = document.getLineOffset(lineNumber-1); - } catch (BadLocationException e) { - } - textEditor.selectAndReveal(currentOffset, currentLength); + + IEditorPart editor = EditorUtility.openInEditor(externalPath, getCProject(marker)); + if (editor != null) { + IDE.gotoMarker(editor, marker); } } } catch (CoreException e) { + CUIPlugin.getDefault().log(e.getStatus()); } } } @@ -123,6 +93,5 @@ public class OpenExternalProblemAction extends ActionDelegate implements IObject } } } - }