Fix for Bug338016
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / viewers / events / TmfEventsTable.java
1 /*******************************************************************************
2 * Copyright (c) 2010 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
11 * Patrick Tasse - Factored out from events view
12 * Francois Chouinard - Replaced Table by TmfVirtualTable
13 *******************************************************************************/
14
15 package org.eclipse.linuxtools.tmf.ui.viewers.events;
16
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.core.runtime.Status;
20 import org.eclipse.core.runtime.jobs.Job;
21 import org.eclipse.linuxtools.tmf.component.ITmfDataProvider;
22 import org.eclipse.linuxtools.tmf.component.TmfComponent;
23 import org.eclipse.linuxtools.tmf.event.TmfEvent;
24 import org.eclipse.linuxtools.tmf.event.TmfTimestamp;
25 import org.eclipse.linuxtools.tmf.experiment.TmfExperiment;
26 import org.eclipse.linuxtools.tmf.request.ITmfDataRequest.ExecutionType;
27 import org.eclipse.linuxtools.tmf.request.TmfDataRequest;
28 import org.eclipse.linuxtools.tmf.signal.TmfExperimentUpdatedSignal;
29 import org.eclipse.linuxtools.tmf.signal.TmfRangeSynchSignal;
30 import org.eclipse.linuxtools.tmf.signal.TmfSignalHandler;
31 import org.eclipse.linuxtools.tmf.signal.TmfTimeSynchSignal;
32 import org.eclipse.linuxtools.tmf.signal.TmfTraceUpdatedSignal;
33 import org.eclipse.linuxtools.tmf.trace.ITmfTrace;
34 import org.eclipse.linuxtools.tmf.ui.TmfUiPlugin;
35 import org.eclipse.linuxtools.tmf.ui.internal.Messages;
36 import org.eclipse.linuxtools.tmf.ui.widgets.ColumnData;
37 import org.eclipse.linuxtools.tmf.ui.widgets.TmfVirtualTable;
38 import org.eclipse.swt.SWT;
39 import org.eclipse.swt.events.SelectionAdapter;
40 import org.eclipse.swt.events.SelectionEvent;
41 import org.eclipse.swt.layout.GridData;
42 import org.eclipse.swt.widgets.Composite;
43 import org.eclipse.swt.widgets.Event;
44 import org.eclipse.swt.widgets.Listener;
45 import org.eclipse.swt.widgets.TableColumn;
46 import org.eclipse.swt.widgets.TableItem;
47
48 /**
49 * <b><u>TmfEventsTable</u></b>
50 */
51 public class TmfEventsTable extends TmfComponent {
52
53 // ------------------------------------------------------------------------
54 // Table data
55 // ------------------------------------------------------------------------
56
57 protected TmfVirtualTable fTable;
58 protected ITmfTrace fTrace;
59 protected boolean fPackDone = false;
60
61 // Table column names
62 static private final String[] COLUMN_NAMES = new String[] {
63 Messages.TmfEventsTable_TimestampColumnHeader,
64 Messages.TmfEventsTable_SourceColumnHeader,
65 Messages.TmfEventsTable_TypeColumnHeader,
66 Messages.TmfEventsTable_ReferenceColumnHeader,
67 Messages.TmfEventsTable_ContentColumnHeader
68 };
69
70 static private ColumnData[] COLUMN_DATA = new ColumnData[] {
71 new ColumnData(COLUMN_NAMES[0], 100, SWT.LEFT),
72 new ColumnData(COLUMN_NAMES[1], 100, SWT.LEFT),
73 new ColumnData(COLUMN_NAMES[2], 100, SWT.LEFT),
74 new ColumnData(COLUMN_NAMES[3], 100, SWT.LEFT),
75 new ColumnData(COLUMN_NAMES[4], 100, SWT.LEFT)
76 };
77
78 // ------------------------------------------------------------------------
79 // Event cache
80 // ------------------------------------------------------------------------
81
82 private final int fCacheSize;
83 private TmfEvent[] fCache;
84 private int fCacheStartIndex = 0;
85 private int fCacheEndIndex = 0;
86
87 private boolean fDisposeOnClose;
88
89 // ------------------------------------------------------------------------
90 // Constructor
91 // ------------------------------------------------------------------------
92
93 public TmfEventsTable(Composite parent, int cacheSize) {
94 this(parent, cacheSize, COLUMN_DATA);
95 }
96
97 public TmfEventsTable(Composite parent, int cacheSize, ColumnData[] columnData) {
98 super("TmfEventsTable"); //$NON-NLS-1$
99
100 fCacheSize = cacheSize;
101 fCache = new TmfEvent[fCacheSize];
102
103 // Create a virtual table
104 final int style = SWT.SINGLE | SWT.FULL_SELECTION | SWT.BORDER;
105 fTable = new TmfVirtualTable(parent, style);
106
107 // Set the table layout
108 GridData layoutData = new GridData(SWT.FILL, SWT.FILL, true, true);
109 fTable.setLayoutData(layoutData);
110
111 // Some cosmetic enhancements
112 fTable.setHeaderVisible(true);
113 fTable.setLinesVisible(true);
114
115 // Set the columns
116 setColumnHeaders(columnData);
117
118 // Handle the table item requests
119 fTable.addSelectionListener(new SelectionAdapter() {
120 @Override
121 public void widgetSelected(SelectionEvent e) {
122 TmfTimestamp ts = (TmfTimestamp) fTable.getSelection()[0].getData();
123 broadcast(new TmfTimeSynchSignal(fTable, ts));
124 }
125 });
126
127 // Handle the table item requests
128 fTable.addListener(SWT.SetData, new Listener() {
129
130 @Override
131 public void handleEvent(Event event) {
132
133 final TableItem item = (TableItem) event.item;
134 final int index = fTable.indexOf(item);
135
136 // If available, return the cached data
137 if ((index >= fCacheStartIndex) && (index < fCacheEndIndex)) {
138 int i = index - fCacheStartIndex;
139 item.setText(extractItemFields(fCache[i]));
140 item.setData(new TmfTimestamp(fCache[i].getTimestamp()));
141 return;
142 }
143
144 // Else, fill the cache asynchronously (and off the UI thread)
145 populateCache(index);
146 }
147 });
148
149 fTable.setItemCount(0);
150 }
151
152 @Override
153 public void dispose() {
154 fTable.dispose();
155 if (fTrace != null && fDisposeOnClose) {
156 fTrace.dispose();
157 }
158 super.dispose();
159 }
160
161 public TmfVirtualTable getTable() {
162 return fTable;
163 }
164
165 /**
166 * @param table
167 *
168 * FIXME: Add support for column selection
169 */
170 protected void setColumnHeaders(ColumnData[] columnData) {
171 fTable.setColumnHeaders(columnData);
172 }
173
174 protected void packColumns() {
175 if (fPackDone) return;
176 for (TableColumn column : fTable.getColumns()) {
177 int headerWidth = column.getWidth();
178 column.pack();
179 if (column.getWidth() < headerWidth) {
180 column.setWidth(headerWidth);
181 }
182 }
183 fPackDone = true;
184 }
185
186 /**
187 * @param event
188 * @return
189 *
190 * FIXME: Add support for column selection
191 */
192 protected String[] extractItemFields(TmfEvent event) {
193 String[] fields = new String[0];
194 if (event != null) {
195 fields = new String[] {
196 new Long(event.getTimestamp().getValue()).toString(),
197 event.getSource().getSourceId().toString(),
198 event.getType().getTypeId().toString(),
199 event.getReference().getReference().toString(),
200 event.getContent().toString()
201 };
202 }
203 return fields;
204 }
205
206 public void setFocus() {
207 fTable.setFocus();
208 }
209
210 /**
211 * @param trace
212 * @param disposeOnClose true if the trace should be disposed when the table is disposed
213 */
214 public void setTrace(ITmfTrace trace, boolean disposeOnClose) {
215 if (fTrace != null && fDisposeOnClose) {
216 fTrace.dispose();
217 }
218 fTrace = trace;
219 fDisposeOnClose = disposeOnClose;
220
221 // Perform the updates on the UI thread
222 fTable.getDisplay().syncExec(new Runnable() {
223 @Override
224 public void run() {
225 //fTable.setSelection(0);
226 fTable.removeAll();
227 fCacheStartIndex = fCacheEndIndex = 0; // Clear the cache
228
229 if (!fTable.isDisposed() && fTrace != null) {
230 //int nbEvents = (int) fTrace.getNbEvents();
231 //fTable.setItemCount((nbEvents > 100) ? nbEvents : 100);
232 fTable.setItemCount((int) fTrace.getNbEvents());
233 }
234 }
235 });
236 }
237
238 // ------------------------------------------------------------------------
239 // Signal handlers
240 // ------------------------------------------------------------------------
241
242 @TmfSignalHandler
243 public void experimentUpdated(TmfExperimentUpdatedSignal signal) {
244 if ((signal.getExperiment() != fTrace) || fTable.isDisposed()) return;
245 // Perform the refresh on the UI thread
246 fTable.getDisplay().asyncExec(new Runnable() {
247 @Override
248 public void run() {
249 if (!fTable.isDisposed() && fTrace != null) {
250 fTable.setItemCount((int) fTrace.getNbEvents());
251 fTable.refresh();
252 }
253 }
254 });
255 }
256
257 @TmfSignalHandler
258 public void traceUpdated(TmfTraceUpdatedSignal signal) {
259 if ((signal.getTrace() != fTrace ) || fTable.isDisposed()) return;
260 // Perform the refresh on the UI thread
261 fTable.getDisplay().asyncExec(new Runnable() {
262 @Override
263 public void run() {
264 if (!fTable.isDisposed() && fTrace != null) {
265 //int nbEvents = (int) fTrace.getNbEvents();
266 //fTable.setItemCount((nbEvents > 100) ? nbEvents : 100);
267 fTable.setItemCount((int) fTrace.getNbEvents());
268 }
269 }
270 });
271 }
272
273 private boolean fRefreshPending = false;
274 @TmfSignalHandler
275 public synchronized void rangeSynched(TmfRangeSynchSignal signal) {
276 if (!fRefreshPending && !fTable.isDisposed()) {
277 // Perform the refresh on the UI thread
278 fRefreshPending = true;
279 fTable.getDisplay().asyncExec(new Runnable() {
280 @Override
281 public void run() {
282 fRefreshPending = false;
283 if (!fTable.isDisposed() && fTrace != null) {
284 fTable.setItemCount((int) fTrace.getNbEvents());
285 }
286 }
287 });
288 }
289 }
290
291 @TmfSignalHandler
292 public void currentTimeUpdated(final TmfTimeSynchSignal signal) {
293 if ((signal.getSource() != fTable) && (fTrace != null) && (!fTable.isDisposed())) {
294
295 // Create a request for one event that will be queued after other ongoing requests. When this request is completed
296 // do the work to select the actual event with the timestamp specified in the signal. This procedure prevents
297 // the method fTrace.getRank() from interfering and delaying ongoing requests.
298 final TmfDataRequest<TmfEvent> subRequest = new TmfDataRequest<TmfEvent>(TmfEvent.class, 0, 1, ExecutionType.FOREGROUND) {
299
300 @Override
301 public void handleData(TmfEvent event) {
302 super.handleData(event);
303 }
304
305 @Override
306 public void handleCompleted() {
307
308 // Verify if event is within the trace range
309 final TmfTimestamp timestamp[] = new TmfTimestamp[1];
310 timestamp[0] = signal.getCurrentTime();
311 if (timestamp[0].compareTo(fTrace.getStartTime(), true) == -1) {
312 timestamp[0] = fTrace.getStartTime();
313 }
314 if (timestamp[0].compareTo(fTrace.getEndTime(), true) == 1) {
315 timestamp[0] = fTrace.getEndTime();
316 }
317
318 // Get the rank for the event selection in the table
319 final int index = (int) fTrace.getRank(timestamp[0]);
320
321 fTable.getDisplay().asyncExec(new Runnable() {
322 @Override
323 public void run() {
324 // Return if table is disposed
325 if (fTable.isDisposed()) return;
326
327 fTable.setSelection(index);
328
329 // If index is in cache, then notify about updated selection.
330 // Otherwise it's done after fetching the relevant events from the trace
331 if ((index >= fCacheStartIndex) && (index < fCacheEndIndex)) {
332 // Use the timestamp in signal to broadcast to avoid moving the selection
333 // at the source of the signal
334 fTable.notifyUpdatedSelection(timestamp[0]);
335 }
336
337 // The timestamp might not correspond to an actual event
338 // and the selection will point to the next experiment event.
339 // But we would like to display both the event before and
340 // after the selected timestamp.
341 // This works fine by default except when the selected event
342 // is the top displayed event. The following ensures that we
343 // always see both events.
344 if ((index > 0) && (index == fTable.getTopIndex())) {
345 fTable.setTopIndex(index - 1);
346 }
347 }
348 });
349 super.handleCompleted();
350 }
351 };
352
353 @SuppressWarnings("unchecked")
354 TmfExperiment<TmfEvent> experiment = (TmfExperiment<TmfEvent>)TmfExperiment.getCurrentExperiment();
355 if (experiment != null) {
356 experiment.sendRequest(subRequest);
357 }
358 }
359 }
360
361 // ------------------------------------------------------------------------
362 // Event cache population
363 // ------------------------------------------------------------------------
364
365 // The event fetching job
366 private Job job;
367 private synchronized void populateCache(final int index) {
368
369 /* Check if the current job will fetch the requested event:
370 * 1. The job must exist
371 * 2. It must be running (i.e. not completed)
372 * 3. The requested index must be within the cache range
373 *
374 * If the job meets these conditions, we simply exit.
375 * Otherwise, we create a new job but we might have to cancel
376 * an existing job for an obsolete range.
377 */
378 if (job != null) {
379 if (job.getState() != Job.NONE) {
380 if (index >= fCacheStartIndex && index < (fCacheStartIndex + fCacheSize)) {
381 return;
382 }
383 // The new index is out of the requested range
384 // Kill the job and start a new one
385 job.cancel();
386 }
387 }
388
389 fCacheStartIndex = index;
390 fCacheEndIndex = index;
391
392 job = new Job("Fetching Events") { //$NON-NLS-1$
393 @Override
394 @SuppressWarnings("unchecked")
395 protected IStatus run(final IProgressMonitor monitor) {
396
397 TmfDataRequest<TmfEvent> request = new TmfDataRequest<TmfEvent>(TmfEvent.class, index, fCacheSize) {
398 private int count = 0;
399 @Override
400 public void handleData(TmfEvent event) {
401 // If the job is canceled, cancel the request so waitForCompletion() will unlock
402 if (monitor.isCanceled()) {
403 cancel();
404 return;
405 }
406 super.handleData(event);
407 if (event != null) {
408 fCache[count++] = event.clone();
409 fCacheEndIndex++; // TODO: Need to protect this??
410 }
411 }
412 };
413
414 ((ITmfDataProvider<TmfEvent>) fTrace).sendRequest(request);
415 try {
416 request.waitForCompletion();
417 } catch (InterruptedException e) {
418 e.printStackTrace();
419 }
420
421 // Event cache is now updated. Perform update on the UI thread
422 if (!fTable.isDisposed() && !monitor.isCanceled()) {
423 fTable.getDisplay().asyncExec(new Runnable() {
424 @Override
425 public void run() {
426 if (!fTable.isDisposed()) {
427 fTable.refresh();
428 fTable.notifyUpdatedSelection();
429 }
430 }
431 });
432 }
433
434 // Flag the UI thread that the cache is ready
435 if (monitor.isCanceled()) {
436 return new Status(IStatus.CANCEL, TmfUiPlugin.PLUGIN_ID, "Canceled"); //$NON-NLS-1$
437 }
438 else {
439 return new Status(IStatus.OK, TmfUiPlugin.PLUGIN_ID, "Completed"); //$NON-NLS-1$
440 }
441 }
442 };
443 job.setPriority(Job.SHORT);
444 job.schedule();
445 }
446 }
This page took 0.0417 seconds and 6 git commands to generate.