]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
218512cd282b5f2df59b19ac6e7c4f84292a8b83
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2012 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.sysdyn.ui.handlers.game;\r
13 \r
14 import java.util.Map;\r
15 \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
26 \r
27 /**\r
28  * Handler for initializing and reloading game experiments.\r
29  * \r
30  * @author Teemu Lempinen\r
31  *\r
32  */\r
33 public class ReloadGameExperimentHandler extends RunBasicExperiment {\r
34         \r
35         private boolean started = false;\r
36         private boolean initialized = false;\r
37         \r
38         \r
39     public static final String COMMAND = "org.simantics.sysdyn.ui.reloadGame";\r
40     \r
41     @Override\r
42     public Object execute(ExecutionEvent event) throws ExecutionException {\r
43         SysdynGameExperimentBase game = getGameExperiment();\r
44         if(game != null)\r
45             game.simulate(true);\r
46         return null;\r
47     }\r
48         \r
49     /**\r
50      * Find currently active game experiment\r
51      * @return Currently active game experiment or null if game experiment not active\r
52      */\r
53     private SysdynGameExperimentBase getGameExperiment() {\r
54         // Find active experiment\r
55         IProject project = SimanticsUI.peekProject();\r
56         if (project == null)\r
57             return null;\r
58         \r
59         IExperimentManager manager = project.getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);\r
60 \r
61         IExperiment active = manager.getActiveExperiment();\r
62         if (!(active instanceof SysdynGameExperimentBase))\r
63             return null;\r
64         \r
65         return (SysdynGameExperimentBase) active;\r
66     }\r
67         \r
68         @SuppressWarnings("rawtypes")\r
69         @Override\r
70         public void updateElement(UIElement element, Map parameters) {\r
71                 // Disable button while initializign a model\r
72                 super.updateElement(element, parameters);\r
73                 \r
74                 \r
75                 // Change tooltip according to the current status\r
76                 SysdynGameExperimentBase game = getGameExperiment();   \r
77 \r
78         if(game == null) {\r
79                         started = false;\r
80                         initialized = false;\r
81                         return;\r
82         }\r
83         \r
84         ExperimentState state = game.getState();\r
85         \r
86                 if(state==ExperimentState.INITIALIZING) {\r
87                         started = false;\r
88                         initialized = false;\r
89                 } else if(state==ExperimentState.RUNNING) {\r
90                         started = true;\r
91                         initialized = false;\r
92                 } else if(state==ExperimentState.STOPPED) {\r
93                         if(started && !initialized) {\r
94                                 initialized = true;\r
95                         } \r
96                 }\r
97                 \r
98         \r
99         if(initialized) {\r
100             element.setTooltip("Reload Game");\r
101         } else {\r
102             element.setTooltip("Initialize Game");\r
103         }\r
104         }\r
105 \r
106 }\r