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"; //$NON-NLS-1$
47 private static final String NANOSEC_LABEL
= "ns"; //$NON-NLS-1$
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() {
101 public void modifyText(ModifyEvent e
) {
102 currentSeconds
= seconds
.getSelection();
103 refreshCurrentTime();
106 seconds
.setBounds(5, 25, 110, 25);
108 Label label
= new Label(group
, SWT
.LEFT
);
109 label
.setText(SECONDS_LABEL
);
110 label
.setBounds(120, 28, 25, 22);
112 nanosec
= new Spinner(group
, SWT
.BORDER
);
113 nanosec
.addModifyListener(new ModifyListener() {
115 public void modifyText(ModifyEvent e
) {
116 currentNanosec
= nanosec
.getSelection();
117 // Correct for nanosec underflow
118 if (currentNanosec
< 0) {
120 currentNanosec
= NS_PER_SECOND
- 1;
122 // Correct for nanosec overflow
123 if (currentNanosec
>= NS_PER_SECOND
) {
127 refreshCurrentTime();
130 nanosec
.setBounds(150, 25, 110, 25);
132 label
= new Label(group
, SWT
.LEFT
);
133 label
.setText(NANOSEC_LABEL
);
134 label
.setBounds(265, 28, 25, 22);
136 setContent(range
, current
);
139 private void refreshCurrentTime() {
140 long newCurrentTime
= ((long) currentSeconds
) * NS_PER_SECOND
+ currentNanosec
;
141 TmfTimestamp ts
= new TmfTimestamp(newCurrentTime
, NS_SCALING_FACTOR
, 0);
143 // fOwner.synchTimeFrameWidgets(this);
146 // ====================================================================
148 // ====================================================================
150 public TmfTimestamp
getStartTime() {
154 public TmfTimestamp
getEndTime() {
158 public TmfTimestamp
getCurrentTime() {
162 public TmfTimestamp
getSpan() {
163 TmfTimestamp span
= new TmfTimestamp(startTime
.getAdjustment(endTime
, NS_SCALING_FACTOR
), NS_SCALING_FACTOR
, 0);
167 public TmfTimeRange
getTimeRange() {
168 TmfTimeRange range
= new TmfTimeRange(startTime
, endTime
);
172 public void setStartTime(TmfTimestamp ts
) {
174 startTime
= ts
.synchronize(0, NS_SCALING_FACTOR
);
175 startSeconds
= (int) (startTime
.getValue() / NS_PER_SECOND
);
176 startNanosec
= (int) (startTime
.getValue() % NS_PER_SECOND
);
178 catch (ArithmeticException e
) {
182 public void setEndTime(TmfTimestamp ts
) {
184 endTime
= ts
.synchronize(0, NS_SCALING_FACTOR
);
185 endSeconds
= (int) (endTime
.getValue() / NS_PER_SECOND
);
186 endNanosec
= (int) (endTime
.getValue() % NS_PER_SECOND
);
188 catch (ArithmeticException e
) {
192 public void setCurrentTime(TmfTimestamp ts
) {
194 currentTime
= ts
.synchronize(0, NS_SCALING_FACTOR
);
195 currentSeconds
= (int) (currentTime
.getValue() / NS_PER_SECOND
);
196 currentNanosec
= (int) (currentTime
.getValue() % NS_PER_SECOND
);
198 catch (ArithmeticException e
) {
202 // ====================================================================
204 // ====================================================================
207 * <b><u>setContent</u></b>
209 * <li>validates that [startTime <= currentTime <= endTime] is respected
210 * <li>sets the start/current/end time and update the spinners
217 public void setContent(TmfTimeRange range
, TmfTimestamp current
) {
220 // Extract the time range
221 TmfTimestamp start
= range
.getStartTime();
222 TmfTimestamp end
= range
.getEndTime();
224 // Assume start time is OK
227 // Make sure end time >= start time
228 if (end
.compareTo(start
, false) < 0) {
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) {
238 if (current
.compareTo(end
, false) > 0) {
242 setCurrentTime(current
);
244 // And configure the spinners
249 * <b><u>setValue</u></b>
251 * <li>validates that [startTime <= currentTime <= endTime] is respected
252 * <li>sets the current time and the spinners
259 public void setValue(TmfTimestamp current
) {
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) {
266 if (current
.compareTo(endTime
, false) > 0) {
269 setCurrentTime(current
);
271 // And configure the spinners
276 * Update the spinners with the new current time value
277 * Perform the update on the UI thread
279 public void updateSpinners() {
280 // Ignore update if disposed
281 if (seconds
.isDisposed()) return;
283 seconds
.getDisplay().asyncExec(new Runnable() {
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
291 if (currentSeconds
<= startSeconds
) {
292 currentSeconds
= startSeconds
;
293 startns
= startNanosec
;
294 if (currentNanosec
< startns
) {
295 currentNanosec
= startns
;
299 // If we are on the end second, ensure that [currentNS <= endNS]
300 // If the currentSeconds < endSeconds, set endns to MAX so we can
302 int endns
= NS_PER_SECOND
;
303 if (currentSeconds
>= endSeconds
) {
304 currentSeconds
= endSeconds
;
306 if (currentNanosec
> endns
) {
307 currentNanosec
= endns
;
311 // Refresh the spinners (value, range, increments, ...)
312 // To ensure that the spinners are properly set, the range has to be > 0
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);
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
);
This page took 0.037585 seconds and 6 git commands to generate.