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 private TimeFrameView fOwner
;
72 * <b><u>Constructor</u></b>
74 * <li>Creates the display group and formats it for the grid cell
75 * <li>Sets the initial values for Start/End/Current time
78 * @param parent - the parent Composite
79 * @param groupName - the group name
80 * @param range - the valid time range (start/end time)
81 * @param current - the current time
83 public SpinnerGroup(TimeFrameView owner
, Composite parent
, String groupName
, TmfTimeRange range
, TmfTimestamp current
) {
88 group
= new Group(parent
, SWT
.BORDER
);
89 group
.setText(groupName
);
91 // Make it use the whole grid cell
92 GridData gridData
= new GridData(SWT
.LEFT
, SWT
.TOP
, true, false);
93 gridData
.horizontalAlignment
= SWT
.FILL
;
94 group
.setLayoutData(gridData
);
96 // Create and position the widgets
97 seconds
= new Spinner(group
, SWT
.BORDER
);
98 seconds
.addModifyListener(new ModifyListener() {
99 public void modifyText(ModifyEvent e
) {
100 currentSeconds
= seconds
.getSelection();
101 refreshCurrentTime();
104 seconds
.setBounds(5, 25, 110, 25);
106 Label label
= new Label(group
, SWT
.LEFT
);
107 label
.setText(SECONDS_LABEL
);
108 label
.setBounds(120, 28, 25, 22);
110 nanosec
= new Spinner(group
, SWT
.BORDER
);
111 nanosec
.addModifyListener(new ModifyListener() {
112 public void modifyText(ModifyEvent e
) {
113 currentNanosec
= nanosec
.getSelection();
114 // Correct for nanosec underflow
115 if (currentNanosec
< 0) {
117 currentNanosec
= NS_PER_SECOND
- 1;
119 // Correct for nanosec overflow
120 if (currentNanosec
>= NS_PER_SECOND
) {
124 refreshCurrentTime();
127 nanosec
.setBounds(150, 25, 110, 25);
129 label
= new Label(group
, SWT
.LEFT
);
130 label
.setText(NANOSEC_LABEL
);
131 label
.setBounds(265, 28, 25, 22);
133 setContent(range
, current
);
136 private void refreshCurrentTime() {
137 long newCurrentTime
= ((long) currentSeconds
) * NS_PER_SECOND
+ currentNanosec
;
138 TmfTimestamp ts
= new TmfTimestamp(newCurrentTime
, NS_SCALING_FACTOR
, 0);
140 fOwner
.synchTimeFrameWidgets(this);
143 // ====================================================================
145 // ====================================================================
147 public TmfTimestamp
getStartTime() {
151 public TmfTimestamp
getEndTime() {
155 public TmfTimestamp
getCurrentTime() {
159 public TmfTimestamp
getSpan() {
160 TmfTimestamp span
= new TmfTimestamp(startTime
.getAdjustment(endTime
), NS_SCALING_FACTOR
, 0);
164 public TmfTimeRange
getTimeRange() {
165 TmfTimeRange range
= new TmfTimeRange(startTime
, endTime
);
169 public void setStartTime(TmfTimestamp ts
) {
171 startTime
= ts
.synchronize(0, NS_SCALING_FACTOR
);
172 startSeconds
= (int) (startTime
.getValue() / NS_PER_SECOND
);
173 startNanosec
= (int) (startTime
.getValue() % NS_PER_SECOND
);
175 catch (ArithmeticException e
) {
179 public void setEndTime(TmfTimestamp ts
) {
181 endTime
= ts
.synchronize(0, NS_SCALING_FACTOR
);
182 endSeconds
= (int) (endTime
.getValue() / NS_PER_SECOND
);
183 endNanosec
= (int) (endTime
.getValue() % NS_PER_SECOND
);
185 catch (ArithmeticException e
) {
189 public void setCurrentTime(TmfTimestamp ts
) {
191 currentTime
= ts
.synchronize(0, NS_SCALING_FACTOR
);
192 currentSeconds
= (int) (currentTime
.getValue() / NS_PER_SECOND
);
193 currentNanosec
= (int) (currentTime
.getValue() % NS_PER_SECOND
);
195 catch (ArithmeticException e
) {
199 // ====================================================================
201 // ====================================================================
204 * <b><u>setContent</u></b>
206 * <li>validates that [startTime <= currentTime <= endTime] is respected
207 * <li>sets the start/current/end time and update the spinners
214 public void setContent(TmfTimeRange range
, TmfTimestamp current
) {
217 // Extract the time range
218 TmfTimestamp start
= range
.getStartTime();
219 TmfTimestamp end
= range
.getEndTime();
221 // Assume start time is OK
224 // Make sure end time >= start time
225 if (end
.compareTo(start
, false) < 0) {
230 // Make sure [start time <= current time <= end time]
231 // If not: current = min(max(start, current), end);
232 if (current
.compareTo(start
, false) < 0) {
235 if (current
.compareTo(end
, false) > 0) {
239 setCurrentTime(current
);
241 // And configure the spinners
246 * <b><u>setValue</u></b>
248 * <li>validates that [startTime <= currentTime <= endTime] is respected
249 * <li>sets the current time and the spinners
256 public void setValue(TmfTimestamp current
) {
258 // Make sure [start time <= current time <= end time]
259 // If not: current = min(max(start, current), end);
260 if (current
.compareTo(startTime
, false) < 0) {
263 if (current
.compareTo(endTime
, false) > 0) {
266 setCurrentTime(current
);
268 // And configure the spinners
273 * Update the spinners with the new current time value
274 * Perform the update on the UI thread
276 public void updateSpinners() {
278 seconds
.getDisplay().asyncExec(new Runnable() {
280 if (!seconds
.isDisposed() && !nanosec
.isDisposed()) {
281 // If we are on the start second, ensure that [currentNS >= startNS]
282 // If the currentSeconds > startSeconds, set startns to -1 so we can
285 if (currentSeconds
<= startSeconds
) {
286 currentSeconds
= startSeconds
;
287 startns
= startNanosec
;
288 if (currentNanosec
< startns
) {
289 currentNanosec
= startns
;
293 // If we are on the end second, ensure that [currentNS <= endNS]
294 // If the currentSeconds < endSeconds, set endns to MAX so we can
296 int endns
= NS_PER_SECOND
;
297 if (currentSeconds
>= endSeconds
) {
298 currentSeconds
= endSeconds
;
300 if (currentNanosec
> endns
) {
301 currentNanosec
= endns
;
305 // Refresh the spinners (value, range, increments, ...)
306 // To ensure that the spinners are properly set, the range has to be > 0
307 seconds
.setValues(currentSeconds
, startSeconds
- 1, endSeconds
+ 1, 0, 1, 10);
308 nanosec
.setValues(currentNanosec
, startns
- 1, endns
+ 1, 0, 1, 1000000);
310 // If start == end (i.e. no range), disable the spinner
311 // (if start == end, the spinner widget range is set to [0..100] by default)
312 seconds
.setEnabled(startSeconds
!= endSeconds
);
313 nanosec
.setEnabled(startns
!= endns
);
This page took 0.039342 seconds and 6 git commands to generate.