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