1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2012 Association for Decentralized Information Management in
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.sysdyn.ui.menu;
\r
14 import org.eclipse.jface.action.IContributionItem;
\r
15 import org.eclipse.jface.resource.JFaceResources;
\r
16 import org.eclipse.jface.resource.LocalResourceManager;
\r
17 import org.eclipse.jface.resource.ResourceManager;
\r
18 import org.eclipse.swt.SWT;
\r
19 import org.eclipse.swt.widgets.ToolBar;
\r
20 import org.eclipse.swt.widgets.ToolItem;
\r
21 import org.eclipse.ui.actions.CompoundContributionItem;
\r
22 import org.simantics.browsing.ui.swt.widgets.TrackedText;
\r
23 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupportImpl;
\r
24 import org.simantics.db.management.ISessionContext;
\r
25 import org.simantics.project.IProject;
\r
26 import org.simantics.simulation.experiment.IExperiment;
\r
27 import org.simantics.simulation.project.IExperimentManager;
\r
28 import org.simantics.sysdyn.SysdynResource;
\r
29 import org.simantics.sysdyn.manager.SysdynGameExperiment;
\r
30 import org.simantics.sysdyn.ui.properties.SysdynBasicColorProvider;
\r
31 import org.simantics.sysdyn.ui.properties.widgets.factories.DoublePropertyFactory;
\r
32 import org.simantics.sysdyn.ui.properties.widgets.factories.DoublePropertyModifier;
\r
33 import org.simantics.ui.SimanticsUI;
\r
34 import org.simantics.utils.ui.validators.DoubleValidator;
\r
38 * Contribution for adding a text field to menu bar. The text field is used
\r
39 * to set the duration of one simulation step. This duration is different from
\r
40 * the step length the simulator uses. Simulator steps as many steps that fit into
\r
41 * the simulation duration determined in this field.
\r
43 * @author Teemu Lempinen
\r
46 public class GameStepDurationContribution extends CompoundContributionItem {
\r
48 private TrackedText stepDuration;
\r
49 private int width = 40;
\r
50 private ToolItem ti;
\r
52 public static String COMMAND = "org.simantics.sysdyn.ui.gameStepDuration";
\r
54 public GameStepDurationContribution() {
\r
59 protected IContributionItem[] getContributionItems() {
\r
60 return new IContributionItem[0];
\r
64 public void fill(final ToolBar parent, final int index) {
\r
65 IProject project = SimanticsUI.peekProject();
\r
66 if (project == null)
\r
69 // Find game experiment
\r
70 IExperimentManager manager = project.getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);
\r
72 IExperiment active = manager.getActiveExperiment();
\r
73 if (!(active instanceof SysdynGameExperiment))
\r
76 final SysdynGameExperiment game = (SysdynGameExperiment) active;
\r
80 ti = new ToolItem(parent, SWT.SEPARATOR, index);
\r
81 ti.setText("Step Duration");
\r
82 ti.setToolTipText("Step Duration");
\r
84 ISessionContext context = SimanticsUI.getSessionContext();
\r
85 WidgetSupportImpl support = new WidgetSupportImpl(); // Dummy widget support for using tracked text
\r
87 stepDuration = new TrackedText(parent, support, SWT.BORDER);
\r
88 ResourceManager resourceManager = new LocalResourceManager(JFaceResources.getResources(), stepDuration.getWidget());
\r
89 stepDuration.setColorProvider(new SysdynBasicColorProvider(resourceManager));
\r
90 stepDuration.setInputValidator(new DoubleValidator());
\r
91 stepDuration.setTextFactory(new DoublePropertyFactory(SysdynResource.URIs.GameExperiment_stepDuration));
\r
92 stepDuration.addModifyListener(new DoublePropertyModifier(context, SysdynResource.URIs.GameExperiment_stepDuration));
\r
94 support.fireInput(context, game.getResource());
\r
97 ti.setControl(stepDuration.getWidget());
\r
102 public boolean isDynamic() {
\r