1 /*******************************************************************************
2 * Copyright (c) 2007, 2019 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 * Semantum Oy - gitlab #384
12 *******************************************************************************/
13 package org.simantics.simulation.ui;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.HashSet;
20 import org.eclipse.ui.IWorkbench;
21 import org.eclipse.ui.PlatformUI;
22 import org.eclipse.ui.contexts.IContextActivation;
23 import org.eclipse.ui.contexts.IContextService;
24 import org.simantics.simulation.experiment.IDynamicExperiment;
25 import org.simantics.simulation.experiment.IExperiment;
26 import org.simantics.simulation.project.IExperimentManager;
27 import org.simantics.simulation.project.IExperimentManagerListener;
29 public class ExperimentManagerListener implements IExperimentManagerListener {
31 public static final String EXPERIMENT_CONTEXT = "org.simantics.simulation.ui.experiment";
32 public static final String DYNAMIC_EXPERIMENT_CONTEXT = "org.simantics.simulation.ui.dynamicExperiment";
34 static Set<IExperimentManager> managers =
35 new HashSet<IExperimentManager>();
37 IExperimentManager manager;
39 Collection<IContextActivation> contextActivations =
40 new ArrayList<IContextActivation>();
42 public ExperimentManagerListener(IExperimentManager manager) {
43 this.manager = manager;
46 public static void listenManager(IExperimentManager manager) {
47 synchronized(managers) {
48 if(managers.contains(manager))
50 ExperimentManagerListener listener =
51 new ExperimentManagerListener(manager);
52 manager.addListener(listener);
53 managers.add(manager);
58 public void activeExperimentLoaded(final IExperiment experiment) {
59 if (!PlatformUI.isWorkbenchRunning())
62 experiment.addListener(new org.simantics.simulation.ui.e4.ExperimentListener());
64 final IWorkbench workbench = PlatformUI.getWorkbench();
65 workbench.getDisplay().asyncExec(new Runnable() {
68 if (workbench.isClosing())
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));
83 public void activeExperimentUnloaded() {
84 synchronized(contextActivations) {
85 final Collection<IContextActivation> oldContextActivations =
88 contextActivations = new ArrayList<IContextActivation>();
90 if (!PlatformUI.isWorkbenchRunning())
93 final IWorkbench workbench = PlatformUI.getWorkbench();
94 workbench.getDisplay().asyncExec(new Runnable() {
97 if (workbench.isClosing())
100 IContextService contextService =
101 (IContextService)workbench.getService(IContextService.class);
102 contextService.deactivateContexts(oldContextActivations);
109 public void managerDisposed() {
110 synchronized(managers) {
111 manager.removeListener(this);
112 managers.remove(manager);