]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Functions for enabling/disabling simulation control buttons (refs #3511)
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Wed, 4 Jul 2012 12:18:19 +0000 (12:18 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Wed, 4 Jul 2012 12:18:19 +0000 (12:18 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@25274 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynExperiment.java

index 1c1852b1c01a3f553f62e17ef42295e28dc42a41..aea336c8100b86f8d53d7a831347bc69ebbc10a7 100644 (file)
@@ -48,6 +48,7 @@ import org.simantics.simulation.data.Datasource;
 import org.simantics.simulation.experiment.Experiment;\r
 import org.simantics.simulation.experiment.ExperimentState;\r
 import org.simantics.simulation.experiment.IDynamicExperiment;\r
+import org.simantics.simulation.experiment.IExperimentListener;\r
 import org.simantics.simulation.ontology.SimulationResource;\r
 import org.simantics.sysdyn.SysdynResource;\r
 import org.simantics.sysdyn.adapter.VariableValueSubscription;\r
@@ -89,7 +90,9 @@ public class SysdynExperiment extends Experiment implements IDynamicExperiment {
         */\r
        public void init(ReadGraph g) {\r
                this.session = g.getSession();\r
-               changeState(ExperimentState.STOPPED);\r
+               state = ExperimentState.STOPPED;\r
+               for(IExperimentListener listener : listeners.getListeners())\r
+                       listener.stateChanged(state);\r
 \r
                session.asyncRequest(new ReadRequest() {\r
 \r
@@ -100,7 +103,8 @@ public class SysdynExperiment extends Experiment implements IDynamicExperiment {
                                toggleActivation(graph, true);\r
                        }\r
                });\r
-\r
+               \r
+               setStateToDatabase(ExperimentState.INITIALIZING);\r
        }\r
 \r
        @Override\r
@@ -145,8 +149,10 @@ public class SysdynExperiment extends Experiment implements IDynamicExperiment {
        @Override\r
        public void simulate(boolean enabled) {\r
                if(enabled && sysdynModel != null) {\r
-                       changeState(ExperimentState.RUNNING);\r
-                       startSimulationJob();\r
+                       if(!ExperimentState.RUNNING.equals(getState())) {\r
+                               changeState(ExperimentState.RUNNING);\r
+                               startSimulationJob();\r
+                       }\r
                } else if (!toggled){\r
                        changeState(ExperimentState.STOPPED);\r
                }\r
@@ -562,32 +568,7 @@ public class SysdynExperiment extends Experiment implements IDynamicExperiment {
        }\r
 \r
        protected void localStateChange() {\r
-               final ExperimentState state = getState();\r
-               session.asyncRequest(new WriteRequest() {\r
-                       @Override\r
-                       public void perform(WriteGraph graph) throws DatabaseException {\r
-                               Layer0 L0 = Layer0.getInstance(graph);\r
-                               SimulationResource SR = SimulationResource.getInstance(graph);\r
-                               graph.deny(model, SR.HasExperimentState);\r
-                               Resource st = graph.newResource();\r
-                               switch(state) {\r
-                               case INITIALIZING:\r
-                                       graph.claim(st, L0.InstanceOf, SR.ExperimentState_Initializing);\r
-                                       break;\r
-                               case RUNNING:\r
-                                       graph.claim(st, L0.InstanceOf, SR.ExperimentState_Running);\r
-                                       break;\r
-                               case STOPPED:\r
-                                       graph.claim(st, L0.InstanceOf, SR.ExperimentState_Stopped);\r
-                                       break;\r
-                               case DISPOSED:\r
-                                       graph.claim(st, L0.InstanceOf, SR.ExperimentState_Disposed);\r
-                                       break;\r
-                               }\r
-\r
-                               graph.claim(model, SR.HasExperimentState, st);\r
-                       }});\r
-\r
+               setStateToDatabase(getState());\r
                switch(state) {\r
                case DISPOSED:\r
                        onExperimentDisposed();\r
@@ -596,6 +577,41 @@ public class SysdynExperiment extends Experiment implements IDynamicExperiment {
 \r
        }\r
        \r
+       protected void setStateToDatabase(final ExperimentState state) {\r
+               session.asyncRequest(new ReadRequest() {\r
+                       \r
+                       @Override\r
+                       public void run(ReadGraph graph) throws DatabaseException {\r
+                               VirtualGraphSupport support = graph.getService(VirtualGraphSupport.class);\r
+                               final Session session = graph.getSession();\r
+                               session.asyncRequest(new WriteRequest(support.getWorkspacePersistent("experiments")) {\r
+                                       @Override\r
+                                       public void perform(WriteGraph graph) throws DatabaseException {\r
+                                               Layer0 L0 = Layer0.getInstance(graph);\r
+                                               SimulationResource SR = SimulationResource.getInstance(graph);\r
+                                               graph.deny(model, SR.HasExperimentState);\r
+                                               Resource st = graph.newResource();\r
+                                               switch(state) {\r
+                                               case INITIALIZING:\r
+                                                       graph.claim(st, L0.InstanceOf, SR.ExperimentState_Initializing);\r
+                                                       break;\r
+                                               case RUNNING:\r
+                                                       graph.claim(st, L0.InstanceOf, SR.ExperimentState_Running);\r
+                                                       break;\r
+                                               case STOPPED:\r
+                                                       graph.claim(st, L0.InstanceOf, SR.ExperimentState_Stopped);\r
+                                                       break;\r
+                                               case DISPOSED:\r
+                                                       graph.claim(st, L0.InstanceOf, SR.ExperimentState_Disposed);\r
+                                                       break;\r
+                                               }\r
+\r
+                                               graph.claim(model, SR.HasExperimentState, st);\r
+                                       }});                            \r
+                       }\r
+               });\r
+       }\r
+       \r
        /**\r
         * Actions performed when experiment is disposed\r
         * @param graph\r