thread safe plugin startup

This commit is contained in:
fros4943 2007-07-17 21:21:24 +00:00
parent 61482fec43
commit 598f4e94cd

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
* *
* $Id: SimControl.java,v 1.6 2007/04/02 16:02:32 fros4943 Exp $ * $Id: SimControl.java,v 1.7 2007/07/17 21:21:24 fros4943 Exp $
*/ */
package se.sics.cooja.plugins; package se.sics.cooja.plugins;
@ -83,40 +83,44 @@ public class SimControl extends VisPlugin {
JButton button; JButton button;
JPanel smallPanel; JPanel smallPanel;
// Register as tickobserver SwingUtilities.invokeLater(new Runnable() {
simulation.addTickObserver(tickObserver = new Observer() { public void run() {
public void update(Observable obs, Object obj) { // Register as tickobserver
if (simulation == null || simulationTime == null) simulation.addTickObserver(tickObserver = new Observer() {
return; public void update(Observable obs, Object obj) {
if (simulation == null || simulationTime == null)
// During simulation running, only update text 10 times each second return;
if (lastTextUpdateTime < System.currentTimeMillis() - 100) {
lastTextUpdateTime = System.currentTimeMillis(); // During simulation running, only update text 10 times each second
simulationTime.setText("Current simulation time: " + simulation.getSimulationTime()); if (lastTextUpdateTime < System.currentTimeMillis() - 100) {
} lastTextUpdateTime = System.currentTimeMillis();
simulationTime.setText("Current simulation time: " + simulation.getSimulationTime());
}
if (simulationStopTime > 0 && simulationStopTime <= simulation.getSimulationTime() && simulation.isRunning()) { if (simulationStopTime > 0 && simulationStopTime <= simulation.getSimulationTime() && simulation.isRunning()) {
// Time to stop simulation now // Time to stop simulation now
simulation.stopSimulation(); simulation.stopSimulation();
simulationStopTime = -1; simulationStopTime = -1;
} }
} }
}); });
// Register as simulation observer // Register as simulation observer
simulation.addObserver(simObserver = new Observer() { simulation.addObserver(simObserver = new Observer() {
public void update(Observable obs, Object obj) { public void update(Observable obs, Object obj) {
if (simulation.isRunning()) { if (simulation.isRunning()) {
startButton.setEnabled(false); startButton.setEnabled(false);
stopButton.setEnabled(true); stopButton.setEnabled(true);
} else { } else {
startButton.setEnabled(true); startButton.setEnabled(true);
stopButton.setEnabled(false); stopButton.setEnabled(false);
simulationStopTime = -1; simulationStopTime = -1;
} }
sliderDelay.setValue(convertTimeToSlide(simulation.getDelayTime())); sliderDelay.setValue(convertTimeToSlide(simulation.getDelayTime()));
simulationTime.setText("Current simulation time: " + simulation.getSimulationTime()); simulationTime.setText("Current simulation time: " + simulation.getSimulationTime());
}
});
} }
}); });