]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
4281055f17f59c4341a97a5b2595533857803b59
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2012 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.sysdyn.ui.menu;\r
13 \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
35 \r
36 \r
37 /**\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
42  * \r
43  * @author Teemu Lempinen\r
44  *\r
45  */\r
46 public class GameStepDurationContribution extends CompoundContributionItem {\r
47 \r
48         private TrackedText                             stepDuration;\r
49         private int                     width           = 40;\r
50     private ToolItem                            ti;\r
51     \r
52     public static String                        COMMAND         = "org.simantics.sysdyn.ui.gameStepDuration";\r
53 \r
54     public GameStepDurationContribution() {\r
55         super(COMMAND);\r
56     }\r
57 \r
58     @Override\r
59     protected IContributionItem[] getContributionItems() {\r
60         return new IContributionItem[0];\r
61     }\r
62 \r
63     @Override\r
64     public void fill(final ToolBar parent, final int index) {\r
65         IProject project = SimanticsUI.peekProject();\r
66         if (project == null)\r
67             return;\r
68 \r
69         // Find game experiment\r
70         IExperimentManager manager = project.getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);\r
71 \r
72         IExperiment active = manager.getActiveExperiment();\r
73         if (!(active instanceof SysdynGameExperiment))\r
74             return;\r
75 \r
76         final SysdynGameExperiment game = (SysdynGameExperiment) active;\r
77         \r
78         // Create the text\r
79         \r
80         ti = new ToolItem(parent, SWT.SEPARATOR, index);\r
81         ti.setText("Step Duration");\r
82         ti.setToolTipText("Step Duration");\r
83         \r
84         ISessionContext context = SimanticsUI.getSessionContext();\r
85         WidgetSupportImpl support = new WidgetSupportImpl(); // Dummy widget support for using tracked text\r
86 \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
93         \r
94         support.fireInput(context, game.getResource());\r
95         \r
96         ti.setWidth(width);\r
97         ti.setControl(stepDuration.getWidget());\r
98 \r
99     }\r
100 \r
101     @Override\r
102     public boolean isDynamic() {\r
103         return true;\r
104     }\r
105 \r
106 }