]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.simulation.ui/src/org/simantics/simulation/ui/handlers/ActivateExperimentHandler.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.simulation.ui / src / org / simantics / simulation / ui / handlers / ActivateExperimentHandler.java
diff --git a/bundles/org.simantics.simulation.ui/src/org/simantics/simulation/ui/handlers/ActivateExperimentHandler.java b/bundles/org.simantics.simulation.ui/src/org/simantics/simulation/ui/handlers/ActivateExperimentHandler.java
new file mode 100644 (file)
index 0000000..9038960
--- /dev/null
@@ -0,0 +1,74 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.simulation.ui.handlers;\r
+\r
+import org.eclipse.core.commands.AbstractHandler;\r
+import org.eclipse.core.commands.ExecutionEvent;\r
+import org.eclipse.core.commands.ExecutionException;\r
+import org.eclipse.jface.viewers.ISelection;\r
+import org.eclipse.ui.handlers.HandlerUtil;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.request.ReadRequest;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.project.IProject;\r
+import org.simantics.simulation.ontology.SimulationResource;\r
+import org.simantics.simulation.project.IExperimentManager;\r
+import org.simantics.ui.SimanticsUI;\r
+import org.simantics.ui.utils.ResourceAdaptionUtils;\r
+import org.simantics.utils.ui.ErrorLogger;\r
+\r
+/**\r
+ * A generic command handler for activating an experiment based on the currently\r
+ * selected experiment in the UI.\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ * \r
+ * @see ActivateExperimentAction\r
+ * @see ExperimentActivator\r
+ */\r
+public class ActivateExperimentHandler extends AbstractHandler {\r
+\r
+    @Override\r
+    public Object execute(ExecutionEvent event) throws ExecutionException {\r
+        ISelection selection = HandlerUtil.getCurrentSelection(event);\r
+        final Resource experiment = ResourceAdaptionUtils.toSingleResource(selection);\r
+        if (experiment == null)\r
+            return null;\r
+\r
+        final IProject project = SimanticsUI.peekProject();\r
+        if (project == null)\r
+            return null;\r
+\r
+        final IExperimentManager experimentManager = project.getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);\r
+        if (experimentManager == null) {\r
+            ErrorLogger.defaultLogWarning("Experiment manager not available.", new Exception());\r
+            return null;\r
+        }\r
+\r
+        try {\r
+            project.getSession().syncRequest(new ReadRequest() {\r
+                @Override\r
+                public void run(ReadGraph graph) throws DatabaseException {\r
+                    if (graph.isInstanceOf(experiment, SimulationResource.getInstance(graph).Experiment)) {\r
+                        ExperimentActivator.scheduleActivation(graph, project, experimentManager, experiment);\r
+                    }\r
+                }\r
+            });\r
+        } catch (DatabaseException e) {\r
+            ErrorLogger.defaultLogError(e);\r
+        }\r
+\r
+        return null;\r
+    }\r
+\r
+}
\ No newline at end of file