]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.simulation.ui/src/org/simantics/simulation/ui/handlers/e4/Step.java
Added Set End Time command and handler as an alternate stepping mode
[simantics/platform.git] / bundles / org.simantics.simulation.ui / src / org / simantics / simulation / ui / handlers / e4 / Step.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.simulation.ui.handlers.e4;
13
14 import javax.annotation.PostConstruct;
15 import javax.inject.Inject;
16
17 import org.eclipse.e4.core.di.annotations.CanExecute;
18 import org.eclipse.e4.core.di.annotations.Execute;
19 import org.eclipse.e4.core.di.annotations.Optional;
20 import org.eclipse.e4.ui.di.UIEventTopic;
21 import org.eclipse.e4.ui.model.application.MApplication;
22 import org.eclipse.e4.ui.model.application.ui.MUILabel;
23 import org.eclipse.e4.ui.model.application.ui.menu.MHandledItem;
24 import org.eclipse.e4.ui.workbench.modeling.EModelService;
25 import org.eclipse.osgi.util.NLS;
26 import org.simantics.Simantics;
27 import org.simantics.simulation.SimulationEvents;
28 import org.simantics.simulation.experiment.ExperimentUtil;
29 import org.simantics.simulation.experiment.IExperiment;
30 import org.simantics.simulation.experiment.SimulationTimeUtil;
31 import org.simantics.simulation.project.IExperimentManager;
32 import org.simantics.simulation.ui.preferences.SimulationPreferenceUtil;
33 import org.simantics.simulation.ui.preferences.SimulationPreferences;
34
35 public class Step {
36
37     static final String HANDLED_ITEM_ID = "org.simantics.simulation.ui.handledtoolitem.step";
38
39     @Inject
40     EModelService modelService;
41
42     @PostConstruct
43     public void updateToolTip(MApplication application) {
44         MHandledItem handledItem = (MHandledItem) modelService.find(HANDLED_ITEM_ID, application);
45         if (handledItem != null) {
46             updateItem(handledItem);
47         }
48     }
49
50     @CanExecute
51     public boolean canExecute() {
52         IExperimentManager manager = Simantics.getProject().getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);
53         if (manager == null)
54             return false;
55         IExperiment experiment = manager.getActiveExperiment();
56         if (experiment == null)
57             return false;
58         return true;
59     }
60
61     @Execute
62     public void execute() {
63         SimulationPreferences prefs = SimulationPreferenceUtil.getPrefs();
64         switch (prefs.stepMode) {
65         case DURATION:
66             ExperimentUtil.step(prefs.stepDuration);
67             break;
68         case END_TIME:
69             ExperimentUtil.stepUntil(prefs.stepEndTime);
70             break;
71         }
72     }
73
74     @Inject
75     @Optional
76     public void updateElement(@Optional @UIEventTopic(SimulationEvents.TOPIC_STEP_CHANGE) Object changed, MApplication application) {
77         MHandledItem handledItem = (MHandledItem) modelService.find(HANDLED_ITEM_ID, application);
78         if (handledItem != null) {
79             updateItem(handledItem);
80         }
81     }
82
83     private static void updateItem(MUILabel item) {
84         SimulationPreferences prefs = SimulationPreferenceUtil.getPrefs();
85         switch (prefs.stepMode) {
86         case DURATION:
87             item.setTooltip(
88                     NLS.bind("Step {0} s ({1})",
89                             SimulationTimeUtil.formatSeconds(prefs.stepDuration),
90                             SimulationTimeUtil.formatHMSS(prefs.stepDuration)));
91             break;
92         case END_TIME:
93             item.setTooltip(
94                     NLS.bind("Step Until Simulation Time Reaches {0} s ({1})",
95                             SimulationTimeUtil.formatSeconds(prefs.stepEndTime),
96                             SimulationTimeUtil.formatHMSS(prefs.stepEndTime)));
97             break;
98         }
99     }
100
101 }