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