--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.ui.workbench;\r
+\r
+import java.util.List;\r
+\r
+import org.eclipse.ui.IPerspectiveDescriptor;\r
+import org.eclipse.ui.IPerspectiveListener4;\r
+import org.eclipse.ui.IWindowListener;\r
+import org.eclipse.ui.IWorkbenchPage;\r
+import org.eclipse.ui.IWorkbenchPartReference;\r
+import org.eclipse.ui.IWorkbenchWindow;\r
+import org.eclipse.ui.PlatformUI;\r
+import org.eclipse.ui.contexts.IContextActivation;\r
+import org.eclipse.ui.contexts.IContextService;\r
+import org.simantics.utils.datastructures.MapList;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class PerspectiveContextActivator implements IPerspectiveListener4, IWindowListener {\r
+\r
+ private IWorkbenchWindow activeWindow;\r
+\r
+ private String oldPerspective;\r
+\r
+ private MapList<String, IContextActivation> activations = new MapList<String, IContextActivation>();\r
+\r
+\r
+ public PerspectiveContextActivator() {\r
+ PlatformUI.getWorkbench().addWindowListener(this);\r
+ }\r
+\r
+ public void dispose() {\r
+ PlatformUI.getWorkbench().removeWindowListener(this);\r
+ }\r
+\r
+ //------------------------------------------------------------------------\r
+ // IPerspectiveListener4\r
+ //------------------------------------------------------------------------\r
+\r
+ @Override\r
+ public void perspectivePreDeactivate(IWorkbenchPage page, IPerspectiveDescriptor perspective) {\r
+ }\r
+\r
+ @Override\r
+ public void perspectiveClosed(IWorkbenchPage page, IPerspectiveDescriptor perspective) {\r
+ }\r
+\r
+ @Override\r
+ public void perspectiveDeactivated(IWorkbenchPage page, IPerspectiveDescriptor perspective) {\r
+ }\r
+\r
+ @Override\r
+ public void perspectiveOpened(IWorkbenchPage page, IPerspectiveDescriptor perspective) {\r
+ }\r
+\r
+ @Override\r
+ public void perspectiveSavedAs(IWorkbenchPage page, IPerspectiveDescriptor oldPerspective,\r
+ IPerspectiveDescriptor newPerspective) {\r
+ }\r
+\r
+ @Override\r
+ public void perspectiveChanged(IWorkbenchPage page, IPerspectiveDescriptor perspective,\r
+ IWorkbenchPartReference partRef, String changeId) {\r
+ // See IWorkbenchPage.CHANGED_* constants for change id's.\r
+ }\r
+\r
+ @Override\r
+ public void perspectiveActivated(IWorkbenchPage page, IPerspectiveDescriptor perspective) {\r
+ activatePerspective(perspective.getId());\r
+ }\r
+\r
+ @Override\r
+ public void perspectiveChanged(IWorkbenchPage page, IPerspectiveDescriptor perspective, String changeId) {\r
+ // See IWorkbenchPage.CHANGED_* constants for change id's.\r
+ }\r
+\r
+ //------------------------------------------------------------------------\r
+ // IWindowListener\r
+ //------------------------------------------------------------------------\r
+\r
+ @Override\r
+ public void windowActivated(IWorkbenchWindow window) {\r
+// System.out.println("attaching to window: " + window);\r
+ attachToWindow(window);\r
+ }\r
+\r
+ @Override\r
+ public void windowClosed(IWorkbenchWindow window) {\r
+ }\r
+\r
+ @Override\r
+ public void windowDeactivated(IWorkbenchWindow window) {\r
+// System.out.println("detaching from window: " + window);\r
+ detachFromWindow(window);\r
+ }\r
+\r
+ @Override\r
+ public void windowOpened(IWorkbenchWindow window) {\r
+ }\r
+\r
+ //------------------------------------------------------------------------\r
+ // UTILITIES\r
+ //------------------------------------------------------------------------\r
+\r
+ private void attachToWindow(IWorkbenchWindow window) {\r
+ activeWindow = window;\r
+ window.addPerspectiveListener(this);\r
+ IPerspectiveDescriptor perspective = window.getActivePage().getPerspective();\r
+ if (perspective != null) {\r
+ activatePerspective(perspective.getId());\r
+ }\r
+ }\r
+\r
+ private void detachFromWindow(IWorkbenchWindow window) {\r
+ window.removePerspectiveListener(this);\r
+ }\r
+\r
+ private IContextService getService(IWorkbenchWindow window) {\r
+ return (IContextService) window.getWorkbench().getService(IContextService.class);\r
+ //return (IContextService) window.getService(IContextService.class);\r
+ }\r
+\r
+ private IContextActivation activate(String ctx) {\r
+ IContextService contextService = getService(activeWindow);\r
+ if (contextService != null && ctx != null) {\r
+ IContextActivation act = contextService.activateContext(ctx);\r
+// System.out.println("activating context: " + act);\r
+ return act;\r
+ }\r
+ return null;\r
+ }\r
+\r
+ private void deactivate(IContextActivation activation) {\r
+ IContextService contextService = getService(activeWindow);\r
+ if (contextService != null && activation != null) {\r
+// System.out.println("deactivating context: " + activation);\r
+ contextService.deactivateContext(activation);\r
+ }\r
+ }\r
+\r
+ private void activatePerspective(String perspectiveId) {\r
+// System.out.println("activating perspective: " + perspectiveId + " (old=" + oldPerspective + ")");\r
+\r
+ if (oldPerspective != null) {\r
+ if (oldPerspective.equals(perspectiveId))\r
+ return;\r
+\r
+ List<IContextActivation> acts = activations.getValues(oldPerspective);\r
+ if (acts != null) {\r
+ activations.remove(oldPerspective);\r
+ for (IContextActivation act : acts) {\r
+ deactivate(act);\r
+ }\r
+ }\r
+ }\r
+ List<IPerspectiveContextExtension> exts = PerspectiveContextBindingManager.getInstance().getExtensions(perspectiveId);\r
+ for (IPerspectiveContextExtension ext : exts) {\r
+ for (String ctx : ext.getContextIds()) {\r
+ IContextActivation activation = activate(ctx);\r
+ if (activation != null) {\r
+ activations.add(perspectiveId, activation);\r
+ }\r
+ }\r
+ }\r
+ oldPerspective = perspectiveId;\r
+ }\r
+\r
+}\r