Reuse opened editor
diff --git a/bundles/org.eclipse.ui/Eclipse UI Standard Components/org/eclipse/ui/views/navigator/ResourceNavigator.java b/bundles/org.eclipse.ui/Eclipse UI Standard Components/org/eclipse/ui/views/navigator/ResourceNavigator.java
index 6f6e513..f9186bd 100644
--- a/bundles/org.eclipse.ui/Eclipse UI Standard Components/org/eclipse/ui/views/navigator/ResourceNavigator.java
+++ b/bundles/org.eclipse.ui/Eclipse UI Standard Components/org/eclipse/ui/views/navigator/ResourceNavigator.java
@@ -477,8 +477,7 @@
IStructuredSelection s = (IStructuredSelection)event.getSelection();
Object element = s.getFirstElement();
if (element instanceof IFile) {
- openFileAction.selectionChanged(s);
- openFileAction.run();
+ runOpenFileAction(s);
}
else {
// 1GBZIA0: ITPUI:WIN2000 - Double-clicking in navigator should expand/collapse containers
@@ -486,7 +485,12 @@
viewer.setExpandedState(element, !viewer.getExpandedState(element));
}
}
-
+}
+/**
+ */
+void runOpenFileAction(IStructuredSelection s) {
+ openFileAction.selectionChanged(s);
+ openFileAction.run();
}
/**
* Handles key events in viewer.
@@ -506,7 +510,8 @@
updateStatusLine(sel);
goIntoAction.update();
updateGlobalActions(sel);
- linkToEditor(sel);
+ if(!linkToEditor(sel))
+ runOpenFileAction(sel);
}
/* (non-Javadoc)
* Method declared on IViewPart.
@@ -591,9 +596,9 @@
/**
* Links to editor (if option enabled)
*/
-void linkToEditor(IStructuredSelection selection) {
+boolean linkToEditor(IStructuredSelection selection) {
if (!isLinkingEnabled())
- return;
+ return false;
Object obj = selection.getFirstElement();
if (obj instanceof IFile && selection.size() == 1) {
@@ -605,10 +610,11 @@
IEditorInput input = editor.getEditorInput();
if (input instanceof IFileEditorInput && file.equals(((IFileEditorInput)input).getFile())) {
page.bringToTop(editor);
- return;
+ return true;
}
}
}
+ return false;
}
/**
* Create self's action objects
diff --git a/bundles/org.eclipse.ui/Eclipse UI Text Editor/org/eclipse/ui/texteditor/AbstractTextEditor.java b/bundles/org.eclipse.ui/Eclipse UI Text Editor/org/eclipse/ui/texteditor/AbstractTextEditor.java
index a821919..f182457 100644
--- a/bundles/org.eclipse.ui/Eclipse UI Text Editor/org/eclipse/ui/texteditor/AbstractTextEditor.java
+++ b/bundles/org.eclipse.ui/Eclipse UI Text Editor/org/eclipse/ui/texteditor/AbstractTextEditor.java
@@ -85,6 +85,7 @@
import org.eclipse.ui.actions.WorkspaceModifyOperation;
import org.eclipse.ui.help.WorkbenchHelp;
import org.eclipse.ui.part.EditorPart;
+import org.eclipse.ui.IReusableEditor;
@@ -98,7 +99,7 @@
*
* @see org.eclipse.ui.editors.text.TextEditor
*/
-public abstract class AbstractTextEditor extends EditorPart implements ITextEditor {
+public abstract class AbstractTextEditor extends EditorPart implements ITextEditor, IReusableEditor {
/**
* Internal element state listener.
@@ -1791,4 +1792,13 @@
updateAction((String) e.next());
}
}
+
+ public boolean getReuseEditor() {
+ return true;
+
+ }
+
+ public void setReuseEditor(boolean reuse) {
+ }
+
}
diff --git a/bundles/org.eclipse.ui/Eclipse UI/org/eclipse/ui/IReusableEditor.java b/bundles/org.eclipse.ui/Eclipse UI/org/eclipse/ui/IReusableEditor.java
new file mode 100644
index 0000000..1ffd7a8
--- /dev/null
+++ b/bundles/org.eclipse.ui/Eclipse UI/org/eclipse/ui/IReusableEditor.java
@@ -0,0 +1,8 @@
+package org.eclipse.ui;
+
+public interface IReusableEditor extends IEditorPart {
+ public boolean getReuseEditor();
+ public void setReuseEditor(boolean reuse);
+ public void setInput(IEditorInput newInput);
+}
+
diff --git a/bundles/org.eclipse.ui/Eclipse UI/org/eclipse/ui/internal/EditorManager.java b/bundles/org.eclipse.ui/Eclipse UI/org/eclipse/ui/internal/EditorManager.java
index 99bd77d..b48d4d0 100644
--- a/bundles/org.eclipse.ui/Eclipse UI/org/eclipse/ui/internal/EditorManager.java
+++ b/bundles/org.eclipse.ui/Eclipse UI/org/eclipse/ui/internal/EditorManager.java
@@ -264,7 +264,6 @@
throws PartInitException
{
IFile file = input.getFile();
-
// If there is a registered editor for the file use it.
EditorDescriptor desc = (EditorDescriptor)getEditorRegistry().
getDefaultEditor(file);
@@ -290,12 +289,79 @@
desc = (EditorDescriptor)getEditorRegistry().getDefaultEditor();
return openEditor(desc, input);
}
+
+/*
+ *
+ */
+private IReusableEditor findReusableEditor(EditorDescriptor desc) {
+ //Must get global preference: "Reuse opened editors".
+ //if(!reuseEditors)
+ // return null;
+ IEditorPart editors[] = getEditors();
+ IReusableEditor dirtyEditor = null;
+ IWorkbenchPart activePart = page.getActivePart();
+ //Find IReusableEditor with the same descriptor id.
+ for(int i = 0;i < editors.length;i++) {
+ IEditorPart editor = editors[i];
+ if(editor == activePart)
+ continue;
+ if(!(editor instanceof IReusableEditor))
+ continue;
+ IReusableEditor reusableEditor = (IReusableEditor)editor;
+ if(!reusableEditor.getReuseEditor())
+ continue;
+ IEditorInput editorInput = reusableEditor.getEditorInput();
+ EditorSite site = (EditorSite)reusableEditor.getEditorSite();
+ EditorDescriptor oldDesc = site.getEditorDescriptor();
+ if(oldDesc == null)
+ oldDesc = (EditorDescriptor)getEditorRegistry().getDefaultEditor();
+ if(desc.getId().equals(oldDesc.getId())) {
+ if(editor.isDirty()) {
+ dirtyEditor = reusableEditor;
+ continue;
+ }
+ return reusableEditor;
+ }
+ }
+ if(dirtyEditor == null)
+ return null;
+
+ //Must get global preference: "Open new Editor when dirty"
+ //if(openNewWhenDirty)
+ // return null;
+ MessageDialog dialog = new MessageDialog(
+ window.getShell(),
+ "Reusing dirty editor",
+ null, // accept the default window icon
+ dirtyEditor.getEditorInput().getName() + " has being modified. Save changes?",
+ MessageDialog.QUESTION,
+ new String[] {IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL,"Open new editor"},
+ 0);
+ int result = dialog.open();
+ if(result == 0) { //YES
+ ProgressMonitorDialog pmd = new ProgressMonitorDialog(dialog.getShell());
+ pmd.open();
+ dirtyEditor.doSave(pmd.getProgressMonitor());
+ pmd.close();
+ } else if(result == 2) {
+ return null;
+ }
+ return dirtyEditor;
+}
/*
* See IWorkbenchPage.
*/
private IEditorPart openEditor(EditorDescriptor desc, IEditorInput input)
throws PartInitException {
if (desc.isInternal()) {
+ IReusableEditor reusableEditor = findReusableEditor(desc);
+ if(reusableEditor != null) {
+ reusableEditor.setInput(input);
+ // Record the happy event.
+ Workbench wb = (Workbench)window.getWorkbench();
+ wb.getEditorHistory().add(input, desc);
+ return reusableEditor;
+ }
return openInternalEditor(desc, input, true,null);
} else
if (desc.isOpenInPlace()) {
diff --git a/bundles/org.eclipse.ui/Eclipse UI/org/eclipse/ui/internal/EditorSite.java b/bundles/org.eclipse.ui/Eclipse UI/org/eclipse/ui/internal/EditorSite.java
index a9f2a9d..d26216f 100644
--- a/bundles/org.eclipse.ui/Eclipse UI/org/eclipse/ui/internal/EditorSite.java
+++ b/bundles/org.eclipse.ui/Eclipse UI/org/eclipse/ui/internal/EditorSite.java
@@ -55,4 +55,8 @@
public IEditorPart getEditorPart() {
return (IEditorPart)getPart();
}
+
+public EditorDescriptor getEditorDescriptor() {
+ return desc;
+}
}