1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.ui.workbench;
14 import java.util.List;
16 import org.eclipse.ui.IPerspectiveDescriptor;
17 import org.eclipse.ui.IPerspectiveListener4;
18 import org.eclipse.ui.IWindowListener;
19 import org.eclipse.ui.IWorkbenchPage;
20 import org.eclipse.ui.IWorkbenchPartReference;
21 import org.eclipse.ui.IWorkbenchWindow;
22 import org.eclipse.ui.PlatformUI;
23 import org.eclipse.ui.contexts.IContextActivation;
24 import org.eclipse.ui.contexts.IContextService;
25 import org.simantics.utils.datastructures.MapList;
28 * @author Tuukka Lehtonen
30 public class PerspectiveContextActivator implements IPerspectiveListener4, IWindowListener {
32 private IWorkbenchWindow activeWindow;
34 private String oldPerspective;
36 private MapList<String, IContextActivation> activations = new MapList<String, IContextActivation>();
39 public PerspectiveContextActivator() {
40 PlatformUI.getWorkbench().addWindowListener(this);
43 public void dispose() {
44 PlatformUI.getWorkbench().removeWindowListener(this);
47 //------------------------------------------------------------------------
48 // IPerspectiveListener4
49 //------------------------------------------------------------------------
52 public void perspectivePreDeactivate(IWorkbenchPage page, IPerspectiveDescriptor perspective) {
56 public void perspectiveClosed(IWorkbenchPage page, IPerspectiveDescriptor perspective) {
60 public void perspectiveDeactivated(IWorkbenchPage page, IPerspectiveDescriptor perspective) {
64 public void perspectiveOpened(IWorkbenchPage page, IPerspectiveDescriptor perspective) {
68 public void perspectiveSavedAs(IWorkbenchPage page, IPerspectiveDescriptor oldPerspective,
69 IPerspectiveDescriptor newPerspective) {
73 public void perspectiveChanged(IWorkbenchPage page, IPerspectiveDescriptor perspective,
74 IWorkbenchPartReference partRef, String changeId) {
75 // See IWorkbenchPage.CHANGED_* constants for change id's.
79 public void perspectiveActivated(IWorkbenchPage page, IPerspectiveDescriptor perspective) {
80 activatePerspective(perspective.getId());
84 public void perspectiveChanged(IWorkbenchPage page, IPerspectiveDescriptor perspective, String changeId) {
85 // See IWorkbenchPage.CHANGED_* constants for change id's.
88 //------------------------------------------------------------------------
90 //------------------------------------------------------------------------
93 public void windowActivated(IWorkbenchWindow window) {
94 // System.out.println("attaching to window: " + window);
95 attachToWindow(window);
99 public void windowClosed(IWorkbenchWindow window) {
103 public void windowDeactivated(IWorkbenchWindow window) {
104 // System.out.println("detaching from window: " + window);
105 detachFromWindow(window);
109 public void windowOpened(IWorkbenchWindow window) {
112 //------------------------------------------------------------------------
114 //------------------------------------------------------------------------
116 private void attachToWindow(IWorkbenchWindow window) {
117 activeWindow = window;
118 window.addPerspectiveListener(this);
119 IPerspectiveDescriptor perspective = window.getActivePage().getPerspective();
120 if (perspective != null) {
121 activatePerspective(perspective.getId());
125 private void detachFromWindow(IWorkbenchWindow window) {
126 window.removePerspectiveListener(this);
129 private IContextService getService(IWorkbenchWindow window) {
130 return (IContextService) window.getWorkbench().getService(IContextService.class);
131 //return (IContextService) window.getService(IContextService.class);
134 private IContextActivation activate(String ctx) {
135 IContextService contextService = getService(activeWindow);
136 if (contextService != null && ctx != null) {
137 IContextActivation act = contextService.activateContext(ctx);
138 // System.out.println("activating context: " + act);
144 private void deactivate(IContextActivation activation) {
145 IContextService contextService = getService(activeWindow);
146 if (contextService != null && activation != null) {
147 // System.out.println("deactivating context: " + activation);
148 contextService.deactivateContext(activation);
152 private void activatePerspective(String perspectiveId) {
153 // System.out.println("activating perspective: " + perspectiveId + " (old=" + oldPerspective + ")");
155 if (oldPerspective != null) {
156 if (oldPerspective.equals(perspectiveId))
159 List<IContextActivation> acts = activations.getValues(oldPerspective);
161 activations.remove(oldPerspective);
162 for (IContextActivation act : acts) {
167 List<IPerspectiveContextExtension> exts = PerspectiveContextBindingManager.getInstance().getExtensions(perspectiveId);
168 for (IPerspectiveContextExtension ext : exts) {
169 for (String ctx : ext.getContextIds()) {
170 IContextActivation activation = activate(ctx);
171 if (activation != null) {
172 activations.add(perspectiveId, activation);
176 oldPerspective = perspectiveId;