mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-06-07 01:36:01 +02:00
Fix for 179456: [Editor] ExternalEditorInput should be persistable
This commit is contained in:
parent
c29ea1ef6b
commit
c444dbe646
3 changed files with 129 additions and 21 deletions
|
@ -60,6 +60,9 @@
|
||||||
<factory
|
<factory
|
||||||
class="org.eclipse.cdt.internal.ui.PersistableCElementFactory"
|
class="org.eclipse.cdt.internal.ui.PersistableCElementFactory"
|
||||||
id="org.eclipse.cdt.ui.PersistableCElementFactory"/>
|
id="org.eclipse.cdt.ui.PersistableCElementFactory"/>
|
||||||
|
<factory
|
||||||
|
class="org.eclipse.cdt.internal.ui.util.ExternalEditorInputFactory"
|
||||||
|
id="org.eclipse.cdt.ui.ExternalEditorInputFactory"/>
|
||||||
</extension>
|
</extension>
|
||||||
|
|
||||||
<!-- Implement our filters. -->
|
<!-- Implement our filters. -->
|
||||||
|
|
|
@ -12,26 +12,28 @@
|
||||||
package org.eclipse.cdt.internal.ui.util;
|
package org.eclipse.cdt.internal.ui.util;
|
||||||
|
|
||||||
|
|
||||||
import org.eclipse.jface.resource.ImageDescriptor;
|
|
||||||
|
|
||||||
import org.eclipse.cdt.core.model.ITranslationUnit;
|
|
||||||
import org.eclipse.cdt.internal.ui.editor.ITranslationUnitEditorInput;
|
|
||||||
|
|
||||||
import org.eclipse.core.resources.IResource;
|
import org.eclipse.core.resources.IResource;
|
||||||
import org.eclipse.core.resources.IStorage;
|
import org.eclipse.core.resources.IStorage;
|
||||||
|
import org.eclipse.core.runtime.CoreException;
|
||||||
import org.eclipse.core.runtime.IPath;
|
import org.eclipse.core.runtime.IPath;
|
||||||
import org.eclipse.core.runtime.Platform;
|
import org.eclipse.core.runtime.Platform;
|
||||||
|
import org.eclipse.jface.resource.ImageDescriptor;
|
||||||
import org.eclipse.ui.IEditorRegistry;
|
import org.eclipse.ui.IEditorRegistry;
|
||||||
|
import org.eclipse.ui.IMemento;
|
||||||
import org.eclipse.ui.IPersistableElement;
|
import org.eclipse.ui.IPersistableElement;
|
||||||
|
import org.eclipse.ui.IStorageEditorInput;
|
||||||
import org.eclipse.ui.PlatformUI;
|
import org.eclipse.ui.PlatformUI;
|
||||||
import org.eclipse.ui.editors.text.ILocationProvider;
|
import org.eclipse.ui.editors.text.ILocationProvider;
|
||||||
|
|
||||||
|
import org.eclipse.cdt.core.model.ITranslationUnit;
|
||||||
|
|
||||||
|
import org.eclipse.cdt.internal.ui.editor.ITranslationUnitEditorInput;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An EditorInput for an external (non-workspace) file.
|
* An EditorInput for an external (non-workspace) file.
|
||||||
*/
|
*/
|
||||||
public class ExternalEditorInput implements ITranslationUnitEditorInput {
|
public class ExternalEditorInput implements ITranslationUnitEditorInput, IPersistableElement {
|
||||||
|
|
||||||
private IStorage externalFile;
|
private IStorage externalFile;
|
||||||
private IResource markerResource;
|
private IResource markerResource;
|
||||||
|
@ -43,17 +45,21 @@ public class ExternalEditorInput implements ITranslationUnitEditorInput {
|
||||||
public boolean equals(Object obj) {
|
public boolean equals(Object obj) {
|
||||||
if (this == obj)
|
if (this == obj)
|
||||||
return true;
|
return true;
|
||||||
if (!(obj instanceof ExternalEditorInput))
|
if (!(obj instanceof IStorageEditorInput))
|
||||||
return false;
|
return false;
|
||||||
ExternalEditorInput other = (ExternalEditorInput)obj;
|
IStorageEditorInput other = (IStorageEditorInput)obj;
|
||||||
return externalFile.equals(other.externalFile);
|
try {
|
||||||
|
return externalFile.equals(other.getStorage());
|
||||||
|
} catch (CoreException exc) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* @see IEditorInput#exists()
|
* @see IEditorInput#exists()
|
||||||
*/
|
*/
|
||||||
public boolean exists() {
|
public boolean exists() {
|
||||||
// External file ca not be deleted
|
// External file can not be deleted
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,13 +68,8 @@ public class ExternalEditorInput implements ITranslationUnitEditorInput {
|
||||||
*/
|
*/
|
||||||
public Object getAdapter(Class adapter) {
|
public Object getAdapter(Class adapter) {
|
||||||
if (ILocationProvider.class.equals(adapter)) {
|
if (ILocationProvider.class.equals(adapter)) {
|
||||||
// workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=180003
|
|
||||||
// see also https://bugs.eclipse.org/bugs/show_bug.cgi?id=179982
|
|
||||||
if (location != null) {
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return Platform.getAdapterManager().getAdapter(this, adapter);
|
return Platform.getAdapterManager().getAdapter(this, adapter);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -91,7 +92,7 @@ public class ExternalEditorInput implements ITranslationUnitEditorInput {
|
||||||
* @see IEditorInput#getPersistable()
|
* @see IEditorInput#getPersistable()
|
||||||
*/
|
*/
|
||||||
public IPersistableElement getPersistable() {
|
public IPersistableElement getPersistable() {
|
||||||
return null;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -153,6 +154,18 @@ public class ExternalEditorInput implements ITranslationUnitEditorInput {
|
||||||
return markerResource;
|
return markerResource;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* @see org.eclipse.ui.IPersistableElement#getFactoryId()
|
||||||
|
*/
|
||||||
|
public String getFactoryId() {
|
||||||
|
return ExternalEditorInputFactory.ID;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* @see org.eclipse.ui.IPersistable#saveState(org.eclipse.ui.IMemento)
|
||||||
|
*/
|
||||||
|
public void saveState(IMemento memento) {
|
||||||
|
ExternalEditorInputFactory.saveState(memento, this);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,92 @@
|
||||||
|
/*******************************************************************************
|
||||||
|
* Copyright (c) 2007 Wind River Systems, Inc. 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
|
||||||
|
* http://www.eclipse.org/legal/epl-v10.html
|
||||||
|
*
|
||||||
|
* Contributors:
|
||||||
|
* Anton Leherbauer (Wind River Systems) - initial API and implementation
|
||||||
|
*******************************************************************************/
|
||||||
|
|
||||||
|
package org.eclipse.cdt.internal.ui.util;
|
||||||
|
|
||||||
|
import org.eclipse.core.resources.IProject;
|
||||||
|
import org.eclipse.core.resources.ResourcesPlugin;
|
||||||
|
import org.eclipse.core.runtime.IAdaptable;
|
||||||
|
import org.eclipse.core.runtime.IPath;
|
||||||
|
import org.eclipse.core.runtime.Path;
|
||||||
|
import org.eclipse.ui.IElementFactory;
|
||||||
|
import org.eclipse.ui.IMemento;
|
||||||
|
import org.eclipse.ui.IPersistableElement;
|
||||||
|
|
||||||
|
import org.eclipse.cdt.core.model.CoreModel;
|
||||||
|
import org.eclipse.cdt.core.model.ICProject;
|
||||||
|
import org.eclipse.cdt.core.model.ITranslationUnit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The ExternalEditorInputFactory is used to save and recreate an ExternalEditorInput object.
|
||||||
|
* As such, it implements the IPersistableElement interface for storage
|
||||||
|
* and the IElementFactory interface for recreation.
|
||||||
|
*
|
||||||
|
* @see IMemento
|
||||||
|
* @see IPersistableElement
|
||||||
|
* @see IElementFactory
|
||||||
|
*
|
||||||
|
* @since 4.0
|
||||||
|
*/
|
||||||
|
public class ExternalEditorInputFactory implements IElementFactory {
|
||||||
|
|
||||||
|
public static final String ID = "org.eclipse.cdt.ui.ExternalEditorInputFactory"; //$NON-NLS-1$
|
||||||
|
|
||||||
|
private static final String TAG_PATH = "path";//$NON-NLS-1$
|
||||||
|
private static final String TAG_PROJECT = "project";//$NON-NLS-1$
|
||||||
|
|
||||||
|
/*
|
||||||
|
* @see org.eclipse.ui.IElementFactory#createElement(org.eclipse.ui.IMemento)
|
||||||
|
*/
|
||||||
|
public IAdaptable createElement(IMemento memento) {
|
||||||
|
// Get the file name.
|
||||||
|
String fileName = memento.getString(TAG_PATH);
|
||||||
|
if (fileName == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
IPath location= new Path(fileName);
|
||||||
|
ICProject cProject= null;
|
||||||
|
|
||||||
|
String projectName= memento.getString(TAG_PROJECT);
|
||||||
|
if (projectName != null) {
|
||||||
|
IProject project= ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
|
||||||
|
if (project.isAccessible() && CoreModel.hasCNature(project)) {
|
||||||
|
cProject= CoreModel.getDefault().create(project);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return EditorUtility.getEditorInputForLocation(location, cProject);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Save the element state.
|
||||||
|
*
|
||||||
|
* @param memento the storage
|
||||||
|
* @param input the element
|
||||||
|
*/
|
||||||
|
static void saveState(IMemento memento, ExternalEditorInput input) {
|
||||||
|
IPath location= input.getPath(input);
|
||||||
|
if (location != null) {
|
||||||
|
memento.putString(TAG_PATH, location.toOSString());
|
||||||
|
}
|
||||||
|
IProject project= null;
|
||||||
|
ITranslationUnit unit= input.getTranslationUnit();
|
||||||
|
if (unit != null) {
|
||||||
|
project= unit.getCProject().getProject();
|
||||||
|
}
|
||||||
|
if (project == null && input.getMarkerResource() instanceof IProject) {
|
||||||
|
project= (IProject)input.getMarkerResource();
|
||||||
|
}
|
||||||
|
if (project != null) {
|
||||||
|
memento.putString(TAG_PROJECT, project.getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue