From c46a23e9fbcdb7cc180e23e6f668d943d752514c Mon Sep 17 00:00:00 2001 From: Daniel Rolka Date: Tue, 16 Apr 2013 16:11:23 +0200 Subject: Bug 338450 - [Compatibility] List of recently opened editors not persisted across workbench restarts --- .../org/eclipse/ui/internal/ReopenEditorMenu.java | 2 +- .../org/eclipse/ui/internal/Workbench.java | 102 +++++++++++++++++++++ 2 files changed, 103 insertions(+), 1 deletion(-) diff --git a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/ReopenEditorMenu.java b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/ReopenEditorMenu.java index 47f64f6deb0..d6644ec39fe 100644 --- a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/ReopenEditorMenu.java +++ b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/ReopenEditorMenu.java @@ -263,7 +263,7 @@ public class ReopenEditorMenu extends ContributionItem { } IEditorInput input = item.getInput(); IEditorDescriptor desc = item.getDescriptor(); - if (input == null || desc == null) { + if (input == null || !input.exists() || desc == null) { String title = WorkbenchMessages.OpenRecent_errorTitle; String msg = NLS.bind(WorkbenchMessages.OpenRecent_unableToOpen, itemName ); MessageDialog.openWarning(window.getShell(), title, msg); diff --git a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/Workbench.java b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/Workbench.java index dd2bb22e8e1..5d604acd219 100644 --- a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/Workbench.java +++ b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/Workbench.java @@ -22,6 +22,8 @@ import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; +import java.io.StringReader; +import java.io.StringWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Dictionary; @@ -59,6 +61,7 @@ import org.eclipse.core.runtime.IRegistryChangeEvent; import org.eclipse.core.runtime.IRegistryChangeListener; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.ListenerList; +import org.eclipse.core.runtime.MultiStatus; import org.eclipse.core.runtime.Platform; import org.eclipse.core.runtime.SafeRunner; import org.eclipse.core.runtime.Status; @@ -140,6 +143,7 @@ import org.eclipse.ui.IEditorPart; import org.eclipse.ui.IEditorRegistry; import org.eclipse.ui.IElementFactory; import org.eclipse.ui.ILocalWorkingSetManager; +import org.eclipse.ui.IMemento; import org.eclipse.ui.IPerspectiveDescriptor; import org.eclipse.ui.IPerspectiveRegistry; import org.eclipse.ui.ISaveableFilter; @@ -160,6 +164,7 @@ import org.eclipse.ui.IWorkingSetManager; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.Saveable; import org.eclipse.ui.WorkbenchException; +import org.eclipse.ui.XMLMemento; import org.eclipse.ui.activities.IWorkbenchActivitySupport; import org.eclipse.ui.application.WorkbenchAdvisor; import org.eclipse.ui.browser.IWorkbenchBrowserSupport; @@ -264,6 +269,8 @@ public final class Workbench extends EventManager implements IWorkbench { public static String WORKBENCH_AUTO_SAVE_JOB = "Workbench Auto-Save Job"; //$NON-NLS-1$ + private static String MEMENTO_KEY = "memento"; //$NON-NLS-1$ + private final class StartupProgressBundleListener implements SynchronousBundleListener { private final IProgressMonitor progressMonitor; @@ -1144,6 +1151,28 @@ public final class Workbench extends EventManager implements IWorkbench { } }); + // persist workbench state + if (getWorkbenchConfigurer().getSaveAndRestore()) { + SafeRunner.run(new SafeRunnable() { + public void run() { + persistWorkbenchState(); + } + + public void handleException(Throwable e) { + String message; + if (e.getMessage() == null) { + message = WorkbenchMessages.ErrorClosingNoArg; + } else { + message = NLS.bind(WorkbenchMessages.ErrorClosingOneArg, e.getMessage()); + } + + if (!MessageDialog.openQuestion(null, WorkbenchMessages.Error, message)) { + isClosing = false; + } + } + }); + } + // persist view states SafeRunner.run(new SafeRunnable() { public void run() { @@ -1560,6 +1589,8 @@ public final class Workbench extends EventManager implements IWorkbench { if (isClosing()) { bail[0] = true; } + + restoreWorkbenchState(); } }); @@ -3526,4 +3557,75 @@ UIEvents.Context.TOPIC_CONTEXT, public MApplication getApplication() { return application; } + + /* + * Record the workbench UI in a document + */ + private void persistWorkbenchState() { + try { + XMLMemento memento = XMLMemento.createWriteRoot(IWorkbenchConstants.TAG_WORKBENCH); + IStatus status = saveWorkbenchState(memento); + + if (status.getSeverity() == IStatus.OK) { + StringWriter writer = new StringWriter(); + memento.save(writer); + application.getPersistedState().put(MEMENTO_KEY, writer.toString()); + } else { + WorkbenchPlugin.log(new Status(status.getSeverity(), PlatformUI.PLUGIN_ID, + WorkbenchMessages.Workbench_problemsSavingMsg)); + } + } catch (IOException e) { + WorkbenchPlugin.log(new Status(IStatus.ERROR, PlatformUI.PLUGIN_ID, 0, + WorkbenchMessages.Workbench_problemsSavingMsg, e)); + } + } + + /* + * Saves the current state of the workbench so it can be restored later on + */ + private IStatus saveWorkbenchState(IMemento memento) { + MultiStatus result = new MultiStatus(PlatformUI.PLUGIN_ID, IStatus.OK, + WorkbenchMessages.Workbench_problemsSaving, null); + + // TODO: Currently we store the editors history only. Add more if needed + + result.add(getEditorHistory().saveState( + memento.createChild(IWorkbenchConstants.TAG_MRU_LIST))); + return result; + } + + private void restoreWorkbenchState() { + try { + String persistedState = application.getPersistedState().get(MEMENTO_KEY); + if (persistedState != null) { + XMLMemento memento = XMLMemento.createReadRoot(new StringReader(persistedState)); + IStatus status = readWorkbenchState(memento); + + if (status.getSeverity() != IStatus.OK) { + WorkbenchPlugin.log(new Status(status.getSeverity(), PlatformUI.PLUGIN_ID, + WorkbenchMessages.Workbench_problemsRestoring)); + } + } + } catch (Exception e) { + WorkbenchPlugin.log(new Status( + IStatus.ERROR, PlatformUI.PLUGIN_ID, 0, + WorkbenchMessages.Workbench_problemsRestoring, e)); + } + } + + private IStatus readWorkbenchState(IMemento memento) { + MultiStatus result = new MultiStatus(PlatformUI.PLUGIN_ID, IStatus.OK, + WorkbenchMessages.Workbench_problemsRestoring, null); + + try { + UIStats.start(UIStats.RESTORE_WORKBENCH, "MRUList"); //$NON-NLS-1$ + IMemento mruMemento = memento.getChild(IWorkbenchConstants.TAG_MRU_LIST); + if (mruMemento != null) { + result.add(getEditorHistory().restoreState(mruMemento)); + } + } finally { + UIStats.end(UIStats.RESTORE_WORKBENCH, this, "MRUList"); //$NON-NLS-1$ + } + return result; + } } -- cgit v1.2.3