1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.simulation.ui.handlers.e4;
14 import javax.annotation.PostConstruct;
15 import javax.inject.Inject;
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;
37 static final String HANDLED_ITEM_ID = "org.simantics.simulation.ui.handledtoolitem.step";
40 EModelService modelService;
43 public void updateToolTip(MApplication application) {
44 MHandledItem handledItem = (MHandledItem) modelService.find(HANDLED_ITEM_ID, application);
45 if (handledItem != null) {
46 updateItem(handledItem);
51 public boolean canExecute() {
52 IExperimentManager manager = Simantics.getProject().getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);
55 IExperiment experiment = manager.getActiveExperiment();
56 if (experiment == null)
62 public void execute() {
63 SimulationPreferences prefs = SimulationPreferenceUtil.getPrefs();
64 switch (prefs.stepMode) {
66 ExperimentUtil.step(prefs.stepDuration);
69 ExperimentUtil.stepUntil(prefs.stepEndTime);
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);
83 private static void updateItem(MUILabel item) {
84 SimulationPreferences prefs = SimulationPreferenceUtil.getPrefs();
85 switch (prefs.stepMode) {
88 NLS.bind("Step {0} s ({1})",
89 SimulationTimeUtil.formatSeconds(prefs.stepDuration),
90 SimulationTimeUtil.formatHMSS(prefs.stepDuration)));
94 NLS.bind("Step Until Simulation Time Reaches {0} s ({1})",
95 SimulationTimeUtil.formatSeconds(prefs.stepEndTime),
96 SimulationTimeUtil.formatHMSS(prefs.stepEndTime)));