Download
Getting Started
Members
Projects
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
More
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
Toggle navigation
Bugzilla – Attachment 123040 Details for
Bug 226618
[context] move task-specific editor memento storage out of the plug-in preference store
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read
this important communication.
[patch]
updated patch
clipboard.txt (text/plain), 30.20 KB, created by
Steffen Pingel
on 2009-01-20 02:32:30 EST
(
hide
)
Description:
updated patch
Filename:
MIME Type:
Creator:
Steffen Pingel
Created:
2009-01-20 02:32:30 EST
Size:
30.20 KB
patch
obsolete
>### Eclipse Workspace Patch 1.0 >#P org.eclipse.mylyn.context.ui >Index: src/org/eclipse/mylyn/internal/context/ui/ContextUiPlugin.java >=================================================================== >RCS file: /cvsroot/tools/org.eclipse.mylyn/org.eclipse.mylyn.context.ui/src/org/eclipse/mylyn/internal/context/ui/ContextUiPlugin.java,v >retrieving revision 1.35 >diff -u -r1.35 ContextUiPlugin.java >--- src/org/eclipse/mylyn/internal/context/ui/ContextUiPlugin.java 22 Dec 2008 20:16:45 -0000 1.35 >+++ src/org/eclipse/mylyn/internal/context/ui/ContextUiPlugin.java 20 Jan 2009 07:31:47 -0000 >@@ -11,6 +11,8 @@ > > package org.eclipse.mylyn.internal.context.ui; > >+import java.io.File; >+import java.io.StringReader; > import java.util.ArrayList; > import java.util.Collections; > import java.util.HashMap; >@@ -27,9 +29,12 @@ > import org.eclipse.core.runtime.IExtension; > import org.eclipse.core.runtime.IExtensionPoint; > import org.eclipse.core.runtime.IExtensionRegistry; >+import org.eclipse.core.runtime.IPath; > import org.eclipse.core.runtime.IStatus; > import org.eclipse.core.runtime.Platform; > import org.eclipse.core.runtime.Status; >+import org.eclipse.core.runtime.preferences.IEclipsePreferences; >+import org.eclipse.core.runtime.preferences.InstanceScope; > import org.eclipse.jface.dialogs.MessageDialog; > import org.eclipse.jface.preference.IPreferenceStore; > import org.eclipse.jface.resource.ImageDescriptor; >@@ -47,6 +52,7 @@ > import org.eclipse.mylyn.context.core.IInteractionRelation; > import org.eclipse.mylyn.context.ui.AbstractContextUiBridge; > import org.eclipse.mylyn.context.ui.IContextUiStartup; >+import org.eclipse.mylyn.internal.context.core.InteractionContext; > import org.eclipse.mylyn.internal.monitor.ui.MonitorUiPlugin; > import org.eclipse.mylyn.monitor.ui.MonitorUi; > import org.eclipse.mylyn.tasks.core.ITask; >@@ -57,14 +63,19 @@ > import org.eclipse.swt.graphics.Image; > import org.eclipse.ui.IEditorInput; > import org.eclipse.ui.IEditorPart; >+import org.eclipse.ui.IMemento; > import org.eclipse.ui.IViewPart; > import org.eclipse.ui.IViewReference; > import org.eclipse.ui.IWorkbench; > import org.eclipse.ui.IWorkbenchWindow; > import org.eclipse.ui.PlatformUI; >+import org.eclipse.ui.WorkbenchException; >+import org.eclipse.ui.XMLMemento; > import org.eclipse.ui.internal.WorkbenchPlugin; > import org.eclipse.ui.plugin.AbstractUIPlugin; >+import org.eclipse.ui.preferences.ScopedPreferenceStore; > import org.osgi.framework.BundleContext; >+import org.osgi.service.prefs.BackingStoreException; > > /** > * Main entry point for the Context UI. >@@ -75,6 +86,8 @@ > */ > public class ContextUiPlugin extends AbstractUIPlugin { > >+ private static final String EDITOR_MEMNTO_PREFS_PREFIX = "editors.task."; //$NON-NLS-1$ >+ > public static final String ID_PLUGIN = "org.eclipse.mylyn.context.ui"; //$NON-NLS-1$ > > private class ContextActivationListener extends AbstractContextListener { >@@ -208,6 +221,8 @@ > > private ContextEditorManager editorManager; > >+ private ContextMementoManager mementoManager; >+ > public ContextUiPlugin() { > INSTANCE = this; > } >@@ -256,8 +271,13 @@ > MonitorUi.addWindowPerspectiveListener(perspectiveManager); > TasksUi.getTaskActivityManager().addActivationListener(TASK_ACTIVATION_LISTENER); > >+ mementoManager = new ContextMementoManager(ContextCore.getContextManager(), getContextMementoPath()); >+ mementoManager.start(); >+ > editorManager = new ContextEditorManager(); >- ContextCore.getContextManager().addListener(editorManager); >+ editorManager.start(ContextCore.getContextManager(), mementoManager); >+ >+ migrateContextMementos(mementoManager); > } catch (Exception e) { > StatusHandler.log(new Status(IStatus.ERROR, ContextUiPlugin.ID_PLUGIN, "Context UI initialization failed", //$NON-NLS-1$ > e)); >@@ -289,9 +309,49 @@ > } > } > >+ private void migrateContextMementos(ContextMementoManager mementoManager) { >+ ScopedPreferenceStore preferenceStore = new ScopedPreferenceStore(new InstanceScope(), >+ "org.eclipse.mylyn.resources.ui"); //$NON-NLS-1$ >+ >+ IEclipsePreferences[] nodes = preferenceStore.getPreferenceNodes(false); >+ if (nodes.length > 0) { >+ String[] keys; >+ try { >+ keys = nodes[0].keys(); >+ for (String key : keys) { >+ if (key.startsWith(EDITOR_MEMNTO_PREFS_PREFIX)) { >+ String contextHandle = key.substring(EDITOR_MEMNTO_PREFS_PREFIX.length()); >+ String mementoString = preferenceStore.getString(key); >+ if (mementoString != null && !mementoString.trim().equals("")) { //$NON-NLS-1$ >+ try { >+ IMemento oldMemento = XMLMemento.createReadRoot(new StringReader(mementoString)); >+ InteractionContext context = new InteractionContext(contextHandle, >+ ContextCore.getCommonContextScaling()); >+ ContextMemento state = mementoManager.createMemento(context); >+ IMemento newMemnto = state.createMemento("editors"); >+ newMemnto.putMemento(oldMemento); >+ mementoManager.write(state); >+ } catch (WorkbenchException e) { >+ StatusHandler.log(new Status(IStatus.ERROR, ContextUiPlugin.ID_PLUGIN, >+ "Migration of editor memento failed", e)); //$NON-NLS-1$ >+ } >+ } >+ } >+ } >+ } catch (BackingStoreException e) { >+ StatusHandler.log(new Status(IStatus.ERROR, ContextUiPlugin.ID_PLUGIN, >+ "Migration of editor mementos failed", e)); //$NON-NLS-1$ >+ } >+ } >+ } >+ > private void lazyStop() { > if (editorManager != null) { >- ContextCore.getContextManager().removeListener(editorManager); >+ editorManager.stop(ContextCore.getContextManager(), mementoManager); >+ } >+ >+ if (mementoManager != null) { >+ mementoManager.stop(); > } > > ContextCore.getContextManager().removeListener(viewerManager); >@@ -645,4 +705,15 @@ > public static ContextPerspectiveManager getPerspectiveManager() { > return INSTANCE.perspectiveManager; > } >+ >+ private File getContextMementoPath() { >+ IPath stateLocation = Platform.getStateLocation(getBundle()); >+ IPath cacheFile = stateLocation.append("workbench"); //$NON-NLS-1$ >+ return cacheFile.toFile(); >+ } >+ >+ public ContextMementoManager getMementoManager() { >+ return mementoManager; >+ } >+ > } >Index: src/org/eclipse/mylyn/internal/context/ui/ContextEditorManager.java >=================================================================== >RCS file: /cvsroot/tools/org.eclipse.mylyn/org.eclipse.mylyn.context.ui/src/org/eclipse/mylyn/internal/context/ui/ContextEditorManager.java,v >retrieving revision 1.14 >diff -u -r1.14 ContextEditorManager.java >--- src/org/eclipse/mylyn/internal/context/ui/ContextEditorManager.java 3 Dec 2008 03:14:20 -0000 1.14 >+++ src/org/eclipse/mylyn/internal/context/ui/ContextEditorManager.java 20 Jan 2009 07:31:47 -0000 >@@ -11,9 +11,6 @@ > > package org.eclipse.mylyn.internal.context.ui; > >-import java.io.IOException; >-import java.io.StringReader; >-import java.io.StringWriter; > import java.util.ArrayList; > import java.util.Arrays; > import java.util.HashSet; >@@ -23,13 +20,12 @@ > import org.eclipse.core.runtime.IStatus; > import org.eclipse.core.runtime.MultiStatus; > import org.eclipse.core.runtime.Status; >-import org.eclipse.core.runtime.preferences.InstanceScope; >-import org.eclipse.jface.preference.IPreferenceStore; > import org.eclipse.mylyn.commons.core.StatusHandler; > import org.eclipse.mylyn.context.core.AbstractContextListener; > import org.eclipse.mylyn.context.core.AbstractContextStructureBridge; > import org.eclipse.mylyn.context.core.ContextCore; > import org.eclipse.mylyn.context.core.IInteractionContext; >+import org.eclipse.mylyn.context.core.IInteractionContextManager; > import org.eclipse.mylyn.context.core.IInteractionElement; > import org.eclipse.mylyn.context.ui.AbstractContextUiBridge; > import org.eclipse.mylyn.context.ui.ContextUi; >@@ -46,25 +42,55 @@ > import org.eclipse.ui.IWorkbenchWindow; > import org.eclipse.ui.PartInitException; > import org.eclipse.ui.PlatformUI; >-import org.eclipse.ui.XMLMemento; > import org.eclipse.ui.internal.EditorManager; > import org.eclipse.ui.internal.IPreferenceConstants; > import org.eclipse.ui.internal.IWorkbenchConstants; > import org.eclipse.ui.internal.Workbench; >-import org.eclipse.ui.internal.WorkbenchMessages; > import org.eclipse.ui.internal.WorkbenchPage; > import org.eclipse.ui.internal.WorkbenchWindow; >-import org.eclipse.ui.preferences.ScopedPreferenceStore; > > /** > * @author Mik Kersten > * @author Shawn Minto > */ >-public class ContextEditorManager extends AbstractContextListener { >+public class ContextEditorManager { > >- private static final String PREFS_PREFIX = "editors.task."; //$NON-NLS-1$ >+ private class MementoListener extends ContextMementoListener { > >- private static final String KEY_CONTEXT_EDITORS = "ContextOpenEditors"; //$NON-NLS-1$ >+ @Override >+ public void clearState(ContextMemento memento) { >+ ContextEditorManager.this.clearState(memento); >+ } >+ >+ @Override >+ public void restoreState(ContextMemento memento) { >+ ContextEditorManager.this.restoreState(memento); >+ } >+ >+ @Override >+ public void saveState(ContextMemento memento) { >+ ContextEditorManager.this.saveState(memento); >+ } >+ >+ } >+ >+ private class ContextListener extends AbstractContextListener { >+ >+ @Override >+ public void interestChanged(List<IInteractionElement> elements) { >+ for (IInteractionElement element : elements) { >+ closeEditor(element, false); >+ } >+ } >+ >+ @Override >+ public void elementsDeleted(List<IInteractionElement> elements) { >+ for (IInteractionElement element : elements) { >+ closeEditor(element, true); >+ } >+ } >+ >+ } > > private static final String KEY_MONITORED_WINDOW_OPEN_EDITORS = "MonitoredWindowOpenEditors"; //$NON-NLS-1$ > >@@ -79,14 +105,29 @@ > private boolean previousCloseEditorsSetting = Workbench.getInstance().getPreferenceStore().getBoolean( > IPreferenceConstants.REUSE_EDITORS_BOOLEAN); > >- private final IPreferenceStore preferenceStore; >+ private final MementoListener mementoListener; >+ >+ private final ContextListener contextListener; >+ >+ //private final IPreferenceStore preferenceStore; > > public ContextEditorManager() { >- preferenceStore = new ScopedPreferenceStore(new InstanceScope(), "org.eclipse.mylyn.resources.ui"); //$NON-NLS-1$ >+ //preferenceStore = new ScopedPreferenceStore(new InstanceScope(), "org.eclipse.mylyn.resources.ui"); //$NON-NLS-1$ >+ this.mementoListener = new MementoListener(); >+ this.contextListener = new ContextListener(); > } > >- @Override >- public void contextActivated(IInteractionContext context) { >+ public void start(IInteractionContextManager contextManager, ContextMementoManager mementoManager) { >+ contextManager.addListener(contextListener); >+ mementoManager.addMementoListener(mementoListener); >+ } >+ >+ public void stop(IInteractionContextManager contextManager, ContextMementoManager mementoManager) { >+ contextManager.removeListener(contextListener); >+ mementoManager.removeMementoListener(mementoListener); >+ } >+ >+ public void restoreState(ContextMemento state) { > if (!Workbench.getInstance().isStarting() > && ContextUiPlugin.getDefault().getPreferenceStore().getBoolean( > IContextUiPreferenceContstants.AUTO_MANAGE_EDITORS)) { >@@ -103,9 +144,11 @@ > // TODO change where memento is stored > IWorkbenchWindow activeWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); > try { >- mementoString = readEditorMemento(context); >- if (mementoString != null && !mementoString.trim().equals("")) { //$NON-NLS-1$ >- IMemento memento = XMLMemento.createReadRoot(new StringReader(mementoString)); >+ //mementoString = readEditorMemento(context); >+// if (mementoString != null && !mementoString.trim().equals("")) { //$NON-NLS-1$ >+// IMemento memento = XMLMemento.createReadRoot(new StringReader(mementoString)); >+ IMemento memento = state.getMemento("editors"); >+ if (memento != null) { > IMemento[] children = memento.getChildren(KEY_MONITORED_WINDOW_OPEN_EDITORS); > if (children.length > 0) { > // This code supports restore from multiple windows >@@ -128,7 +171,7 @@ > "Could not restore all editors, memento: \"" + mementoString + "\"", e)); //$NON-NLS-1$ //$NON-NLS-2$ > } > activeWindow.setActivePage(activeWindow.getActivePage()); >- IInteractionElement activeNode = context.getActiveNode(); >+ IInteractionElement activeNode = state.getContext().getActiveNode(); > if (activeNode != null) { > ContextUi.getUiBridge(activeNode.getContentType()).open(activeNode); > } >@@ -142,7 +185,6 @@ > } > > private WorkbenchPage getWorkbenchPageForMemento(IMemento memento, IWorkbenchWindow activeWindow) { >- > String windowToRestoreClassName = memento.getString(ATTRIBUTE_CLASS); > if (windowToRestoreClassName == null) { > windowToRestoreClassName = ""; //$NON-NLS-1$ >@@ -196,23 +238,23 @@ > return null; > } > >- private String readEditorMemento(IInteractionContext context) { >- return preferenceStore.getString(PREFS_PREFIX + context.getHandleIdentifier()); >- } >+// private String readEditorMemento(IInteractionContext context) { >+// return preferenceStore.getString(PREFS_PREFIX + context.getHandleIdentifier()); >+// } > >- @Override >- public void contextDeactivated(IInteractionContext context) { >+ public void saveState(ContextMemento state) { > if (!PlatformUI.getWorkbench().isClosing() > && ContextUiPlugin.getDefault().getPreferenceStore().getBoolean( > IContextUiPreferenceContstants.AUTO_MANAGE_EDITORS)) { >- closeAllButActiveTaskEditor(context.getHandleIdentifier()); >+ closeAllButActiveTaskEditor(state.getContext().getHandleIdentifier()); > >- XMLMemento rootMemento = XMLMemento.createWriteRoot(KEY_CONTEXT_EDITORS); >+ //XMLMemento rootMemento = XMLMemento.createWriteRoot(KEY_CONTEXT_EDITORS); > > IWorkbenchWindow activeWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); > IWorkbenchWindow launchingWindow = MonitorUi.getLaunchingWorkbenchWindow(); > Set<IWorkbenchWindow> monitoredWindows = MonitorUi.getMonitoredWindows(); > >+ IMemento rootMemento = state.createMemento("editors"); > for (IWorkbenchWindow window : monitoredWindows) { > IMemento memento = rootMemento.createChild(KEY_MONITORED_WINDOW_OPEN_EDITORS); > >@@ -227,15 +269,15 @@ > memento.putString(ATTRIBUTE_IS_ACTIVE, (window == activeWindow) ? "true" : "false"); //$NON-NLS-1$ //$NON-NLS-2$ > ((WorkbenchPage) window.getActivePage()).getEditorManager().saveState(memento); > } >- // TODO: avoid storing with preferences due to bloat? >- StringWriter writer = new StringWriter(); >- try { >- rootMemento.save(writer); >- writeEditorMemento(context, writer.getBuffer().toString()); >- } catch (IOException e) { >- StatusHandler.log(new Status(IStatus.ERROR, ContextUiPlugin.ID_PLUGIN, "Could not store editor state", //$NON-NLS-1$ >- e)); >- } >+// // TODO: avoid storing with preferences due to bloat? >+// StringWriter writer = new StringWriter(); >+// try { >+// rootMemento.save(writer); >+// writeEditorMemento(context, writer.getBuffer().toString()); >+// } catch (IOException e) { >+// StatusHandler.log(new Status(IStatus.ERROR, ContextUiPlugin.ID_PLUGIN, "Could not store editor state", //$NON-NLS-1$ >+// e)); >+// } > > Workbench.getInstance().getPreferenceStore().setValue(IPreferenceConstants.REUSE_EDITORS_BOOLEAN, > previousCloseEditorsSetting); >@@ -243,26 +285,27 @@ > } > } > >- public void writeEditorMemento(IInteractionContext context, String memento) { >- preferenceStore.setValue(PREFS_PREFIX + context.getHandleIdentifier(), memento); >- } >+// public void writeEditorMemento(IInteractionContext context, String memento) { >+// preferenceStore.setValue(PREFS_PREFIX + context.getHandleIdentifier(), memento); >+// } > >- @Override >- public void contextCleared(IInteractionContext context) { >+ public void clearState(ContextMemento memento) { >+ IInteractionContext context = memento.getContext(); > if (context == null) { > return; > } > closeAllButActiveTaskEditor(context.getHandleIdentifier()); >- XMLMemento memento = XMLMemento.createWriteRoot(KEY_CONTEXT_EDITORS); > >- // TODO: avoid storing with preferences due to bloat? >- StringWriter writer = new StringWriter(); >- try { >- memento.save(writer); >- writeEditorMemento(context, writer.getBuffer().toString()); >- } catch (IOException e) { >- StatusHandler.log(new Status(IStatus.ERROR, ContextUiPlugin.ID_PLUGIN, "Could not store editor state", e)); //$NON-NLS-1$ >- } >+// XMLMemento memento = XMLMemento.createWriteRoot(KEY_CONTEXT_EDITORS); >+// >+// // TODO: avoid storing with preferences due to bloat? >+// StringWriter writer = new StringWriter(); >+// try { >+// memento.save(writer); >+// writeEditorMemento(context, writer.getBuffer().toString()); >+// } catch (IOException e) { >+// StatusHandler.log(new Status(IStatus.ERROR, ContextUiPlugin.ID_PLUGIN, "Could not store editor state", e)); >+// } > > Workbench.getInstance().getPreferenceStore().setValue(IPreferenceConstants.REUSE_EDITORS_BOOLEAN, > previousCloseEditorsSetting); >@@ -277,8 +320,7 @@ > EditorManager editorManager = page.getEditorManager(); > final ArrayList visibleEditors = new ArrayList(5); > final IEditorReference activeEditor[] = new IEditorReference[1]; >- final MultiStatus result = new MultiStatus(PlatformUI.PLUGIN_ID, IStatus.OK, >- "", null); //$NON-NLS-1$ >+ final MultiStatus result = new MultiStatus(PlatformUI.PLUGIN_ID, IStatus.OK, "", null); //$NON-NLS-1$ > > try { > IMemento[] editorMementos = memento.getChildren(IWorkbenchConstants.TAG_EDITOR); >@@ -310,7 +352,7 @@ > } > } > } catch (Exception e) { >- StatusHandler.log(new Status(IStatus.ERROR, ContextUiPlugin.ID_PLUGIN, "Could not restore editors", e)); //$NON-NLS-1$ >+ StatusHandler.log(new Status(IStatus.ERROR, ContextUiPlugin.ID_PLUGIN, "Could not restore editors", e)); > } > } > >@@ -345,7 +387,7 @@ > } > } > } catch (Throwable t) { >- StatusHandler.log(new Status(IStatus.ERROR, ContextUiPlugin.ID_PLUGIN, "Could not auto close editor", t)); //$NON-NLS-1$ >+ StatusHandler.log(new Status(IStatus.ERROR, ContextUiPlugin.ID_PLUGIN, "Could not auto close editor", t)); > } > } > >@@ -368,7 +410,7 @@ > } > } > } catch (Throwable t) { >- StatusHandler.log(new Status(IStatus.ERROR, ContextUiPlugin.ID_PLUGIN, "Could not auto close editor", t)); //$NON-NLS-1$ >+ StatusHandler.log(new Status(IStatus.ERROR, ContextUiPlugin.ID_PLUGIN, "Could not auto close editor", t)); > } > } > >@@ -380,20 +422,6 @@ > return true; > } > >- @Override >- public void interestChanged(List<IInteractionElement> elements) { >- for (IInteractionElement element : elements) { >- closeEditor(element, false); >- } >- } >- >- @Override >- public void elementsDeleted(List<IInteractionElement> elements) { >- for (IInteractionElement element : elements) { >- closeEditor(element, true); >- } >- } >- > private void closeEditor(IInteractionElement element, boolean force) { > if (ContextUiPlugin.getDefault().getPreferenceStore().getBoolean( > IContextUiPreferenceContstants.AUTO_MANAGE_EDITORS)) { >Index: src/org/eclipse/mylyn/internal/context/ui/ContextMementoManager.java >=================================================================== >RCS file: src/org/eclipse/mylyn/internal/context/ui/ContextMementoManager.java >diff -N src/org/eclipse/mylyn/internal/context/ui/ContextMementoManager.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/mylyn/internal/context/ui/ContextMementoManager.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,248 @@ >+/******************************************************************************* >+ * Copyright (c) 2004, 2008 Tasktop Technologies 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: >+ * Tasktop Technologies - initial API and implementation >+ *******************************************************************************/ >+ >+package org.eclipse.mylyn.internal.context.ui; >+ >+import java.io.BufferedReader; >+import java.io.BufferedWriter; >+import java.io.File; >+import java.io.FileInputStream; >+import java.io.FileOutputStream; >+import java.io.IOException; >+import java.io.InputStreamReader; >+import java.io.OutputStreamWriter; >+import java.util.HashMap; >+import java.util.List; >+import java.util.Map; >+import java.util.concurrent.CopyOnWriteArrayList; >+ >+import org.eclipse.core.runtime.Assert; >+import org.eclipse.core.runtime.IProgressMonitor; >+import org.eclipse.core.runtime.IStatus; >+import org.eclipse.core.runtime.MultiStatus; >+import org.eclipse.core.runtime.SafeRunner; >+import org.eclipse.core.runtime.Status; >+import org.eclipse.core.runtime.jobs.Job; >+import org.eclipse.jface.util.SafeRunnable; >+import org.eclipse.mylyn.commons.core.StatusHandler; >+import org.eclipse.mylyn.context.core.AbstractContextListener; >+import org.eclipse.mylyn.context.core.IInteractionContext; >+import org.eclipse.mylyn.context.core.IInteractionContextManager; >+import org.eclipse.mylyn.internal.provisional.tasks.core.TasksUtil; >+import org.eclipse.osgi.util.NLS; >+import org.eclipse.ui.WorkbenchException; >+import org.eclipse.ui.XMLMemento; >+ >+/** >+ * @author Steffen Pingel >+ */ >+public class ContextMementoManager { >+ >+ private class ContextListener extends AbstractContextListener { >+ >+ @Override >+ public void contextActivated(IInteractionContext context) { >+ restoreState(context); >+ } >+ >+ @Override >+ public void contextDeactivated(IInteractionContext context) { >+ saveState(context); >+ } >+ >+ @Override >+ public void contextCleared(IInteractionContext context) { >+ clearState(context); >+ } >+ >+ } >+ >+ private class FlushJob extends Job { >+ >+ public FlushJob() { >+ super("Flush context mementos"); //$NON-NLS-1$ >+ } >+ >+ @Override >+ protected IStatus run(IProgressMonitor monitor) { >+ ContextMementoManager.this.flushPending(); >+ return Status.OK_STATUS; >+ } >+ >+ } >+ >+ private static final String TAG_CONTEXT_STATE = "ContextState"; //$NON-NLS-1$ >+ >+ private static final long FLUSH_DELAY = 500; >+ >+ private static final String CHARSET = "UTF-8"; //$NON-NLS-1$ >+ >+ private final Map<String, ContextMemento> pending; >+ >+ private final File location; >+ >+ private FlushJob flushJob; >+ >+ private boolean scheduled; >+ >+ private final IInteractionContextManager contextManager; >+ >+ private final ContextListener contextListener; >+ >+ private final List<ContextMementoListener> listeners; >+ >+ public ContextMementoManager(IInteractionContextManager contextManager, File location) { >+ Assert.isNotNull(contextManager); >+ Assert.isNotNull(location); >+ this.contextManager = contextManager; >+ this.location = location; >+ this.contextListener = new ContextListener(); >+ this.pending = new HashMap<String, ContextMemento>(); >+ this.listeners = new CopyOnWriteArrayList<ContextMementoListener>(); >+ } >+ >+ public void addMementoListener(ContextMementoListener listener) { >+ if (listeners.contains(listener)) { >+ throw new IllegalStateException("listener already added"); //$NON-NLS-1$ >+ } >+ listeners.add(listener); >+ } >+ >+ public void removeMementoListener(ContextMementoListener listener) { >+ listeners.remove(listener); >+ } >+ >+ public void clearState(IInteractionContext context) { >+ >+ } >+ >+ public void saveState(IInteractionContext context) { >+ final ContextMemento memento = createMemento(context); >+ for (final ContextMementoListener listener : listeners) { >+ SafeRunner.run(new SafeRunnable() { >+ public void run() throws Exception { >+ listener.saveState(memento); >+ } >+ >+ @Override >+ public void handleException(Throwable e) { >+ StatusHandler.log(new Status(IStatus.ERROR, ContextUiPlugin.ID_PLUGIN, >+ "Unexpected error processing context event", e)); //$NON-NLS-1$ >+ removeMementoListener(listener); >+ } >+ }); >+ } >+ if (memento.isChanged()) { >+ writeLater(memento); >+ } >+ } >+ >+ ContextMemento createMemento(IInteractionContext context) { >+ return new ContextMemento(context, XMLMemento.createWriteRoot(TAG_CONTEXT_STATE)); >+ } >+ >+ public void restoreState(IInteractionContext context) { >+ final ContextMemento memento = read(context); >+ for (final ContextMementoListener listener : listeners) { >+ SafeRunner.run(new SafeRunnable() { >+ public void run() throws Exception { >+ listener.restoreState(memento); >+ } >+ >+ @Override >+ public void handleException(Throwable e) { >+ StatusHandler.log(new Status(IStatus.ERROR, ContextUiPlugin.ID_PLUGIN, >+ "Unexpected error processing context event", e)); //$NON-NLS-1$ >+ removeMementoListener(listener); >+ } >+ }); >+ } >+ } >+ >+ public synchronized void start() { >+ contextManager.addListener(contextListener); >+ } >+ >+ public synchronized void stop() { >+ contextManager.removeListener(contextListener); >+ } >+ >+ public synchronized void flushPending() { >+ MultiStatus status = new MultiStatus(ContextUiPlugin.ID_PLUGIN, 0, >+ "Failed to save context workbench state", null); //$NON-NLS-1$ >+ for (ContextMemento memento : pending.values()) { >+ if (memento.isChanged()) { >+ IStatus result = write(memento); >+ status.add(result); >+ } >+ } >+ pending.clear(); >+ if (!status.isOK()) { >+ StatusHandler.log(status); >+ } >+ } >+ >+ private ContextMemento read(IInteractionContext context) { >+ File file = getFile(context); >+ if (file.exists()) { >+ try { >+ BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), CHARSET)); >+ XMLMemento memento = XMLMemento.createReadRoot(reader); >+ return new ContextMemento(context, memento); >+ } catch (IOException e) { >+ StatusHandler.log(new Status(IStatus.ERROR, ContextUiPlugin.ID_PLUGIN, NLS.bind( >+ "Failed to read context memento from \"{0}\"", file.getAbsolutePath()), e)); //$NON-NLS-1$ >+ } catch (WorkbenchException e) { >+ StatusHandler.log(new Status(IStatus.ERROR, ContextUiPlugin.ID_PLUGIN, NLS.bind( >+ "Failed to read context memento from \"{0}\"", file.getAbsolutePath()), e)); //$NON-NLS-1$ >+ } >+ } >+ return createMemento(context); >+ } >+ >+ IStatus write(ContextMemento memento) { >+ File file = getFile(memento.getContext()); >+ if (!file.getParentFile().exists()) { >+ file.getParentFile().mkdirs(); >+ } >+ try { >+ BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), CHARSET)); >+ try { >+ (memento.getMemento()).save(writer); >+ } finally { >+ writer.close(); >+ } >+ } catch (IOException e) { >+ return new Status(IStatus.ERROR, ContextUiPlugin.ID_PLUGIN, NLS.bind( >+ "Failed to write context memento to \"{0}\"", file.getAbsolutePath()), e); //$NON-NLS-1$ >+ } >+ return Status.OK_STATUS; >+ } >+ >+ private File getFile(IInteractionContext context) { >+ return new File(location, TasksUtil.encode(context.getHandleIdentifier())); >+ } >+ >+ public synchronized void writeLater(ContextMemento memento) { >+ pending.put(memento.getContext().getHandleIdentifier(), memento); >+ schedule(); >+ } >+ >+ private synchronized void schedule() { >+ if (!scheduled) { >+ if (flushJob == null) { >+ flushJob = new FlushJob(); >+ } >+ flushJob.schedule(FLUSH_DELAY); >+ } >+ } >+ >+} >Index: src/org/eclipse/mylyn/internal/context/ui/ContextMemento.java >=================================================================== >RCS file: src/org/eclipse/mylyn/internal/context/ui/ContextMemento.java >diff -N src/org/eclipse/mylyn/internal/context/ui/ContextMemento.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/mylyn/internal/context/ui/ContextMemento.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,77 @@ >+/******************************************************************************* >+ * Copyright (c) 2004, 2008 Tasktop Technologies 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: >+ * Tasktop Technologies - initial API and implementation >+ *******************************************************************************/ >+ >+package org.eclipse.mylyn.internal.context.ui; >+ >+import org.eclipse.core.runtime.Assert; >+import org.eclipse.mylyn.context.core.IInteractionContext; >+import org.eclipse.ui.IMemento; >+import org.eclipse.ui.XMLMemento; >+ >+/** >+ * Stores workspace specific settings for a context. >+ * >+ * @author Steffen Pingel >+ */ >+public class ContextMemento { >+ >+ private boolean changed; >+ >+ private final IInteractionContext context; >+ >+ private final XMLMemento memento; >+ >+ public ContextMemento(IInteractionContext context, XMLMemento memento) { >+ Assert.isNotNull(context); >+ Assert.isNotNull(memento); >+ this.context = context; >+ this.memento = memento; >+ } >+ >+ @Override >+ public boolean equals(Object obj) { >+ if (!(obj instanceof ContextMemento)) { >+ return false; >+ } >+ return context.equals(((ContextMemento) obj).context); >+ } >+ >+ public IInteractionContext getContext() { >+ return context; >+ } >+ >+ XMLMemento getMemento() { >+ return memento; >+ } >+ >+ public IMemento getMemento(String type) { >+ return memento.getChild(type); >+ } >+ >+ public IMemento createMemento(String type) { >+ changed = true; >+ return memento.createChild(type); >+ } >+ >+ @Override >+ public int hashCode() { >+ return context.hashCode(); >+ } >+ >+ public boolean isChanged() { >+ return changed; >+ } >+ >+ public void removeMemento(String type) { >+ memento.createChild(type); >+ } >+ >+} >Index: src/org/eclipse/mylyn/internal/context/ui/ContextMementoListener.java >=================================================================== >RCS file: src/org/eclipse/mylyn/internal/context/ui/ContextMementoListener.java >diff -N src/org/eclipse/mylyn/internal/context/ui/ContextMementoListener.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/mylyn/internal/context/ui/ContextMementoListener.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,25 @@ >+/******************************************************************************* >+ * Copyright (c) 2004, 2008 Tasktop Technologies 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: >+ * Tasktop Technologies - initial API and implementation >+ *******************************************************************************/ >+ >+package org.eclipse.mylyn.internal.context.ui; >+ >+/** >+ * @author Steffen Pingel >+ */ >+public abstract class ContextMementoListener { >+ >+ public abstract void clearState(ContextMemento memento); >+ >+ public abstract void restoreState(ContextMemento memento); >+ >+ public abstract void saveState(ContextMemento memento); >+ >+}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 226618
:
123033
|
123034
|
123040
|
123142
|
135417
|
152557
|
152558
|
153653
|
154288