]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.simulation.ui/src/org/simantics/simulation/ui/handlers/Reload.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.simulation.ui / src / org / simantics / simulation / ui / handlers / Reload.java
diff --git a/bundles/org.simantics.simulation.ui/src/org/simantics/simulation/ui/handlers/Reload.java b/bundles/org.simantics.simulation.ui/src/org/simantics/simulation/ui/handlers/Reload.java
new file mode 100644 (file)
index 0000000..0f9699a
--- /dev/null
@@ -0,0 +1,67 @@
+/*******************************************************************************\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.core.runtime.IProgressMonitor;\r
+import org.eclipse.core.runtime.IStatus;\r
+import org.eclipse.core.runtime.Status;\r
+import org.eclipse.core.runtime.jobs.Job;\r
+import org.simantics.ObjectIdentitySchedulingRule;\r
+import org.simantics.simulation.experiment.IExperiment;\r
+import org.simantics.simulation.project.IExperimentManager;\r
+import org.simantics.simulation.ui.Activator;\r
+import org.simantics.ui.SimanticsUI;\r
+\r
+public class Reload extends AbstractHandler {\r
+\r
+    @Override\r
+    public Object execute(ExecutionEvent event) throws ExecutionException {\r
+        IExperimentManager manager =\r
+            SimanticsUI.getProject().getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);\r
+        IExperiment experiment = manager.getActiveExperiment();\r
+        if (experiment != null) {\r
+            Job j = new RefreshJob(experiment);\r
+            j.setRule(new ObjectIdentitySchedulingRule(experiment));\r
+            j.schedule();\r
+        }\r
+        return null;\r
+    }\r
+\r
+    static class RefreshJob extends Job {\r
+        private final IExperiment experiment;\r
+\r
+        public RefreshJob(IExperiment experiment) {\r
+            super("Refresh Experiment");\r
+            this.experiment = experiment;\r
+        }\r
+\r
+        @Override\r
+        protected IStatus run(IProgressMonitor monitor) {\r
+            try {\r
+                monitor.beginTask("", IProgressMonitor.UNKNOWN);\r
+                experiment.refresh(SimanticsUI.getSession());\r
+                if (monitor.isCanceled())\r
+                    return Status.CANCEL_STATUS;\r
+                return Status.OK_STATUS;\r
+            } catch (Throwable t) {\r
+                return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Failed to refresh experiment "\r
+                        + experiment + " with ID " + experiment.getIdentifier(), t);\r
+            } finally {\r
+                monitor.done();\r
+            }\r
+        }\r
+    }\r
+\r
+}\r