]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Added Set End Time command and handler as an alternate stepping mode 70/2870/2
authorTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Thu, 2 May 2019 12:15:57 +0000 (15:15 +0300)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Thu, 2 May 2019 12:24:13 +0000 (15:24 +0300)
Previously only duration-based simulation stepping mode was available.
These changes add another mode: simulate until an end time.

gitlab #292
APROS-15393

Change-Id: Ia08bbd04abb40a2718c63781a8318a40d616b711

15 files changed:
bundles/org.simantics.simulation.ui/fragment.e4xmi
bundles/org.simantics.simulation.ui/plugin.xml
bundles/org.simantics.simulation.ui/src/org/simantics/simulation/ui/handlers/SetDuration.java [deleted file]
bundles/org.simantics.simulation.ui/src/org/simantics/simulation/ui/handlers/e4/SetDuration.java
bundles/org.simantics.simulation.ui/src/org/simantics/simulation/ui/handlers/e4/SetEndTime.java [new file with mode: 0644]
bundles/org.simantics.simulation.ui/src/org/simantics/simulation/ui/handlers/e4/Step.java
bundles/org.simantics.simulation.ui/src/org/simantics/simulation/ui/handlers/e4/TimeInputDialog.java [new file with mode: 0644]
bundles/org.simantics.simulation.ui/src/org/simantics/simulation/ui/handlers/e4/TimeValidator.java [new file with mode: 0644]
bundles/org.simantics.simulation.ui/src/org/simantics/simulation/ui/handlers/e4/WorkbenchUtil.java [new file with mode: 0644]
bundles/org.simantics.simulation.ui/src/org/simantics/simulation/ui/preferences/SimulationPreferenceUtil.java
bundles/org.simantics.simulation.ui/src/org/simantics/simulation/ui/preferences/SimulationPreferences.java
bundles/org.simantics.simulation/src/org/simantics/simulation/SimulationEvents.java
bundles/org.simantics.simulation/src/org/simantics/simulation/experiment/ExperimentUtil.java
bundles/org.simantics.simulation/src/org/simantics/simulation/experiment/IDynamicExperiment.java
bundles/org.simantics.simulation/src/org/simantics/simulation/experiment/SimulationTimeUtil.java

index 99834be789c4537b1900c70b4829f61f810181ff..4ca765a6685b92e39e492fafbbc3ed6099f76938 100644 (file)
@@ -4,6 +4,7 @@
     <elements xsi:type="commands:Handler" xmi:id="__O4KgKMJEeW8eee008tTJw" elementId="org.simantics.simulation.ui.handler.run" contributionURI="bundleclass://org.simantics.simulation.ui/org.simantics.simulation.ui.handlers.e4.Run" command="_38VJ8KMJEeW8eee008tTJw"/>
     <elements xsi:type="commands:Handler" xmi:id="_TDgKkKMVEeW8eee008tTJw" elementId="org.simantics.simulation.ui.handler.step" contributionURI="bundleclass://org.simantics.simulation.ui/org.simantics.simulation.ui.handlers.e4.Step" command="_NxXikKMVEeW8eee008tTJw"/>
     <elements xsi:type="commands:Handler" xmi:id="__eyi4KMWEeW8eee008tTJw" elementId="org.simantics.simulation.ui.handler.setduration" contributionURI="bundleclass://org.simantics.simulation.ui/org.simantics.simulation.ui.handlers.e4.SetDuration" command="_PhrCkKMVEeW8eee008tTJw"/>
+    <elements xsi:type="commands:Handler" xmi:id="_EnmMQGtCEemEZc07DWbcZA" elementId="org.simantics.simulation.ui.handler.setendtime" contributionURI="bundleclass://org.simantics.simulation.ui/org.simantics.simulation.ui.handlers.e4.SetEndTime" command="_-IPjMGtBEemEZc07DWbcZA"/>
     <elements xsi:type="commands:Handler" xmi:id="_uxvLIKPkEeW8eee008tTJw" elementId="org.simantics.simulation.ui.handler.reload" contributionURI="bundleclass://org.simantics.simulation.ui/org.simantics.simulation.ui.handlers.e4.Reload" command="_r2lBkKPkEeW8eee008tTJw"/>
     <elements xsi:type="commands:Handler" xmi:id="_l75g8KPmEeW8eee008tTJw" elementId="org.simantics.simulation.ui.handler.savestate" contributionURI="bundleclass://org.simantics.simulation.ui/org.simantics.simulation.ui.handlers.e4.SaveState" command="_kjF68KPmEeW8eee008tTJw"/>
   </fragments>
@@ -11,6 +12,7 @@
     <elements xsi:type="commands:Command" xmi:id="_38VJ8KMJEeW8eee008tTJw" elementId="org.simantics.simulation.ui.command.run" commandName="Run Simulation" description="Runs active simulator" category="_mEzHsKMJEeW8eee008tTJw"/>
     <elements xsi:type="commands:Command" xmi:id="_NxXikKMVEeW8eee008tTJw" elementId="org.simantics.simulation.ui.command.step" commandName="Step" category="_mEzHsKMJEeW8eee008tTJw"/>
     <elements xsi:type="commands:Command" xmi:id="_PhrCkKMVEeW8eee008tTJw" elementId="org.simantics.simulation.ui.command.setduration" commandName="Set Duration" category="_mEzHsKMJEeW8eee008tTJw"/>
+    <elements xsi:type="commands:Command" xmi:id="_-IPjMGtBEemEZc07DWbcZA" elementId="org.simantics.simulation.ui.command.setendtime" commandName="Set End Time" category="_mEzHsKMJEeW8eee008tTJw"/>
     <elements xsi:type="commands:Command" xmi:id="_r2lBkKPkEeW8eee008tTJw" elementId="org.simantics.simulation.ui.command.reload" commandName="Reload"/>
     <elements xsi:type="commands:Command" xmi:id="_kjF68KPmEeW8eee008tTJw" elementId="org.simantics.simulation.ui.command.savestate" commandName="Save State"/>
   </fragments>
index 2bf32f1535142f1faabc03f3268ad83b8cdff9d6..004f7d4a7406f962f571281c9b78e463d5148109 100644 (file)
             categoryId="org.simantics.simulation.ui.category"
             id="org.simantics.simulation.ui.dispose">
       </command>
-      <command
-            defaultHandler="org.simantics.simulation.ui.handlers.SetDuration"
-            name="Set Duration"
-            categoryId="org.simantics.simulation.ui.category"
-            id="org.simantics.simulation.ui.setDuration">
-      </command>
       <command
             defaultHandler="org.simantics.simulation.ui.handlers.SaveState"
             name="Save State"
diff --git a/bundles/org.simantics.simulation.ui/src/org/simantics/simulation/ui/handlers/SetDuration.java b/bundles/org.simantics.simulation.ui/src/org/simantics/simulation/ui/handlers/SetDuration.java
deleted file mode 100644 (file)
index 8b50ddb..0000000
+++ /dev/null
@@ -1,57 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management
- * in Industry THTH ry.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- *     VTT Technical Research Centre of Finland - initial API and implementation
- *******************************************************************************/
-package org.simantics.simulation.ui.handlers;
-
-import org.eclipse.core.commands.AbstractHandler;
-import org.eclipse.core.commands.Command;
-import org.eclipse.core.commands.ExecutionEvent;
-import org.eclipse.core.commands.ExecutionException;
-import org.eclipse.jface.dialogs.InputDialog;
-import org.eclipse.ui.PlatformUI;
-import org.eclipse.ui.commands.ICommandService;
-import org.osgi.service.prefs.BackingStoreException;
-import org.simantics.simulation.ui.preferences.SimulationPreferenceUtil;
-import org.simantics.simulation.ui.preferences.SimulationPreferences;
-import org.simantics.utils.ui.ExceptionUtils;
-import org.simantics.utils.ui.validators.DoubleValidator;
-
-public class SetDuration extends AbstractHandler {
-
-    @Override
-    public Object execute(ExecutionEvent event) throws ExecutionException {
-        SimulationPreferences prefs = SimulationPreferenceUtil.getPrefs();
-
-        InputDialog dialog = new InputDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
-                "Set Simulation Step Duration", "Simulation step duration in seconds", "" + prefs.stepDuration, new DoubleValidator());
-        if (dialog.open() == InputDialog.OK) {
-            final double duration = Double.parseDouble(dialog.getValue());
-            try {
-                SimulationPreferenceUtil.flushPrefs(new SimulationPreferences(duration));
-            } catch (BackingStoreException e) {
-                ExceptionUtils.logAndShowError(e);
-            }
-
-            // Update tool-tip for Step command
-            ICommandService service = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class); 
-            Command command = service.getCommand("org.simantics.simulation.ui.step");
-            service.refreshElements(command.getId(), null); 
-
-//            IExperimentManager manager = SimanticsUI.getProject2().getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);
-//            IExperiment experiment = manager.getActiveExperiment();
-//            if (experiment instanceof IDynamicExperiment)
-//                ((IDynamicExperiment) experiment).simulateDuration(duration);
-        }
-
-        return null;
-    }
-
-}
index 71e72c9e8e7086ac3482951cf157ed78fd863cba..01a2ff5b75efcfbbae5f7c8e06324f579c3df4ba 100644 (file)
  *******************************************************************************/
 package org.simantics.simulation.ui.handlers.e4;
 
+import javax.inject.Inject;
+
 import org.eclipse.e4.core.di.annotations.Execute;
+import org.eclipse.e4.core.di.annotations.Optional;
 import org.eclipse.e4.core.services.events.IEventBroker;
-import org.eclipse.e4.ui.internal.workbench.E4Workbench;
+import org.eclipse.e4.ui.di.UIEventTopic;
+import org.eclipse.e4.ui.model.application.MApplication;
+import org.eclipse.e4.ui.model.application.ui.menu.MMenuItem;
+import org.eclipse.e4.ui.workbench.modeling.EModelService;
 import org.eclipse.jface.dialogs.InputDialog;
 import org.eclipse.ui.PlatformUI;
 import org.osgi.service.prefs.BackingStoreException;
 import org.simantics.simulation.SimulationEvents;
 import org.simantics.simulation.ui.preferences.SimulationPreferenceUtil;
 import org.simantics.simulation.ui.preferences.SimulationPreferences;
+import org.simantics.simulation.ui.preferences.SimulationPreferences.StepMode;
 import org.simantics.utils.ui.ExceptionUtils;
-import org.simantics.utils.ui.validators.DoubleValidator;
 
 public class SetDuration {
 
+    private static final String SET_DURATION_ITEM_ID = "org.simantics.simulation.ui.handledmenuitem.setduration";
+
+    @Inject
+    private EModelService modelService;
+
+    @Inject
     private IEventBroker eventBroker;
-    
-    public SetDuration() {
-        eventBroker = E4Workbench.getServiceContext().get(IEventBroker.class);
-    }
-    
+
     @Execute
-    public void execute() {
+    public void execute(MMenuItem item) {
+        // This is a radio menu item, execute is called also when it is deselected
+        if (!item.isSelected())
+            return;
+
         SimulationPreferences prefs = SimulationPreferenceUtil.getPrefs();
 
-        InputDialog dialog = new InputDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
-                "Set Simulation Step Duration", "Simulation step duration in seconds", "" + prefs.stepDuration, new DoubleValidator());
+        TimeInputDialog dialog = new TimeInputDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
+                "Set Simulation Step Duration", "simulation step duration", "" + prefs.stepDuration);
         if (dialog.open() == InputDialog.OK) {
             final double duration = Double.parseDouble(dialog.getValue());
             try {
-                SimulationPreferenceUtil.flushPrefs(new SimulationPreferences(duration));
+                SimulationPreferenceUtil.flushPrefs(prefs.withDuration(duration).withStepMode(StepMode.DURATION));
                 eventBroker.post(SimulationEvents.TOPIC_STEP_DURATION, duration);
+                eventBroker.post(SimulationEvents.TOPIC_STEP_MODE, StepMode.DURATION);
             } catch (BackingStoreException e) {
                 ExceptionUtils.logAndShowError(e);
             }
+        } else {
+            eventBroker.post(SimulationEvents.TOPIC_STEP_MODE, prefs.stepMode);
         }
     }
 
+    @Inject
+    @Optional
+    public void updateElement(@Optional @UIEventTopic(SimulationEvents.TOPIC_STEP_MODE) StepMode mode, MApplication application) {
+        WorkbenchUtil.setMenuItemSelected(modelService, application, Step.HANDLED_ITEM_ID, SET_DURATION_ITEM_ID, mode == StepMode.DURATION);
+    }
+
 }
diff --git a/bundles/org.simantics.simulation.ui/src/org/simantics/simulation/ui/handlers/e4/SetEndTime.java b/bundles/org.simantics.simulation.ui/src/org/simantics/simulation/ui/handlers/e4/SetEndTime.java
new file mode 100644 (file)
index 0000000..0b1db28
--- /dev/null
@@ -0,0 +1,81 @@
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.simulation.ui.handlers.e4;
+
+import java.text.ParseException;
+
+import javax.inject.Inject;
+
+import org.eclipse.e4.core.di.annotations.Execute;
+import org.eclipse.e4.core.di.annotations.Optional;
+import org.eclipse.e4.core.services.events.IEventBroker;
+import org.eclipse.e4.ui.di.UIEventTopic;
+import org.eclipse.e4.ui.model.application.MApplication;
+import org.eclipse.e4.ui.model.application.ui.menu.MMenuItem;
+import org.eclipse.e4.ui.workbench.modeling.EModelService;
+import org.eclipse.jface.dialogs.InputDialog;
+import org.eclipse.ui.PlatformUI;
+import org.osgi.service.prefs.BackingStoreException;
+import org.simantics.simulation.SimulationEvents;
+import org.simantics.simulation.experiment.SimulationTimeUtil;
+import org.simantics.simulation.ui.preferences.SimulationPreferenceUtil;
+import org.simantics.simulation.ui.preferences.SimulationPreferences;
+import org.simantics.simulation.ui.preferences.SimulationPreferences.StepMode;
+import org.simantics.utils.ui.ExceptionUtils;
+
+/**
+ * @author Tuukka Lehtonen
+ * @since 1.39.0
+ */
+public class SetEndTime {
+
+    private static final String SET_END_TIME_ITEM_ID = "org.simantics.simulation.ui.handledmenuitem.setendtime";
+
+    @Inject
+    private EModelService modelService;
+
+    @Inject
+    private IEventBroker eventBroker;
+
+    @Execute
+    public void execute(MMenuItem item) {
+        // This is a radio menu item, execute is called also when it is deselected
+        if (!item.isSelected())
+            return;
+
+        SimulationPreferences prefs = SimulationPreferenceUtil.getPrefs();
+
+        InputDialog dialog = new TimeInputDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
+                "Set Simulation Step End Time", "simulation step end time", "" + prefs.stepEndTime);
+        if (dialog.open() == InputDialog.OK) {
+            try {
+                double endTime = (double) SimulationTimeUtil.getTimeFormat().parseObject(dialog.getValue());
+                SimulationPreferenceUtil.flushPrefs(prefs.withEndTime(endTime).withStepMode(StepMode.END_TIME));
+                eventBroker.post(SimulationEvents.TOPIC_STEP_END_TIME, endTime);
+                eventBroker.post(SimulationEvents.TOPIC_STEP_MODE, StepMode.END_TIME);
+            } catch (BackingStoreException e) {
+                ExceptionUtils.logAndShowError(e);
+            } catch (ParseException e) {
+                ExceptionUtils.logAndShowError(e);
+            }
+        } else {
+            eventBroker.post(SimulationEvents.TOPIC_STEP_MODE, prefs.stepMode);
+        }
+    }
+
+    @Inject
+    @Optional
+    public void updateElement(@Optional @UIEventTopic(SimulationEvents.TOPIC_STEP_MODE) StepMode mode, MApplication application) {
+        WorkbenchUtil.setMenuItemSelected(modelService, application, Step.HANDLED_ITEM_ID, SET_END_TIME_ITEM_ID, mode == StepMode.END_TIME);
+    }
+
+}
index 0dd71f2e54d798f0785c95bce6c5b40423d09ecf..7e2bc034319d45a487543102a4237b6fa2d6d328 100644 (file)
@@ -19,32 +19,34 @@ import org.eclipse.e4.core.di.annotations.Execute;
 import org.eclipse.e4.core.di.annotations.Optional;
 import org.eclipse.e4.ui.di.UIEventTopic;
 import org.eclipse.e4.ui.model.application.MApplication;
+import org.eclipse.e4.ui.model.application.ui.MUILabel;
 import org.eclipse.e4.ui.model.application.ui.menu.MHandledItem;
 import org.eclipse.e4.ui.workbench.modeling.EModelService;
+import org.eclipse.osgi.util.NLS;
 import org.simantics.Simantics;
 import org.simantics.simulation.SimulationEvents;
 import org.simantics.simulation.experiment.ExperimentUtil;
 import org.simantics.simulation.experiment.IExperiment;
+import org.simantics.simulation.experiment.SimulationTimeUtil;
 import org.simantics.simulation.project.IExperimentManager;
 import org.simantics.simulation.ui.preferences.SimulationPreferenceUtil;
 import org.simantics.simulation.ui.preferences.SimulationPreferences;
 
 public class Step {
 
-    private static final String HANDLED_ITEM_ID = "org.simantics.simulation.ui.handledtoolitem.step";
+    static final String HANDLED_ITEM_ID = "org.simantics.simulation.ui.handledtoolitem.step";
 
     @Inject
     EModelService modelService;
-    
+
     @PostConstruct
     public void updateToolTip(MApplication application) {
         MHandledItem handledItem = (MHandledItem) modelService.find(HANDLED_ITEM_ID, application);
         if (handledItem != null) {
-            SimulationPreferences prefs = SimulationPreferenceUtil.getPrefs();
-            handledItem.setTooltip("Step " + prefs.stepDuration + "s");
+            updateItem(handledItem);
         }
     }
-    
+
     @CanExecute
     public boolean canExecute() {
         IExperimentManager manager = Simantics.getProject().getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);
@@ -55,19 +57,44 @@ public class Step {
             return false;
         return true;
     }
-    
+
     @Execute
     public void execute() {
         SimulationPreferences prefs = SimulationPreferenceUtil.getPrefs();
-        ExperimentUtil.step(prefs.stepDuration);
+        switch (prefs.stepMode) {
+        case DURATION:
+            ExperimentUtil.step(prefs.stepDuration);
+            break;
+        case END_TIME:
+            ExperimentUtil.stepUntil(prefs.stepEndTime);
+            break;
+        }
     }
 
     @Inject
     @Optional
-    public void updateElement(@UIEventTopic(SimulationEvents.TOPIC_STEP_DURATION) double duration, MApplication application) {
+    public void updateElement(@Optional @UIEventTopic(SimulationEvents.TOPIC_STEP_CHANGE) Object changed, MApplication application) {
         MHandledItem handledItem = (MHandledItem) modelService.find(HANDLED_ITEM_ID, application);
         if (handledItem != null) {
-            handledItem.setTooltip("Step " + duration + "s");
+            updateItem(handledItem);
+        }
+    }
+
+    private static void updateItem(MUILabel item) {
+        SimulationPreferences prefs = SimulationPreferenceUtil.getPrefs();
+        switch (prefs.stepMode) {
+        case DURATION:
+            item.setTooltip(
+                    NLS.bind("Step {0} s ({1})",
+                            SimulationTimeUtil.formatSeconds(prefs.stepDuration),
+                            SimulationTimeUtil.formatHMSS(prefs.stepDuration)));
+            break;
+        case END_TIME:
+            item.setTooltip(
+                    NLS.bind("Step Until Simulation Time Reaches {0} s ({1})",
+                            SimulationTimeUtil.formatSeconds(prefs.stepEndTime),
+                            SimulationTimeUtil.formatHMSS(prefs.stepEndTime)));
+            break;
         }
     }
 
diff --git a/bundles/org.simantics.simulation.ui/src/org/simantics/simulation/ui/handlers/e4/TimeInputDialog.java b/bundles/org.simantics.simulation.ui/src/org/simantics/simulation/ui/handlers/e4/TimeInputDialog.java
new file mode 100644 (file)
index 0000000..e2615b6
--- /dev/null
@@ -0,0 +1,60 @@
+package org.simantics.simulation.ui.handlers.e4;
+
+import java.text.ParseException;
+
+import org.eclipse.jface.dialogs.InputDialog;
+import org.eclipse.osgi.util.NLS;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.swt.widgets.Text;
+import org.simantics.simulation.experiment.SimulationTimeUtil;
+
+/**
+ * @author Tuukka Lehtonen
+ */
+public class TimeInputDialog extends InputDialog {
+
+    private Text statusText;
+
+    public TimeInputDialog(Shell parentShell, String dialogTitle, String topic, String initialValue) {
+        super(parentShell, dialogTitle, message(topic), initialValue, new TimeValidator());
+    }
+
+    @Override
+    protected Control createDialogArea(Composite parent) {
+        Composite composite = (Composite) super.createDialogArea(parent);
+
+        statusText = new Text(composite, SWT.READ_ONLY | SWT.WRAP);
+        statusText.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL
+                | GridData.HORIZONTAL_ALIGN_FILL));
+
+        return composite;
+    }
+
+    @Override
+    protected void validateInput() {
+        super.validateInput();
+
+        try {
+            double t = (double) SimulationTimeUtil.getTimeFormat().parseObject(getText().getText());
+            statusText.setText(statusMessage(t));
+        } catch (ParseException e) {
+            statusText.setText("");
+        }
+    }
+
+    private static String message(String topic) {
+        return NLS.bind("Give {0} in seconds or timestamp format ([Yy] [Dd] HH:mm:ss.ddd)", topic);
+    }
+
+    private static String statusMessage(double time) {
+        return NLS.bind(
+                "Specified time is {0} s ({1})",
+                SimulationTimeUtil.formatSeconds(time),
+                SimulationTimeUtil.formatHMSS(time));
+    }
+
+}
diff --git a/bundles/org.simantics.simulation.ui/src/org/simantics/simulation/ui/handlers/e4/TimeValidator.java b/bundles/org.simantics.simulation.ui/src/org/simantics/simulation/ui/handlers/e4/TimeValidator.java
new file mode 100644 (file)
index 0000000..ecdd18c
--- /dev/null
@@ -0,0 +1,24 @@
+package org.simantics.simulation.ui.handlers.e4;
+
+import java.text.ParseException;
+
+import org.eclipse.jface.dialogs.IInputValidator;
+import org.eclipse.osgi.util.NLS;
+import org.simantics.simulation.experiment.SimulationTimeUtil;
+
+/**
+ * @author Tuukka Lehtonen
+ * @since 1.39.0
+ */
+class TimeValidator implements IInputValidator {
+
+    public String isValid(String s) {
+        try {
+            SimulationTimeUtil.getTimeFormat().parseObject(s);
+            return null;
+        } catch (ParseException e) {
+            return NLS.bind("\"{0}\" is not a valid timestamp.", s);
+        }
+    }
+
+}
\ No newline at end of file
diff --git a/bundles/org.simantics.simulation.ui/src/org/simantics/simulation/ui/handlers/e4/WorkbenchUtil.java b/bundles/org.simantics.simulation.ui/src/org/simantics/simulation/ui/handlers/e4/WorkbenchUtil.java
new file mode 100644 (file)
index 0000000..b5d9fd6
--- /dev/null
@@ -0,0 +1,41 @@
+/*******************************************************************************
+ * Copyright (c) 2019 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     Semantum Oy - initial API and implementation
+ *******************************************************************************/
+package org.simantics.simulation.ui.handlers.e4;
+
+import org.eclipse.e4.ui.model.application.MApplication;
+import org.eclipse.e4.ui.model.application.ui.menu.MItem;
+import org.eclipse.e4.ui.model.application.ui.menu.MMenu;
+import org.eclipse.e4.ui.model.application.ui.menu.MToolItem;
+import org.eclipse.e4.ui.workbench.modeling.EModelService;
+
+/**
+ * @author Tuukka Lehtonen
+ * @since 1.39.0
+ */
+class WorkbenchUtil {
+
+    public static boolean setMenuItemSelected(EModelService modelService, MApplication application, String toolItemId, String menuItemId, boolean selected) {
+        MToolItem toolItem = (MToolItem) modelService.find(toolItemId, application);
+        if (toolItem != null) {
+            MMenu menu = toolItem.getMenu();
+            if (menu != null) {
+                MItem menuItem = (MItem) modelService.find(menuItemId, menu);
+                if (menuItem != null) {
+                    menuItem.setSelected(selected);
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
+
+}
index 0c2cfc40dbd6019136adbf7ec4d45c6f89d9eb2a..5ca1591ca306ae67953e41706e07f9cd398b39a9 100644 (file)
@@ -18,6 +18,7 @@ import org.eclipse.core.runtime.preferences.InstanceScope;
 import org.osgi.service.prefs.BackingStoreException;
 import org.osgi.service.prefs.Preferences;
 import org.simantics.simulation.ui.Activator;
+import org.simantics.simulation.ui.preferences.SimulationPreferences.StepMode;
 
 
 /**
@@ -42,8 +43,10 @@ public final class SimulationPreferenceUtil {
     public static SimulationPreferences getPrefs(IScopeContext context) {
         IEclipsePreferences node = context.getNode(Activator.PLUGIN_ID);
         double stepDuration = node.getDouble(SimulationPreferences.P_SIMULATION_STEP_DURATION, SimulationPreferences.DEFAULT_SIMULATION_STEP_DURATION);
+        double stepEndTime = node.getDouble(SimulationPreferences.P_SIMULATION_STEP_END_TIME, SimulationPreferences.DEFAULT_SIMULATION_STEP_END_TIME);
+        int stepMode = node.getInt(SimulationPreferences.P_SIMULATION_STEP_MODE, SimulationPreferences.DEFAULT_SIMULATION_STEP_MODE.ordinal());
 
-        return new SimulationPreferences(node, stepDuration);
+        return new SimulationPreferences(node, stepDuration, stepEndTime, StepMode.fromInt(stepMode));
     }
 
     /**
@@ -78,6 +81,8 @@ public final class SimulationPreferenceUtil {
     private static IEclipsePreferences _setPrefs(IScopeContext context, SimulationPreferences prefs) {
         IEclipsePreferences node = context.getNode(Activator.PLUGIN_ID);
         node.putDouble(SimulationPreferences.P_SIMULATION_STEP_DURATION, prefs.stepDuration);
+        node.putDouble(SimulationPreferences.P_SIMULATION_STEP_END_TIME, prefs.stepEndTime);
+        node.putInt(SimulationPreferences.P_SIMULATION_STEP_MODE, prefs.stepMode.ordinal());
         return node;
     }
 
index 862f51d8d8d1f6711bff193ae15006b9d2dd3dab..b3fe9506d1f4bc49e79f9bb77d657407d20ecc29 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * Copyright (c) 2007, 2019 Association for Decentralized Information Management
  * in Industry THTH ry.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
@@ -8,10 +8,12 @@
  *
  * Contributors:
  *     VTT Technical Research Centre of Finland - initial API and implementation
+ *     Semantum Oy - added step end time and mode
  *******************************************************************************/
 package org.simantics.simulation.ui.preferences;
 
 import org.eclipse.core.runtime.preferences.IEclipsePreferences;
+import org.simantics.utils.format.TimeFormat;
 
 /**
  * Constant definitions for plug-in preferences
@@ -19,27 +21,66 @@ import org.eclipse.core.runtime.preferences.IEclipsePreferences;
 public final class SimulationPreferences {
 
     public static final String                P_SIMULATION_STEP_DURATION       = "simulation.step.duration";
+    public static final String                P_SIMULATION_STEP_END_TIME       = "simulation.step.endTime";
+    public static final String                P_SIMULATION_STEP_MODE           = "simulation.step.mode";
+
+    public static enum StepMode {
+        DURATION,
+        END_TIME;
+
+        public static StepMode fromInt(int stepMode) {
+            switch (stepMode) {
+            case 1: return END_TIME;
+            default: return DURATION;
+            }
+        }
+    }
 
     public static final double                DEFAULT_SIMULATION_STEP_DURATION = 0.1;
+    public static final double                DEFAULT_SIMULATION_STEP_END_TIME = 0;
+    public static final StepMode              DEFAULT_SIMULATION_STEP_MODE     = StepMode.DURATION;
 
     public static final SimulationPreferences DEFAULT_PREFS
-        = new SimulationPreferences(null, DEFAULT_SIMULATION_STEP_DURATION);
+        = new SimulationPreferences(null,
+                DEFAULT_SIMULATION_STEP_DURATION,
+                DEFAULT_SIMULATION_STEP_END_TIME,
+                DEFAULT_SIMULATION_STEP_MODE);
 
     public final IEclipsePreferences          prefs;
     public final double                       stepDuration;
+    public final double                       stepEndTime;
+    public final StepMode                     stepMode;
 
-    public SimulationPreferences(double stepDuration) {
-        this(null, stepDuration);
+    public SimulationPreferences(double stepDuration, double stepEndTime, StepMode stepMode) {
+        this(null, stepDuration, stepEndTime, stepMode);
     }
 
-    public SimulationPreferences(IEclipsePreferences prefs, double stepDuration) {
+    public SimulationPreferences(IEclipsePreferences prefs, double stepDuration, double stepEndTime, StepMode stepMode) {
         this.prefs = prefs;
         this.stepDuration = stepDuration;
+        this.stepEndTime = stepEndTime;
+        this.stepMode = stepMode;
+    }
+
+    public SimulationPreferences withDuration(double duration) {
+        return new SimulationPreferences(duration, stepEndTime, stepMode);
+    }
+
+    public SimulationPreferences withEndTime(double endTime) {
+        return new SimulationPreferences(stepDuration, endTime, stepMode);
+    }
+
+    public SimulationPreferences withStepMode(StepMode mode) {
+        return new SimulationPreferences(stepDuration, stepEndTime, mode);
     }
 
     @Override
     public String toString() {
-        return getClass().getSimpleName() + "[step duration=" + stepDuration + "]";
+        return getClass().getSimpleName()
+                + "[step duration=" + stepDuration
+                + ", step end time=" + stepEndTime
+                + ", step mode=" + stepMode
+                + "]";
     }
 
 }
index cf05e62f05d3d7bb869bec7ba8615438c45592ff..978e1c35a6a4b48f1c2a262610870b45000147cc 100644 (file)
@@ -6,5 +6,9 @@ public interface SimulationEvents {
     public static final String TOPIC_STATE_CHANGE = "org/simantics/simulation/state/*";
     public static final String TOPIC_STATE_RUNNING = "org/simantics/simulation/state/running";
     public static final String TOPIC_STATE_STOPPED = "org/simantics/simulation/state/stopped";
+    public static final String TOPIC_STEP_CHANGE = "org/simantics/simulation/step/*";
     public static final String TOPIC_STEP_DURATION = "org/simantics/simulation/step/duration";
-}
+    public static final String TOPIC_STEP_END_TIME = "org/simantics/simulation/step/endTime";
+    public static final String TOPIC_STEP_MODE = "org/simantics/simulation/step/mode";
+
+}
\ No newline at end of file
index fb9f6780551ba488e32d838bc9ac9597cdcf61fc..d2fc9b560e7e9b54e1e3e4e81e7a5ea7acedec2d 100644 (file)
@@ -88,6 +88,31 @@ public final class ExperimentUtil {
             ((IDynamicExperiment)experiment).simulateDuration(duration);
     }
 
+    public static boolean canStepUntil(double endTime) {
+        IExperimentManager manager = 
+            Simantics.getProject().getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);
+        IExperiment experiment = manager.getActiveExperiment();
+        if (experiment instanceof IDynamicExperiment) {
+            IDynamicExperiment exp = (IDynamicExperiment) experiment;
+            double currentTime = exp.getSimulationTime();
+            return currentTime < endTime;
+        }
+        return false;
+    }
+
+    public static void stepUntil(double endTime) {
+        IExperimentManager manager = 
+            Simantics.getProject().getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);
+        IExperiment experiment = manager.getActiveExperiment();
+        if (experiment instanceof IDynamicExperiment) {
+            IDynamicExperiment exp = (IDynamicExperiment) experiment;
+            double currentTime = exp.getSimulationTime();
+            if (currentTime < endTime) {
+                exp.simulateDuration(endTime - currentTime);
+            }
+        }
+    }
+
     public static void simulate(boolean enabled) {
         IExperimentManager manager =
             Simantics.getProject().getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);
index 19dba1ee9669feef5097d94a347f1e239ef9c6df..2b249bcd9a4030b3c70f5b0f719f8c9ed9ac3502 100644 (file)
@@ -40,6 +40,11 @@ public interface IDynamicExperiment extends IExperiment {
         */
        public void rewindTo(double time);
 
+       /**
+        * @return current simulation time
+        */
+       double getSimulationTime();
+
        /**
         * Ensure that the current state of the experiment at the time of invocation
         * is stored so that it can be returned to during a later session.
index 0420a7b33880582bbeb3166f2fb5766aa1d433d3..82fb1511c3d09e87bcc99100af6672b68d690bb5 100644 (file)
@@ -51,4 +51,8 @@ public class SimulationTimeUtil {
         return result;
     }
 
+    public static TimeFormat getTimeFormat() {
+        return timeFormat;
+    }
+
 }