]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.server/src/org/simantics/document/server/ExperimentStoppedListener.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.document.server / src / org / simantics / document / server / ExperimentStoppedListener.java
1 /*******************************************************************************
2  * Copyright (c) 2013 Association for Decentralized Information Management in
3  * 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.document.server;
13
14 import org.simantics.document.server.serverResponse.DelayedResponse;
15 import org.simantics.simulation.experiment.ExperimentState;
16 import org.simantics.simulation.experiment.IExperiment;
17 import org.simantics.simulation.experiment.IExperimentListener;
18
19 /**
20  * Listener for listening experiment stopped events. Listener removes itself 
21  * when experiment has first been run and then stopped and notifies response.
22  * 
23  * @author Teemu Lempinen
24  *
25  */
26 public class ExperimentStoppedListener implements IExperimentListener {
27
28     private boolean hasRun = false;
29     private DelayedResponse response;
30     private IExperiment experiment;
31
32     public ExperimentStoppedListener(IExperiment experiment, DelayedResponse response) {
33         this.experiment = experiment;
34         this.response = response;
35     }
36
37     @Override
38     public void stateChanged(ExperimentState state) {
39         if(state.equals(ExperimentState.RUNNING))
40             hasRun = true;
41
42         if(state.equals(ExperimentState.STOPPED) && hasRun) {
43             response.setHasRun();
44             experiment.removeListener(this);
45             synchronized(response) {
46                 response.notify();
47             }
48         }
49     }
50 }