|
Lines 11-39
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; |
| 20 |
import java.util.List; |
17 |
import java.util.List; |
| 21 |
import java.util.Set; |
18 |
import java.util.Set; |
| 22 |
|
19 |
|
| 23 |
import org.eclipse.core.runtime.Assert; |
|
|
| 24 |
import org.eclipse.core.runtime.ISafeRunnable; |
20 |
import org.eclipse.core.runtime.ISafeRunnable; |
| 25 |
import org.eclipse.core.runtime.IStatus; |
21 |
import org.eclipse.core.runtime.IStatus; |
| 26 |
import org.eclipse.core.runtime.MultiStatus; |
22 |
import org.eclipse.core.runtime.MultiStatus; |
| 27 |
import org.eclipse.core.runtime.Status; |
23 |
import org.eclipse.core.runtime.Status; |
| 28 |
import org.eclipse.core.runtime.preferences.InstanceScope; |
|
|
| 29 |
import org.eclipse.jface.preference.IPreferenceStore; |
| 30 |
import org.eclipse.jface.util.SafeRunnable; |
24 |
import org.eclipse.jface.util.SafeRunnable; |
| 31 |
import org.eclipse.mylyn.commons.core.StatusHandler; |
25 |
import org.eclipse.mylyn.commons.core.StatusHandler; |
| 32 |
import org.eclipse.mylyn.context.core.AbstractContextListener; |
26 |
import org.eclipse.mylyn.context.core.AbstractContextListener; |
| 33 |
import org.eclipse.mylyn.context.core.AbstractContextStructureBridge; |
27 |
import org.eclipse.mylyn.context.core.AbstractContextStructureBridge; |
| 34 |
import org.eclipse.mylyn.context.core.ContextChangeEvent; |
28 |
import org.eclipse.mylyn.context.core.ContextChangeEvent; |
| 35 |
import org.eclipse.mylyn.context.core.ContextCore; |
29 |
import org.eclipse.mylyn.context.core.ContextCore; |
| 36 |
import org.eclipse.mylyn.context.core.IInteractionContext; |
30 |
import org.eclipse.mylyn.context.core.IInteractionContextManager; |
| 37 |
import org.eclipse.mylyn.context.core.IInteractionElement; |
31 |
import org.eclipse.mylyn.context.core.IInteractionElement; |
| 38 |
import org.eclipse.mylyn.context.ui.AbstractContextUiBridge; |
32 |
import org.eclipse.mylyn.context.ui.AbstractContextUiBridge; |
| 39 |
import org.eclipse.mylyn.context.ui.ContextUi; |
33 |
import org.eclipse.mylyn.context.ui.ContextUi; |
|
Lines 51-74
Link Here
|
| 51 |
import org.eclipse.ui.IWorkbenchWindow; |
45 |
import org.eclipse.ui.IWorkbenchWindow; |
| 52 |
import org.eclipse.ui.PartInitException; |
46 |
import org.eclipse.ui.PartInitException; |
| 53 |
import org.eclipse.ui.PlatformUI; |
47 |
import org.eclipse.ui.PlatformUI; |
| 54 |
import org.eclipse.ui.XMLMemento; |
|
|
| 55 |
import org.eclipse.ui.internal.EditorManager; |
48 |
import org.eclipse.ui.internal.EditorManager; |
| 56 |
import org.eclipse.ui.internal.IPreferenceConstants; |
49 |
import org.eclipse.ui.internal.IPreferenceConstants; |
| 57 |
import org.eclipse.ui.internal.IWorkbenchConstants; |
50 |
import org.eclipse.ui.internal.IWorkbenchConstants; |
| 58 |
import org.eclipse.ui.internal.Workbench; |
51 |
import org.eclipse.ui.internal.Workbench; |
| 59 |
import org.eclipse.ui.internal.WorkbenchPage; |
52 |
import org.eclipse.ui.internal.WorkbenchPage; |
| 60 |
import org.eclipse.ui.internal.WorkbenchWindow; |
53 |
import org.eclipse.ui.internal.WorkbenchWindow; |
| 61 |
import org.eclipse.ui.preferences.ScopedPreferenceStore; |
|
|
| 62 |
|
54 |
|
| 63 |
/** |
55 |
/** |
| 64 |
* @author Mik Kersten |
56 |
* @author Mik Kersten |
| 65 |
* @author Shawn Minto |
57 |
* @author Shawn Minto |
| 66 |
*/ |
58 |
*/ |
| 67 |
public class ContextEditorManager extends AbstractContextListener { |
59 |
public class ContextEditorManager { |
| 68 |
|
60 |
|
| 69 |
private static final String PREFS_PREFIX = "editors.task."; //$NON-NLS-1$ |
61 |
static final String MEMENTO_EDITORS = "org.eclipse.mylyn.context.ui.editors"; //$NON-NLS-1$ |
| 70 |
|
62 |
|
| 71 |
private static final String KEY_CONTEXT_EDITORS = "ContextOpenEditors"; //$NON-NLS-1$ |
63 |
private class MementoListener extends ContextMementoListener { |
|
|
64 |
|
| 65 |
@Override |
| 66 |
public void clearState(String contextHandle, boolean isActiveContext) { |
| 67 |
ContextEditorManager.this.clearState(contextHandle, isActiveContext); |
| 68 |
} |
| 69 |
|
| 70 |
@Override |
| 71 |
public void restoreState(ContextMemento memento) { |
| 72 |
ContextEditorManager.this.restoreState(memento); |
| 73 |
} |
| 74 |
|
| 75 |
@Override |
| 76 |
public void saveState(ContextMemento memento) { |
| 77 |
ContextEditorManager.this.saveState(memento); |
| 78 |
} |
| 79 |
|
| 80 |
} |
| 81 |
|
| 82 |
private class ContextListener extends AbstractContextListener { |
| 83 |
|
| 84 |
@Override |
| 85 |
public void contextChanged(ContextChangeEvent event) { |
| 86 |
switch (event.getEventKind()) { |
| 87 |
case INTEREST_CHANGED: |
| 88 |
for (IInteractionElement element : event.getElements()) { |
| 89 |
closeEditor(element, false); |
| 90 |
} |
| 91 |
break; |
| 92 |
case ELEMENTS_DELETED: |
| 93 |
for (IInteractionElement element : event.getElements()) { |
| 94 |
closeEditor(element, true); |
| 95 |
} |
| 96 |
break; |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 72 |
|
100 |
|
| 73 |
private static final String KEY_MONITORED_WINDOW_OPEN_EDITORS = "MonitoredWindowOpenEditors"; //$NON-NLS-1$ |
101 |
private static final String KEY_MONITORED_WINDOW_OPEN_EDITORS = "MonitoredWindowOpenEditors"; //$NON-NLS-1$ |
| 74 |
|
102 |
|
|
Lines 83-123
Link Here
|
| 83 |
private boolean previousCloseEditorsSetting = Workbench.getInstance().getPreferenceStore().getBoolean( |
111 |
private boolean previousCloseEditorsSetting = Workbench.getInstance().getPreferenceStore().getBoolean( |
| 84 |
IPreferenceConstants.REUSE_EDITORS_BOOLEAN); |
112 |
IPreferenceConstants.REUSE_EDITORS_BOOLEAN); |
| 85 |
|
113 |
|
| 86 |
private final IPreferenceStore preferenceStore; |
114 |
private final MementoListener mementoListener; |
|
|
115 |
|
| 116 |
private final ContextListener contextListener; |
| 117 |
|
| 118 |
//private final IPreferenceStore preferenceStore; |
| 87 |
|
119 |
|
| 88 |
public ContextEditorManager() { |
120 |
public ContextEditorManager() { |
| 89 |
preferenceStore = new ScopedPreferenceStore(new InstanceScope(), "org.eclipse.mylyn.resources.ui"); //$NON-NLS-1$ |
121 |
//preferenceStore = new ScopedPreferenceStore(new InstanceScope(), "org.eclipse.mylyn.resources.ui"); //$NON-NLS-1$ |
|
|
122 |
this.mementoListener = new MementoListener(); |
| 123 |
this.contextListener = new ContextListener(); |
| 90 |
} |
124 |
} |
| 91 |
|
125 |
|
| 92 |
@Override |
126 |
public void start(IInteractionContextManager contextManager, ContextMementoManager mementoManager) { |
| 93 |
public void contextChanged(ContextChangeEvent event) { |
127 |
contextManager.addListener(contextListener); |
| 94 |
switch (event.getEventKind()) { |
128 |
mementoManager.addMementoListener(mementoListener); |
| 95 |
case ACTIVATED: |
129 |
} |
| 96 |
openEditorsFromMemento(event.getContext()); |
|
|
| 97 |
break; |
| 98 |
case DEACTIVATED: |
| 99 |
closeEditorsAndSaveMemento(event.getContext()); |
| 100 |
break; |
| 101 |
case INTEREST_CHANGED: |
| 102 |
for (IInteractionElement element : event.getElements()) { |
| 103 |
closeEditor(element, false); |
| 104 |
} |
| 105 |
break; |
| 106 |
case ELEMENTS_DELETED: |
| 107 |
for (IInteractionElement element : event.getElements()) { |
| 108 |
closeEditor(element, true); |
| 109 |
} |
| 110 |
break; |
| 111 |
case CLEARED: |
| 112 |
// use the handle since the context is null when it is cleared |
| 113 |
// bug 255588 |
| 114 |
clearEditorMemento(event.getContextHandle(), event.isActiveContext()); |
| 115 |
break; |
| 116 |
|
130 |
|
| 117 |
} |
131 |
public void stop(IInteractionContextManager contextManager, ContextMementoManager mementoManager) { |
|
|
132 |
contextManager.removeListener(contextListener); |
| 133 |
mementoManager.removeMementoListener(mementoListener); |
| 118 |
} |
134 |
} |
| 119 |
|
135 |
|
| 120 |
public void openEditorsFromMemento(IInteractionContext context) { |
136 |
public void restoreState(ContextMemento state) { |
| 121 |
if (!Workbench.getInstance().isStarting() |
137 |
if (!Workbench.getInstance().isStarting() |
| 122 |
&& ContextUiPlugin.getDefault().getPreferenceStore().getBoolean( |
138 |
&& ContextUiPlugin.getDefault().getPreferenceStore().getBoolean( |
| 123 |
IContextUiPreferenceContstants.AUTO_MANAGE_EDITORS) && !TaskMigrator.isActive()) { |
139 |
IContextUiPreferenceContstants.AUTO_MANAGE_EDITORS) && !TaskMigrator.isActive()) { |
|
Lines 131-142
Link Here
|
| 131 |
ContextCore.getContextManager().setContextCapturePaused(true); |
147 |
ContextCore.getContextManager().setContextCapturePaused(true); |
| 132 |
} |
148 |
} |
| 133 |
String mementoString = null; |
149 |
String mementoString = null; |
| 134 |
// TODO change where memento is stored |
|
|
| 135 |
IWorkbenchWindow activeWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); |
150 |
IWorkbenchWindow activeWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); |
| 136 |
try { |
151 |
try { |
| 137 |
mementoString = readEditorMemento(context.getHandleIdentifier()); |
152 |
// mementoString = readEditorMemento(context.getHandleIdentifier()); |
| 138 |
if (mementoString != null && !mementoString.trim().equals("")) { //$NON-NLS-1$ |
153 |
// if (mementoString != null && !mementoString.trim().equals("")) { //$NON-NLS-1$ |
| 139 |
IMemento memento = XMLMemento.createReadRoot(new StringReader(mementoString)); |
154 |
// IMemento memento = XMLMemento.createReadRoot(new StringReader(mementoString)); |
|
|
155 |
IMemento memento = state.getMemento(MEMENTO_EDITORS); |
| 156 |
if (memento != null) { |
| 140 |
IMemento[] children = memento.getChildren(KEY_MONITORED_WINDOW_OPEN_EDITORS); |
157 |
IMemento[] children = memento.getChildren(KEY_MONITORED_WINDOW_OPEN_EDITORS); |
| 141 |
if (children.length > 0) { |
158 |
if (children.length > 0) { |
| 142 |
// This code supports restore from multiple windows |
159 |
// This code supports restore from multiple windows |
|
Lines 159-165
Link Here
|
| 159 |
"Could not restore all editors, memento: \"" + mementoString + "\"", e)); //$NON-NLS-1$ //$NON-NLS-2$ |
176 |
"Could not restore all editors, memento: \"" + mementoString + "\"", e)); //$NON-NLS-1$ //$NON-NLS-2$ |
| 160 |
} |
177 |
} |
| 161 |
activeWindow.setActivePage(activeWindow.getActivePage()); |
178 |
activeWindow.setActivePage(activeWindow.getActivePage()); |
| 162 |
IInteractionElement activeNode = context.getActiveNode(); |
179 |
IInteractionElement activeNode = state.getContext().getActiveNode(); |
| 163 |
if (activeNode != null) { |
180 |
if (activeNode != null) { |
| 164 |
ContextUi.getUiBridge(activeNode.getContentType()).open(activeNode); |
181 |
ContextUi.getUiBridge(activeNode.getContentType()).open(activeNode); |
| 165 |
} |
182 |
} |
|
Lines 173-179
Link Here
|
| 173 |
} |
190 |
} |
| 174 |
|
191 |
|
| 175 |
private WorkbenchPage getWorkbenchPageForMemento(IMemento memento, IWorkbenchWindow activeWindow) { |
192 |
private WorkbenchPage getWorkbenchPageForMemento(IMemento memento, IWorkbenchWindow activeWindow) { |
| 176 |
|
|
|
| 177 |
String windowToRestoreClassName = memento.getString(ATTRIBUTE_CLASS); |
193 |
String windowToRestoreClassName = memento.getString(ATTRIBUTE_CLASS); |
| 178 |
if (windowToRestoreClassName == null) { |
194 |
if (windowToRestoreClassName == null) { |
| 179 |
windowToRestoreClassName = ""; //$NON-NLS-1$ |
195 |
windowToRestoreClassName = ""; //$NON-NLS-1$ |
|
Lines 226-247
Link Here
|
| 226 |
return null; |
242 |
return null; |
| 227 |
} |
243 |
} |
| 228 |
|
244 |
|
| 229 |
private String readEditorMemento(String handleIdentifier) { |
245 |
// private String readEditorMemento(String handleIdentifier) { |
| 230 |
return preferenceStore.getString(PREFS_PREFIX + handleIdentifier); |
246 |
// return preferenceStore.getString(PREFS_PREFIX + handleIdentifier); |
| 231 |
} |
247 |
// } |
| 232 |
|
248 |
|
| 233 |
public void closeEditorsAndSaveMemento(IInteractionContext context) { |
249 |
public void saveState(ContextMemento state) { |
| 234 |
if (!PlatformUI.getWorkbench().isClosing() |
250 |
if (!PlatformUI.getWorkbench().isClosing() |
| 235 |
&& ContextUiPlugin.getDefault().getPreferenceStore().getBoolean( |
251 |
&& ContextUiPlugin.getDefault().getPreferenceStore().getBoolean( |
| 236 |
IContextUiPreferenceContstants.AUTO_MANAGE_EDITORS) && !TaskMigrator.isActive()) { |
252 |
IContextUiPreferenceContstants.AUTO_MANAGE_EDITORS) && !TaskMigrator.isActive()) { |
| 237 |
closeAllButActiveTaskEditor(context.getHandleIdentifier()); |
253 |
closeAllButActiveTaskEditor(state.getContextHandle()); |
| 238 |
|
254 |
|
| 239 |
XMLMemento rootMemento = XMLMemento.createWriteRoot(KEY_CONTEXT_EDITORS); |
255 |
// XMLMemento rootMemento = XMLMemento.createWriteRoot(KEY_CONTEXT_EDITORS); |
| 240 |
|
256 |
|
| 241 |
IWorkbenchWindow activeWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); |
257 |
IWorkbenchWindow activeWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); |
| 242 |
IWorkbenchWindow launchingWindow = MonitorUi.getLaunchingWorkbenchWindow(); |
258 |
IWorkbenchWindow launchingWindow = MonitorUi.getLaunchingWorkbenchWindow(); |
| 243 |
Set<IWorkbenchWindow> monitoredWindows = MonitorUi.getMonitoredWindows(); |
259 |
Set<IWorkbenchWindow> monitoredWindows = MonitorUi.getMonitoredWindows(); |
| 244 |
|
260 |
|
|
|
261 |
IMemento rootMemento = state.createMemento(MEMENTO_EDITORS); |
| 245 |
for (IWorkbenchWindow window : monitoredWindows) { |
262 |
for (IWorkbenchWindow window : monitoredWindows) { |
| 246 |
IMemento memento = rootMemento.createChild(KEY_MONITORED_WINDOW_OPEN_EDITORS); |
263 |
IMemento memento = rootMemento.createChild(KEY_MONITORED_WINDOW_OPEN_EDITORS); |
| 247 |
|
264 |
|
|
Lines 255-269
Link Here
|
| 255 |
memento.putBoolean(ATTRIBUTE_IS_ACTIVE, window == activeWindow); |
272 |
memento.putBoolean(ATTRIBUTE_IS_ACTIVE, window == activeWindow); |
| 256 |
((WorkbenchPage) window.getActivePage()).getEditorManager().saveState(memento); |
273 |
((WorkbenchPage) window.getActivePage()).getEditorManager().saveState(memento); |
| 257 |
} |
274 |
} |
| 258 |
// TODO: avoid storing with preferences due to bloat? |
275 |
// // TODO: avoid storing with preferences due to bloat? |
| 259 |
StringWriter writer = new StringWriter(); |
276 |
// StringWriter writer = new StringWriter(); |
| 260 |
try { |
277 |
// try { |
| 261 |
rootMemento.save(writer); |
278 |
// rootMemento.save(writer); |
| 262 |
writeEditorMemento(context.getHandleIdentifier(), writer.getBuffer().toString()); |
279 |
// writeEditorMemento(context, writer.getBuffer().toString()); |
| 263 |
} catch (IOException e) { |
280 |
// } catch (IOException e) { |
| 264 |
StatusHandler.log(new Status(IStatus.ERROR, ContextUiPlugin.ID_PLUGIN, "Could not store editor state", //$NON-NLS-1$ |
281 |
// StatusHandler.log(new Status(IStatus.ERROR, ContextUiPlugin.ID_PLUGIN, "Could not store editor state", //$NON-NLS-1$ |
| 265 |
e)); |
282 |
// e)); |
| 266 |
} |
283 |
// } |
| 267 |
|
284 |
|
| 268 |
Workbench.getInstance().getPreferenceStore().setValue(IPreferenceConstants.REUSE_EDITORS_BOOLEAN, |
285 |
Workbench.getInstance().getPreferenceStore().setValue(IPreferenceConstants.REUSE_EDITORS_BOOLEAN, |
| 269 |
previousCloseEditorsSetting); |
286 |
previousCloseEditorsSetting); |
|
Lines 271-297
Link Here
|
| 271 |
} |
288 |
} |
| 272 |
} |
289 |
} |
| 273 |
|
290 |
|
| 274 |
public void writeEditorMemento(String contextHandle, String memento) { |
291 |
// public void writeEditorMemento(IInteractionContext context, String memento) { |
| 275 |
preferenceStore.setValue(PREFS_PREFIX + contextHandle, memento); |
292 |
// preferenceStore.setValue(PREFS_PREFIX + context.getHandleIdentifier(), memento); |
| 276 |
} |
293 |
// } |
| 277 |
|
294 |
|
| 278 |
public void clearEditorMemento(String contextHandle, boolean closeEditors) { |
295 |
public void clearState(String contextHandle, boolean closeEditors) { |
| 279 |
|
296 |
|
| 280 |
if (closeEditors) { |
297 |
if (closeEditors) { |
| 281 |
closeAllButActiveTaskEditor(contextHandle); |
298 |
closeAllButActiveTaskEditor(contextHandle); |
| 282 |
} |
299 |
} |
| 283 |
|
300 |
|
| 284 |
XMLMemento memento = XMLMemento.createWriteRoot(KEY_CONTEXT_EDITORS); |
|
|
| 285 |
|
| 286 |
// TODO: avoid storing with preferences due to bloat? |
| 287 |
StringWriter writer = new StringWriter(); |
| 288 |
try { |
| 289 |
memento.save(writer); |
| 290 |
writeEditorMemento(contextHandle, writer.getBuffer().toString()); |
| 291 |
} catch (IOException e) { |
| 292 |
StatusHandler.log(new Status(IStatus.ERROR, ContextUiPlugin.ID_PLUGIN, "Could not store editor state", e)); //$NON-NLS-1$ |
| 293 |
} |
| 294 |
|
| 295 |
Workbench.getInstance().getPreferenceStore().setValue(IPreferenceConstants.REUSE_EDITORS_BOOLEAN, |
301 |
Workbench.getInstance().getPreferenceStore().setValue(IPreferenceConstants.REUSE_EDITORS_BOOLEAN, |
| 296 |
previousCloseEditorsSetting); |
302 |
previousCloseEditorsSetting); |
| 297 |
if (closeEditors) { |
303 |
if (closeEditors) { |
|
Lines 302-311
Link Here
|
| 302 |
/** |
308 |
/** |
| 303 |
* HACK: will fail to restore different parts with same name |
309 |
* HACK: will fail to restore different parts with same name |
| 304 |
*/ |
310 |
*/ |
| 305 |
@SuppressWarnings("unchecked") |
|
|
| 306 |
private void restoreEditors(WorkbenchPage page, IMemento memento, boolean isActiveWindow) { |
311 |
private void restoreEditors(WorkbenchPage page, IMemento memento, boolean isActiveWindow) { |
| 307 |
EditorManager editorManager = page.getEditorManager(); |
312 |
EditorManager editorManager = page.getEditorManager(); |
| 308 |
final ArrayList visibleEditors = new ArrayList(5); |
313 |
final ArrayList<?> visibleEditors = new ArrayList<Object>(5); |
| 309 |
final IEditorReference activeEditor[] = new IEditorReference[1]; |
314 |
final IEditorReference activeEditor[] = new IEditorReference[1]; |
| 310 |
final MultiStatus result = new MultiStatus(PlatformUI.PLUGIN_ID, IStatus.OK, "", null); //$NON-NLS-1$ |
315 |
final MultiStatus result = new MultiStatus(PlatformUI.PLUGIN_ID, IStatus.OK, "", null); //$NON-NLS-1$ |
| 311 |
|
316 |
|
|
Lines 441-453
Link Here
|
| 441 |
} |
446 |
} |
| 442 |
} |
447 |
} |
| 443 |
|
448 |
|
| 444 |
public void copyEditorMemento(String sourceHandle, String targetHandle) { |
|
|
| 445 |
Assert.isNotNull(sourceHandle); |
| 446 |
Assert.isNotNull(targetHandle); |
| 447 |
String memento = readEditorMemento(sourceHandle); |
| 448 |
if (memento != null) { |
| 449 |
writeEditorMemento(targetHandle, memento); |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
} |
449 |
} |