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