X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.simulation.ui%2Fsrc%2Forg%2Fsimantics%2Fsimulation%2Fui%2FExperimentManagerListener.java;fp=bundles%2Forg.simantics.simulation.ui%2Fsrc%2Forg%2Fsimantics%2Fsimulation%2Fui%2FExperimentManagerListener.java;h=504e81ccb00f1d135d5f3a5449874a51076b5153;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.simulation.ui/src/org/simantics/simulation/ui/ExperimentManagerListener.java b/bundles/org.simantics.simulation.ui/src/org/simantics/simulation/ui/ExperimentManagerListener.java new file mode 100644 index 000000000..504e81ccb --- /dev/null +++ b/bundles/org.simantics.simulation.ui/src/org/simantics/simulation/ui/ExperimentManagerListener.java @@ -0,0 +1,116 @@ +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.simulation.ui; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import java.util.Set; + +import org.eclipse.ui.IWorkbench; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.contexts.IContextActivation; +import org.eclipse.ui.contexts.IContextService; +import org.simantics.simulation.experiment.IDynamicExperiment; +import org.simantics.simulation.experiment.IExperiment; +import org.simantics.simulation.project.IExperimentManager; +import org.simantics.simulation.project.IExperimentManagerListener; + +public class ExperimentManagerListener implements IExperimentManagerListener { + + public static final String EXPERIMENT_CONTEXT = "org.simantics.simulation.ui.experiment"; + public static final String DYNAMIC_EXPERIMENT_CONTEXT = "org.simantics.simulation.ui.dynamicExperiment"; + + static Set managers = + new HashSet(); + + IExperimentManager manager; + + Collection contextActivations = + new ArrayList(); + + public ExperimentManagerListener(IExperimentManager manager) { + this.manager = manager; + } + + public static void listenManager(IExperimentManager manager) { + synchronized(managers) { + if(managers.contains(manager)) + return; + ExperimentManagerListener listener = + new ExperimentManagerListener(manager); + manager.addListener(listener); + managers.add(manager); + } + } + + @Override + public void activeExperimentLoaded(final IExperiment experiment) { +// experiment.addListener(new ExperimentListener()); + experiment.addListener(new org.simantics.simulation.ui.e4.ExperimentListener()); + + if (!PlatformUI.isWorkbenchRunning()) + return; + + final IWorkbench workbench = PlatformUI.getWorkbench(); + workbench.getDisplay().asyncExec(new Runnable() { + @Override + public void run() { + if (workbench.isClosing()) + return; + IContextService contextService = + (IContextService)PlatformUI.getWorkbench() + .getService(IContextService.class); + synchronized(contextActivations) { + contextActivations.add(contextService.activateContext(EXPERIMENT_CONTEXT)); + if(experiment instanceof IDynamicExperiment) + contextActivations.add(contextService.activateContext(DYNAMIC_EXPERIMENT_CONTEXT)); + } + } + }); + } + + @Override + public void activeExperimentUnloaded() { + synchronized(contextActivations) { + final Collection oldContextActivations = + contextActivations; + + contextActivations = new ArrayList(); + + if (!PlatformUI.isWorkbenchRunning()) + return; + + final IWorkbench workbench = PlatformUI.getWorkbench(); + workbench.getDisplay().asyncExec(new Runnable() { + @Override + public void run() { + if (workbench.isClosing()) + return; + + IContextService contextService = + (IContextService)workbench.getService(IContextService.class); + contextService.deactivateContexts(oldContextActivations); + } + }); + } + } + + @Override + public void managerDisposed() { + synchronized(managers) { + manager.removeListener(this); + managers.remove(manager); + } + } + +}