|
Lines 30-37
Link Here
|
| 30 |
import org.eclipse.mylyn.context.core.IInteractionElement; |
30 |
import org.eclipse.mylyn.context.core.IInteractionElement; |
| 31 |
import org.eclipse.mylyn.context.ui.AbstractContextUiBridge; |
31 |
import org.eclipse.mylyn.context.ui.AbstractContextUiBridge; |
| 32 |
import org.eclipse.mylyn.context.ui.ContextUi; |
32 |
import org.eclipse.mylyn.context.ui.ContextUi; |
| 33 |
import org.eclipse.mylyn.internal.monitor.ui.MonitorUiPlugin; |
|
|
| 34 |
import org.eclipse.mylyn.internal.tasks.ui.TasksUiPlugin; |
33 |
import org.eclipse.mylyn.internal.tasks.ui.TasksUiPlugin; |
|
|
34 |
import org.eclipse.mylyn.monitor.ui.MonitorUi; |
| 35 |
import org.eclipse.mylyn.tasks.core.AbstractTask; |
35 |
import org.eclipse.mylyn.tasks.core.AbstractTask; |
| 36 |
import org.eclipse.mylyn.tasks.ui.editors.NewTaskEditorInput; |
36 |
import org.eclipse.mylyn.tasks.ui.editors.NewTaskEditorInput; |
| 37 |
import org.eclipse.mylyn.tasks.ui.editors.TaskEditorInput; |
37 |
import org.eclipse.mylyn.tasks.ui.editors.TaskEditorInput; |
|
Lines 51-56
Link Here
|
| 51 |
import org.eclipse.ui.internal.Workbench; |
51 |
import org.eclipse.ui.internal.Workbench; |
| 52 |
import org.eclipse.ui.internal.WorkbenchMessages; |
52 |
import org.eclipse.ui.internal.WorkbenchMessages; |
| 53 |
import org.eclipse.ui.internal.WorkbenchPage; |
53 |
import org.eclipse.ui.internal.WorkbenchPage; |
|
|
54 |
import org.eclipse.ui.internal.WorkbenchWindow; |
| 54 |
import org.eclipse.ui.preferences.ScopedPreferenceStore; |
55 |
import org.eclipse.ui.preferences.ScopedPreferenceStore; |
| 55 |
|
56 |
|
| 56 |
/** |
57 |
/** |
|
Lines 63-68
Link Here
|
| 63 |
|
64 |
|
| 64 |
private static final String KEY_CONTEXT_EDITORS = "ContextOpenEditors"; |
65 |
private static final String KEY_CONTEXT_EDITORS = "ContextOpenEditors"; |
| 65 |
|
66 |
|
|
|
67 |
private static final String KEY_MONITORED_WINDOW_OPEN_EDITORS = "MonitoredWindowOpenEditors"; |
| 68 |
|
| 69 |
private static final String ATTRIBUTE_CLASS = "class"; |
| 70 |
|
| 71 |
private static final String ATTRIBUTE_NUMER = "number"; |
| 72 |
|
| 73 |
private static final String ATTRIBUTE_IS_LAUNCHING = "isLaunching"; |
| 74 |
|
| 75 |
private static final String ATTRIBUTE_IS_ACTIVE = "isActive"; |
| 76 |
|
| 66 |
private boolean previousCloseEditorsSetting = Workbench.getInstance().getPreferenceStore().getBoolean( |
77 |
private boolean previousCloseEditorsSetting = Workbench.getInstance().getPreferenceStore().getBoolean( |
| 67 |
IPreferenceConstants.REUSE_EDITORS_BOOLEAN); |
78 |
IPreferenceConstants.REUSE_EDITORS_BOOLEAN); |
| 68 |
|
79 |
|
|
Lines 85-103
Link Here
|
| 85 |
if (!wasPaused) { |
96 |
if (!wasPaused) { |
| 86 |
ContextCore.getContextManager().setContextCapturePaused(true); |
97 |
ContextCore.getContextManager().setContextCapturePaused(true); |
| 87 |
} |
98 |
} |
| 88 |
WorkbenchPage page = (WorkbenchPage) workbench.getActiveWorkbenchWindow().getActivePage(); |
|
|
| 89 |
|
| 90 |
String mementoString = null; |
99 |
String mementoString = null; |
| 91 |
// API-3.0: remove coupling to AbstractTask, change where memento is stored |
100 |
// API-3.0: remove coupling to AbstractTask, change where memento is stored |
| 92 |
AbstractTask task = TasksUiPlugin.getTaskListManager().getTaskList().getTask( |
101 |
AbstractTask task = TasksUiPlugin.getTaskListManager().getTaskList().getTask( |
| 93 |
context.getHandleIdentifier()); |
102 |
context.getHandleIdentifier()); |
|
|
103 |
IWorkbenchWindow activeWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); |
| 94 |
if (task != null) { |
104 |
if (task != null) { |
| 95 |
try { |
105 |
try { |
| 96 |
mementoString = readEditorMemento(task); |
106 |
mementoString = readEditorMemento(task); |
| 97 |
if (mementoString != null && !mementoString.trim().equals("")) { |
107 |
if (mementoString != null && !mementoString.trim().equals("")) { |
| 98 |
IMemento memento = XMLMemento.createReadRoot(new StringReader(mementoString)); |
108 |
IMemento memento = XMLMemento.createReadRoot(new StringReader(mementoString)); |
| 99 |
if (memento != null) { |
109 |
IMemento[] children = memento.getChildren(KEY_MONITORED_WINDOW_OPEN_EDITORS); |
| 100 |
restoreEditors(page, memento); |
110 |
if (children.length > 0) { |
|
|
111 |
// This code supports restore from multiple windows |
| 112 |
for (IMemento child : children) { |
| 113 |
WorkbenchPage page = getWorkbenchPageForMemento(child, activeWindow); |
| 114 |
if (child != null && page != null) { |
| 115 |
restoreEditors(page, child, page.getWorkbenchWindow() == activeWindow); |
| 116 |
} |
| 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 |
} |
| 101 |
} |
124 |
} |
| 102 |
} |
125 |
} |
| 103 |
} catch (Exception e) { |
126 |
} catch (Exception e) { |
|
Lines 105-110
Link Here
|
| 105 |
"Could not restore all editors, memento: \"" + mementoString + "\"", e)); |
128 |
"Could not restore all editors, memento: \"" + mementoString + "\"", e)); |
| 106 |
} |
129 |
} |
| 107 |
} |
130 |
} |
|
|
131 |
activeWindow.setActivePage(activeWindow.getActivePage()); |
| 108 |
IInteractionElement activeNode = context.getActiveNode(); |
132 |
IInteractionElement activeNode = context.getActiveNode(); |
| 109 |
if (activeNode != null) { |
133 |
if (activeNode != null) { |
| 110 |
ContextUi.getUiBridge(activeNode.getContentType()).open(activeNode); |
134 |
ContextUi.getUiBridge(activeNode.getContentType()).open(activeNode); |
|
Lines 118-123
Link Here
|
| 118 |
} |
142 |
} |
| 119 |
} |
143 |
} |
| 120 |
|
144 |
|
|
|
145 |
private WorkbenchPage getWorkbenchPageForMemento(IMemento memento, IWorkbenchWindow activeWindow) { |
| 146 |
|
| 147 |
String windowToRestoreClassName = memento.getString(ATTRIBUTE_CLASS); |
| 148 |
if (windowToRestoreClassName == null) { |
| 149 |
windowToRestoreClassName = ""; |
| 150 |
} |
| 151 |
Integer windowToRestorenumber = memento.getInteger(ATTRIBUTE_NUMER); |
| 152 |
if (windowToRestorenumber == null) { |
| 153 |
windowToRestorenumber = 0; |
| 154 |
} |
| 155 |
|
| 156 |
// try to match the open windows to the one that we want to restore |
| 157 |
Set<IWorkbenchWindow> monitoredWindows = MonitorUi.getMonitoredWindows(); |
| 158 |
for (IWorkbenchWindow window : monitoredWindows) { |
| 159 |
int windowNumber = 0; |
| 160 |
if (window instanceof WorkbenchWindow) { |
| 161 |
windowNumber = ((WorkbenchWindow) window).getNumber(); |
| 162 |
} |
| 163 |
if (window.getClass().getCanonicalName().equals(windowToRestoreClassName) |
| 164 |
&& windowNumber == windowToRestorenumber) { |
| 165 |
return (WorkbenchPage) window.getActivePage(); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
// we don't have a good match here, try to make an educated guess |
| 170 |
Boolean isActive = memento.getBoolean(ATTRIBUTE_IS_ACTIVE); |
| 171 |
if (isActive == null) { |
| 172 |
isActive = false; |
| 173 |
} |
| 174 |
|
| 175 |
// both of these defaulting to true should ensure that all editors are opened even if their previous editor is not around |
| 176 |
boolean shouldRestoreUnknownWindowToActive = true; // TODO could add a preference here |
| 177 |
boolean shouldRestoreActiveWindowToActive = true; // TODO could add a preference here |
| 178 |
|
| 179 |
if (isActive && shouldRestoreActiveWindowToActive) { |
| 180 |
// if the window that we are trying to restore was the active window, restore it to the active window |
| 181 |
return (WorkbenchPage) activeWindow.getActivePage(); |
| 182 |
} |
| 183 |
|
| 184 |
if (shouldRestoreUnknownWindowToActive) { |
| 185 |
// we can't find a good window, so restore it to the active one |
| 186 |
return (WorkbenchPage) activeWindow.getActivePage(); |
| 187 |
} |
| 188 |
|
| 189 |
if (shouldRestoreActiveWindowToActive && shouldRestoreUnknownWindowToActive) { |
| 190 |
StatusHandler.log(new Status(IStatus.ERROR, ContextUiPlugin.ID_PLUGIN, |
| 191 |
"Unable to find window to restore memento to.", new Exception())); |
| 192 |
} |
| 193 |
|
| 194 |
// we dont have a window that will work, so don't restore the editors |
| 195 |
// we shouldn't get here if both *WindowToActive booleans are true |
| 196 |
return null; |
| 197 |
} |
| 198 |
|
| 121 |
private String readEditorMemento(AbstractTask task) { |
199 |
private String readEditorMemento(AbstractTask task) { |
| 122 |
return preferenceStore.getString(PREFS_PREFIX + task.getHandleIdentifier()); |
200 |
return preferenceStore.getString(PREFS_PREFIX + task.getHandleIdentifier()); |
| 123 |
} |
201 |
} |
|
Lines 128-143
Link Here
|
| 128 |
ContextUiPrefContstants.AUTO_MANAGE_EDITORS)) { |
206 |
ContextUiPrefContstants.AUTO_MANAGE_EDITORS)) { |
| 129 |
closeAllButActiveTaskEditor(context.getHandleIdentifier()); |
207 |
closeAllButActiveTaskEditor(context.getHandleIdentifier()); |
| 130 |
|
208 |
|
| 131 |
XMLMemento memento = XMLMemento.createWriteRoot(KEY_CONTEXT_EDITORS); |
209 |
XMLMemento rootMemento = XMLMemento.createWriteRoot(KEY_CONTEXT_EDITORS); |
| 132 |
((WorkbenchPage) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()).getEditorManager() |
210 |
|
| 133 |
.saveState(memento); |
211 |
IWorkbenchWindow activeWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); |
|
|
212 |
IWorkbenchWindow launchingWindow = MonitorUi.getLaunchingWorkbenchWindow(); |
| 213 |
Set<IWorkbenchWindow> monitoredWindows = MonitorUi.getMonitoredWindows(); |
| 214 |
|
| 215 |
for (IWorkbenchWindow window : monitoredWindows) { |
| 216 |
IMemento memento = rootMemento.createChild(KEY_MONITORED_WINDOW_OPEN_EDITORS); |
| 217 |
|
| 218 |
memento.putString(ATTRIBUTE_CLASS, window.getClass().getCanonicalName()); |
| 219 |
int number = 0; |
| 220 |
if (window instanceof WorkbenchWindow) { |
| 221 |
number = ((WorkbenchWindow) window).getNumber(); |
| 222 |
} |
| 223 |
memento.putInteger(ATTRIBUTE_NUMER, number); |
| 224 |
memento.putBoolean(ATTRIBUTE_IS_LAUNCHING, window == launchingWindow); |
| 225 |
memento.putBoolean(ATTRIBUTE_IS_ACTIVE, window == activeWindow); |
| 226 |
((WorkbenchPage) window.getActivePage()).getEditorManager().saveState(memento); |
| 227 |
} |
| 134 |
|
228 |
|
| 135 |
AbstractTask task = TasksUiPlugin.getTaskListManager().getTaskList().getTask(context.getHandleIdentifier()); |
229 |
AbstractTask task = TasksUiPlugin.getTaskListManager().getTaskList().getTask(context.getHandleIdentifier()); |
| 136 |
if (task != null) { |
230 |
if (task != null) { |
| 137 |
// TODO: avoid storing with preferences due to bloat? |
231 |
// TODO: avoid storing with preferences due to bloat? |
| 138 |
StringWriter writer = new StringWriter(); |
232 |
StringWriter writer = new StringWriter(); |
| 139 |
try { |
233 |
try { |
| 140 |
memento.save(writer); |
234 |
rootMemento.save(writer); |
| 141 |
writeEditorMemento(task, writer.getBuffer().toString()); |
235 |
writeEditorMemento(task, writer.getBuffer().toString()); |
| 142 |
} catch (IOException e) { |
236 |
} catch (IOException e) { |
| 143 |
StatusHandler.log(new Status(IStatus.ERROR, ContextUiPlugin.ID_PLUGIN, |
237 |
StatusHandler.log(new Status(IStatus.ERROR, ContextUiPlugin.ID_PLUGIN, |
|
Lines 164-170
Link Here
|
| 164 |
XMLMemento memento = XMLMemento.createWriteRoot(KEY_CONTEXT_EDITORS); |
258 |
XMLMemento memento = XMLMemento.createWriteRoot(KEY_CONTEXT_EDITORS); |
| 165 |
|
259 |
|
| 166 |
if (task != null) { |
260 |
if (task != null) { |
| 167 |
// TODO: avoid storing with preferneces due to bloat? |
261 |
// TODO: avoid storing with preferences due to bloat? |
| 168 |
StringWriter writer = new StringWriter(); |
262 |
StringWriter writer = new StringWriter(); |
| 169 |
try { |
263 |
try { |
| 170 |
memento.save(writer); |
264 |
memento.save(writer); |
|
Lines 184-190
Link Here
|
| 184 |
* HACK: will fail to restore different parts with same name |
278 |
* HACK: will fail to restore different parts with same name |
| 185 |
*/ |
279 |
*/ |
| 186 |
@SuppressWarnings("unchecked") |
280 |
@SuppressWarnings("unchecked") |
| 187 |
private void restoreEditors(WorkbenchPage page, IMemento memento) { |
281 |
private void restoreEditors(WorkbenchPage page, IMemento memento, boolean isActiveWindow) { |
| 188 |
EditorManager editorManager = page.getEditorManager(); |
282 |
EditorManager editorManager = page.getEditorManager(); |
| 189 |
final ArrayList visibleEditors = new ArrayList(5); |
283 |
final ArrayList visibleEditors = new ArrayList(5); |
| 190 |
final IEditorReference activeEditor[] = new IEditorReference[1]; |
284 |
final IEditorReference activeEditor[] = new IEditorReference[1]; |
|
Lines 214-220
Link Here
|
| 214 |
editorManager.setVisibleEditor((IEditorReference) visibleEditors.get(i), false); |
308 |
editorManager.setVisibleEditor((IEditorReference) visibleEditors.get(i), false); |
| 215 |
} |
309 |
} |
| 216 |
|
310 |
|
| 217 |
if (activeEditor[0] != null) { |
311 |
if (activeEditor[0] != null && isActiveWindow) { |
| 218 |
IWorkbenchPart editor = activeEditor[0].getPart(true); |
312 |
IWorkbenchPart editor = activeEditor[0].getPart(true); |
| 219 |
if (editor != null) { |
313 |
if (editor != null) { |
| 220 |
page.activate(editor); |
314 |
page.activate(editor); |
|
Lines 230-236
Link Here
|
| 230 |
if (PlatformUI.getWorkbench().isClosing()) { |
324 |
if (PlatformUI.getWorkbench().isClosing()) { |
| 231 |
return; |
325 |
return; |
| 232 |
} |
326 |
} |
| 233 |
for (IWorkbenchWindow window : MonitorUiPlugin.getDefault().getMonitoredWindows()) { |
327 |
for (IWorkbenchWindow window : MonitorUi.getMonitoredWindows()) { |
| 234 |
IWorkbenchPage page = window.getActivePage(); |
328 |
IWorkbenchPage page = window.getActivePage(); |
| 235 |
if (page != null) { |
329 |
if (page != null) { |
| 236 |
IEditorReference[] references = page.getEditorReferences(); |
330 |
IEditorReference[] references = page.getEditorReferences(); |
|
Lines 265-271
Link Here
|
| 265 |
if (PlatformUI.getWorkbench().isClosing()) { |
359 |
if (PlatformUI.getWorkbench().isClosing()) { |
| 266 |
return; |
360 |
return; |
| 267 |
} |
361 |
} |
| 268 |
for (IWorkbenchWindow window : MonitorUiPlugin.getDefault().getMonitoredWindows()) { |
362 |
for (IWorkbenchWindow window : MonitorUi.getMonitoredWindows()) { |
| 269 |
IWorkbenchPage page = window.getActivePage(); |
363 |
IWorkbenchPage page = window.getActivePage(); |
| 270 |
if (page != null) { |
364 |
if (page != null) { |
| 271 |
IEditorReference[] references = page.getEditorReferences(); |
365 |
IEditorReference[] references = page.getEditorReferences(); |