]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.simulation/src/org/simantics/simulation/project/CountingExperimentActivationListener.java
Added new ExperimentRuns.createRun utility
[simantics/platform.git] / bundles / org.simantics.simulation / src / org / simantics / simulation / project / CountingExperimentActivationListener.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.project;
13
14 import java.util.concurrent.atomic.AtomicBoolean;
15 import java.util.concurrent.atomic.AtomicInteger;
16
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.core.runtime.IStatus;
19 import org.simantics.simulation.Activator;
20 import org.simantics.simulation.experiment.IExperiment;
21
22 /**
23  * @author Tuukka Lehtonen
24  */
25 public class CountingExperimentActivationListener implements IExperimentActivationListener {
26
27     IExperimentActivationListener proxy;
28     AtomicInteger                 activationCounter;
29     IProgressMonitor              monitor;
30     AtomicBoolean                 excepted = new AtomicBoolean(false);
31
32     public CountingExperimentActivationListener(IExperimentActivationListener proxy, int expectedActivations) {
33         this(proxy, expectedActivations, null);
34     }
35
36     public CountingExperimentActivationListener(IExperimentActivationListener proxy, int expectedActivations, IProgressMonitor monitor) {
37         this.proxy = proxy;
38         this.activationCounter = new AtomicInteger(expectedActivations);
39         this.monitor = monitor;
40     }
41
42     @Override
43     public void onMessage(IStatus message) {
44         proxy.onMessage(message);
45     }
46
47     @Override
48     public void onExperimentActivated(IExperiment experiment) {
49         if (activationCounter.decrementAndGet() == 0) {
50             if (!excepted.get())
51                 proxy.onExperimentActivated(experiment);
52         }
53     }
54
55     @Override
56     public void onFailure(Throwable e) {
57         if (excepted.compareAndSet(false, true))
58             proxy.onFailure(e);
59         else
60             Activator.logError(e.getMessage(), e);
61     }
62
63     @Override
64     public IProgressMonitor getProgressMonitor() {
65         return monitor != null ? monitor : proxy.getProgressMonitor();
66     }
67
68 }