ctf: Update paths in the CTF-Testsuite tests
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / viewers / events / TmfEventsTable.java
CommitLineData
db4721fb 1/*******************************************************************************
c8422608 2 * Copyright (c) 2010, 2013 Ericsson
db4721fb
PT
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 * Patrick Tasse - Filter implementation (inspired by www.eclipse.org/mat)
ac44ef71 14 * Ansgar Radermacher - Support navigation to model URIs (Bug 396956)
f47ed727 15 * Bernd Hufmann - Updated call site and model URI implementation
db4721fb
PT
16 *******************************************************************************/
17
18package org.eclipse.linuxtools.tmf.ui.viewers.events;
19
60fb38b8 20import java.io.FileNotFoundException;
db4721fb
PT
21import java.util.ArrayList;
22import java.util.Arrays;
23import java.util.HashMap;
24import java.util.List;
25import java.util.Map;
26import java.util.Map.Entry;
27import java.util.regex.Pattern;
28import java.util.regex.PatternSyntaxException;
29
d3de0920
XR
30import org.eclipse.core.commands.Command;
31import org.eclipse.core.commands.ExecutionException;
32import org.eclipse.core.commands.NotEnabledException;
33import org.eclipse.core.commands.NotHandledException;
34import org.eclipse.core.commands.ParameterizedCommand;
35import org.eclipse.core.commands.common.NotDefinedException;
36import org.eclipse.core.expressions.IEvaluationContext;
db4721fb
PT
37import org.eclipse.core.resources.IFile;
38import org.eclipse.core.resources.IMarker;
39import org.eclipse.core.resources.IResource;
60fb38b8 40import org.eclipse.core.resources.IResourceVisitor;
ac44ef71 41import org.eclipse.core.resources.ResourcesPlugin;
db4721fb 42import org.eclipse.core.runtime.CoreException;
ac44ef71 43import org.eclipse.core.runtime.IPath;
db4721fb
PT
44import org.eclipse.core.runtime.IProgressMonitor;
45import org.eclipse.core.runtime.IStatus;
93bfd50a 46import org.eclipse.core.runtime.ListenerList;
ac44ef71 47import org.eclipse.core.runtime.Path;
db4721fb
PT
48import org.eclipse.core.runtime.Status;
49import org.eclipse.core.runtime.jobs.Job;
ac44ef71
AR
50import org.eclipse.emf.common.util.URI;
51import org.eclipse.emf.ecore.EValidator;
db4721fb
PT
52import org.eclipse.jface.action.Action;
53import org.eclipse.jface.action.IAction;
54import org.eclipse.jface.action.IMenuListener;
55import org.eclipse.jface.action.IMenuManager;
3f43dc48 56import org.eclipse.jface.action.IStatusLineManager;
db4721fb
PT
57import org.eclipse.jface.action.MenuManager;
58import org.eclipse.jface.action.Separator;
59import org.eclipse.jface.dialogs.InputDialog;
60import org.eclipse.jface.dialogs.MessageDialog;
61import org.eclipse.jface.resource.FontDescriptor;
62import org.eclipse.jface.resource.JFaceResources;
63import org.eclipse.jface.resource.LocalResourceManager;
ac44ef71 64import org.eclipse.jface.util.OpenStrategy;
93bfd50a 65import org.eclipse.jface.util.SafeRunnable;
60fb38b8 66import org.eclipse.jface.viewers.ArrayContentProvider;
93bfd50a
PT
67import org.eclipse.jface.viewers.ISelection;
68import org.eclipse.jface.viewers.ISelectionChangedListener;
69import org.eclipse.jface.viewers.ISelectionProvider;
60fb38b8 70import org.eclipse.jface.viewers.LabelProvider;
93bfd50a
PT
71import org.eclipse.jface.viewers.SelectionChangedEvent;
72import org.eclipse.jface.viewers.StructuredSelection;
db4721fb
PT
73import org.eclipse.jface.window.Window;
74import org.eclipse.linuxtools.internal.tmf.ui.Activator;
75import org.eclipse.linuxtools.internal.tmf.ui.Messages;
d3de0920 76import org.eclipse.linuxtools.internal.tmf.ui.commands.ExportToTextCommandHandler;
3be2946f 77import org.eclipse.linuxtools.internal.tmf.ui.dialogs.MultiLineInputDialog;
db4721fb
PT
78import org.eclipse.linuxtools.tmf.core.component.ITmfDataProvider;
79import org.eclipse.linuxtools.tmf.core.component.TmfComponent;
80import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
81import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
db4721fb 82import org.eclipse.linuxtools.tmf.core.event.TmfEventField;
f47ed727
BH
83import org.eclipse.linuxtools.tmf.core.event.lookup.ITmfCallsite;
84import org.eclipse.linuxtools.tmf.core.event.lookup.ITmfModelLookup;
85import org.eclipse.linuxtools.tmf.core.event.lookup.ITmfSourceLookup;
db4721fb
PT
86import org.eclipse.linuxtools.tmf.core.filter.ITmfFilter;
87import org.eclipse.linuxtools.tmf.core.filter.model.ITmfFilterTreeNode;
88import org.eclipse.linuxtools.tmf.core.filter.model.TmfFilterAndNode;
89import org.eclipse.linuxtools.tmf.core.filter.model.TmfFilterMatchesNode;
90import org.eclipse.linuxtools.tmf.core.filter.model.TmfFilterNode;
91import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest.ExecutionType;
92import org.eclipse.linuxtools.tmf.core.request.TmfDataRequest;
faa38350
PT
93import org.eclipse.linuxtools.tmf.core.signal.TmfEventFilterAppliedSignal;
94import org.eclipse.linuxtools.tmf.core.signal.TmfEventSearchAppliedSignal;
db4721fb
PT
95import org.eclipse.linuxtools.tmf.core.signal.TmfSignalHandler;
96import org.eclipse.linuxtools.tmf.core.signal.TmfTimeSynchSignal;
97import org.eclipse.linuxtools.tmf.core.signal.TmfTraceUpdatedSignal;
3bd46eef
AM
98import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp;
99import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
db4721fb 100import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
db4721fb 101import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
a3db8436 102import org.eclipse.linuxtools.tmf.core.trace.location.ITmfLocation;
db4721fb
PT
103import org.eclipse.linuxtools.tmf.ui.viewers.events.TmfEventsCache.CachedEvent;
104import org.eclipse.linuxtools.tmf.ui.views.colors.ColorSetting;
105import org.eclipse.linuxtools.tmf.ui.views.colors.ColorSettingsManager;
106import org.eclipse.linuxtools.tmf.ui.views.colors.IColorSettingsListener;
107import org.eclipse.linuxtools.tmf.ui.views.filter.FilterManager;
108import org.eclipse.linuxtools.tmf.ui.widgets.rawviewer.TmfRawEventViewer;
109import org.eclipse.linuxtools.tmf.ui.widgets.virtualtable.ColumnData;
110import org.eclipse.linuxtools.tmf.ui.widgets.virtualtable.TmfVirtualTable;
111import org.eclipse.swt.SWT;
112import org.eclipse.swt.custom.SashForm;
113import org.eclipse.swt.custom.TableEditor;
114import org.eclipse.swt.events.FocusAdapter;
115import org.eclipse.swt.events.FocusEvent;
116import org.eclipse.swt.events.KeyAdapter;
117import org.eclipse.swt.events.KeyEvent;
118import org.eclipse.swt.events.MouseAdapter;
119import org.eclipse.swt.events.MouseEvent;
120import org.eclipse.swt.events.SelectionAdapter;
121import org.eclipse.swt.events.SelectionEvent;
122import org.eclipse.swt.graphics.Color;
123import org.eclipse.swt.graphics.Font;
124import org.eclipse.swt.graphics.Image;
125import org.eclipse.swt.graphics.Point;
126import org.eclipse.swt.graphics.Rectangle;
127import org.eclipse.swt.layout.FillLayout;
128import org.eclipse.swt.layout.GridData;
129import org.eclipse.swt.layout.GridLayout;
130import org.eclipse.swt.widgets.Composite;
131import org.eclipse.swt.widgets.Display;
132import org.eclipse.swt.widgets.Event;
133import org.eclipse.swt.widgets.Label;
134import org.eclipse.swt.widgets.Listener;
135import org.eclipse.swt.widgets.Menu;
136import org.eclipse.swt.widgets.MessageBox;
137import org.eclipse.swt.widgets.Shell;
138import org.eclipse.swt.widgets.TableColumn;
139import org.eclipse.swt.widgets.TableItem;
140import org.eclipse.swt.widgets.Text;
ac44ef71 141import org.eclipse.ui.IWorkbenchPage;
db4721fb 142import org.eclipse.ui.PlatformUI;
d3de0920 143import org.eclipse.ui.commands.ICommandService;
60fb38b8 144import org.eclipse.ui.dialogs.ListDialog;
d3de0920 145import org.eclipse.ui.handlers.IHandlerService;
ac44ef71 146import org.eclipse.ui.ide.IDE;
db4721fb
PT
147import org.eclipse.ui.ide.IGotoMarker;
148import org.eclipse.ui.themes.ColorUtil;
149
150/**
151 * The generic TMF Events table
152 *
153 * This is a view that will list events that are read from a trace.
154 *
155 * @version 1.0
156 * @author Francois Chouinard
157 * @author Patrick Tasse
93bfd50a 158 * @since 2.0
db4721fb 159 */
faa38350 160public class TmfEventsTable extends TmfComponent implements IGotoMarker, IColorSettingsListener, ISelectionProvider {
db4721fb
PT
161
162 private static final Image BOOKMARK_IMAGE = Activator.getDefault().getImageFromPath(
163 "icons/elcl16/bookmark_obj.gif"); //$NON-NLS-1$
164 private static final Image SEARCH_IMAGE = Activator.getDefault().getImageFromPath("icons/elcl16/search.gif"); //$NON-NLS-1$
165 private static final Image SEARCH_MATCH_IMAGE = Activator.getDefault().getImageFromPath(
166 "icons/elcl16/search_match.gif"); //$NON-NLS-1$
167 private static final Image SEARCH_MATCH_BOOKMARK_IMAGE = Activator.getDefault().getImageFromPath(
168 "icons/elcl16/search_match_bookmark.gif"); //$NON-NLS-1$
169 private static final Image FILTER_IMAGE = Activator.getDefault()
170 .getImageFromPath("icons/elcl16/filter_items.gif"); //$NON-NLS-1$
171 private static final Image STOP_IMAGE = Activator.getDefault().getImageFromPath("icons/elcl16/stop.gif"); //$NON-NLS-1$
172 private static final String SEARCH_HINT = Messages.TmfEventsTable_SearchHint;
173 private static final String FILTER_HINT = Messages.TmfEventsTable_FilterHint;
174 private static final int MAX_CACHE_SIZE = 1000;
d3de0920
XR
175 /**
176 * Empty ITmfEventField array, used by {@link #extractItemFields(ITmfEvent)}
177 * @since 2.2
178 */
179 public static final ITmfEventField[] EMPTY_FIELD_ARRAY = new TmfEventField[0];
db4721fb
PT
180
181 /**
182 * The events table search/filter keys
183 *
184 * @version 1.0
185 * @author Patrick Tasse
186 */
187 public interface Key {
188 /** Search text */
189 String SEARCH_TXT = "$srch_txt"; //$NON-NLS-1$
190
191 /** Search object */
192 String SEARCH_OBJ = "$srch_obj"; //$NON-NLS-1$
193
194 /** Filter text */
195 String FILTER_TXT = "$fltr_txt"; //$NON-NLS-1$
196
197 /** Filter object */
198 String FILTER_OBJ = "$fltr_obj"; //$NON-NLS-1$
199
dadf6e1a 200 /** Timestamp */
db4721fb
PT
201 String TIMESTAMP = "$time"; //$NON-NLS-1$
202
203 /** Rank */
204 String RANK = "$rank"; //$NON-NLS-1$
205
206 /** Field ID */
207 String FIELD_ID = "$field_id"; //$NON-NLS-1$
208
209 /** Bookmark indicator */
210 String BOOKMARK = "$bookmark"; //$NON-NLS-1$
211 }
212
213 /**
214 * The events table search/filter state
215 *
216 * @version 1.0
217 * @author Patrick Tasse
218 */
219 public static enum HeaderState {
220 /** A search is being run */
221 SEARCH,
222
223 /** A filter is applied */
224 FILTER
225 }
226
227 interface Direction {
228 int FORWARD = +1;
229 int BACKWARD = -1;
230 }
231
232 // ------------------------------------------------------------------------
233 // Table data
234 // ------------------------------------------------------------------------
235
a0a88f65 236 /** The virtual event table */
db4721fb 237 protected TmfVirtualTable fTable;
a0a88f65
AM
238
239 private Composite fComposite;
240 private SashForm fSashForm;
241 private TmfRawEventViewer fRawViewer;
242 private ITmfTrace fTrace;
243 private boolean fPackDone = false;
244 private HeaderState fHeaderState = HeaderState.SEARCH;
245 private long fSelectedRank = 0;
3f43dc48
PT
246 private ITmfTimestamp fSelectedBeginTimestamp = null;
247 private IStatusLineManager fStatusLineManager = null;
db4721fb
PT
248
249 // Filter data
a0a88f65
AM
250 private long fFilterMatchCount;
251 private long fFilterCheckCount;
252 private FilterThread fFilterThread;
253 private boolean fFilterThreadResume = false;
254 private final Object fFilterSyncObj = new Object();
255 private SearchThread fSearchThread;
256 private final Object fSearchSyncObj = new Object();
db4721fb 257
93bfd50a
PT
258 /**
259 * List of selection change listeners (element type: <code>ISelectionChangedListener</code>).
260 *
261 * @see #fireSelectionChanged
262 */
263 private ListenerList selectionChangedListeners = new ListenerList();
264
db4721fb 265 // Bookmark map <Rank, MarkerId>
a0a88f65
AM
266 private Map<Long, Long> fBookmarksMap = new HashMap<Long, Long>();
267 private IFile fBookmarksFile;
268 private long fPendingGotoRank = -1;
db4721fb
PT
269
270 // SWT resources
a0a88f65
AM
271 private LocalResourceManager fResourceManager = new LocalResourceManager(JFaceResources.getResources());
272 private Color fGrayColor;
273 private Color fGreenColor;
274 private Font fBoldFont;
db4721fb
PT
275
276 // Table column names
a0a88f65 277 private static final String[] COLUMN_NAMES = new String[] { Messages.TmfEventsTable_TimestampColumnHeader,
db4721fb
PT
278 Messages.TmfEventsTable_SourceColumnHeader, Messages.TmfEventsTable_TypeColumnHeader,
279 Messages.TmfEventsTable_ReferenceColumnHeader, Messages.TmfEventsTable_ContentColumnHeader };
280
a0a88f65 281 private static final ColumnData[] COLUMN_DATA = new ColumnData[] { new ColumnData(COLUMN_NAMES[0], 100, SWT.LEFT),
db4721fb
PT
282 new ColumnData(COLUMN_NAMES[1], 100, SWT.LEFT), new ColumnData(COLUMN_NAMES[2], 100, SWT.LEFT),
283 new ColumnData(COLUMN_NAMES[3], 100, SWT.LEFT), new ColumnData(COLUMN_NAMES[4], 100, SWT.LEFT) };
284
285 // Event cache
286 private final TmfEventsCache fCache;
287 private boolean fCacheUpdateBusy = false;
288 private boolean fCacheUpdatePending = false;
289 private boolean fCacheUpdateCompleted = false;
290 private final Object fCacheUpdateSyncObj = new Object();
291
292 private boolean fDisposeOnClose;
293
294 // ------------------------------------------------------------------------
a0a88f65 295 // Constructors
db4721fb
PT
296 // ------------------------------------------------------------------------
297
298 /**
299 * Basic constructor, will use default column data.
300 *
301 * @param parent
302 * The parent composite UI object
303 * @param cacheSize
304 * The size of the event table cache
305 */
306 public TmfEventsTable(final Composite parent, final int cacheSize) {
307 this(parent, cacheSize, COLUMN_DATA);
308 }
309
310 /**
311 * Advanced constructor, where we also define which column data to use.
312 *
313 * @param parent
314 * The parent composite UI object
315 * @param cacheSize
316 * The size of the event table cache
317 * @param columnData
318 * The column data to use for this table
319 */
320 public TmfEventsTable(final Composite parent, int cacheSize, final ColumnData[] columnData) {
321 super("TmfEventsTable"); //$NON-NLS-1$
322
323 fComposite = new Composite(parent, SWT.NONE);
324 final GridLayout gl = new GridLayout(1, false);
325 gl.marginHeight = 0;
326 gl.marginWidth = 0;
327 gl.verticalSpacing = 0;
328 fComposite.setLayout(gl);
329
330 fSashForm = new SashForm(fComposite, SWT.HORIZONTAL);
331 fSashForm.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
332
333 // Create a virtual table
3f43dc48 334 final int style = SWT.H_SCROLL | SWT.V_SCROLL | SWT.MULTI | SWT.FULL_SELECTION;
db4721fb
PT
335 fTable = new TmfVirtualTable(fSashForm, style);
336
337 // Set the table layout
338 final GridData layoutData = new GridData(SWT.FILL, SWT.FILL, true, true);
339 fTable.setLayoutData(layoutData);
340
341 // Some cosmetic enhancements
342 fTable.setHeaderVisible(true);
343 fTable.setLinesVisible(true);
344
345 // Set the columns
346 setColumnHeaders(columnData);
347
348 // Set the default column field ids if this is not a subclass
349 if (Arrays.equals(columnData, COLUMN_DATA)) {
350 fTable.getColumns()[0].setData(Key.FIELD_ID, ITmfEvent.EVENT_FIELD_TIMESTAMP);
351 fTable.getColumns()[1].setData(Key.FIELD_ID, ITmfEvent.EVENT_FIELD_SOURCE);
352 fTable.getColumns()[2].setData(Key.FIELD_ID, ITmfEvent.EVENT_FIELD_TYPE);
353 fTable.getColumns()[3].setData(Key.FIELD_ID, ITmfEvent.EVENT_FIELD_REFERENCE);
354 fTable.getColumns()[4].setData(Key.FIELD_ID, ITmfEvent.EVENT_FIELD_CONTENT);
355 }
356
357 // Set the frozen row for header row
358 fTable.setFrozenRowCount(1);
359
360 // Create the header row cell editor
361 createHeaderEditor();
362
363 // Handle the table item selection
364 fTable.addSelectionListener(new SelectionAdapter() {
365 @Override
366 public void widgetSelected(final SelectionEvent e) {
3f43dc48
PT
367 if (e.item == null) {
368 return;
369 }
370 updateStatusLine(null);
371 if (fTable.getSelectionIndices().length > 0) {
372 if (e.item.getData(Key.RANK) instanceof Long) {
373 fSelectedRank = (Long) e.item.getData(Key.RANK);
374 fRawViewer.selectAndReveal((Long) e.item.getData(Key.RANK));
375 }
376 if (e.item.getData(Key.TIMESTAMP) instanceof ITmfTimestamp) {
377 final ITmfTimestamp ts = (ITmfTimestamp) e.item.getData(Key.TIMESTAMP);
378 if (fTable.getSelectionIndices().length == 1) {
379 fSelectedBeginTimestamp = ts;
380 }
381 if (fSelectedBeginTimestamp != null) {
382 if (fSelectedBeginTimestamp.compareTo(ts) <= 0) {
383 broadcast(new TmfTimeSynchSignal(TmfEventsTable.this, fSelectedBeginTimestamp, ts));
384 if (fTable.getSelectionIndices().length == 2) {
385 updateStatusLine(ts.getDelta(fSelectedBeginTimestamp));
386 }
387 } else {
388 broadcast(new TmfTimeSynchSignal(TmfEventsTable.this, ts, fSelectedBeginTimestamp));
dadf6e1a 389 updateStatusLine(fSelectedBeginTimestamp.getDelta(ts));
3f43dc48 390 }
db4721fb 391 }
3f43dc48
PT
392 } else {
393 if (fTable.getSelectionIndices().length == 1) {
394 fSelectedBeginTimestamp = null;
db4721fb
PT
395 }
396 }
397 }
3f43dc48
PT
398 if (e.item.getData() != null) {
399 fireSelectionChanged(new SelectionChangedEvent(TmfEventsTable.this, new StructuredSelection(e.item.getData())));
400 } else {
401 fireSelectionChanged(new SelectionChangedEvent(TmfEventsTable.this, StructuredSelection.EMPTY));
402 }
db4721fb
PT
403 }
404 });
405
41b5c37f
AM
406 int realCacheSize = Math.max(cacheSize, Display.getDefault().getBounds().height / fTable.getItemHeight());
407 realCacheSize = Math.min(realCacheSize, MAX_CACHE_SIZE);
408 fCache = new TmfEventsCache(realCacheSize, this);
db4721fb
PT
409
410 // Handle the table item requests
411 fTable.addListener(SWT.SetData, new Listener() {
412
413 @Override
414 public void handleEvent(final Event event) {
415
416 final TableItem item = (TableItem) event.item;
417 int index = event.index - 1; // -1 for the header row
418
419 if (event.index == 0) {
420 setHeaderRowItemData(item);
421 return;
422 }
423
424 if (fTable.getData(Key.FILTER_OBJ) != null) {
425 if ((event.index == 1) || (event.index == (fTable.getItemCount() - 1))) {
426 setFilterStatusRowItemData(item);
427 return;
428 }
429 index = index - 1; // -1 for top filter status row
430 }
431
432 final CachedEvent cachedEvent = fCache.getEvent(index);
433 if (cachedEvent != null) {
434 setItemData(item, cachedEvent.event, cachedEvent.rank);
435 return;
436 }
437
438 // Else, fill the cache asynchronously (and off the UI thread)
439 event.doit = false;
440 }
441 });
442
443 fTable.addMouseListener(new MouseAdapter() {
444 @Override
445 public void mouseDoubleClick(final MouseEvent event) {
446 if (event.button != 1) {
447 return;
448 }
449 // Identify the selected row
450 final Point point = new Point(event.x, event.y);
451 final TableItem item = fTable.getItem(point);
452 if (item != null) {
453 final Rectangle imageBounds = item.getImageBounds(0);
454 imageBounds.width = BOOKMARK_IMAGE.getBounds().width;
455 if (imageBounds.contains(point)) {
456 final Long rank = (Long) item.getData(Key.RANK);
457 if (rank != null) {
458 toggleBookmark(rank);
459 }
460 }
461 }
462 }
463 });
464
465 final Listener tooltipListener = new Listener () {
466 Shell tooltipShell = null;
467 @Override
468 public void handleEvent(final Event event) {
469 switch (event.type) {
470 case SWT.MouseHover:
471 final TableItem item = fTable.getItem(new Point(event.x, event.y));
472 if (item == null) {
473 return;
474 }
475 final Long rank = (Long) item.getData(Key.RANK);
476 if (rank == null) {
477 return;
478 }
479 final String tooltipText = (String) item.getData(Key.BOOKMARK);
480 final Rectangle bounds = item.getImageBounds(0);
481 bounds.width = BOOKMARK_IMAGE.getBounds().width;
482 if (!bounds.contains(event.x,event.y)) {
483 return;
484 }
485 if ((tooltipShell != null) && !tooltipShell.isDisposed()) {
486 tooltipShell.dispose();
487 }
488 tooltipShell = new Shell(fTable.getShell(), SWT.ON_TOP | SWT.NO_FOCUS | SWT.TOOL);
489 tooltipShell.setBackground(PlatformUI.getWorkbench().getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND));
490 final FillLayout layout = new FillLayout();
491 layout.marginWidth = 2;
492 tooltipShell.setLayout(layout);
493 final Label label = new Label(tooltipShell, SWT.WRAP);
494 String text = rank.toString() + (tooltipText != null ? ": " + tooltipText : ""); //$NON-NLS-1$ //$NON-NLS-2$
495 label.setForeground(PlatformUI.getWorkbench().getDisplay().getSystemColor(SWT.COLOR_INFO_FOREGROUND));
496 label.setBackground(PlatformUI.getWorkbench().getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND));
497 label.setText(text);
498 label.addListener(SWT.MouseExit, this);
499 label.addListener(SWT.MouseDown, this);
500 label.addListener(SWT.MouseWheel, this);
501 final Point size = tooltipShell.computeSize(SWT.DEFAULT, SWT.DEFAULT);
502 /*
503 * Bug in Linux. The coordinates of the event have an origin that excludes the table header but
504 * the method toDisplay() expects coordinates relative to an origin that includes the table header.
505 */
506 int y = event.y;
507 if (System.getProperty("os.name").contains("Linux")) { //$NON-NLS-1$ //$NON-NLS-2$
508 y += fTable.getHeaderHeight();
509 }
510 Point pt = fTable.toDisplay(event.x, y);
511 pt.x += BOOKMARK_IMAGE.getBounds().width;
3be2946f 512 pt.y += item.getBounds().height;
db4721fb
PT
513 tooltipShell.setBounds(pt.x, pt.y, size.x, size.y);
514 tooltipShell.setVisible(true);
515 break;
516 case SWT.Dispose:
517 case SWT.KeyDown:
518 case SWT.MouseMove:
519 case SWT.MouseExit:
520 case SWT.MouseDown:
521 case SWT.MouseWheel:
522 if (tooltipShell != null) {
523 tooltipShell.dispose();
524 tooltipShell = null;
525 }
526 break;
527 default:
528 break;
529 }
530 }
531 };
532
533 fTable.addListener(SWT.MouseHover, tooltipListener);
534 fTable.addListener(SWT.Dispose, tooltipListener);
535 fTable.addListener(SWT.KeyDown, tooltipListener);
536 fTable.addListener(SWT.MouseMove, tooltipListener);
537 fTable.addListener(SWT.MouseExit, tooltipListener);
538 fTable.addListener(SWT.MouseDown, tooltipListener);
539 fTable.addListener(SWT.MouseWheel, tooltipListener);
540
541 // Create resources
542 createResources();
543
544 ColorSettingsManager.addColorSettingsListener(this);
545
546 fTable.setItemCount(1); // +1 for header row
547
548 fRawViewer = new TmfRawEventViewer(fSashForm, SWT.H_SCROLL | SWT.V_SCROLL);
549
550 fRawViewer.addSelectionListener(new Listener() {
551 @Override
552 public void handleEvent(final Event e) {
553 if (e.data instanceof Long) {
554 final long rank = (Long) e.data;
555 int index = (int) rank;
556 if (fTable.getData(Key.FILTER_OBJ) != null) {
557 index = fCache.getFilteredEventIndex(rank) + 1; // +1 for top filter status row
558 }
559 fTable.setSelection(index + 1); // +1 for header row
560 fSelectedRank = rank;
3f43dc48 561 updateStatusLine(null);
1e1bef82 562 } else if (e.data instanceof ITmfLocation) {
db4721fb
PT
563 // DOES NOT WORK: rank undefined in context from seekLocation()
564 // ITmfLocation<?> location = (ITmfLocation<?>) e.data;
565 // TmfContext context = fTrace.seekLocation(location);
566 // fTable.setSelection((int) context.getRank());
567 return;
568 } else {
569 return;
570 }
571 final TableItem[] selection = fTable.getSelection();
572 if ((selection != null) && (selection.length > 0)) {
573 final TmfTimestamp ts = (TmfTimestamp) fTable.getSelection()[0].getData(Key.TIMESTAMP);
574 if (ts != null) {
575 broadcast(new TmfTimeSynchSignal(TmfEventsTable.this, ts));
576 }
577 }
578 }
579 });
580
581 fSashForm.setWeights(new int[] { 1, 1 });
582 fRawViewer.setVisible(false);
583
584 createPopupMenu();
585 }
586
a0a88f65
AM
587 /**
588 * Create a pop-up menu.
589 */
db4721fb
PT
590 protected void createPopupMenu() {
591 final IAction showTableAction = new Action(Messages.TmfEventsTable_ShowTableActionText) {
592 @Override
593 public void run() {
594 fTable.setVisible(true);
595 fSashForm.layout();
596 }
597 };
598
599 final IAction hideTableAction = new Action(Messages.TmfEventsTable_HideTableActionText) {
600 @Override
601 public void run() {
602 fTable.setVisible(false);
603 fSashForm.layout();
604 }
605 };
606
607 final IAction showRawAction = new Action(Messages.TmfEventsTable_ShowRawActionText) {
608 @Override
609 public void run() {
610 fRawViewer.setVisible(true);
611 fSashForm.layout();
612 final int index = fTable.getSelectionIndex();
dadf6e1a 613 if (index >= 1) {
db4721fb
PT
614 fRawViewer.selectAndReveal(index - 1);
615 }
616 }
617 };
618
619 final IAction hideRawAction = new Action(Messages.TmfEventsTable_HideRawActionText) {
620 @Override
621 public void run() {
622 fRawViewer.setVisible(false);
623 fSashForm.layout();
624 }
625 };
626
029df6e3 627 final IAction openCallsiteAction = new Action(Messages.TmfEventsTable_OpenSourceCodeActionText) {
60fb38b8
PT
628 @Override
629 public void run() {
630 final TableItem items[] = fTable.getSelection();
631 if (items.length != 1) {
632 return;
633 }
634 final TableItem item = items[0];
635
636 final Object data = item.getData();
f47ed727
BH
637 if (data instanceof ITmfSourceLookup) {
638 ITmfSourceLookup event = (ITmfSourceLookup) data;
639 ITmfCallsite cs = event.getCallsite();
60fb38b8
PT
640 if (cs == null || cs.getFileName() == null) {
641 return;
642 }
643 IMarker marker = null;
644 try {
645 String fileName = cs.getFileName();
646 final String trimmedPath = fileName.replaceAll("\\.\\./", ""); //$NON-NLS-1$ //$NON-NLS-2$
647 final ArrayList<IFile> files = new ArrayList<IFile>();
648 ResourcesPlugin.getWorkspace().getRoot().accept(new IResourceVisitor() {
649 @Override
650 public boolean visit(IResource resource) throws CoreException {
651 if (resource instanceof IFile && resource.getFullPath().toString().endsWith(trimmedPath)) {
652 files.add((IFile) resource);
653 }
654 return true;
655 }
656 });
657 IFile file = null;
658 if (files.size() > 1) {
659 ListDialog dialog = new ListDialog(getTable().getShell());
660 dialog.setContentProvider(ArrayContentProvider.getInstance());
661 dialog.setLabelProvider(new LabelProvider() {
662 @Override
663 public String getText(Object element) {
664 return ((IFile) element).getFullPath().toString();
665 }
666 });
667 dialog.setInput(files);
029df6e3
JCK
668 dialog.setTitle(Messages.TmfEventsTable_OpenSourceCodeSelectFileDialogTitle);
669 dialog.setMessage(Messages.TmfEventsTable_OpenSourceCodeSelectFileDialogTitle + '\n' + cs.toString());
60fb38b8
PT
670 dialog.open();
671 Object[] result = dialog.getResult();
672 if (result != null && result.length > 0) {
673 file = (IFile) result[0];
674 }
675 } else if (files.size() == 1) {
676 file = files.get(0);
677 }
678 if (file != null) {
679 marker = file.createMarker(IMarker.MARKER);
680 marker.setAttribute(IMarker.LINE_NUMBER, Long.valueOf(cs.getLineNumber()).intValue());
681 IDE.openEditor(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(), marker);
682 marker.delete();
683 } else if (files.size() == 0){
029df6e3 684 displayException(new FileNotFoundException('\'' + cs.toString() + '\'' + '\n' + Messages.TmfEventsTable_OpenSourceCodeNotFound));
60fb38b8 685 }
60fb38b8 686 } catch (CoreException e) {
8e95e814 687 displayException(e);
60fb38b8
PT
688 }
689 }
690 }
691 };
692
693 final IAction openModelAction = new Action(Messages.TmfEventsTable_OpenModelActionText) {
ac44ef71
AR
694 @Override
695 public void run() {
696
697 final TableItem items[] = fTable.getSelection();
698 if (items.length != 1) {
699 return;
700 }
701 final TableItem item = items[0];
702
703 final Object eventData = item.getData();
f47ed727
BH
704 if (eventData instanceof ITmfModelLookup) {
705 String modelURI = ((ITmfModelLookup) eventData).getModelUri();
ac44ef71
AR
706
707 if (modelURI != null) {
708 IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
709
710 IFile file = null;
711 final URI uri = URI.createURI(modelURI);
712 if (uri.isPlatformResource()) {
713 IPath path = new Path(uri.toPlatformString(true));
714 file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
715 } else if (uri.isFile() && !uri.isRelative()) {
716 file = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(
717 new Path(uri.toFileString()));
718 }
719
720 if (file != null) {
721 try {
722 /*
723 * create a temporary validation marker on the
724 * model file, remove it afterwards thus,
725 * navigation works with all model editors
726 * supporting the navigation to a marker
727 */
728 IMarker marker = file.createMarker(EValidator.MARKER);
729 marker.setAttribute(EValidator.URI_ATTRIBUTE, modelURI);
730 marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_INFO);
731
732 IDE.openEditor(activePage, marker, OpenStrategy.activateOnOpen());
733 marker.delete();
734 }
735 catch (CoreException e) {
736 displayException(e);
737 }
60fb38b8
PT
738 } else {
739 displayException(new FileNotFoundException('\'' + modelURI + '\'' + '\n' + Messages.TmfEventsTable_OpenModelUnsupportedURI));
ac44ef71
AR
740 }
741 }
742 }
743 }
744 };
60fb38b8 745
d3de0920
XR
746 final IAction exportToTextAction = new Action(Messages.TmfEventsTable_Export_to_text) {
747 @Override
748 public void run() {
749 IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
750 IHandlerService handlerService = (IHandlerService) activePage.getActiveEditor().getSite().getService(IHandlerService.class);
751 ICommandService cmdService = (ICommandService) activePage.getActiveEditor().getSite().getService(ICommandService.class);
752 try {
753 HashMap<String, Object> parameters = new HashMap<String, Object>();
754 StringBuilder header = new StringBuilder();
755 boolean needTab = false;
756 for (TableColumn tc: fTable.getColumns()) {
757 if (needTab) {
758 header.append('\t');
759 }
760 header.append(tc.getText());
761 needTab = true;
762 }
763 Command command = cmdService.getCommand(ExportToTextCommandHandler.COMMAND_ID);
764 ParameterizedCommand cmd = ParameterizedCommand.generateCommand(command,parameters);
765 IEvaluationContext context = handlerService.getCurrentState();
766 context.addVariable(ExportToTextCommandHandler.TMF_EVENT_TABLE_HEADER_ID, header.toString());
767 context.addVariable(ExportToTextCommandHandler.TMF_EVENT_TABLE_PARAMETER_ID, TmfEventsTable.this);
768 handlerService.executeCommandInContext(cmd, null, context);
769 } catch (ExecutionException e) {
770 displayException(e);
771 } catch (NotDefinedException e) {
772 displayException(e);
773 } catch (NotEnabledException e) {
774 displayException(e);
775 } catch (NotHandledException e) {
776 displayException(e);
777 }
778 }
779 };
780
db4721fb
PT
781 final IAction showSearchBarAction = new Action(Messages.TmfEventsTable_ShowSearchBarActionText) {
782 @Override
783 public void run() {
784 fHeaderState = HeaderState.SEARCH;
785 fTable.refresh();
786 }
787 };
788
789 final IAction showFilterBarAction = new Action(Messages.TmfEventsTable_ShowFilterBarActionText) {
790 @Override
791 public void run() {
792 fHeaderState = HeaderState.FILTER;
793 fTable.refresh();
794 }
795 };
796
797 final IAction clearFiltersAction = new Action(Messages.TmfEventsTable_ClearFiltersActionText) {
798 @Override
799 public void run() {
800 clearFilters();
801 }
802 };
803
804 class ToggleBookmarkAction extends Action {
805 long fRank;
806
807 public ToggleBookmarkAction(final String text, final long rank) {
808 super(text);
809 fRank = rank;
810 }
811
812 @Override
813 public void run() {
814 toggleBookmark(fRank);
815 }
816 }
817
818 final MenuManager tablePopupMenu = new MenuManager();
819 tablePopupMenu.setRemoveAllWhenShown(true);
820 tablePopupMenu.addMenuListener(new IMenuListener() {
821 @Override
822 public void menuAboutToShow(final IMenuManager manager) {
823 if (fTable.getSelectionIndex() == 0) {
824 // Right-click on header row
825 if (fHeaderState == HeaderState.FILTER) {
826 tablePopupMenu.add(showSearchBarAction);
827 } else {
828 tablePopupMenu.add(showFilterBarAction);
829 }
830 return;
831 }
832 final Point point = fTable.toControl(Display.getDefault().getCursorLocation());
833 final TableItem item = fTable.getSelection().length > 0 ? fTable.getSelection()[0] : null;
834 if (item != null) {
835 final Rectangle imageBounds = item.getImageBounds(0);
836 imageBounds.width = BOOKMARK_IMAGE.getBounds().width;
837 if (point.x <= (imageBounds.x + imageBounds.width)) {
838 // Right-click on left margin
839 final Long rank = (Long) item.getData(Key.RANK);
840 if ((rank != null) && (fBookmarksFile != null)) {
841 if (fBookmarksMap.containsKey(rank)) {
842 tablePopupMenu.add(new ToggleBookmarkAction(
843 Messages.TmfEventsTable_RemoveBookmarkActionText, rank));
844 } else {
845 tablePopupMenu.add(new ToggleBookmarkAction(
846 Messages.TmfEventsTable_AddBookmarkActionText, rank));
847 }
848 }
849 return;
850 }
851 }
60fb38b8 852
db4721fb
PT
853 // Right-click on table
854 if (fTable.isVisible() && fRawViewer.isVisible()) {
855 tablePopupMenu.add(hideTableAction);
856 tablePopupMenu.add(hideRawAction);
857 } else if (!fTable.isVisible()) {
858 tablePopupMenu.add(showTableAction);
859 } else if (!fRawViewer.isVisible()) {
860 tablePopupMenu.add(showRawAction);
861 }
d3de0920 862 tablePopupMenu.add(exportToTextAction);
db4721fb 863 tablePopupMenu.add(new Separator());
60fb38b8 864
ac44ef71 865 if (item != null) {
60fb38b8 866 final Object data = item.getData();
f47ed727
BH
867 Separator separator = null;
868 if (data instanceof ITmfSourceLookup) {
869 ITmfSourceLookup event = (ITmfSourceLookup) data;
60fb38b8
PT
870 if (event.getCallsite() != null) {
871 tablePopupMenu.add(openCallsiteAction);
872 separator = new Separator();
873 }
f47ed727
BH
874 }
875
876 if (data instanceof ITmfModelLookup) {
877 ITmfModelLookup event = (ITmfModelLookup) data;
878 if (event.getModelUri() != null) {
60fb38b8
PT
879 tablePopupMenu.add(openModelAction);
880 separator = new Separator();
881 }
f47ed727 882
60fb38b8
PT
883 if (separator != null) {
884 tablePopupMenu.add(separator);
ac44ef71
AR
885 }
886 }
887 }
60fb38b8 888
db4721fb
PT
889 tablePopupMenu.add(clearFiltersAction);
890 final ITmfFilterTreeNode[] savedFilters = FilterManager.getSavedFilters();
891 if (savedFilters.length > 0) {
892 final MenuManager subMenu = new MenuManager(Messages.TmfEventsTable_ApplyPresetFilterMenuName);
893 for (final ITmfFilterTreeNode node : savedFilters) {
894 if (node instanceof TmfFilterNode) {
895 final TmfFilterNode filter = (TmfFilterNode) node;
896 subMenu.add(new Action(filter.getFilterName()) {
897 @Override
898 public void run() {
899 applyFilter(filter);
900 }
901 });
902 }
903 }
904 tablePopupMenu.add(subMenu);
905 }
906 appendToTablePopupMenu(tablePopupMenu, item);
907 }
908 });
909
910 final MenuManager rawViewerPopupMenu = new MenuManager();
911 rawViewerPopupMenu.setRemoveAllWhenShown(true);
912 rawViewerPopupMenu.addMenuListener(new IMenuListener() {
913 @Override
914 public void menuAboutToShow(final IMenuManager manager) {
915 if (fTable.isVisible() && fRawViewer.isVisible()) {
916 rawViewerPopupMenu.add(hideTableAction);
917 rawViewerPopupMenu.add(hideRawAction);
918 } else if (!fTable.isVisible()) {
919 rawViewerPopupMenu.add(showTableAction);
920 } else if (!fRawViewer.isVisible()) {
921 rawViewerPopupMenu.add(showRawAction);
922 }
923 appendToRawPopupMenu(tablePopupMenu);
924 }
925 });
926
927 Menu menu = tablePopupMenu.createContextMenu(fTable);
928 fTable.setMenu(menu);
929
930 menu = rawViewerPopupMenu.createContextMenu(fRawViewer);
931 fRawViewer.setMenu(menu);
932 }
933
f8177ba2 934
a0a88f65
AM
935 /**
936 * Append an item to the event table's pop-up menu.
937 *
938 * @param tablePopupMenu
939 * The menu manager
940 * @param selectedItem
941 * The item to append
942 */
db4721fb
PT
943 protected void appendToTablePopupMenu(final MenuManager tablePopupMenu, final TableItem selectedItem) {
944 // override to append more actions
945 }
946
a0a88f65
AM
947 /**
948 * Append an item to the raw viewer's pop-up menu.
949 *
950 * @param rawViewerPopupMenu
951 * The menu manager
952 */
db4721fb
PT
953 protected void appendToRawPopupMenu(final MenuManager rawViewerPopupMenu) {
954 // override to append more actions
955 }
956
957 @Override
958 public void dispose() {
959 stopSearchThread();
960 stopFilterThread();
961 ColorSettingsManager.removeColorSettingsListener(this);
962 fComposite.dispose();
963 if ((fTrace != null) && fDisposeOnClose) {
964 fTrace.dispose();
965 }
966 fResourceManager.dispose();
967 super.dispose();
968 }
969
970 /**
971 * Assign a layout data object to this view.
972 *
973 * @param layoutData
974 * The layout data to assign
975 */
976 public void setLayoutData(final Object layoutData) {
977 fComposite.setLayoutData(layoutData);
978 }
979
980 /**
981 * Get the virtual table contained in this event table.
982 *
983 * @return The TMF virtual table
984 */
985 public TmfVirtualTable getTable() {
986 return fTable;
987 }
988
989 /**
990 * @param columnData
991 *
992 * FIXME: Add support for column selection
993 */
994 protected void setColumnHeaders(final ColumnData[] columnData) {
995 fTable.setColumnHeaders(columnData);
996 }
997
a0a88f65
AM
998 /**
999 * Set a table item's data.
1000 *
1001 * @param item
1002 * The item to set
1003 * @param event
1004 * Which trace event to link with this entry
1005 * @param rank
1006 * Which rank this event has in the trace/experiment
1007 */
db4721fb
PT
1008 protected void setItemData(final TableItem item, final ITmfEvent event, final long rank) {
1009 final ITmfEventField[] fields = extractItemFields(event);
1010 final String[] content = new String[fields.length];
1011 for (int i = 0; i < fields.length; i++) {
1012 content[i] = fields[i].getValue() != null ? fields[i].getValue().toString() : ""; //$NON-NLS-1$
1013 }
1014 item.setText(content);
93bfd50a 1015 item.setData(event);
db4721fb
PT
1016 item.setData(Key.TIMESTAMP, new TmfTimestamp(event.getTimestamp()));
1017 item.setData(Key.RANK, rank);
1018
1019 boolean bookmark = false;
1020 final Long markerId = fBookmarksMap.get(rank);
1021 if (markerId != null) {
1022 bookmark = true;
1023 try {
1024 final IMarker marker = fBookmarksFile.findMarker(markerId);
1025 item.setData(Key.BOOKMARK, marker.getAttribute(IMarker.MESSAGE));
1026 } catch (final CoreException e) {
1027 displayException(e);
1028 }
1029 } else {
1030 item.setData(Key.BOOKMARK, null);
1031 }
1032
1033 boolean searchMatch = false;
1034 boolean searchNoMatch = false;
1035 final ITmfFilter searchFilter = (ITmfFilter) fTable.getData(Key.SEARCH_OBJ);
1036 if (searchFilter != null) {
1037 if (searchFilter.matches(event)) {
1038 searchMatch = true;
1039 } else {
1040 searchNoMatch = true;
1041 }
1042 }
1043
1044 final ColorSetting colorSetting = ColorSettingsManager.getColorSetting(event);
1045 if (searchNoMatch) {
1046 item.setForeground(colorSetting.getDimmedForegroundColor());
1047 item.setBackground(colorSetting.getDimmedBackgroundColor());
1048 } else {
1049 item.setForeground(colorSetting.getForegroundColor());
1050 item.setBackground(colorSetting.getBackgroundColor());
1051 }
1052
1053 if (searchMatch) {
1054 if (bookmark) {
1055 item.setImage(SEARCH_MATCH_BOOKMARK_IMAGE);
1056 } else {
1057 item.setImage(SEARCH_MATCH_IMAGE);
1058 }
1059 } else if (bookmark) {
1060 item.setImage(BOOKMARK_IMAGE);
1061 } else {
1062 item.setImage((Image) null);
1063 }
1064 }
1065
a0a88f65
AM
1066 /**
1067 * Set the item data of the header row.
1068 *
1069 * @param item
1070 * The item to use as table header
1071 */
db4721fb
PT
1072 protected void setHeaderRowItemData(final TableItem item) {
1073 String txtKey = null;
1074 if (fHeaderState == HeaderState.SEARCH) {
1075 item.setImage(SEARCH_IMAGE);
1076 txtKey = Key.SEARCH_TXT;
1077 } else if (fHeaderState == HeaderState.FILTER) {
1078 item.setImage(FILTER_IMAGE);
1079 txtKey = Key.FILTER_TXT;
1080 }
1081 item.setForeground(fGrayColor);
1082 for (int i = 0; i < fTable.getColumns().length; i++) {
1083 final TableColumn column = fTable.getColumns()[i];
1084 final String filter = (String) column.getData(txtKey);
1085 if (filter == null) {
1086 if (fHeaderState == HeaderState.SEARCH) {
1087 item.setText(i, SEARCH_HINT);
1088 } else if (fHeaderState == HeaderState.FILTER) {
1089 item.setText(i, FILTER_HINT);
1090 }
1091 item.setForeground(i, fGrayColor);
1092 item.setFont(i, fTable.getFont());
1093 } else {
1094 item.setText(i, filter);
1095 item.setForeground(i, fGreenColor);
1096 item.setFont(i, fBoldFont);
1097 }
1098 }
1099 }
1100
a0a88f65
AM
1101 /**
1102 * Set the item data of the "filter status" row.
1103 *
1104 * @param item
1105 * The item to use as filter status row
1106 */
db4721fb
PT
1107 protected void setFilterStatusRowItemData(final TableItem item) {
1108 for (int i = 0; i < fTable.getColumns().length; i++) {
1109 if (i == 0) {
1110 if ((fTrace == null) || (fFilterCheckCount == fTrace.getNbEvents())) {
1111 item.setImage(FILTER_IMAGE);
1112 } else {
1113 item.setImage(STOP_IMAGE);
1114 }
1115 item.setText(0, fFilterMatchCount + "/" + fFilterCheckCount); //$NON-NLS-1$
1116 } else {
1117 item.setText(i, ""); //$NON-NLS-1$
1118 }
1119 }
93bfd50a 1120 item.setData(null);
db4721fb
PT
1121 item.setData(Key.TIMESTAMP, null);
1122 item.setData(Key.RANK, null);
1123 item.setForeground(null);
1124 item.setBackground(null);
1125 }
1126
a0a88f65
AM
1127 /**
1128 * Create an editor for the header.
1129 */
db4721fb
PT
1130 protected void createHeaderEditor() {
1131 final TableEditor tableEditor = fTable.createTableEditor();
1132 tableEditor.horizontalAlignment = SWT.LEFT;
1133 tableEditor.verticalAlignment = SWT.CENTER;
1134 tableEditor.grabHorizontal = true;
1135 tableEditor.minimumWidth = 50;
1136
1137 // Handle the header row selection
1138 fTable.addMouseListener(new MouseAdapter() {
1139 int columnIndex;
1140 TableColumn column;
1141 TableItem item;
1142
1143 @Override
1144 public void mouseDown(final MouseEvent event) {
1145 if (event.button != 1) {
1146 return;
1147 }
1148 // Identify the selected row
1149 final Point point = new Point(event.x, event.y);
1150 item = fTable.getItem(point);
1151
1152 // Header row selected
1153 if ((item != null) && (fTable.indexOf(item) == 0)) {
1154
1155 // Icon selected
1156 if (item.getImageBounds(0).contains(point)) {
1157 if (fHeaderState == HeaderState.SEARCH) {
1158 fHeaderState = HeaderState.FILTER;
1159 } else if (fHeaderState == HeaderState.FILTER) {
1160 fHeaderState = HeaderState.SEARCH;
1161 }
3f43dc48 1162 fTable.setSelection(0);
db4721fb
PT
1163 fTable.refresh();
1164 return;
1165 }
1166
1167 // Identify the selected column
1168 columnIndex = -1;
1169 for (int i = 0; i < fTable.getColumns().length; i++) {
1170 final Rectangle rect = item.getBounds(i);
1171 if (rect.contains(point)) {
1172 columnIndex = i;
1173 break;
1174 }
1175 }
1176
1177 if (columnIndex == -1) {
1178 return;
1179 }
1180
1181 column = fTable.getColumns()[columnIndex];
1182
1183 String txtKey = null;
1184 if (fHeaderState == HeaderState.SEARCH) {
1185 txtKey = Key.SEARCH_TXT;
1186 } else if (fHeaderState == HeaderState.FILTER) {
1187 txtKey = Key.FILTER_TXT;
1188 }
1189
1190 // The control that will be the editor must be a child of the Table
1191 final Text newEditor = (Text) fTable.createTableEditorControl(Text.class);
1192 final String headerString = (String) column.getData(txtKey);
1193 if (headerString != null) {
1194 newEditor.setText(headerString);
1195 }
1196 newEditor.addFocusListener(new FocusAdapter() {
1197 @Override
1198 public void focusLost(final FocusEvent e) {
1199 final boolean changed = updateHeader(newEditor.getText());
1200 if (changed) {
1201 applyHeader();
1202 }
1203 }
1204 });
1205 newEditor.addKeyListener(new KeyAdapter() {
1206 @Override
1207 public void keyPressed(final KeyEvent e) {
1208 if (e.character == SWT.CR) {
1209 updateHeader(newEditor.getText());
1210 applyHeader();
1211 } else if (e.character == SWT.ESC) {
1212 tableEditor.getEditor().dispose();
1213 }
1214 }
1215 });
1216 newEditor.selectAll();
1217 newEditor.setFocus();
1218 tableEditor.setEditor(newEditor, item, columnIndex);
1219 }
1220 }
1221
1222 /*
1223 * returns true is value was changed
1224 */
1225 private boolean updateHeader(final String text) {
1226 String objKey = null;
1227 String txtKey = null;
1228 if (fHeaderState == HeaderState.SEARCH) {
1229 objKey = Key.SEARCH_OBJ;
1230 txtKey = Key.SEARCH_TXT;
1231 } else if (fHeaderState == HeaderState.FILTER) {
1232 objKey = Key.FILTER_OBJ;
1233 txtKey = Key.FILTER_TXT;
1234 }
1235 if (text.trim().length() > 0) {
1236 try {
1237 final String regex = TmfFilterMatchesNode.regexFix(text);
1238 Pattern.compile(regex);
1239 if (regex.equals(column.getData(txtKey))) {
1240 tableEditor.getEditor().dispose();
1241 return false;
1242 }
1243 final TmfFilterMatchesNode filter = new TmfFilterMatchesNode(null);
1244 String fieldId = (String) column.getData(Key.FIELD_ID);
1245 if (fieldId == null) {
1246 fieldId = column.getText();
1247 }
1248 filter.setField(fieldId);
1249 filter.setRegex(regex);
1250 column.setData(objKey, filter);
1251 column.setData(txtKey, regex);
1252 } catch (final PatternSyntaxException ex) {
1253 tableEditor.getEditor().dispose();
1254 MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
1255 ex.getDescription(), ex.getMessage());
1256 return false;
1257 }
1258 } else {
1259 if (column.getData(txtKey) == null) {
1260 tableEditor.getEditor().dispose();
1261 return false;
1262 }
1263 column.setData(objKey, null);
1264 column.setData(txtKey, null);
1265 }
1266 return true;
1267 }
1268
1269 private void applyHeader() {
1270 if (fHeaderState == HeaderState.SEARCH) {
1271 stopSearchThread();
1272 final TmfFilterAndNode filter = new TmfFilterAndNode(null);
3dca7aa5
AM
1273 for (final TableColumn col : fTable.getColumns()) {
1274 final Object filterObj = col.getData(Key.SEARCH_OBJ);
db4721fb
PT
1275 if (filterObj instanceof ITmfFilterTreeNode) {
1276 filter.addChild((ITmfFilterTreeNode) filterObj);
1277 }
1278 }
1279 if (filter.getChildrenCount() > 0) {
1280 fTable.setData(Key.SEARCH_OBJ, filter);
1281 fTable.refresh();
1282 searchNext();
1283 fireSearchApplied(filter);
1284 } else {
1285 fTable.setData(Key.SEARCH_OBJ, null);
1286 fTable.refresh();
1287 fireSearchApplied(null);
1288 }
1289 } else if (fHeaderState == HeaderState.FILTER) {
1290 final TmfFilterAndNode filter = new TmfFilterAndNode(null);
3dca7aa5
AM
1291 for (final TableColumn col : fTable.getColumns()) {
1292 final Object filterObj = col.getData(Key.FILTER_OBJ);
db4721fb
PT
1293 if (filterObj instanceof ITmfFilterTreeNode) {
1294 filter.addChild((ITmfFilterTreeNode) filterObj);
1295 }
1296 }
1297 if (filter.getChildrenCount() > 0) {
1298 applyFilter(filter);
1299 } else {
1300 clearFilters();
1301 }
1302 }
1303
1304 tableEditor.getEditor().dispose();
1305 }
1306 });
1307
1308 fTable.addKeyListener(new KeyAdapter() {
1309 @Override
1310 public void keyPressed(final KeyEvent e) {
1311 e.doit = false;
1312 if (e.character == SWT.ESC) {
1313 stopFilterThread();
1314 stopSearchThread();
1315 fTable.refresh();
1316 } else if (e.character == SWT.DEL) {
1317 if (fHeaderState == HeaderState.SEARCH) {
1318 stopSearchThread();
1319 for (final TableColumn column : fTable.getColumns()) {
1320 column.setData(Key.SEARCH_OBJ, null);
1321 column.setData(Key.SEARCH_TXT, null);
1322 }
1323 fTable.setData(Key.SEARCH_OBJ, null);
1324 fTable.refresh();
1325 fireSearchApplied(null);
1326 } else if (fHeaderState == HeaderState.FILTER) {
1327 clearFilters();
1328 }
1329 } else if (e.character == SWT.CR) {
1330 if ((e.stateMask & SWT.SHIFT) == 0) {
1331 searchNext();
1332 } else {
1333 searchPrevious();
1334 }
1335 }
1336 }
1337 });
1338 }
1339
a0a88f65
AM
1340 /**
1341 * Send an event indicating a filter has been applied.
1342 *
1343 * @param filter
1344 * The filter that was just applied
1345 */
db4721fb 1346 protected void fireFilterApplied(final ITmfFilter filter) {
faa38350 1347 broadcast(new TmfEventFilterAppliedSignal(this, fTrace, filter));
db4721fb
PT
1348 }
1349
a0a88f65
AM
1350 /**
1351 * Send an event indicating that a search has been applied.
1352 *
1353 * @param filter
1354 * The search filter that was just applied
1355 */
db4721fb 1356 protected void fireSearchApplied(final ITmfFilter filter) {
faa38350 1357 broadcast(new TmfEventSearchAppliedSignal(this, fTrace, filter));
db4721fb
PT
1358 }
1359
a0a88f65
AM
1360 /**
1361 * Start the filtering thread.
1362 */
db4721fb
PT
1363 protected void startFilterThread() {
1364 synchronized (fFilterSyncObj) {
1365 final ITmfFilterTreeNode filter = (ITmfFilterTreeNode) fTable.getData(Key.FILTER_OBJ);
1366 if (fFilterThread == null || fFilterThread.filter != filter) {
1367 if (fFilterThread != null) {
1368 fFilterThread.cancel();
1369 fFilterThreadResume = false;
1370 }
1371 fFilterThread = new FilterThread(filter);
1372 fFilterThread.start();
1373 } else {
1374 fFilterThreadResume = true;
1375 }
1376 }
1377 }
1378
a0a88f65
AM
1379 /**
1380 * Stop the filtering thread.
1381 */
db4721fb
PT
1382 protected void stopFilterThread() {
1383 synchronized (fFilterSyncObj) {
1384 if (fFilterThread != null) {
1385 fFilterThread.cancel();
1386 fFilterThread = null;
1387 fFilterThreadResume = false;
1388 }
1389 }
1390 }
1391
1392 /**
a0a88f65
AM
1393 * Apply a filter.
1394 *
1395 * @param filter
1396 * The filter to apply
db4721fb
PT
1397 * @since 1.1
1398 */
1399 protected void applyFilter(ITmfFilter filter) {
f29f8868
BH
1400 stopFilterThread();
1401 stopSearchThread();
db4721fb
PT
1402 fFilterMatchCount = 0;
1403 fFilterCheckCount = 0;
1404 fCache.applyFilter(filter);
1405 fTable.clearAll();
1406 fTable.setData(Key.FILTER_OBJ, filter);
1407 fTable.setItemCount(3); // +1 for header row, +2 for top and bottom filter status rows
1408 startFilterThread();
1409 fireFilterApplied(filter);
1410 }
1411
a0a88f65
AM
1412 /**
1413 * Clear all currently active filters.
1414 */
db4721fb
PT
1415 protected void clearFilters() {
1416 if (fTable.getData(Key.FILTER_OBJ) == null) {
1417 return;
1418 }
1419 stopFilterThread();
1420 stopSearchThread();
1421 fCache.clearFilter();
1422 fTable.clearAll();
1423 for (final TableColumn column : fTable.getColumns()) {
1424 column.setData(Key.FILTER_OBJ, null);
1425 column.setData(Key.FILTER_TXT, null);
1426 }
1427 fTable.setData(Key.FILTER_OBJ, null);
1428 if (fTrace != null) {
1429 fTable.setItemCount((int) fTrace.getNbEvents() + 1); // +1 for header row
1430 } else {
1431 fTable.setItemCount(1); // +1 for header row
1432 }
1433 fFilterMatchCount = 0;
1434 fFilterCheckCount = 0;
1435 if (fSelectedRank >= 0) {
1436 fTable.setSelection((int) fSelectedRank + 1); // +1 for header row
1437 } else {
1438 fTable.setSelection(0);
1439 }
1440 fireFilterApplied(null);
3f43dc48 1441 updateStatusLine(null);
db4721fb
PT
1442 }
1443
a0a88f65
AM
1444 /**
1445 * Wrapper Thread object for the filtering thread.
1446 */
db4721fb
PT
1447 protected class FilterThread extends Thread {
1448 private final ITmfFilterTreeNode filter;
1449 private TmfDataRequest request;
1450 private boolean refreshBusy = false;
1451 private boolean refreshPending = false;
1452 private final Object syncObj = new Object();
1453
a0a88f65
AM
1454 /**
1455 * Constructor.
1456 *
1457 * @param filter
1458 * The filter this thread will be processing
1459 */
db4721fb
PT
1460 public FilterThread(final ITmfFilterTreeNode filter) {
1461 super("Filter Thread"); //$NON-NLS-1$
1462 this.filter = filter;
1463 }
1464
1465 @Override
1466 public void run() {
1467 if (fTrace == null) {
1468 return;
1469 }
1470 final int nbRequested = (int) (fTrace.getNbEvents() - fFilterCheckCount);
1471 if (nbRequested <= 0) {
1472 return;
1473 }
1474 request = new TmfDataRequest(ITmfEvent.class, (int) fFilterCheckCount,
672a642a 1475 nbRequested, ExecutionType.BACKGROUND) {
db4721fb
PT
1476 @Override
1477 public void handleData(final ITmfEvent event) {
1478 super.handleData(event);
1479 if (request.isCancelled()) {
1480 return;
1481 }
1482 if (filter.matches(event)) {
1483 final long rank = fFilterCheckCount;
1484 final int index = (int) fFilterMatchCount;
1485 fFilterMatchCount++;
bd54d363 1486 fCache.storeEvent(event, rank, index);
db4721fb
PT
1487 refreshTable();
1488 } else if ((fFilterCheckCount % 100) == 0) {
1489 refreshTable();
1490 }
1491 fFilterCheckCount++;
1492 }
1493 };
1494 ((ITmfDataProvider) fTrace).sendRequest(request);
1495 try {
1496 request.waitForCompletion();
1497 } catch (final InterruptedException e) {
1498 }
1499 refreshTable();
1500 synchronized (fFilterSyncObj) {
1501 fFilterThread = null;
1502 if (fFilterThreadResume) {
1503 fFilterThreadResume = false;
1504 fFilterThread = new FilterThread(filter);
1505 fFilterThread.start();
1506 }
1507 }
1508 }
1509
a0a88f65
AM
1510 /**
1511 * Refresh the filter.
1512 */
db4721fb
PT
1513 public void refreshTable() {
1514 synchronized (syncObj) {
1515 if (refreshBusy) {
1516 refreshPending = true;
1517 return;
1518 }
1519 refreshBusy = true;
1520 }
1521 Display.getDefault().asyncExec(new Runnable() {
1522 @Override
1523 public void run() {
1524 if (request.isCancelled()) {
1525 return;
1526 }
1527 if (fTable.isDisposed()) {
1528 return;
1529 }
1530 fTable.setItemCount((int) fFilterMatchCount + 3); // +1 for header row, +2 for top and bottom filter status rows
1531 fTable.refresh();
1532 synchronized (syncObj) {
1533 refreshBusy = false;
1534 if (refreshPending) {
1535 refreshPending = false;
1536 refreshTable();
1537 }
1538 }
1539 }
1540 });
1541 }
1542
a0a88f65
AM
1543 /**
1544 * Cancel this filtering thread.
1545 */
db4721fb
PT
1546 public void cancel() {
1547 if (request != null) {
1548 request.cancel();
1549 }
1550 }
1551 }
1552
a0a88f65
AM
1553 /**
1554 * Go to the next item of a search.
1555 */
db4721fb
PT
1556 protected void searchNext() {
1557 synchronized (fSearchSyncObj) {
1558 if (fSearchThread != null) {
1559 return;
1560 }
1561 final ITmfFilterTreeNode searchFilter = (ITmfFilterTreeNode) fTable.getData(Key.SEARCH_OBJ);
1562 if (searchFilter == null) {
1563 return;
1564 }
1565 final int selectionIndex = fTable.getSelectionIndex();
1566 int startIndex;
1567 if (selectionIndex > 0) {
1568 startIndex = selectionIndex; // -1 for header row, +1 for next event
1569 } else {
1570 // header row is selected, start at top event
1571 startIndex = Math.max(0, fTable.getTopIndex() - 1); // -1 for header row
1572 }
1573 final ITmfFilterTreeNode eventFilter = (ITmfFilterTreeNode) fTable.getData(Key.FILTER_OBJ);
60fb38b8 1574 if (eventFilter != null) {
db4721fb
PT
1575 startIndex = Math.max(0, startIndex - 1); // -1 for top filter status row
1576 }
1577 fSearchThread = new SearchThread(searchFilter, eventFilter, startIndex, fSelectedRank, Direction.FORWARD);
1578 fSearchThread.schedule();
1579 }
1580 }
1581
a0a88f65
AM
1582 /**
1583 * Go to the previous item of a search.
1584 */
db4721fb
PT
1585 protected void searchPrevious() {
1586 synchronized (fSearchSyncObj) {
1587 if (fSearchThread != null) {
1588 return;
1589 }
1590 final ITmfFilterTreeNode searchFilter = (ITmfFilterTreeNode) fTable.getData(Key.SEARCH_OBJ);
1591 if (searchFilter == null) {
1592 return;
1593 }
1594 final int selectionIndex = fTable.getSelectionIndex();
1595 int startIndex;
1596 if (selectionIndex > 0) {
1597 startIndex = selectionIndex - 2; // -1 for header row, -1 for previous event
1598 } else {
1599 // header row is selected, start at precedent of top event
1600 startIndex = fTable.getTopIndex() - 2; // -1 for header row, -1 for previous event
1601 }
1602 final ITmfFilterTreeNode eventFilter = (ITmfFilterTreeNode) fTable.getData(Key.FILTER_OBJ);
60fb38b8 1603 if (eventFilter != null) {
db4721fb
PT
1604 startIndex = startIndex - 1; // -1 for top filter status row
1605 }
1606 fSearchThread = new SearchThread(searchFilter, eventFilter, startIndex, fSelectedRank, Direction.BACKWARD);
1607 fSearchThread.schedule();
1608 }
1609 }
1610
a0a88f65
AM
1611 /**
1612 * Stop the search thread.
1613 */
db4721fb
PT
1614 protected void stopSearchThread() {
1615 fPendingGotoRank = -1;
1616 synchronized (fSearchSyncObj) {
1617 if (fSearchThread != null) {
1618 fSearchThread.cancel();
1619 fSearchThread = null;
1620 }
1621 }
1622 }
1623
a0a88f65
AM
1624 /**
1625 * Wrapper for the search thread.
1626 */
db4721fb 1627 protected class SearchThread extends Job {
a0a88f65
AM
1628
1629 private ITmfFilterTreeNode searchFilter;
1630 private ITmfFilterTreeNode eventFilter;
1631 private int startIndex;
1632 private int direction;
1633 private long rank;
1634 private long foundRank = -1;
1635 private TmfDataRequest request;
ae3ffd37 1636 private ITmfTimestamp foundTimestamp = null;
db4721fb 1637
a0a88f65
AM
1638 /**
1639 * Constructor.
1640 *
1641 * @param searchFilter
1642 * The search filter
1643 * @param eventFilter
1644 * The event filter
1645 * @param startIndex
1646 * The index at which we should start searching
1647 * @param currentRank
1648 * The current rank
1649 * @param direction
1650 * In which direction should we search, forward or backwards
1651 */
1652 public SearchThread(final ITmfFilterTreeNode searchFilter,
1653 final ITmfFilterTreeNode eventFilter, final int startIndex,
db4721fb
PT
1654 final long currentRank, final int direction) {
1655 super(Messages.TmfEventsTable_SearchingJobName);
1656 this.searchFilter = searchFilter;
1657 this.eventFilter = eventFilter;
1658 this.startIndex = startIndex;
1659 this.rank = currentRank;
1660 this.direction = direction;
1661 }
1662
1663 @Override
1664 protected IStatus run(final IProgressMonitor monitor) {
1665 if (fTrace == null) {
1666 return Status.OK_STATUS;
1667 }
1668 final Display display = Display.getDefault();
1669 if (startIndex < 0) {
1670 rank = (int) fTrace.getNbEvents() - 1;
1671 } else if (startIndex >= (fTable.getItemCount() - (eventFilter == null ? 1 : 3))) { // -1 for header row, -2 for top and bottom filter status rows
1672 rank = 0;
1673 } else {
1674 int idx = startIndex;
1675 while (foundRank == -1) {
1676 final CachedEvent event = fCache.peekEvent(idx);
1677 if (event == null) {
1678 break;
1679 }
1680 rank = event.rank;
1681 if (searchFilter.matches(event.event) && ((eventFilter == null) || eventFilter.matches(event.event))) {
1682 foundRank = event.rank;
ae3ffd37 1683 foundTimestamp = event.event.getTimestamp();
db4721fb
PT
1684 break;
1685 }
1686 if (direction == Direction.FORWARD) {
1687 idx++;
1688 } else {
1689 idx--;
1690 }
1691 }
1692 if (foundRank == -1) {
1693 if (direction == Direction.FORWARD) {
1694 rank++;
1695 if (rank > (fTrace.getNbEvents() - 1)) {
1696 rank = 0;
1697 }
1698 } else {
1699 rank--;
1700 if (rank < 0) {
1701 rank = (int) fTrace.getNbEvents() - 1;
1702 }
1703 }
1704 }
1705 }
1706 final int startRank = (int) rank;
1707 boolean wrapped = false;
1708 while (!monitor.isCanceled() && (foundRank == -1) && (fTrace != null)) {
1709 int nbRequested = (direction == Direction.FORWARD ? Integer.MAX_VALUE : Math.min((int) rank + 1, fTrace.getCacheSize()));
1710 if (direction == Direction.BACKWARD) {
1711 rank = Math.max(0, rank - fTrace.getCacheSize() + 1);
1712 }
672a642a 1713 request = new TmfDataRequest(ITmfEvent.class, (int) rank, nbRequested, ExecutionType.BACKGROUND) {
db4721fb
PT
1714 long currentRank = rank;
1715
1716 @Override
1717 public void handleData(final ITmfEvent event) {
1718 super.handleData(event);
1719 if (searchFilter.matches(event) && ((eventFilter == null) || eventFilter.matches(event))) {
1720 foundRank = currentRank;
ae3ffd37 1721 foundTimestamp = event.getTimestamp();
db4721fb
PT
1722 if (direction == Direction.FORWARD) {
1723 done();
1724 return;
1725 }
1726 }
1727 currentRank++;
1728 }
1729 };
1730 ((ITmfDataProvider) fTrace).sendRequest(request);
1731 try {
1732 request.waitForCompletion();
1733 if (request.isCancelled()) {
1734 return Status.OK_STATUS;
1735 }
1736 } catch (final InterruptedException e) {
1737 synchronized (fSearchSyncObj) {
1738 fSearchThread = null;
1739 }
1740 return Status.OK_STATUS;
1741 }
1742 if (foundRank == -1) {
1743 if (direction == Direction.FORWARD) {
1744 if (rank == 0) {
1745 synchronized (fSearchSyncObj) {
1746 fSearchThread = null;
1747 }
1748 return Status.OK_STATUS;
1749 }
1750 nbRequested = (int) rank;
1751 rank = 0;
1752 wrapped = true;
1753 } else {
1754 rank--;
1755 if (rank < 0) {
1756 rank = (int) fTrace.getNbEvents() - 1;
1757 wrapped = true;
1758 }
1759 if ((rank <= startRank) && wrapped) {
1760 synchronized (fSearchSyncObj) {
1761 fSearchThread = null;
1762 }
1763 return Status.OK_STATUS;
1764 }
1765 }
1766 }
1767 }
1768 int index = (int) foundRank;
1769 if (eventFilter != null) {
1770 index = fCache.getFilteredEventIndex(foundRank);
1771 }
1772 final int selection = index + 1 + (eventFilter != null ? +1 : 0); // +1 for header row, +1 for top filter status row
1773
1774 display.asyncExec(new Runnable() {
1775 @Override
1776 public void run() {
1777 if (monitor.isCanceled()) {
1778 return;
1779 }
1780 if (fTable.isDisposed()) {
1781 return;
1782 }
1783 fTable.setSelection(selection);
1784 fSelectedRank = foundRank;
ae3ffd37
PT
1785 fRawViewer.selectAndReveal(fSelectedRank);
1786 if (foundTimestamp != null) {
1787 broadcast(new TmfTimeSynchSignal(TmfEventsTable.this, foundTimestamp));
1788 }
a56ec2b8 1789 fireSelectionChanged(new SelectionChangedEvent(TmfEventsTable.this, getSelection()));
db4721fb
PT
1790 synchronized (fSearchSyncObj) {
1791 fSearchThread = null;
1792 }
3f43dc48 1793 updateStatusLine(null);
db4721fb
PT
1794 }
1795 });
1796 return Status.OK_STATUS;
1797 }
1798
1799 @Override
1800 protected void canceling() {
1801 request.cancel();
1802 synchronized (fSearchSyncObj) {
1803 fSearchThread = null;
1804 }
1805 }
1806 }
1807
a0a88f65
AM
1808 /**
1809 * Create the resources.
1810 */
db4721fb
PT
1811 protected void createResources() {
1812 fGrayColor = fResourceManager.createColor(ColorUtil.blend(fTable.getBackground().getRGB(), fTable
1813 .getForeground().getRGB()));
1814 fGreenColor = fTable.getDisplay().getSystemColor(SWT.COLOR_DARK_GREEN);
1815 fBoldFont = fResourceManager.createFont(FontDescriptor.createFrom(fTable.getFont()).setStyle(SWT.BOLD));
1816 }
1817
a0a88f65
AM
1818 /**
1819 * Pack the columns.
1820 */
db4721fb
PT
1821 protected void packColumns() {
1822 if (fPackDone) {
1823 return;
1824 }
f29f8868
BH
1825 boolean isLinux = System.getProperty("os.name").contains("Linux") ? true : false; //$NON-NLS-1$ //$NON-NLS-2$
1826
1827 TableColumn tableColumns[] = fTable.getColumns();
1828 for (int i = 0; i < tableColumns.length; i++) {
1829 final TableColumn column = tableColumns[i];
db4721fb
PT
1830 final int headerWidth = column.getWidth();
1831 column.pack();
f29f8868
BH
1832 // Workaround for Linux which doesn't consider the image width of
1833 // search/filter row in TableColumn.pack() after having executed
1834 // TableItem.setImage((Image)null) for other rows than search/filter row.
1835 if (isLinux && (i == 0)) {
1836 column.setWidth(column.getWidth() + SEARCH_IMAGE.getBounds().width);
1837 }
1838
db4721fb
PT
1839 if (column.getWidth() < headerWidth) {
1840 column.setWidth(headerWidth);
1841 }
1842 }
1843 fPackDone = true;
1844 }
1845
d3de0920
XR
1846 /**
1847 * Extract the fields of an event (item in the table).
1848 *
1849 * @param event
1850 * The event to extract from
1851 * @return The array of fields
1852 * @since 2.2
1853 */
1854 public final ITmfEventField[] getItemFields(final ITmfEvent event) {
1855 return extractItemFields(event);
1856 }
1857
db4721fb 1858 /**
a0a88f65
AM
1859 * Extract the fields of an event (item in the table).
1860 *
db4721fb 1861 * @param event
a0a88f65
AM
1862 * The event to extract from
1863 * @return The array of fields
db4721fb
PT
1864 *
1865 * FIXME: Add support for column selection
1866 */
1867 protected ITmfEventField[] extractItemFields(final ITmfEvent event) {
d3de0920 1868 ITmfEventField[] fields = EMPTY_FIELD_ARRAY;
db4721fb
PT
1869 if (event != null) {
1870 final String timestamp = event.getTimestamp().toString();
1871 final String source = event.getSource();
1872 final String type = event.getType().getName();
1873 final String reference = event.getReference();
a4a6acf4 1874 final String content = event.getContent().toString();
db4721fb 1875 fields = new TmfEventField[] {
214cc822
AM
1876 new TmfEventField(ITmfEvent.EVENT_FIELD_TIMESTAMP, timestamp, null),
1877 new TmfEventField(ITmfEvent.EVENT_FIELD_SOURCE, source, null),
1878 new TmfEventField(ITmfEvent.EVENT_FIELD_TYPE, type, null),
1879 new TmfEventField(ITmfEvent.EVENT_FIELD_REFERENCE, reference, null),
1880 new TmfEventField(ITmfEvent.EVENT_FIELD_CONTENT, content, null)
db4721fb
PT
1881 };
1882 }
1883 return fields;
1884 }
1885
1886 /**
1887 * Notify this table that is got the UI focus.
1888 */
1889 public void setFocus() {
1890 fTable.setFocus();
1891 }
1892
1893 /**
1894 * Assign a new trace to this event table.
1895 *
1896 * @param trace
1897 * The trace to assign to this event table
1898 * @param disposeOnClose
1899 * true if the trace should be disposed when the table is
1900 * disposed
1901 */
1902 public void setTrace(final ITmfTrace trace, final boolean disposeOnClose) {
1903 if ((fTrace != null) && fDisposeOnClose) {
1904 fTrace.dispose();
1905 }
1906 fTrace = trace;
1907 fPackDone = false;
1908 fSelectedRank = 0;
1909 fDisposeOnClose = disposeOnClose;
1910
1911 // Perform the updates on the UI thread
1912 fTable.getDisplay().syncExec(new Runnable() {
1913 @Override
1914 public void run() {
1915 fTable.removeAll();
1916 fCache.setTrace(fTrace); // Clear the cache
1917 if (fTrace != null) {
1918 if (!fTable.isDisposed() && (fTrace != null)) {
1919 if (fTable.getData(Key.FILTER_OBJ) == null) {
1920 fTable.setItemCount((int) fTrace.getNbEvents() + 1); // +1 for header row
1921 } else {
1922 stopFilterThread();
1923 fFilterMatchCount = 0;
1924 fFilterCheckCount = 0;
1925 fTable.setItemCount(3); // +1 for header row, +2 for top and bottom filter status rows
1926 startFilterThread();
1927 }
1928 }
db4721fb 1929 }
ea279a69 1930 fRawViewer.setTrace(fTrace);
db4721fb
PT
1931 }
1932 });
1933 }
1934
3f43dc48
PT
1935 /**
1936 * Assign the status line manager
1937 *
1938 * @param statusLineManager
1939 * The status line manager, or null to disable status line messages
4b121c48 1940 * @since 2.1
3f43dc48
PT
1941 */
1942 public void setStatusLineManager(IStatusLineManager statusLineManager) {
1943 if (fStatusLineManager != null && statusLineManager == null) {
1944 fStatusLineManager.setMessage(""); //$NON-NLS-1$
1945 }
1946 fStatusLineManager = statusLineManager;
1947 }
1948
1949 private void updateStatusLine(ITmfTimestamp delta) {
1950 if (fStatusLineManager != null) {
1951 if (delta != null) {
1952 fStatusLineManager.setMessage("\u0394: " + delta); //$NON-NLS-1$
1953 } else {
1954 fStatusLineManager.setMessage(null);
1955 }
1956 }
1957 }
1958
db4721fb
PT
1959 // ------------------------------------------------------------------------
1960 // Event cache
1961 // ------------------------------------------------------------------------
1962
1963 /**
1964 * Notify that the event cache has been updated
1965 *
1966 * @param completed
1967 * Also notify if the populating of the cache is complete, or
1968 * not.
1969 */
1970 public void cacheUpdated(final boolean completed) {
1971 synchronized (fCacheUpdateSyncObj) {
1972 if (fCacheUpdateBusy) {
1973 fCacheUpdatePending = true;
1974 fCacheUpdateCompleted = completed;
1975 return;
1976 }
1977 fCacheUpdateBusy = true;
1978 }
1979 // Event cache is now updated. Perform update on the UI thread
1980 if (!fTable.isDisposed()) {
1981 fTable.getDisplay().asyncExec(new Runnable() {
1982 @Override
1983 public void run() {
1984 if (!fTable.isDisposed()) {
1985 fTable.refresh();
1986 packColumns();
1987 }
1988 if (completed) {
1989 populateCompleted();
1990 }
1991 synchronized (fCacheUpdateSyncObj) {
1992 fCacheUpdateBusy = false;
1993 if (fCacheUpdatePending) {
1994 fCacheUpdatePending = false;
1995 cacheUpdated(fCacheUpdateCompleted);
1996 }
1997 }
1998 }
1999 });
2000 }
2001 }
2002
a0a88f65
AM
2003 /**
2004 * Callback for when populating the table is complete.
2005 */
db4721fb
PT
2006 protected void populateCompleted() {
2007 // Nothing by default;
2008 }
2009
93bfd50a
PT
2010 // ------------------------------------------------------------------------
2011 // ISelectionProvider
2012 // ------------------------------------------------------------------------
2013
93bfd50a
PT
2014 /**
2015 * @since 2.0
2016 */
2017 @Override
2018 public void addSelectionChangedListener(ISelectionChangedListener listener) {
2019 selectionChangedListeners.add(listener);
2020 }
2021
93bfd50a
PT
2022 /**
2023 * @since 2.0
2024 */
2025 @Override
2026 public ISelection getSelection() {
2027 if (fTable == null || fTable.isDisposed()) {
2028 return StructuredSelection.EMPTY;
2029 }
2030 List<Object> list = new ArrayList<Object>(fTable.getSelection().length);
2031 for (TableItem item : fTable.getSelection()) {
2032 if (item.getData() != null) {
2033 list.add(item.getData());
2034 }
2035 }
2036 return new StructuredSelection(list);
2037 }
2038
93bfd50a
PT
2039 /**
2040 * @since 2.0
2041 */
2042 @Override
2043 public void removeSelectionChangedListener(ISelectionChangedListener listener) {
2044 selectionChangedListeners.remove(listener);
2045 }
2046
93bfd50a
PT
2047 /**
2048 * @since 2.0
2049 */
2050 @Override
2051 public void setSelection(ISelection selection) {
2052 // not implemented
2053 }
2054
2055 /**
2056 * Notifies any selection changed listeners that the viewer's selection has changed.
2057 * Only listeners registered at the time this method is called are notified.
2058 *
2059 * @param event a selection changed event
2060 *
2061 * @see ISelectionChangedListener#selectionChanged
2062 * @since 2.0
2063 */
2064 protected void fireSelectionChanged(final SelectionChangedEvent event) {
2065 Object[] listeners = selectionChangedListeners.getListeners();
2066 for (int i = 0; i < listeners.length; ++i) {
2067 final ISelectionChangedListener l = (ISelectionChangedListener) listeners[i];
2068 SafeRunnable.run(new SafeRunnable() {
faa38350 2069 @Override
93bfd50a
PT
2070 public void run() {
2071 l.selectionChanged(event);
2072 }
2073 });
2074 }
2075 }
2076
db4721fb
PT
2077 // ------------------------------------------------------------------------
2078 // Bookmark handling
2079 // ------------------------------------------------------------------------
2080
2081 /**
2082 * Add a bookmark to this event table.
2083 *
2084 * @param bookmarksFile
2085 * The file to use for the bookmarks
2086 */
2087 public void addBookmark(final IFile bookmarksFile) {
2088 fBookmarksFile = bookmarksFile;
2089 final TableItem[] selection = fTable.getSelection();
2090 if (selection.length > 0) {
2091 final TableItem tableItem = selection[0];
2092 if (tableItem.getData(Key.RANK) != null) {
2093 final StringBuffer defaultMessage = new StringBuffer();
2094 for (int i = 0; i < fTable.getColumns().length; i++) {
60fb38b8 2095 if (i > 0) {
db4721fb
PT
2096 defaultMessage.append(", "); //$NON-NLS-1$
2097 }
2098 defaultMessage.append(tableItem.getText(i));
2099 }
3be2946f
PT
2100 final InputDialog dialog = new MultiLineInputDialog(
2101 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
2102 Messages.TmfEventsTable_AddBookmarkDialogTitle,
2103 Messages.TmfEventsTable_AddBookmarkDialogMessage,
2104 defaultMessage.toString());
db4721fb
PT
2105 if (dialog.open() == Window.OK) {
2106 final String message = dialog.getValue();
2107 try {
2108 final IMarker bookmark = bookmarksFile.createMarker(IMarker.BOOKMARK);
2109 if (bookmark.exists()) {
2110 bookmark.setAttribute(IMarker.MESSAGE, message.toString());
2111 final long rank = (Long) tableItem.getData(Key.RANK);
2112 final int location = (int) rank;
2113 bookmark.setAttribute(IMarker.LOCATION, (Integer) location);
2114 fBookmarksMap.put(rank, bookmark.getId());
2115 fTable.refresh();
2116 }
2117 } catch (final CoreException e) {
2118 displayException(e);
2119 }
2120 }
2121 }
2122 }
2123
2124 }
2125
2126 /**
2127 * Remove a bookmark from this event table.
2128 *
2129 * @param bookmark
2130 * The bookmark to remove
2131 */
2132 public void removeBookmark(final IMarker bookmark) {
2133 for (final Entry<Long, Long> entry : fBookmarksMap.entrySet()) {
2134 if (entry.getValue().equals(bookmark.getId())) {
2135 fBookmarksMap.remove(entry.getKey());
2136 fTable.refresh();
2137 return;
2138 }
2139 }
2140 }
2141
2142 private void toggleBookmark(final long rank) {
2143 if (fBookmarksFile == null) {
2144 return;
2145 }
2146 if (fBookmarksMap.containsKey(rank)) {
2147 final Long markerId = fBookmarksMap.remove(rank);
2148 fTable.refresh();
2149 try {
2150 final IMarker bookmark = fBookmarksFile.findMarker(markerId);
2151 if (bookmark != null) {
2152 bookmark.delete();
2153 }
2154 } catch (final CoreException e) {
2155 displayException(e);
2156 }
2157 } else {
2158 addBookmark(fBookmarksFile);
2159 }
2160 }
2161
2162 /**
2163 * Refresh the bookmarks assigned to this trace, from the contents of a
2164 * bookmark file.
2165 *
2166 * @param bookmarksFile
2167 * The bookmark file to use
2168 */
2169 public void refreshBookmarks(final IFile bookmarksFile) {
2170 fBookmarksFile = bookmarksFile;
2171 if (bookmarksFile == null) {
2172 fBookmarksMap.clear();
2173 fTable.refresh();
2174 return;
2175 }
2176 try {
2177 fBookmarksMap.clear();
2178 for (final IMarker bookmark : bookmarksFile.findMarkers(IMarker.BOOKMARK, false, IResource.DEPTH_ZERO)) {
2179 final int location = bookmark.getAttribute(IMarker.LOCATION, -1);
2180 if (location != -1) {
2181 final long rank = location;
2182 fBookmarksMap.put(rank, bookmark.getId());
2183 }
2184 }
2185 fTable.refresh();
2186 } catch (final CoreException e) {
2187 displayException(e);
2188 }
2189 }
2190
2191 @Override
2192 public void gotoMarker(final IMarker marker) {
2193 final int rank = marker.getAttribute(IMarker.LOCATION, -1);
2194 if (rank != -1) {
2195 int index = rank;
2196 if (fTable.getData(Key.FILTER_OBJ) != null) {
2197 index = fCache.getFilteredEventIndex(rank) + 1; // +1 for top filter status row
2198 } else if (rank >= fTable.getItemCount()) {
2199 fPendingGotoRank = rank;
2200 }
a56ec2b8 2201 fSelectedRank = rank;
db4721fb 2202 fTable.setSelection(index + 1); // +1 for header row
3f43dc48 2203 updateStatusLine(null);
db4721fb
PT
2204 }
2205 }
2206
2207 // ------------------------------------------------------------------------
2208 // Listeners
2209 // ------------------------------------------------------------------------
2210
db4721fb
PT
2211 @Override
2212 public void colorSettingsChanged(final ColorSetting[] colorSettings) {
2213 fTable.refresh();
2214 }
2215
db4721fb
PT
2216 // ------------------------------------------------------------------------
2217 // Signal handlers
2218 // ------------------------------------------------------------------------
2219
db4721fb
PT
2220 /**
2221 * Handler for the trace updated signal
2222 *
2223 * @param signal
2224 * The incoming signal
2225 */
2226 @TmfSignalHandler
2227 public void traceUpdated(final TmfTraceUpdatedSignal signal) {
2228 if ((signal.getTrace() != fTrace) || fTable.isDisposed()) {
2229 return;
2230 }
2231 // Perform the refresh on the UI thread
2232 Display.getDefault().asyncExec(new Runnable() {
2233 @Override
2234 public void run() {
2235 if (!fTable.isDisposed() && (fTrace != null)) {
2236 if (fTable.getData(Key.FILTER_OBJ) == null) {
2237 fTable.setItemCount((int) fTrace.getNbEvents() + 1); // +1 for header row
2238 if ((fPendingGotoRank != -1) && ((fPendingGotoRank + 1) < fTable.getItemCount())) { // +1 for header row
2239 fTable.setSelection((int) fPendingGotoRank + 1); // +1 for header row
2240 fPendingGotoRank = -1;
3f43dc48 2241 updateStatusLine(null);
db4721fb
PT
2242 }
2243 } else {
2244 startFilterThread();
2245 }
2246 }
2247 if (!fRawViewer.isDisposed() && (fTrace != null)) {
2248 fRawViewer.refreshEventCount();
2249 }
2250 }
2251 });
2252 }
2253
2254 /**
2255 * Handler for the time synch signal.
2256 *
2257 * @param signal
2258 * The incoming signal
2259 */
2260 @TmfSignalHandler
2261 public void currentTimeUpdated(final TmfTimeSynchSignal signal) {
2262 if ((signal.getSource() != this) && (fTrace != null) && (!fTable.isDisposed())) {
2263
2264 // Create a request for one event that will be queued after other ongoing requests. When this request is completed
2265 // do the work to select the actual event with the timestamp specified in the signal. This procedure prevents
2266 // the method fTrace.getRank() from interfering and delaying ongoing requests.
9765b9f1 2267 final TmfDataRequest subRequest = new TmfDataRequest(ITmfEvent.class,
672a642a 2268 0, 1, ExecutionType.FOREGROUND) {
db4721fb 2269
0fcf3b09 2270 TmfTimestamp ts = new TmfTimestamp(signal.getBeginTime());
db4721fb
PT
2271
2272 @Override
2273 public void handleData(final ITmfEvent event) {
2274 super.handleData(event);
2275 }
2276
2277 @Override
5419a136 2278 public void handleCompleted() {
db4721fb
PT
2279 super.handleCompleted();
2280 if (fTrace == null) {
2281 return;
2282 }
2283
2284 // Verify if the event is within the trace range and adjust if necessary
2285 ITmfTimestamp timestamp = ts;
2286 if (timestamp.compareTo(fTrace.getStartTime(), true) == -1) {
2287 timestamp = fTrace.getStartTime();
2288 }
2289 if (timestamp.compareTo(fTrace.getEndTime(), true) == 1) {
2290 timestamp = fTrace.getEndTime();
2291 }
2292
2293 // Get the rank of the selected event in the table
2294 final ITmfContext context = fTrace.seekEvent(timestamp);
2295 final long rank = context.getRank();
4c9f2944 2296 context.dispose();
db4721fb
PT
2297 fSelectedRank = rank;
2298
2299 fTable.getDisplay().asyncExec(new Runnable() {
2300 @Override
2301 public void run() {
2302 // Return if table is disposed
2303 if (fTable.isDisposed()) {
2304 return;
2305 }
2306
2307 int index = (int) rank;
2308 if (fTable.isDisposed()) {
2309 return;
2310 }
60fb38b8 2311 if (fTable.getData(Key.FILTER_OBJ) != null) {
db4721fb
PT
2312 index = fCache.getFilteredEventIndex(rank) + 1; // +1 for top filter status row
2313 }
2314 fTable.setSelection(index + 1); // +1 for header row
2315 fRawViewer.selectAndReveal(rank);
3f43dc48 2316 updateStatusLine(null);
db4721fb
PT
2317 }
2318 });
2319 }
2320 };
2321
2322 ((ITmfDataProvider) fTrace).sendRequest(subRequest);
2323 }
2324 }
2325
2326 // ------------------------------------------------------------------------
2327 // Error handling
2328 // ------------------------------------------------------------------------
2329
2330 /**
2331 * Display an exception in a message box
2332 *
2333 * @param e the exception
2334 */
2335 private static void displayException(final Exception e) {
2336 final MessageBox mb = new MessageBox(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
60fb38b8 2337 mb.setText(e.getClass().getSimpleName());
db4721fb
PT
2338 mb.setMessage(e.getMessage());
2339 mb.open();
2340 }
2341
f8177ba2
FC
2342 /**
2343 * @since 2.0
2344 */
2345 public void refresh() {
2346 fCache.clear();
2347 fTable.refresh();
2348 fTable.redraw();
2349 }
db4721fb 2350}
This page took 0.179513 seconds and 5 git commands to generate.