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