]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.simulation.ui/src/org/simantics/simulation/ui/handlers/e4/SetDuration.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 / SetDuration.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.inject.Inject;
15
16 import org.eclipse.e4.core.di.annotations.Execute;
17 import org.eclipse.e4.core.di.annotations.Optional;
18 import org.eclipse.e4.core.services.events.IEventBroker;
19 import org.eclipse.e4.ui.di.UIEventTopic;
20 import org.eclipse.e4.ui.model.application.MApplication;
21 import org.eclipse.e4.ui.model.application.ui.menu.MMenuItem;
22 import org.eclipse.e4.ui.workbench.modeling.EModelService;
23 import org.eclipse.jface.dialogs.InputDialog;
24 import org.eclipse.ui.PlatformUI;
25 import org.osgi.service.prefs.BackingStoreException;
26 import org.simantics.simulation.SimulationEvents;
27 import org.simantics.simulation.ui.preferences.SimulationPreferenceUtil;
28 import org.simantics.simulation.ui.preferences.SimulationPreferences;
29 import org.simantics.simulation.ui.preferences.SimulationPreferences.StepMode;
30 import org.simantics.utils.ui.ExceptionUtils;
31
32 public class SetDuration {
33
34     private static final String SET_DURATION_ITEM_ID = "org.simantics.simulation.ui.handledmenuitem.setduration";
35
36     @Inject
37     private EModelService modelService;
38
39     @Inject
40     private IEventBroker eventBroker;
41
42     @Execute
43     public void execute(MMenuItem item) {
44         // This is a radio menu item, execute is called also when it is deselected
45         if (!item.isSelected())
46             return;
47
48         SimulationPreferences prefs = SimulationPreferenceUtil.getPrefs();
49
50         TimeInputDialog dialog = new TimeInputDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
51                 "Set Simulation Step Duration", "simulation step duration", "" + prefs.stepDuration);
52         if (dialog.open() == InputDialog.OK) {
53             final double duration = Double.parseDouble(dialog.getValue());
54             try {
55                 SimulationPreferenceUtil.flushPrefs(prefs.withDuration(duration).withStepMode(StepMode.DURATION));
56                 eventBroker.post(SimulationEvents.TOPIC_STEP_DURATION, duration);
57                 eventBroker.post(SimulationEvents.TOPIC_STEP_MODE, StepMode.DURATION);
58             } catch (BackingStoreException e) {
59                 ExceptionUtils.logAndShowError(e);
60             }
61         } else {
62             eventBroker.post(SimulationEvents.TOPIC_STEP_MODE, prefs.stepMode);
63         }
64     }
65
66     @Inject
67     @Optional
68     public void updateElement(@Optional @UIEventTopic(SimulationEvents.TOPIC_STEP_MODE) StepMode mode, MApplication application) {
69         WorkbenchUtil.setMenuItemSelected(modelService, application, Step.HANDLED_ITEM_ID, SET_DURATION_ITEM_ID, mode == StepMode.DURATION);
70     }
71
72 }