]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.simulation.ui/src/org/simantics/simulation/ui/ExperimentManagerListener.java
402ad6fdb9734f4f29a960883a189b3f1d4ffa2e
[simantics/platform.git] / bundles / org.simantics.simulation.ui / src / org / simantics / simulation / ui / ExperimentManagerListener.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.simulation.ui;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.HashSet;
17 import java.util.Set;
18
19 import org.eclipse.ui.IWorkbench;
20 import org.eclipse.ui.PlatformUI;
21 import org.eclipse.ui.contexts.IContextActivation;
22 import org.eclipse.ui.contexts.IContextService;
23 import org.simantics.simulation.experiment.IDynamicExperiment;
24 import org.simantics.simulation.experiment.IExperiment;
25 import org.simantics.simulation.project.IExperimentManager;
26 import org.simantics.simulation.project.IExperimentManagerListener;
27
28 public class ExperimentManagerListener implements IExperimentManagerListener {
29
30     public static final String     EXPERIMENT_CONTEXT         = "org.simantics.simulation.ui.experiment";
31     public static final String     DYNAMIC_EXPERIMENT_CONTEXT = "org.simantics.simulation.ui.dynamicExperiment";
32
33     static Set<IExperimentManager> managers = 
34         new HashSet<IExperimentManager>();
35
36     IExperimentManager manager;
37
38     Collection<IContextActivation> contextActivations = 
39         new ArrayList<IContextActivation>();
40
41     public ExperimentManagerListener(IExperimentManager manager) {
42         this.manager = manager;
43     }
44
45     public static void listenManager(IExperimentManager manager) {
46         synchronized(managers) {
47             if(managers.contains(manager))
48                 return;
49             ExperimentManagerListener listener = 
50                 new ExperimentManagerListener(manager);
51             manager.addListener(listener);
52             managers.add(manager);
53         }
54     }
55
56     @Override
57     public void activeExperimentLoaded(final IExperiment experiment) {
58 //        experiment.addListener(new ExperimentListener());
59         experiment.addListener(new org.simantics.simulation.ui.e4.ExperimentListener());
60
61         if (!PlatformUI.isWorkbenchRunning())
62             return;
63
64         final IWorkbench workbench = PlatformUI.getWorkbench();
65         workbench.getDisplay().asyncExec(new Runnable() {
66             @Override
67             public void run() {
68                 if (workbench.isClosing())
69                     return;
70                 IContextService contextService =
71                     (IContextService)PlatformUI.getWorkbench()
72                     .getService(IContextService.class);
73                 synchronized(contextActivations) {
74                     contextActivations.add(contextService.activateContext(EXPERIMENT_CONTEXT));
75                     if(experiment instanceof IDynamicExperiment)
76                         contextActivations.add(contextService.activateContext(DYNAMIC_EXPERIMENT_CONTEXT));
77                 }
78             }
79         });
80     }
81
82     @Override
83     public void activeExperimentUnloaded() {
84         synchronized(contextActivations) {
85             final Collection<IContextActivation> oldContextActivations = 
86                 contextActivations;
87
88             contextActivations = new ArrayList<IContextActivation>();
89
90             if (!PlatformUI.isWorkbenchRunning())
91                 return;
92
93             final IWorkbench workbench = PlatformUI.getWorkbench();
94             workbench.getDisplay().asyncExec(new Runnable() {
95                 @Override
96                 public void run() {
97                     if (workbench.isClosing())
98                         return;
99
100                     IContextService contextService =
101                         (IContextService)workbench.getService(IContextService.class);
102                     contextService.deactivateContexts(oldContextActivations);
103                 }
104             });
105         }
106     }
107
108     @Override
109     public void managerDisposed() {
110         synchronized(managers) {
111             manager.removeListener(this);
112             managers.remove(manager);
113         }
114     }
115
116 }