Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 226618 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/mylyn/internal/context/ui/ContextUiPlugin.java (-1 / +18 lines)
Lines 11-16 Link Here
11
11
12
package org.eclipse.mylyn.internal.context.ui;
12
package org.eclipse.mylyn.internal.context.ui;
13
13
14
import java.io.File;
14
import java.util.ArrayList;
15
import java.util.ArrayList;
15
import java.util.Collections;
16
import java.util.Collections;
16
import java.util.HashMap;
17
import java.util.HashMap;
Lines 27-32 Link Here
27
import org.eclipse.core.runtime.IExtension;
28
import org.eclipse.core.runtime.IExtension;
28
import org.eclipse.core.runtime.IExtensionPoint;
29
import org.eclipse.core.runtime.IExtensionPoint;
29
import org.eclipse.core.runtime.IExtensionRegistry;
30
import org.eclipse.core.runtime.IExtensionRegistry;
31
import org.eclipse.core.runtime.IPath;
30
import org.eclipse.core.runtime.IStatus;
32
import org.eclipse.core.runtime.IStatus;
31
import org.eclipse.core.runtime.Platform;
33
import org.eclipse.core.runtime.Platform;
32
import org.eclipse.core.runtime.Status;
34
import org.eclipse.core.runtime.Status;
Lines 208-213 Link Here
208
210
209
	private ContextEditorManager editorManager;
211
	private ContextEditorManager editorManager;
210
212
213
	private ContextMementoManager mementoManager;
214
211
	public ContextUiPlugin() {
215
	public ContextUiPlugin() {
212
		INSTANCE = this;
216
		INSTANCE = this;
213
	}
217
	}
Lines 256-262 Link Here
256
			MonitorUi.addWindowPerspectiveListener(perspectiveManager);
260
			MonitorUi.addWindowPerspectiveListener(perspectiveManager);
257
			TasksUi.getTaskActivityManager().addActivationListener(TASK_ACTIVATION_LISTENER);
261
			TasksUi.getTaskActivityManager().addActivationListener(TASK_ACTIVATION_LISTENER);
258
262
259
			editorManager = new ContextEditorManager();
263
			mementoManager = new ContextMementoManager(getContextMementoPath());
264
265
			editorManager = new ContextEditorManager(mementoManager);
260
			ContextCore.getContextManager().addListener(editorManager);
266
			ContextCore.getContextManager().addListener(editorManager);
261
		} catch (Exception e) {
267
		} catch (Exception e) {
262
			StatusHandler.log(new Status(IStatus.ERROR, ContextUiPlugin.ID_PLUGIN, "Context UI initialization failed", //$NON-NLS-1$
268
			StatusHandler.log(new Status(IStatus.ERROR, ContextUiPlugin.ID_PLUGIN, "Context UI initialization failed", //$NON-NLS-1$
Lines 645-648 Link Here
645
	public static ContextPerspectiveManager getPerspectiveManager() {
651
	public static ContextPerspectiveManager getPerspectiveManager() {
646
		return INSTANCE.perspectiveManager;
652
		return INSTANCE.perspectiveManager;
647
	}
653
	}
654
655
	private File getContextMementoPath() {
656
		IPath stateLocation = Platform.getStateLocation(getBundle());
657
		IPath cacheFile = stateLocation.append("workbench"); //$NON-NLS-1$
658
		return cacheFile.toFile();
659
	}
660
661
	public ContextMementoManager getMementoManager() {
662
		return mementoManager;
663
	}
664
648
}
665
}
(-)src/org/eclipse/mylyn/internal/context/ui/ContextEditorManager.java (-65 / +81 lines)
Lines 11-19 Link Here
11
11
12
package org.eclipse.mylyn.internal.context.ui;
12
package org.eclipse.mylyn.internal.context.ui;
13
13
14
import java.io.IOException;
15
import java.io.StringReader;
16
import java.io.StringWriter;
17
import java.util.ArrayList;
14
import java.util.ArrayList;
18
import java.util.Arrays;
15
import java.util.Arrays;
19
import java.util.HashSet;
16
import java.util.HashSet;
Lines 23-30 Link Here
23
import org.eclipse.core.runtime.IStatus;
20
import org.eclipse.core.runtime.IStatus;
24
import org.eclipse.core.runtime.MultiStatus;
21
import org.eclipse.core.runtime.MultiStatus;
25
import org.eclipse.core.runtime.Status;
22
import org.eclipse.core.runtime.Status;
26
import org.eclipse.core.runtime.preferences.InstanceScope;
27
import org.eclipse.jface.preference.IPreferenceStore;
28
import org.eclipse.mylyn.commons.core.StatusHandler;
23
import org.eclipse.mylyn.commons.core.StatusHandler;
29
import org.eclipse.mylyn.context.core.AbstractContextListener;
24
import org.eclipse.mylyn.context.core.AbstractContextListener;
30
import org.eclipse.mylyn.context.core.AbstractContextStructureBridge;
25
import org.eclipse.mylyn.context.core.AbstractContextStructureBridge;
Lines 46-60 Link Here
46
import org.eclipse.ui.IWorkbenchWindow;
41
import org.eclipse.ui.IWorkbenchWindow;
47
import org.eclipse.ui.PartInitException;
42
import org.eclipse.ui.PartInitException;
48
import org.eclipse.ui.PlatformUI;
43
import org.eclipse.ui.PlatformUI;
49
import org.eclipse.ui.XMLMemento;
50
import org.eclipse.ui.internal.EditorManager;
44
import org.eclipse.ui.internal.EditorManager;
51
import org.eclipse.ui.internal.IPreferenceConstants;
45
import org.eclipse.ui.internal.IPreferenceConstants;
52
import org.eclipse.ui.internal.IWorkbenchConstants;
46
import org.eclipse.ui.internal.IWorkbenchConstants;
53
import org.eclipse.ui.internal.Workbench;
47
import org.eclipse.ui.internal.Workbench;
54
import org.eclipse.ui.internal.WorkbenchMessages;
55
import org.eclipse.ui.internal.WorkbenchPage;
48
import org.eclipse.ui.internal.WorkbenchPage;
56
import org.eclipse.ui.internal.WorkbenchWindow;
49
import org.eclipse.ui.internal.WorkbenchWindow;
57
import org.eclipse.ui.preferences.ScopedPreferenceStore;
58
50
59
/**
51
/**
60
 * @author Mik Kersten
52
 * @author Mik Kersten
Lines 79-88 Link Here
79
	private boolean previousCloseEditorsSetting = Workbench.getInstance().getPreferenceStore().getBoolean(
71
	private boolean previousCloseEditorsSetting = Workbench.getInstance().getPreferenceStore().getBoolean(
80
			IPreferenceConstants.REUSE_EDITORS_BOOLEAN);
72
			IPreferenceConstants.REUSE_EDITORS_BOOLEAN);
81
73
82
	private final IPreferenceStore preferenceStore;
74
	//private final IPreferenceStore preferenceStore;
83
75
84
	public ContextEditorManager() {
76
	private final ContextMementoManager mementoManager;
85
		preferenceStore = new ScopedPreferenceStore(new InstanceScope(), "org.eclipse.mylyn.resources.ui"); //$NON-NLS-1$
77
78
	public ContextEditorManager(ContextMementoManager mementoManager) {
79
		this.mementoManager = mementoManager;
80
		//preferenceStore = new ScopedPreferenceStore(new InstanceScope(), "org.eclipse.mylyn.resources.ui"); //$NON-NLS-1$
86
	}
81
	}
87
82
88
	@Override
83
	@Override
Lines 103-127 Link Here
103
				// TODO change where memento is stored
98
				// TODO change where memento is stored
104
				IWorkbenchWindow activeWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
99
				IWorkbenchWindow activeWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
105
				try {
100
				try {
106
					mementoString = readEditorMemento(context);
101
					try {
107
					if (mementoString != null && !mementoString.trim().equals("")) { //$NON-NLS-1$
102
						ContextMemento state = mementoManager.lock(context);
108
						IMemento memento = XMLMemento.createReadRoot(new StringReader(mementoString));
103
						//mementoString = readEditorMemento(context);
109
						IMemento[] children = memento.getChildren(KEY_MONITORED_WINDOW_OPEN_EDITORS);
104
//					if (mementoString != null && !mementoString.trim().equals("")) { //$NON-NLS-1$
110
						if (children.length > 0) {
105
//						IMemento memento = XMLMemento.createReadRoot(new StringReader(mementoString));
111
							// This code supports restore from multiple windows
106
						IMemento memento = state.getMemento("editors");
112
							for (IMemento child : children) {
107
						if (memento != null) {
113
								WorkbenchPage page = getWorkbenchPageForMemento(child, activeWindow);
108
							IMemento[] children = memento.getChildren(KEY_MONITORED_WINDOW_OPEN_EDITORS);
114
								if (child != null && page != null) {
109
							if (children.length > 0) {
115
									restoreEditors(page, child, page.getWorkbenchWindow() == activeWindow);
110
								// This code supports restore from multiple windows
111
								for (IMemento child : children) {
112
									WorkbenchPage page = getWorkbenchPageForMemento(child, activeWindow);
113
									if (child != null && page != null) {
114
										restoreEditors(page, child, page.getWorkbenchWindow() == activeWindow);
115
									}
116
								}
117
							} else {
118
								// This code is for supporting the old editor management - only the active window
119
								WorkbenchPage page = (WorkbenchPage) activeWindow.getActivePage();
120
								if (memento != null) {
121
									restoreEditors(page, memento, true);
116
								}
122
								}
117
							}
118
						} else {
119
							// This code is for supporting the old editor management - only the active window
120
							WorkbenchPage page = (WorkbenchPage) activeWindow.getActivePage();
121
							if (memento != null) {
122
								restoreEditors(page, memento, true);
123
							}
123
							}
124
						}
124
						}
125
					} finally {
126
						mementoManager.unlock(context);
125
					}
127
					}
126
				} catch (Exception e) {
128
				} catch (Exception e) {
127
					StatusHandler.log(new Status(IStatus.ERROR, ContextUiPlugin.ID_PLUGIN,
129
					StatusHandler.log(new Status(IStatus.ERROR, ContextUiPlugin.ID_PLUGIN,
Lines 196-204 Link Here
196
		return null;
198
		return null;
197
	}
199
	}
198
200
199
	private String readEditorMemento(IInteractionContext context) {
201
//	private String readEditorMemento(IInteractionContext context) {
200
		return preferenceStore.getString(PREFS_PREFIX + context.getHandleIdentifier());
202
//		return preferenceStore.getString(PREFS_PREFIX + context.getHandleIdentifier());
201
	}
203
//	}
202
204
203
	@Override
205
	@Override
204
	public void contextDeactivated(IInteractionContext context) {
206
	public void contextDeactivated(IInteractionContext context) {
Lines 207-241 Link Here
207
						IContextUiPreferenceContstants.AUTO_MANAGE_EDITORS)) {
209
						IContextUiPreferenceContstants.AUTO_MANAGE_EDITORS)) {
208
			closeAllButActiveTaskEditor(context.getHandleIdentifier());
210
			closeAllButActiveTaskEditor(context.getHandleIdentifier());
209
211
210
			XMLMemento rootMemento = XMLMemento.createWriteRoot(KEY_CONTEXT_EDITORS);
212
			//XMLMemento rootMemento = XMLMemento.createWriteRoot(KEY_CONTEXT_EDITORS);
211
213
212
			IWorkbenchWindow activeWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
214
			IWorkbenchWindow activeWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
213
			IWorkbenchWindow launchingWindow = MonitorUi.getLaunchingWorkbenchWindow();
215
			IWorkbenchWindow launchingWindow = MonitorUi.getLaunchingWorkbenchWindow();
214
			Set<IWorkbenchWindow> monitoredWindows = MonitorUi.getMonitoredWindows();
216
			Set<IWorkbenchWindow> monitoredWindows = MonitorUi.getMonitoredWindows();
215
217
216
			for (IWorkbenchWindow window : monitoredWindows) {
217
				IMemento memento = rootMemento.createChild(KEY_MONITORED_WINDOW_OPEN_EDITORS);
218
219
				memento.putString(ATTRIBUTE_CLASS, window.getClass().getCanonicalName());
220
				int number = 0;
221
				if (window instanceof WorkbenchWindow) {
222
					number = ((WorkbenchWindow) window).getNumber();
223
				}
224
				memento.putInteger(ATTRIBUTE_NUMER, number);
225
				// TODO e3.4 replace by memento.putBoolean()
226
				memento.putString(ATTRIBUTE_IS_LAUNCHING, (window == launchingWindow) ? "true" : "false"); //$NON-NLS-1$ //$NON-NLS-2$
227
				memento.putString(ATTRIBUTE_IS_ACTIVE, (window == activeWindow) ? "true" : "false"); //$NON-NLS-1$ //$NON-NLS-2$
228
				((WorkbenchPage) window.getActivePage()).getEditorManager().saveState(memento);
229
			}
230
			// TODO: avoid storing with preferences due to bloat?
231
			StringWriter writer = new StringWriter();
232
			try {
218
			try {
233
				rootMemento.save(writer);
219
				ContextMemento state = mementoManager.lock(context);
234
				writeEditorMemento(context, writer.getBuffer().toString());
220
				IMemento rootMemento = state.createMemento("editors");
235
			} catch (IOException e) {
221
				for (IWorkbenchWindow window : monitoredWindows) {
236
				StatusHandler.log(new Status(IStatus.ERROR, ContextUiPlugin.ID_PLUGIN, "Could not store editor state", //$NON-NLS-1$
222
					IMemento memento = rootMemento.createChild(KEY_MONITORED_WINDOW_OPEN_EDITORS);
237
						e));
223
224
					memento.putString(ATTRIBUTE_CLASS, window.getClass().getCanonicalName());
225
					int number = 0;
226
					if (window instanceof WorkbenchWindow) {
227
						number = ((WorkbenchWindow) window).getNumber();
228
					}
229
					memento.putInteger(ATTRIBUTE_NUMER, number);
230
					// TODO e3.4 replace by memento.putBoolean()
231
					memento.putString(ATTRIBUTE_IS_LAUNCHING, (window == launchingWindow) ? "true" : "false"); //$NON-NLS-1$ //$NON-NLS-2$
232
					memento.putString(ATTRIBUTE_IS_ACTIVE, (window == activeWindow) ? "true" : "false"); //$NON-NLS-1$ //$NON-NLS-2$
233
					((WorkbenchPage) window.getActivePage()).getEditorManager().saveState(memento);
234
				}
235
				state.setChanged(true);
236
			} finally {
237
				mementoManager.unlock(context);
238
			}
238
			}
239
//			// TODO: avoid storing with preferences due to bloat?
240
//			StringWriter writer = new StringWriter();
241
//			try {
242
//				rootMemento.save(writer);
243
//				writeEditorMemento(context, writer.getBuffer().toString());
244
//			} catch (IOException e) {
245
//				StatusHandler.log(new Status(IStatus.ERROR, ContextUiPlugin.ID_PLUGIN, "Could not store editor state", //$NON-NLS-1$
246
//						e));
247
//			}
239
248
240
			Workbench.getInstance().getPreferenceStore().setValue(IPreferenceConstants.REUSE_EDITORS_BOOLEAN,
249
			Workbench.getInstance().getPreferenceStore().setValue(IPreferenceConstants.REUSE_EDITORS_BOOLEAN,
241
					previousCloseEditorsSetting);
250
					previousCloseEditorsSetting);
Lines 243-251 Link Here
243
		}
252
		}
244
	}
253
	}
245
254
246
	public void writeEditorMemento(IInteractionContext context, String memento) {
255
//	public void writeEditorMemento(IInteractionContext context, String memento) {
247
		preferenceStore.setValue(PREFS_PREFIX + context.getHandleIdentifier(), memento);
256
//		preferenceStore.setValue(PREFS_PREFIX + context.getHandleIdentifier(), memento);
248
	}
257
//	}
249
258
250
	@Override
259
	@Override
251
	public void contextCleared(IInteractionContext context) {
260
	public void contextCleared(IInteractionContext context) {
Lines 253-268 Link Here
253
			return;
262
			return;
254
		}
263
		}
255
		closeAllButActiveTaskEditor(context.getHandleIdentifier());
264
		closeAllButActiveTaskEditor(context.getHandleIdentifier());
256
		XMLMemento memento = XMLMemento.createWriteRoot(KEY_CONTEXT_EDITORS);
257
265
258
		// TODO: avoid storing with preferences due to bloat?
259
		StringWriter writer = new StringWriter();
260
		try {
266
		try {
261
			memento.save(writer);
267
			ContextMemento state = mementoManager.lock(context);
262
			writeEditorMemento(context, writer.getBuffer().toString());
268
			state.removeMemento("editors");
263
		} catch (IOException e) {
269
			state.setChanged(true);
264
			StatusHandler.log(new Status(IStatus.ERROR, ContextUiPlugin.ID_PLUGIN, "Could not store editor state", e)); //$NON-NLS-1$
270
		} finally {
265
		}
271
			mementoManager.unlock(context);
272
		}
273
//		XMLMemento memento = XMLMemento.createWriteRoot(KEY_CONTEXT_EDITORS);
274
//
275
//		// TODO: avoid storing with preferences due to bloat?
276
//		StringWriter writer = new StringWriter();
277
//		try {
278
//			memento.save(writer);
279
//			writeEditorMemento(context, writer.getBuffer().toString());
280
//		} catch (IOException e) {
281
//			StatusHandler.log(new Status(IStatus.ERROR, ContextUiPlugin.ID_PLUGIN, "Could not store editor state", e)); //$NON-NLS-1$
282
//		}
266
283
267
		Workbench.getInstance().getPreferenceStore().setValue(IPreferenceConstants.REUSE_EDITORS_BOOLEAN,
284
		Workbench.getInstance().getPreferenceStore().setValue(IPreferenceConstants.REUSE_EDITORS_BOOLEAN,
268
				previousCloseEditorsSetting);
285
				previousCloseEditorsSetting);
Lines 277-284 Link Here
277
		EditorManager editorManager = page.getEditorManager();
294
		EditorManager editorManager = page.getEditorManager();
278
		final ArrayList visibleEditors = new ArrayList(5);
295
		final ArrayList visibleEditors = new ArrayList(5);
279
		final IEditorReference activeEditor[] = new IEditorReference[1];
296
		final IEditorReference activeEditor[] = new IEditorReference[1];
280
		final MultiStatus result = new MultiStatus(PlatformUI.PLUGIN_ID, IStatus.OK,
297
		final MultiStatus result = new MultiStatus(PlatformUI.PLUGIN_ID, IStatus.OK, "", null); //$NON-NLS-1$
281
				"", null); //$NON-NLS-1$
282
298
283
		try {
299
		try {
284
			IMemento[] editorMementos = memento.getChildren(IWorkbenchConstants.TAG_EDITOR);
300
			IMemento[] editorMementos = memento.getChildren(IWorkbenchConstants.TAG_EDITOR);
(-)src/org/eclipse/mylyn/internal/context/ui/ContextMementoManager.java (+173 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2004, 2008 Tasktop Technologies and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Tasktop Technologies - initial API and implementation
10
 *******************************************************************************/
11
12
package org.eclipse.mylyn.internal.context.ui;
13
14
import java.io.BufferedReader;
15
import java.io.BufferedWriter;
16
import java.io.File;
17
import java.io.FileInputStream;
18
import java.io.FileOutputStream;
19
import java.io.IOException;
20
import java.io.InputStreamReader;
21
import java.io.OutputStreamWriter;
22
import java.util.HashMap;
23
import java.util.Map;
24
25
import org.eclipse.core.runtime.Assert;
26
import org.eclipse.core.runtime.IProgressMonitor;
27
import org.eclipse.core.runtime.IStatus;
28
import org.eclipse.core.runtime.MultiStatus;
29
import org.eclipse.core.runtime.Status;
30
import org.eclipse.core.runtime.jobs.Job;
31
import org.eclipse.mylyn.commons.core.StatusHandler;
32
import org.eclipse.mylyn.context.core.IInteractionContext;
33
import org.eclipse.mylyn.internal.provisional.tasks.core.TasksUtil;
34
import org.eclipse.osgi.util.NLS;
35
import org.eclipse.ui.WorkbenchException;
36
import org.eclipse.ui.XMLMemento;
37
38
/**
39
 * @author Steffen Pingel
40
 */
41
public class ContextMementoManager {
42
43
	private class FlushJob extends Job {
44
45
		public FlushJob() {
46
			super("Flush context mementos"); //$NON-NLS-1$
47
		}
48
49
		@Override
50
		protected IStatus run(IProgressMonitor monitor) {
51
			ContextMementoManager.this.flushPending();
52
			return Status.OK_STATUS;
53
		}
54
55
	}
56
57
	private static final String TAG_CONTEXT_STATE = "ContextState"; //$NON-NLS-1$
58
59
	private static final long FLUSH_DELAY = 500;
60
61
	private static final String CHARSET = "UTF-8"; //$NON-NLS-1$
62
63
	private final Map<String, ContextMemento> locked;
64
65
	private final Map<String, ContextMemento> pending;
66
67
	private final File location;
68
69
	private FlushJob flushJob;
70
71
	private boolean scheduled;
72
73
	public ContextMementoManager(File location) {
74
		Assert.isNotNull(location);
75
		this.location = location;
76
		this.locked = new HashMap<String, ContextMemento>();
77
		this.pending = new HashMap<String, ContextMemento>();
78
	}
79
80
	public synchronized void flushPending() {
81
		MultiStatus status = new MultiStatus(ContextUiPlugin.ID_PLUGIN, 0,
82
				"Failed to save context workbench state", null); //$NON-NLS-1$
83
		for (ContextMemento memento : pending.values()) {
84
			if (memento.isChanged()) {
85
				IStatus result = write(memento);
86
				status.add(result);
87
			}
88
		}
89
		pending.clear();
90
		if (!status.isOK()) {
91
			StatusHandler.log(status);
92
		}
93
	}
94
95
	public synchronized ContextMemento lock(IInteractionContext context) {
96
		if (locked.containsKey(context.getHandleIdentifier())) {
97
			throw new IllegalStateException(NLS.bind("Memento for \"{0}\" is already locked", //$NON-NLS-1$
98
					context.getHandleIdentifier()));
99
		}
100
		ContextMemento result = pending.remove(context.getHandleIdentifier());
101
		if (result == null) {
102
			result = read(context);
103
		}
104
		// if reading failed or memento did not exist, start with an empty one
105
		if (result == null) {
106
			result = new ContextMemento(context, XMLMemento.createWriteRoot(TAG_CONTEXT_STATE));
107
		}
108
		locked.put(context.getHandleIdentifier(), result);
109
		return result;
110
	}
111
112
	private ContextMemento read(IInteractionContext context) {
113
		File file = getFile(context);
114
		if (file.exists()) {
115
			try {
116
				BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), CHARSET));
117
				XMLMemento memento = XMLMemento.createReadRoot(reader);
118
				return new ContextMemento(context, memento);
119
			} catch (IOException e) {
120
				StatusHandler.log(new Status(IStatus.ERROR, ContextUiPlugin.ID_PLUGIN, NLS.bind(
121
						"Failed to read context memento from \"{0}\"", file.getAbsolutePath()), e)); //$NON-NLS-1$
122
			} catch (WorkbenchException e) {
123
				StatusHandler.log(new Status(IStatus.ERROR, ContextUiPlugin.ID_PLUGIN, NLS.bind(
124
						"Failed to read context memento from \"{0}\"", file.getAbsolutePath()), e)); //$NON-NLS-1$
125
			}
126
		}
127
		return null;
128
	}
129
130
	private IStatus write(ContextMemento memento) {
131
		File file = getFile(memento.getContext());
132
		if (!file.getParentFile().exists()) {
133
			file.getParentFile().mkdirs();
134
		}
135
		try {
136
			BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), CHARSET));
137
			try {
138
				(memento.getMemento()).save(writer);
139
			} finally {
140
				writer.close();
141
			}
142
		} catch (IOException e) {
143
			return new Status(IStatus.ERROR, ContextUiPlugin.ID_PLUGIN, NLS.bind(
144
					"Failed to write context memento to \"{0}\"", file.getAbsolutePath()), e); //$NON-NLS-1$
145
		}
146
		return Status.OK_STATUS;
147
	}
148
149
	private File getFile(IInteractionContext context) {
150
		return new File(location, TasksUtil.encode(context.getHandleIdentifier()));
151
	}
152
153
	public synchronized void unlock(IInteractionContext context) {
154
		ContextMemento result = locked.remove(context.getHandleIdentifier());
155
		if (result == null) {
156
			StatusHandler.log(new Status(IStatus.ERROR, ContextUiPlugin.ID_PLUGIN, NLS.bind(
157
					"Failed attempt to unlock memento \"{0}\"", context.getHandleIdentifier()))); //$NON-NLS-1$
158
			return;
159
		}
160
		pending.put(context.getHandleIdentifier(), result);
161
		schedule();
162
	}
163
164
	private synchronized void schedule() {
165
		if (!scheduled) {
166
			if (flushJob == null) {
167
				flushJob = new FlushJob();
168
			}
169
			flushJob.schedule(FLUSH_DELAY);
170
		}
171
	}
172
173
}
(-)src/org/eclipse/mylyn/internal/context/ui/ContextMemento.java (+80 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2004, 2008 Tasktop Technologies and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Tasktop Technologies - initial API and implementation
10
 *******************************************************************************/
11
12
package org.eclipse.mylyn.internal.context.ui;
13
14
import org.eclipse.core.runtime.Assert;
15
import org.eclipse.mylyn.context.core.IInteractionContext;
16
import org.eclipse.ui.IMemento;
17
import org.eclipse.ui.XMLMemento;
18
19
/**
20
 * Stores workspace specific settings for a context.
21
 * 
22
 * @author Steffen Pingel
23
 */
24
public class ContextMemento {
25
26
	private boolean changed;
27
28
	private final IInteractionContext context;
29
30
	private final XMLMemento memento;
31
32
	public ContextMemento(IInteractionContext context, XMLMemento memento) {
33
		Assert.isNotNull(context);
34
		Assert.isNotNull(memento);
35
		this.context = context;
36
		this.memento = memento;
37
	}
38
39
	@Override
40
	public boolean equals(Object obj) {
41
		if (!(obj instanceof ContextMemento)) {
42
			return false;
43
		}
44
		return context.equals(((ContextMemento) obj).context);
45
	}
46
47
	public IInteractionContext getContext() {
48
		return context;
49
	}
50
51
	XMLMemento getMemento() {
52
		return memento;
53
	}
54
55
	public IMemento getMemento(String type) {
56
		return memento.getChild(type);
57
	}
58
59
	public IMemento createMemento(String type) {
60
		return memento.createChild(type);
61
	}
62
63
	@Override
64
	public int hashCode() {
65
		return context.hashCode();
66
	}
67
68
	public boolean isChanged() {
69
		return changed;
70
	}
71
72
	public void setChanged(boolean changed) {
73
		this.changed = changed;
74
	}
75
76
	public void removeMemento(String type) {
77
		memento.createChild(type);
78
	}
79
80
}

Return to bug 226618