1 /*******************************************************************************
\r
2 * Copyright (c) 2010, 2012 Association for Decentralized Information Management in
\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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.sysdyn.ui.listeners;
\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
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
31 public class SysdynExperimentManagerListener implements IExperimentManagerListener{
\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
38 static Set<IExperimentManager> managers =
\r
39 new HashSet<IExperimentManager>();
\r
41 IExperimentManager manager;
\r
43 Collection<IContextActivation> contextActivations =
\r
44 new ArrayList<IContextActivation>();
\r
46 public SysdynExperimentManagerListener(IExperimentManager manager) {
\r
47 this.manager = manager;
\r
50 public static void listenManager(IExperimentManager manager) {
\r
51 synchronized(managers) {
\r
52 if(managers.contains(manager))
\r
54 SysdynExperimentManagerListener listener =
\r
55 new SysdynExperimentManagerListener(manager);
\r
56 manager.addListener(listener);
\r
57 managers.add(manager);
\r
62 public void activeExperimentLoaded(final IExperiment experiment) {
\r
63 experiment.addListener(new SysdynExperimentListener());
\r
65 PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
\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
92 public void activeExperimentUnloaded() {
\r
94 synchronized(contextActivations) {
\r
95 final Collection<IContextActivation> oldContextActivations =
\r
98 contextActivations = new ArrayList<IContextActivation>();
\r
100 final IWorkbench workbench = PlatformUI.getWorkbench();
\r
101 workbench.getDisplay().asyncExec(new Runnable() {
\r
104 public void run() {
\r
105 if (workbench.isClosing())
\r
108 IContextService contextService =
\r
109 (IContextService)workbench.getActiveWorkbenchWindow().getService(IContextService.class);
\r
110 contextService.deactivateContexts(oldContextActivations);
\r
119 public void managerDisposed() {
\r
120 synchronized(managers) {
\r
121 managers.remove(manager);
\r