0dd71f2e54d798f0785c95bce6c5b40423d09ecf
[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.menu.MHandledItem;
23 import org.eclipse.e4.ui.workbench.modeling.EModelService;
24 import org.simantics.Simantics;
25 import org.simantics.simulation.SimulationEvents;
26 import org.simantics.simulation.experiment.ExperimentUtil;
27 import org.simantics.simulation.experiment.IExperiment;
28 import org.simantics.simulation.project.IExperimentManager;
29 import org.simantics.simulation.ui.preferences.SimulationPreferenceUtil;
30 import org.simantics.simulation.ui.preferences.SimulationPreferences;
31
32 public class Step {
33
34     private static final String HANDLED_ITEM_ID = "org.simantics.simulation.ui.handledtoolitem.step";
35
36     @Inject
37     EModelService modelService;
38     
39     @PostConstruct
40     public void updateToolTip(MApplication application) {
41         MHandledItem handledItem = (MHandledItem) modelService.find(HANDLED_ITEM_ID, application);
42         if (handledItem != null) {
43             SimulationPreferences prefs = SimulationPreferenceUtil.getPrefs();
44             handledItem.setTooltip("Step " + prefs.stepDuration + "s");
45         }
46     }
47     
48     @CanExecute
49     public boolean canExecute() {
50         IExperimentManager manager = Simantics.getProject().getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);
51         if (manager == null)
52             return false;
53         IExperiment experiment = manager.getActiveExperiment();
54         if (experiment == null)
55             return false;
56         return true;
57     }
58     
59     @Execute
60     public void execute() {
61         SimulationPreferences prefs = SimulationPreferenceUtil.getPrefs();
62         ExperimentUtil.step(prefs.stepDuration);
63     }
64
65     @Inject
66     @Optional
67     public void updateElement(@UIEventTopic(SimulationEvents.TOPIC_STEP_DURATION) double duration, MApplication application) {
68         MHandledItem handledItem = (MHandledItem) modelService.find(HANDLED_ITEM_ID, application);
69         if (handledItem != null) {
70             handledItem.setTooltip("Step " + duration + "s");
71         }
72     }
73
74 }