]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/workbench/PerspectiveContextActivator.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / PerspectiveContextActivator.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.ui.workbench;
13
14 import java.util.List;
15
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;
26
27 /**
28  * @author Tuukka Lehtonen
29  */
30 public class PerspectiveContextActivator implements IPerspectiveListener4, IWindowListener {
31
32     private IWorkbenchWindow                    activeWindow;
33
34     private String                              oldPerspective;
35
36     private MapList<String, IContextActivation> activations = new MapList<String, IContextActivation>();
37
38
39     public PerspectiveContextActivator() {
40         PlatformUI.getWorkbench().addWindowListener(this);
41     }
42
43     public void dispose() {
44         PlatformUI.getWorkbench().removeWindowListener(this);
45     }
46
47     //------------------------------------------------------------------------
48     // IPerspectiveListener4
49     //------------------------------------------------------------------------
50
51     @Override
52     public void perspectivePreDeactivate(IWorkbenchPage page, IPerspectiveDescriptor perspective) {
53     }
54
55     @Override
56     public void perspectiveClosed(IWorkbenchPage page, IPerspectiveDescriptor perspective) {
57     }
58
59     @Override
60     public void perspectiveDeactivated(IWorkbenchPage page, IPerspectiveDescriptor perspective) {
61     }
62
63     @Override
64     public void perspectiveOpened(IWorkbenchPage page, IPerspectiveDescriptor perspective) {
65     }
66
67     @Override
68     public void perspectiveSavedAs(IWorkbenchPage page, IPerspectiveDescriptor oldPerspective,
69             IPerspectiveDescriptor newPerspective) {
70     }
71
72     @Override
73     public void perspectiveChanged(IWorkbenchPage page, IPerspectiveDescriptor perspective,
74             IWorkbenchPartReference partRef, String changeId) {
75         // See IWorkbenchPage.CHANGED_* constants for change id's.
76     }
77
78     @Override
79     public void perspectiveActivated(IWorkbenchPage page, IPerspectiveDescriptor perspective) {
80         activatePerspective(perspective.getId());
81     }
82
83     @Override
84     public void perspectiveChanged(IWorkbenchPage page, IPerspectiveDescriptor perspective, String changeId) {
85         // See IWorkbenchPage.CHANGED_* constants for change id's.
86     }
87
88     //------------------------------------------------------------------------
89     // IWindowListener
90     //------------------------------------------------------------------------
91
92     @Override
93     public void windowActivated(IWorkbenchWindow window) {
94 //        System.out.println("attaching to window: " + window);
95         attachToWindow(window);
96     }
97
98     @Override
99     public void windowClosed(IWorkbenchWindow window) {
100     }
101
102     @Override
103     public void windowDeactivated(IWorkbenchWindow window) {
104 //        System.out.println("detaching from window: " + window);
105         detachFromWindow(window);
106     }
107
108     @Override
109     public void windowOpened(IWorkbenchWindow window) {
110     }
111
112     //------------------------------------------------------------------------
113     // UTILITIES
114     //------------------------------------------------------------------------
115
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());
122         }
123     }
124
125     private void detachFromWindow(IWorkbenchWindow window) {
126         window.removePerspectiveListener(this);
127     }
128
129     private IContextService getService(IWorkbenchWindow window) {
130         return (IContextService) window.getWorkbench().getService(IContextService.class);
131         //return (IContextService) window.getService(IContextService.class);
132     }
133
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);
139             return act;
140         }
141         return null;
142     }
143
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);
149         }
150     }
151
152     private void activatePerspective(String perspectiveId) {
153 //        System.out.println("activating perspective: " + perspectiveId + " (old=" + oldPerspective + ")");
154
155         if (oldPerspective != null) {
156             if (oldPerspective.equals(perspectiveId))
157                 return;
158
159             List<IContextActivation> acts = activations.getValues(oldPerspective);
160             if (acts != null) {
161                 activations.remove(oldPerspective);
162                 for (IContextActivation act : acts) {
163                     deactivate(act);
164                 }
165             }
166         }
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);
173                 }
174             }
175         }
176         oldPerspective = perspectiveId;
177     }
178
179 }