1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2012 Association for Decentralized Information Management in
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.sysdyn.ui.handlers.game;
\r
14 import java.util.Map;
\r
16 import org.eclipse.core.commands.ExecutionEvent;
\r
17 import org.eclipse.core.commands.ExecutionException;
\r
18 import org.eclipse.ui.menus.UIElement;
\r
19 import org.simantics.project.IProject;
\r
20 import org.simantics.simulation.experiment.ExperimentState;
\r
21 import org.simantics.simulation.experiment.IExperiment;
\r
22 import org.simantics.simulation.project.IExperimentManager;
\r
23 import org.simantics.sysdyn.manager.SysdynGameExperimentBase;
\r
24 import org.simantics.sysdyn.ui.handlers.RunBasicExperiment;
\r
25 import org.simantics.ui.SimanticsUI;
\r
28 * Handler for initializing and reloading game experiments.
\r
30 * @author Teemu Lempinen
\r
33 public class ReloadGameExperimentHandler extends RunBasicExperiment {
\r
35 private boolean started = false;
\r
36 private boolean initialized = false;
\r
39 public static final String COMMAND = "org.simantics.sysdyn.ui.reloadGame";
\r
42 public Object execute(ExecutionEvent event) throws ExecutionException {
\r
43 SysdynGameExperimentBase game = getGameExperiment();
\r
45 game.simulate(true);
\r
50 * Find currently active game experiment
\r
51 * @return Currently active game experiment or null if game experiment not active
\r
53 private SysdynGameExperimentBase getGameExperiment() {
\r
54 // Find active experiment
\r
55 IProject project = SimanticsUI.peekProject();
\r
56 if (project == null)
\r
59 IExperimentManager manager = project.getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);
\r
61 IExperiment active = manager.getActiveExperiment();
\r
62 if (!(active instanceof SysdynGameExperimentBase))
\r
65 return (SysdynGameExperimentBase) active;
\r
68 @SuppressWarnings("rawtypes")
\r
70 public void updateElement(UIElement element, Map parameters) {
\r
71 // Disable button while initializign a model
\r
72 super.updateElement(element, parameters);
\r
75 // Change tooltip according to the current status
\r
76 SysdynGameExperimentBase game = getGameExperiment();
\r
80 initialized = false;
\r
84 ExperimentState state = game.getState();
\r
86 if(state==ExperimentState.INITIALIZING) {
\r
88 initialized = false;
\r
89 } else if(state==ExperimentState.RUNNING) {
\r
91 initialized = false;
\r
92 } else if(state==ExperimentState.STOPPED) {
\r
93 if(started && !initialized) {
\r
100 element.setTooltip("Reload Game");
\r
102 element.setTooltip("Initialize Game");
\r