tmf: Don't use ITmfEventField in TmfEventsTable
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / viewers / events / TmfEventsTable.java
CommitLineData
db4721fb 1/*******************************************************************************
60ae41e1 2 * Copyright (c) 2010, 2014 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;
fd3f1eff 78import org.eclipse.linuxtools.tmf.core.component.ITmfEventProvider;
db4721fb
PT
79import org.eclipse.linuxtools.tmf.core.component.TmfComponent;
80import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
f47ed727
BH
81import org.eclipse.linuxtools.tmf.core.event.lookup.ITmfCallsite;
82import org.eclipse.linuxtools.tmf.core.event.lookup.ITmfModelLookup;
83import org.eclipse.linuxtools.tmf.core.event.lookup.ITmfSourceLookup;
db4721fb
PT
84import org.eclipse.linuxtools.tmf.core.filter.ITmfFilter;
85import org.eclipse.linuxtools.tmf.core.filter.model.ITmfFilterTreeNode;
86import org.eclipse.linuxtools.tmf.core.filter.model.TmfFilterAndNode;
87import org.eclipse.linuxtools.tmf.core.filter.model.TmfFilterMatchesNode;
88import org.eclipse.linuxtools.tmf.core.filter.model.TmfFilterNode;
fd3f1eff
AM
89import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest.ExecutionType;
90import org.eclipse.linuxtools.tmf.core.request.TmfEventRequest;
faa38350
PT
91import org.eclipse.linuxtools.tmf.core.signal.TmfEventFilterAppliedSignal;
92import org.eclipse.linuxtools.tmf.core.signal.TmfEventSearchAppliedSignal;
db4721fb
PT
93import org.eclipse.linuxtools.tmf.core.signal.TmfSignalHandler;
94import org.eclipse.linuxtools.tmf.core.signal.TmfTimeSynchSignal;
95import org.eclipse.linuxtools.tmf.core.signal.TmfTraceUpdatedSignal;
3bd46eef 96import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp;
fd3f1eff 97import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange;
3bd46eef 98import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
db4721fb 99import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
db4721fb 100import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
a3db8436 101import org.eclipse.linuxtools.tmf.core.trace.location.ITmfLocation;
db4721fb
PT
102import org.eclipse.linuxtools.tmf.ui.viewers.events.TmfEventsCache.CachedEvent;
103import org.eclipse.linuxtools.tmf.ui.views.colors.ColorSetting;
104import org.eclipse.linuxtools.tmf.ui.views.colors.ColorSettingsManager;
105import org.eclipse.linuxtools.tmf.ui.views.colors.IColorSettingsListener;
106import org.eclipse.linuxtools.tmf.ui.views.filter.FilterManager;
107import org.eclipse.linuxtools.tmf.ui.widgets.rawviewer.TmfRawEventViewer;
108import org.eclipse.linuxtools.tmf.ui.widgets.virtualtable.ColumnData;
109import org.eclipse.linuxtools.tmf.ui.widgets.virtualtable.TmfVirtualTable;
110import org.eclipse.swt.SWT;
111import org.eclipse.swt.custom.SashForm;
112import org.eclipse.swt.custom.TableEditor;
113import org.eclipse.swt.events.FocusAdapter;
114import org.eclipse.swt.events.FocusEvent;
115import org.eclipse.swt.events.KeyAdapter;
116import org.eclipse.swt.events.KeyEvent;
117import org.eclipse.swt.events.MouseAdapter;
118import org.eclipse.swt.events.MouseEvent;
119import org.eclipse.swt.events.SelectionAdapter;
120import org.eclipse.swt.events.SelectionEvent;
121import org.eclipse.swt.graphics.Color;
122import org.eclipse.swt.graphics.Font;
123import org.eclipse.swt.graphics.Image;
124import org.eclipse.swt.graphics.Point;
125import org.eclipse.swt.graphics.Rectangle;
126import org.eclipse.swt.layout.FillLayout;
127import org.eclipse.swt.layout.GridData;
128import org.eclipse.swt.layout.GridLayout;
129import org.eclipse.swt.widgets.Composite;
130import org.eclipse.swt.widgets.Display;
131import org.eclipse.swt.widgets.Event;
132import org.eclipse.swt.widgets.Label;
133import org.eclipse.swt.widgets.Listener;
134import org.eclipse.swt.widgets.Menu;
135import org.eclipse.swt.widgets.MessageBox;
136import org.eclipse.swt.widgets.Shell;
137import org.eclipse.swt.widgets.TableColumn;
138import org.eclipse.swt.widgets.TableItem;
139import org.eclipse.swt.widgets.Text;
ac44ef71 140import org.eclipse.ui.IWorkbenchPage;
db4721fb 141import org.eclipse.ui.PlatformUI;
d3de0920 142import org.eclipse.ui.commands.ICommandService;
60fb38b8 143import org.eclipse.ui.dialogs.ListDialog;
d3de0920 144import org.eclipse.ui.handlers.IHandlerService;
ac44ef71 145import org.eclipse.ui.ide.IDE;
db4721fb
PT
146import org.eclipse.ui.ide.IGotoMarker;
147import org.eclipse.ui.themes.ColorUtil;
148
149/**
150 * The generic TMF Events table
151 *
152 * This is a view that will list events that are read from a trace.
153 *
154 * @version 1.0
155 * @author Francois Chouinard
156 * @author Patrick Tasse
93bfd50a 157 * @since 2.0
db4721fb 158 */
faa38350 159public class TmfEventsTable extends TmfComponent implements IGotoMarker, IColorSettingsListener, ISelectionProvider {
db4721fb 160
cf37ad9f
AM
161 /**
162 * Empty string array, used by {@link #getItemStrings}.
163 * @since 3.0
164 */
165 protected static final String[] EMPTY_STRING_ARRAY = new String[0];
166
db4721fb
PT
167 private static final Image BOOKMARK_IMAGE = Activator.getDefault().getImageFromPath(
168 "icons/elcl16/bookmark_obj.gif"); //$NON-NLS-1$
169 private static final Image SEARCH_IMAGE = Activator.getDefault().getImageFromPath("icons/elcl16/search.gif"); //$NON-NLS-1$
170 private static final Image SEARCH_MATCH_IMAGE = Activator.getDefault().getImageFromPath(
171 "icons/elcl16/search_match.gif"); //$NON-NLS-1$
172 private static final Image SEARCH_MATCH_BOOKMARK_IMAGE = Activator.getDefault().getImageFromPath(
173 "icons/elcl16/search_match_bookmark.gif"); //$NON-NLS-1$
174 private static final Image FILTER_IMAGE = Activator.getDefault()
175 .getImageFromPath("icons/elcl16/filter_items.gif"); //$NON-NLS-1$
176 private static final Image STOP_IMAGE = Activator.getDefault().getImageFromPath("icons/elcl16/stop.gif"); //$NON-NLS-1$
177 private static final String SEARCH_HINT = Messages.TmfEventsTable_SearchHint;
178 private static final String FILTER_HINT = Messages.TmfEventsTable_FilterHint;
179 private static final int MAX_CACHE_SIZE = 1000;
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>
507b1336 266 private Map<Long, Long> fBookmarksMap = new HashMap<>();
a0a88f65
AM
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$
507b1336 647 final ArrayList<IFile> files = new ArrayList<>();
60fb38b8
PT
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 {
507b1336 753 HashMap<String, Object> parameters = new HashMap<>();
d3de0920
XR
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 {
0126a8ca 805 Long fRank;
db4721fb 806
0126a8ca 807 public ToggleBookmarkAction(final String text, final Long rank) {
db4721fb
PT
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();
3a97628a 967 fRawViewer.dispose();
db4721fb
PT
968 super.dispose();
969 }
970
971 /**
972 * Assign a layout data object to this view.
973 *
974 * @param layoutData
975 * The layout data to assign
976 */
977 public void setLayoutData(final Object layoutData) {
978 fComposite.setLayoutData(layoutData);
979 }
980
981 /**
982 * Get the virtual table contained in this event table.
983 *
984 * @return The TMF virtual table
985 */
986 public TmfVirtualTable getTable() {
987 return fTable;
988 }
989
990 /**
991 * @param columnData
992 *
993 * FIXME: Add support for column selection
994 */
995 protected void setColumnHeaders(final ColumnData[] columnData) {
996 fTable.setColumnHeaders(columnData);
997 }
998
a0a88f65
AM
999 /**
1000 * Set a table item's data.
1001 *
1002 * @param item
1003 * The item to set
1004 * @param event
1005 * Which trace event to link with this entry
1006 * @param rank
1007 * Which rank this event has in the trace/experiment
1008 */
db4721fb 1009 protected void setItemData(final TableItem item, final ITmfEvent event, final long rank) {
cf37ad9f 1010 item.setText(getItemStrings(event));
93bfd50a 1011 item.setData(event);
db4721fb
PT
1012 item.setData(Key.TIMESTAMP, new TmfTimestamp(event.getTimestamp()));
1013 item.setData(Key.RANK, rank);
1014
1015 boolean bookmark = false;
1016 final Long markerId = fBookmarksMap.get(rank);
1017 if (markerId != null) {
1018 bookmark = true;
1019 try {
1020 final IMarker marker = fBookmarksFile.findMarker(markerId);
1021 item.setData(Key.BOOKMARK, marker.getAttribute(IMarker.MESSAGE));
1022 } catch (final CoreException e) {
1023 displayException(e);
1024 }
1025 } else {
1026 item.setData(Key.BOOKMARK, null);
1027 }
1028
1029 boolean searchMatch = false;
1030 boolean searchNoMatch = false;
1031 final ITmfFilter searchFilter = (ITmfFilter) fTable.getData(Key.SEARCH_OBJ);
1032 if (searchFilter != null) {
1033 if (searchFilter.matches(event)) {
1034 searchMatch = true;
1035 } else {
1036 searchNoMatch = true;
1037 }
1038 }
1039
1040 final ColorSetting colorSetting = ColorSettingsManager.getColorSetting(event);
1041 if (searchNoMatch) {
1042 item.setForeground(colorSetting.getDimmedForegroundColor());
1043 item.setBackground(colorSetting.getDimmedBackgroundColor());
1044 } else {
1045 item.setForeground(colorSetting.getForegroundColor());
1046 item.setBackground(colorSetting.getBackgroundColor());
1047 }
1048
1049 if (searchMatch) {
1050 if (bookmark) {
1051 item.setImage(SEARCH_MATCH_BOOKMARK_IMAGE);
1052 } else {
1053 item.setImage(SEARCH_MATCH_IMAGE);
1054 }
1055 } else if (bookmark) {
1056 item.setImage(BOOKMARK_IMAGE);
1057 } else {
1058 item.setImage((Image) null);
1059 }
1060 }
1061
a0a88f65
AM
1062 /**
1063 * Set the item data of the header row.
1064 *
1065 * @param item
1066 * The item to use as table header
1067 */
db4721fb
PT
1068 protected void setHeaderRowItemData(final TableItem item) {
1069 String txtKey = null;
1070 if (fHeaderState == HeaderState.SEARCH) {
1071 item.setImage(SEARCH_IMAGE);
1072 txtKey = Key.SEARCH_TXT;
1073 } else if (fHeaderState == HeaderState.FILTER) {
1074 item.setImage(FILTER_IMAGE);
1075 txtKey = Key.FILTER_TXT;
1076 }
1077 item.setForeground(fGrayColor);
1078 for (int i = 0; i < fTable.getColumns().length; i++) {
1079 final TableColumn column = fTable.getColumns()[i];
1080 final String filter = (String) column.getData(txtKey);
1081 if (filter == null) {
1082 if (fHeaderState == HeaderState.SEARCH) {
1083 item.setText(i, SEARCH_HINT);
1084 } else if (fHeaderState == HeaderState.FILTER) {
1085 item.setText(i, FILTER_HINT);
1086 }
1087 item.setForeground(i, fGrayColor);
1088 item.setFont(i, fTable.getFont());
1089 } else {
1090 item.setText(i, filter);
1091 item.setForeground(i, fGreenColor);
1092 item.setFont(i, fBoldFont);
1093 }
1094 }
1095 }
1096
a0a88f65
AM
1097 /**
1098 * Set the item data of the "filter status" row.
1099 *
1100 * @param item
1101 * The item to use as filter status row
1102 */
db4721fb
PT
1103 protected void setFilterStatusRowItemData(final TableItem item) {
1104 for (int i = 0; i < fTable.getColumns().length; i++) {
1105 if (i == 0) {
1106 if ((fTrace == null) || (fFilterCheckCount == fTrace.getNbEvents())) {
1107 item.setImage(FILTER_IMAGE);
1108 } else {
1109 item.setImage(STOP_IMAGE);
1110 }
1111 item.setText(0, fFilterMatchCount + "/" + fFilterCheckCount); //$NON-NLS-1$
1112 } else {
1113 item.setText(i, ""); //$NON-NLS-1$
1114 }
1115 }
93bfd50a 1116 item.setData(null);
db4721fb
PT
1117 item.setData(Key.TIMESTAMP, null);
1118 item.setData(Key.RANK, null);
1119 item.setForeground(null);
1120 item.setBackground(null);
1121 }
1122
a0a88f65
AM
1123 /**
1124 * Create an editor for the header.
1125 */
db4721fb
PT
1126 protected void createHeaderEditor() {
1127 final TableEditor tableEditor = fTable.createTableEditor();
1128 tableEditor.horizontalAlignment = SWT.LEFT;
1129 tableEditor.verticalAlignment = SWT.CENTER;
1130 tableEditor.grabHorizontal = true;
1131 tableEditor.minimumWidth = 50;
1132
1133 // Handle the header row selection
1134 fTable.addMouseListener(new MouseAdapter() {
1135 int columnIndex;
1136 TableColumn column;
1137 TableItem item;
1138
1139 @Override
1140 public void mouseDown(final MouseEvent event) {
1141 if (event.button != 1) {
1142 return;
1143 }
1144 // Identify the selected row
1145 final Point point = new Point(event.x, event.y);
1146 item = fTable.getItem(point);
1147
1148 // Header row selected
1149 if ((item != null) && (fTable.indexOf(item) == 0)) {
1150
1151 // Icon selected
1152 if (item.getImageBounds(0).contains(point)) {
1153 if (fHeaderState == HeaderState.SEARCH) {
1154 fHeaderState = HeaderState.FILTER;
1155 } else if (fHeaderState == HeaderState.FILTER) {
1156 fHeaderState = HeaderState.SEARCH;
1157 }
3f43dc48 1158 fTable.setSelection(0);
db4721fb
PT
1159 fTable.refresh();
1160 return;
1161 }
1162
1163 // Identify the selected column
1164 columnIndex = -1;
1165 for (int i = 0; i < fTable.getColumns().length; i++) {
1166 final Rectangle rect = item.getBounds(i);
1167 if (rect.contains(point)) {
1168 columnIndex = i;
1169 break;
1170 }
1171 }
1172
1173 if (columnIndex == -1) {
1174 return;
1175 }
1176
1177 column = fTable.getColumns()[columnIndex];
1178
1179 String txtKey = null;
1180 if (fHeaderState == HeaderState.SEARCH) {
1181 txtKey = Key.SEARCH_TXT;
1182 } else if (fHeaderState == HeaderState.FILTER) {
1183 txtKey = Key.FILTER_TXT;
1184 }
1185
1186 // The control that will be the editor must be a child of the Table
1187 final Text newEditor = (Text) fTable.createTableEditorControl(Text.class);
1188 final String headerString = (String) column.getData(txtKey);
1189 if (headerString != null) {
1190 newEditor.setText(headerString);
1191 }
1192 newEditor.addFocusListener(new FocusAdapter() {
1193 @Override
1194 public void focusLost(final FocusEvent e) {
1195 final boolean changed = updateHeader(newEditor.getText());
1196 if (changed) {
1197 applyHeader();
1198 }
1199 }
1200 });
1201 newEditor.addKeyListener(new KeyAdapter() {
1202 @Override
1203 public void keyPressed(final KeyEvent e) {
1204 if (e.character == SWT.CR) {
1205 updateHeader(newEditor.getText());
1206 applyHeader();
d81b17ea
MAL
1207
1208 // Set focus on the table so that the next carriage return goes to the next result
1209 TmfEventsTable.this.getTable().setFocus();
db4721fb
PT
1210 } else if (e.character == SWT.ESC) {
1211 tableEditor.getEditor().dispose();
1212 }
1213 }
1214 });
1215 newEditor.selectAll();
1216 newEditor.setFocus();
1217 tableEditor.setEditor(newEditor, item, columnIndex);
1218 }
1219 }
1220
1221 /*
1222 * returns true is value was changed
1223 */
1224 private boolean updateHeader(final String text) {
1225 String objKey = null;
1226 String txtKey = null;
1227 if (fHeaderState == HeaderState.SEARCH) {
1228 objKey = Key.SEARCH_OBJ;
1229 txtKey = Key.SEARCH_TXT;
1230 } else if (fHeaderState == HeaderState.FILTER) {
1231 objKey = Key.FILTER_OBJ;
1232 txtKey = Key.FILTER_TXT;
1233 }
1234 if (text.trim().length() > 0) {
1235 try {
1236 final String regex = TmfFilterMatchesNode.regexFix(text);
1237 Pattern.compile(regex);
1238 if (regex.equals(column.getData(txtKey))) {
1239 tableEditor.getEditor().dispose();
1240 return false;
1241 }
1242 final TmfFilterMatchesNode filter = new TmfFilterMatchesNode(null);
1243 String fieldId = (String) column.getData(Key.FIELD_ID);
1244 if (fieldId == null) {
1245 fieldId = column.getText();
1246 }
1247 filter.setField(fieldId);
1248 filter.setRegex(regex);
1249 column.setData(objKey, filter);
1250 column.setData(txtKey, regex);
1251 } catch (final PatternSyntaxException ex) {
1252 tableEditor.getEditor().dispose();
1253 MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
1254 ex.getDescription(), ex.getMessage());
1255 return false;
1256 }
1257 } else {
1258 if (column.getData(txtKey) == null) {
1259 tableEditor.getEditor().dispose();
1260 return false;
1261 }
1262 column.setData(objKey, null);
1263 column.setData(txtKey, null);
1264 }
1265 return true;
1266 }
1267
1268 private void applyHeader() {
1269 if (fHeaderState == HeaderState.SEARCH) {
1270 stopSearchThread();
1271 final TmfFilterAndNode filter = new TmfFilterAndNode(null);
3dca7aa5
AM
1272 for (final TableColumn col : fTable.getColumns()) {
1273 final Object filterObj = col.getData(Key.SEARCH_OBJ);
db4721fb
PT
1274 if (filterObj instanceof ITmfFilterTreeNode) {
1275 filter.addChild((ITmfFilterTreeNode) filterObj);
1276 }
1277 }
1278 if (filter.getChildrenCount() > 0) {
1279 fTable.setData(Key.SEARCH_OBJ, filter);
1280 fTable.refresh();
1281 searchNext();
1282 fireSearchApplied(filter);
1283 } else {
1284 fTable.setData(Key.SEARCH_OBJ, null);
1285 fTable.refresh();
1286 fireSearchApplied(null);
1287 }
1288 } else if (fHeaderState == HeaderState.FILTER) {
1289 final TmfFilterAndNode filter = new TmfFilterAndNode(null);
3dca7aa5
AM
1290 for (final TableColumn col : fTable.getColumns()) {
1291 final Object filterObj = col.getData(Key.FILTER_OBJ);
db4721fb
PT
1292 if (filterObj instanceof ITmfFilterTreeNode) {
1293 filter.addChild((ITmfFilterTreeNode) filterObj);
1294 }
1295 }
1296 if (filter.getChildrenCount() > 0) {
1297 applyFilter(filter);
1298 } else {
1299 clearFilters();
1300 }
1301 }
1302
1303 tableEditor.getEditor().dispose();
1304 }
1305 });
1306
1307 fTable.addKeyListener(new KeyAdapter() {
1308 @Override
1309 public void keyPressed(final KeyEvent e) {
1310 e.doit = false;
1311 if (e.character == SWT.ESC) {
1312 stopFilterThread();
1313 stopSearchThread();
1314 fTable.refresh();
1315 } else if (e.character == SWT.DEL) {
1316 if (fHeaderState == HeaderState.SEARCH) {
1317 stopSearchThread();
1318 for (final TableColumn column : fTable.getColumns()) {
1319 column.setData(Key.SEARCH_OBJ, null);
1320 column.setData(Key.SEARCH_TXT, null);
1321 }
1322 fTable.setData(Key.SEARCH_OBJ, null);
1323 fTable.refresh();
1324 fireSearchApplied(null);
1325 } else if (fHeaderState == HeaderState.FILTER) {
1326 clearFilters();
1327 }
1328 } else if (e.character == SWT.CR) {
1329 if ((e.stateMask & SWT.SHIFT) == 0) {
1330 searchNext();
1331 } else {
1332 searchPrevious();
1333 }
1334 }
1335 }
1336 });
1337 }
1338
a0a88f65
AM
1339 /**
1340 * Send an event indicating a filter has been applied.
1341 *
1342 * @param filter
1343 * The filter that was just applied
1344 */
db4721fb 1345 protected void fireFilterApplied(final ITmfFilter filter) {
faa38350 1346 broadcast(new TmfEventFilterAppliedSignal(this, fTrace, filter));
db4721fb
PT
1347 }
1348
a0a88f65
AM
1349 /**
1350 * Send an event indicating that a search has been applied.
1351 *
1352 * @param filter
1353 * The search filter that was just applied
1354 */
db4721fb 1355 protected void fireSearchApplied(final ITmfFilter filter) {
faa38350 1356 broadcast(new TmfEventSearchAppliedSignal(this, fTrace, filter));
db4721fb
PT
1357 }
1358
a0a88f65
AM
1359 /**
1360 * Start the filtering thread.
1361 */
db4721fb
PT
1362 protected void startFilterThread() {
1363 synchronized (fFilterSyncObj) {
1364 final ITmfFilterTreeNode filter = (ITmfFilterTreeNode) fTable.getData(Key.FILTER_OBJ);
1365 if (fFilterThread == null || fFilterThread.filter != filter) {
1366 if (fFilterThread != null) {
1367 fFilterThread.cancel();
1368 fFilterThreadResume = false;
1369 }
1370 fFilterThread = new FilterThread(filter);
1371 fFilterThread.start();
1372 } else {
1373 fFilterThreadResume = true;
1374 }
1375 }
1376 }
1377
a0a88f65
AM
1378 /**
1379 * Stop the filtering thread.
1380 */
db4721fb
PT
1381 protected void stopFilterThread() {
1382 synchronized (fFilterSyncObj) {
1383 if (fFilterThread != null) {
1384 fFilterThread.cancel();
1385 fFilterThread = null;
1386 fFilterThreadResume = false;
1387 }
1388 }
1389 }
1390
1391 /**
a0a88f65
AM
1392 * Apply a filter.
1393 *
1394 * @param filter
1395 * The filter to apply
db4721fb
PT
1396 * @since 1.1
1397 */
1398 protected void applyFilter(ITmfFilter filter) {
f29f8868
BH
1399 stopFilterThread();
1400 stopSearchThread();
db4721fb
PT
1401 fFilterMatchCount = 0;
1402 fFilterCheckCount = 0;
1403 fCache.applyFilter(filter);
1404 fTable.clearAll();
1405 fTable.setData(Key.FILTER_OBJ, filter);
1406 fTable.setItemCount(3); // +1 for header row, +2 for top and bottom filter status rows
1407 startFilterThread();
1408 fireFilterApplied(filter);
1409 }
1410
a0a88f65
AM
1411 /**
1412 * Clear all currently active filters.
1413 */
db4721fb
PT
1414 protected void clearFilters() {
1415 if (fTable.getData(Key.FILTER_OBJ) == null) {
1416 return;
1417 }
1418 stopFilterThread();
1419 stopSearchThread();
1420 fCache.clearFilter();
1421 fTable.clearAll();
1422 for (final TableColumn column : fTable.getColumns()) {
1423 column.setData(Key.FILTER_OBJ, null);
1424 column.setData(Key.FILTER_TXT, null);
1425 }
1426 fTable.setData(Key.FILTER_OBJ, null);
1427 if (fTrace != null) {
1428 fTable.setItemCount((int) fTrace.getNbEvents() + 1); // +1 for header row
1429 } else {
1430 fTable.setItemCount(1); // +1 for header row
1431 }
1432 fFilterMatchCount = 0;
1433 fFilterCheckCount = 0;
1434 if (fSelectedRank >= 0) {
1435 fTable.setSelection((int) fSelectedRank + 1); // +1 for header row
1436 } else {
1437 fTable.setSelection(0);
1438 }
1439 fireFilterApplied(null);
3f43dc48 1440 updateStatusLine(null);
db4721fb
PT
1441 }
1442
a0a88f65
AM
1443 /**
1444 * Wrapper Thread object for the filtering thread.
1445 */
db4721fb
PT
1446 protected class FilterThread extends Thread {
1447 private final ITmfFilterTreeNode filter;
fd3f1eff 1448 private TmfEventRequest request;
db4721fb
PT
1449 private boolean refreshBusy = false;
1450 private boolean refreshPending = false;
1451 private final Object syncObj = new Object();
1452
a0a88f65
AM
1453 /**
1454 * Constructor.
1455 *
1456 * @param filter
1457 * The filter this thread will be processing
1458 */
db4721fb
PT
1459 public FilterThread(final ITmfFilterTreeNode filter) {
1460 super("Filter Thread"); //$NON-NLS-1$
1461 this.filter = filter;
1462 }
1463
1464 @Override
1465 public void run() {
1466 if (fTrace == null) {
1467 return;
1468 }
1469 final int nbRequested = (int) (fTrace.getNbEvents() - fFilterCheckCount);
1470 if (nbRequested <= 0) {
1471 return;
1472 }
fd3f1eff
AM
1473 request = new TmfEventRequest(ITmfEvent.class, TmfTimeRange.ETERNITY,
1474 (int) fFilterCheckCount, nbRequested, ExecutionType.BACKGROUND) {
db4721fb
PT
1475 @Override
1476 public void handleData(final ITmfEvent event) {
1477 super.handleData(event);
1478 if (request.isCancelled()) {
1479 return;
1480 }
1481 if (filter.matches(event)) {
1482 final long rank = fFilterCheckCount;
1483 final int index = (int) fFilterMatchCount;
1484 fFilterMatchCount++;
bd54d363 1485 fCache.storeEvent(event, rank, index);
db4721fb
PT
1486 refreshTable();
1487 } else if ((fFilterCheckCount % 100) == 0) {
1488 refreshTable();
1489 }
1490 fFilterCheckCount++;
1491 }
1492 };
fd3f1eff 1493 ((ITmfEventProvider) fTrace).sendRequest(request);
db4721fb
PT
1494 try {
1495 request.waitForCompletion();
1496 } catch (final InterruptedException e) {
1497 }
1498 refreshTable();
1499 synchronized (fFilterSyncObj) {
1500 fFilterThread = null;
1501 if (fFilterThreadResume) {
1502 fFilterThreadResume = false;
1503 fFilterThread = new FilterThread(filter);
1504 fFilterThread.start();
1505 }
1506 }
1507 }
1508
a0a88f65
AM
1509 /**
1510 * Refresh the filter.
1511 */
db4721fb
PT
1512 public void refreshTable() {
1513 synchronized (syncObj) {
1514 if (refreshBusy) {
1515 refreshPending = true;
1516 return;
1517 }
1518 refreshBusy = true;
1519 }
1520 Display.getDefault().asyncExec(new Runnable() {
1521 @Override
1522 public void run() {
1523 if (request.isCancelled()) {
1524 return;
1525 }
1526 if (fTable.isDisposed()) {
1527 return;
1528 }
1529 fTable.setItemCount((int) fFilterMatchCount + 3); // +1 for header row, +2 for top and bottom filter status rows
1530 fTable.refresh();
1531 synchronized (syncObj) {
1532 refreshBusy = false;
1533 if (refreshPending) {
1534 refreshPending = false;
1535 refreshTable();
1536 }
1537 }
1538 }
1539 });
1540 }
1541
a0a88f65
AM
1542 /**
1543 * Cancel this filtering thread.
1544 */
db4721fb
PT
1545 public void cancel() {
1546 if (request != null) {
1547 request.cancel();
1548 }
1549 }
1550 }
1551
a0a88f65
AM
1552 /**
1553 * Go to the next item of a search.
1554 */
db4721fb
PT
1555 protected void searchNext() {
1556 synchronized (fSearchSyncObj) {
1557 if (fSearchThread != null) {
1558 return;
1559 }
1560 final ITmfFilterTreeNode searchFilter = (ITmfFilterTreeNode) fTable.getData(Key.SEARCH_OBJ);
1561 if (searchFilter == null) {
1562 return;
1563 }
1564 final int selectionIndex = fTable.getSelectionIndex();
1565 int startIndex;
1566 if (selectionIndex > 0) {
1567 startIndex = selectionIndex; // -1 for header row, +1 for next event
1568 } else {
1569 // header row is selected, start at top event
1570 startIndex = Math.max(0, fTable.getTopIndex() - 1); // -1 for header row
1571 }
1572 final ITmfFilterTreeNode eventFilter = (ITmfFilterTreeNode) fTable.getData(Key.FILTER_OBJ);
60fb38b8 1573 if (eventFilter != null) {
db4721fb
PT
1574 startIndex = Math.max(0, startIndex - 1); // -1 for top filter status row
1575 }
1576 fSearchThread = new SearchThread(searchFilter, eventFilter, startIndex, fSelectedRank, Direction.FORWARD);
1577 fSearchThread.schedule();
1578 }
1579 }
1580
a0a88f65
AM
1581 /**
1582 * Go to the previous item of a search.
1583 */
db4721fb
PT
1584 protected void searchPrevious() {
1585 synchronized (fSearchSyncObj) {
1586 if (fSearchThread != null) {
1587 return;
1588 }
1589 final ITmfFilterTreeNode searchFilter = (ITmfFilterTreeNode) fTable.getData(Key.SEARCH_OBJ);
1590 if (searchFilter == null) {
1591 return;
1592 }
1593 final int selectionIndex = fTable.getSelectionIndex();
1594 int startIndex;
1595 if (selectionIndex > 0) {
1596 startIndex = selectionIndex - 2; // -1 for header row, -1 for previous event
1597 } else {
1598 // header row is selected, start at precedent of top event
1599 startIndex = fTable.getTopIndex() - 2; // -1 for header row, -1 for previous event
1600 }
1601 final ITmfFilterTreeNode eventFilter = (ITmfFilterTreeNode) fTable.getData(Key.FILTER_OBJ);
60fb38b8 1602 if (eventFilter != null) {
db4721fb
PT
1603 startIndex = startIndex - 1; // -1 for top filter status row
1604 }
1605 fSearchThread = new SearchThread(searchFilter, eventFilter, startIndex, fSelectedRank, Direction.BACKWARD);
1606 fSearchThread.schedule();
1607 }
1608 }
1609
a0a88f65
AM
1610 /**
1611 * Stop the search thread.
1612 */
db4721fb
PT
1613 protected void stopSearchThread() {
1614 fPendingGotoRank = -1;
1615 synchronized (fSearchSyncObj) {
1616 if (fSearchThread != null) {
1617 fSearchThread.cancel();
1618 fSearchThread = null;
1619 }
1620 }
1621 }
1622
a0a88f65
AM
1623 /**
1624 * Wrapper for the search thread.
1625 */
db4721fb 1626 protected class SearchThread extends Job {
a0a88f65
AM
1627
1628 private ITmfFilterTreeNode searchFilter;
1629 private ITmfFilterTreeNode eventFilter;
1630 private int startIndex;
1631 private int direction;
1632 private long rank;
1633 private long foundRank = -1;
fd3f1eff 1634 private TmfEventRequest request;
ae3ffd37 1635 private ITmfTimestamp foundTimestamp = null;
db4721fb 1636
a0a88f65
AM
1637 /**
1638 * Constructor.
1639 *
1640 * @param searchFilter
1641 * The search filter
1642 * @param eventFilter
1643 * The event filter
1644 * @param startIndex
1645 * The index at which we should start searching
1646 * @param currentRank
1647 * The current rank
1648 * @param direction
1649 * In which direction should we search, forward or backwards
1650 */
1651 public SearchThread(final ITmfFilterTreeNode searchFilter,
1652 final ITmfFilterTreeNode eventFilter, final int startIndex,
db4721fb
PT
1653 final long currentRank, final int direction) {
1654 super(Messages.TmfEventsTable_SearchingJobName);
1655 this.searchFilter = searchFilter;
1656 this.eventFilter = eventFilter;
1657 this.startIndex = startIndex;
1658 this.rank = currentRank;
1659 this.direction = direction;
1660 }
1661
1662 @Override
1663 protected IStatus run(final IProgressMonitor monitor) {
1664 if (fTrace == null) {
1665 return Status.OK_STATUS;
1666 }
1667 final Display display = Display.getDefault();
1668 if (startIndex < 0) {
1669 rank = (int) fTrace.getNbEvents() - 1;
1670 } else if (startIndex >= (fTable.getItemCount() - (eventFilter == null ? 1 : 3))) { // -1 for header row, -2 for top and bottom filter status rows
1671 rank = 0;
1672 } else {
1673 int idx = startIndex;
1674 while (foundRank == -1) {
1675 final CachedEvent event = fCache.peekEvent(idx);
1676 if (event == null) {
1677 break;
1678 }
1679 rank = event.rank;
1680 if (searchFilter.matches(event.event) && ((eventFilter == null) || eventFilter.matches(event.event))) {
1681 foundRank = event.rank;
ae3ffd37 1682 foundTimestamp = event.event.getTimestamp();
db4721fb
PT
1683 break;
1684 }
1685 if (direction == Direction.FORWARD) {
1686 idx++;
1687 } else {
1688 idx--;
1689 }
1690 }
1691 if (foundRank == -1) {
1692 if (direction == Direction.FORWARD) {
1693 rank++;
1694 if (rank > (fTrace.getNbEvents() - 1)) {
1695 rank = 0;
1696 }
1697 } else {
1698 rank--;
1699 if (rank < 0) {
1700 rank = (int) fTrace.getNbEvents() - 1;
1701 }
1702 }
1703 }
1704 }
1705 final int startRank = (int) rank;
1706 boolean wrapped = false;
1707 while (!monitor.isCanceled() && (foundRank == -1) && (fTrace != null)) {
1708 int nbRequested = (direction == Direction.FORWARD ? Integer.MAX_VALUE : Math.min((int) rank + 1, fTrace.getCacheSize()));
1709 if (direction == Direction.BACKWARD) {
1710 rank = Math.max(0, rank - fTrace.getCacheSize() + 1);
1711 }
fd3f1eff
AM
1712 request = new TmfEventRequest(ITmfEvent.class, TmfTimeRange.ETERNITY,
1713 (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 };
fd3f1eff 1730 ((ITmfEventProvider) fTrace).sendRequest(request);
db4721fb
PT
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 }
d3bc98ee
EB
1825 fTable.setRedraw(false);
1826
f29f8868
BH
1827 boolean isLinux = System.getProperty("os.name").contains("Linux") ? true : false; //$NON-NLS-1$ //$NON-NLS-2$
1828
1829 TableColumn tableColumns[] = fTable.getColumns();
1830 for (int i = 0; i < tableColumns.length; i++) {
1831 final TableColumn column = tableColumns[i];
db4721fb
PT
1832 final int headerWidth = column.getWidth();
1833 column.pack();
f29f8868
BH
1834 // Workaround for Linux which doesn't consider the image width of
1835 // search/filter row in TableColumn.pack() after having executed
1836 // TableItem.setImage((Image)null) for other rows than search/filter row.
1837 if (isLinux && (i == 0)) {
1838 column.setWidth(column.getWidth() + SEARCH_IMAGE.getBounds().width);
1839 }
1840
db4721fb
PT
1841 if (column.getWidth() < headerWidth) {
1842 column.setWidth(headerWidth);
1843 }
1844 }
d3bc98ee
EB
1845
1846 fTable.setRedraw(true);
db4721fb
PT
1847 fPackDone = true;
1848 }
1849
d3de0920 1850 /**
cf37ad9f
AM
1851 * Get the contents of the row in the events table corresponding to an
1852 * event. The order of the elements corresponds to the order of the columns.
d3de0920 1853 *
cf37ad9f
AM
1854 * TODO Use column IDs, not indexes, so that the column order can be
1855 * re-arranged.
a0a88f65 1856 *
db4721fb 1857 * @param event
cf37ad9f
AM
1858 * The event printed in this row
1859 * @return The event row entries
1860 * @since 3.0
db4721fb 1861 */
cf37ad9f
AM
1862 public String[] getItemStrings(ITmfEvent event) {
1863 if (event == null) {
1864 return EMPTY_STRING_ARRAY;
db4721fb 1865 }
cf37ad9f
AM
1866 return new String[] {
1867 event.getTimestamp().toString(),
1868 event.getSource(),
1869 event.getType().getName(),
1870 event.getReference(),
1871 event.getContent().toString()
1872 };
db4721fb
PT
1873 }
1874
1875 /**
1876 * Notify this table that is got the UI focus.
1877 */
1878 public void setFocus() {
1879 fTable.setFocus();
1880 }
1881
1882 /**
1883 * Assign a new trace to this event table.
1884 *
1885 * @param trace
1886 * The trace to assign to this event table
1887 * @param disposeOnClose
1888 * true if the trace should be disposed when the table is
1889 * disposed
1890 */
1891 public void setTrace(final ITmfTrace trace, final boolean disposeOnClose) {
1892 if ((fTrace != null) && fDisposeOnClose) {
1893 fTrace.dispose();
1894 }
1895 fTrace = trace;
1896 fPackDone = false;
1897 fSelectedRank = 0;
1898 fDisposeOnClose = disposeOnClose;
1899
1900 // Perform the updates on the UI thread
1901 fTable.getDisplay().syncExec(new Runnable() {
1902 @Override
1903 public void run() {
1904 fTable.removeAll();
1905 fCache.setTrace(fTrace); // Clear the cache
1906 if (fTrace != null) {
1907 if (!fTable.isDisposed() && (fTrace != null)) {
1908 if (fTable.getData(Key.FILTER_OBJ) == null) {
1909 fTable.setItemCount((int) fTrace.getNbEvents() + 1); // +1 for header row
1910 } else {
1911 stopFilterThread();
1912 fFilterMatchCount = 0;
1913 fFilterCheckCount = 0;
1914 fTable.setItemCount(3); // +1 for header row, +2 for top and bottom filter status rows
1915 startFilterThread();
1916 }
1917 }
db4721fb 1918 }
ea279a69 1919 fRawViewer.setTrace(fTrace);
db4721fb
PT
1920 }
1921 });
1922 }
1923
3f43dc48
PT
1924 /**
1925 * Assign the status line manager
1926 *
1927 * @param statusLineManager
1928 * The status line manager, or null to disable status line messages
4b121c48 1929 * @since 2.1
3f43dc48
PT
1930 */
1931 public void setStatusLineManager(IStatusLineManager statusLineManager) {
1932 if (fStatusLineManager != null && statusLineManager == null) {
1933 fStatusLineManager.setMessage(""); //$NON-NLS-1$
1934 }
1935 fStatusLineManager = statusLineManager;
1936 }
1937
1938 private void updateStatusLine(ITmfTimestamp delta) {
1939 if (fStatusLineManager != null) {
1940 if (delta != null) {
1941 fStatusLineManager.setMessage("\u0394: " + delta); //$NON-NLS-1$
1942 } else {
1943 fStatusLineManager.setMessage(null);
1944 }
1945 }
1946 }
1947
db4721fb
PT
1948 // ------------------------------------------------------------------------
1949 // Event cache
1950 // ------------------------------------------------------------------------
1951
1952 /**
1953 * Notify that the event cache has been updated
1954 *
1955 * @param completed
1956 * Also notify if the populating of the cache is complete, or
1957 * not.
1958 */
1959 public void cacheUpdated(final boolean completed) {
1960 synchronized (fCacheUpdateSyncObj) {
1961 if (fCacheUpdateBusy) {
1962 fCacheUpdatePending = true;
1963 fCacheUpdateCompleted = completed;
1964 return;
1965 }
1966 fCacheUpdateBusy = true;
1967 }
1968 // Event cache is now updated. Perform update on the UI thread
1969 if (!fTable.isDisposed()) {
1970 fTable.getDisplay().asyncExec(new Runnable() {
1971 @Override
1972 public void run() {
1973 if (!fTable.isDisposed()) {
1974 fTable.refresh();
1975 packColumns();
1976 }
1977 if (completed) {
1978 populateCompleted();
1979 }
1980 synchronized (fCacheUpdateSyncObj) {
1981 fCacheUpdateBusy = false;
1982 if (fCacheUpdatePending) {
1983 fCacheUpdatePending = false;
1984 cacheUpdated(fCacheUpdateCompleted);
1985 }
1986 }
1987 }
1988 });
1989 }
1990 }
1991
a0a88f65
AM
1992 /**
1993 * Callback for when populating the table is complete.
1994 */
db4721fb
PT
1995 protected void populateCompleted() {
1996 // Nothing by default;
1997 }
1998
93bfd50a
PT
1999 // ------------------------------------------------------------------------
2000 // ISelectionProvider
2001 // ------------------------------------------------------------------------
2002
93bfd50a
PT
2003 /**
2004 * @since 2.0
2005 */
2006 @Override
2007 public void addSelectionChangedListener(ISelectionChangedListener listener) {
2008 selectionChangedListeners.add(listener);
2009 }
2010
93bfd50a
PT
2011 /**
2012 * @since 2.0
2013 */
2014 @Override
2015 public ISelection getSelection() {
2016 if (fTable == null || fTable.isDisposed()) {
2017 return StructuredSelection.EMPTY;
2018 }
507b1336 2019 List<Object> list = new ArrayList<>(fTable.getSelection().length);
93bfd50a
PT
2020 for (TableItem item : fTable.getSelection()) {
2021 if (item.getData() != null) {
2022 list.add(item.getData());
2023 }
2024 }
2025 return new StructuredSelection(list);
2026 }
2027
93bfd50a
PT
2028 /**
2029 * @since 2.0
2030 */
2031 @Override
2032 public void removeSelectionChangedListener(ISelectionChangedListener listener) {
2033 selectionChangedListeners.remove(listener);
2034 }
2035
93bfd50a
PT
2036 /**
2037 * @since 2.0
2038 */
2039 @Override
2040 public void setSelection(ISelection selection) {
2041 // not implemented
2042 }
2043
2044 /**
2045 * Notifies any selection changed listeners that the viewer's selection has changed.
2046 * Only listeners registered at the time this method is called are notified.
2047 *
2048 * @param event a selection changed event
2049 *
2050 * @see ISelectionChangedListener#selectionChanged
2051 * @since 2.0
2052 */
2053 protected void fireSelectionChanged(final SelectionChangedEvent event) {
2054 Object[] listeners = selectionChangedListeners.getListeners();
2055 for (int i = 0; i < listeners.length; ++i) {
2056 final ISelectionChangedListener l = (ISelectionChangedListener) listeners[i];
2057 SafeRunnable.run(new SafeRunnable() {
faa38350 2058 @Override
93bfd50a
PT
2059 public void run() {
2060 l.selectionChanged(event);
2061 }
2062 });
2063 }
2064 }
2065
db4721fb
PT
2066 // ------------------------------------------------------------------------
2067 // Bookmark handling
2068 // ------------------------------------------------------------------------
2069
2070 /**
2071 * Add a bookmark to this event table.
2072 *
2073 * @param bookmarksFile
2074 * The file to use for the bookmarks
2075 */
2076 public void addBookmark(final IFile bookmarksFile) {
2077 fBookmarksFile = bookmarksFile;
2078 final TableItem[] selection = fTable.getSelection();
2079 if (selection.length > 0) {
2080 final TableItem tableItem = selection[0];
2081 if (tableItem.getData(Key.RANK) != null) {
2082 final StringBuffer defaultMessage = new StringBuffer();
2083 for (int i = 0; i < fTable.getColumns().length; i++) {
60fb38b8 2084 if (i > 0) {
db4721fb
PT
2085 defaultMessage.append(", "); //$NON-NLS-1$
2086 }
2087 defaultMessage.append(tableItem.getText(i));
2088 }
3be2946f
PT
2089 final InputDialog dialog = new MultiLineInputDialog(
2090 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
2091 Messages.TmfEventsTable_AddBookmarkDialogTitle,
2092 Messages.TmfEventsTable_AddBookmarkDialogMessage,
2093 defaultMessage.toString());
db4721fb
PT
2094 if (dialog.open() == Window.OK) {
2095 final String message = dialog.getValue();
2096 try {
2097 final IMarker bookmark = bookmarksFile.createMarker(IMarker.BOOKMARK);
2098 if (bookmark.exists()) {
2099 bookmark.setAttribute(IMarker.MESSAGE, message.toString());
0126a8ca
AM
2100 final Long rank = (Long) tableItem.getData(Key.RANK);
2101 final int location = rank.intValue();
2102 bookmark.setAttribute(IMarker.LOCATION, Integer.valueOf(location));
db4721fb
PT
2103 fBookmarksMap.put(rank, bookmark.getId());
2104 fTable.refresh();
2105 }
2106 } catch (final CoreException e) {
2107 displayException(e);
2108 }
2109 }
2110 }
2111 }
2112
2113 }
2114
2115 /**
2116 * Remove a bookmark from this event table.
2117 *
2118 * @param bookmark
2119 * The bookmark to remove
2120 */
2121 public void removeBookmark(final IMarker bookmark) {
2122 for (final Entry<Long, Long> entry : fBookmarksMap.entrySet()) {
2123 if (entry.getValue().equals(bookmark.getId())) {
2124 fBookmarksMap.remove(entry.getKey());
2125 fTable.refresh();
2126 return;
2127 }
2128 }
2129 }
2130
0126a8ca 2131 private void toggleBookmark(final Long rank) {
db4721fb
PT
2132 if (fBookmarksFile == null) {
2133 return;
2134 }
2135 if (fBookmarksMap.containsKey(rank)) {
2136 final Long markerId = fBookmarksMap.remove(rank);
2137 fTable.refresh();
2138 try {
2139 final IMarker bookmark = fBookmarksFile.findMarker(markerId);
2140 if (bookmark != null) {
2141 bookmark.delete();
2142 }
2143 } catch (final CoreException e) {
2144 displayException(e);
2145 }
2146 } else {
2147 addBookmark(fBookmarksFile);
2148 }
2149 }
2150
2151 /**
2152 * Refresh the bookmarks assigned to this trace, from the contents of a
2153 * bookmark file.
2154 *
2155 * @param bookmarksFile
2156 * The bookmark file to use
2157 */
2158 public void refreshBookmarks(final IFile bookmarksFile) {
2159 fBookmarksFile = bookmarksFile;
2160 if (bookmarksFile == null) {
2161 fBookmarksMap.clear();
2162 fTable.refresh();
2163 return;
2164 }
2165 try {
2166 fBookmarksMap.clear();
2167 for (final IMarker bookmark : bookmarksFile.findMarkers(IMarker.BOOKMARK, false, IResource.DEPTH_ZERO)) {
2168 final int location = bookmark.getAttribute(IMarker.LOCATION, -1);
2169 if (location != -1) {
2170 final long rank = location;
2171 fBookmarksMap.put(rank, bookmark.getId());
2172 }
2173 }
2174 fTable.refresh();
2175 } catch (final CoreException e) {
2176 displayException(e);
2177 }
2178 }
2179
2180 @Override
2181 public void gotoMarker(final IMarker marker) {
2182 final int rank = marker.getAttribute(IMarker.LOCATION, -1);
2183 if (rank != -1) {
2184 int index = rank;
2185 if (fTable.getData(Key.FILTER_OBJ) != null) {
2186 index = fCache.getFilteredEventIndex(rank) + 1; // +1 for top filter status row
2187 } else if (rank >= fTable.getItemCount()) {
2188 fPendingGotoRank = rank;
2189 }
a56ec2b8 2190 fSelectedRank = rank;
db4721fb 2191 fTable.setSelection(index + 1); // +1 for header row
3f43dc48 2192 updateStatusLine(null);
db4721fb
PT
2193 }
2194 }
2195
2196 // ------------------------------------------------------------------------
2197 // Listeners
2198 // ------------------------------------------------------------------------
2199
db4721fb
PT
2200 @Override
2201 public void colorSettingsChanged(final ColorSetting[] colorSettings) {
2202 fTable.refresh();
2203 }
2204
db4721fb
PT
2205 // ------------------------------------------------------------------------
2206 // Signal handlers
2207 // ------------------------------------------------------------------------
2208
db4721fb
PT
2209 /**
2210 * Handler for the trace updated signal
2211 *
2212 * @param signal
2213 * The incoming signal
2214 */
2215 @TmfSignalHandler
2216 public void traceUpdated(final TmfTraceUpdatedSignal signal) {
2217 if ((signal.getTrace() != fTrace) || fTable.isDisposed()) {
2218 return;
2219 }
2220 // Perform the refresh on the UI thread
2221 Display.getDefault().asyncExec(new Runnable() {
2222 @Override
2223 public void run() {
2224 if (!fTable.isDisposed() && (fTrace != null)) {
2225 if (fTable.getData(Key.FILTER_OBJ) == null) {
2226 fTable.setItemCount((int) fTrace.getNbEvents() + 1); // +1 for header row
2227 if ((fPendingGotoRank != -1) && ((fPendingGotoRank + 1) < fTable.getItemCount())) { // +1 for header row
2228 fTable.setSelection((int) fPendingGotoRank + 1); // +1 for header row
2229 fPendingGotoRank = -1;
3f43dc48 2230 updateStatusLine(null);
db4721fb
PT
2231 }
2232 } else {
2233 startFilterThread();
2234 }
2235 }
2236 if (!fRawViewer.isDisposed() && (fTrace != null)) {
2237 fRawViewer.refreshEventCount();
2238 }
2239 }
2240 });
2241 }
2242
2243 /**
2244 * Handler for the time synch signal.
2245 *
2246 * @param signal
2247 * The incoming signal
2248 */
2249 @TmfSignalHandler
2250 public void currentTimeUpdated(final TmfTimeSynchSignal signal) {
2251 if ((signal.getSource() != this) && (fTrace != null) && (!fTable.isDisposed())) {
2252
2253 // Create a request for one event that will be queued after other ongoing requests. When this request is completed
2254 // do the work to select the actual event with the timestamp specified in the signal. This procedure prevents
2255 // the method fTrace.getRank() from interfering and delaying ongoing requests.
fd3f1eff
AM
2256 final TmfEventRequest subRequest = new TmfEventRequest(ITmfEvent.class,
2257 TmfTimeRange.ETERNITY, 0, 1, ExecutionType.FOREGROUND) {
db4721fb 2258
0fcf3b09 2259 TmfTimestamp ts = new TmfTimestamp(signal.getBeginTime());
db4721fb
PT
2260
2261 @Override
2262 public void handleData(final ITmfEvent event) {
2263 super.handleData(event);
2264 }
2265
2266 @Override
5419a136 2267 public void handleCompleted() {
db4721fb
PT
2268 super.handleCompleted();
2269 if (fTrace == null) {
2270 return;
2271 }
2272
2273 // Verify if the event is within the trace range and adjust if necessary
2274 ITmfTimestamp timestamp = ts;
2275 if (timestamp.compareTo(fTrace.getStartTime(), true) == -1) {
2276 timestamp = fTrace.getStartTime();
2277 }
2278 if (timestamp.compareTo(fTrace.getEndTime(), true) == 1) {
2279 timestamp = fTrace.getEndTime();
2280 }
2281
2282 // Get the rank of the selected event in the table
2283 final ITmfContext context = fTrace.seekEvent(timestamp);
2284 final long rank = context.getRank();
4c9f2944 2285 context.dispose();
db4721fb
PT
2286 fSelectedRank = rank;
2287
2288 fTable.getDisplay().asyncExec(new Runnable() {
2289 @Override
2290 public void run() {
2291 // Return if table is disposed
2292 if (fTable.isDisposed()) {
2293 return;
2294 }
2295
2296 int index = (int) rank;
2297 if (fTable.isDisposed()) {
2298 return;
2299 }
60fb38b8 2300 if (fTable.getData(Key.FILTER_OBJ) != null) {
db4721fb
PT
2301 index = fCache.getFilteredEventIndex(rank) + 1; // +1 for top filter status row
2302 }
2303 fTable.setSelection(index + 1); // +1 for header row
2304 fRawViewer.selectAndReveal(rank);
3f43dc48 2305 updateStatusLine(null);
db4721fb
PT
2306 }
2307 });
2308 }
2309 };
2310
fd3f1eff 2311 ((ITmfEventProvider) fTrace).sendRequest(subRequest);
db4721fb
PT
2312 }
2313 }
2314
2315 // ------------------------------------------------------------------------
2316 // Error handling
2317 // ------------------------------------------------------------------------
2318
2319 /**
2320 * Display an exception in a message box
2321 *
2322 * @param e the exception
2323 */
2324 private static void displayException(final Exception e) {
2325 final MessageBox mb = new MessageBox(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
60fb38b8 2326 mb.setText(e.getClass().getSimpleName());
db4721fb
PT
2327 mb.setMessage(e.getMessage());
2328 mb.open();
2329 }
2330
f8177ba2
FC
2331 /**
2332 * @since 2.0
2333 */
2334 public void refresh() {
2335 fCache.clear();
2336 fTable.refresh();
2337 fTable.redraw();
2338 }
db4721fb 2339}
This page took 0.172211 seconds and 5 git commands to generate.