]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
aae0b0ef51f9d66cbcf9b48136652bb0114147f9
[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[3];\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[3];\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                         numbers[2] = graph.getPossibleRelatedValue(model, sr.SysdynModel_outputInterval);\r
75                         return numbers;\r
76                     }\r
77                 });\r
78             } catch (DatabaseException e1) {\r
79                 e1.printStackTrace();\r
80             }\r
81 \r
82             // Separator ToolItem can contain a composite. Add a composite with a slider to this item\r
83             ToolItem ti = new ToolItem(parent, SWT.SEPARATOR);\r
84 \r
85             Composite composite = new Composite(parent, SWT.NONE);\r
86             GridLayoutFactory.fillDefaults().margins(3, SWT.DEFAULT).numColumns(2).applyTo(composite);\r
87             GridDataFactory.fillDefaults().applyTo(composite);\r
88 \r
89             s = new Slider(composite, SWT.NONE);\r
90             s.setMinimum(0);\r
91             s.setMaximum(100);\r
92             s.setIncrement(1);\r
93             s.setPageIncrement(1);\r
94             s.setThumb(1);\r
95 \r
96             final Label label = new Label(composite, SWT.NONE);\r
97             GridDataFactory.fillDefaults().hint(70, SWT.DEFAULT).applyTo(label);\r
98             label.setText("0.0");\r
99 \r
100             ti.setWidth(270);\r
101             ti.setControl(composite);\r
102 \r
103             startTime = numbers[0];\r
104             endTime = numbers[1];\r
105             \r
106             // Create a DesimalFormat for rounding the time\r
107             final DecimalFormat format = new DecimalFormat();\r
108             format.setMinimumFractionDigits(0);\r
109             format.setMaximumFractionDigits(2);\r
110             DecimalFormatSymbols symbols = new DecimalFormatSymbols();\r
111             symbols.setDecimalSeparator('.');\r
112             symbols.setGroupingSeparator(' ');\r
113             format.setDecimalFormatSymbols(symbols);\r
114             \r
115             // Selection listener for the slider\r
116             s.addSelectionListener(new SelectionListener() {\r
117 \r
118                 @Override\r
119                 public void widgetSelected(SelectionEvent e) {\r
120                     Slider s = (Slider)e.widget;\r
121                     Double time = s.getSelection() / 99.0 * (endTime - startTime) + startTime;\r
122                     spe.setTimeAndContinue(time);\r
123                     if(!label.isDisposed()) {\r
124                         label.setText(format.format(time));\r
125                     }\r
126                 }\r
127 \r
128                 @Override\r
129                 public void widgetDefaultSelected(SelectionEvent e) {\r
130                 }\r
131             });\r
132 \r
133             if(timeListener != null) {\r
134                 spe.removeTimeListener(timeListener);\r
135             }\r
136 \r
137             // Time listener for setting the time in the slider if the time is changed somewhere else\r
138             timeListener = new Runnable() {\r
139 \r
140                 @Override\r
141                 public void run() {\r
142                     s.getDisplay().asyncExec(new Runnable() {\r
143 \r
144                         @Override\r
145                         public void run() {\r
146                             if(!startTime.equals(spe.getStartTime()) || !endTime.equals(spe.getEndTime())) {\r
147                                 startTime = spe.getStartTime();\r
148                                 endTime = spe.getEndTime();\r
149                             }\r
150                             int value = (int) Math.round(((spe.getTime() - startTime) / (endTime - startTime) * 99));\r
151                             s.setSelection(value);\r
152                             label.setText(format.format(spe.getTime()));\r
153                         }\r
154                     });\r
155 \r
156                 }\r
157 \r
158             };\r
159             spe.addTimeListener(timeListener);\r
160         }\r
161     }\r
162 \r
163     @Override\r
164     public void dispose() {\r
165         if(this.timeListener != null && spe != null) {\r
166             spe.removeTimeListener(timeListener);\r
167             this.timeListener = null;\r
168         }\r
169         super.dispose();\r
170 \r
171     }\r
172 }\r