]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
98b8f65a6f39d6995ae684e194a692c9e2445a08
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2010 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.SysdynPlaybackExperiment;\r
28 \r
29 public class SysdynExperimentManagerListener implements IExperimentManagerListener{\r
30 \r
31     public static final String BASIC_EXPERIMENT_CONTEXT = "org.simantics.sysdyn.ui.basicExperiment";\r
32     public static final String PLAYBACK_EXPERIMENT_CONTEXT = "org.simantics.sysdyn.ui.playbackExperiment";\r
33    \r
34     static Set<IExperimentManager> managers = \r
35         new HashSet<IExperimentManager>();\r
36     \r
37     IExperimentManager manager;\r
38     \r
39     Collection<IContextActivation> contextActivations = \r
40         new ArrayList<IContextActivation>();\r
41         \r
42     public SysdynExperimentManagerListener(IExperimentManager manager) {\r
43         this.manager = manager;\r
44     }\r
45 \r
46     public static void listenManager(IExperimentManager manager) {\r
47         synchronized(managers) {\r
48             if(managers.contains(manager))\r
49                 return;\r
50             SysdynExperimentManagerListener listener = \r
51                 new SysdynExperimentManagerListener(manager);\r
52             manager.addListener(listener);\r
53             managers.add(manager);\r
54         }\r
55     }\r
56     \r
57     @Override\r
58     public void activeExperimentLoaded(final IExperiment experiment) {        \r
59         experiment.addListener(new SysdynExperimentListener());\r
60 \r
61         PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {\r
62 \r
63             @Override\r
64             public void run() {\r
65                 IContextService contextService =\r
66                     (IContextService)PlatformUI.getWorkbench()\r
67                     .getActiveWorkbenchWindow().getService(IContextService.class);\r
68                 synchronized(contextActivations) {\r
69                     if(experiment instanceof SysdynPlaybackExperiment) {\r
70                         contextActivations.add(contextService.activateContext(PLAYBACK_EXPERIMENT_CONTEXT));\r
71                         experiment.addListener(new SysdynPlaybackExperimentListener((SysdynPlaybackExperiment)experiment));\r
72                     } else if(experiment instanceof SysdynExperiment) {\r
73                         contextActivations.add(contextService.activateContext(BASIC_EXPERIMENT_CONTEXT));\r
74                     }\r
75                 }\r
76             }\r
77             \r
78         });\r
79     }\r
80 \r
81     @Override\r
82     public void activeExperimentUnloaded() {\r
83         \r
84         synchronized(contextActivations) {\r
85             final Collection<IContextActivation> oldContextActivations = \r
86                 contextActivations;\r
87             \r
88             contextActivations = new ArrayList<IContextActivation>();\r
89             \r
90             final IWorkbench workbench = PlatformUI.getWorkbench();\r
91             workbench.getDisplay().asyncExec(new Runnable() {\r
92     \r
93                 @Override\r
94                 public void run() {\r
95                     if (workbench.isClosing())\r
96                         return;\r
97                     \r
98                     IContextService contextService =\r
99                         (IContextService)workbench.getActiveWorkbenchWindow().getService(IContextService.class);\r
100                     contextService.deactivateContexts(oldContextActivations);                                \r
101                 }\r
102                 \r
103             });\r
104         }\r
105             \r
106     }\r
107 \r
108     @Override\r
109     public void managerDisposed() {\r
110         synchronized(managers) {\r
111             managers.remove(manager);\r
112         }\r
113     }\r
114 \r
115 }\r