Commit | Line | Data |
---|---|---|
6e512b93 ASL |
1 | /******************************************************************************* |
2 | * Copyright (c) 2009 Ericsson | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * Francois Chouinard - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.lttng.ui.views.timeframe; | |
14 | ||
6c13869b FC |
15 | import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange; |
16 | import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp; | |
6e512b93 ASL |
17 | import org.eclipse.swt.SWT; |
18 | import org.eclipse.swt.events.ModifyEvent; | |
19 | import org.eclipse.swt.events.ModifyListener; | |
20 | import org.eclipse.swt.layout.GridData; | |
21 | import org.eclipse.swt.widgets.Composite; | |
22 | import org.eclipse.swt.widgets.Group; | |
23 | import org.eclipse.swt.widgets.Label; | |
24 | import org.eclipse.swt.widgets.Spinner; | |
25 | ||
26 | // ======================================================================== | |
27 | // SpinnerGroup | |
28 | // ======================================================================== | |
29 | ||
30 | /** | |
31 | * <b><u>SpinnerGroup</u></b> | |
32 | * <p> | |
33 | * A SpinnerGroup holds two coordinated spinners (for seconds and | |
34 | * nanoseconds) representing the current time within the trace. | |
35 | * <p> | |
36 | * The current time can take any value anything within the time range (start | |
37 | * and end time). | |
38 | */ | |
39 | public class SpinnerGroup { | |
40 | ||
41 | // The nanosecond scale (10^9) | |
8d2e2848 FC |
42 | private static final int NS_PER_SECOND = 1000 * 1000 * 1000; |
43 | private static final byte NS_SCALING_FACTOR = -9; | |
6e512b93 | 44 | |
62d1696a | 45 | // Labels |
9c4eb5f7 FC |
46 | private static final String SECONDS_LABEL = "sec"; //$NON-NLS-1$ |
47 | private static final String NANOSEC_LABEL = "ns"; //$NON-NLS-1$ | |
62d1696a | 48 | |
6e512b93 ASL |
49 | // Widgets |
50 | private Group group; | |
51 | private Spinner seconds; | |
52 | private Spinner nanosec; | |
53 | ||
54 | // The valid time range - start time | |
55 | private TmfTimestamp startTime; | |
56 | private int startSeconds; | |
57 | private int startNanosec; | |
58 | ||
59 | // The valid time range - end time | |
60 | private TmfTimestamp endTime; | |
61 | private int endSeconds; | |
62 | private int endNanosec; | |
63 | ||
64 | // The current time value | |
65 | private TmfTimestamp currentTime; | |
66 | private int currentSeconds; | |
67 | private int currentNanosec; | |
68 | ||
13198c7a | 69 | @SuppressWarnings("unused") |
6e512b93 ASL |
70 | private TimeFrameView fOwner; |
71 | ||
72 | /** | |
73 | * <b><u>Constructor</u></b> | |
74 | * <p> | |
75 | * <li>Creates the display group and formats it for the grid cell | |
76 | * <li>Sets the initial values for Start/End/Current time | |
77 | * </li> | |
78 | * <p> | |
79 | * @param parent - the parent Composite | |
80 | * @param groupName - the group name | |
81 | * @param range - the valid time range (start/end time) | |
82 | * @param current - the current time | |
83 | */ | |
84 | public SpinnerGroup(TimeFrameView owner, Composite parent, String groupName, TmfTimeRange range, TmfTimestamp current) { | |
85 | ||
86 | fOwner = owner; | |
87 | ||
88 | // Create the group | |
89 | group = new Group(parent, SWT.BORDER); | |
90 | group.setText(groupName); | |
91 | ||
92 | // Make it use the whole grid cell | |
93 | GridData gridData = new GridData(SWT.LEFT, SWT.TOP, true, false); | |
94 | gridData.horizontalAlignment = SWT.FILL; | |
95 | group.setLayoutData(gridData); | |
96 | ||
97 | // Create and position the widgets | |
98 | seconds = new Spinner(group, SWT.BORDER); | |
99 | seconds.addModifyListener(new ModifyListener() { | |
d4011df2 FC |
100 | @Override |
101 | public void modifyText(ModifyEvent e) { | |
6e512b93 ASL |
102 | currentSeconds = seconds.getSelection(); |
103 | refreshCurrentTime(); | |
104 | } | |
105 | }); | |
106 | seconds.setBounds(5, 25, 110, 25); | |
107 | ||
108 | Label label = new Label(group, SWT.LEFT); | |
62d1696a | 109 | label.setText(SECONDS_LABEL); |
6e512b93 ASL |
110 | label.setBounds(120, 28, 25, 22); |
111 | ||
112 | nanosec = new Spinner(group, SWT.BORDER); | |
113 | nanosec.addModifyListener(new ModifyListener() { | |
d4011df2 FC |
114 | @Override |
115 | public void modifyText(ModifyEvent e) { | |
6e512b93 ASL |
116 | currentNanosec = nanosec.getSelection(); |
117 | // Correct for nanosec underflow | |
118 | if (currentNanosec < 0) { | |
119 | currentSeconds--; | |
8d2e2848 | 120 | currentNanosec = NS_PER_SECOND - 1; |
6e512b93 ASL |
121 | } |
122 | // Correct for nanosec overflow | |
8d2e2848 | 123 | if (currentNanosec >= NS_PER_SECOND) { |
6e512b93 ASL |
124 | currentSeconds++; |
125 | currentNanosec = 0; | |
126 | } | |
127 | refreshCurrentTime(); | |
128 | } | |
129 | }); | |
130 | nanosec.setBounds(150, 25, 110, 25); | |
131 | ||
132 | label = new Label(group, SWT.LEFT); | |
62d1696a | 133 | label.setText(NANOSEC_LABEL); |
6e512b93 ASL |
134 | label.setBounds(265, 28, 25, 22); |
135 | ||
136 | setContent(range, current); | |
137 | } | |
138 | ||
139 | private void refreshCurrentTime() { | |
8d2e2848 FC |
140 | long newCurrentTime = ((long) currentSeconds) * NS_PER_SECOND + currentNanosec; |
141 | TmfTimestamp ts = new TmfTimestamp(newCurrentTime, NS_SCALING_FACTOR, 0); | |
6e512b93 | 142 | currentTime = ts; |
09acd610 | 143 | // fOwner.synchTimeFrameWidgets(this); |
6e512b93 ASL |
144 | } |
145 | ||
146 | // ==================================================================== | |
147 | // Get/Set | |
148 | // ==================================================================== | |
149 | ||
150 | public TmfTimestamp getStartTime() { | |
151 | return startTime; | |
152 | } | |
153 | ||
154 | public TmfTimestamp getEndTime() { | |
155 | return endTime; | |
156 | } | |
157 | ||
158 | public TmfTimestamp getCurrentTime() { | |
159 | return currentTime; | |
160 | } | |
161 | ||
162 | public TmfTimestamp getSpan() { | |
cbd4ad82 | 163 | TmfTimestamp span = new TmfTimestamp(startTime.getAdjustment(endTime, NS_SCALING_FACTOR), NS_SCALING_FACTOR, 0); |
6e512b93 ASL |
164 | return span; |
165 | } | |
166 | ||
167 | public TmfTimeRange getTimeRange() { | |
168 | TmfTimeRange range = new TmfTimeRange(startTime, endTime); | |
169 | return range; | |
170 | } | |
171 | ||
172 | public void setStartTime(TmfTimestamp ts) { | |
62d1696a | 173 | try { |
8d2e2848 FC |
174 | startTime = ts.synchronize(0, NS_SCALING_FACTOR); |
175 | startSeconds = (int) (startTime.getValue() / NS_PER_SECOND); | |
176 | startNanosec = (int) (startTime.getValue() % NS_PER_SECOND); | |
62d1696a FC |
177 | } |
178 | catch (ArithmeticException e) { | |
179 | } | |
6e512b93 ASL |
180 | } |
181 | ||
182 | public void setEndTime(TmfTimestamp ts) { | |
62d1696a | 183 | try { |
8d2e2848 FC |
184 | endTime = ts.synchronize(0, NS_SCALING_FACTOR); |
185 | endSeconds = (int) (endTime.getValue() / NS_PER_SECOND); | |
186 | endNanosec = (int) (endTime.getValue() % NS_PER_SECOND); | |
62d1696a FC |
187 | } |
188 | catch (ArithmeticException e) { | |
189 | } | |
6e512b93 ASL |
190 | } |
191 | ||
192 | public void setCurrentTime(TmfTimestamp ts) { | |
62d1696a | 193 | try { |
8d2e2848 FC |
194 | currentTime = ts.synchronize(0, NS_SCALING_FACTOR); |
195 | currentSeconds = (int) (currentTime.getValue() / NS_PER_SECOND); | |
196 | currentNanosec = (int) (currentTime.getValue() % NS_PER_SECOND); | |
62d1696a FC |
197 | } |
198 | catch (ArithmeticException e) { | |
199 | } | |
6e512b93 ASL |
200 | } |
201 | ||
202 | // ==================================================================== | |
203 | // Operators | |
204 | // ==================================================================== | |
205 | ||
206 | /** | |
207 | * <b><u>setContent</u></b> | |
208 | * <p> | |
209 | * <li>validates that [startTime <= currentTime <= endTime] is respected | |
210 | * <li>sets the start/current/end time and update the spinners | |
211 | * </li> | |
212 | * <p> | |
213 | * | |
214 | * @param range | |
215 | * @param current | |
216 | */ | |
217 | public void setContent(TmfTimeRange range, TmfTimestamp current) { | |
218 | ||
219 | if (range != null) { | |
220 | // Extract the time range | |
221 | TmfTimestamp start = range.getStartTime(); | |
09acd610 | 222 | TmfTimestamp end = range.getEndTime(); |
6e512b93 ASL |
223 | |
224 | // Assume start time is OK | |
225 | setStartTime(start); | |
226 | ||
227 | // Make sure end time >= start time | |
228 | if (end.compareTo(start, false) < 0) { | |
229 | end = start; | |
230 | } | |
231 | setEndTime(end); | |
232 | ||
233 | // Make sure [start time <= current time <= end time] | |
234 | // If not: current = min(max(start, current), end); | |
235 | if (current.compareTo(start, false) < 0) { | |
236 | current = start; | |
237 | } | |
238 | if (current.compareTo(end, false) > 0) { | |
239 | current = end; | |
240 | } | |
241 | } | |
242 | setCurrentTime(current); | |
243 | ||
244 | // And configure the spinners | |
245 | updateSpinners(); | |
246 | } | |
247 | ||
248 | /** | |
249 | * <b><u>setValue</u></b> | |
250 | * <p> | |
251 | * <li>validates that [startTime <= currentTime <= endTime] is respected | |
252 | * <li>sets the current time and the spinners | |
253 | * </li> | |
254 | * <p> | |
255 | * | |
256 | * @param range | |
257 | * @param current | |
258 | */ | |
259 | public void setValue(TmfTimestamp current) { | |
260 | ||
261 | // Make sure [start time <= current time <= end time] | |
262 | // If not: current = min(max(start, current), end); | |
263 | if (current.compareTo(startTime, false) < 0) { | |
264 | current = startTime; | |
265 | } | |
266 | if (current.compareTo(endTime, false) > 0) { | |
267 | current = endTime; | |
268 | } | |
269 | setCurrentTime(current); | |
270 | ||
271 | // And configure the spinners | |
272 | updateSpinners(); | |
273 | } | |
274 | ||
275 | /** | |
276 | * Update the spinners with the new current time value | |
277 | * Perform the update on the UI thread | |
278 | */ | |
279 | public void updateSpinners() { | |
db1ea19b FC |
280 | // Ignore update if disposed |
281 | if (seconds.isDisposed()) return; | |
282 | ||
6e512b93 | 283 | seconds.getDisplay().asyncExec(new Runnable() { |
d4011df2 | 284 | @Override |
6e512b93 ASL |
285 | public void run() { |
286 | if (!seconds.isDisposed() && !nanosec.isDisposed()) { | |
287 | // If we are on the start second, ensure that [currentNS >= startNS] | |
288 | // If the currentSeconds > startSeconds, set startns to -1 so we can | |
289 | // "underflow" | |
290 | int startns = -1; | |
291 | if (currentSeconds <= startSeconds) { | |
292 | currentSeconds = startSeconds; | |
293 | startns = startNanosec; | |
294 | if (currentNanosec < startns) { | |
295 | currentNanosec = startns; | |
296 | } | |
297 | } | |
298 | ||
299 | // If we are on the end second, ensure that [currentNS <= endNS] | |
300 | // If the currentSeconds < endSeconds, set endns to MAX so we can | |
301 | // "overflow" | |
8d2e2848 | 302 | int endns = NS_PER_SECOND; |
6e512b93 ASL |
303 | if (currentSeconds >= endSeconds) { |
304 | currentSeconds = endSeconds; | |
305 | endns = endNanosec; | |
306 | if (currentNanosec > endns) { | |
307 | currentNanosec = endns; | |
308 | } | |
309 | } | |
310 | ||
311 | // Refresh the spinners (value, range, increments, ...) | |
312 | // To ensure that the spinners are properly set, the range has to be > 0 | |
09acd610 FC |
313 | // seconds.setValues(currentSeconds, startSeconds - 1, endSeconds + 1, 0, 1, 10); |
314 | // nanosec.setValues(currentNanosec, startns - 1, endns + 1, 0, 1, 1000000); | |
315 | seconds.setValues(currentSeconds, startSeconds, endSeconds, 0, 1, 10); | |
316 | nanosec.setValues(currentNanosec, startns, endns, 0, 100000, 10000000); | |
6e512b93 ASL |
317 | |
318 | // If start == end (i.e. no range), disable the spinner | |
319 | // (if start == end, the spinner widget range is set to [0..100] by default) | |
320 | seconds.setEnabled(startSeconds != endSeconds); | |
321 | nanosec.setEnabled(startns != endns); | |
322 | } | |
323 | } | |
324 | }); | |
325 | } | |
bf5aa8e4 | 326 | } |