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 java.text.ParseException;
16 import javax.inject.Inject;
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;
36 * @author Tuukka Lehtonen
39 public class SetEndTime {
41 private static final String SET_END_TIME_ITEM_ID = "org.simantics.simulation.ui.handledmenuitem.setendtime";
44 private EModelService modelService;
47 private IEventBroker eventBroker;
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())
55 SimulationPreferences prefs = SimulationPreferenceUtil.getPrefs();
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) {
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);
71 eventBroker.post(SimulationEvents.TOPIC_STEP_MODE, prefs.stepMode);
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);