Skip to main content
summaryrefslogtreecommitdiffstats
blob: be2a5e76226a8ce71d33e68872344462617a550e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*******************************************************************************
 * Copyright (c) 2009 Ericsson
 * 
 * All rights reserved. This program and the accompanying materials are
 * made available under the terms of the Eclipse Public License v1.0 which
 * accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *   Francois Chouinard - Initial API and implementation
 *******************************************************************************/

package org.eclipse.linuxtools.lttng.ui.views.timeframe;

import org.eclipse.linuxtools.tmf.event.TmfTimeRange;
import org.eclipse.linuxtools.tmf.event.TmfTimestamp;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Spinner;

// ========================================================================
// SpinnerGroup
// ========================================================================

/**
 * <b><u>SpinnerGroup</u></b>
 * <p>
 * A SpinnerGroup holds two coordinated spinners (for seconds and
 * nanoseconds) representing the current time within the trace.
 * <p>
 * The current time can take any value anything within the time range (start
 * and end time).
 */
public class SpinnerGroup {

    // The nanosecond scale (10^9)
    private static final int  NS_PER_SECOND = 1000 * 1000 * 1000;
    private static final byte NS_SCALING_FACTOR = -9;

    // Labels
    private static final String SECONDS_LABEL = "sec";
    private static final String NANOSEC_LABEL = "ns";

    // Widgets
    private Group group;
    private Spinner seconds;
    private Spinner nanosec;

    // The valid time range - start time
    private TmfTimestamp startTime;
    private int startSeconds;
    private int startNanosec;

    // The valid time range - end time
    private TmfTimestamp endTime;
    private int endSeconds;
    private int endNanosec;

    // The current time value
    private TmfTimestamp currentTime;
    private int currentSeconds;
    private int currentNanosec;
    
	@SuppressWarnings("unused")
	private TimeFrameView fOwner;

    /**
     * <b><u>Constructor</u></b>
     * <p>
     * <li>Creates the display group and formats it for the grid cell
     * <li>Sets the initial values for Start/End/Current time
     * </li>
     * <p>
     * @param parent    - the parent Composite
     * @param groupName - the group name
     * @param range     - the valid time range (start/end time)
     * @param current   - the current time
     */
    public SpinnerGroup(TimeFrameView owner, Composite parent, String groupName, TmfTimeRange range, TmfTimestamp current) {

    	fOwner = owner;

        // Create the group
        group = new Group(parent, SWT.BORDER);
        group.setText(groupName);

        // Make it use the whole grid cell
        GridData gridData = new GridData(SWT.LEFT, SWT.TOP, true, false);
        gridData.horizontalAlignment = SWT.FILL;
        group.setLayoutData(gridData);

        // Create and position the widgets
        seconds = new Spinner(group, SWT.BORDER);
        seconds.addModifyListener(new ModifyListener() {
            public void modifyText(ModifyEvent e) {
                currentSeconds = seconds.getSelection();
                refreshCurrentTime();
            }
        });
        seconds.setBounds(5, 25, 110, 25);

        Label label = new Label(group, SWT.LEFT);
        label.setText(SECONDS_LABEL);
        label.setBounds(120, 28, 25, 22);

        nanosec = new Spinner(group, SWT.BORDER);
        nanosec.addModifyListener(new ModifyListener() {
            public void modifyText(ModifyEvent e) {
                currentNanosec = nanosec.getSelection();
                // Correct for nanosec underflow
                if (currentNanosec < 0) {
                    currentSeconds--;
                    currentNanosec = NS_PER_SECOND - 1;
                }
                // Correct for nanosec overflow
                if (currentNanosec >= NS_PER_SECOND) {
                    currentSeconds++;
                    currentNanosec = 0;
                }
                refreshCurrentTime();
            }
        });
        nanosec.setBounds(150, 25, 110, 25);

        label = new Label(group, SWT.LEFT);
        label.setText(NANOSEC_LABEL);
        label.setBounds(265, 28, 25, 22);

        setContent(range, current);
    }

    private void refreshCurrentTime() {
        long newCurrentTime = ((long) currentSeconds) * NS_PER_SECOND + currentNanosec;
        TmfTimestamp ts = new TmfTimestamp(newCurrentTime, NS_SCALING_FACTOR, 0);
        currentTime = ts;
//        fOwner.synchTimeFrameWidgets(this);
    }

    // ====================================================================
    // Get/Set
    // ====================================================================

    public TmfTimestamp getStartTime() {
        return startTime;
    }

    public TmfTimestamp getEndTime() {
        return endTime;
    }

    public TmfTimestamp getCurrentTime() {
        return currentTime;
    }

    public TmfTimestamp getSpan() {
        TmfTimestamp span = new TmfTimestamp(startTime.getAdjustment(endTime, NS_SCALING_FACTOR), NS_SCALING_FACTOR, 0);
        return span;
    }

    public TmfTimeRange getTimeRange() {
        TmfTimeRange range = new TmfTimeRange(startTime, endTime);
        return range;
    }

    public void setStartTime(TmfTimestamp ts) {
    	try {
    		startTime = ts.synchronize(0, NS_SCALING_FACTOR);
    		startSeconds = (int) (startTime.getValue() / NS_PER_SECOND);
    		startNanosec = (int) (startTime.getValue() % NS_PER_SECOND);
    	}
    	catch (ArithmeticException e) {
    	}
    }

    public void setEndTime(TmfTimestamp ts) {
    	try {
    		endTime = ts.synchronize(0, NS_SCALING_FACTOR);
    		endSeconds = (int) (endTime.getValue() / NS_PER_SECOND);
    		endNanosec = (int) (endTime.getValue() % NS_PER_SECOND);
    	}
    	catch (ArithmeticException e) {
    	}
    }

    public void setCurrentTime(TmfTimestamp ts) {
    	try {
    		currentTime = ts.synchronize(0, NS_SCALING_FACTOR);
    		currentSeconds = (int) (currentTime.getValue() / NS_PER_SECOND);
    		currentNanosec = (int) (currentTime.getValue() % NS_PER_SECOND);
    	}
    	catch (ArithmeticException e) {
    	}
    }

    // ====================================================================
    // Operators
    // ====================================================================

    /**
     * <b><u>setContent</u></b>
     * <p>
     * <li>validates that [startTime <= currentTime <= endTime] is respected
     * <li>sets the start/current/end time and update the spinners
     * </li>
     * <p>
     * 
     * @param range
     * @param current
     */
    public void setContent(TmfTimeRange range, TmfTimestamp current) {

    	if (range != null) {
        	// Extract the time range
            TmfTimestamp start = range.getStartTime();
            TmfTimestamp end   = range.getEndTime();

            // Assume start time is OK
            setStartTime(start);

            // Make sure end time >= start time
            if (end.compareTo(start, false) < 0) {
                end = start;
            }
            setEndTime(end);

            // Make sure [start time <= current time <= end time]
            // If not: current = min(max(start, current), end);
            if (current.compareTo(start, false) < 0) {
                current = start;
            }
            if (current.compareTo(end, false) > 0) {
                current = end;
            }
    	}
        setCurrentTime(current);

        // And configure the spinners
        updateSpinners();
    }

    /**
     * <b><u>setValue</u></b>
     * <p>
     * <li>validates that [startTime <= currentTime <= endTime] is respected
     * <li>sets the current time and the spinners
     * </li>
     * <p>
     * 
     * @param range
     * @param current
     */
    public void setValue(TmfTimestamp current) {

        // Make sure [start time <= current time <= end time]
        // If not: current = min(max(start, current), end);
        if (current.compareTo(startTime, false) < 0) {
            current = startTime;
        }
        if (current.compareTo(endTime, false) > 0) {
            current = endTime;
        }
        setCurrentTime(current);

        // And configure the spinners
        updateSpinners();
    }

    /**
     * Update the spinners with the new current time value
     * Perform the update on the UI thread
     */
    public void updateSpinners() {

        seconds.getDisplay().asyncExec(new Runnable() {
			public void run() {
				if (!seconds.isDisposed() && !nanosec.isDisposed()) {
		            // If we are on the start second, ensure that [currentNS >= startNS]
		            // If the currentSeconds > startSeconds, set startns to -1 so we can
		            // "underflow"
		            int startns = -1;
		            if (currentSeconds <= startSeconds) {
		                currentSeconds = startSeconds;
		                startns = startNanosec;
		                if (currentNanosec < startns) {
		                    currentNanosec = startns;
		                }
		            }

		            // If we are on the end second, ensure that [currentNS <= endNS]
		            // If the currentSeconds < endSeconds, set endns to MAX so we can
		            // "overflow"
		            int endns = NS_PER_SECOND;
		            if (currentSeconds >= endSeconds) {
		                currentSeconds = endSeconds;
		                endns = endNanosec;
		                if (currentNanosec > endns) {
		                    currentNanosec = endns;
		                }
		            }

		            // Refresh the spinners (value, range, increments, ...)
					// To ensure that the spinners are properly set, the range has to be > 0 
//					seconds.setValues(currentSeconds, startSeconds - 1, endSeconds + 1, 0, 1, 10);
//					nanosec.setValues(currentNanosec, startns - 1, endns + 1, 0, 1, 1000000);
					seconds.setValues(currentSeconds, startSeconds, endSeconds, 0, 1, 10);
					nanosec.setValues(currentNanosec, startns, endns, 0, 100000, 10000000);

		            // If start == end (i.e. no range), disable the spinner
		            // (if start == end, the spinner widget range is set to [0..100] by default)
		            seconds.setEnabled(startSeconds != endSeconds);
		            nanosec.setEnabled(startns != endns);
				}
			}
        });
    }
}

Back to the top