diff options
author | Christian Janz | 2013-02-11 19:53:54 +0000 |
---|---|---|
committer | Eric Moffatt | 2013-02-11 19:54:36 +0000 |
commit | e296914671cd2cffd2c1a988475d1eeddaebf30d (patch) | |
tree | 0bc2a1aa263781c0bb04e1597a10b5432b146fe2 | |
parent | 9cfab4598be188446b8e12895862a19be6dd7753 (diff) | |
download | eclipse.platform.ui-e296914671cd2cffd2c1a988475d1eeddaebf30d.tar.gz eclipse.platform.ui-e296914671cd2cffd2c1a988475d1eeddaebf30d.tar.xz eclipse.platform.ui-e296914671cd2cffd2c1a988475d1eeddaebf30d.zip |
Fix for Bug 385592 - Working Set "Window Working Set" not persistedv20130211-195436
across sessions
2 files changed, 129 insertions, 3 deletions
diff --git a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/WorkbenchPage.java b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/WorkbenchPage.java index ffcdfffa640..594538e57d9 100644 --- a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/WorkbenchPage.java +++ b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/WorkbenchPage.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2012 IBM Corporation and others. + * Copyright (c) 2000, 2013 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -7,11 +7,13 @@ * * Contributors: * IBM Corporation - initial API and implementation + * Christian Janz - <christian.janz@gmail.com> Fix for Bug 385592 *******************************************************************************/ package org.eclipse.ui.internal; import java.io.IOException; +import java.io.StringReader; import java.io.StringWriter; import java.util.ArrayList; import java.util.Arrays; @@ -27,12 +29,15 @@ import java.util.Map.Entry; import java.util.Set; import java.util.WeakHashMap; import javax.annotation.PostConstruct; +import javax.annotation.PreDestroy; import org.eclipse.core.runtime.Assert; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IAdaptable; import org.eclipse.core.runtime.IConfigurationElement; +import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.ListenerList; import org.eclipse.core.runtime.SafeRunner; +import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker; import org.eclipse.e4.core.contexts.ContextInjectionFactory; import org.eclipse.e4.core.contexts.IEclipseContext; @@ -149,6 +154,7 @@ import org.eclipse.ui.internal.util.Util; import org.eclipse.ui.model.IWorkbenchAdapter; import org.eclipse.ui.part.IShowInSource; import org.eclipse.ui.part.ShowInContext; +import org.eclipse.ui.statushandlers.StatusManager; import org.eclipse.ui.views.IStickyViewDescriptor; import org.eclipse.ui.views.IViewDescriptor; import org.osgi.service.event.Event; @@ -160,6 +166,8 @@ import org.osgi.service.event.EventHandler; public class WorkbenchPage extends CompatibleWorkbenchPage implements IWorkbenchPage { + private static final String ATT_AGGREGATE_WORKING_SET_ID = "aggregateWorkingSetId"; //$NON-NLS-1$ + class E4PartListener implements org.eclipse.e4.ui.workbench.modeling.IPartListener { public void partActivated(MPart part) { @@ -2636,6 +2644,77 @@ public class WorkbenchPage extends CompatibleWorkbenchPage implements } } + @PostConstruct + public void restoreWorkingSets() { + String workingSetName = getWindowModel().getPersistedState().get( + IWorkbenchConstants.TAG_WORKING_SET); + if (workingSetName != null) { + AbstractWorkingSetManager workingSetManager = (AbstractWorkingSetManager) getWorkbenchWindow() + .getWorkbench().getWorkingSetManager(); + setWorkingSet(workingSetManager.getWorkingSet(workingSetName)); + } + + String workingSetMemString = getWindowModel().getPersistedState().get( + IWorkbenchConstants.TAG_WORKING_SETS); + if (workingSetMemString != null) { + IMemento workingSetMem; + try { + workingSetMem = XMLMemento.createReadRoot(new StringReader(workingSetMemString)); + IMemento[] workingSetChildren = workingSetMem + .getChildren(IWorkbenchConstants.TAG_WORKING_SET); + List workingSetList = new ArrayList(workingSetChildren.length); + for (int i = 0; i < workingSetChildren.length; i++) { + IWorkingSet set = getWorkbenchWindow().getWorkbench().getWorkingSetManager() + .getWorkingSet(workingSetChildren[i].getID()); + if (set != null) { + workingSetList.add(set); + } + } + + workingSets = (IWorkingSet[]) workingSetList.toArray(new IWorkingSet[workingSetList + .size()]); + } catch (WorkbenchException e) { + StatusManager.getManager().handle( + new Status(IStatus.ERROR, PlatformUI.PLUGIN_ID, IStatus.ERROR, + WorkbenchMessages.WorkbenchPage_problemRestoringTitle, e)); + } + } + + aggregateWorkingSetId = getWindowModel().getPersistedState().get( + ATT_AGGREGATE_WORKING_SET_ID); + } + + @PreDestroy + public void saveWorkingSets() { + // Save working set if set + if (workingSet != null) { + getWindowModel().getPersistedState().put(IWorkbenchConstants.TAG_WORKING_SET, + workingSet.getName()); + } else { + getWindowModel().getPersistedState().remove(IWorkbenchConstants.TAG_WORKING_SET); + } + + XMLMemento workingSetMem = XMLMemento.createWriteRoot(IWorkbenchConstants.TAG_WORKING_SETS); + for (int i = 0; i < workingSets.length; i++) { + workingSetMem + .createChild(IWorkbenchConstants.TAG_WORKING_SET, workingSets[i].getName()); + } + StringWriter writer = new StringWriter(); + try { + workingSetMem.save(writer); + getWindowModel().getPersistedState().put(IWorkbenchConstants.TAG_WORKING_SETS, + writer.getBuffer().toString()); + } catch (IOException e) { + // Simply don't store the settings + StatusManager.getManager().handle( + new Status(IStatus.ERROR, PlatformUI.PLUGIN_ID, IStatus.ERROR, + WorkbenchMessages.SavingProblem, e)); + } + + getWindowModel().getPersistedState().put(ATT_AGGREGATE_WORKING_SET_ID, + aggregateWorkingSetId); + } + /** * Extends the perspectives within the given stack with action set * contributions from the <code>perspectiveExtensions</code> extension diff --git a/tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/api/IWorkbenchPageTest.java b/tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/api/IWorkbenchPageTest.java index 8a227d72d72..7610c2e48d7 100644 --- a/tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/api/IWorkbenchPageTest.java +++ b/tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/api/IWorkbenchPageTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2010 IBM Corporation and others. + * Copyright (c) 2000, 2013 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -7,6 +7,7 @@ * * Contributors: * IBM Corporation - initial API and implementation + * Christian Janz - <christian.janz@gmail.com> Fix for Bug 385592 *******************************************************************************/ package org.eclipse.ui.tests.api; @@ -263,7 +264,53 @@ public class IWorkbenchPageTest extends UITestCase { } /** - * Test the VIEW_VISIBLE parameter for showView, opening the view in the + * Test if the WorkingSet related settings are persisted across sessions. + */ + public void testWorkingSetsPersisted_Bug385592() { + IWorkingSetManager manager = fActivePage.getWorkbenchWindow() + .getWorkbench().getWorkingSetManager(); + + // get initial state and save it + IWorkingSet[] workingSetsBeforeSave = fActivePage.getWorkingSets(); + String aggrWorkingSetIdBeforeSave = fActivePage + .getAggregateWorkingSet().getName(); + ((WorkbenchPage) fActivePage).saveWorkingSets(); + assertNotNull(workingSetsBeforeSave); + assertNotNull(aggrWorkingSetIdBeforeSave); + assertEquals(0, workingSetsBeforeSave.length); + + IWorkingSet set1 = null; + try { + set1 = manager.createWorkingSet("w1", new IAdaptable[0]); + manager.addWorkingSet(set1); + + // change the working sets + fActivePage.setWorkingSets(new IWorkingSet[] { set1 }); + assertNotNull(fActivePage.getWorkingSets()); + assertEquals(1, fActivePage.getWorkingSets().length); + + // restore the previous state + ((WorkbenchPage) fActivePage).restoreWorkingSets(); + assertEquals(aggrWorkingSetIdBeforeSave, fActivePage + .getAggregateWorkingSet().getName()); + assertNotNull(fActivePage.getWorkingSets()); + assertEquals(workingSetsBeforeSave.length, + fActivePage.getWorkingSets().length); + + // change again, save and restore the settings + fActivePage.setWorkingSets(new IWorkingSet[] { set1 }); + ((WorkbenchPage) fActivePage).saveWorkingSets(); + ((WorkbenchPage) fActivePage).restoreWorkingSets(); + assertEquals(aggrWorkingSetIdBeforeSave, fActivePage + .getAggregateWorkingSet().getName()); + assertEquals(1, fActivePage.getWorkingSets().length); + } finally { + if (set1 != null) + manager.removeWorkingSet(set1); + } + } + + /** * Test the VIEW_VISIBLE parameter for showView, opening the view in the * stack that does not contain the active view. Ensures that the created * view is not the active part but is the top part in its stack. */ |