Skip to main content
summaryrefslogtreecommitdiffstats
blob: e0d96324b582ee11f8412a2eb14108ec23633dd4 (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
/*******************************************************************************
 * Copyright (c) 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
 *   Patrick Tasse - Factored out from events view
 *   Francois Chouinard - Replaced Table by TmfVirtualTable
 *******************************************************************************/

package org.eclipse.linuxtools.tmf.ui.viewers.events;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.linuxtools.tmf.component.ITmfDataProvider;
import org.eclipse.linuxtools.tmf.component.TmfComponent;
import org.eclipse.linuxtools.tmf.event.TmfEvent;
import org.eclipse.linuxtools.tmf.event.TmfTimestamp;
import org.eclipse.linuxtools.tmf.request.TmfDataRequest;
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.signal.TmfTraceUpdatedSignal;
import org.eclipse.linuxtools.tmf.trace.ITmfTrace;
import org.eclipse.linuxtools.tmf.ui.widgets.ColumnData;
import org.eclipse.linuxtools.tmf.ui.widgets.TmfVirtualTable;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

/**
 * <b><u>TmfEventsTable</u></b>
 */
public class TmfEventsTable extends TmfComponent {

//    private Shell fShell;
    
    // ------------------------------------------------------------------------
    // Table data
    // ------------------------------------------------------------------------

    protected TmfVirtualTable fTable;
    protected ITmfTrace fTrace;
    protected boolean fPackDone = false;

    // Table column names
    static private final String TIMESTAMP_COLUMN = "Timestamp";
    static private final String SOURCE_COLUMN    = "Source";
    static private final String TYPE_COLUMN      = "Type";
    static private final String REFERENCE_COLUMN = "File";
    static private final String CONTENT_COLUMN   = "Content";
    static private final String[] COLUMN_NAMES =  new String[] {
        TIMESTAMP_COLUMN,
        SOURCE_COLUMN,
        TYPE_COLUMN,
        REFERENCE_COLUMN,
        CONTENT_COLUMN
    };

    static private ColumnData[] COLUMN_DATA = new ColumnData[] {
        new ColumnData(COLUMN_NAMES[0], 100, SWT.LEFT),
        new ColumnData(COLUMN_NAMES[1], 100, SWT.LEFT),
        new ColumnData(COLUMN_NAMES[2], 100, SWT.LEFT),
        new ColumnData(COLUMN_NAMES[3], 100, SWT.LEFT),
        new ColumnData(COLUMN_NAMES[4], 100, SWT.LEFT)
    };

    // ------------------------------------------------------------------------
    // Event cache
    // ------------------------------------------------------------------------

    private final int  fCacheSize;
    private TmfEvent[] fCache;
    private int fCacheStartIndex = 0;
    private int fCacheEndIndex   = 0;

    private boolean fDisposeOnClose;

    // ------------------------------------------------------------------------
    // Constructor
    // ------------------------------------------------------------------------

    public TmfEventsTable(Composite parent, int cacheSize) {
    	this(parent, cacheSize, COLUMN_DATA);
    }

    public TmfEventsTable(Composite parent, int cacheSize, ColumnData[] columnData) {
        super("TmfEventsTable");
        
        fCacheSize = cacheSize;
        fCache = new TmfEvent[fCacheSize];
        
        // Create a virtual table
        final int style = SWT.SINGLE | SWT.FULL_SELECTION | SWT.BORDER;
        fTable = new TmfVirtualTable(parent, style);

        // Set the table layout
        GridData layoutData = new GridData(SWT.FILL, SWT.FILL, true, true);
        fTable.setLayoutData(layoutData);

        // Some cosmetic enhancements
        fTable.setHeaderVisible(true);
        fTable.setLinesVisible(true);

        // Set the columns
        setColumnHeaders(columnData);

        // Handle the table item requests 
        fTable.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                TmfTimestamp ts = (TmfTimestamp) fTable.getSelection()[0].getData();
                broadcast(new TmfTimeSynchSignal(fTable, ts));
            }
        });

        // Handle the table item requests 
        fTable.addListener(SWT.SetData, new Listener() {

            @SuppressWarnings("unchecked")
			public void handleEvent(Event event) {

                final TableItem item = (TableItem) event.item;
                final int index = fTable.indexOf(item);

                // Note: this works because handleEvent() is called once for each row, in sequence  
                if ((index >= fCacheStartIndex) && (index < fCacheEndIndex)) {
                    int i = index - fCacheStartIndex;
                    item.setText(extractItemFields(fCache[i]));
                    item.setData(new TmfTimestamp(fCache[i].getTimestamp()));
                    return;
                }

                TmfDataRequest<TmfEvent> request = new TmfDataRequest<TmfEvent>(TmfEvent.class, index, fCacheSize) {
                    @Override
                    public void handleData() {
                        TmfEvent[] tmpEvent = getData();
                        if ((tmpEvent != null) && (tmpEvent.length > 0)) {
                            fCache = tmpEvent;
                            fCacheStartIndex = index;
                            fCacheEndIndex = index + tmpEvent.length;
                        }
                    }
                };
                ((ITmfDataProvider<TmfEvent>) fTrace).sendRequest(request);
                try {
                    request.waitForCompletion();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                
                if (fCache[0] != null && fCacheStartIndex == index) {
                    item.setText(extractItemFields(fCache[0]));
                    item.setData(new TmfTimestamp(fCache[0].getTimestamp()));
                    packColumns();
                }
                
            }
        });

        fTable.setItemCount(0);
    }

    @Override
	public void dispose() {
        fTable.dispose();
        if (fTrace != null && fDisposeOnClose) {
            fTrace.dispose();
        }
        super.dispose();
    }

    public TmfVirtualTable getTable() {
        return fTable;
    }
    
    /**
     * @param table
     * 
     * FIXME: Add support for column selection
     */
    protected void setColumnHeaders(ColumnData[] columnData) {
    	fTable.setColumnHeaders(columnData);
    }

    protected void packColumns() {
        if (fPackDone) return;
        for (TableColumn column : fTable.getColumns()) {
            int headerWidth = column.getWidth();
            column.pack();
            if (column.getWidth() < headerWidth) {
                column.setWidth(headerWidth);
            }
        }
        fPackDone = true;
    }
    
    /**
     * @param event
     * @return
     * 
     * FIXME: Add support for column selection
     */
    protected String[] extractItemFields(TmfEvent event) {
        String[] fields = new String[0];
        if (event != null) {
            fields = new String[] {
                new Long(event.getTimestamp().getValue()).toString(),       
                event.getSource().getSourceId().toString(),
                event.getType().getTypeId().toString(),
                event.getReference().getReference().toString(),
                event.getContent().toString()
            };
        }
        return fields;
    }

    public void setFocus() {
        fTable.setFocus();
    }

    /**
     * @param trace
     * @param disposeOnClose true if the trace should be disposed when the table is disposed
     */
    public void setTrace(ITmfTrace trace, boolean disposeOnClose) {
        if (fTrace != null && fDisposeOnClose) {
            fTrace.dispose();
        }
        fTrace = trace;
        fDisposeOnClose = disposeOnClose;
        
        // Perform the updates on the UI thread
        fTable.getDisplay().syncExec(new Runnable() {
            public void run() {
                //fTable.setSelection(0);
                fTable.removeAll();
                fCacheStartIndex = fCacheEndIndex = 0; // Clear the cache
                
                if (!fTable.isDisposed() && fTrace != null) {
                    //int nbEvents = (int) fTrace.getNbEvents();
                    //fTable.setItemCount((nbEvents > 100) ? nbEvents : 100);
                    fTable.setItemCount((int) fTrace.getNbEvents());
                }
            }
        });
    }

    // ------------------------------------------------------------------------
    // Signal handlers
    // ------------------------------------------------------------------------
    
    @TmfSignalHandler
    public void experimentUpdated(TmfExperimentUpdatedSignal signal) {
        if (signal.getExperiment() != fTrace) return;
        // Perform the refresh on the UI thread
        fTable.getDisplay().asyncExec(new Runnable() {
            public void run() {
                if (!fTable.isDisposed() && fTrace != null) {
                    fTable.setItemCount((int) fTrace.getNbEvents());
                    fTable.refresh();
                }
            }
        });
    }
    
    @TmfSignalHandler
    public void traceUpdated(TmfTraceUpdatedSignal signal) {
        if (signal.getTrace() != fTrace) return;
        // Perform the refresh on the UI thread
        fTable.getDisplay().asyncExec(new Runnable() {
            public void run() {
                if (!fTable.isDisposed() && fTrace != null) {
                    //int nbEvents = (int) fTrace.getNbEvents();
                    //fTable.setItemCount((nbEvents > 100) ? nbEvents : 100);
                    fTable.setItemCount((int) fTrace.getNbEvents());
                }
            }
        });
    }

    private boolean fRefreshPending = false;
    @TmfSignalHandler
    public synchronized void rangeSynched(TmfRangeSynchSignal signal) {
        if (!fRefreshPending) {
            // Perform the refresh on the UI thread
            fRefreshPending = true;
            fTable.getDisplay().asyncExec(new Runnable() {
                public void run() {
                    fRefreshPending = false;
                    if (!fTable.isDisposed() && fTrace != null) {
                        fTable.setItemCount((int) fTrace.getNbEvents());
                    }
                }
            });
        }
    }
    
    @TmfSignalHandler
    public void currentTimeUpdated(final TmfTimeSynchSignal signal) {
        if (signal.getSource() != fTable && fTrace != null) {
            Job job = new Job("seeking...") {
                @Override
                protected IStatus run(IProgressMonitor monitor) {
                    final int index = (int) fTrace.getRank(signal.getCurrentTime());
                    // Perform the updates on the UI thread
                    fTable.getDisplay().asyncExec(new Runnable() {
                        public void run() {
                            fTable.setSelection(index);
                            // The timestamp might not correspond to an actual event
                            // and the selection will point to the next experiment event.
                            // But we would like to display both the event before and
                            // after the selected timestamp.
                            // This works fine by default except when the selected event
                            // is the top displayed event. The following ensures that we
                            // always see both events.
                            if ((index > 0) && (index == fTable.getTopIndex())) {
                                fTable.setTopIndex(index - 1);
                            }
                        }
                    });
                    return Status.OK_STATUS;
                }};
            job.schedule();
        }
    }
}

Back to the top