1 /*******************************************************************************
2 * Copyright (c) 2009 Ericsson
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
10 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.lttng
.ui
.views
.timeframe
;
15 import org
.eclipse
.linuxtools
.tmf
.event
.TmfTimeRange
;
16 import org
.eclipse
.linuxtools
.tmf
.event
.TmfTimestamp
;
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
;
26 // ========================================================================
28 // ========================================================================
31 * <b><u>SpinnerGroup</u></b>
33 * A SpinnerGroup holds two coordinated spinners (for seconds and
34 * nanoseconds) representing the current time within the trace.
36 * The current time can take any value anything within the time range (start
39 public class SpinnerGroup
{
41 // The nanosecond scale (10^9)
42 private static final int NS_PER_SECOND
= 1000 * 1000 * 1000;
43 private static final byte NS_SCALING_FACTOR
= -9;
46 private static final String SECONDS_LABEL
= "sec";
47 private static final String NANOSEC_LABEL
= "ns";
51 private Spinner seconds
;
52 private Spinner nanosec
;
54 // The valid time range - start time
55 private TmfTimestamp startTime
;
56 private int startSeconds
;
57 private int startNanosec
;
59 // The valid time range - end time
60 private TmfTimestamp endTime
;
61 private int endSeconds
;
62 private int endNanosec
;
64 // The current time value
65 private TmfTimestamp currentTime
;
66 private int currentSeconds
;
67 private int currentNanosec
;
69 @SuppressWarnings("unused")
70 private TimeFrameView fOwner
;
73 * <b><u>Constructor</u></b>
75 * <li>Creates the display group and formats it for the grid cell
76 * <li>Sets the initial values for Start/End/Current time
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
84 public SpinnerGroup(TimeFrameView owner
, Composite parent
, String groupName
, TmfTimeRange range
, TmfTimestamp current
) {
89 group
= new Group(parent
, SWT
.BORDER
);
90 group
.setText(groupName
);
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
);
97 // Create and position the widgets
98 seconds
= new Spinner(group
, SWT
.BORDER
);
99 seconds
.addModifyListener(new ModifyListener() {
100 public void modifyText(ModifyEvent e
) {
101 currentSeconds
= seconds
.getSelection();
102 refreshCurrentTime();
105 seconds
.setBounds(5, 25, 110, 25);
107 Label label
= new Label(group
, SWT
.LEFT
);
108 label
.setText(SECONDS_LABEL
);
109 label
.setBounds(120, 28, 25, 22);
111 nanosec
= new Spinner(group
, SWT
.BORDER
);
112 nanosec
.addModifyListener(new ModifyListener() {
113 public void modifyText(ModifyEvent e
) {
114 currentNanosec
= nanosec
.getSelection();
115 // Correct for nanosec underflow
116 if (currentNanosec
< 0) {
118 currentNanosec
= NS_PER_SECOND
- 1;
120 // Correct for nanosec overflow
121 if (currentNanosec
>= NS_PER_SECOND
) {
125 refreshCurrentTime();
128 nanosec
.setBounds(150, 25, 110, 25);
130 label
= new Label(group
, SWT
.LEFT
);
131 label
.setText(NANOSEC_LABEL
);
132 label
.setBounds(265, 28, 25, 22);
134 setContent(range
, current
);
137 private void refreshCurrentTime() {
138 long newCurrentTime
= ((long) currentSeconds
) * NS_PER_SECOND
+ currentNanosec
;
139 TmfTimestamp ts
= new TmfTimestamp(newCurrentTime
, NS_SCALING_FACTOR
, 0);
141 // fOwner.synchTimeFrameWidgets(this);
144 // ====================================================================
146 // ====================================================================
148 public TmfTimestamp
getStartTime() {
152 public TmfTimestamp
getEndTime() {
156 public TmfTimestamp
getCurrentTime() {
160 public TmfTimestamp
getSpan() {
161 TmfTimestamp span
= new TmfTimestamp(startTime
.getAdjustment(endTime
, NS_SCALING_FACTOR
), NS_SCALING_FACTOR
, 0);
165 public TmfTimeRange
getTimeRange() {
166 TmfTimeRange range
= new TmfTimeRange(startTime
, endTime
);
170 public void setStartTime(TmfTimestamp ts
) {
172 startTime
= ts
.synchronize(0, NS_SCALING_FACTOR
);
173 startSeconds
= (int) (startTime
.getValue() / NS_PER_SECOND
);
174 startNanosec
= (int) (startTime
.getValue() % NS_PER_SECOND
);
176 catch (ArithmeticException e
) {
180 public void setEndTime(TmfTimestamp ts
) {
182 endTime
= ts
.synchronize(0, NS_SCALING_FACTOR
);
183 endSeconds
= (int) (endTime
.getValue() / NS_PER_SECOND
);
184 endNanosec
= (int) (endTime
.getValue() % NS_PER_SECOND
);
186 catch (ArithmeticException e
) {
190 public void setCurrentTime(TmfTimestamp ts
) {
192 currentTime
= ts
.synchronize(0, NS_SCALING_FACTOR
);
193 currentSeconds
= (int) (currentTime
.getValue() / NS_PER_SECOND
);
194 currentNanosec
= (int) (currentTime
.getValue() % NS_PER_SECOND
);
196 catch (ArithmeticException e
) {
200 // ====================================================================
202 // ====================================================================
205 * <b><u>setContent</u></b>
207 * <li>validates that [startTime <= currentTime <= endTime] is respected
208 * <li>sets the start/current/end time and update the spinners
215 public void setContent(TmfTimeRange range
, TmfTimestamp current
) {
218 // Extract the time range
219 TmfTimestamp start
= range
.getStartTime();
220 TmfTimestamp end
= range
.getEndTime();
222 // Assume start time is OK
225 // Make sure end time >= start time
226 if (end
.compareTo(start
, false) < 0) {
231 // Make sure [start time <= current time <= end time]
232 // If not: current = min(max(start, current), end);
233 if (current
.compareTo(start
, false) < 0) {
236 if (current
.compareTo(end
, false) > 0) {
240 setCurrentTime(current
);
242 // And configure the spinners
247 * <b><u>setValue</u></b>
249 * <li>validates that [startTime <= currentTime <= endTime] is respected
250 * <li>sets the current time and the spinners
257 public void setValue(TmfTimestamp current
) {
259 // Make sure [start time <= current time <= end time]
260 // If not: current = min(max(start, current), end);
261 if (current
.compareTo(startTime
, false) < 0) {
264 if (current
.compareTo(endTime
, false) > 0) {
267 setCurrentTime(current
);
269 // And configure the spinners
274 * Update the spinners with the new current time value
275 * Perform the update on the UI thread
277 public void updateSpinners() {
279 seconds
.getDisplay().asyncExec(new Runnable() {
281 if (!seconds
.isDisposed() && !nanosec
.isDisposed()) {
282 // If we are on the start second, ensure that [currentNS >= startNS]
283 // If the currentSeconds > startSeconds, set startns to -1 so we can
286 if (currentSeconds
<= startSeconds
) {
287 currentSeconds
= startSeconds
;
288 startns
= startNanosec
;
289 if (currentNanosec
< startns
) {
290 currentNanosec
= startns
;
294 // If we are on the end second, ensure that [currentNS <= endNS]
295 // If the currentSeconds < endSeconds, set endns to MAX so we can
297 int endns
= NS_PER_SECOND
;
298 if (currentSeconds
>= endSeconds
) {
299 currentSeconds
= endSeconds
;
301 if (currentNanosec
> endns
) {
302 currentNanosec
= endns
;
306 // Refresh the spinners (value, range, increments, ...)
307 // To ensure that the spinners are properly set, the range has to be > 0
308 // seconds.setValues(currentSeconds, startSeconds - 1, endSeconds + 1, 0, 1, 10);
309 // nanosec.setValues(currentNanosec, startns - 1, endns + 1, 0, 1, 1000000);
310 seconds
.setValues(currentSeconds
, startSeconds
, endSeconds
, 0, 1, 10);
311 nanosec
.setValues(currentNanosec
, startns
, endns
, 0, 100000, 10000000);
313 // If start == end (i.e. no range), disable the spinner
314 // (if start == end, the spinner widget range is set to [0..100] by default)
315 seconds
.setEnabled(startSeconds
!= endSeconds
);
316 nanosec
.setEnabled(startns
!= endns
);
This page took 0.040578 seconds and 6 git commands to generate.