|
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); |