1 /*******************************************************************************
\r
2 * Copyright (c) 2010 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.SysdynPlaybackExperiment;
\r
29 public class SysdynExperimentManagerListener implements IExperimentManagerListener{
\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
34 static Set<IExperimentManager> managers =
\r
35 new HashSet<IExperimentManager>();
\r
37 IExperimentManager manager;
\r
39 Collection<IContextActivation> contextActivations =
\r
40 new ArrayList<IContextActivation>();
\r
42 public SysdynExperimentManagerListener(IExperimentManager manager) {
\r
43 this.manager = manager;
\r
46 public static void listenManager(IExperimentManager manager) {
\r
47 synchronized(managers) {
\r
48 if(managers.contains(manager))
\r
50 SysdynExperimentManagerListener listener =
\r
51 new SysdynExperimentManagerListener(manager);
\r
52 manager.addListener(listener);
\r
53 managers.add(manager);
\r
58 public void activeExperimentLoaded(final IExperiment experiment) {
\r
59 experiment.addListener(new SysdynExperimentListener());
\r
61 PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
\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
82 public void activeExperimentUnloaded() {
\r
84 synchronized(contextActivations) {
\r
85 final Collection<IContextActivation> oldContextActivations =
\r
88 contextActivations = new ArrayList<IContextActivation>();
\r
90 final IWorkbench workbench = PlatformUI.getWorkbench();
\r
91 workbench.getDisplay().asyncExec(new Runnable() {
\r
95 if (workbench.isClosing())
\r
98 IContextService contextService =
\r
99 (IContextService)workbench.getActiveWorkbenchWindow().getService(IContextService.class);
\r
100 contextService.deactivateContexts(oldContextActivations);
\r
109 public void managerDisposed() {
\r
110 synchronized(managers) {
\r
111 managers.remove(manager);
\r