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