]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
27e3b551bdaad5b75e0fee2ade08acb214eb6259
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2011 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 java.text.DecimalFormat;\r
15 import java.text.DecimalFormatSymbols;\r
16 \r
17 import org.eclipse.jface.action.ToolBarContributionItem;\r
18 import org.eclipse.jface.layout.GridDataFactory;\r
19 import org.eclipse.jface.layout.GridLayoutFactory;\r
20 import org.eclipse.swt.SWT;\r
21 import org.eclipse.swt.events.SelectionEvent;\r
22 import org.eclipse.swt.events.SelectionListener;\r
23 import org.eclipse.swt.widgets.Composite;\r
24 import org.eclipse.swt.widgets.Label;\r
25 import org.eclipse.swt.widgets.Slider;\r
26 import org.eclipse.swt.widgets.ToolBar;\r
27 import org.eclipse.swt.widgets.ToolItem;\r
28 import org.simantics.db.ReadGraph;\r
29 import org.simantics.db.Resource;\r
30 import org.simantics.db.exception.DatabaseException;\r
31 import org.simantics.db.request.Read;\r
32 import org.simantics.simulation.experiment.IExperiment;\r
33 import org.simantics.simulation.project.IExperimentManager;\r
34 import org.simantics.sysdyn.SysdynResource;\r
35 import org.simantics.sysdyn.manager.SysdynPlaybackExperiment;\r
36 import org.simantics.ui.SimanticsUI;\r
37 \r
38 /**\r
39  * Contribution to the main toolbar. PlaybackSliderContribution contains a slider\r
40  * that can be used to control the time in a playback experiment\r
41  * \r
42  * @author Teemu Lempinen\r
43  *\r
44  */\r
45 public class PlaybackSliderContribution extends ToolBarContributionItem {\r
46 \r
47     Runnable timeListener;\r
48     SysdynPlaybackExperiment spe;\r
49     Slider s;\r
50     Double startTime, endTime;\r
51     boolean ignoreChange = false;\r
52 \r
53     @Override\r
54     public void fill(ToolBar parent, int index)\r
55     {\r
56         if (parent != null) {\r
57 \r
58             IExperimentManager manager = SimanticsUI.getProject().getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);\r
59             IExperiment experiment = manager.getActiveExperiment();\r
60             if(experiment == null || !(experiment instanceof SysdynPlaybackExperiment)) \r
61                 return;\r
62             spe = (SysdynPlaybackExperiment)experiment;\r
63 \r
64             Double[] numbers = new Double[2];\r
65             try {\r
66                 numbers = SimanticsUI.getSession().syncRequest(new Read<Double[]>() {\r
67                     @Override\r
68                     public Double[] perform(ReadGraph graph) throws DatabaseException {\r
69                         Double[] numbers = new Double[2];\r
70                         Resource model = spe.getModel();\r
71                         SysdynResource sr = SysdynResource.getInstance(graph);\r
72                         numbers[0] = graph.getRelatedValue(model, sr.SysdynModel_startTime);\r
73                         numbers[1] = graph.getRelatedValue(model, sr.SysdynModel_stopTime);\r
74                         return numbers;\r
75                     }\r
76                 });\r
77             } catch (DatabaseException e1) {\r
78                 e1.printStackTrace();\r
79             }\r
80 \r
81             // Separator ToolItem can contain a composite. Add a composite with a slider to this item\r
82             ToolItem ti = new ToolItem(parent, SWT.SEPARATOR);\r
83 \r
84             Composite composite = new Composite(parent, SWT.NONE);\r
85             GridLayoutFactory.fillDefaults().margins(3, SWT.DEFAULT).numColumns(2).applyTo(composite);\r
86             GridDataFactory.fillDefaults().applyTo(composite);\r
87 \r
88             s = new Slider(composite, SWT.NONE);\r
89             s.setMinimum(0);\r
90             s.setMaximum(100);\r
91             s.setIncrement(1);\r
92             s.setPageIncrement(1);\r
93             s.setThumb(1);\r
94 \r
95             final Label label = new Label(composite, SWT.NONE);\r
96             GridDataFactory.fillDefaults().hint(70, SWT.DEFAULT).applyTo(label);\r
97             label.setText("0.0");\r
98 \r
99             ti.setWidth(270);\r
100             ti.setControl(composite);\r
101 \r
102             startTime = numbers[0];\r
103             endTime = numbers[1];\r
104             \r
105             // Create a DesimalFormat for rounding the time\r
106             final DecimalFormat format = new DecimalFormat();\r
107             format.setMinimumFractionDigits(0);\r
108             format.setMaximumFractionDigits(2);\r
109             DecimalFormatSymbols symbols = new DecimalFormatSymbols();\r
110             symbols.setDecimalSeparator('.');\r
111             symbols.setGroupingSeparator(' ');\r
112             format.setDecimalFormatSymbols(symbols);\r
113             \r
114             // Selection listener for the slider\r
115             s.addSelectionListener(new SelectionListener() {\r
116 \r
117                 @Override\r
118                 public void widgetSelected(SelectionEvent e) {\r
119                     Slider s = (Slider)e.widget;\r
120                     Double time = s.getSelection() / 99.0 * (endTime - startTime) + startTime;\r
121                     spe.setTimeAndContinue(time);\r
122                     if(!label.isDisposed()) {\r
123                         label.setText(format.format(time));\r
124                     }\r
125                 }\r
126 \r
127                 @Override\r
128                 public void widgetDefaultSelected(SelectionEvent e) {\r
129                 }\r
130             });\r
131 \r
132             if(timeListener != null) {\r
133                 spe.removeTimeListener(timeListener);\r
134             }\r
135 \r
136             // Time listener for setting the time in the slider if the time is changed somewhere else\r
137             timeListener = new Runnable() {\r
138 \r
139                 @Override\r
140                 public void run() {\r
141                     s.getDisplay().asyncExec(new Runnable() {\r
142 \r
143                         @Override\r
144                         public void run() {\r
145                             if(!startTime.equals(spe.getStartTime()) || !endTime.equals(spe.getEndTime())) {\r
146                                 startTime = spe.getStartTime();\r
147                                 endTime = spe.getEndTime();\r
148                             }\r
149                             int value = (int) Math.round(((spe.getTime() - startTime) / (endTime - startTime) * 99));\r
150                             s.setSelection(value);\r
151                             label.setText(format.format(spe.getTime()));\r
152                         }\r
153                     });\r
154 \r
155                 }\r
156 \r
157             };\r
158             spe.addTimeListener(timeListener);\r
159         }\r
160     }\r
161 \r
162     @Override\r
163     public void dispose() {\r
164         if(this.timeListener != null && spe != null) {\r
165             spe.removeTimeListener(timeListener);\r
166             this.timeListener = null;\r
167         }\r
168         super.dispose();\r
169 \r
170     }\r
171 }\r