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.inject.Inject;
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;
32 public class SetDuration {
34 private static final String SET_DURATION_ITEM_ID = "org.simantics.simulation.ui.handledmenuitem.setduration";
37 private EModelService modelService;
40 private IEventBroker eventBroker;
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())
48 SimulationPreferences prefs = SimulationPreferenceUtil.getPrefs();
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());
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);
62 eventBroker.post(SimulationEvents.TOPIC_STEP_MODE, prefs.stepMode);
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);