1 /*******************************************************************************
2 * Copyright (c) 2010 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 * Patrick Tasse - Factored out from events view
12 * Francois Chouinard - Replaced Table by TmfVirtualTable
13 *******************************************************************************/
15 package org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.events
;
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
;
49 * <b><u>TmfEventsTable</u></b>
51 public class TmfEventsTable
extends TmfComponent
{
53 // ------------------------------------------------------------------------
55 // ------------------------------------------------------------------------
57 protected TmfVirtualTable fTable
;
58 protected ITmfTrace fTrace
;
59 protected boolean fPackDone
= false;
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
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
)
78 // ------------------------------------------------------------------------
80 // ------------------------------------------------------------------------
82 private final int fCacheSize
;
83 private TmfEvent
[] fCache
;
84 private int fCacheStartIndex
= 0;
85 private int fCacheEndIndex
= 0;
87 private boolean fDisposeOnClose
;
89 // ------------------------------------------------------------------------
91 // ------------------------------------------------------------------------
93 public TmfEventsTable(Composite parent
, int cacheSize
) {
94 this(parent
, cacheSize
, COLUMN_DATA
);
97 public TmfEventsTable(Composite parent
, int cacheSize
, ColumnData
[] columnData
) {
98 super("TmfEventsTable"); //$NON-NLS-1$
100 fCacheSize
= cacheSize
;
101 fCache
= new TmfEvent
[fCacheSize
];
103 // Create a virtual table
104 final int style
= SWT
.SINGLE
| SWT
.FULL_SELECTION
| SWT
.BORDER
;
105 fTable
= new TmfVirtualTable(parent
, style
);
107 // Set the table layout
108 GridData layoutData
= new GridData(SWT
.FILL
, SWT
.FILL
, true, true);
109 fTable
.setLayoutData(layoutData
);
111 // Some cosmetic enhancements
112 fTable
.setHeaderVisible(true);
113 fTable
.setLinesVisible(true);
116 setColumnHeaders(columnData
);
118 // Handle the table item requests
119 fTable
.addSelectionListener(new SelectionAdapter() {
121 public void widgetSelected(SelectionEvent e
) {
122 TmfTimestamp ts
= (TmfTimestamp
) fTable
.getSelection()[0].getData();
123 broadcast(new TmfTimeSynchSignal(fTable
, ts
));
127 // Handle the table item requests
128 fTable
.addListener(SWT
.SetData
, new Listener() {
131 public void handleEvent(Event event
) {
133 final TableItem item
= (TableItem
) event
.item
;
134 final int index
= fTable
.indexOf(item
);
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()));
144 // Else, fill the cache asynchronously (and off the UI thread)
145 populateCache(index
);
149 fTable
.setItemCount(0);
153 public void dispose() {
155 if (fTrace
!= null && fDisposeOnClose
) {
161 public TmfVirtualTable
getTable() {
168 * FIXME: Add support for column selection
170 protected void setColumnHeaders(ColumnData
[] columnData
) {
171 fTable
.setColumnHeaders(columnData
);
174 protected void packColumns() {
175 if (fPackDone
) return;
176 for (TableColumn column
: fTable
.getColumns()) {
177 int headerWidth
= column
.getWidth();
179 if (column
.getWidth() < headerWidth
) {
180 column
.setWidth(headerWidth
);
190 * FIXME: Add support for column selection
192 protected String
[] extractItemFields(TmfEvent event
) {
193 String
[] fields
= new String
[0];
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()
206 public void setFocus() {
212 * @param disposeOnClose true if the trace should be disposed when the table is disposed
214 public void setTrace(ITmfTrace trace
, boolean disposeOnClose
) {
215 if (fTrace
!= null && fDisposeOnClose
) {
219 fDisposeOnClose
= disposeOnClose
;
221 // Perform the updates on the UI thread
222 fTable
.getDisplay().syncExec(new Runnable() {
225 //fTable.setSelection(0);
227 fCacheStartIndex
= fCacheEndIndex
= 0; // Clear the cache
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());
238 // ------------------------------------------------------------------------
240 // ------------------------------------------------------------------------
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() {
249 if (!fTable
.isDisposed() && fTrace
!= null) {
250 fTable
.setItemCount((int) fTrace
.getNbEvents());
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() {
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());
273 private boolean fRefreshPending
= false;
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() {
282 fRefreshPending
= false;
283 if (!fTable
.isDisposed() && fTrace
!= null) {
284 fTable
.setItemCount((int) fTrace
.getNbEvents());
292 public void currentTimeUpdated(final TmfTimeSynchSignal signal
) {
293 if ((signal
.getSource() != fTable
) && (fTrace
!= null) && (!fTable
.isDisposed())) {
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
) {
301 public void handleData(TmfEvent event
) {
302 super.handleData(event
);
306 public void handleCompleted() {
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();
314 if (timestamp
[0].compareTo(fTrace
.getEndTime(), true) == 1) {
315 timestamp
[0] = fTrace
.getEndTime();
318 // Get the rank for the event selection in the table
319 final int index
= (int) fTrace
.getRank(timestamp
[0]);
321 fTable
.getDisplay().asyncExec(new Runnable() {
324 // Return if table is disposed
325 if (fTable
.isDisposed()) return;
327 fTable
.setSelection(index
);
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]);
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);
349 super.handleCompleted();
353 @SuppressWarnings("unchecked")
354 TmfExperiment
<TmfEvent
> experiment
= (TmfExperiment
<TmfEvent
>)TmfExperiment
.getCurrentExperiment();
355 if (experiment
!= null) {
356 experiment
.sendRequest(subRequest
);
361 // ------------------------------------------------------------------------
362 // Event cache population
363 // ------------------------------------------------------------------------
365 // The event fetching job
367 private synchronized void populateCache(final int index
) {
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
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.
379 if (job
.getState() != Job
.NONE
) {
380 if (index
>= fCacheStartIndex
&& index
< (fCacheStartIndex
+ fCacheSize
)) {
383 // The new index is out of the requested range
384 // Kill the job and start a new one
389 fCacheStartIndex
= index
;
390 fCacheEndIndex
= index
;
392 job
= new Job("Fetching Events") { //$NON-NLS-1$
394 @SuppressWarnings("unchecked")
395 protected IStatus
run(final IProgressMonitor monitor
) {
397 TmfDataRequest
<TmfEvent
> request
= new TmfDataRequest
<TmfEvent
>(TmfEvent
.class, index
, fCacheSize
) {
398 private int count
= 0;
400 public void handleData(TmfEvent event
) {
401 // If the job is canceled, cancel the request so waitForCompletion() will unlock
402 if (monitor
.isCanceled()) {
406 super.handleData(event
);
408 fCache
[count
++] = event
.clone();
409 fCacheEndIndex
++; // TODO: Need to protect this??
414 ((ITmfDataProvider
<TmfEvent
>) fTrace
).sendRequest(request
);
416 request
.waitForCompletion();
417 } catch (InterruptedException e
) {
421 // Event cache is now updated. Perform update on the UI thread
422 if (!fTable
.isDisposed() && !monitor
.isCanceled()) {
423 fTable
.getDisplay().asyncExec(new Runnable() {
426 if (!fTable
.isDisposed()) {
428 fTable
.notifyUpdatedSelection();
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$
439 return new Status(IStatus
.OK
, TmfUiPlugin
.PLUGIN_ID
, "Completed"); //$NON-NLS-1$
443 job
.setPriority(Job
.SHORT
);