Skip to main content
summaryrefslogtreecommitdiffstats
blob: dd234847e1f63ee2cd5788b2947e44c16eb48bb1 (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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*******************************************************************************
 * Copyright (c) 2009 2010 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.lttng.event.LttngEvent;
import org.eclipse.linuxtools.tmf.event.TmfTimeRange;
import org.eclipse.linuxtools.tmf.event.TmfTimestamp;
import org.eclipse.linuxtools.tmf.experiment.TmfExperiment;
import org.eclipse.linuxtools.tmf.signal.TmfExperimentSelectedSignal;
import org.eclipse.linuxtools.tmf.signal.TmfExperimentUpdatedSignal;
import org.eclipse.linuxtools.tmf.signal.TmfRangeSynchSignal;
import org.eclipse.linuxtools.tmf.signal.TmfSignalHandler;
import org.eclipse.linuxtools.tmf.signal.TmfTimeSynchSignal;
import org.eclipse.linuxtools.tmf.ui.views.TmfView;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Slider;

/**
 * <b><u>TimeFrameView</u></b>
 * <p>
 * The TimeFrameView provides a set of spinners to monitor and set the start
 * time, end time, the current time interval and current time of the trace
 * set at the nanosecond level.
 * <p>
 * It ensures that the following relations are always true:
 * <p>
 * <li>[ startTime >= start time of the trace ]
 * <li>[ endTime <= end time of the trace ]
 * <li>[ startTime <= currentTime <= endTime ]
 * <li>[ interval == (endTime - startTime) ]
 * </li>
 * <p>
 * It provides a slider to rapidly set the current time within the time range
 * (i.e. between startTime and endTime).
 * <p>
 * Finally, it allows modification of the time range and the current time. This
 * triggers notifications to the other LTTng views.
 * <p>
 * FIXME: The slider is very jumpy due to the large number of async updates
 * FIXME: Revisit the control flow between View, Spinners and Slider
 */
public class TimeFrameView extends TmfView {

    public static final String ID = "org.eclipse.linuxtools.lttng.ui.views.timeframe";

    // ========================================================================
    // TimeFrameView
    // ========================================================================

    // The event log timestamp characteristics
    private TmfTimestamp fTraceStartTime = new TmfTimestamp();
    private TmfTimestamp fTraceEndTime   = new TmfTimestamp();

    private TmfTimestamp fCurrentTime   = new TmfTimestamp();
    
    private TmfTimeRange fTraceTimeRange = new TmfTimeRange(fTraceStartTime, fTraceEndTime);
    private TmfTimeRange fTraceSpan      = new TmfTimeRange(fTraceStartTime, fTraceEndTime);
    private byte fScale = 0;

    // Labels
    private static final String START_TIME_LABEL   = "Window Start Time";
    private static final String END_TIME_LABEL     = "Window End Time";
    private static final String TIME_RANGE_LABEL   = "Window Range";
    private static final String CURRENT_TIME_LABEL = "Current Time";

    private static final int SLIDER_RANGE = 10000;

    private SpinnerGroup fStartGroup;
    private SpinnerGroup fEndGroup;
    private SpinnerGroup fRangeGroup;
    private SpinnerGroup fCurrentGroup;

    // The slider
    private Slider fSlider;

    // The current experiment
    TmfExperiment<LttngEvent> fExperiment = null;
	// notify external listeners may not be needed if the update originated
	// externally
	private boolean fupdateExternalListeners = true;


    /**
     * Constructor
     */
    public TimeFrameView() {
    	super("TimeFrameView");
    }

    /* (non-Javadoc)
     * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
     */
	@Override
	public void createPartControl(Composite parent) {

        // Set the view layout
        GridLayout layout = new GridLayout(4, true);
        parent.setLayout(layout);

        fStartGroup   = new SpinnerGroup(this, parent, START_TIME_LABEL,   fTraceTimeRange, fTraceStartTime);
        fEndGroup     = new SpinnerGroup(this, parent, END_TIME_LABEL,     fTraceTimeRange, fTraceEndTime);
        fRangeGroup   = new SpinnerGroup(this, parent, TIME_RANGE_LABEL,   fTraceTimeRange, fTraceEndTime);
        fCurrentGroup = new SpinnerGroup(this, parent, CURRENT_TIME_LABEL, fTraceTimeRange, fTraceStartTime);

        // Create the slider
        createSlider(parent);
    }

    /* (non-Javadoc)
     * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
     */
	@Override
	public void setFocus() {
        // TODO Auto-generated method stub
    }

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

    /**
     * One of the spinners has been updated. Synchronize the other widgets.
     */
    public void synchTimeFrameWidgets(SpinnerGroup trigger) {
		boolean trangeUpdated = false;

        // Collect the data
        TmfTimestamp startTime   = fStartGroup.getCurrentTime();
        TmfTimestamp endTime     = fEndGroup.getCurrentTime();
        TmfTimestamp timeRange   = fRangeGroup.getCurrentTime();
        TmfTimestamp currentTime = fCurrentGroup.getCurrentTime();

        // If startTime was set beyond endTime, adjust endTime and interval
        if (trigger == fStartGroup) {
            if (startTime.compareTo(endTime, false) > 0) {
                endTime = startTime;
				trangeUpdated = true;
            }
        }

        // If endTime was set beyond startTime, adjust startTime and interval
        if (trigger == fEndGroup) {
            if (endTime.compareTo(startTime, false) < 0) {
                startTime = endTime;
				trangeUpdated = true;
            }
        }

        // If timeRange was set, adjust endTime
        if (trigger == fRangeGroup) {
            long start = startTime.getValue();
            long span  = timeRange.getValue();
            TmfTimestamp ts = new TmfTimestamp(start + span, startTime.getScale(), 0);
            if (ts.compareTo(fTraceEndTime, false) > 0) {
                ts = fTraceEndTime.synchronize(fTraceEndTime.getValue(), startTime.getScale());
            }
            endTime = ts;
			trangeUpdated = true;
        }

        // Compute the new time range
        TmfTimeRange subrange = new TmfTimeRange(startTime, endTime);
        byte scale = startTime.getScale();
        TmfTimestamp interval = new TmfTimestamp(startTime.getAdjustment(endTime, scale), scale, 0);

        // Update the spinner groups
        fStartGroup.setContent(fTraceTimeRange, startTime);
        fEndGroup.setContent(fTraceTimeRange, endTime);
        fRangeGroup.setContent(fTraceSpan, interval);
        fCurrentGroup.setContent(subrange, currentTime);

        updateSlider(subrange, currentTime);
		// Notify other views, only if the update originated from this view
		if (fupdateExternalListeners) {
			if (!fCurrentTime.equals(currentTime)) {
				fCurrentTime = currentTime;
				broadcast(new TmfTimeSynchSignal(this, currentTime));
			}

			// Notify the views if the time range has been impacted
			if (trangeUpdated) {
				TmfTimeRange trange = new TmfTimeRange(startTime, endTime);
				broadcast(new TmfRangeSynchSignal(this, trange, currentTime));
			}
        }
    }

    // ========================================================================
    // Slider Handling
    // ========================================================================

    /**
     * @param parent
     */
    private void createSlider(Composite parent) {
        fSlider = new Slider(parent, SWT.SMOOTH | SWT.FILL);
        fSlider.setMinimum(0);
        fSlider.setMaximum(SLIDER_RANGE + fSlider.getThumb());
        fSlider.setIncrement(SLIDER_RANGE / 100);
        fSlider.setPageIncrement(SLIDER_RANGE / 10);
        fSlider.setSelection(0);

        GridData gridData = new GridData(SWT.LEFT, SWT.TOP, true, false);
        gridData.horizontalAlignment = SWT.FILL;
        gridData.horizontalSpan = 4;
        fSlider.setLayoutData(gridData);

        fSlider.addListener(SWT.Selection, new Listener() {
        	public void handleEvent(Event event) {
				int ratio = fSlider.getSelection();
				TmfTimestamp span = fCurrentGroup.getSpan();
				long value = span.getValue() * ratio / SLIDER_RANGE;
				TmfTimestamp start = fCurrentGroup.getStartTime();
				TmfTimestamp current = new TmfTimestamp(start.getValue() + value, start.getScale(), 0);
				fCurrentGroup.setValue(current);
			}
        });

    }

    /**
     * @param range
     * @param timestamp
     */
    private void updateSlider(TmfTimeRange range, TmfTimestamp timestamp) {

        // Determine the new relative position
    	byte scale = range.getEndTime().getScale();
        long total    = range.getStartTime().getAdjustment(range.getEndTime(), scale);
        long relative = range.getStartTime().getAdjustment(timestamp, scale);

        // Set the slider value
        final long position = (total > 0) ? (relative * SLIDER_RANGE / total) : 0;

        // Update the slider on the UI thread
        long current = fSlider.getSelection();
        if (position != current) {
        	fSlider.getDisplay().asyncExec(new Runnable() {
        		public void run() {
        			fSlider.setSelection((int) position);
        		}
        	});
        }
    }

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "[TimeFrameView]";
	}

    // ========================================================================
    // TMF Signal Handling
    // ========================================================================

	/**
     * @param signal
     */
    @SuppressWarnings("unchecked")
    @TmfSignalHandler
    public void experimentSelected(TmfExperimentSelectedSignal<LttngEvent> signal) {

        // Update the trace reference
        fExperiment = (TmfExperiment<LttngEvent>) signal.getExperiment();

        // Update the time frame
        fTraceTimeRange = fExperiment.getTimeRange();
        fTraceStartTime = fTraceTimeRange.getStartTime();
        fTraceEndTime   = fTraceTimeRange.getEndTime();
        fScale          = fTraceStartTime.getScale();

        // Update the widgets
        fStartGroup.setContent(fTraceTimeRange, fTraceStartTime);
        fEndGroup.setContent(fTraceTimeRange, fTraceEndTime);
        fCurrentGroup.setContent(fTraceTimeRange, fTraceStartTime);

        fCurrentTime = fTraceStartTime;

        TmfTimestamp delta = new TmfTimestamp(fTraceStartTime.getAdjustment(fTraceEndTime, fScale), fScale, 0);
        fTraceSpan = new TmfTimeRange(new TmfTimestamp(0, fScale, 0), delta);
//        fRangeGroup.setContent(fTraceSpan, delta);
        TmfTimestamp start = new TmfTimestamp(1, (byte) -1, 0);
        fRangeGroup.setContent(fTraceSpan, start);
    }

    /**
     * @param signal
     */
    @TmfSignalHandler
    public void experimentUpdated(TmfExperimentUpdatedSignal signal) {

        // Update the time frame
//      fTraceTimeRange = signal.getTrace().getTimeRange();
       	fTraceTimeRange = signal.getExperiment().getTimeRange();
        fTraceStartTime = fTraceTimeRange.getStartTime();
        fTraceEndTime   = fTraceTimeRange.getEndTime();
        fScale          = fTraceStartTime.getScale();

        // Update the widgets
        fStartGroup.setContent(fTraceTimeRange, fStartGroup.getCurrentTime());
        fEndGroup.setContent(fTraceTimeRange, fTraceEndTime);
        fCurrentGroup.setContent(fTraceTimeRange, fCurrentGroup.getCurrentTime());

        TmfTimestamp delta = new TmfTimestamp(fTraceStartTime.getAdjustment(fTraceEndTime, fScale), fScale, 0);
        fTraceSpan = new TmfTimeRange(new TmfTimestamp(0, fScale, 0), delta);
        fRangeGroup.setContent(fTraceSpan, delta);
    }

	/**
	 * @param signal
	 */
	@TmfSignalHandler
	public void currentTimeRangeUpdated(TmfRangeSynchSignal signal) {
		if (signal.getSource() != this) {
			// Update the time frame
			TmfTimeRange selTimeRange = signal.getCurrentRange();
			TmfTimestamp selStart = selTimeRange.getStartTime().synchronize(0,
					fScale);
			TmfTimestamp selEnd = selTimeRange.getEndTime().synchronize(0,
					fScale);

			fupdateExternalListeners = false;
			// Update the widgets and prevent broadcast notifications to
			// the views which have been notified already.
			{
				fStartGroup.setContent(fTraceTimeRange, selStart);
				fEndGroup.setContent(fTraceTimeRange, selEnd);

				TmfTimestamp delta = new TmfTimestamp(selStart.getAdjustment(
						selEnd, fScale), fScale, 0);

				fRangeGroup.setContent(fTraceSpan, delta);
			}

			// restore the external notification flag
			fupdateExternalListeners = true;

		}
	}

    /**
     * @param signal
     */
    @TmfSignalHandler
    public void currentTimeUpdated(TmfTimeSynchSignal signal) {
    	if (signal.getSource() != this) {
			// prevent loop to external notifications
			fupdateExternalListeners = false;
            fCurrentTime = signal.getCurrentTime().synchronize(0, fStartGroup.getCurrentTime().getScale());
            if (fStartGroup.getCurrentTime().compareTo(fCurrentTime, false) > 0) {
            	fStartGroup.setContent(new TmfTimeRange(fCurrentTime, fEndGroup.getCurrentTime()), fCurrentTime);
            }
            if (fEndGroup.getCurrentTime().compareTo(fCurrentTime, false) < 0) {
            	fEndGroup.setContent(new TmfTimeRange(fStartGroup.getCurrentTime(), fCurrentTime), fCurrentTime);
            }
            fCurrentGroup.setContent(null, fCurrentTime);
            updateSlider(fCurrentGroup.getTimeRange(), fCurrentTime);

			// Enable external notifications
			fupdateExternalListeners = true;
    	}
    }

}

Back to the top