]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
d9c50e813e291796c9a38d658d492ac4126b705b
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2010, 2012 Association for Decentralized Information Management in\r
3  * 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.sysdyn.ui.listeners;\r
13 \r
14 import java.util.ArrayList;\r
15 import java.util.Collection;\r
16 import java.util.HashSet;\r
17 import java.util.Set;\r
18 \r
19 import org.eclipse.ui.IWorkbench;\r
20 import org.eclipse.ui.PlatformUI;\r
21 import org.eclipse.ui.contexts.IContextActivation;\r
22 import org.eclipse.ui.contexts.IContextService;\r
23 import org.simantics.simulation.experiment.IExperiment;\r
24 import org.simantics.simulation.project.IExperimentManager;\r
25 import org.simantics.simulation.project.IExperimentManagerListener;\r
26 import org.simantics.sysdyn.manager.SysdynExperiment;\r
27 import org.simantics.sysdyn.manager.SysdynGameExperiment;\r
28 import org.simantics.sysdyn.manager.SysdynPlaybackExperiment;\r
29 import org.simantics.sysdyn.manager.SysdynSensitivityAnalysisExperiment;\r
30 \r
31 public class SysdynExperimentManagerListener implements IExperimentManagerListener{\r
32 \r
33     public static final String BASIC_EXPERIMENT_CONTEXT = "org.simantics.sysdyn.ui.basicExperiment";\r
34     public static final String PLAYBACK_EXPERIMENT_CONTEXT = "org.simantics.sysdyn.ui.playbackExperiment";\r
35     public static final String GAME_EXPERIMENT_CONTEXT = "org.simantics.sysdyn.ui.gameExperiment";\r
36     public static final String SENSITIVITY_ANALYSIS_EXPERIMENT_CONTEXT = "org.simantics.sysdyn.ui.sensitivityAnalysisExperiment";\r
37     \r
38     static Set<IExperimentManager> managers = \r
39         new HashSet<IExperimentManager>();\r
40     \r
41     IExperimentManager manager;\r
42     \r
43     Collection<IContextActivation> contextActivations = \r
44         new ArrayList<IContextActivation>();\r
45         \r
46     public SysdynExperimentManagerListener(IExperimentManager manager) {\r
47         this.manager = manager;\r
48     }\r
49 \r
50     public static void listenManager(IExperimentManager manager) {\r
51         synchronized(managers) {\r
52             if(managers.contains(manager))\r
53                 return;\r
54             SysdynExperimentManagerListener listener = \r
55                 new SysdynExperimentManagerListener(manager);\r
56             manager.addListener(listener);\r
57             managers.add(manager);\r
58         }\r
59     }\r
60     \r
61     @Override\r
62     public void activeExperimentLoaded(final IExperiment experiment) {        \r
63         experiment.addListener(new SysdynExperimentListener());\r
64 \r
65         PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {\r
66 \r
67             @Override\r
68             public void run() {\r
69                 IContextService contextService =\r
70                     (IContextService)PlatformUI.getWorkbench()\r
71                     .getActiveWorkbenchWindow().getService(IContextService.class);\r
72                 synchronized(contextActivations) {\r
73                     if(experiment instanceof SysdynPlaybackExperiment) {\r
74                         contextActivations.add(contextService.activateContext(PLAYBACK_EXPERIMENT_CONTEXT));\r
75                         experiment.addListener(new SysdynPlaybackExperimentListener((SysdynPlaybackExperiment)experiment));\r
76                     } else if(experiment instanceof SysdynGameExperiment) {\r
77                         contextActivations.add(contextService.activateContext(GAME_EXPERIMENT_CONTEXT));\r
78                         // TODO: some listener?\r
79                     } else if(experiment instanceof SysdynSensitivityAnalysisExperiment) {\r
80                         contextActivations.add(contextService.activateContext(SENSITIVITY_ANALYSIS_EXPERIMENT_CONTEXT));\r
81                         // TODO: some listener?\r
82                     } else if(experiment instanceof SysdynExperiment) {\r
83                         contextActivations.add(contextService.activateContext(BASIC_EXPERIMENT_CONTEXT));\r
84                     }\r
85                 }\r
86             }\r
87             \r
88         });\r
89     }\r
90 \r
91     @Override\r
92     public void activeExperimentUnloaded() {\r
93         \r
94         synchronized(contextActivations) {\r
95             final Collection<IContextActivation> oldContextActivations = \r
96                 contextActivations;\r
97             \r
98             contextActivations = new ArrayList<IContextActivation>();\r
99             \r
100             final IWorkbench workbench = PlatformUI.getWorkbench();\r
101             workbench.getDisplay().asyncExec(new Runnable() {\r
102     \r
103                 @Override\r
104                 public void run() {\r
105                     if (workbench.isClosing())\r
106                         return;\r
107                     \r
108                     IContextService contextService =\r
109                         (IContextService)workbench.getActiveWorkbenchWindow().getService(IContextService.class);\r
110                     contextService.deactivateContexts(oldContextActivations);                                \r
111                 }\r
112                 \r
113             });\r
114         }\r
115             \r
116     }\r
117 \r
118     @Override\r
119     public void managerDisposed() {\r
120         synchronized(managers) {\r
121             managers.remove(manager);\r
122         }\r
123     }\r
124 \r
125 }\r