|
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-35
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.ContextCore; |
26 |
import org.eclipse.mylyn.context.core.ContextCore; |
| 32 |
import org.eclipse.mylyn.context.core.IInteractionContext; |
27 |
import org.eclipse.mylyn.context.core.IInteractionContext; |
|
|
28 |
import org.eclipse.mylyn.context.core.IInteractionContextManager; |
| 33 |
import org.eclipse.mylyn.context.core.IInteractionElement; |
29 |
import org.eclipse.mylyn.context.core.IInteractionElement; |
| 34 |
import org.eclipse.mylyn.context.ui.AbstractContextUiBridge; |
30 |
import org.eclipse.mylyn.context.ui.AbstractContextUiBridge; |
| 35 |
import org.eclipse.mylyn.context.ui.ContextUi; |
31 |
import org.eclipse.mylyn.context.ui.ContextUi; |
|
Lines 46-70
Link Here
|
| 46 |
import org.eclipse.ui.IWorkbenchWindow; |
42 |
import org.eclipse.ui.IWorkbenchWindow; |
| 47 |
import org.eclipse.ui.PartInitException; |
43 |
import org.eclipse.ui.PartInitException; |
| 48 |
import org.eclipse.ui.PlatformUI; |
44 |
import org.eclipse.ui.PlatformUI; |
| 49 |
import org.eclipse.ui.XMLMemento; |
|
|
| 50 |
import org.eclipse.ui.internal.EditorManager; |
45 |
import org.eclipse.ui.internal.EditorManager; |
| 51 |
import org.eclipse.ui.internal.IPreferenceConstants; |
46 |
import org.eclipse.ui.internal.IPreferenceConstants; |
| 52 |
import org.eclipse.ui.internal.IWorkbenchConstants; |
47 |
import org.eclipse.ui.internal.IWorkbenchConstants; |
| 53 |
import org.eclipse.ui.internal.Workbench; |
48 |
import org.eclipse.ui.internal.Workbench; |
| 54 |
import org.eclipse.ui.internal.WorkbenchMessages; |
|
|
| 55 |
import org.eclipse.ui.internal.WorkbenchPage; |
49 |
import org.eclipse.ui.internal.WorkbenchPage; |
| 56 |
import org.eclipse.ui.internal.WorkbenchWindow; |
50 |
import org.eclipse.ui.internal.WorkbenchWindow; |
| 57 |
import org.eclipse.ui.preferences.ScopedPreferenceStore; |
|
|
| 58 |
|
51 |
|
| 59 |
/** |
52 |
/** |
| 60 |
* @author Mik Kersten |
53 |
* @author Mik Kersten |
| 61 |
* @author Shawn Minto |
54 |
* @author Shawn Minto |
| 62 |
*/ |
55 |
*/ |
| 63 |
public class ContextEditorManager extends AbstractContextListener { |
56 |
public class ContextEditorManager { |
| 64 |
|
57 |
|
| 65 |
private static final String PREFS_PREFIX = "editors.task."; //$NON-NLS-1$ |
58 |
private class MementoListener extends ContextMementoListener { |
| 66 |
|
59 |
|
| 67 |
private static final String KEY_CONTEXT_EDITORS = "ContextOpenEditors"; //$NON-NLS-1$ |
60 |
@Override |
|
|
61 |
public void clearState(ContextMemento memento) { |
| 62 |
ContextEditorManager.this.clearState(memento); |
| 63 |
} |
| 64 |
|
| 65 |
@Override |
| 66 |
public void restoreState(ContextMemento memento) { |
| 67 |
ContextEditorManager.this.restoreState(memento); |
| 68 |
} |
| 69 |
|
| 70 |
@Override |
| 71 |
public void saveState(ContextMemento memento) { |
| 72 |
ContextEditorManager.this.saveState(memento); |
| 73 |
} |
| 74 |
|
| 75 |
} |
| 76 |
|
| 77 |
private class ContextListener extends AbstractContextListener { |
| 78 |
|
| 79 |
@Override |
| 80 |
public void interestChanged(List<IInteractionElement> elements) { |
| 81 |
for (IInteractionElement element : elements) { |
| 82 |
closeEditor(element, false); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
@Override |
| 87 |
public void elementsDeleted(List<IInteractionElement> elements) { |
| 88 |
for (IInteractionElement element : elements) { |
| 89 |
closeEditor(element, true); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
} |
| 68 |
|
94 |
|
| 69 |
private static final String KEY_MONITORED_WINDOW_OPEN_EDITORS = "MonitoredWindowOpenEditors"; //$NON-NLS-1$ |
95 |
private static final String KEY_MONITORED_WINDOW_OPEN_EDITORS = "MonitoredWindowOpenEditors"; //$NON-NLS-1$ |
| 70 |
|
96 |
|
|
Lines 79-92
Link Here
|
| 79 |
private boolean previousCloseEditorsSetting = Workbench.getInstance().getPreferenceStore().getBoolean( |
105 |
private boolean previousCloseEditorsSetting = Workbench.getInstance().getPreferenceStore().getBoolean( |
| 80 |
IPreferenceConstants.REUSE_EDITORS_BOOLEAN); |
106 |
IPreferenceConstants.REUSE_EDITORS_BOOLEAN); |
| 81 |
|
107 |
|
| 82 |
private final IPreferenceStore preferenceStore; |
108 |
private final MementoListener mementoListener; |
|
|
109 |
|
| 110 |
private final ContextListener contextListener; |
| 111 |
|
| 112 |
//private final IPreferenceStore preferenceStore; |
| 83 |
|
113 |
|
| 84 |
public ContextEditorManager() { |
114 |
public ContextEditorManager() { |
| 85 |
preferenceStore = new ScopedPreferenceStore(new InstanceScope(), "org.eclipse.mylyn.resources.ui"); //$NON-NLS-1$ |
115 |
//preferenceStore = new ScopedPreferenceStore(new InstanceScope(), "org.eclipse.mylyn.resources.ui"); //$NON-NLS-1$ |
|
|
116 |
this.mementoListener = new MementoListener(); |
| 117 |
this.contextListener = new ContextListener(); |
| 86 |
} |
118 |
} |
| 87 |
|
119 |
|
| 88 |
@Override |
120 |
public void start(IInteractionContextManager contextManager, ContextMementoManager mementoManager) { |
| 89 |
public void contextActivated(IInteractionContext context) { |
121 |
contextManager.addListener(contextListener); |
|
|
122 |
mementoManager.addMementoListener(mementoListener); |
| 123 |
} |
| 124 |
|
| 125 |
public void stop(IInteractionContextManager contextManager, ContextMementoManager mementoManager) { |
| 126 |
contextManager.removeListener(contextListener); |
| 127 |
mementoManager.removeMementoListener(mementoListener); |
| 128 |
} |
| 129 |
|
| 130 |
public void restoreState(ContextMemento state) { |
| 90 |
if (!Workbench.getInstance().isStarting() |
131 |
if (!Workbench.getInstance().isStarting() |
| 91 |
&& ContextUiPlugin.getDefault().getPreferenceStore().getBoolean( |
132 |
&& ContextUiPlugin.getDefault().getPreferenceStore().getBoolean( |
| 92 |
IContextUiPreferenceContstants.AUTO_MANAGE_EDITORS)) { |
133 |
IContextUiPreferenceContstants.AUTO_MANAGE_EDITORS)) { |
|
Lines 103-111
Link Here
|
| 103 |
// TODO change where memento is stored |
144 |
// TODO change where memento is stored |
| 104 |
IWorkbenchWindow activeWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); |
145 |
IWorkbenchWindow activeWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); |
| 105 |
try { |
146 |
try { |
| 106 |
mementoString = readEditorMemento(context); |
147 |
//mementoString = readEditorMemento(context); |
| 107 |
if (mementoString != null && !mementoString.trim().equals("")) { //$NON-NLS-1$ |
148 |
// if (mementoString != null && !mementoString.trim().equals("")) { //$NON-NLS-1$ |
| 108 |
IMemento memento = XMLMemento.createReadRoot(new StringReader(mementoString)); |
149 |
// IMemento memento = XMLMemento.createReadRoot(new StringReader(mementoString)); |
|
|
150 |
IMemento memento = state.getMemento("editors"); |
| 151 |
if (memento != null) { |
| 109 |
IMemento[] children = memento.getChildren(KEY_MONITORED_WINDOW_OPEN_EDITORS); |
152 |
IMemento[] children = memento.getChildren(KEY_MONITORED_WINDOW_OPEN_EDITORS); |
| 110 |
if (children.length > 0) { |
153 |
if (children.length > 0) { |
| 111 |
// This code supports restore from multiple windows |
154 |
// This code supports restore from multiple windows |
|
Lines 128-134
Link Here
|
| 128 |
"Could not restore all editors, memento: \"" + mementoString + "\"", e)); //$NON-NLS-1$ //$NON-NLS-2$ |
171 |
"Could not restore all editors, memento: \"" + mementoString + "\"", e)); //$NON-NLS-1$ //$NON-NLS-2$ |
| 129 |
} |
172 |
} |
| 130 |
activeWindow.setActivePage(activeWindow.getActivePage()); |
173 |
activeWindow.setActivePage(activeWindow.getActivePage()); |
| 131 |
IInteractionElement activeNode = context.getActiveNode(); |
174 |
IInteractionElement activeNode = state.getContext().getActiveNode(); |
| 132 |
if (activeNode != null) { |
175 |
if (activeNode != null) { |
| 133 |
ContextUi.getUiBridge(activeNode.getContentType()).open(activeNode); |
176 |
ContextUi.getUiBridge(activeNode.getContentType()).open(activeNode); |
| 134 |
} |
177 |
} |
|
Lines 142-148
Link Here
|
| 142 |
} |
185 |
} |
| 143 |
|
186 |
|
| 144 |
private WorkbenchPage getWorkbenchPageForMemento(IMemento memento, IWorkbenchWindow activeWindow) { |
187 |
private WorkbenchPage getWorkbenchPageForMemento(IMemento memento, IWorkbenchWindow activeWindow) { |
| 145 |
|
|
|
| 146 |
String windowToRestoreClassName = memento.getString(ATTRIBUTE_CLASS); |
188 |
String windowToRestoreClassName = memento.getString(ATTRIBUTE_CLASS); |
| 147 |
if (windowToRestoreClassName == null) { |
189 |
if (windowToRestoreClassName == null) { |
| 148 |
windowToRestoreClassName = ""; //$NON-NLS-1$ |
190 |
windowToRestoreClassName = ""; //$NON-NLS-1$ |
|
Lines 196-218
Link Here
|
| 196 |
return null; |
238 |
return null; |
| 197 |
} |
239 |
} |
| 198 |
|
240 |
|
| 199 |
private String readEditorMemento(IInteractionContext context) { |
241 |
// private String readEditorMemento(IInteractionContext context) { |
| 200 |
return preferenceStore.getString(PREFS_PREFIX + context.getHandleIdentifier()); |
242 |
// return preferenceStore.getString(PREFS_PREFIX + context.getHandleIdentifier()); |
| 201 |
} |
243 |
// } |
| 202 |
|
244 |
|
| 203 |
@Override |
245 |
public void saveState(ContextMemento state) { |
| 204 |
public void contextDeactivated(IInteractionContext context) { |
|
|
| 205 |
if (!PlatformUI.getWorkbench().isClosing() |
246 |
if (!PlatformUI.getWorkbench().isClosing() |
| 206 |
&& ContextUiPlugin.getDefault().getPreferenceStore().getBoolean( |
247 |
&& ContextUiPlugin.getDefault().getPreferenceStore().getBoolean( |
| 207 |
IContextUiPreferenceContstants.AUTO_MANAGE_EDITORS)) { |
248 |
IContextUiPreferenceContstants.AUTO_MANAGE_EDITORS)) { |
| 208 |
closeAllButActiveTaskEditor(context.getHandleIdentifier()); |
249 |
closeAllButActiveTaskEditor(state.getContext().getHandleIdentifier()); |
| 209 |
|
250 |
|
| 210 |
XMLMemento rootMemento = XMLMemento.createWriteRoot(KEY_CONTEXT_EDITORS); |
251 |
//XMLMemento rootMemento = XMLMemento.createWriteRoot(KEY_CONTEXT_EDITORS); |
| 211 |
|
252 |
|
| 212 |
IWorkbenchWindow activeWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); |
253 |
IWorkbenchWindow activeWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); |
| 213 |
IWorkbenchWindow launchingWindow = MonitorUi.getLaunchingWorkbenchWindow(); |
254 |
IWorkbenchWindow launchingWindow = MonitorUi.getLaunchingWorkbenchWindow(); |
| 214 |
Set<IWorkbenchWindow> monitoredWindows = MonitorUi.getMonitoredWindows(); |
255 |
Set<IWorkbenchWindow> monitoredWindows = MonitorUi.getMonitoredWindows(); |
| 215 |
|
256 |
|
|
|
257 |
IMemento rootMemento = state.createMemento("editors"); |
| 216 |
for (IWorkbenchWindow window : monitoredWindows) { |
258 |
for (IWorkbenchWindow window : monitoredWindows) { |
| 217 |
IMemento memento = rootMemento.createChild(KEY_MONITORED_WINDOW_OPEN_EDITORS); |
259 |
IMemento memento = rootMemento.createChild(KEY_MONITORED_WINDOW_OPEN_EDITORS); |
| 218 |
|
260 |
|
|
Lines 227-241
Link Here
|
| 227 |
memento.putString(ATTRIBUTE_IS_ACTIVE, (window == activeWindow) ? "true" : "false"); //$NON-NLS-1$ //$NON-NLS-2$ |
269 |
memento.putString(ATTRIBUTE_IS_ACTIVE, (window == activeWindow) ? "true" : "false"); //$NON-NLS-1$ //$NON-NLS-2$ |
| 228 |
((WorkbenchPage) window.getActivePage()).getEditorManager().saveState(memento); |
270 |
((WorkbenchPage) window.getActivePage()).getEditorManager().saveState(memento); |
| 229 |
} |
271 |
} |
| 230 |
// TODO: avoid storing with preferences due to bloat? |
272 |
// // TODO: avoid storing with preferences due to bloat? |
| 231 |
StringWriter writer = new StringWriter(); |
273 |
// StringWriter writer = new StringWriter(); |
| 232 |
try { |
274 |
// try { |
| 233 |
rootMemento.save(writer); |
275 |
// rootMemento.save(writer); |
| 234 |
writeEditorMemento(context, writer.getBuffer().toString()); |
276 |
// writeEditorMemento(context, writer.getBuffer().toString()); |
| 235 |
} catch (IOException e) { |
277 |
// } catch (IOException e) { |
| 236 |
StatusHandler.log(new Status(IStatus.ERROR, ContextUiPlugin.ID_PLUGIN, "Could not store editor state", //$NON-NLS-1$ |
278 |
// StatusHandler.log(new Status(IStatus.ERROR, ContextUiPlugin.ID_PLUGIN, "Could not store editor state", //$NON-NLS-1$ |
| 237 |
e)); |
279 |
// e)); |
| 238 |
} |
280 |
// } |
| 239 |
|
281 |
|
| 240 |
Workbench.getInstance().getPreferenceStore().setValue(IPreferenceConstants.REUSE_EDITORS_BOOLEAN, |
282 |
Workbench.getInstance().getPreferenceStore().setValue(IPreferenceConstants.REUSE_EDITORS_BOOLEAN, |
| 241 |
previousCloseEditorsSetting); |
283 |
previousCloseEditorsSetting); |
|
Lines 243-268
Link Here
|
| 243 |
} |
285 |
} |
| 244 |
} |
286 |
} |
| 245 |
|
287 |
|
| 246 |
public void writeEditorMemento(IInteractionContext context, String memento) { |
288 |
// public void writeEditorMemento(IInteractionContext context, String memento) { |
| 247 |
preferenceStore.setValue(PREFS_PREFIX + context.getHandleIdentifier(), memento); |
289 |
// preferenceStore.setValue(PREFS_PREFIX + context.getHandleIdentifier(), memento); |
| 248 |
} |
290 |
// } |
| 249 |
|
291 |
|
| 250 |
@Override |
292 |
public void clearState(ContextMemento memento) { |
| 251 |
public void contextCleared(IInteractionContext context) { |
293 |
IInteractionContext context = memento.getContext(); |
| 252 |
if (context == null) { |
294 |
if (context == null) { |
| 253 |
return; |
295 |
return; |
| 254 |
} |
296 |
} |
| 255 |
closeAllButActiveTaskEditor(context.getHandleIdentifier()); |
297 |
closeAllButActiveTaskEditor(context.getHandleIdentifier()); |
| 256 |
XMLMemento memento = XMLMemento.createWriteRoot(KEY_CONTEXT_EDITORS); |
|
|
| 257 |
|
298 |
|
| 258 |
// TODO: avoid storing with preferences due to bloat? |
299 |
// XMLMemento memento = XMLMemento.createWriteRoot(KEY_CONTEXT_EDITORS); |
| 259 |
StringWriter writer = new StringWriter(); |
300 |
// |
| 260 |
try { |
301 |
// // TODO: avoid storing with preferences due to bloat? |
| 261 |
memento.save(writer); |
302 |
// StringWriter writer = new StringWriter(); |
| 262 |
writeEditorMemento(context, writer.getBuffer().toString()); |
303 |
// try { |
| 263 |
} catch (IOException e) { |
304 |
// memento.save(writer); |
| 264 |
StatusHandler.log(new Status(IStatus.ERROR, ContextUiPlugin.ID_PLUGIN, "Could not store editor state", e)); //$NON-NLS-1$ |
305 |
// writeEditorMemento(context, writer.getBuffer().toString()); |
| 265 |
} |
306 |
// } catch (IOException e) { |
|
|
307 |
// StatusHandler.log(new Status(IStatus.ERROR, ContextUiPlugin.ID_PLUGIN, "Could not store editor state", e)); |
| 308 |
// } |
| 266 |
|
309 |
|
| 267 |
Workbench.getInstance().getPreferenceStore().setValue(IPreferenceConstants.REUSE_EDITORS_BOOLEAN, |
310 |
Workbench.getInstance().getPreferenceStore().setValue(IPreferenceConstants.REUSE_EDITORS_BOOLEAN, |
| 268 |
previousCloseEditorsSetting); |
311 |
previousCloseEditorsSetting); |
|
Lines 277-284
Link Here
|
| 277 |
EditorManager editorManager = page.getEditorManager(); |
320 |
EditorManager editorManager = page.getEditorManager(); |
| 278 |
final ArrayList visibleEditors = new ArrayList(5); |
321 |
final ArrayList visibleEditors = new ArrayList(5); |
| 279 |
final IEditorReference activeEditor[] = new IEditorReference[1]; |
322 |
final IEditorReference activeEditor[] = new IEditorReference[1]; |
| 280 |
final MultiStatus result = new MultiStatus(PlatformUI.PLUGIN_ID, IStatus.OK, |
323 |
final MultiStatus result = new MultiStatus(PlatformUI.PLUGIN_ID, IStatus.OK, "", null); //$NON-NLS-1$ |
| 281 |
"", null); //$NON-NLS-1$ |
|
|
| 282 |
|
324 |
|
| 283 |
try { |
325 |
try { |
| 284 |
IMemento[] editorMementos = memento.getChildren(IWorkbenchConstants.TAG_EDITOR); |
326 |
IMemento[] editorMementos = memento.getChildren(IWorkbenchConstants.TAG_EDITOR); |
|
Lines 310-316
Link Here
|
| 310 |
} |
352 |
} |
| 311 |
} |
353 |
} |
| 312 |
} catch (Exception e) { |
354 |
} catch (Exception e) { |
| 313 |
StatusHandler.log(new Status(IStatus.ERROR, ContextUiPlugin.ID_PLUGIN, "Could not restore editors", e)); //$NON-NLS-1$ |
355 |
StatusHandler.log(new Status(IStatus.ERROR, ContextUiPlugin.ID_PLUGIN, "Could not restore editors", e)); |
| 314 |
} |
356 |
} |
| 315 |
} |
357 |
} |
| 316 |
|
358 |
|
|
Lines 345-351
Link Here
|
| 345 |
} |
387 |
} |
| 346 |
} |
388 |
} |
| 347 |
} catch (Throwable t) { |
389 |
} catch (Throwable t) { |
| 348 |
StatusHandler.log(new Status(IStatus.ERROR, ContextUiPlugin.ID_PLUGIN, "Could not auto close editor", t)); //$NON-NLS-1$ |
390 |
StatusHandler.log(new Status(IStatus.ERROR, ContextUiPlugin.ID_PLUGIN, "Could not auto close editor", t)); |
| 349 |
} |
391 |
} |
| 350 |
} |
392 |
} |
| 351 |
|
393 |
|
|
Lines 368-374
Link Here
|
| 368 |
} |
410 |
} |
| 369 |
} |
411 |
} |
| 370 |
} catch (Throwable t) { |
412 |
} catch (Throwable t) { |
| 371 |
StatusHandler.log(new Status(IStatus.ERROR, ContextUiPlugin.ID_PLUGIN, "Could not auto close editor", t)); //$NON-NLS-1$ |
413 |
StatusHandler.log(new Status(IStatus.ERROR, ContextUiPlugin.ID_PLUGIN, "Could not auto close editor", t)); |
| 372 |
} |
414 |
} |
| 373 |
} |
415 |
} |
| 374 |
|
416 |
|
|
Lines 380-399
Link Here
|
| 380 |
return true; |
422 |
return true; |
| 381 |
} |
423 |
} |
| 382 |
|
424 |
|
| 383 |
@Override |
|
|
| 384 |
public void interestChanged(List<IInteractionElement> elements) { |
| 385 |
for (IInteractionElement element : elements) { |
| 386 |
closeEditor(element, false); |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
@Override |
| 391 |
public void elementsDeleted(List<IInteractionElement> elements) { |
| 392 |
for (IInteractionElement element : elements) { |
| 393 |
closeEditor(element, true); |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
private void closeEditor(IInteractionElement element, boolean force) { |
425 |
private void closeEditor(IInteractionElement element, boolean force) { |
| 398 |
if (ContextUiPlugin.getDefault().getPreferenceStore().getBoolean( |
426 |
if (ContextUiPlugin.getDefault().getPreferenceStore().getBoolean( |
| 399 |
IContextUiPreferenceContstants.AUTO_MANAGE_EDITORS)) { |
427 |
IContextUiPreferenceContstants.AUTO_MANAGE_EDITORS)) { |