tmf: Delete unused code in GraphNode
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / viewers / events / TmfEventsTable.java
CommitLineData
db4721fb 1/*******************************************************************************
ed902a2b 2 * Copyright (c) 2010, 2015 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:
baafe54c
AM
10 * Francois Chouinard - Initial API and implementation, replaced Table by TmfVirtualTable
11 * Patrick Tasse - Factored out from events view,
12 * Filter implementation (inspired by www.eclipse.org/mat)
ac44ef71 13 * Ansgar Radermacher - Support navigation to model URIs (Bug 396956)
f47ed727 14 * Bernd Hufmann - Updated call site and model URI implementation
baafe54c 15 * Alexandre Montplaisir - Update to new column API
346ffed7 16 * Matthew Khouzam - Add hide columns
db4721fb
PT
17 *******************************************************************************/
18
2bdf0193 19package org.eclipse.tracecompass.tmf.ui.viewers.events;
db4721fb 20
5db5a3a4
AM
21import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
22
60fb38b8 23import java.io.FileNotFoundException;
9ba0a108 24import java.lang.reflect.InvocationTargetException;
db4721fb 25import java.util.ArrayList;
2db176a0 26import java.util.Collection;
db4721fb 27import java.util.HashMap;
baafe54c 28import java.util.LinkedList;
db4721fb 29import java.util.List;
db4721fb 30import java.util.Map.Entry;
110d44a1 31import java.util.regex.Matcher;
db4721fb
PT
32import java.util.regex.Pattern;
33import java.util.regex.PatternSyntaxException;
34
d3de0920
XR
35import org.eclipse.core.commands.Command;
36import org.eclipse.core.commands.ExecutionException;
37import org.eclipse.core.commands.NotEnabledException;
38import org.eclipse.core.commands.NotHandledException;
39import org.eclipse.core.commands.ParameterizedCommand;
40import org.eclipse.core.commands.common.NotDefinedException;
41import org.eclipse.core.expressions.IEvaluationContext;
db4721fb
PT
42import org.eclipse.core.resources.IFile;
43import org.eclipse.core.resources.IMarker;
44import org.eclipse.core.resources.IResource;
60fb38b8 45import org.eclipse.core.resources.IResourceVisitor;
ac44ef71 46import org.eclipse.core.resources.ResourcesPlugin;
db4721fb 47import org.eclipse.core.runtime.CoreException;
ac44ef71 48import org.eclipse.core.runtime.IPath;
db4721fb
PT
49import org.eclipse.core.runtime.IProgressMonitor;
50import org.eclipse.core.runtime.IStatus;
93bfd50a 51import org.eclipse.core.runtime.ListenerList;
ac44ef71 52import org.eclipse.core.runtime.Path;
db4721fb
PT
53import org.eclipse.core.runtime.Status;
54import org.eclipse.core.runtime.jobs.Job;
ac44ef71
AR
55import org.eclipse.emf.common.util.URI;
56import org.eclipse.emf.ecore.EValidator;
46671228 57import org.eclipse.jdt.annotation.NonNull;
db4721fb
PT
58import org.eclipse.jface.action.Action;
59import org.eclipse.jface.action.IAction;
60import org.eclipse.jface.action.IMenuListener;
61import org.eclipse.jface.action.IMenuManager;
3f43dc48 62import org.eclipse.jface.action.IStatusLineManager;
db4721fb
PT
63import org.eclipse.jface.action.MenuManager;
64import org.eclipse.jface.action.Separator;
65import org.eclipse.jface.dialogs.InputDialog;
66import org.eclipse.jface.dialogs.MessageDialog;
9ba0a108 67import org.eclipse.jface.operation.IRunnableWithProgress;
bb7c657f 68import org.eclipse.jface.resource.ColorRegistry;
812e7197 69import org.eclipse.jface.resource.FontRegistry;
db4721fb
PT
70import org.eclipse.jface.resource.JFaceResources;
71import org.eclipse.jface.resource.LocalResourceManager;
812e7197 72import org.eclipse.jface.util.IPropertyChangeListener;
ac44ef71 73import org.eclipse.jface.util.OpenStrategy;
812e7197 74import org.eclipse.jface.util.PropertyChangeEvent;
93bfd50a 75import org.eclipse.jface.util.SafeRunnable;
60fb38b8 76import org.eclipse.jface.viewers.ArrayContentProvider;
93bfd50a
PT
77import org.eclipse.jface.viewers.ISelection;
78import org.eclipse.jface.viewers.ISelectionChangedListener;
79import org.eclipse.jface.viewers.ISelectionProvider;
60fb38b8 80import org.eclipse.jface.viewers.LabelProvider;
93bfd50a
PT
81import org.eclipse.jface.viewers.SelectionChangedEvent;
82import org.eclipse.jface.viewers.StructuredSelection;
db4721fb 83import org.eclipse.jface.window.Window;
db4721fb
PT
84import org.eclipse.swt.SWT;
85import org.eclipse.swt.custom.SashForm;
110d44a1 86import org.eclipse.swt.custom.StyleRange;
db4721fb 87import org.eclipse.swt.custom.TableEditor;
6817308e
PT
88import org.eclipse.swt.events.ControlAdapter;
89import org.eclipse.swt.events.ControlEvent;
db4721fb
PT
90import org.eclipse.swt.events.FocusAdapter;
91import org.eclipse.swt.events.FocusEvent;
92import org.eclipse.swt.events.KeyAdapter;
93import org.eclipse.swt.events.KeyEvent;
94import org.eclipse.swt.events.MouseAdapter;
95import org.eclipse.swt.events.MouseEvent;
96import org.eclipse.swt.events.SelectionAdapter;
97import org.eclipse.swt.events.SelectionEvent;
98import org.eclipse.swt.graphics.Color;
99import org.eclipse.swt.graphics.Font;
110d44a1 100import org.eclipse.swt.graphics.GC;
db4721fb
PT
101import org.eclipse.swt.graphics.Image;
102import org.eclipse.swt.graphics.Point;
103import org.eclipse.swt.graphics.Rectangle;
104import org.eclipse.swt.layout.FillLayout;
105import org.eclipse.swt.layout.GridData;
106import org.eclipse.swt.layout.GridLayout;
107import org.eclipse.swt.widgets.Composite;
108import org.eclipse.swt.widgets.Display;
109import org.eclipse.swt.widgets.Event;
110import org.eclipse.swt.widgets.Label;
111import org.eclipse.swt.widgets.Listener;
112import org.eclipse.swt.widgets.Menu;
113import org.eclipse.swt.widgets.MessageBox;
114import org.eclipse.swt.widgets.Shell;
115import org.eclipse.swt.widgets.TableColumn;
116import org.eclipse.swt.widgets.TableItem;
117import org.eclipse.swt.widgets.Text;
6cfc180e 118import org.eclipse.tracecompass.common.core.NonNullUtils;
2bdf0193
AM
119import org.eclipse.tracecompass.internal.tmf.core.filter.TmfCollapseFilter;
120import org.eclipse.tracecompass.internal.tmf.ui.Activator;
121import org.eclipse.tracecompass.internal.tmf.ui.Messages;
9ba0a108 122import org.eclipse.tracecompass.internal.tmf.ui.commands.CopyToClipboardOperation;
2bdf0193
AM
123import org.eclipse.tracecompass.internal.tmf.ui.commands.ExportToTextCommandHandler;
124import org.eclipse.tracecompass.internal.tmf.ui.dialogs.MultiLineInputDialog;
125import org.eclipse.tracecompass.tmf.core.component.ITmfEventProvider;
126import org.eclipse.tracecompass.tmf.core.component.TmfComponent;
127import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
9447c7ee 128import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect;
40dfafb3 129import org.eclipse.tracecompass.tmf.core.event.aspect.TmfContentFieldAspect;
2bdf0193
AM
130import org.eclipse.tracecompass.tmf.core.event.collapse.ITmfCollapsibleEvent;
131import org.eclipse.tracecompass.tmf.core.event.lookup.ITmfCallsite;
132import org.eclipse.tracecompass.tmf.core.event.lookup.ITmfModelLookup;
133import org.eclipse.tracecompass.tmf.core.event.lookup.ITmfSourceLookup;
134import org.eclipse.tracecompass.tmf.core.filter.ITmfFilter;
135import org.eclipse.tracecompass.tmf.core.filter.model.ITmfFilterTreeNode;
136import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterAndNode;
137import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterMatchesNode;
138import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterNode;
139import org.eclipse.tracecompass.tmf.core.request.ITmfEventRequest.ExecutionType;
140import org.eclipse.tracecompass.tmf.core.request.TmfEventRequest;
141import org.eclipse.tracecompass.tmf.core.signal.TmfEventFilterAppliedSignal;
142import org.eclipse.tracecompass.tmf.core.signal.TmfEventSearchAppliedSignal;
143import org.eclipse.tracecompass.tmf.core.signal.TmfEventSelectedSignal;
97c71024 144import org.eclipse.tracecompass.tmf.core.signal.TmfSelectionRangeUpdatedSignal;
bb7c657f 145import org.eclipse.tracecompass.tmf.core.signal.TmfSignalHandler;
2bdf0193
AM
146import org.eclipse.tracecompass.tmf.core.signal.TmfTraceUpdatedSignal;
147import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
148import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange;
149import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
150import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
151import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
b04903a2 152import org.eclipse.tracecompass.tmf.core.trace.TmfTrace;
2bdf0193
AM
153import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
154import org.eclipse.tracecompass.tmf.core.trace.location.ITmfLocation;
155import org.eclipse.tracecompass.tmf.ui.viewers.events.TmfEventsCache.CachedEvent;
156import org.eclipse.tracecompass.tmf.ui.viewers.events.columns.TmfEventTableColumn;
2bdf0193
AM
157import org.eclipse.tracecompass.tmf.ui.views.colors.ColorSetting;
158import org.eclipse.tracecompass.tmf.ui.views.colors.ColorSettingsManager;
159import org.eclipse.tracecompass.tmf.ui.views.colors.IColorSettingsListener;
160import org.eclipse.tracecompass.tmf.ui.views.filter.FilterManager;
161import org.eclipse.tracecompass.tmf.ui.widgets.rawviewer.TmfRawEventViewer;
162import org.eclipse.tracecompass.tmf.ui.widgets.virtualtable.TmfVirtualTable;
ac44ef71 163import org.eclipse.ui.IWorkbenchPage;
db4721fb 164import org.eclipse.ui.PlatformUI;
d3de0920 165import org.eclipse.ui.commands.ICommandService;
60fb38b8 166import org.eclipse.ui.dialogs.ListDialog;
d3de0920 167import org.eclipse.ui.handlers.IHandlerService;
ac44ef71 168import org.eclipse.ui.ide.IDE;
db4721fb
PT
169import org.eclipse.ui.ide.IGotoMarker;
170import org.eclipse.ui.themes.ColorUtil;
812e7197 171import org.eclipse.ui.themes.IThemeManager;
db4721fb 172
2db176a0
PT
173import com.google.common.base.Joiner;
174import com.google.common.collect.HashMultimap;
baafe54c 175import com.google.common.collect.ImmutableList;
2db176a0
PT
176import com.google.common.collect.Multimap;
177
db4721fb
PT
178/**
179 * The generic TMF Events table
180 *
181 * This is a view that will list events that are read from a trace.
182 *
db4721fb
PT
183 * @author Francois Chouinard
184 * @author Patrick Tasse
185 */
812e7197 186public class TmfEventsTable extends TmfComponent implements IGotoMarker, IColorSettingsListener, ISelectionProvider, IPropertyChangeListener {
db4721fb 187
cf37ad9f
AM
188 /**
189 * Empty string array, used by {@link #getItemStrings}.
cf37ad9f 190 */
46671228 191 protected static final @NonNull String[] EMPTY_STRING_ARRAY = new String[0];
cf37ad9f 192
baafe54c
AM
193 /**
194 * Empty string
baafe54c
AM
195 */
196 protected static final @NonNull String EMPTY_STRING = ""; //$NON-NLS-1$
197
03142470
BH
198 private static final boolean IS_LINUX = System.getProperty("os.name").contains("Linux") ? true : false; //$NON-NLS-1$ //$NON-NLS-2$
199
812e7197 200 private static final String FONT_DEFINITION_ID = "org.eclipse.tracecompass.tmf.ui.font.eventtable"; //$NON-NLS-1$
bb7c657f 201 private static final String HIGHLIGHT_COLOR_DEFINITION_ID = "org.eclipse.tracecompass.tmf.ui.color.eventtable.highlight"; //$NON-NLS-1$
812e7197 202
db4721fb
PT
203 private static final Image BOOKMARK_IMAGE = Activator.getDefault().getImageFromPath(
204 "icons/elcl16/bookmark_obj.gif"); //$NON-NLS-1$
205 private static final Image SEARCH_IMAGE = Activator.getDefault().getImageFromPath("icons/elcl16/search.gif"); //$NON-NLS-1$
206 private static final Image SEARCH_MATCH_IMAGE = Activator.getDefault().getImageFromPath(
207 "icons/elcl16/search_match.gif"); //$NON-NLS-1$
208 private static final Image SEARCH_MATCH_BOOKMARK_IMAGE = Activator.getDefault().getImageFromPath(
209 "icons/elcl16/search_match_bookmark.gif"); //$NON-NLS-1$
8b7eb3cd 210 private static final Image FILTER_IMAGE = Activator.getDefault().getImageFromPath("icons/elcl16/filter_items.gif"); //$NON-NLS-1$
db4721fb
PT
211 private static final Image STOP_IMAGE = Activator.getDefault().getImageFromPath("icons/elcl16/stop.gif"); //$NON-NLS-1$
212 private static final String SEARCH_HINT = Messages.TmfEventsTable_SearchHint;
213 private static final String FILTER_HINT = Messages.TmfEventsTable_FilterHint;
214 private static final int MAX_CACHE_SIZE = 1000;
215
03142470
BH
216 private static final int MARGIN_COLUMN_INDEX = 0;
217 private static final int FILTER_SUMMARY_INDEX = 1;
218 private static final int EVENT_COLUMNS_START_INDEX = MARGIN_COLUMN_INDEX + 1;
219
db4721fb 220 /**
346ffed7 221 * The events table search/filter/data keys
db4721fb 222 *
db4721fb 223 * @author Patrick Tasse
34c87ae8
AM
224 * @noimplement This interface only contains Event Table specific
225 * static definitions.
db4721fb
PT
226 */
227 public interface Key {
34c87ae8 228
db4721fb
PT
229 /** Search text */
230 String SEARCH_TXT = "$srch_txt"; //$NON-NLS-1$
231
232 /** Search object */
233 String SEARCH_OBJ = "$srch_obj"; //$NON-NLS-1$
234
235 /** Filter text */
236 String FILTER_TXT = "$fltr_txt"; //$NON-NLS-1$
237
238 /** Filter object */
239 String FILTER_OBJ = "$fltr_obj"; //$NON-NLS-1$
240
dadf6e1a 241 /** Timestamp */
db4721fb
PT
242 String TIMESTAMP = "$time"; //$NON-NLS-1$
243
244 /** Rank */
245 String RANK = "$rank"; //$NON-NLS-1$
246
db4721fb
PT
247 /** Bookmark indicator */
248 String BOOKMARK = "$bookmark"; //$NON-NLS-1$
c409f16b
AM
249
250 /** Event aspect represented by this column */
251 String ASPECT = "$aspect"; //$NON-NLS-1$
110d44a1 252
8b7eb3cd
MK
253 /**
254 * Table item list of style ranges
255 *
256 * @since 1.0
257 */
110d44a1 258 String STYLE_RANGES = "$style_ranges"; //$NON-NLS-1$
346ffed7
MK
259
260 /**
261 * The width of a table item
262 *
34c87ae8 263 * @since 2.0
346ffed7
MK
264 */
265 String WIDTH = "$width"; //$NON-NLS-1$
db4721fb
PT
266 }
267
268 /**
269 * The events table search/filter state
270 *
271 * @version 1.0
272 * @author Patrick Tasse
273 */
274 public static enum HeaderState {
275 /** A search is being run */
276 SEARCH,
277
278 /** A filter is applied */
279 FILTER
280 }
281
282 interface Direction {
283 int FORWARD = +1;
284 int BACKWARD = -1;
285 }
286
287 // ------------------------------------------------------------------------
288 // Table data
289 // ------------------------------------------------------------------------
290
a0a88f65 291 /** The virtual event table */
db4721fb 292 protected TmfVirtualTable fTable;
a0a88f65
AM
293
294 private Composite fComposite;
295 private SashForm fSashForm;
296 private TmfRawEventViewer fRawViewer;
297 private ITmfTrace fTrace;
574f43ad 298 private volatile boolean fPackDone = false;
a0a88f65 299 private HeaderState fHeaderState = HeaderState.SEARCH;
9ba0a108
PT
300 private long fSelectedRank = -1;
301 private long fSelectedBeginRank = -1;
3f43dc48
PT
302 private ITmfTimestamp fSelectedBeginTimestamp = null;
303 private IStatusLineManager fStatusLineManager = null;
db4721fb
PT
304
305 // Filter data
a0a88f65
AM
306 private long fFilterMatchCount;
307 private long fFilterCheckCount;
308 private FilterThread fFilterThread;
309 private boolean fFilterThreadResume = false;
310 private final Object fFilterSyncObj = new Object();
311 private SearchThread fSearchThread;
312 private final Object fSearchSyncObj = new Object();
db4721fb 313
93bfd50a 314 /**
8b7eb3cd
MK
315 * List of selection change listeners (element type:
316 * <code>ISelectionChangedListener</code>).
93bfd50a
PT
317 *
318 * @see #fireSelectionChanged
319 */
320 private ListenerList selectionChangedListeners = new ListenerList();
321
db4721fb 322 // Bookmark map <Rank, MarkerId>
2db176a0 323 private Multimap<Long, Long> fBookmarksMap = HashMultimap.create();
a0a88f65
AM
324 private IFile fBookmarksFile;
325 private long fPendingGotoRank = -1;
db4721fb
PT
326
327 // SWT resources
a0a88f65
AM
328 private LocalResourceManager fResourceManager = new LocalResourceManager(JFaceResources.getResources());
329 private Color fGrayColor;
330 private Color fGreenColor;
bb7c657f 331 private Color fHighlightColor;
812e7197 332 private Font fFont;
a0a88f65 333 private Font fBoldFont;
db4721fb 334
baafe54c 335 private final List<TmfEventTableColumn> fColumns = new LinkedList<>();
db4721fb
PT
336
337 // Event cache
338 private final TmfEventsCache fCache;
339 private boolean fCacheUpdateBusy = false;
340 private boolean fCacheUpdatePending = false;
341 private boolean fCacheUpdateCompleted = false;
342 private final Object fCacheUpdateSyncObj = new Object();
343
14665360
PT
344 // Keep track of column order, it is needed after table is disposed
345 private int[] fColumnOrder;
346
db4721fb
PT
347 private boolean fDisposeOnClose;
348
346ffed7
MK
349 private Menu fHeaderMenu;
350
351 private Menu fTablePopup;
352
353 private Menu fRawTablePopup;
354
665990bb 355 private Point fLastMenuCursorLocation;
3bccf3b1
MAL
356 private MenuManager fRawViewerPopupMenuManager;
357 private MenuManager fTablePopupMenuManager;
358 private MenuManager fHeaderPopupMenuManager;
359
db4721fb 360 // ------------------------------------------------------------------------
a0a88f65 361 // Constructors
db4721fb
PT
362 // ------------------------------------------------------------------------
363
364 /**
baafe54c 365 * Basic constructor, using the default set of columns
db4721fb
PT
366 *
367 * @param parent
368 * The parent composite UI object
369 * @param cacheSize
370 * The size of the event table cache
371 */
372 public TmfEventsTable(final Composite parent, final int cacheSize) {
b04903a2 373 this(parent, cacheSize, TmfTrace.BASE_ASPECTS);
db4721fb
PT
374 }
375
376 /**
baafe54c 377 * Legacy constructor, using ColumnData to define columns
db4721fb
PT
378 *
379 * @param parent
380 * The parent composite UI object
381 * @param cacheSize
382 * The size of the event table cache
383 * @param columnData
40dfafb3 384 * The column data array
baafe54c
AM
385 * @deprecated Deprecated constructor, use
386 * {@link #TmfEventsTable(Composite, int, Collection)}
387 */
388 @Deprecated
389 public TmfEventsTable(final Composite parent, int cacheSize,
2bdf0193 390 final org.eclipse.tracecompass.tmf.ui.widgets.virtualtable.ColumnData[] columnData) {
baafe54c
AM
391 /*
392 * We'll do a "best-effort" to keep trace types still using this API to
8b7eb3cd
MK
393 * keep working, by defining a TmfEventTableColumn for each ColumnData
394 * they passed.
baafe54c
AM
395 */
396 this(parent, cacheSize, convertFromColumnData(columnData));
397 }
398
399 @Deprecated
8192209b 400 private static @NonNull Iterable<ITmfEventAspect> convertFromColumnData(
2bdf0193 401 org.eclipse.tracecompass.tmf.ui.widgets.virtualtable.ColumnData[] columnData) {
baafe54c 402
b04903a2 403 ImmutableList.Builder<ITmfEventAspect> builder = new ImmutableList.Builder<>();
2bdf0193 404 for (org.eclipse.tracecompass.tmf.ui.widgets.virtualtable.ColumnData col : columnData) {
9447c7ee
AM
405 String fieldName = col.header;
406 if (fieldName != null) {
40dfafb3 407 builder.add(new TmfContentFieldAspect(fieldName, fieldName));
baafe54c
AM
408 }
409 }
5db5a3a4 410 return checkNotNull(builder.build());
baafe54c
AM
411 }
412
413 /**
414 * Standard constructor, where we define which columns to use.
415 *
416 * @param parent
417 * The parent composite UI object
418 * @param cacheSize
419 * The size of the event table cache
b04903a2
AM
420 * @param aspects
421 * The event aspects to display in this table. One column per
422 * aspect will be created.
baafe54c
AM
423 * <p>
424 * The iteration order of this collection will correspond to the
b04903a2 425 * initial ordering of the columns in the table.
baafe54c 426 * </p>
db4721fb 427 */
baafe54c 428 public TmfEventsTable(final Composite parent, int cacheSize,
8192209b 429 @NonNull Iterable<ITmfEventAspect> aspects) {
db4721fb
PT
430 super("TmfEventsTable"); //$NON-NLS-1$
431
432 fComposite = new Composite(parent, SWT.NONE);
433 final GridLayout gl = new GridLayout(1, false);
434 gl.marginHeight = 0;
435 gl.marginWidth = 0;
436 gl.verticalSpacing = 0;
437 fComposite.setLayout(gl);
438
439 fSashForm = new SashForm(fComposite, SWT.HORIZONTAL);
440 fSashForm.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
441
442 // Create a virtual table
3f43dc48 443 final int style = SWT.H_SCROLL | SWT.V_SCROLL | SWT.MULTI | SWT.FULL_SELECTION;
db4721fb
PT
444 fTable = new TmfVirtualTable(fSashForm, style);
445
446 // Set the table layout
447 final GridData layoutData = new GridData(SWT.FILL, SWT.FILL, true, true);
448 fTable.setLayoutData(layoutData);
449
450 // Some cosmetic enhancements
451 fTable.setHeaderVisible(true);
452 fTable.setLinesVisible(true);
453
baafe54c 454 // Setup the columns
8192209b
AM
455 for (ITmfEventAspect aspect : aspects) {
456 if (aspect != null) {
457 fColumns.add(new TmfEventTableColumn(aspect));
b04903a2 458 }
db4721fb
PT
459 }
460
03142470
BH
461 TmfMarginColumn collapseCol = new TmfMarginColumn();
462 fColumns.add(MARGIN_COLUMN_INDEX, collapseCol);
463
346ffed7 464 fHeaderMenu = new Menu(fTable);
baafe54c 465 // Create the UI columns in the table
2fcd5eea
AM
466 for (TmfEventTableColumn col : fColumns) {
467 TableColumn column = fTable.newTableColumn(SWT.LEFT);
468 column.setText(col.getHeaderName());
469 column.setToolTipText(col.getHeaderTooltip());
c409f16b 470 column.setData(Key.ASPECT, col.getEventAspect());
2fcd5eea 471 column.pack();
03142470
BH
472 if (col instanceof TmfMarginColumn) {
473 column.setResizable(false);
6817308e
PT
474 } else {
475 column.setMoveable(true);
be42703d 476 column.setData(Key.WIDTH, -1);
03142470 477 }
14665360
PT
478 column.addControlListener(new ControlAdapter() {
479 /*
8b7eb3cd
MK
480 * Make sure that the margin column is always first and keep the
481 * column order variable up to date.
14665360
PT
482 */
483 @Override
484 public void controlMoved(ControlEvent e) {
485 int[] order = fTable.getColumnOrder();
486 if (order[0] == MARGIN_COLUMN_INDEX) {
487 fColumnOrder = order;
488 return;
489 }
490 for (int i = order.length - 1; i > 0; i--) {
491 if (order[i] == MARGIN_COLUMN_INDEX) {
492 order[i] = order[i - 1];
493 order[i - 1] = MARGIN_COLUMN_INDEX;
494 }
495 }
496 fTable.setColumnOrder(order);
497 fColumnOrder = fTable.getColumnOrder();
498 }
499 });
2fcd5eea 500 }
14665360 501 fColumnOrder = fTable.getColumnOrder();
baafe54c 502
db4721fb
PT
503 // Set the frozen row for header row
504 fTable.setFrozenRowCount(1);
505
506 // Create the header row cell editor
507 createHeaderEditor();
508
509 // Handle the table item selection
510 fTable.addSelectionListener(new SelectionAdapter() {
511 @Override
512 public void widgetSelected(final SelectionEvent e) {
3f43dc48
PT
513 if (e.item == null) {
514 return;
515 }
516 updateStatusLine(null);
517 if (fTable.getSelectionIndices().length > 0) {
518 if (e.item.getData(Key.RANK) instanceof Long) {
519 fSelectedRank = (Long) e.item.getData(Key.RANK);
520 fRawViewer.selectAndReveal((Long) e.item.getData(Key.RANK));
9ba0a108
PT
521 } else {
522 fSelectedRank = -1;
523 }
524 if (fTable.getSelectionIndices().length == 1) {
525 fSelectedBeginRank = fSelectedRank;
3f43dc48
PT
526 }
527 if (e.item.getData(Key.TIMESTAMP) instanceof ITmfTimestamp) {
6cfc180e 528 final ITmfTimestamp ts = NonNullUtils.checkNotNull((ITmfTimestamp) e.item.getData(Key.TIMESTAMP));
3f43dc48
PT
529 if (fTable.getSelectionIndices().length == 1) {
530 fSelectedBeginTimestamp = ts;
531 }
6cfc180e
GB
532 ITmfTimestamp selectedBeginTimestamp = fSelectedBeginTimestamp;
533 if (selectedBeginTimestamp != null) {
85203d74
PT
534 broadcast(new TmfSelectionRangeUpdatedSignal(TmfEventsTable.this, selectedBeginTimestamp, ts));
535 if (fTable.getSelectionIndices().length == 2) {
536 updateStatusLine(ts.getDelta(selectedBeginTimestamp));
3f43dc48 537 }
db4721fb 538 }
3f43dc48
PT
539 } else {
540 if (fTable.getSelectionIndices().length == 1) {
541 fSelectedBeginTimestamp = null;
db4721fb
PT
542 }
543 }
544 }
6b44794a
MK
545 if (e.item.getData() instanceof ITmfEvent) {
546 broadcast(new TmfEventSelectedSignal(TmfEventsTable.this, (ITmfEvent) e.item.getData()));
3f43dc48
PT
547 fireSelectionChanged(new SelectionChangedEvent(TmfEventsTable.this, new StructuredSelection(e.item.getData())));
548 } else {
549 fireSelectionChanged(new SelectionChangedEvent(TmfEventsTable.this, StructuredSelection.EMPTY));
550 }
db4721fb
PT
551 }
552 });
553
41b5c37f
AM
554 int realCacheSize = Math.max(cacheSize, Display.getDefault().getBounds().height / fTable.getItemHeight());
555 realCacheSize = Math.min(realCacheSize, MAX_CACHE_SIZE);
556 fCache = new TmfEventsCache(realCacheSize, this);
db4721fb
PT
557
558 // Handle the table item requests
559 fTable.addListener(SWT.SetData, new Listener() {
560
561 @Override
562 public void handleEvent(final Event event) {
563
564 final TableItem item = (TableItem) event.item;
565 int index = event.index - 1; // -1 for the header row
566
567 if (event.index == 0) {
568 setHeaderRowItemData(item);
569 return;
570 }
571
572 if (fTable.getData(Key.FILTER_OBJ) != null) {
573 if ((event.index == 1) || (event.index == (fTable.getItemCount() - 1))) {
574 setFilterStatusRowItemData(item);
575 return;
576 }
8b7eb3cd
MK
577 /* -1 for top filter status row */
578 index = index - 1;
db4721fb
PT
579 }
580
581 final CachedEvent cachedEvent = fCache.getEvent(index);
582 if (cachedEvent != null) {
03142470 583 setItemData(item, cachedEvent, cachedEvent.rank);
db4721fb
PT
584 return;
585 }
586
587 // Else, fill the cache asynchronously (and off the UI thread)
588 event.doit = false;
589 }
590 });
591
346ffed7
MK
592 fTable.addListener(SWT.MenuDetect, new Listener() {
593 @Override
594 public void handleEvent(Event event) {
665990bb
MAL
595 fLastMenuCursorLocation = new Point(event.x, event.y);
596 Point pt = fTable.getDisplay().map(null, fTable, fLastMenuCursorLocation);
346ffed7
MK
597 Rectangle clientArea = fTable.getClientArea();
598 boolean header = clientArea.y <= pt.y && pt.y < (clientArea.y + fTable.getHeaderHeight());
599 fTable.setMenu(header ? fHeaderMenu : fTablePopup);
600 }
601 });
602
db4721fb
PT
603 fTable.addMouseListener(new MouseAdapter() {
604 @Override
605 public void mouseDoubleClick(final MouseEvent event) {
606 if (event.button != 1) {
607 return;
608 }
609 // Identify the selected row
610 final Point point = new Point(event.x, event.y);
611 final TableItem item = fTable.getItem(point);
612 if (item != null) {
613 final Rectangle imageBounds = item.getImageBounds(0);
614 imageBounds.width = BOOKMARK_IMAGE.getBounds().width;
615 if (imageBounds.contains(point)) {
616 final Long rank = (Long) item.getData(Key.RANK);
617 if (rank != null) {
618 toggleBookmark(rank);
619 }
620 }
621 }
622 }
623 });
624
8b7eb3cd 625 final Listener tooltipListener = new Listener() {
db4721fb 626 Shell tooltipShell = null;
8b7eb3cd 627
db4721fb
PT
628 @Override
629 public void handleEvent(final Event event) {
630 switch (event.type) {
8b7eb3cd
MK
631 case SWT.MouseHover:
632 final TableItem item = fTable.getItem(new Point(event.x, event.y));
633 if (item == null) {
634 return;
635 }
636 final Long rank = (Long) item.getData(Key.RANK);
637 if (rank == null) {
638 return;
639 }
640 final String tooltipText = (String) item.getData(Key.BOOKMARK);
641 final Rectangle bounds = item.getImageBounds(0);
642 bounds.width = BOOKMARK_IMAGE.getBounds().width;
643 if (!bounds.contains(event.x, event.y)) {
644 return;
645 }
646 if ((tooltipShell != null) && !tooltipShell.isDisposed()) {
647 tooltipShell.dispose();
648 }
649 tooltipShell = new Shell(fTable.getShell(), SWT.ON_TOP | SWT.NO_FOCUS | SWT.TOOL);
650 tooltipShell.setBackground(PlatformUI.getWorkbench().getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND));
651 final FillLayout layout = new FillLayout();
652 layout.marginWidth = 2;
653 tooltipShell.setLayout(layout);
654 final Label label = new Label(tooltipShell, SWT.WRAP);
655 String text = rank.toString() + (tooltipText != null ? ": " + tooltipText : EMPTY_STRING); //$NON-NLS-1$
656 label.setForeground(PlatformUI.getWorkbench().getDisplay().getSystemColor(SWT.COLOR_INFO_FOREGROUND));
657 label.setBackground(PlatformUI.getWorkbench().getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND));
658 label.setText(text);
659 label.addListener(SWT.MouseExit, this);
660 label.addListener(SWT.MouseDown, this);
661 label.addListener(SWT.MouseWheel, this);
662 final Point size = tooltipShell.computeSize(SWT.DEFAULT, SWT.DEFAULT);
663 /*
664 * Bug in Linux. The coordinates of the event have an origin
665 * that excludes the table header but the method toDisplay()
666 * expects coordinates relative to an origin that includes
667 * the table header.
668 */
669 int y = event.y;
670 if (IS_LINUX) {
671 y += fTable.getHeaderHeight();
672 }
673 Point pt = fTable.toDisplay(event.x, y);
674 pt.x += BOOKMARK_IMAGE.getBounds().width;
675 pt.y += item.getBounds().height;
676 tooltipShell.setBounds(pt.x, pt.y, size.x, size.y);
677 tooltipShell.setVisible(true);
678 break;
679 case SWT.Dispose:
680 case SWT.KeyDown:
681 case SWT.MouseMove:
682 case SWT.MouseExit:
683 case SWT.MouseDown:
684 case SWT.MouseWheel:
685 if (tooltipShell != null) {
686 tooltipShell.dispose();
687 tooltipShell = null;
688 }
689 break;
690 default:
691 break;
db4721fb
PT
692 }
693 }
694 };
695
696 fTable.addListener(SWT.MouseHover, tooltipListener);
697 fTable.addListener(SWT.Dispose, tooltipListener);
698 fTable.addListener(SWT.KeyDown, tooltipListener);
699 fTable.addListener(SWT.MouseMove, tooltipListener);
700 fTable.addListener(SWT.MouseExit, tooltipListener);
701 fTable.addListener(SWT.MouseDown, tooltipListener);
702 fTable.addListener(SWT.MouseWheel, tooltipListener);
703
110d44a1
PT
704 fTable.addListener(SWT.EraseItem, new Listener() {
705 @Override
706 public void handleEvent(Event event) {
707 TableItem item = (TableItem) event.item;
708 List<?> styleRanges = (List<?>) item.getData(Key.STYLE_RANGES);
709
710 GC gc = event.gc;
711 Color background = item.getBackground(event.index);
712 /*
713 * Paint the background if it is not the default system color.
714 * In Windows, if you let the widget draw the background, it
715 * will not show the item's background color if the item is
716 * selected or hot. If there are no style ranges and the item
717 * background is the default system color, we do not want to
718 * paint it or otherwise we would override the platform theme
719 * (e.g. alternating colors).
720 */
4c27e423 721 if (styleRanges != null || !background.equals(item.getParent().getBackground())) {
110d44a1
PT
722 // we will paint the table item's background
723 event.detail &= ~SWT.BACKGROUND;
724
725 // paint the item's default background
726 gc.setBackground(background);
727 gc.fillRectangle(event.x, event.y, event.width, event.height);
728 }
729
730 /*
8b7eb3cd
MK
731 * We will paint the table item's foreground. In Windows, if you
732 * paint the background but let the widget draw the foreground,
733 * it will override your background, unless the item is selected
734 * or hot.
110d44a1
PT
735 */
736 event.detail &= ~SWT.FOREGROUND;
737
738 // paint the highlighted background for all style ranges
739 if (styleRanges != null) {
740 Rectangle textBounds = item.getTextBounds(event.index);
741 String text = item.getText(event.index);
742 for (Object o : styleRanges) {
743 if (o instanceof StyleRange) {
744 StyleRange styleRange = (StyleRange) o;
745 if (styleRange.data.equals(event.index)) {
746 int startIndex = styleRange.start;
747 int endIndex = startIndex + styleRange.length;
567d3095
PT
748 int startX = gc.textExtent(text.substring(0, startIndex)).x;
749 int endX = gc.textExtent(text.substring(0, endIndex)).x;
110d44a1
PT
750 gc.setBackground(styleRange.background);
751 gc.fillRectangle(textBounds.x + startX, textBounds.y, (endX - startX), textBounds.height);
752 }
753 }
754 }
755 }
756 }
757 });
758
759 fTable.addListener(SWT.PaintItem, new Listener() {
760 @Override
761 public void handleEvent(Event event) {
762 TableItem item = (TableItem) event.item;
763
764 // we promised to paint the table item's foreground
765 GC gc = event.gc;
766 Image image = item.getImage(event.index);
767 if (image != null) {
768 Rectangle imageBounds = item.getImageBounds(event.index);
769 /*
770 * The image bounds don't match the default image position.
771 */
4c27e423
PT
772 if (IS_LINUX) {
773 gc.drawImage(image, imageBounds.x + 1, imageBounds.y + 3);
774 } else {
775 gc.drawImage(image, imageBounds.x, imageBounds.y + 1);
776 }
110d44a1
PT
777 }
778 gc.setForeground(item.getForeground(event.index));
779 gc.setFont(item.getFont(event.index));
780 String text = item.getText(event.index);
781 Rectangle textBounds = item.getTextBounds(event.index);
782 /*
783 * The text bounds don't match the default text position.
784 */
4c27e423
PT
785 if (IS_LINUX) {
786 gc.drawText(text, textBounds.x + 1, textBounds.y + 3, true);
787 } else {
788 gc.drawText(text, textBounds.x - 1, textBounds.y + 2, true);
789 }
110d44a1
PT
790 }
791 });
792
db4721fb
PT
793 // Create resources
794 createResources();
795
812e7197 796 initializeFonts();
bb7c657f 797 initializeColors();
812e7197
PT
798 PlatformUI.getWorkbench().getThemeManager().addPropertyChangeListener(this);
799
db4721fb
PT
800 ColorSettingsManager.addColorSettingsListener(this);
801
802 fTable.setItemCount(1); // +1 for header row
803
804 fRawViewer = new TmfRawEventViewer(fSashForm, SWT.H_SCROLL | SWT.V_SCROLL);
805
806 fRawViewer.addSelectionListener(new Listener() {
807 @Override
808 public void handleEvent(final Event e) {
7df9ba27
PT
809 if (fTrace == null) {
810 return;
811 }
812 long rank;
db4721fb 813 if (e.data instanceof Long) {
7df9ba27 814 rank = (Long) e.data;
1e1bef82 815 } else if (e.data instanceof ITmfLocation) {
7df9ba27 816 rank = findRank((ITmfLocation) e.data);
db4721fb
PT
817 } else {
818 return;
819 }
7df9ba27
PT
820 int index = (int) rank;
821 if (fTable.getData(Key.FILTER_OBJ) != null) {
822 // +1 for top filter status row
823 index = fCache.getFilteredEventIndex(rank) + 1;
824 }
825 // +1 for header row
826 fTable.setSelection(index + 1);
827 fSelectedRank = rank;
9ba0a108 828 fSelectedBeginRank = fSelectedRank;
7df9ba27 829 updateStatusLine(null);
db4721fb
PT
830 final TableItem[] selection = fTable.getSelection();
831 if ((selection != null) && (selection.length > 0)) {
476a18ba
PT
832 TableItem item = fTable.getSelection()[0];
833 final TmfTimestamp ts = (TmfTimestamp) item.getData(Key.TIMESTAMP);
db4721fb 834 if (ts != null) {
97c71024 835 broadcast(new TmfSelectionRangeUpdatedSignal(TmfEventsTable.this, ts));
db4721fb 836 }
476a18ba
PT
837 if (item.getData() instanceof ITmfEvent) {
838 broadcast(new TmfEventSelectedSignal(TmfEventsTable.this, (ITmfEvent) item.getData()));
839 fireSelectionChanged(new SelectionChangedEvent(TmfEventsTable.this, new StructuredSelection(item.getData())));
840 } else {
841 fireSelectionChanged(new SelectionChangedEvent(TmfEventsTable.this, StructuredSelection.EMPTY));
842 }
db4721fb
PT
843 }
844 }
7df9ba27
PT
845
846 private long findRank(final ITmfLocation selectedLocation) {
847 final double selectedRatio = fTrace.getLocationRatio(selectedLocation);
848 long low = 0;
849 long high = fTrace.getNbEvents();
850 long rank = high / 2;
851 double ratio = -1;
852 while (ratio != selectedRatio) {
853 ITmfContext context = fTrace.seekEvent(rank);
854 ratio = fTrace.getLocationRatio(context.getLocation());
855 context.dispose();
856 if (ratio < selectedRatio) {
857 low = rank;
858 rank = (rank + high) / 2;
859 } else if (ratio > selectedRatio) {
860 high = rank;
861 rank = (rank + low) / 2;
862 }
863 if ((high - low) < 2) {
864 break;
865 }
866 }
867 return rank;
868 }
db4721fb
PT
869 });
870
871 fSashForm.setWeights(new int[] { 1, 1 });
872 fRawViewer.setVisible(false);
873
874 createPopupMenu();
875 }
876
346ffed7
MK
877 /**
878 * Checked menu creator to make columns visible or not.
879 *
880 * @param parent
881 * the parent menu
882 * @param column
883 * the column
884 */
be42703d
MK
885 private static IAction createHeaderAction(final TableColumn column) {
886 final IAction columnMenuAction = new Action(column.getText(), IAction.AS_CHECK_BOX) {
346ffed7 887 @Override
be42703d
MK
888 public void run() {
889 if (isChecked()) {
346ffed7
MK
890 column.setWidth((int) column.getData(Key.WIDTH));
891 column.setResizable(true);
892 } else {
893 column.setData(Key.WIDTH, column.getWidth());
894 column.setWidth(0);
895 column.setResizable(false);
896 }
897 }
be42703d
MK
898 };
899 columnMenuAction.setChecked(column.getResizable());
900 return columnMenuAction;
346ffed7
MK
901 }
902
be42703d
MK
903 private IAction createResetHeaderAction() {
904 return new Action(Messages.TmfEventsTable_ShowAll) {
346ffed7 905 @Override
be42703d 906 public void run() {
346ffed7
MK
907 for (TableColumn column : fTable.getColumns()) {
908 final Object widthVal = column.getData(Key.WIDTH);
909 if (widthVal instanceof Integer) {
910 Integer width = (Integer) widthVal;
911 if (!column.getResizable()) {
912 column.setWidth(width);
913 column.setResizable(true);
914 /*
915 * This is because Linux always resizes the last
916 * column to fill in the void, this means that
917 * hiding a column resizes others and we can have 10
918 * columns that are 1000 pixels wide by hiding the
919 * last one progressively.
920 */
921 if (IS_LINUX) {
922 column.pack();
923 }
924 }
925 }
926 }
346ffed7 927 }
be42703d 928 };
346ffed7
MK
929 }
930
baafe54c
AM
931 // ------------------------------------------------------------------------
932 // Operations
933 // ------------------------------------------------------------------------
934
a0a88f65
AM
935 /**
936 * Create a pop-up menu.
937 */
a890f499 938 private void createPopupMenu() {
9ba0a108
PT
939 final IAction copyAction = new Action(Messages.TmfEventsTable_CopyToClipboardActionText) {
940 @Override
941 public void run() {
942 ITmfTrace trace = fTrace;
943 if (trace == null || (fSelectedRank == -1 && fSelectedBeginRank == -1)) {
944 return;
945 }
946
947 List<TmfEventTableColumn> columns = new ArrayList<>();
948 for (int i : fTable.getColumnOrder()) {
949 TableColumn column = fTable.getColumns()[i];
950 // Omit the margin column and hidden columns
951 if (i >= EVENT_COLUMNS_START_INDEX && (column.getResizable() || column.getWidth() > 0)) {
952 columns.add(fColumns.get(i));
953 }
954 }
955
956 long start = Math.min(fSelectedBeginRank, fSelectedRank);
957 long end = Math.max(fSelectedBeginRank, fSelectedRank);
958 final ITmfFilter filter = (ITmfFilter) fTable.getData(Key.FILTER_OBJ);
959 IRunnableWithProgress operation = new CopyToClipboardOperation(trace, filter, columns, start, end);
960 try {
961 PlatformUI.getWorkbench().getProgressService().busyCursorWhile(operation);
962 } catch (InvocationTargetException e) {
963 Activator.getDefault().logError("Invocation target exception copying to clipboard ", e); //$NON-NLS-1$
964 } catch (InterruptedException e) {
965 /* ignored */
966 }
967 }
968 };
969
db4721fb
PT
970 final IAction showTableAction = new Action(Messages.TmfEventsTable_ShowTableActionText) {
971 @Override
972 public void run() {
973 fTable.setVisible(true);
974 fSashForm.layout();
975 }
976 };
977
978 final IAction hideTableAction = new Action(Messages.TmfEventsTable_HideTableActionText) {
979 @Override
980 public void run() {
981 fTable.setVisible(false);
982 fSashForm.layout();
983 }
984 };
985
986 final IAction showRawAction = new Action(Messages.TmfEventsTable_ShowRawActionText) {
987 @Override
988 public void run() {
989 fRawViewer.setVisible(true);
990 fSashForm.layout();
991 final int index = fTable.getSelectionIndex();
dadf6e1a 992 if (index >= 1) {
db4721fb
PT
993 fRawViewer.selectAndReveal(index - 1);
994 }
995 }
996 };
997
998 final IAction hideRawAction = new Action(Messages.TmfEventsTable_HideRawActionText) {
999 @Override
1000 public void run() {
1001 fRawViewer.setVisible(false);
1002 fSashForm.layout();
1003 }
1004 };
1005
029df6e3 1006 final IAction openCallsiteAction = new Action(Messages.TmfEventsTable_OpenSourceCodeActionText) {
60fb38b8
PT
1007 @Override
1008 public void run() {
1009 final TableItem items[] = fTable.getSelection();
1010 if (items.length != 1) {
1011 return;
1012 }
1013 final TableItem item = items[0];
1014
1015 final Object data = item.getData();
f47ed727
BH
1016 if (data instanceof ITmfSourceLookup) {
1017 ITmfSourceLookup event = (ITmfSourceLookup) data;
1018 ITmfCallsite cs = event.getCallsite();
60fb38b8
PT
1019 if (cs == null || cs.getFileName() == null) {
1020 return;
1021 }
1022 IMarker marker = null;
1023 try {
1024 String fileName = cs.getFileName();
03142470 1025 final String trimmedPath = fileName.replaceAll("\\.\\./", EMPTY_STRING); //$NON-NLS-1$
507b1336 1026 final ArrayList<IFile> files = new ArrayList<>();
60fb38b8
PT
1027 ResourcesPlugin.getWorkspace().getRoot().accept(new IResourceVisitor() {
1028 @Override
1029 public boolean visit(IResource resource) throws CoreException {
1030 if (resource instanceof IFile && resource.getFullPath().toString().endsWith(trimmedPath)) {
1031 files.add((IFile) resource);
1032 }
1033 return true;
1034 }
1035 });
1036 IFile file = null;
1037 if (files.size() > 1) {
1038 ListDialog dialog = new ListDialog(getTable().getShell());
1039 dialog.setContentProvider(ArrayContentProvider.getInstance());
1040 dialog.setLabelProvider(new LabelProvider() {
1041 @Override
1042 public String getText(Object element) {
1043 return ((IFile) element).getFullPath().toString();
1044 }
1045 });
1046 dialog.setInput(files);
029df6e3
JCK
1047 dialog.setTitle(Messages.TmfEventsTable_OpenSourceCodeSelectFileDialogTitle);
1048 dialog.setMessage(Messages.TmfEventsTable_OpenSourceCodeSelectFileDialogTitle + '\n' + cs.toString());
60fb38b8
PT
1049 dialog.open();
1050 Object[] result = dialog.getResult();
1051 if (result != null && result.length > 0) {
1052 file = (IFile) result[0];
1053 }
1054 } else if (files.size() == 1) {
1055 file = files.get(0);
1056 }
1057 if (file != null) {
1058 marker = file.createMarker(IMarker.MARKER);
1059 marker.setAttribute(IMarker.LINE_NUMBER, Long.valueOf(cs.getLineNumber()).intValue());
1060 IDE.openEditor(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(), marker);
1061 marker.delete();
8b7eb3cd 1062 } else if (files.size() == 0) {
029df6e3 1063 displayException(new FileNotFoundException('\'' + cs.toString() + '\'' + '\n' + Messages.TmfEventsTable_OpenSourceCodeNotFound));
60fb38b8 1064 }
60fb38b8 1065 } catch (CoreException e) {
8e95e814 1066 displayException(e);
60fb38b8
PT
1067 }
1068 }
1069 }
1070 };
1071
1072 final IAction openModelAction = new Action(Messages.TmfEventsTable_OpenModelActionText) {
ac44ef71
AR
1073 @Override
1074 public void run() {
1075
1076 final TableItem items[] = fTable.getSelection();
1077 if (items.length != 1) {
1078 return;
1079 }
1080 final TableItem item = items[0];
1081
1082 final Object eventData = item.getData();
f47ed727
BH
1083 if (eventData instanceof ITmfModelLookup) {
1084 String modelURI = ((ITmfModelLookup) eventData).getModelUri();
ac44ef71
AR
1085
1086 if (modelURI != null) {
1087 IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
1088
1089 IFile file = null;
1090 final URI uri = URI.createURI(modelURI);
1091 if (uri.isPlatformResource()) {
1092 IPath path = new Path(uri.toPlatformString(true));
1093 file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
1094 } else if (uri.isFile() && !uri.isRelative()) {
1095 file = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(
1096 new Path(uri.toFileString()));
1097 }
1098
1099 if (file != null) {
1100 try {
1101 /*
1102 * create a temporary validation marker on the
1103 * model file, remove it afterwards thus,
1104 * navigation works with all model editors
1105 * supporting the navigation to a marker
1106 */
1107 IMarker marker = file.createMarker(EValidator.MARKER);
1108 marker.setAttribute(EValidator.URI_ATTRIBUTE, modelURI);
1109 marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_INFO);
1110
1111 IDE.openEditor(activePage, marker, OpenStrategy.activateOnOpen());
1112 marker.delete();
8b7eb3cd 1113 } catch (CoreException e) {
ac44ef71
AR
1114 displayException(e);
1115 }
60fb38b8
PT
1116 } else {
1117 displayException(new FileNotFoundException('\'' + modelURI + '\'' + '\n' + Messages.TmfEventsTable_OpenModelUnsupportedURI));
ac44ef71
AR
1118 }
1119 }
1120 }
1121 }
1122 };
60fb38b8 1123
d3de0920
XR
1124 final IAction exportToTextAction = new Action(Messages.TmfEventsTable_Export_to_text) {
1125 @Override
1126 public void run() {
1127 IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
346fa221
MAL
1128 Object handlerServiceObject = activePage.getActiveEditor().getSite().getService(IHandlerService.class);
1129 IHandlerService handlerService = (IHandlerService) handlerServiceObject;
1130 Object cmdServiceObject = activePage.getActiveEditor().getSite().getService(ICommandService.class);
1131 ICommandService cmdService = (ICommandService) cmdServiceObject;
d3de0920 1132 try {
507b1336 1133 HashMap<String, Object> parameters = new HashMap<>();
d3de0920 1134 Command command = cmdService.getCommand(ExportToTextCommandHandler.COMMAND_ID);
03142470
BH
1135 ParameterizedCommand cmd = ParameterizedCommand.generateCommand(command, parameters);
1136
d3de0920 1137 IEvaluationContext context = handlerService.getCurrentState();
6817308e
PT
1138 List<TmfEventTableColumn> exportColumns = new ArrayList<>();
1139 for (int i : fTable.getColumnOrder()) {
9ba0a108
PT
1140 TableColumn column = fTable.getColumns()[i];
1141 // Omit the margin column and hidden columns
1142 if (i >= EVENT_COLUMNS_START_INDEX && (column.getResizable() || column.getWidth() > 0)) {
6817308e
PT
1143 exportColumns.add(fColumns.get(i));
1144 }
1145 }
03142470
BH
1146 context.addVariable(ExportToTextCommandHandler.TMF_EVENT_TABLE_COLUMNS_ID, exportColumns);
1147
d3de0920 1148 handlerService.executeCommandInContext(cmd, null, context);
23c625f1 1149 } catch (ExecutionException | NotDefinedException | NotEnabledException | NotHandledException e) {
d3de0920
XR
1150 displayException(e);
1151 }
1152 }
1153 };
1154
db4721fb
PT
1155 final IAction showSearchBarAction = new Action(Messages.TmfEventsTable_ShowSearchBarActionText) {
1156 @Override
1157 public void run() {
1158 fHeaderState = HeaderState.SEARCH;
1159 fTable.refresh();
110d44a1 1160 fTable.redraw();
db4721fb
PT
1161 }
1162 };
1163
1164 final IAction showFilterBarAction = new Action(Messages.TmfEventsTable_ShowFilterBarActionText) {
1165 @Override
1166 public void run() {
1167 fHeaderState = HeaderState.FILTER;
1168 fTable.refresh();
110d44a1 1169 fTable.redraw();
db4721fb
PT
1170 }
1171 };
1172
1173 final IAction clearFiltersAction = new Action(Messages.TmfEventsTable_ClearFiltersActionText) {
1174 @Override
1175 public void run() {
1176 clearFilters();
1177 }
1178 };
1179
03142470
BH
1180 final IAction collapseAction = new Action(Messages.TmfEventsTable_CollapseFilterMenuName) {
1181 @Override
1182 public void run() {
1183 applyFilter(new TmfCollapseFilter());
1184 }
1185 };
1186
db4721fb 1187 class ToggleBookmarkAction extends Action {
0126a8ca 1188 Long fRank;
db4721fb 1189
0126a8ca 1190 public ToggleBookmarkAction(final String text, final Long rank) {
db4721fb
PT
1191 super(text);
1192 fRank = rank;
1193 }
1194
1195 @Override
1196 public void run() {
1197 toggleBookmark(fRank);
1198 }
1199 }
1200
3bccf3b1
MAL
1201 fHeaderPopupMenuManager = new MenuManager();
1202 fHeaderPopupMenuManager.setRemoveAllWhenShown(true);
1203 fHeaderPopupMenuManager.addMenuListener(new IMenuListener() {
be42703d
MK
1204 @Override
1205 public void menuAboutToShow(IMenuManager manager) {
1206 for (int index : fTable.getColumnOrder()) {
1207 if (fTable.getColumns()[index].getData(Key.WIDTH) != null) {
3bccf3b1 1208 fHeaderPopupMenuManager.add(createHeaderAction(fTable.getColumns()[index]));
be42703d
MK
1209 }
1210 }
3bccf3b1
MAL
1211 fHeaderPopupMenuManager.add(new Separator());
1212 fHeaderPopupMenuManager.add(createResetHeaderAction());
be42703d
MK
1213 }
1214 });
1215
3bccf3b1
MAL
1216 fTablePopupMenuManager = new MenuManager();
1217 fTablePopupMenuManager.setRemoveAllWhenShown(true);
1218 fTablePopupMenuManager.addMenuListener(new IMenuListener() {
db4721fb
PT
1219 @Override
1220 public void menuAboutToShow(final IMenuManager manager) {
9ba0a108 1221 if (fTable.getSelectionIndices().length == 1 && fTable.getSelectionIndices()[0] == 0) {
db4721fb
PT
1222 // Right-click on header row
1223 if (fHeaderState == HeaderState.FILTER) {
3bccf3b1 1224 fTablePopupMenuManager.add(showSearchBarAction);
db4721fb 1225 } else {
3bccf3b1 1226 fTablePopupMenuManager.add(showFilterBarAction);
db4721fb
PT
1227 }
1228 return;
1229 }
665990bb 1230 final Point point = fTable.toControl(fLastMenuCursorLocation);
db4721fb
PT
1231 final TableItem item = fTable.getSelection().length > 0 ? fTable.getSelection()[0] : null;
1232 if (item != null) {
1233 final Rectangle imageBounds = item.getImageBounds(0);
1234 imageBounds.width = BOOKMARK_IMAGE.getBounds().width;
1235 if (point.x <= (imageBounds.x + imageBounds.width)) {
1236 // Right-click on left margin
1237 final Long rank = (Long) item.getData(Key.RANK);
1238 if ((rank != null) && (fBookmarksFile != null)) {
1239 if (fBookmarksMap.containsKey(rank)) {
3bccf3b1 1240 fTablePopupMenuManager.add(new ToggleBookmarkAction(
db4721fb
PT
1241 Messages.TmfEventsTable_RemoveBookmarkActionText, rank));
1242 } else {
3bccf3b1 1243 fTablePopupMenuManager.add(new ToggleBookmarkAction(
db4721fb
PT
1244 Messages.TmfEventsTable_AddBookmarkActionText, rank));
1245 }
1246 }
1247 return;
1248 }
1249 }
60fb38b8 1250
db4721fb 1251 // Right-click on table
9ba0a108 1252 if (fSelectedRank != -1 && fSelectedBeginRank != -1) {
3bccf3b1
MAL
1253 fTablePopupMenuManager.add(copyAction);
1254 fTablePopupMenuManager.add(new Separator());
9ba0a108 1255 }
db4721fb 1256 if (fTable.isVisible() && fRawViewer.isVisible()) {
3bccf3b1
MAL
1257 fTablePopupMenuManager.add(hideTableAction);
1258 fTablePopupMenuManager.add(hideRawAction);
db4721fb 1259 } else if (!fTable.isVisible()) {
3bccf3b1 1260 fTablePopupMenuManager.add(showTableAction);
db4721fb 1261 } else if (!fRawViewer.isVisible()) {
3bccf3b1 1262 fTablePopupMenuManager.add(showRawAction);
db4721fb 1263 }
3bccf3b1
MAL
1264 fTablePopupMenuManager.add(exportToTextAction);
1265 fTablePopupMenuManager.add(new Separator());
60fb38b8 1266
ac44ef71 1267 if (item != null) {
60fb38b8 1268 final Object data = item.getData();
f47ed727
BH
1269 Separator separator = null;
1270 if (data instanceof ITmfSourceLookup) {
1271 ITmfSourceLookup event = (ITmfSourceLookup) data;
60fb38b8 1272 if (event.getCallsite() != null) {
3bccf3b1 1273 fTablePopupMenuManager.add(openCallsiteAction);
60fb38b8
PT
1274 separator = new Separator();
1275 }
f47ed727
BH
1276 }
1277
1278 if (data instanceof ITmfModelLookup) {
1279 ITmfModelLookup event = (ITmfModelLookup) data;
1280 if (event.getModelUri() != null) {
3bccf3b1 1281 fTablePopupMenuManager.add(openModelAction);
60fb38b8
PT
1282 separator = new Separator();
1283 }
f47ed727 1284
60fb38b8 1285 if (separator != null) {
3bccf3b1 1286 fTablePopupMenuManager.add(separator);
ac44ef71
AR
1287 }
1288 }
1289 }
60fb38b8 1290
8b7eb3cd
MK
1291 /*
1292 * Only show collapse filter if at least one trace can be
1293 * collapsed.
1294 */
03142470
BH
1295 boolean isCollapsible = false;
1296 if (fTrace != null) {
c14c0757 1297 for (ITmfTrace trace : TmfTraceManager.getTraceSet(fTrace)) {
8b7eb3cd 1298 Class<? extends ITmfEvent> eventClass = trace.getEventType();
03142470
BH
1299 isCollapsible = ITmfCollapsibleEvent.class.isAssignableFrom(eventClass);
1300 if (isCollapsible) {
1301 break;
1302 }
1303 }
1304 }
1305
1306 if (isCollapsible && !(fTable.getData(Key.FILTER_OBJ) instanceof TmfCollapseFilter)) {
3bccf3b1
MAL
1307 fTablePopupMenuManager.add(collapseAction);
1308 fTablePopupMenuManager.add(new Separator());
03142470
BH
1309 }
1310
3bccf3b1 1311 fTablePopupMenuManager.add(clearFiltersAction);
db4721fb
PT
1312 final ITmfFilterTreeNode[] savedFilters = FilterManager.getSavedFilters();
1313 if (savedFilters.length > 0) {
1314 final MenuManager subMenu = new MenuManager(Messages.TmfEventsTable_ApplyPresetFilterMenuName);
1315 for (final ITmfFilterTreeNode node : savedFilters) {
1316 if (node instanceof TmfFilterNode) {
1317 final TmfFilterNode filter = (TmfFilterNode) node;
1318 subMenu.add(new Action(filter.getFilterName()) {
1319 @Override
1320 public void run() {
1321 applyFilter(filter);
1322 }
1323 });
1324 }
1325 }
3bccf3b1 1326 fTablePopupMenuManager.add(subMenu);
db4721fb 1327 }
3bccf3b1 1328 appendToTablePopupMenu(fTablePopupMenuManager, item);
db4721fb
PT
1329 }
1330 });
1331
3bccf3b1
MAL
1332 fRawViewerPopupMenuManager = new MenuManager();
1333 fRawViewerPopupMenuManager.setRemoveAllWhenShown(true);
1334 fRawViewerPopupMenuManager.addMenuListener(new IMenuListener() {
db4721fb
PT
1335 @Override
1336 public void menuAboutToShow(final IMenuManager manager) {
1337 if (fTable.isVisible() && fRawViewer.isVisible()) {
3bccf3b1
MAL
1338 fRawViewerPopupMenuManager.add(hideTableAction);
1339 fRawViewerPopupMenuManager.add(hideRawAction);
db4721fb 1340 } else if (!fTable.isVisible()) {
3bccf3b1 1341 fRawViewerPopupMenuManager.add(showTableAction);
db4721fb 1342 } else if (!fRawViewer.isVisible()) {
3bccf3b1 1343 fRawViewerPopupMenuManager.add(showRawAction);
db4721fb 1344 }
18d4ca27 1345 appendToRawPopupMenu(fRawViewerPopupMenuManager);
db4721fb
PT
1346 }
1347 });
1348
3bccf3b1 1349 fHeaderMenu = fHeaderPopupMenuManager.createContextMenu(fTable);
be42703d 1350
3bccf3b1 1351 fTablePopup = fTablePopupMenuManager.createContextMenu(fTable);
346ffed7 1352 fTable.setMenu(fTablePopup);
db4721fb 1353
3bccf3b1 1354 fRawTablePopup = fRawViewerPopupMenuManager.createContextMenu(fRawViewer);
346ffed7 1355 fRawViewer.setMenu(fRawTablePopup);
db4721fb
PT
1356 }
1357
a0a88f65
AM
1358 /**
1359 * Append an item to the event table's pop-up menu.
1360 *
1361 * @param tablePopupMenu
1362 * The menu manager
1363 * @param selectedItem
1364 * The item to append
1365 */
db4721fb
PT
1366 protected void appendToTablePopupMenu(final MenuManager tablePopupMenu, final TableItem selectedItem) {
1367 // override to append more actions
1368 }
1369
a0a88f65
AM
1370 /**
1371 * Append an item to the raw viewer's pop-up menu.
1372 *
1373 * @param rawViewerPopupMenu
1374 * The menu manager
1375 */
db4721fb
PT
1376 protected void appendToRawPopupMenu(final MenuManager rawViewerPopupMenu) {
1377 // override to append more actions
1378 }
1379
1380 @Override
1381 public void dispose() {
1382 stopSearchThread();
1383 stopFilterThread();
812e7197 1384 PlatformUI.getWorkbench().getThemeManager().removePropertyChangeListener(this);
db4721fb
PT
1385 ColorSettingsManager.removeColorSettingsListener(this);
1386 fComposite.dispose();
1387 if ((fTrace != null) && fDisposeOnClose) {
1388 fTrace.dispose();
1389 }
1390 fResourceManager.dispose();
3a97628a 1391 fRawViewer.dispose();
3bccf3b1
MAL
1392 if (fRawViewerPopupMenuManager != null) {
1393 fRawViewerPopupMenuManager.dispose();
1394 }
1395 if (fHeaderPopupMenuManager != null) {
1396 fHeaderPopupMenuManager.dispose();
1397 }
1398 if (fTablePopupMenuManager != null) {
1399 fTablePopupMenuManager.dispose();
1400 }
1401
db4721fb
PT
1402 super.dispose();
1403 }
1404
1405 /**
1406 * Assign a layout data object to this view.
1407 *
1408 * @param layoutData
1409 * The layout data to assign
1410 */
1411 public void setLayoutData(final Object layoutData) {
1412 fComposite.setLayoutData(layoutData);
1413 }
1414
1415 /**
1416 * Get the virtual table contained in this event table.
1417 *
1418 * @return The TMF virtual table
1419 */
1420 public TmfVirtualTable getTable() {
1421 return fTable;
1422 }
1423
1424 /**
1425 * @param columnData
baafe54c
AM
1426 * columnData
1427 * @deprecated The column headers are now set at the constructor, this
1428 * shouldn't be called anymore.
db4721fb 1429 */
baafe54c 1430 @Deprecated
8b7eb3cd 1431 protected void setColumnHeaders(final org.eclipse.tracecompass.tmf.ui.widgets.virtualtable.ColumnData[] columnData) {
baafe54c 1432 /* No-op */
db4721fb
PT
1433 }
1434
a0a88f65
AM
1435 /**
1436 * Set a table item's data.
1437 *
1438 * @param item
1439 * The item to set
1440 * @param event
1441 * Which trace event to link with this entry
1442 * @param rank
1443 * Which rank this event has in the trace/experiment
1444 */
db4721fb 1445 protected void setItemData(final TableItem item, final ITmfEvent event, final long rank) {
03142470 1446 String[] itemStrings = getItemStrings(fColumns, event);
32528869
BH
1447
1448 // Get the actual ITmfEvent from the CachedEvent
1449 ITmfEvent tmfEvent = event;
1450 if (event instanceof CachedEvent) {
1451 tmfEvent = ((CachedEvent) event).event;
1452 }
03142470 1453 item.setText(itemStrings);
32528869
BH
1454 item.setData(tmfEvent);
1455 item.setData(Key.TIMESTAMP, new TmfTimestamp(tmfEvent.getTimestamp()));
db4721fb
PT
1456 item.setData(Key.RANK, rank);
1457
2db176a0
PT
1458 final Collection<Long> markerIds = fBookmarksMap.get(rank);
1459 if (!markerIds.isEmpty()) {
1460 Joiner joiner = Joiner.on("\n -").skipNulls(); //$NON-NLS-1$
1461 List<Object> parts = new ArrayList<>();
1462 if (markerIds.size() > 1) {
1463 parts.add(Messages.TmfEventsTable_MultipleBookmarksToolTip);
1464 }
db4721fb 1465 try {
2db176a0
PT
1466 for (long markerId : markerIds) {
1467 final IMarker marker = fBookmarksFile.findMarker(markerId);
1468 parts.add(marker.getAttribute(IMarker.MESSAGE));
1469 }
1470 } catch (CoreException e) {
db4721fb
PT
1471 displayException(e);
1472 }
2db176a0 1473 item.setData(Key.BOOKMARK, joiner.join(parts));
db4721fb
PT
1474 } else {
1475 item.setData(Key.BOOKMARK, null);
1476 }
1477
1478 boolean searchMatch = false;
1479 boolean searchNoMatch = false;
1480 final ITmfFilter searchFilter = (ITmfFilter) fTable.getData(Key.SEARCH_OBJ);
1481 if (searchFilter != null) {
32528869 1482 if (searchFilter.matches(tmfEvent)) {
db4721fb
PT
1483 searchMatch = true;
1484 } else {
1485 searchNoMatch = true;
1486 }
1487 }
1488
32528869 1489 final ColorSetting colorSetting = ColorSettingsManager.getColorSetting(tmfEvent);
db4721fb
PT
1490 if (searchNoMatch) {
1491 item.setForeground(colorSetting.getDimmedForegroundColor());
1492 item.setBackground(colorSetting.getDimmedBackgroundColor());
1493 } else {
1494 item.setForeground(colorSetting.getForegroundColor());
1495 item.setBackground(colorSetting.getBackgroundColor());
1496 }
812e7197 1497 item.setFont(fFont);
db4721fb
PT
1498
1499 if (searchMatch) {
2db176a0 1500 if (!markerIds.isEmpty()) {
db4721fb
PT
1501 item.setImage(SEARCH_MATCH_BOOKMARK_IMAGE);
1502 } else {
1503 item.setImage(SEARCH_MATCH_IMAGE);
1504 }
2db176a0 1505 } else if (!markerIds.isEmpty()) {
db4721fb
PT
1506 item.setImage(BOOKMARK_IMAGE);
1507 } else {
1508 item.setImage((Image) null);
1509 }
03142470 1510
110d44a1
PT
1511 List<StyleRange> styleRanges = new ArrayList<>();
1512 for (int index = 0; index < fTable.getColumns().length; index++) {
1513 TableColumn column = fTable.getColumns()[index];
1514 String regex = null;
1515 if (fHeaderState == HeaderState.FILTER) {
1516 regex = (String) column.getData(Key.FILTER_TXT);
1517 } else if (searchMatch) {
1518 regex = (String) column.getData(Key.SEARCH_TXT);
1519 }
1520 if (regex != null) {
110d44a1
PT
1521 String text = item.getText(index);
1522 try {
1523 Pattern pattern = Pattern.compile(regex);
1524 Matcher matcher = pattern.matcher(text);
1525 while (matcher.find()) {
1526 int start = matcher.start();
1527 int length = matcher.end() - start;
1528 Color foreground = colorSetting.getForegroundColor();
bb7c657f 1529 Color background = fHighlightColor;
110d44a1
PT
1530 StyleRange styleRange = new StyleRange(start, length, foreground, background);
1531 styleRange.data = index;
1532 styleRanges.add(styleRange);
1533 }
1534 } catch (PatternSyntaxException e) {
1535 /* ignored */
1536 }
1537 }
1538 }
1539 if (styleRanges.isEmpty()) {
1540 item.setData(Key.STYLE_RANGES, null);
1541 } else {
1542 item.setData(Key.STYLE_RANGES, styleRanges);
1543 }
1544 item.getParent().redraw();
1545
03142470
BH
1546 if ((itemStrings[MARGIN_COLUMN_INDEX] != null) && !itemStrings[MARGIN_COLUMN_INDEX].isEmpty()) {
1547 packMarginColumn();
1548 }
db4721fb
PT
1549 }
1550
a0a88f65
AM
1551 /**
1552 * Set the item data of the header row.
1553 *
1554 * @param item
1555 * The item to use as table header
1556 */
db4721fb
PT
1557 protected void setHeaderRowItemData(final TableItem item) {
1558 String txtKey = null;
1559 if (fHeaderState == HeaderState.SEARCH) {
1560 item.setImage(SEARCH_IMAGE);
1561 txtKey = Key.SEARCH_TXT;
1562 } else if (fHeaderState == HeaderState.FILTER) {
1563 item.setImage(FILTER_IMAGE);
1564 txtKey = Key.FILTER_TXT;
1565 }
1566 item.setForeground(fGrayColor);
03142470
BH
1567 // Ignore collapse and image column
1568 for (int i = EVENT_COLUMNS_START_INDEX; i < fTable.getColumns().length; i++) {
db4721fb
PT
1569 final TableColumn column = fTable.getColumns()[i];
1570 final String filter = (String) column.getData(txtKey);
1571 if (filter == null) {
1572 if (fHeaderState == HeaderState.SEARCH) {
1573 item.setText(i, SEARCH_HINT);
1574 } else if (fHeaderState == HeaderState.FILTER) {
1575 item.setText(i, FILTER_HINT);
1576 }
1577 item.setForeground(i, fGrayColor);
812e7197 1578 item.setFont(i, fFont);
db4721fb
PT
1579 } else {
1580 item.setText(i, filter);
1581 item.setForeground(i, fGreenColor);
1582 item.setFont(i, fBoldFont);
1583 }
1584 }
1585 }
1586
a0a88f65
AM
1587 /**
1588 * Set the item data of the "filter status" row.
1589 *
1590 * @param item
1591 * The item to use as filter status row
1592 */
db4721fb
PT
1593 protected void setFilterStatusRowItemData(final TableItem item) {
1594 for (int i = 0; i < fTable.getColumns().length; i++) {
03142470 1595 if (i == MARGIN_COLUMN_INDEX) {
db4721fb
PT
1596 if ((fTrace == null) || (fFilterCheckCount == fTrace.getNbEvents())) {
1597 item.setImage(FILTER_IMAGE);
1598 } else {
1599 item.setImage(STOP_IMAGE);
1600 }
03142470
BH
1601 }
1602
1603 if (i == FILTER_SUMMARY_INDEX) {
1604 item.setText(FILTER_SUMMARY_INDEX, fFilterMatchCount + "/" + fFilterCheckCount); //$NON-NLS-1$
db4721fb 1605 } else {
03142470 1606 item.setText(i, EMPTY_STRING);
db4721fb
PT
1607 }
1608 }
93bfd50a 1609 item.setData(null);
db4721fb
PT
1610 item.setData(Key.TIMESTAMP, null);
1611 item.setData(Key.RANK, null);
110d44a1 1612 item.setData(Key.STYLE_RANGES, null);
db4721fb
PT
1613 item.setForeground(null);
1614 item.setBackground(null);
812e7197 1615 item.setFont(fFont);
db4721fb
PT
1616 }
1617
a0a88f65
AM
1618 /**
1619 * Create an editor for the header.
1620 */
a890f499 1621 private void createHeaderEditor() {
db4721fb
PT
1622 final TableEditor tableEditor = fTable.createTableEditor();
1623 tableEditor.horizontalAlignment = SWT.LEFT;
1624 tableEditor.verticalAlignment = SWT.CENTER;
1625 tableEditor.grabHorizontal = true;
1626 tableEditor.minimumWidth = 50;
1627
1628 // Handle the header row selection
1629 fTable.addMouseListener(new MouseAdapter() {
1630 int columnIndex;
1631 TableColumn column;
1632 TableItem item;
1633
1634 @Override
1635 public void mouseDown(final MouseEvent event) {
1636 if (event.button != 1) {
1637 return;
1638 }
1639 // Identify the selected row
1640 final Point point = new Point(event.x, event.y);
1641 item = fTable.getItem(point);
1642
1643 // Header row selected
1644 if ((item != null) && (fTable.indexOf(item) == 0)) {
1645
8ec1247f
BH
1646 // Margin column selected
1647 if (item.getBounds(0).contains(point)) {
db4721fb
PT
1648 if (fHeaderState == HeaderState.SEARCH) {
1649 fHeaderState = HeaderState.FILTER;
1650 } else if (fHeaderState == HeaderState.FILTER) {
1651 fHeaderState = HeaderState.SEARCH;
1652 }
3f43dc48 1653 fTable.setSelection(0);
db4721fb 1654 fTable.refresh();
110d44a1 1655 fTable.redraw();
db4721fb
PT
1656 return;
1657 }
1658
1659 // Identify the selected column
1660 columnIndex = -1;
1661 for (int i = 0; i < fTable.getColumns().length; i++) {
1662 final Rectangle rect = item.getBounds(i);
1663 if (rect.contains(point)) {
1664 columnIndex = i;
1665 break;
1666 }
1667 }
1668
1669 if (columnIndex == -1) {
1670 return;
1671 }
1672
1673 column = fTable.getColumns()[columnIndex];
1674
1675 String txtKey = null;
1676 if (fHeaderState == HeaderState.SEARCH) {
1677 txtKey = Key.SEARCH_TXT;
1678 } else if (fHeaderState == HeaderState.FILTER) {
1679 txtKey = Key.FILTER_TXT;
1680 }
1681
8b7eb3cd
MK
1682 /*
1683 * The control that will be the editor must be a child of
1684 * the Table
1685 */
db4721fb
PT
1686 final Text newEditor = (Text) fTable.createTableEditorControl(Text.class);
1687 final String headerString = (String) column.getData(txtKey);
1688 if (headerString != null) {
1689 newEditor.setText(headerString);
1690 }
1691 newEditor.addFocusListener(new FocusAdapter() {
1692 @Override
1693 public void focusLost(final FocusEvent e) {
1694 final boolean changed = updateHeader(newEditor.getText());
1695 if (changed) {
1696 applyHeader();
1697 }
1698 }
1699 });
1700 newEditor.addKeyListener(new KeyAdapter() {
1701 @Override
1702 public void keyPressed(final KeyEvent e) {
1703 if (e.character == SWT.CR) {
1704 updateHeader(newEditor.getText());
1705 applyHeader();
d81b17ea 1706
8b7eb3cd
MK
1707 /*
1708 * Set focus on the table so that the next
1709 * carriage return goes to the next result
1710 */
d81b17ea 1711 TmfEventsTable.this.getTable().setFocus();
db4721fb
PT
1712 } else if (e.character == SWT.ESC) {
1713 tableEditor.getEditor().dispose();
1714 }
1715 }
1716 });
1717 newEditor.selectAll();
1718 newEditor.setFocus();
1719 tableEditor.setEditor(newEditor, item, columnIndex);
1720 }
1721 }
1722
1723 /*
1724 * returns true is value was changed
1725 */
17dd85d7 1726 private boolean updateHeader(final String regex) {
db4721fb
PT
1727 String objKey = null;
1728 String txtKey = null;
1729 if (fHeaderState == HeaderState.SEARCH) {
1730 objKey = Key.SEARCH_OBJ;
1731 txtKey = Key.SEARCH_TXT;
1732 } else if (fHeaderState == HeaderState.FILTER) {
1733 objKey = Key.FILTER_OBJ;
1734 txtKey = Key.FILTER_TXT;
1735 }
17dd85d7 1736 if (regex.length() > 0) {
db4721fb 1737 try {
db4721fb
PT
1738 Pattern.compile(regex);
1739 if (regex.equals(column.getData(txtKey))) {
1740 tableEditor.getEditor().dispose();
1741 return false;
1742 }
ec34bf48 1743 final TmfFilterMatchesNode filter = new TmfFilterMatchesNode(null);
c409f16b
AM
1744 ITmfEventAspect aspect = (ITmfEventAspect) column.getData(Key.ASPECT);
1745 filter.setEventAspect(aspect);
db4721fb
PT
1746 filter.setRegex(regex);
1747 column.setData(objKey, filter);
1748 column.setData(txtKey, regex);
1749 } catch (final PatternSyntaxException ex) {
1750 tableEditor.getEditor().dispose();
1751 MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
1752 ex.getDescription(), ex.getMessage());
1753 return false;
1754 }
1755 } else {
1756 if (column.getData(txtKey) == null) {
1757 tableEditor.getEditor().dispose();
1758 return false;
1759 }
1760 column.setData(objKey, null);
1761 column.setData(txtKey, null);
1762 }
1763 return true;
1764 }
1765
1766 private void applyHeader() {
1767 if (fHeaderState == HeaderState.SEARCH) {
1768 stopSearchThread();
1769 final TmfFilterAndNode filter = new TmfFilterAndNode(null);
3dca7aa5
AM
1770 for (final TableColumn col : fTable.getColumns()) {
1771 final Object filterObj = col.getData(Key.SEARCH_OBJ);
db4721fb
PT
1772 if (filterObj instanceof ITmfFilterTreeNode) {
1773 filter.addChild((ITmfFilterTreeNode) filterObj);
1774 }
1775 }
1776 if (filter.getChildrenCount() > 0) {
1777 fTable.setData(Key.SEARCH_OBJ, filter);
1778 fTable.refresh();
1779 searchNext();
1780 fireSearchApplied(filter);
1781 } else {
1782 fTable.setData(Key.SEARCH_OBJ, null);
1783 fTable.refresh();
1784 fireSearchApplied(null);
1785 }
1786 } else if (fHeaderState == HeaderState.FILTER) {
1787 final TmfFilterAndNode filter = new TmfFilterAndNode(null);
3dca7aa5
AM
1788 for (final TableColumn col : fTable.getColumns()) {
1789 final Object filterObj = col.getData(Key.FILTER_OBJ);
db4721fb
PT
1790 if (filterObj instanceof ITmfFilterTreeNode) {
1791 filter.addChild((ITmfFilterTreeNode) filterObj);
1792 }
1793 }
1794 if (filter.getChildrenCount() > 0) {
1795 applyFilter(filter);
1796 } else {
1797 clearFilters();
1798 }
1799 }
1800
1801 tableEditor.getEditor().dispose();
1802 }
1803 });
1804
1805 fTable.addKeyListener(new KeyAdapter() {
1806 @Override
1807 public void keyPressed(final KeyEvent e) {
1808 e.doit = false;
1809 if (e.character == SWT.ESC) {
1810 stopFilterThread();
1811 stopSearchThread();
1812 fTable.refresh();
1813 } else if (e.character == SWT.DEL) {
1814 if (fHeaderState == HeaderState.SEARCH) {
1815 stopSearchThread();
1816 for (final TableColumn column : fTable.getColumns()) {
1817 column.setData(Key.SEARCH_OBJ, null);
1818 column.setData(Key.SEARCH_TXT, null);
1819 }
1820 fTable.setData(Key.SEARCH_OBJ, null);
1821 fTable.refresh();
1822 fireSearchApplied(null);
1823 } else if (fHeaderState == HeaderState.FILTER) {
1824 clearFilters();
1825 }
1826 } else if (e.character == SWT.CR) {
1827 if ((e.stateMask & SWT.SHIFT) == 0) {
1828 searchNext();
1829 } else {
1830 searchPrevious();
1831 }
1832 }
1833 }
1834 });
1835 }
1836
a0a88f65
AM
1837 /**
1838 * Send an event indicating a filter has been applied.
1839 *
1840 * @param filter
1841 * The filter that was just applied
1842 */
db4721fb 1843 protected void fireFilterApplied(final ITmfFilter filter) {
faa38350 1844 broadcast(new TmfEventFilterAppliedSignal(this, fTrace, filter));
db4721fb
PT
1845 }
1846
a0a88f65
AM
1847 /**
1848 * Send an event indicating that a search has been applied.
1849 *
1850 * @param filter
1851 * The search filter that was just applied
1852 */
db4721fb 1853 protected void fireSearchApplied(final ITmfFilter filter) {
faa38350 1854 broadcast(new TmfEventSearchAppliedSignal(this, fTrace, filter));
db4721fb
PT
1855 }
1856
a0a88f65
AM
1857 /**
1858 * Start the filtering thread.
1859 */
db4721fb
PT
1860 protected void startFilterThread() {
1861 synchronized (fFilterSyncObj) {
1862 final ITmfFilterTreeNode filter = (ITmfFilterTreeNode) fTable.getData(Key.FILTER_OBJ);
1863 if (fFilterThread == null || fFilterThread.filter != filter) {
1864 if (fFilterThread != null) {
1865 fFilterThread.cancel();
1866 fFilterThreadResume = false;
1867 }
1868 fFilterThread = new FilterThread(filter);
1869 fFilterThread.start();
1870 } else {
1871 fFilterThreadResume = true;
1872 }
1873 }
1874 }
1875
a0a88f65
AM
1876 /**
1877 * Stop the filtering thread.
1878 */
db4721fb
PT
1879 protected void stopFilterThread() {
1880 synchronized (fFilterSyncObj) {
1881 if (fFilterThread != null) {
1882 fFilterThread.cancel();
1883 fFilterThread = null;
1884 fFilterThreadResume = false;
1885 }
1886 }
1887 }
1888
1889 /**
a0a88f65
AM
1890 * Apply a filter.
1891 *
1892 * @param filter
1893 * The filter to apply
db4721fb
PT
1894 */
1895 protected void applyFilter(ITmfFilter filter) {
f29f8868
BH
1896 stopFilterThread();
1897 stopSearchThread();
db4721fb
PT
1898 fFilterMatchCount = 0;
1899 fFilterCheckCount = 0;
1900 fCache.applyFilter(filter);
1901 fTable.clearAll();
1902 fTable.setData(Key.FILTER_OBJ, filter);
8b7eb3cd
MK
1903 /* +1 for header row, +2 for top and bottom filter status rows */
1904 fTable.setItemCount(3);
db4721fb
PT
1905 startFilterThread();
1906 fireFilterApplied(filter);
1907 }
1908
a0a88f65
AM
1909 /**
1910 * Clear all currently active filters.
1911 */
db4721fb
PT
1912 protected void clearFilters() {
1913 if (fTable.getData(Key.FILTER_OBJ) == null) {
1914 return;
1915 }
1916 stopFilterThread();
1917 stopSearchThread();
1918 fCache.clearFilter();
1919 fTable.clearAll();
1920 for (final TableColumn column : fTable.getColumns()) {
1921 column.setData(Key.FILTER_OBJ, null);
1922 column.setData(Key.FILTER_TXT, null);
1923 }
1924 fTable.setData(Key.FILTER_OBJ, null);
1925 if (fTrace != null) {
8b7eb3cd
MK
1926 /* +1 for header row */
1927 fTable.setItemCount((int) fTrace.getNbEvents() + 1);
db4721fb 1928 } else {
8b7eb3cd
MK
1929 /* +1 for header row */
1930 fTable.setItemCount(1);
db4721fb
PT
1931 }
1932 fFilterMatchCount = 0;
1933 fFilterCheckCount = 0;
1934 if (fSelectedRank >= 0) {
8b7eb3cd
MK
1935 /* +1 for header row */
1936 fTable.setSelection((int) fSelectedRank + 1);
db4721fb
PT
1937 } else {
1938 fTable.setSelection(0);
1939 }
1940 fireFilterApplied(null);
3f43dc48 1941 updateStatusLine(null);
03142470
BH
1942
1943 // Set original width
1944 fTable.getColumns()[MARGIN_COLUMN_INDEX].setWidth(0);
1945 packMarginColumn();
db4721fb
PT
1946 }
1947
a0a88f65
AM
1948 /**
1949 * Wrapper Thread object for the filtering thread.
1950 */
db4721fb
PT
1951 protected class FilterThread extends Thread {
1952 private final ITmfFilterTreeNode filter;
fd3f1eff 1953 private TmfEventRequest request;
db4721fb
PT
1954 private boolean refreshBusy = false;
1955 private boolean refreshPending = false;
1956 private final Object syncObj = new Object();
1957
a0a88f65
AM
1958 /**
1959 * Constructor.
1960 *
1961 * @param filter
1962 * The filter this thread will be processing
1963 */
db4721fb
PT
1964 public FilterThread(final ITmfFilterTreeNode filter) {
1965 super("Filter Thread"); //$NON-NLS-1$
1966 this.filter = filter;
1967 }
1968
1969 @Override
1970 public void run() {
1971 if (fTrace == null) {
1972 return;
1973 }
1974 final int nbRequested = (int) (fTrace.getNbEvents() - fFilterCheckCount);
1975 if (nbRequested <= 0) {
1976 return;
1977 }
fd3f1eff
AM
1978 request = new TmfEventRequest(ITmfEvent.class, TmfTimeRange.ETERNITY,
1979 (int) fFilterCheckCount, nbRequested, ExecutionType.BACKGROUND) {
db4721fb
PT
1980 @Override
1981 public void handleData(final ITmfEvent event) {
1982 super.handleData(event);
1983 if (request.isCancelled()) {
1984 return;
1985 }
03142470 1986 boolean refresh = false;
db4721fb
PT
1987 if (filter.matches(event)) {
1988 final long rank = fFilterCheckCount;
1989 final int index = (int) fFilterMatchCount;
1990 fFilterMatchCount++;
bd54d363 1991 fCache.storeEvent(event, rank, index);
03142470
BH
1992 refresh = true;
1993 } else {
1994 if (filter instanceof TmfCollapseFilter) {
1995 fCache.updateCollapsedEvent((int) fFilterMatchCount - 1);
1996 }
1997 }
1998
1999 if (refresh || (fFilterCheckCount % 100) == 0) {
db4721fb
PT
2000 refreshTable();
2001 }
2002 fFilterCheckCount++;
2003 }
2004 };
fd3f1eff 2005 ((ITmfEventProvider) fTrace).sendRequest(request);
db4721fb
PT
2006 try {
2007 request.waitForCompletion();
2008 } catch (final InterruptedException e) {
2009 }
2010 refreshTable();
2011 synchronized (fFilterSyncObj) {
2012 fFilterThread = null;
2013 if (fFilterThreadResume) {
2014 fFilterThreadResume = false;
2015 fFilterThread = new FilterThread(filter);
2016 fFilterThread.start();
2017 }
2018 }
2019 }
2020
a0a88f65
AM
2021 /**
2022 * Refresh the filter.
2023 */
db4721fb
PT
2024 public void refreshTable() {
2025 synchronized (syncObj) {
2026 if (refreshBusy) {
2027 refreshPending = true;
2028 return;
2029 }
2030 refreshBusy = true;
2031 }
2032 Display.getDefault().asyncExec(new Runnable() {
2033 @Override
2034 public void run() {
2035 if (request.isCancelled()) {
2036 return;
2037 }
2038 if (fTable.isDisposed()) {
2039 return;
2040 }
8b7eb3cd
MK
2041 /*
2042 * +1 for header row, +2 for top and bottom filter status
2043 * rows
2044 */
2045 fTable.setItemCount((int) fFilterMatchCount + 3);
db4721fb
PT
2046 fTable.refresh();
2047 synchronized (syncObj) {
2048 refreshBusy = false;
2049 if (refreshPending) {
2050 refreshPending = false;
2051 refreshTable();
2052 }
2053 }
2054 }
2055 });
2056 }
2057
a0a88f65
AM
2058 /**
2059 * Cancel this filtering thread.
2060 */
db4721fb
PT
2061 public void cancel() {
2062 if (request != null) {
2063 request.cancel();
2064 }
2065 }
2066 }
2067
a0a88f65
AM
2068 /**
2069 * Go to the next item of a search.
2070 */
db4721fb
PT
2071 protected void searchNext() {
2072 synchronized (fSearchSyncObj) {
2073 if (fSearchThread != null) {
2074 return;
2075 }
2076 final ITmfFilterTreeNode searchFilter = (ITmfFilterTreeNode) fTable.getData(Key.SEARCH_OBJ);
2077 if (searchFilter == null) {
2078 return;
2079 }
2080 final int selectionIndex = fTable.getSelectionIndex();
2081 int startIndex;
2082 if (selectionIndex > 0) {
8b7eb3cd
MK
2083 /* -1 for header row, +1 for next event */
2084 startIndex = selectionIndex;
db4721fb 2085 } else {
8b7eb3cd
MK
2086 /*
2087 * header row is selected, start at top event
2088 */
2089 /* -1 for header row */
2090 startIndex = Math.max(0, fTable.getTopIndex() - 1);
db4721fb
PT
2091 }
2092 final ITmfFilterTreeNode eventFilter = (ITmfFilterTreeNode) fTable.getData(Key.FILTER_OBJ);
60fb38b8 2093 if (eventFilter != null) {
8b7eb3cd
MK
2094 // -1 for top filter status row
2095 startIndex = Math.max(0, startIndex - 1);
db4721fb
PT
2096 }
2097 fSearchThread = new SearchThread(searchFilter, eventFilter, startIndex, fSelectedRank, Direction.FORWARD);
2098 fSearchThread.schedule();
2099 }
2100 }
2101
a0a88f65
AM
2102 /**
2103 * Go to the previous item of a search.
2104 */
db4721fb
PT
2105 protected void searchPrevious() {
2106 synchronized (fSearchSyncObj) {
2107 if (fSearchThread != null) {
2108 return;
2109 }
2110 final ITmfFilterTreeNode searchFilter = (ITmfFilterTreeNode) fTable.getData(Key.SEARCH_OBJ);
2111 if (searchFilter == null) {
2112 return;
2113 }
2114 final int selectionIndex = fTable.getSelectionIndex();
2115 int startIndex;
2116 if (selectionIndex > 0) {
8b7eb3cd
MK
2117 /* -1 for header row, -1 for previous event */
2118 startIndex = selectionIndex - 2;
db4721fb 2119 } else {
8b7eb3cd
MK
2120 /*
2121 * Header row is selected, start at precedent of top event
2122 */
2123 /* -1 for header row, -1 for previous event */
2124 startIndex = fTable.getTopIndex() - 2;
db4721fb
PT
2125 }
2126 final ITmfFilterTreeNode eventFilter = (ITmfFilterTreeNode) fTable.getData(Key.FILTER_OBJ);
60fb38b8 2127 if (eventFilter != null) {
8b7eb3cd
MK
2128 /* -1 for top filter status row */
2129 startIndex = startIndex - 1;
db4721fb
PT
2130 }
2131 fSearchThread = new SearchThread(searchFilter, eventFilter, startIndex, fSelectedRank, Direction.BACKWARD);
2132 fSearchThread.schedule();
2133 }
2134 }
2135
a0a88f65
AM
2136 /**
2137 * Stop the search thread.
2138 */
db4721fb
PT
2139 protected void stopSearchThread() {
2140 fPendingGotoRank = -1;
2141 synchronized (fSearchSyncObj) {
2142 if (fSearchThread != null) {
2143 fSearchThread.cancel();
2144 fSearchThread = null;
2145 }
2146 }
2147 }
2148
a0a88f65
AM
2149 /**
2150 * Wrapper for the search thread.
2151 */
db4721fb 2152 protected class SearchThread extends Job {
a0a88f65
AM
2153
2154 private ITmfFilterTreeNode searchFilter;
2155 private ITmfFilterTreeNode eventFilter;
2156 private int startIndex;
2157 private int direction;
2158 private long rank;
2159 private long foundRank = -1;
fd3f1eff 2160 private TmfEventRequest request;
ae3ffd37 2161 private ITmfTimestamp foundTimestamp = null;
db4721fb 2162
a0a88f65
AM
2163 /**
2164 * Constructor.
2165 *
2166 * @param searchFilter
2167 * The search filter
2168 * @param eventFilter
2169 * The event filter
2170 * @param startIndex
2171 * The index at which we should start searching
2172 * @param currentRank
2173 * The current rank
2174 * @param direction
2175 * In which direction should we search, forward or backwards
2176 */
2177 public SearchThread(final ITmfFilterTreeNode searchFilter,
2178 final ITmfFilterTreeNode eventFilter, final int startIndex,
db4721fb
PT
2179 final long currentRank, final int direction) {
2180 super(Messages.TmfEventsTable_SearchingJobName);
2181 this.searchFilter = searchFilter;
2182 this.eventFilter = eventFilter;
2183 this.startIndex = startIndex;
2184 this.rank = currentRank;
2185 this.direction = direction;
2186 }
2187
2188 @Override
2189 protected IStatus run(final IProgressMonitor monitor) {
f4e06774
FLN
2190 final ITmfTrace trace = fTrace;
2191 if (trace == null) {
db4721fb
PT
2192 return Status.OK_STATUS;
2193 }
2194 final Display display = Display.getDefault();
2195 if (startIndex < 0) {
f4e06774 2196 rank = (int) trace.getNbEvents() - 1;
8b7eb3cd
MK
2197 /*
2198 * -1 for header row, -3 for header and top and bottom filter
2199 * status rows
2200 */
2201 } else if (startIndex >= (fTable.getItemCount() - (eventFilter == null ? 1 : 3))) {
db4721fb
PT
2202 rank = 0;
2203 } else {
2204 int idx = startIndex;
2205 while (foundRank == -1) {
2206 final CachedEvent event = fCache.peekEvent(idx);
2207 if (event == null) {
2208 break;
2209 }
2210 rank = event.rank;
2211 if (searchFilter.matches(event.event) && ((eventFilter == null) || eventFilter.matches(event.event))) {
2212 foundRank = event.rank;
ae3ffd37 2213 foundTimestamp = event.event.getTimestamp();
db4721fb
PT
2214 break;
2215 }
2216 if (direction == Direction.FORWARD) {
2217 idx++;
2218 } else {
2219 idx--;
2220 }
2221 }
2222 if (foundRank == -1) {
2223 if (direction == Direction.FORWARD) {
2224 rank++;
f4e06774 2225 if (rank > (trace.getNbEvents() - 1)) {
db4721fb
PT
2226 rank = 0;
2227 }
2228 } else {
2229 rank--;
2230 if (rank < 0) {
f4e06774 2231 rank = (int) trace.getNbEvents() - 1;
db4721fb
PT
2232 }
2233 }
2234 }
2235 }
2236 final int startRank = (int) rank;
2237 boolean wrapped = false;
f4e06774
FLN
2238 while (!monitor.isCanceled() && (foundRank == -1)) {
2239 int nbRequested = (direction == Direction.FORWARD ? Integer.MAX_VALUE : Math.min((int) rank + 1, trace.getCacheSize()));
db4721fb 2240 if (direction == Direction.BACKWARD) {
f4e06774 2241 rank = Math.max(0, rank - trace.getCacheSize() + 1);
db4721fb 2242 }
fd3f1eff
AM
2243 request = new TmfEventRequest(ITmfEvent.class, TmfTimeRange.ETERNITY,
2244 (int) rank, nbRequested, ExecutionType.BACKGROUND) {
db4721fb
PT
2245 long currentRank = rank;
2246
2247 @Override
2248 public void handleData(final ITmfEvent event) {
2249 super.handleData(event);
2250 if (searchFilter.matches(event) && ((eventFilter == null) || eventFilter.matches(event))) {
2251 foundRank = currentRank;
ae3ffd37 2252 foundTimestamp = event.getTimestamp();
db4721fb
PT
2253 if (direction == Direction.FORWARD) {
2254 done();
2255 return;
2256 }
2257 }
2258 currentRank++;
2259 }
2260 };
f4e06774 2261 ((ITmfEventProvider) trace).sendRequest(request);
db4721fb
PT
2262 try {
2263 request.waitForCompletion();
2264 if (request.isCancelled()) {
2265 return Status.OK_STATUS;
2266 }
2267 } catch (final InterruptedException e) {
2268 synchronized (fSearchSyncObj) {
2269 fSearchThread = null;
2270 }
2271 return Status.OK_STATUS;
2272 }
2273 if (foundRank == -1) {
2274 if (direction == Direction.FORWARD) {
2275 if (rank == 0) {
2276 synchronized (fSearchSyncObj) {
2277 fSearchThread = null;
2278 }
2279 return Status.OK_STATUS;
2280 }
2281 nbRequested = (int) rank;
2282 rank = 0;
2283 wrapped = true;
2284 } else {
2285 rank--;
2286 if (rank < 0) {
f4e06774 2287 rank = (int) trace.getNbEvents() - 1;
db4721fb
PT
2288 wrapped = true;
2289 }
2290 if ((rank <= startRank) && wrapped) {
2291 synchronized (fSearchSyncObj) {
2292 fSearchThread = null;
2293 }
2294 return Status.OK_STATUS;
2295 }
2296 }
2297 }
2298 }
2299 int index = (int) foundRank;
2300 if (eventFilter != null) {
2301 index = fCache.getFilteredEventIndex(foundRank);
2302 }
8b7eb3cd
MK
2303 /* +1 for header row, +1 for top filter status row */
2304 final int selection = index + 1 + (eventFilter != null ? +1 : 0);
db4721fb
PT
2305
2306 display.asyncExec(new Runnable() {
2307 @Override
2308 public void run() {
2309 if (monitor.isCanceled()) {
2310 return;
2311 }
2312 if (fTable.isDisposed()) {
2313 return;
2314 }
2315 fTable.setSelection(selection);
2316 fSelectedRank = foundRank;
9ba0a108 2317 fSelectedBeginRank = fSelectedRank;
ae3ffd37
PT
2318 fRawViewer.selectAndReveal(fSelectedRank);
2319 if (foundTimestamp != null) {
97c71024 2320 broadcast(new TmfSelectionRangeUpdatedSignal(TmfEventsTable.this, foundTimestamp));
ae3ffd37 2321 }
a56ec2b8 2322 fireSelectionChanged(new SelectionChangedEvent(TmfEventsTable.this, getSelection()));
db4721fb
PT
2323 synchronized (fSearchSyncObj) {
2324 fSearchThread = null;
2325 }
3f43dc48 2326 updateStatusLine(null);
db4721fb
PT
2327 }
2328 });
2329 return Status.OK_STATUS;
2330 }
2331
2332 @Override
2333 protected void canceling() {
2334 request.cancel();
2335 synchronized (fSearchSyncObj) {
2336 fSearchThread = null;
2337 }
2338 }
2339 }
2340
a0a88f65
AM
2341 /**
2342 * Create the resources.
2343 */
a890f499 2344 private void createResources() {
8b7eb3cd 2345 fGrayColor = fResourceManager.createColor(ColorUtil.blend(fTable.getBackground().getRGB(), fTable.getForeground().getRGB()));
78688b59 2346 fGreenColor = PlatformUI.getWorkbench().getDisplay().getSystemColor(SWT.COLOR_DARK_GREEN);
812e7197
PT
2347 }
2348
2349 /**
2350 * Initialize the fonts.
812e7197 2351 */
a890f499 2352 private void initializeFonts() {
812e7197
PT
2353 FontRegistry fontRegistry = PlatformUI.getWorkbench().getThemeManager().getCurrentTheme().getFontRegistry();
2354 fFont = fontRegistry.get(FONT_DEFINITION_ID);
2355 fBoldFont = fontRegistry.getBold(FONT_DEFINITION_ID);
2356 fTable.setFont(fFont);
2357 /* Column header font cannot be set. See Bug 63038 */
2358 }
2359
bb7c657f
PT
2360 /**
2361 * Initialize the colors.
bb7c657f 2362 */
a890f499 2363 private void initializeColors() {
bb7c657f
PT
2364 ColorRegistry colorRegistry = PlatformUI.getWorkbench().getThemeManager().getCurrentTheme().getColorRegistry();
2365 fHighlightColor = colorRegistry.get(HIGHLIGHT_COLOR_DEFINITION_ID);
2366 }
2367
812e7197
PT
2368 /**
2369 * @since 1.0
2370 */
2371 @Override
2372 public void propertyChange(PropertyChangeEvent event) {
2373 if ((IThemeManager.CHANGE_CURRENT_THEME.equals(event.getProperty())) ||
2374 (FONT_DEFINITION_ID.equals(event.getProperty()))) {
2375 initializeFonts();
2376 fTable.refresh();
2377 }
bb7c657f
PT
2378 if ((IThemeManager.CHANGE_CURRENT_THEME.equals(event.getProperty())) ||
2379 (HIGHLIGHT_COLOR_DEFINITION_ID.equals(event.getProperty()))) {
2380 initializeColors();
2381 fTable.refresh();
2382 }
db4721fb
PT
2383 }
2384
a0a88f65
AM
2385 /**
2386 * Pack the columns.
2387 */
db4721fb
PT
2388 protected void packColumns() {
2389 if (fPackDone) {
2390 return;
2391 }
d3bc98ee 2392 fTable.setRedraw(false);
03142470
BH
2393 try {
2394 TableColumn tableColumns[] = fTable.getColumns();
2395 for (int i = 0; i < tableColumns.length; i++) {
2396 final TableColumn column = tableColumns[i];
2397 packSingleColumn(i, column);
2398 }
346ffed7 2399
03142470
BH
2400 } finally {
2401 // Make sure that redraw is always enabled.
2402 fTable.setRedraw(true);
2403 }
2404 fPackDone = true;
2405 }
d3bc98ee 2406
03142470
BH
2407 private void packMarginColumn() {
2408 TableColumn[] columns = fTable.getColumns();
2409 if (columns.length > 0) {
2410 packSingleColumn(0, columns[0]);
2411 }
2412 }
f29f8868 2413
03142470
BH
2414 private void packSingleColumn(int i, final TableColumn column) {
2415 final int headerWidth = column.getWidth();
2416 column.pack();
8b7eb3cd
MK
2417 /*
2418 * Workaround for Linux which doesn't consider the image width of
2419 * search/filter row in TableColumn.pack() after having executed
2420 * TableItem.setImage(null) for other rows than search/filter row.
2421 */
03142470
BH
2422 boolean isCollapseFilter = fTable.getData(Key.FILTER_OBJ) instanceof TmfCollapseFilter;
2423 if (IS_LINUX && (i == 0) && isCollapseFilter) {
2424 column.setWidth(column.getWidth() + SEARCH_IMAGE.getBounds().width);
db4721fb 2425 }
d3bc98ee 2426
03142470
BH
2427 if (column.getWidth() < headerWidth) {
2428 column.setWidth(headerWidth);
2429 }
db4721fb
PT
2430 }
2431
d3de0920 2432 /**
baafe54c
AM
2433 * Get the array of item strings (e.g., what to display in each cell of the
2434 * table row) corresponding to the columns and trace event passed in
2435 * parameter. The order of the Strings in the returned array will correspond
2436 * to the iteration order of 'columns'.
d3de0920 2437 *
baafe54c
AM
2438 * <p>
2439 * To ensure consistent results, make sure only call this within a scope
2440 * synchronized on 'columns'! If the order of 'columns' changes right after
2441 * this method is called, the returned value won't be ordered correctly
2442 * anymore.
2443 */
2444 private static String[] getItemStrings(List<TmfEventTableColumn> columns, ITmfEvent event) {
2445 if (event == null) {
2446 return EMPTY_STRING_ARRAY;
2447 }
2448 synchronized (columns) {
2449 List<String> itemStrings = new ArrayList<>(columns.size());
2450 for (TmfEventTableColumn column : columns) {
03142470
BH
2451 ITmfEvent passedEvent = event;
2452 if (!(column instanceof TmfMarginColumn) && (event instanceof CachedEvent)) {
8b7eb3cd
MK
2453 /*
2454 * Make sure that the event object from the trace is passed
2455 * to all columns but the TmfMarginColumn
2456 */
03142470
BH
2457 passedEvent = ((CachedEvent) event).event;
2458 }
2459 if (passedEvent == null) {
2460 itemStrings.add(EMPTY_STRING);
2461 } else {
2462 itemStrings.add(column.getItemString(passedEvent));
2463 }
2464
baafe54c
AM
2465 }
2466 return itemStrings.toArray(new String[0]);
2467 }
2468 }
2469
2470 /**
2471 * Get the contents of the row in the events table corresponding to an
2472 * event. The order of the elements corresponds to the current order of the
2473 * columns.
a0a88f65 2474 *
db4721fb 2475 * @param event
cf37ad9f
AM
2476 * The event printed in this row
2477 * @return The event row entries
db4721fb 2478 */
cf37ad9f 2479 public String[] getItemStrings(ITmfEvent event) {
6817308e
PT
2480 List<TmfEventTableColumn> columns = new ArrayList<>();
2481 for (int i : fTable.getColumnOrder()) {
2482 columns.add(fColumns.get(i));
2483 }
2484 return getItemStrings(columns, event);
db4721fb
PT
2485 }
2486
14665360 2487 /**
8b7eb3cd
MK
2488 * Returns an array of zero-relative integers that map the creation order of
2489 * the receiver's columns to the order in which they are currently being
2490 * displayed.
14665360 2491 * <p>
8b7eb3cd
MK
2492 * Specifically, the indices of the returned array represent the current
2493 * visual order of the columns, and the contents of the array represent the
2494 * creation order of the columns.
14665360
PT
2495 *
2496 * @return the current visual order of the receiver's columns
2497 * @since 1.0
2498 */
2499 public int[] getColumnOrder() {
2500 return fColumnOrder;
2501 }
2502
2503 /**
8b7eb3cd
MK
2504 * Sets the order that the columns in the receiver should be displayed in to
2505 * the given argument which is described in terms of the zero-relative
2506 * ordering of when the columns were added.
14665360 2507 * <p>
8b7eb3cd
MK
2508 * Specifically, the contents of the array represent the original position
2509 * of each column at the time its creation.
14665360 2510 *
8b7eb3cd
MK
2511 * @param order
2512 * the new order to display the columns
14665360
PT
2513 * @since 1.0
2514 */
2515 public void setColumnOrder(int[] order) {
2516 if (order == null || order.length != fTable.getColumns().length) {
2517 return;
2518 }
2519 fTable.setColumnOrder(order);
2520 fColumnOrder = fTable.getColumnOrder();
2521 }
2522
db4721fb
PT
2523 /**
2524 * Notify this table that is got the UI focus.
2525 */
2526 public void setFocus() {
2527 fTable.setFocus();
2528 }
2529
2530 /**
2531 * Assign a new trace to this event table.
2532 *
2533 * @param trace
2534 * The trace to assign to this event table
2535 * @param disposeOnClose
2536 * true if the trace should be disposed when the table is
2537 * disposed
2538 */
2539 public void setTrace(final ITmfTrace trace, final boolean disposeOnClose) {
2540 if ((fTrace != null) && fDisposeOnClose) {
2541 fTrace.dispose();
2542 }
2543 fTrace = trace;
2544 fPackDone = false;
db4721fb
PT
2545 fDisposeOnClose = disposeOnClose;
2546
2547 // Perform the updates on the UI thread
78688b59 2548 PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {
db4721fb
PT
2549 @Override
2550 public void run() {
9ba0a108
PT
2551 fSelectedRank = -1;
2552 fSelectedBeginRank = -1;
db4721fb 2553 fTable.removeAll();
f4e06774
FLN
2554 fCache.setTrace(trace); // Clear the cache
2555 if (trace != null) {
2556 if (!fTable.isDisposed()) {
db4721fb 2557 if (fTable.getData(Key.FILTER_OBJ) == null) {
8b7eb3cd 2558 // +1 for header row
f4e06774 2559 fTable.setItemCount((int) trace.getNbEvents() + 1);
db4721fb
PT
2560 } else {
2561 stopFilterThread();
2562 fFilterMatchCount = 0;
2563 fFilterCheckCount = 0;
8b7eb3cd
MK
2564 /*
2565 * +1 for header row, +2 for top and bottom filter
2566 * status rows
2567 */
2568 fTable.setItemCount(3);
db4721fb
PT
2569 startFilterThread();
2570 }
2571 }
db4721fb 2572 }
f4e06774 2573 fRawViewer.setTrace(trace);
db4721fb
PT
2574 }
2575 });
2576 }
2577
3f43dc48
PT
2578 /**
2579 * Assign the status line manager
2580 *
2581 * @param statusLineManager
8b7eb3cd
MK
2582 * The status line manager, or null to disable status line
2583 * messages
3f43dc48
PT
2584 */
2585 public void setStatusLineManager(IStatusLineManager statusLineManager) {
2586 if (fStatusLineManager != null && statusLineManager == null) {
03142470 2587 fStatusLineManager.setMessage(EMPTY_STRING);
3f43dc48
PT
2588 }
2589 fStatusLineManager = statusLineManager;
2590 }
2591
2592 private void updateStatusLine(ITmfTimestamp delta) {
2593 if (fStatusLineManager != null) {
2594 if (delta != null) {
2595 fStatusLineManager.setMessage("\u0394: " + delta); //$NON-NLS-1$
2596 } else {
2597 fStatusLineManager.setMessage(null);
2598 }
2599 }
2600 }
2601
db4721fb
PT
2602 // ------------------------------------------------------------------------
2603 // Event cache
2604 // ------------------------------------------------------------------------
2605
2606 /**
2607 * Notify that the event cache has been updated
2608 *
2609 * @param completed
2610 * Also notify if the populating of the cache is complete, or
2611 * not.
2612 */
2613 public void cacheUpdated(final boolean completed) {
2614 synchronized (fCacheUpdateSyncObj) {
2615 if (fCacheUpdateBusy) {
2616 fCacheUpdatePending = true;
2617 fCacheUpdateCompleted = completed;
2618 return;
2619 }
2620 fCacheUpdateBusy = true;
2621 }
2622 // Event cache is now updated. Perform update on the UI thread
78688b59
PT
2623 PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
2624 @Override
2625 public void run() {
2626 if (!fTable.isDisposed()) {
2627 fTable.refresh();
2628 packColumns();
2629 }
2630 if (completed) {
2631 populateCompleted();
2632 }
2633 synchronized (fCacheUpdateSyncObj) {
2634 fCacheUpdateBusy = false;
2635 if (fCacheUpdatePending) {
2636 fCacheUpdatePending = false;
2637 cacheUpdated(fCacheUpdateCompleted);
db4721fb
PT
2638 }
2639 }
78688b59
PT
2640 }
2641 });
db4721fb
PT
2642 }
2643
a0a88f65
AM
2644 /**
2645 * Callback for when populating the table is complete.
2646 */
db4721fb
PT
2647 protected void populateCompleted() {
2648 // Nothing by default;
2649 }
2650
93bfd50a
PT
2651 // ------------------------------------------------------------------------
2652 // ISelectionProvider
2653 // ------------------------------------------------------------------------
2654
93bfd50a
PT
2655 @Override
2656 public void addSelectionChangedListener(ISelectionChangedListener listener) {
2657 selectionChangedListeners.add(listener);
2658 }
2659
93bfd50a
PT
2660 @Override
2661 public ISelection getSelection() {
2662 if (fTable == null || fTable.isDisposed()) {
2663 return StructuredSelection.EMPTY;
2664 }
507b1336 2665 List<Object> list = new ArrayList<>(fTable.getSelection().length);
93bfd50a
PT
2666 for (TableItem item : fTable.getSelection()) {
2667 if (item.getData() != null) {
2668 list.add(item.getData());
2669 }
2670 }
2671 return new StructuredSelection(list);
2672 }
2673
93bfd50a
PT
2674 @Override
2675 public void removeSelectionChangedListener(ISelectionChangedListener listener) {
2676 selectionChangedListeners.remove(listener);
2677 }
2678
93bfd50a
PT
2679 @Override
2680 public void setSelection(ISelection selection) {
2681 // not implemented
2682 }
2683
2684 /**
8b7eb3cd
MK
2685 * Notifies any selection changed listeners that the viewer's selection has
2686 * changed. Only listeners registered at the time this method is called are
2687 * notified.
93bfd50a 2688 *
8b7eb3cd
MK
2689 * @param event
2690 * a selection changed event
93bfd50a
PT
2691 *
2692 * @see ISelectionChangedListener#selectionChanged
93bfd50a
PT
2693 */
2694 protected void fireSelectionChanged(final SelectionChangedEvent event) {
2695 Object[] listeners = selectionChangedListeners.getListeners();
2696 for (int i = 0; i < listeners.length; ++i) {
2697 final ISelectionChangedListener l = (ISelectionChangedListener) listeners[i];
2698 SafeRunnable.run(new SafeRunnable() {
faa38350 2699 @Override
93bfd50a
PT
2700 public void run() {
2701 l.selectionChanged(event);
2702 }
2703 });
2704 }
2705 }
2706
db4721fb
PT
2707 // ------------------------------------------------------------------------
2708 // Bookmark handling
2709 // ------------------------------------------------------------------------
2710
2711 /**
2712 * Add a bookmark to this event table.
2713 *
2714 * @param bookmarksFile
2715 * The file to use for the bookmarks
2716 */
2717 public void addBookmark(final IFile bookmarksFile) {
2718 fBookmarksFile = bookmarksFile;
2719 final TableItem[] selection = fTable.getSelection();
2720 if (selection.length > 0) {
2721 final TableItem tableItem = selection[0];
2722 if (tableItem.getData(Key.RANK) != null) {
2723 final StringBuffer defaultMessage = new StringBuffer();
2724 for (int i = 0; i < fTable.getColumns().length; i++) {
60fb38b8 2725 if (i > 0) {
db4721fb
PT
2726 defaultMessage.append(", "); //$NON-NLS-1$
2727 }
2728 defaultMessage.append(tableItem.getText(i));
2729 }
3be2946f
PT
2730 final InputDialog dialog = new MultiLineInputDialog(
2731 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
2732 Messages.TmfEventsTable_AddBookmarkDialogTitle,
2733 Messages.TmfEventsTable_AddBookmarkDialogMessage,
2734 defaultMessage.toString());
db4721fb
PT
2735 if (dialog.open() == Window.OK) {
2736 final String message = dialog.getValue();
2737 try {
2738 final IMarker bookmark = bookmarksFile.createMarker(IMarker.BOOKMARK);
2739 if (bookmark.exists()) {
2740 bookmark.setAttribute(IMarker.MESSAGE, message.toString());
0126a8ca
AM
2741 final Long rank = (Long) tableItem.getData(Key.RANK);
2742 final int location = rank.intValue();
2743 bookmark.setAttribute(IMarker.LOCATION, Integer.valueOf(location));
db4721fb
PT
2744 fBookmarksMap.put(rank, bookmark.getId());
2745 fTable.refresh();
2746 }
2747 } catch (final CoreException e) {
2748 displayException(e);
2749 }
2750 }
2751 }
2752 }
2753
2754 }
2755
2756 /**
2757 * Remove a bookmark from this event table.
2758 *
2759 * @param bookmark
2760 * The bookmark to remove
2761 */
2762 public void removeBookmark(final IMarker bookmark) {
2db176a0 2763 for (final Entry<Long, Long> entry : fBookmarksMap.entries()) {
db4721fb 2764 if (entry.getValue().equals(bookmark.getId())) {
2db176a0 2765 fBookmarksMap.remove(entry.getKey(), entry.getValue());
db4721fb
PT
2766 fTable.refresh();
2767 return;
2768 }
2769 }
2770 }
2771
0126a8ca 2772 private void toggleBookmark(final Long rank) {
db4721fb
PT
2773 if (fBookmarksFile == null) {
2774 return;
2775 }
2776 if (fBookmarksMap.containsKey(rank)) {
2db176a0 2777 final Collection<Long> markerIds = fBookmarksMap.removeAll(rank);
db4721fb
PT
2778 fTable.refresh();
2779 try {
2db176a0
PT
2780 for (long markerId : markerIds) {
2781 final IMarker bookmark = fBookmarksFile.findMarker(markerId);
2782 if (bookmark != null) {
2783 bookmark.delete();
2784 }
db4721fb
PT
2785 }
2786 } catch (final CoreException e) {
2787 displayException(e);
2788 }
2789 } else {
2790 addBookmark(fBookmarksFile);
2791 }
2792 }
2793
2794 /**
2795 * Refresh the bookmarks assigned to this trace, from the contents of a
2796 * bookmark file.
2797 *
2798 * @param bookmarksFile
2799 * The bookmark file to use
2800 */
2801 public void refreshBookmarks(final IFile bookmarksFile) {
2802 fBookmarksFile = bookmarksFile;
2803 if (bookmarksFile == null) {
2804 fBookmarksMap.clear();
2805 fTable.refresh();
2806 return;
2807 }
2808 try {
2809 fBookmarksMap.clear();
2810 for (final IMarker bookmark : bookmarksFile.findMarkers(IMarker.BOOKMARK, false, IResource.DEPTH_ZERO)) {
2811 final int location = bookmark.getAttribute(IMarker.LOCATION, -1);
2812 if (location != -1) {
2813 final long rank = location;
2814 fBookmarksMap.put(rank, bookmark.getId());
2815 }
2816 }
2817 fTable.refresh();
2818 } catch (final CoreException e) {
2819 displayException(e);
2820 }
2821 }
2822
2823 @Override
2824 public void gotoMarker(final IMarker marker) {
2825 final int rank = marker.getAttribute(IMarker.LOCATION, -1);
2826 if (rank != -1) {
2827 int index = rank;
2828 if (fTable.getData(Key.FILTER_OBJ) != null) {
8b7eb3cd
MK
2829 // +1 for top filter status row
2830 index = fCache.getFilteredEventIndex(rank) + 1;
db4721fb
PT
2831 } else if (rank >= fTable.getItemCount()) {
2832 fPendingGotoRank = rank;
2833 }
a56ec2b8 2834 fSelectedRank = rank;
9ba0a108 2835 fSelectedBeginRank = fSelectedRank;
db4721fb 2836 fTable.setSelection(index + 1); // +1 for header row
3f43dc48 2837 updateStatusLine(null);
db4721fb
PT
2838 }
2839 }
2840
2841 // ------------------------------------------------------------------------
2842 // Listeners
2843 // ------------------------------------------------------------------------
2844
db4721fb
PT
2845 @Override
2846 public void colorSettingsChanged(final ColorSetting[] colorSettings) {
2847 fTable.refresh();
2848 }
2849
db4721fb
PT
2850 // ------------------------------------------------------------------------
2851 // Signal handlers
2852 // ------------------------------------------------------------------------
2853
db4721fb
PT
2854 /**
2855 * Handler for the trace updated signal
2856 *
2857 * @param signal
2858 * The incoming signal
2859 */
2860 @TmfSignalHandler
2861 public void traceUpdated(final TmfTraceUpdatedSignal signal) {
2862 if ((signal.getTrace() != fTrace) || fTable.isDisposed()) {
2863 return;
2864 }
2865 // Perform the refresh on the UI thread
2866 Display.getDefault().asyncExec(new Runnable() {
2867 @Override
2868 public void run() {
2869 if (!fTable.isDisposed() && (fTrace != null)) {
2870 if (fTable.getData(Key.FILTER_OBJ) == null) {
8b7eb3cd
MK
2871 /* +1 for header row */
2872 fTable.setItemCount((int) fTrace.getNbEvents() + 1);
2873 /* +1 for header row */
2874 if ((fPendingGotoRank != -1) && ((fPendingGotoRank + 1) < fTable.getItemCount())) {
2875 /* +1 for header row */
2876 fTable.setSelection((int) fPendingGotoRank + 1);
db4721fb 2877 fPendingGotoRank = -1;
3f43dc48 2878 updateStatusLine(null);
db4721fb
PT
2879 }
2880 } else {
2881 startFilterThread();
2882 }
2883 }
2884 if (!fRawViewer.isDisposed() && (fTrace != null)) {
2885 fRawViewer.refreshEventCount();
2886 }
2887 }
2888 });
2889 }
2890
2891 /**
97c71024 2892 * Handler for the selection range signal.
db4721fb
PT
2893 *
2894 * @param signal
2895 * The incoming signal
97c71024 2896 * @since 1.0
db4721fb
PT
2897 */
2898 @TmfSignalHandler
97c71024 2899 public void selectionRangeUpdated(final TmfSelectionRangeUpdatedSignal signal) {
db4721fb
PT
2900 if ((signal.getSource() != this) && (fTrace != null) && (!fTable.isDisposed())) {
2901
8b7eb3cd
MK
2902 /*
2903 * Create a request for one event that will be queued after other
2904 * ongoing requests. When this request is completed do the work to
2905 * select the actual event with the timestamp specified in the
2906 * signal. This procedure prevents the method fTrace.getRank() from
2907 * interfering and delaying ongoing requests.
2908 */
fd3f1eff
AM
2909 final TmfEventRequest subRequest = new TmfEventRequest(ITmfEvent.class,
2910 TmfTimeRange.ETERNITY, 0, 1, ExecutionType.FOREGROUND) {
db4721fb 2911
0fcf3b09 2912 TmfTimestamp ts = new TmfTimestamp(signal.getBeginTime());
db4721fb
PT
2913
2914 @Override
2915 public void handleData(final ITmfEvent event) {
2916 super.handleData(event);
2917 }
2918
2919 @Override
73fce654
AM
2920 public void handleSuccess() {
2921 super.handleSuccess();
db4721fb
PT
2922 if (fTrace == null) {
2923 return;
2924 }
2925
8b7eb3cd
MK
2926 /*
2927 * Verify if the event is within the trace range and adjust
2928 * if necessary
2929 */
db4721fb 2930 ITmfTimestamp timestamp = ts;
065cc19b 2931 if (timestamp.compareTo(fTrace.getStartTime()) == -1) {
db4721fb
PT
2932 timestamp = fTrace.getStartTime();
2933 }
065cc19b 2934 if (timestamp.compareTo(fTrace.getEndTime()) == 1) {
db4721fb
PT
2935 timestamp = fTrace.getEndTime();
2936 }
2937
2938 // Get the rank of the selected event in the table
2939 final ITmfContext context = fTrace.seekEvent(timestamp);
2940 final long rank = context.getRank();
4c9f2944 2941 context.dispose();
db4721fb 2942
78688b59 2943 PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
db4721fb
PT
2944 @Override
2945 public void run() {
2946 // Return if table is disposed
2947 if (fTable.isDisposed()) {
2948 return;
2949 }
2950
9ba0a108
PT
2951 fSelectedRank = rank;
2952 fSelectedBeginRank = fSelectedRank;
db4721fb 2953 int index = (int) rank;
60fb38b8 2954 if (fTable.getData(Key.FILTER_OBJ) != null) {
8b7eb3cd
MK
2955 /* +1 for top filter status row */
2956 index = fCache.getFilteredEventIndex(rank) + 1;
db4721fb 2957 }
8b7eb3cd
MK
2958 /* +1 for header row */
2959 fTable.setSelection(index + 1);
db4721fb 2960 fRawViewer.selectAndReveal(rank);
3f43dc48 2961 updateStatusLine(null);
db4721fb
PT
2962 }
2963 });
2964 }
2965 };
2966
fd3f1eff 2967 ((ITmfEventProvider) fTrace).sendRequest(subRequest);
db4721fb
PT
2968 }
2969 }
2970
2971 // ------------------------------------------------------------------------
2972 // Error handling
2973 // ------------------------------------------------------------------------
2974
2975 /**
2976 * Display an exception in a message box
2977 *
8b7eb3cd
MK
2978 * @param e
2979 * the exception
db4721fb
PT
2980 */
2981 private static void displayException(final Exception e) {
2982 final MessageBox mb = new MessageBox(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
60fb38b8 2983 mb.setText(e.getClass().getSimpleName());
db4721fb
PT
2984 mb.setMessage(e.getMessage());
2985 mb.open();
2986 }
2987
f8177ba2 2988 /**
ae09c4ad 2989 * Refresh the table
f8177ba2
FC
2990 */
2991 public void refresh() {
2992 fCache.clear();
2993 fTable.refresh();
2994 fTable.redraw();
2995 }
03142470
BH
2996
2997 /**
9447c7ee
AM
2998 * Margin column for images and special text (e.g. collapse count)
2999 */
3000 private static final class TmfMarginColumn extends TmfEventTableColumn {
3001
3002 private static final @NonNull ITmfEventAspect MARGIN_ASPECT = new ITmfEventAspect() {
3003
3004 @Override
3005 public String getName() {
3006 return EMPTY_STRING;
3007 }
3008
3009 @Override
3010 public String resolve(ITmfEvent event) {
3011 if (!(event instanceof CachedEvent) || ((CachedEvent) event).repeatCount == 0) {
3012 return EMPTY_STRING;
3013 }
3014 return "+" + ((CachedEvent) event).repeatCount; //$NON-NLS-1$
3015 }
3016
3017 @Override
3018 public String getHelpText() {
3019 return EMPTY_STRING;
3020 }
9447c7ee
AM
3021 };
3022
3023 /**
3024 * Constructor
3025 */
3026 public TmfMarginColumn() {
3027 super(MARGIN_ASPECT);
3028 }
3029 }
03142470 3030
db4721fb 3031}
This page took 0.243574 seconds and 5 git commands to generate.