1 /*******************************************************************************
2 * Copyright (c) 2010, 2011, 2012 Ericsson
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Francois Chouinard - Initial API and implementation
11 * Patrick Tasse - Factored out from events view
12 * Francois Chouinard - Replaced Table by TmfVirtualTable
13 * Patrick Tasse - Filter implementation (inspired by www.eclipse.org/mat)
14 *******************************************************************************/
16 package org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.events
;
18 import java
.util
.ArrayList
;
19 import java
.util
.Arrays
;
20 import java
.util
.HashMap
;
21 import java
.util
.List
;
23 import java
.util
.Map
.Entry
;
24 import java
.util
.regex
.Pattern
;
25 import java
.util
.regex
.PatternSyntaxException
;
27 import org
.eclipse
.core
.resources
.IFile
;
28 import org
.eclipse
.core
.resources
.IMarker
;
29 import org
.eclipse
.core
.resources
.IResource
;
30 import org
.eclipse
.core
.runtime
.CoreException
;
31 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
32 import org
.eclipse
.core
.runtime
.IStatus
;
33 import org
.eclipse
.core
.runtime
.ListenerList
;
34 import org
.eclipse
.core
.runtime
.Status
;
35 import org
.eclipse
.core
.runtime
.jobs
.Job
;
36 import org
.eclipse
.jface
.action
.Action
;
37 import org
.eclipse
.jface
.action
.IAction
;
38 import org
.eclipse
.jface
.action
.IMenuListener
;
39 import org
.eclipse
.jface
.action
.IMenuManager
;
40 import org
.eclipse
.jface
.action
.MenuManager
;
41 import org
.eclipse
.jface
.action
.Separator
;
42 import org
.eclipse
.jface
.dialogs
.InputDialog
;
43 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
44 import org
.eclipse
.jface
.resource
.FontDescriptor
;
45 import org
.eclipse
.jface
.resource
.JFaceResources
;
46 import org
.eclipse
.jface
.resource
.LocalResourceManager
;
47 import org
.eclipse
.jface
.util
.SafeRunnable
;
48 import org
.eclipse
.jface
.viewers
.ISelection
;
49 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
50 import org
.eclipse
.jface
.viewers
.ISelectionProvider
;
51 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
52 import org
.eclipse
.jface
.viewers
.SelectionChangedEvent
;
53 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
54 import org
.eclipse
.jface
.window
.Window
;
55 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Activator
;
56 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Messages
;
57 import org
.eclipse
.linuxtools
.tmf
.core
.component
.ITmfDataProvider
;
58 import org
.eclipse
.linuxtools
.tmf
.core
.component
.TmfComponent
;
59 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
60 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEventField
;
61 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfTimestamp
;
62 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEventField
;
63 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimestamp
;
64 import org
.eclipse
.linuxtools
.tmf
.core
.filter
.ITmfFilter
;
65 import org
.eclipse
.linuxtools
.tmf
.core
.filter
.model
.ITmfFilterTreeNode
;
66 import org
.eclipse
.linuxtools
.tmf
.core
.filter
.model
.TmfFilterAndNode
;
67 import org
.eclipse
.linuxtools
.tmf
.core
.filter
.model
.TmfFilterMatchesNode
;
68 import org
.eclipse
.linuxtools
.tmf
.core
.filter
.model
.TmfFilterNode
;
69 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfDataRequest
.ExecutionType
;
70 import org
.eclipse
.linuxtools
.tmf
.core
.request
.TmfDataRequest
;
71 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfExperimentUpdatedSignal
;
72 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignalHandler
;
73 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTimeSynchSignal
;
74 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceUpdatedSignal
;
75 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
76 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfLocation
;
77 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
78 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.events
.TmfEventsCache
.CachedEvent
;
79 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.colors
.ColorSetting
;
80 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.colors
.ColorSettingsManager
;
81 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.colors
.IColorSettingsListener
;
82 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.filter
.FilterManager
;
83 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.rawviewer
.TmfRawEventViewer
;
84 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.virtualtable
.ColumnData
;
85 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.virtualtable
.TmfVirtualTable
;
86 import org
.eclipse
.swt
.SWT
;
87 import org
.eclipse
.swt
.custom
.SashForm
;
88 import org
.eclipse
.swt
.custom
.TableEditor
;
89 import org
.eclipse
.swt
.events
.FocusAdapter
;
90 import org
.eclipse
.swt
.events
.FocusEvent
;
91 import org
.eclipse
.swt
.events
.KeyAdapter
;
92 import org
.eclipse
.swt
.events
.KeyEvent
;
93 import org
.eclipse
.swt
.events
.MouseAdapter
;
94 import org
.eclipse
.swt
.events
.MouseEvent
;
95 import org
.eclipse
.swt
.events
.SelectionAdapter
;
96 import org
.eclipse
.swt
.events
.SelectionEvent
;
97 import org
.eclipse
.swt
.graphics
.Color
;
98 import org
.eclipse
.swt
.graphics
.Font
;
99 import org
.eclipse
.swt
.graphics
.Image
;
100 import org
.eclipse
.swt
.graphics
.Point
;
101 import org
.eclipse
.swt
.graphics
.Rectangle
;
102 import org
.eclipse
.swt
.layout
.FillLayout
;
103 import org
.eclipse
.swt
.layout
.GridData
;
104 import org
.eclipse
.swt
.layout
.GridLayout
;
105 import org
.eclipse
.swt
.widgets
.Composite
;
106 import org
.eclipse
.swt
.widgets
.Display
;
107 import org
.eclipse
.swt
.widgets
.Event
;
108 import org
.eclipse
.swt
.widgets
.Label
;
109 import org
.eclipse
.swt
.widgets
.Listener
;
110 import org
.eclipse
.swt
.widgets
.Menu
;
111 import org
.eclipse
.swt
.widgets
.MessageBox
;
112 import org
.eclipse
.swt
.widgets
.Shell
;
113 import org
.eclipse
.swt
.widgets
.TableColumn
;
114 import org
.eclipse
.swt
.widgets
.TableItem
;
115 import org
.eclipse
.swt
.widgets
.Text
;
116 import org
.eclipse
.ui
.PlatformUI
;
117 import org
.eclipse
.ui
.ide
.IGotoMarker
;
118 import org
.eclipse
.ui
.themes
.ColorUtil
;
121 * The generic TMF Events table
123 * This is a view that will list events that are read from a trace.
126 * @author Francois Chouinard
127 * @author Patrick Tasse
130 public class TmfEventsTable
extends TmfComponent
implements IGotoMarker
,
131 IColorSettingsListener
, ITmfEventsFilterProvider
, ISelectionProvider
{
133 private static final Image BOOKMARK_IMAGE
= Activator
.getDefault().getImageFromPath(
134 "icons/elcl16/bookmark_obj.gif"); //$NON-NLS-1$
135 private static final Image SEARCH_IMAGE
= Activator
.getDefault().getImageFromPath("icons/elcl16/search.gif"); //$NON-NLS-1$
136 private static final Image SEARCH_MATCH_IMAGE
= Activator
.getDefault().getImageFromPath(
137 "icons/elcl16/search_match.gif"); //$NON-NLS-1$
138 private static final Image SEARCH_MATCH_BOOKMARK_IMAGE
= Activator
.getDefault().getImageFromPath(
139 "icons/elcl16/search_match_bookmark.gif"); //$NON-NLS-1$
140 private static final Image FILTER_IMAGE
= Activator
.getDefault()
141 .getImageFromPath("icons/elcl16/filter_items.gif"); //$NON-NLS-1$
142 private static final Image STOP_IMAGE
= Activator
.getDefault().getImageFromPath("icons/elcl16/stop.gif"); //$NON-NLS-1$
143 private static final String SEARCH_HINT
= Messages
.TmfEventsTable_SearchHint
;
144 private static final String FILTER_HINT
= Messages
.TmfEventsTable_FilterHint
;
145 private static final int MAX_CACHE_SIZE
= 1000;
148 * The events table search/filter keys
151 * @author Patrick Tasse
153 public interface Key
{
155 String SEARCH_TXT
= "$srch_txt"; //$NON-NLS-1$
158 String SEARCH_OBJ
= "$srch_obj"; //$NON-NLS-1$
161 String FILTER_TXT
= "$fltr_txt"; //$NON-NLS-1$
164 String FILTER_OBJ
= "$fltr_obj"; //$NON-NLS-1$
167 String TIMESTAMP
= "$time"; //$NON-NLS-1$
170 String RANK
= "$rank"; //$NON-NLS-1$
173 String FIELD_ID
= "$field_id"; //$NON-NLS-1$
175 /** Bookmark indicator */
176 String BOOKMARK
= "$bookmark"; //$NON-NLS-1$
180 * The events table search/filter state
183 * @author Patrick Tasse
185 public static enum HeaderState
{
186 /** A search is being run */
189 /** A filter is applied */
193 interface Direction
{
198 // ------------------------------------------------------------------------
200 // ------------------------------------------------------------------------
202 protected Composite fComposite
;
203 protected SashForm fSashForm
;
204 protected TmfVirtualTable fTable
;
205 protected TmfRawEventViewer fRawViewer
;
206 protected ITmfTrace fTrace
;
207 protected boolean fPackDone
= false;
208 protected HeaderState fHeaderState
= HeaderState
.SEARCH
;
209 protected long fSelectedRank
= 0;
212 protected long fFilterMatchCount
;
213 protected long fFilterCheckCount
;
214 protected FilterThread fFilterThread
;
215 protected boolean fFilterThreadResume
= false;
216 protected final Object fFilterSyncObj
= new Object();
217 protected SearchThread fSearchThread
;
218 protected final Object fSearchSyncObj
= new Object();
219 protected List
<ITmfEventsFilterListener
> fEventsFilterListeners
= new ArrayList
<ITmfEventsFilterListener
>();
222 * List of selection change listeners (element type: <code>ISelectionChangedListener</code>).
224 * @see #fireSelectionChanged
226 private ListenerList selectionChangedListeners
= new ListenerList();
228 // Bookmark map <Rank, MarkerId>
229 protected Map
<Long
, Long
> fBookmarksMap
= new HashMap
<Long
, Long
>();
230 protected IFile fBookmarksFile
;
231 protected long fPendingGotoRank
= -1;
234 protected LocalResourceManager fResourceManager
= new LocalResourceManager(JFaceResources
.getResources());
235 protected Color fGrayColor
;
236 protected Color fGreenColor
;
237 protected Font fBoldFont
;
239 // Table column names
240 static private final String
[] COLUMN_NAMES
= new String
[] { Messages
.TmfEventsTable_TimestampColumnHeader
,
241 Messages
.TmfEventsTable_SourceColumnHeader
, Messages
.TmfEventsTable_TypeColumnHeader
,
242 Messages
.TmfEventsTable_ReferenceColumnHeader
, Messages
.TmfEventsTable_ContentColumnHeader
};
244 static private final ColumnData
[] COLUMN_DATA
= new ColumnData
[] { new ColumnData(COLUMN_NAMES
[0], 100, SWT
.LEFT
),
245 new ColumnData(COLUMN_NAMES
[1], 100, SWT
.LEFT
), new ColumnData(COLUMN_NAMES
[2], 100, SWT
.LEFT
),
246 new ColumnData(COLUMN_NAMES
[3], 100, SWT
.LEFT
), new ColumnData(COLUMN_NAMES
[4], 100, SWT
.LEFT
) };
249 private final TmfEventsCache fCache
;
250 private boolean fCacheUpdateBusy
= false;
251 private boolean fCacheUpdatePending
= false;
252 private boolean fCacheUpdateCompleted
= false;
253 private final Object fCacheUpdateSyncObj
= new Object();
255 private boolean fDisposeOnClose
;
257 // ------------------------------------------------------------------------
259 // ------------------------------------------------------------------------
262 * Basic constructor, will use default column data.
265 * The parent composite UI object
267 * The size of the event table cache
269 public TmfEventsTable(final Composite parent
, final int cacheSize
) {
270 this(parent
, cacheSize
, COLUMN_DATA
);
274 * Advanced constructor, where we also define which column data to use.
277 * The parent composite UI object
279 * The size of the event table cache
281 * The column data to use for this table
283 public TmfEventsTable(final Composite parent
, int cacheSize
, final ColumnData
[] columnData
) {
284 super("TmfEventsTable"); //$NON-NLS-1$
286 fComposite
= new Composite(parent
, SWT
.NONE
);
287 final GridLayout gl
= new GridLayout(1, false);
290 gl
.verticalSpacing
= 0;
291 fComposite
.setLayout(gl
);
293 fSashForm
= new SashForm(fComposite
, SWT
.HORIZONTAL
);
294 fSashForm
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
296 // Create a virtual table
297 final int style
= SWT
.H_SCROLL
| SWT
.V_SCROLL
| SWT
.SINGLE
| SWT
.FULL_SELECTION
;
298 fTable
= new TmfVirtualTable(fSashForm
, style
);
300 // Set the table layout
301 final GridData layoutData
= new GridData(SWT
.FILL
, SWT
.FILL
, true, true);
302 fTable
.setLayoutData(layoutData
);
304 // Some cosmetic enhancements
305 fTable
.setHeaderVisible(true);
306 fTable
.setLinesVisible(true);
309 setColumnHeaders(columnData
);
311 // Set the default column field ids if this is not a subclass
312 if (Arrays
.equals(columnData
, COLUMN_DATA
)) {
313 fTable
.getColumns()[0].setData(Key
.FIELD_ID
, ITmfEvent
.EVENT_FIELD_TIMESTAMP
);
314 fTable
.getColumns()[1].setData(Key
.FIELD_ID
, ITmfEvent
.EVENT_FIELD_SOURCE
);
315 fTable
.getColumns()[2].setData(Key
.FIELD_ID
, ITmfEvent
.EVENT_FIELD_TYPE
);
316 fTable
.getColumns()[3].setData(Key
.FIELD_ID
, ITmfEvent
.EVENT_FIELD_REFERENCE
);
317 fTable
.getColumns()[4].setData(Key
.FIELD_ID
, ITmfEvent
.EVENT_FIELD_CONTENT
);
320 // Set the frozen row for header row
321 fTable
.setFrozenRowCount(1);
323 // Create the header row cell editor
324 createHeaderEditor();
326 // Handle the table item selection
327 fTable
.addSelectionListener(new SelectionAdapter() {
329 public void widgetSelected(final SelectionEvent e
) {
330 final TableItem
[] selection
= fTable
.getSelection();
331 if (selection
.length
> 0) {
332 final TableItem selectedTableItem
= selection
[0];
333 if (selectedTableItem
!= null) {
334 if (selectedTableItem
.getData(Key
.RANK
) instanceof Long
) {
335 fSelectedRank
= (Long
) selectedTableItem
.getData(Key
.RANK
);
336 fRawViewer
.selectAndReveal((Long
) selectedTableItem
.getData(Key
.RANK
));
338 if (selectedTableItem
.getData(Key
.TIMESTAMP
) instanceof TmfTimestamp
) {
339 final TmfTimestamp ts
= (TmfTimestamp
) selectedTableItem
.getData(Key
.TIMESTAMP
);
340 broadcast(new TmfTimeSynchSignal(TmfEventsTable
.this, ts
));
344 fireSelectionChanged(new SelectionChangedEvent(TmfEventsTable
.this, getSelection()));
348 cacheSize
= Math
.max(cacheSize
, Display
.getDefault().getBounds().height
/ fTable
.getItemHeight());
349 cacheSize
= Math
.min(cacheSize
, MAX_CACHE_SIZE
);
350 fCache
= new TmfEventsCache(cacheSize
, this);
352 // Handle the table item requests
353 fTable
.addListener(SWT
.SetData
, new Listener() {
356 public void handleEvent(final Event event
) {
358 final TableItem item
= (TableItem
) event
.item
;
359 int index
= event
.index
- 1; // -1 for the header row
361 if (event
.index
== 0) {
362 setHeaderRowItemData(item
);
366 if (fTable
.getData(Key
.FILTER_OBJ
) != null) {
367 if ((event
.index
== 1) || (event
.index
== (fTable
.getItemCount() - 1))) {
368 setFilterStatusRowItemData(item
);
371 index
= index
- 1; // -1 for top filter status row
374 final CachedEvent cachedEvent
= fCache
.getEvent(index
);
375 if (cachedEvent
!= null) {
376 setItemData(item
, cachedEvent
.event
, cachedEvent
.rank
);
380 // Else, fill the cache asynchronously (and off the UI thread)
385 fTable
.addMouseListener(new MouseAdapter() {
387 public void mouseDoubleClick(final MouseEvent event
) {
388 if (event
.button
!= 1) {
391 // Identify the selected row
392 final Point point
= new Point(event
.x
, event
.y
);
393 final TableItem item
= fTable
.getItem(point
);
395 final Rectangle imageBounds
= item
.getImageBounds(0);
396 imageBounds
.width
= BOOKMARK_IMAGE
.getBounds().width
;
397 if (imageBounds
.contains(point
)) {
398 final Long rank
= (Long
) item
.getData(Key
.RANK
);
400 toggleBookmark(rank
);
407 final Listener tooltipListener
= new Listener () {
408 Shell tooltipShell
= null;
410 public void handleEvent(final Event event
) {
411 switch (event
.type
) {
413 final TableItem item
= fTable
.getItem(new Point(event
.x
, event
.y
));
417 final Long rank
= (Long
) item
.getData(Key
.RANK
);
421 final String tooltipText
= (String
) item
.getData(Key
.BOOKMARK
);
422 final Rectangle bounds
= item
.getImageBounds(0);
423 bounds
.width
= BOOKMARK_IMAGE
.getBounds().width
;
424 if (!bounds
.contains(event
.x
,event
.y
)) {
427 if ((tooltipShell
!= null) && !tooltipShell
.isDisposed()) {
428 tooltipShell
.dispose();
430 tooltipShell
= new Shell(fTable
.getShell(), SWT
.ON_TOP
| SWT
.NO_FOCUS
| SWT
.TOOL
);
431 tooltipShell
.setBackground(PlatformUI
.getWorkbench().getDisplay().getSystemColor(SWT
.COLOR_INFO_BACKGROUND
));
432 final FillLayout layout
= new FillLayout();
433 layout
.marginWidth
= 2;
434 tooltipShell
.setLayout(layout
);
435 final Label label
= new Label(tooltipShell
, SWT
.WRAP
);
436 String text
= rank
.toString() + (tooltipText
!= null ?
": " + tooltipText
: ""); //$NON-NLS-1$ //$NON-NLS-2$
437 label
.setForeground(PlatformUI
.getWorkbench().getDisplay().getSystemColor(SWT
.COLOR_INFO_FOREGROUND
));
438 label
.setBackground(PlatformUI
.getWorkbench().getDisplay().getSystemColor(SWT
.COLOR_INFO_BACKGROUND
));
440 label
.addListener(SWT
.MouseExit
, this);
441 label
.addListener(SWT
.MouseDown
, this);
442 label
.addListener(SWT
.MouseWheel
, this);
443 final Point size
= tooltipShell
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
);
445 * Bug in Linux. The coordinates of the event have an origin that excludes the table header but
446 * the method toDisplay() expects coordinates relative to an origin that includes the table header.
449 if (System
.getProperty("os.name").contains("Linux")) { //$NON-NLS-1$ //$NON-NLS-2$
450 y
+= fTable
.getHeaderHeight();
452 Point pt
= fTable
.toDisplay(event
.x
, y
);
453 pt
.x
+= BOOKMARK_IMAGE
.getBounds().width
;
455 tooltipShell
.setBounds(pt
.x
, pt
.y
, size
.x
, size
.y
);
456 tooltipShell
.setVisible(true);
464 if (tooltipShell
!= null) {
465 tooltipShell
.dispose();
475 fTable
.addListener(SWT
.MouseHover
, tooltipListener
);
476 fTable
.addListener(SWT
.Dispose
, tooltipListener
);
477 fTable
.addListener(SWT
.KeyDown
, tooltipListener
);
478 fTable
.addListener(SWT
.MouseMove
, tooltipListener
);
479 fTable
.addListener(SWT
.MouseExit
, tooltipListener
);
480 fTable
.addListener(SWT
.MouseDown
, tooltipListener
);
481 fTable
.addListener(SWT
.MouseWheel
, tooltipListener
);
486 ColorSettingsManager
.addColorSettingsListener(this);
488 fTable
.setItemCount(1); // +1 for header row
490 fRawViewer
= new TmfRawEventViewer(fSashForm
, SWT
.H_SCROLL
| SWT
.V_SCROLL
);
492 fRawViewer
.addSelectionListener(new Listener() {
494 public void handleEvent(final Event e
) {
495 if (e
.data
instanceof Long
) {
496 final long rank
= (Long
) e
.data
;
497 int index
= (int) rank
;
498 if (fTable
.getData(Key
.FILTER_OBJ
) != null) {
499 index
= fCache
.getFilteredEventIndex(rank
) + 1; // +1 for top filter status row
501 fTable
.setSelection(index
+ 1); // +1 for header row
502 fSelectedRank
= rank
;
503 } else if (e
.data
instanceof ITmfLocation
) {
504 // DOES NOT WORK: rank undefined in context from seekLocation()
505 // ITmfLocation<?> location = (ITmfLocation<?>) e.data;
506 // TmfContext context = fTrace.seekLocation(location);
507 // fTable.setSelection((int) context.getRank());
512 final TableItem
[] selection
= fTable
.getSelection();
513 if ((selection
!= null) && (selection
.length
> 0)) {
514 final TmfTimestamp ts
= (TmfTimestamp
) fTable
.getSelection()[0].getData(Key
.TIMESTAMP
);
516 broadcast(new TmfTimeSynchSignal(TmfEventsTable
.this, ts
));
522 fSashForm
.setWeights(new int[] { 1, 1 });
523 fRawViewer
.setVisible(false);
528 protected void createPopupMenu() {
529 final IAction showTableAction
= new Action(Messages
.TmfEventsTable_ShowTableActionText
) {
532 fTable
.setVisible(true);
537 final IAction hideTableAction
= new Action(Messages
.TmfEventsTable_HideTableActionText
) {
540 fTable
.setVisible(false);
545 final IAction showRawAction
= new Action(Messages
.TmfEventsTable_ShowRawActionText
) {
548 fRawViewer
.setVisible(true);
550 final int index
= fTable
.getSelectionIndex();
552 fRawViewer
.selectAndReveal(index
- 1);
557 final IAction hideRawAction
= new Action(Messages
.TmfEventsTable_HideRawActionText
) {
560 fRawViewer
.setVisible(false);
565 final IAction showSearchBarAction
= new Action(Messages
.TmfEventsTable_ShowSearchBarActionText
) {
568 fHeaderState
= HeaderState
.SEARCH
;
573 final IAction showFilterBarAction
= new Action(Messages
.TmfEventsTable_ShowFilterBarActionText
) {
576 fHeaderState
= HeaderState
.FILTER
;
581 final IAction clearFiltersAction
= new Action(Messages
.TmfEventsTable_ClearFiltersActionText
) {
588 class ToggleBookmarkAction
extends Action
{
591 public ToggleBookmarkAction(final String text
, final long rank
) {
598 toggleBookmark(fRank
);
602 final MenuManager tablePopupMenu
= new MenuManager();
603 tablePopupMenu
.setRemoveAllWhenShown(true);
604 tablePopupMenu
.addMenuListener(new IMenuListener() {
606 public void menuAboutToShow(final IMenuManager manager
) {
607 if (fTable
.getSelectionIndex() == 0) {
608 // Right-click on header row
609 if (fHeaderState
== HeaderState
.FILTER
) {
610 tablePopupMenu
.add(showSearchBarAction
);
612 tablePopupMenu
.add(showFilterBarAction
);
616 final Point point
= fTable
.toControl(Display
.getDefault().getCursorLocation());
617 final TableItem item
= fTable
.getSelection().length
> 0 ? fTable
.getSelection()[0] : null;
619 final Rectangle imageBounds
= item
.getImageBounds(0);
620 imageBounds
.width
= BOOKMARK_IMAGE
.getBounds().width
;
621 if (point
.x
<= (imageBounds
.x
+ imageBounds
.width
)) {
622 // Right-click on left margin
623 final Long rank
= (Long
) item
.getData(Key
.RANK
);
624 if ((rank
!= null) && (fBookmarksFile
!= null)) {
625 if (fBookmarksMap
.containsKey(rank
)) {
626 tablePopupMenu
.add(new ToggleBookmarkAction(
627 Messages
.TmfEventsTable_RemoveBookmarkActionText
, rank
));
629 tablePopupMenu
.add(new ToggleBookmarkAction(
630 Messages
.TmfEventsTable_AddBookmarkActionText
, rank
));
636 // Right-click on table
637 if (fTable
.isVisible() && fRawViewer
.isVisible()) {
638 tablePopupMenu
.add(hideTableAction
);
639 tablePopupMenu
.add(hideRawAction
);
640 } else if (!fTable
.isVisible()) {
641 tablePopupMenu
.add(showTableAction
);
642 } else if (!fRawViewer
.isVisible()) {
643 tablePopupMenu
.add(showRawAction
);
645 tablePopupMenu
.add(new Separator());
646 tablePopupMenu
.add(clearFiltersAction
);
647 final ITmfFilterTreeNode
[] savedFilters
= FilterManager
.getSavedFilters();
648 if (savedFilters
.length
> 0) {
649 final MenuManager subMenu
= new MenuManager(Messages
.TmfEventsTable_ApplyPresetFilterMenuName
);
650 for (final ITmfFilterTreeNode node
: savedFilters
) {
651 if (node
instanceof TmfFilterNode
) {
652 final TmfFilterNode filter
= (TmfFilterNode
) node
;
653 subMenu
.add(new Action(filter
.getFilterName()) {
661 tablePopupMenu
.add(subMenu
);
663 appendToTablePopupMenu(tablePopupMenu
, item
);
667 final MenuManager rawViewerPopupMenu
= new MenuManager();
668 rawViewerPopupMenu
.setRemoveAllWhenShown(true);
669 rawViewerPopupMenu
.addMenuListener(new IMenuListener() {
671 public void menuAboutToShow(final IMenuManager manager
) {
672 if (fTable
.isVisible() && fRawViewer
.isVisible()) {
673 rawViewerPopupMenu
.add(hideTableAction
);
674 rawViewerPopupMenu
.add(hideRawAction
);
675 } else if (!fTable
.isVisible()) {
676 rawViewerPopupMenu
.add(showTableAction
);
677 } else if (!fRawViewer
.isVisible()) {
678 rawViewerPopupMenu
.add(showRawAction
);
680 appendToRawPopupMenu(tablePopupMenu
);
684 Menu menu
= tablePopupMenu
.createContextMenu(fTable
);
685 fTable
.setMenu(menu
);
687 menu
= rawViewerPopupMenu
.createContextMenu(fRawViewer
);
688 fRawViewer
.setMenu(menu
);
691 protected void appendToTablePopupMenu(final MenuManager tablePopupMenu
, final TableItem selectedItem
) {
692 // override to append more actions
695 protected void appendToRawPopupMenu(final MenuManager rawViewerPopupMenu
) {
696 // override to append more actions
700 public void dispose() {
703 ColorSettingsManager
.removeColorSettingsListener(this);
704 fComposite
.dispose();
705 if ((fTrace
!= null) && fDisposeOnClose
) {
708 fResourceManager
.dispose();
713 * Assign a layout data object to this view.
716 * The layout data to assign
718 public void setLayoutData(final Object layoutData
) {
719 fComposite
.setLayoutData(layoutData
);
723 * Get the virtual table contained in this event table.
725 * @return The TMF virtual table
727 public TmfVirtualTable
getTable() {
734 * FIXME: Add support for column selection
736 protected void setColumnHeaders(final ColumnData
[] columnData
) {
737 fTable
.setColumnHeaders(columnData
);
740 protected void setItemData(final TableItem item
, final ITmfEvent event
, final long rank
) {
741 final ITmfEventField
[] fields
= extractItemFields(event
);
742 final String
[] content
= new String
[fields
.length
];
743 for (int i
= 0; i
< fields
.length
; i
++) {
744 content
[i
] = fields
[i
].getValue() != null ? fields
[i
].getValue().toString() : ""; //$NON-NLS-1$
746 item
.setText(content
);
748 item
.setData(Key
.TIMESTAMP
, new TmfTimestamp(event
.getTimestamp()));
749 item
.setData(Key
.RANK
, rank
);
751 boolean bookmark
= false;
752 final Long markerId
= fBookmarksMap
.get(rank
);
753 if (markerId
!= null) {
756 final IMarker marker
= fBookmarksFile
.findMarker(markerId
);
757 item
.setData(Key
.BOOKMARK
, marker
.getAttribute(IMarker
.MESSAGE
));
758 } catch (final CoreException e
) {
762 item
.setData(Key
.BOOKMARK
, null);
765 boolean searchMatch
= false;
766 boolean searchNoMatch
= false;
767 final ITmfFilter searchFilter
= (ITmfFilter
) fTable
.getData(Key
.SEARCH_OBJ
);
768 if (searchFilter
!= null) {
769 if (searchFilter
.matches(event
)) {
772 searchNoMatch
= true;
776 final ColorSetting colorSetting
= ColorSettingsManager
.getColorSetting(event
);
778 item
.setForeground(colorSetting
.getDimmedForegroundColor());
779 item
.setBackground(colorSetting
.getDimmedBackgroundColor());
781 item
.setForeground(colorSetting
.getForegroundColor());
782 item
.setBackground(colorSetting
.getBackgroundColor());
787 item
.setImage(SEARCH_MATCH_BOOKMARK_IMAGE
);
789 item
.setImage(SEARCH_MATCH_IMAGE
);
791 } else if (bookmark
) {
792 item
.setImage(BOOKMARK_IMAGE
);
794 item
.setImage((Image
) null);
798 protected void setHeaderRowItemData(final TableItem item
) {
799 String txtKey
= null;
800 if (fHeaderState
== HeaderState
.SEARCH
) {
801 item
.setImage(SEARCH_IMAGE
);
802 txtKey
= Key
.SEARCH_TXT
;
803 } else if (fHeaderState
== HeaderState
.FILTER
) {
804 item
.setImage(FILTER_IMAGE
);
805 txtKey
= Key
.FILTER_TXT
;
807 item
.setForeground(fGrayColor
);
808 for (int i
= 0; i
< fTable
.getColumns().length
; i
++) {
809 final TableColumn column
= fTable
.getColumns()[i
];
810 final String filter
= (String
) column
.getData(txtKey
);
811 if (filter
== null) {
812 if (fHeaderState
== HeaderState
.SEARCH
) {
813 item
.setText(i
, SEARCH_HINT
);
814 } else if (fHeaderState
== HeaderState
.FILTER
) {
815 item
.setText(i
, FILTER_HINT
);
817 item
.setForeground(i
, fGrayColor
);
818 item
.setFont(i
, fTable
.getFont());
820 item
.setText(i
, filter
);
821 item
.setForeground(i
, fGreenColor
);
822 item
.setFont(i
, fBoldFont
);
827 protected void setFilterStatusRowItemData(final TableItem item
) {
828 for (int i
= 0; i
< fTable
.getColumns().length
; i
++) {
830 if ((fTrace
== null) || (fFilterCheckCount
== fTrace
.getNbEvents())) {
831 item
.setImage(FILTER_IMAGE
);
833 item
.setImage(STOP_IMAGE
);
835 item
.setText(0, fFilterMatchCount
+ "/" + fFilterCheckCount
); //$NON-NLS-1$
837 item
.setText(i
, ""); //$NON-NLS-1$
841 item
.setData(Key
.TIMESTAMP
, null);
842 item
.setData(Key
.RANK
, null);
843 item
.setForeground(null);
844 item
.setBackground(null);
847 protected void createHeaderEditor() {
848 final TableEditor tableEditor
= fTable
.createTableEditor();
849 tableEditor
.horizontalAlignment
= SWT
.LEFT
;
850 tableEditor
.verticalAlignment
= SWT
.CENTER
;
851 tableEditor
.grabHorizontal
= true;
852 tableEditor
.minimumWidth
= 50;
854 // Handle the header row selection
855 fTable
.addMouseListener(new MouseAdapter() {
861 public void mouseDown(final MouseEvent event
) {
862 if (event
.button
!= 1) {
865 // Identify the selected row
866 final Point point
= new Point(event
.x
, event
.y
);
867 item
= fTable
.getItem(point
);
869 // Header row selected
870 if ((item
!= null) && (fTable
.indexOf(item
) == 0)) {
873 if (item
.getImageBounds(0).contains(point
)) {
874 if (fHeaderState
== HeaderState
.SEARCH
) {
875 fHeaderState
= HeaderState
.FILTER
;
876 } else if (fHeaderState
== HeaderState
.FILTER
) {
877 fHeaderState
= HeaderState
.SEARCH
;
883 // Identify the selected column
885 for (int i
= 0; i
< fTable
.getColumns().length
; i
++) {
886 final Rectangle rect
= item
.getBounds(i
);
887 if (rect
.contains(point
)) {
893 if (columnIndex
== -1) {
897 column
= fTable
.getColumns()[columnIndex
];
899 String txtKey
= null;
900 if (fHeaderState
== HeaderState
.SEARCH
) {
901 txtKey
= Key
.SEARCH_TXT
;
902 } else if (fHeaderState
== HeaderState
.FILTER
) {
903 txtKey
= Key
.FILTER_TXT
;
906 // The control that will be the editor must be a child of the Table
907 final Text newEditor
= (Text
) fTable
.createTableEditorControl(Text
.class);
908 final String headerString
= (String
) column
.getData(txtKey
);
909 if (headerString
!= null) {
910 newEditor
.setText(headerString
);
912 newEditor
.addFocusListener(new FocusAdapter() {
914 public void focusLost(final FocusEvent e
) {
915 final boolean changed
= updateHeader(newEditor
.getText());
921 newEditor
.addKeyListener(new KeyAdapter() {
923 public void keyPressed(final KeyEvent e
) {
924 if (e
.character
== SWT
.CR
) {
925 updateHeader(newEditor
.getText());
927 } else if (e
.character
== SWT
.ESC
) {
928 tableEditor
.getEditor().dispose();
932 newEditor
.selectAll();
933 newEditor
.setFocus();
934 tableEditor
.setEditor(newEditor
, item
, columnIndex
);
939 * returns true is value was changed
941 private boolean updateHeader(final String text
) {
942 String objKey
= null;
943 String txtKey
= null;
944 if (fHeaderState
== HeaderState
.SEARCH
) {
945 objKey
= Key
.SEARCH_OBJ
;
946 txtKey
= Key
.SEARCH_TXT
;
947 } else if (fHeaderState
== HeaderState
.FILTER
) {
948 objKey
= Key
.FILTER_OBJ
;
949 txtKey
= Key
.FILTER_TXT
;
951 if (text
.trim().length() > 0) {
953 final String regex
= TmfFilterMatchesNode
.regexFix(text
);
954 Pattern
.compile(regex
);
955 if (regex
.equals(column
.getData(txtKey
))) {
956 tableEditor
.getEditor().dispose();
959 final TmfFilterMatchesNode filter
= new TmfFilterMatchesNode(null);
960 String fieldId
= (String
) column
.getData(Key
.FIELD_ID
);
961 if (fieldId
== null) {
962 fieldId
= column
.getText();
964 filter
.setField(fieldId
);
965 filter
.setRegex(regex
);
966 column
.setData(objKey
, filter
);
967 column
.setData(txtKey
, regex
);
968 } catch (final PatternSyntaxException ex
) {
969 tableEditor
.getEditor().dispose();
970 MessageDialog
.openError(PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell(),
971 ex
.getDescription(), ex
.getMessage());
975 if (column
.getData(txtKey
) == null) {
976 tableEditor
.getEditor().dispose();
979 column
.setData(objKey
, null);
980 column
.setData(txtKey
, null);
985 private void applyHeader() {
986 if (fHeaderState
== HeaderState
.SEARCH
) {
988 final TmfFilterAndNode filter
= new TmfFilterAndNode(null);
989 for (final TableColumn column
: fTable
.getColumns()) {
990 final Object filterObj
= column
.getData(Key
.SEARCH_OBJ
);
991 if (filterObj
instanceof ITmfFilterTreeNode
) {
992 filter
.addChild((ITmfFilterTreeNode
) filterObj
);
995 if (filter
.getChildrenCount() > 0) {
996 fTable
.setData(Key
.SEARCH_OBJ
, filter
);
999 fireSearchApplied(filter
);
1001 fTable
.setData(Key
.SEARCH_OBJ
, null);
1003 fireSearchApplied(null);
1005 } else if (fHeaderState
== HeaderState
.FILTER
) {
1006 final TmfFilterAndNode filter
= new TmfFilterAndNode(null);
1007 for (final TableColumn column
: fTable
.getColumns()) {
1008 final Object filterObj
= column
.getData(Key
.FILTER_OBJ
);
1009 if (filterObj
instanceof ITmfFilterTreeNode
) {
1010 filter
.addChild((ITmfFilterTreeNode
) filterObj
);
1013 if (filter
.getChildrenCount() > 0) {
1014 applyFilter(filter
);
1020 tableEditor
.getEditor().dispose();
1024 fTable
.addKeyListener(new KeyAdapter() {
1026 public void keyPressed(final KeyEvent e
) {
1028 if (e
.character
== SWT
.ESC
) {
1032 } else if (e
.character
== SWT
.DEL
) {
1033 if (fHeaderState
== HeaderState
.SEARCH
) {
1035 for (final TableColumn column
: fTable
.getColumns()) {
1036 column
.setData(Key
.SEARCH_OBJ
, null);
1037 column
.setData(Key
.SEARCH_TXT
, null);
1039 fTable
.setData(Key
.SEARCH_OBJ
, null);
1041 fireSearchApplied(null);
1042 } else if (fHeaderState
== HeaderState
.FILTER
) {
1045 } else if (e
.character
== SWT
.CR
) {
1046 if ((e
.stateMask
& SWT
.SHIFT
) == 0) {
1056 protected void fireFilterApplied(final ITmfFilter filter
) {
1057 for (final ITmfEventsFilterListener listener
: fEventsFilterListeners
) {
1058 listener
.filterApplied(filter
, fTrace
);
1062 protected void fireSearchApplied(final ITmfFilter filter
) {
1063 for (final ITmfEventsFilterListener listener
: fEventsFilterListeners
) {
1064 listener
.searchApplied(filter
, fTrace
);
1068 protected void startFilterThread() {
1069 synchronized (fFilterSyncObj
) {
1070 final ITmfFilterTreeNode filter
= (ITmfFilterTreeNode
) fTable
.getData(Key
.FILTER_OBJ
);
1071 if (fFilterThread
== null || fFilterThread
.filter
!= filter
) {
1072 if (fFilterThread
!= null) {
1073 fFilterThread
.cancel();
1074 fFilterThreadResume
= false;
1076 fFilterThread
= new FilterThread(filter
);
1077 fFilterThread
.start();
1079 fFilterThreadResume
= true;
1084 protected void stopFilterThread() {
1085 synchronized (fFilterSyncObj
) {
1086 if (fFilterThread
!= null) {
1087 fFilterThread
.cancel();
1088 fFilterThread
= null;
1089 fFilterThreadResume
= false;
1097 protected void applyFilter(ITmfFilter filter
) {
1100 fFilterMatchCount
= 0;
1101 fFilterCheckCount
= 0;
1102 fCache
.applyFilter(filter
);
1104 fTable
.setData(Key
.FILTER_OBJ
, filter
);
1105 fTable
.setItemCount(3); // +1 for header row, +2 for top and bottom filter status rows
1106 startFilterThread();
1107 fireFilterApplied(filter
);
1110 protected void clearFilters() {
1111 if (fTable
.getData(Key
.FILTER_OBJ
) == null) {
1116 fCache
.clearFilter();
1118 for (final TableColumn column
: fTable
.getColumns()) {
1119 column
.setData(Key
.FILTER_OBJ
, null);
1120 column
.setData(Key
.FILTER_TXT
, null);
1122 fTable
.setData(Key
.FILTER_OBJ
, null);
1123 if (fTrace
!= null) {
1124 fTable
.setItemCount((int) fTrace
.getNbEvents() + 1); // +1 for header row
1126 fTable
.setItemCount(1); // +1 for header row
1128 fFilterMatchCount
= 0;
1129 fFilterCheckCount
= 0;
1130 if (fSelectedRank
>= 0) {
1131 fTable
.setSelection((int) fSelectedRank
+ 1); // +1 for header row
1133 fTable
.setSelection(0);
1135 fireFilterApplied(null);
1138 protected class FilterThread
extends Thread
{
1139 private final ITmfFilterTreeNode filter
;
1140 private TmfDataRequest request
;
1141 private boolean refreshBusy
= false;
1142 private boolean refreshPending
= false;
1143 private final Object syncObj
= new Object();
1145 public FilterThread(final ITmfFilterTreeNode filter
) {
1146 super("Filter Thread"); //$NON-NLS-1$
1147 this.filter
= filter
;
1152 if (fTrace
== null) {
1155 final int nbRequested
= (int) (fTrace
.getNbEvents() - fFilterCheckCount
);
1156 if (nbRequested
<= 0) {
1159 request
= new TmfDataRequest(ITmfEvent
.class, (int) fFilterCheckCount
,
1160 nbRequested
, fTrace
.getCacheSize(), ExecutionType
.BACKGROUND
) {
1162 public void handleData(final ITmfEvent event
) {
1163 super.handleData(event
);
1164 if (request
.isCancelled()) {
1167 if (filter
.matches(event
)) {
1168 final long rank
= fFilterCheckCount
;
1169 final int index
= (int) fFilterMatchCount
;
1170 fFilterMatchCount
++;
1171 fCache
.storeEvent(event
.clone(), rank
, index
);
1173 } else if ((fFilterCheckCount
% 100) == 0) {
1176 fFilterCheckCount
++;
1179 ((ITmfDataProvider
) fTrace
).sendRequest(request
);
1181 request
.waitForCompletion();
1182 } catch (final InterruptedException e
) {
1185 synchronized (fFilterSyncObj
) {
1186 fFilterThread
= null;
1187 if (fFilterThreadResume
) {
1188 fFilterThreadResume
= false;
1189 fFilterThread
= new FilterThread(filter
);
1190 fFilterThread
.start();
1195 public void refreshTable() {
1196 synchronized (syncObj
) {
1198 refreshPending
= true;
1203 Display
.getDefault().asyncExec(new Runnable() {
1206 if (request
.isCancelled()) {
1209 if (fTable
.isDisposed()) {
1212 fTable
.setItemCount((int) fFilterMatchCount
+ 3); // +1 for header row, +2 for top and bottom filter status rows
1214 synchronized (syncObj
) {
1215 refreshBusy
= false;
1216 if (refreshPending
) {
1217 refreshPending
= false;
1225 public void cancel() {
1226 if (request
!= null) {
1232 protected void searchNext() {
1233 synchronized (fSearchSyncObj
) {
1234 if (fSearchThread
!= null) {
1237 final ITmfFilterTreeNode searchFilter
= (ITmfFilterTreeNode
) fTable
.getData(Key
.SEARCH_OBJ
);
1238 if (searchFilter
== null) {
1241 final int selectionIndex
= fTable
.getSelectionIndex();
1243 if (selectionIndex
> 0) {
1244 startIndex
= selectionIndex
; // -1 for header row, +1 for next event
1246 // header row is selected, start at top event
1247 startIndex
= Math
.max(0, fTable
.getTopIndex() - 1); // -1 for header row
1249 final ITmfFilterTreeNode eventFilter
= (ITmfFilterTreeNode
) fTable
.getData(Key
.FILTER_OBJ
);
1250 if (eventFilter
!= null)
1252 startIndex
= Math
.max(0, startIndex
- 1); // -1 for top filter status row
1254 fSearchThread
= new SearchThread(searchFilter
, eventFilter
, startIndex
, fSelectedRank
, Direction
.FORWARD
);
1255 fSearchThread
.schedule();
1259 protected void searchPrevious() {
1260 synchronized (fSearchSyncObj
) {
1261 if (fSearchThread
!= null) {
1264 final ITmfFilterTreeNode searchFilter
= (ITmfFilterTreeNode
) fTable
.getData(Key
.SEARCH_OBJ
);
1265 if (searchFilter
== null) {
1268 final int selectionIndex
= fTable
.getSelectionIndex();
1270 if (selectionIndex
> 0) {
1271 startIndex
= selectionIndex
- 2; // -1 for header row, -1 for previous event
1273 // header row is selected, start at precedent of top event
1274 startIndex
= fTable
.getTopIndex() - 2; // -1 for header row, -1 for previous event
1276 final ITmfFilterTreeNode eventFilter
= (ITmfFilterTreeNode
) fTable
.getData(Key
.FILTER_OBJ
);
1277 if (eventFilter
!= null)
1279 startIndex
= startIndex
- 1; // -1 for top filter status row
1281 fSearchThread
= new SearchThread(searchFilter
, eventFilter
, startIndex
, fSelectedRank
, Direction
.BACKWARD
);
1282 fSearchThread
.schedule();
1286 protected void stopSearchThread() {
1287 fPendingGotoRank
= -1;
1288 synchronized (fSearchSyncObj
) {
1289 if (fSearchThread
!= null) {
1290 fSearchThread
.cancel();
1291 fSearchThread
= null;
1296 protected class SearchThread
extends Job
{
1297 protected ITmfFilterTreeNode searchFilter
;
1298 protected ITmfFilterTreeNode eventFilter
;
1299 protected int startIndex
;
1300 protected int direction
;
1301 protected long rank
;
1302 protected long foundRank
= -1;
1303 protected TmfDataRequest request
;
1304 private ITmfTimestamp foundTimestamp
= null;
1306 public SearchThread(final ITmfFilterTreeNode searchFilter
, final ITmfFilterTreeNode eventFilter
, final int startIndex
,
1307 final long currentRank
, final int direction
) {
1308 super(Messages
.TmfEventsTable_SearchingJobName
);
1309 this.searchFilter
= searchFilter
;
1310 this.eventFilter
= eventFilter
;
1311 this.startIndex
= startIndex
;
1312 this.rank
= currentRank
;
1313 this.direction
= direction
;
1317 protected IStatus
run(final IProgressMonitor monitor
) {
1318 if (fTrace
== null) {
1319 return Status
.OK_STATUS
;
1321 final Display display
= Display
.getDefault();
1322 if (startIndex
< 0) {
1323 rank
= (int) fTrace
.getNbEvents() - 1;
1324 } else if (startIndex
>= (fTable
.getItemCount() - (eventFilter
== null ?
1 : 3))) { // -1 for header row, -2 for top and bottom filter status rows
1327 int idx
= startIndex
;
1328 while (foundRank
== -1) {
1329 final CachedEvent event
= fCache
.peekEvent(idx
);
1330 if (event
== null) {
1334 if (searchFilter
.matches(event
.event
) && ((eventFilter
== null) || eventFilter
.matches(event
.event
))) {
1335 foundRank
= event
.rank
;
1336 foundTimestamp
= event
.event
.getTimestamp();
1339 if (direction
== Direction
.FORWARD
) {
1345 if (foundRank
== -1) {
1346 if (direction
== Direction
.FORWARD
) {
1348 if (rank
> (fTrace
.getNbEvents() - 1)) {
1354 rank
= (int) fTrace
.getNbEvents() - 1;
1359 final int startRank
= (int) rank
;
1360 boolean wrapped
= false;
1361 while (!monitor
.isCanceled() && (foundRank
== -1) && (fTrace
!= null)) {
1362 int nbRequested
= (direction
== Direction
.FORWARD ? Integer
.MAX_VALUE
: Math
.min((int) rank
+ 1, fTrace
.getCacheSize()));
1363 if (direction
== Direction
.BACKWARD
) {
1364 rank
= Math
.max(0, rank
- fTrace
.getCacheSize() + 1);
1366 request
= new TmfDataRequest(ITmfEvent
.class, (int) rank
, nbRequested
, fTrace
.getCacheSize(), ExecutionType
.BACKGROUND
) {
1367 long currentRank
= rank
;
1370 public void handleData(final ITmfEvent event
) {
1371 super.handleData(event
);
1372 if (searchFilter
.matches(event
) && ((eventFilter
== null) || eventFilter
.matches(event
))) {
1373 foundRank
= currentRank
;
1374 foundTimestamp
= event
.getTimestamp();
1375 if (direction
== Direction
.FORWARD
) {
1383 ((ITmfDataProvider
) fTrace
).sendRequest(request
);
1385 request
.waitForCompletion();
1386 if (request
.isCancelled()) {
1387 return Status
.OK_STATUS
;
1389 } catch (final InterruptedException e
) {
1390 synchronized (fSearchSyncObj
) {
1391 fSearchThread
= null;
1393 return Status
.OK_STATUS
;
1395 if (foundRank
== -1) {
1396 if (direction
== Direction
.FORWARD
) {
1398 synchronized (fSearchSyncObj
) {
1399 fSearchThread
= null;
1401 return Status
.OK_STATUS
;
1403 nbRequested
= (int) rank
;
1409 rank
= (int) fTrace
.getNbEvents() - 1;
1412 if ((rank
<= startRank
) && wrapped
) {
1413 synchronized (fSearchSyncObj
) {
1414 fSearchThread
= null;
1416 return Status
.OK_STATUS
;
1421 int index
= (int) foundRank
;
1422 if (eventFilter
!= null) {
1423 index
= fCache
.getFilteredEventIndex(foundRank
);
1425 final int selection
= index
+ 1 + (eventFilter
!= null ?
+1 : 0); // +1 for header row, +1 for top filter status row
1427 display
.asyncExec(new Runnable() {
1430 if (monitor
.isCanceled()) {
1433 if (fTable
.isDisposed()) {
1436 fTable
.setSelection(selection
);
1437 fSelectedRank
= foundRank
;
1438 fRawViewer
.selectAndReveal(fSelectedRank
);
1439 if (foundTimestamp
!= null) {
1440 broadcast(new TmfTimeSynchSignal(TmfEventsTable
.this, foundTimestamp
));
1442 synchronized (fSearchSyncObj
) {
1443 fSearchThread
= null;
1447 return Status
.OK_STATUS
;
1451 protected void canceling() {
1453 synchronized (fSearchSyncObj
) {
1454 fSearchThread
= null;
1459 protected void createResources() {
1460 fGrayColor
= fResourceManager
.createColor(ColorUtil
.blend(fTable
.getBackground().getRGB(), fTable
1461 .getForeground().getRGB()));
1462 fGreenColor
= fTable
.getDisplay().getSystemColor(SWT
.COLOR_DARK_GREEN
);
1463 fBoldFont
= fResourceManager
.createFont(FontDescriptor
.createFrom(fTable
.getFont()).setStyle(SWT
.BOLD
));
1466 protected void packColumns() {
1470 for (final TableColumn column
: fTable
.getColumns()) {
1471 final int headerWidth
= column
.getWidth();
1473 if (column
.getWidth() < headerWidth
) {
1474 column
.setWidth(headerWidth
);
1484 * FIXME: Add support for column selection
1486 protected ITmfEventField
[] extractItemFields(final ITmfEvent event
) {
1487 ITmfEventField
[] fields
= new TmfEventField
[0];
1488 if (event
!= null) {
1489 final String timestamp
= event
.getTimestamp().toString();
1490 final String source
= event
.getSource();
1491 final String type
= event
.getType().getName();
1492 final String reference
= event
.getReference();
1493 final ITmfEventField content
= event
.getContent();
1494 final String value
= (content
.getValue() != null) ? content
.getValue().toString() : content
.toString();
1495 fields
= new TmfEventField
[] {
1496 new TmfEventField(ITmfEvent
.EVENT_FIELD_TIMESTAMP
, timestamp
),
1497 new TmfEventField(ITmfEvent
.EVENT_FIELD_SOURCE
, source
),
1498 new TmfEventField(ITmfEvent
.EVENT_FIELD_TYPE
, type
),
1499 new TmfEventField(ITmfEvent
.EVENT_FIELD_REFERENCE
, reference
),
1500 new TmfEventField(ITmfEvent
.EVENT_FIELD_CONTENT
, value
)
1507 * Notify this table that is got the UI focus.
1509 public void setFocus() {
1514 * Assign a new trace to this event table.
1517 * The trace to assign to this event table
1518 * @param disposeOnClose
1519 * true if the trace should be disposed when the table is
1522 public void setTrace(final ITmfTrace trace
, final boolean disposeOnClose
) {
1523 if ((fTrace
!= null) && fDisposeOnClose
) {
1529 fDisposeOnClose
= disposeOnClose
;
1531 // Perform the updates on the UI thread
1532 fTable
.getDisplay().syncExec(new Runnable() {
1536 fCache
.setTrace(fTrace
); // Clear the cache
1537 if (fTrace
!= null) {
1538 if (!fTable
.isDisposed() && (fTrace
!= null)) {
1539 if (fTable
.getData(Key
.FILTER_OBJ
) == null) {
1540 fTable
.setItemCount((int) fTrace
.getNbEvents() + 1); // +1 for header row
1543 fFilterMatchCount
= 0;
1544 fFilterCheckCount
= 0;
1545 fTable
.setItemCount(3); // +1 for header row, +2 for top and bottom filter status rows
1546 startFilterThread();
1550 fRawViewer
.setTrace(fTrace
);
1555 // ------------------------------------------------------------------------
1557 // ------------------------------------------------------------------------
1560 * Notify that the event cache has been updated
1563 * Also notify if the populating of the cache is complete, or
1566 public void cacheUpdated(final boolean completed
) {
1567 synchronized (fCacheUpdateSyncObj
) {
1568 if (fCacheUpdateBusy
) {
1569 fCacheUpdatePending
= true;
1570 fCacheUpdateCompleted
= completed
;
1573 fCacheUpdateBusy
= true;
1575 // Event cache is now updated. Perform update on the UI thread
1576 if (!fTable
.isDisposed()) {
1577 fTable
.getDisplay().asyncExec(new Runnable() {
1580 if (!fTable
.isDisposed()) {
1585 populateCompleted();
1587 synchronized (fCacheUpdateSyncObj
) {
1588 fCacheUpdateBusy
= false;
1589 if (fCacheUpdatePending
) {
1590 fCacheUpdatePending
= false;
1591 cacheUpdated(fCacheUpdateCompleted
);
1599 protected void populateCompleted() {
1600 // Nothing by default;
1603 // ------------------------------------------------------------------------
1604 // ISelectionProvider
1605 // ------------------------------------------------------------------------
1608 * @see org.eclipse.jface.viewers.ISelectionProvider#addSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
1614 public void addSelectionChangedListener(ISelectionChangedListener listener
) {
1615 selectionChangedListeners
.add(listener
);
1619 * @see org.eclipse.jface.viewers.ISelectionProvider#getSelection()
1625 public ISelection
getSelection() {
1626 if (fTable
== null || fTable
.isDisposed()) {
1627 return StructuredSelection
.EMPTY
;
1629 List
<Object
> list
= new ArrayList
<Object
>(fTable
.getSelection().length
);
1630 for (TableItem item
: fTable
.getSelection()) {
1631 if (item
.getData() != null) {
1632 list
.add(item
.getData());
1635 return new StructuredSelection(list
);
1639 * @see org.eclipse.jface.viewers.ISelectionProvider#removeSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
1645 public void removeSelectionChangedListener(ISelectionChangedListener listener
) {
1646 selectionChangedListeners
.remove(listener
);
1650 * @see org.eclipse.jface.viewers.ISelectionProvider#setSelection(org.eclipse.jface.viewers.ISelection)
1656 public void setSelection(ISelection selection
) {
1661 * Notifies any selection changed listeners that the viewer's selection has changed.
1662 * Only listeners registered at the time this method is called are notified.
1664 * @param event a selection changed event
1666 * @see ISelectionChangedListener#selectionChanged
1669 protected void fireSelectionChanged(final SelectionChangedEvent event
) {
1670 Object
[] listeners
= selectionChangedListeners
.getListeners();
1671 for (int i
= 0; i
< listeners
.length
; ++i
) {
1672 final ISelectionChangedListener l
= (ISelectionChangedListener
) listeners
[i
];
1673 SafeRunnable
.run(new SafeRunnable() {
1675 l
.selectionChanged(event
);
1681 // ------------------------------------------------------------------------
1682 // Bookmark handling
1683 // ------------------------------------------------------------------------
1686 * Add a bookmark to this event table.
1688 * @param bookmarksFile
1689 * The file to use for the bookmarks
1691 public void addBookmark(final IFile bookmarksFile
) {
1692 fBookmarksFile
= bookmarksFile
;
1693 final TableItem
[] selection
= fTable
.getSelection();
1694 if (selection
.length
> 0) {
1695 final TableItem tableItem
= selection
[0];
1696 if (tableItem
.getData(Key
.RANK
) != null) {
1697 final StringBuffer defaultMessage
= new StringBuffer();
1698 for (int i
= 0; i
< fTable
.getColumns().length
; i
++) {
1701 defaultMessage
.append(", "); //$NON-NLS-1$
1703 defaultMessage
.append(tableItem
.getText(i
));
1705 final InputDialog dialog
= new InputDialog(PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell(),
1706 Messages
.TmfEventsTable_AddBookmarkDialogTitle
, Messages
.TmfEventsTable_AddBookmarkDialogText
,
1707 defaultMessage
.toString(), null);
1708 if (dialog
.open() == Window
.OK
) {
1709 final String message
= dialog
.getValue();
1711 final IMarker bookmark
= bookmarksFile
.createMarker(IMarker
.BOOKMARK
);
1712 if (bookmark
.exists()) {
1713 bookmark
.setAttribute(IMarker
.MESSAGE
, message
.toString());
1714 final long rank
= (Long
) tableItem
.getData(Key
.RANK
);
1715 final int location
= (int) rank
;
1716 bookmark
.setAttribute(IMarker
.LOCATION
, (Integer
) location
);
1717 fBookmarksMap
.put(rank
, bookmark
.getId());
1720 } catch (final CoreException e
) {
1721 displayException(e
);
1730 * Remove a bookmark from this event table.
1733 * The bookmark to remove
1735 public void removeBookmark(final IMarker bookmark
) {
1736 for (final Entry
<Long
, Long
> entry
: fBookmarksMap
.entrySet()) {
1737 if (entry
.getValue().equals(bookmark
.getId())) {
1738 fBookmarksMap
.remove(entry
.getKey());
1745 private void toggleBookmark(final long rank
) {
1746 if (fBookmarksFile
== null) {
1749 if (fBookmarksMap
.containsKey(rank
)) {
1750 final Long markerId
= fBookmarksMap
.remove(rank
);
1753 final IMarker bookmark
= fBookmarksFile
.findMarker(markerId
);
1754 if (bookmark
!= null) {
1757 } catch (final CoreException e
) {
1758 displayException(e
);
1761 addBookmark(fBookmarksFile
);
1766 * Refresh the bookmarks assigned to this trace, from the contents of a
1769 * @param bookmarksFile
1770 * The bookmark file to use
1772 public void refreshBookmarks(final IFile bookmarksFile
) {
1773 fBookmarksFile
= bookmarksFile
;
1774 if (bookmarksFile
== null) {
1775 fBookmarksMap
.clear();
1780 fBookmarksMap
.clear();
1781 for (final IMarker bookmark
: bookmarksFile
.findMarkers(IMarker
.BOOKMARK
, false, IResource
.DEPTH_ZERO
)) {
1782 final int location
= bookmark
.getAttribute(IMarker
.LOCATION
, -1);
1783 if (location
!= -1) {
1784 final long rank
= location
;
1785 fBookmarksMap
.put(rank
, bookmark
.getId());
1789 } catch (final CoreException e
) {
1790 displayException(e
);
1795 public void gotoMarker(final IMarker marker
) {
1796 final int rank
= marker
.getAttribute(IMarker
.LOCATION
, -1);
1799 if (fTable
.getData(Key
.FILTER_OBJ
) != null) {
1800 index
= fCache
.getFilteredEventIndex(rank
) + 1; // +1 for top filter status row
1801 } else if (rank
>= fTable
.getItemCount()) {
1802 fPendingGotoRank
= rank
;
1804 fTable
.setSelection(index
+ 1); // +1 for header row
1808 // ------------------------------------------------------------------------
1810 // ------------------------------------------------------------------------
1815 * @see org.eclipse.linuxtools.tmf.ui.views.colors.IColorSettingsListener#colorSettingsChanged(org.eclipse.linuxtools.tmf.ui.views.colors.ColorSetting[])
1818 public void colorSettingsChanged(final ColorSetting
[] colorSettings
) {
1823 public void addEventsFilterListener(final ITmfEventsFilterListener listener
) {
1824 if (!fEventsFilterListeners
.contains(listener
)) {
1825 fEventsFilterListeners
.add(listener
);
1830 public void removeEventsFilterListener(final ITmfEventsFilterListener listener
) {
1831 fEventsFilterListeners
.remove(listener
);
1834 // ------------------------------------------------------------------------
1836 // ------------------------------------------------------------------------
1839 * Handler for the experiment updated signal.
1842 * The incoming signal
1845 public void experimentUpdated(final TmfExperimentUpdatedSignal signal
) {
1846 if ((signal
.getExperiment() != fTrace
) || fTable
.isDisposed()) {
1849 // Perform the refresh on the UI thread
1850 Display
.getDefault().asyncExec(new Runnable() {
1853 if (!fTable
.isDisposed() && (fTrace
!= null)) {
1854 if (fTable
.getData(Key
.FILTER_OBJ
) == null) {
1855 fTable
.setItemCount((int) fTrace
.getNbEvents() + 1); // +1 for header row
1856 if ((fPendingGotoRank
!= -1) && ((fPendingGotoRank
+ 1) < fTable
.getItemCount())) { // +1 for header row
1857 fTable
.setSelection((int) fPendingGotoRank
+ 1); // +1 for header row
1858 fPendingGotoRank
= -1;
1861 startFilterThread();
1864 if (!fRawViewer
.isDisposed() && (fTrace
!= null)) {
1865 fRawViewer
.refreshEventCount();
1872 * Handler for the trace updated signal
1875 * The incoming signal
1878 public void traceUpdated(final TmfTraceUpdatedSignal signal
) {
1879 if ((signal
.getTrace() != fTrace
) || fTable
.isDisposed()) {
1882 // Perform the refresh on the UI thread
1883 Display
.getDefault().asyncExec(new Runnable() {
1886 if (!fTable
.isDisposed() && (fTrace
!= null)) {
1887 if (fTable
.getData(Key
.FILTER_OBJ
) == null) {
1888 fTable
.setItemCount((int) fTrace
.getNbEvents() + 1); // +1 for header row
1889 if ((fPendingGotoRank
!= -1) && ((fPendingGotoRank
+ 1) < fTable
.getItemCount())) { // +1 for header row
1890 fTable
.setSelection((int) fPendingGotoRank
+ 1); // +1 for header row
1891 fPendingGotoRank
= -1;
1894 startFilterThread();
1897 if (!fRawViewer
.isDisposed() && (fTrace
!= null)) {
1898 fRawViewer
.refreshEventCount();
1905 * Handler for the time synch signal.
1908 * The incoming signal
1911 public void currentTimeUpdated(final TmfTimeSynchSignal signal
) {
1912 if ((signal
.getSource() != this) && (fTrace
!= null) && (!fTable
.isDisposed())) {
1914 // Create a request for one event that will be queued after other ongoing requests. When this request is completed
1915 // do the work to select the actual event with the timestamp specified in the signal. This procedure prevents
1916 // the method fTrace.getRank() from interfering and delaying ongoing requests.
1917 final TmfDataRequest subRequest
= new TmfDataRequest(ITmfEvent
.class, 0, 1, ExecutionType
.FOREGROUND
) {
1919 TmfTimestamp ts
= new TmfTimestamp(signal
.getCurrentTime());
1922 public void handleData(final ITmfEvent event
) {
1923 super.handleData(event
);
1927 public void handleCompleted() {
1928 super.handleCompleted();
1929 if (fTrace
== null) {
1933 // Verify if the event is within the trace range and adjust if necessary
1934 ITmfTimestamp timestamp
= ts
;
1935 if (timestamp
.compareTo(fTrace
.getStartTime(), true) == -1) {
1936 timestamp
= fTrace
.getStartTime();
1938 if (timestamp
.compareTo(fTrace
.getEndTime(), true) == 1) {
1939 timestamp
= fTrace
.getEndTime();
1942 // Get the rank of the selected event in the table
1943 final ITmfContext context
= fTrace
.seekEvent(timestamp
);
1944 final long rank
= context
.getRank();
1946 fSelectedRank
= rank
;
1948 fTable
.getDisplay().asyncExec(new Runnable() {
1951 // Return if table is disposed
1952 if (fTable
.isDisposed()) {
1956 int index
= (int) rank
;
1957 if (fTable
.isDisposed()) {
1960 if (fTable
.getData(Key
.FILTER_OBJ
) != null)
1962 index
= fCache
.getFilteredEventIndex(rank
) + 1; // +1 for top filter status row
1964 fTable
.setSelection(index
+ 1); // +1 for header row
1965 fRawViewer
.selectAndReveal(rank
);
1971 ((ITmfDataProvider
) fTrace
).sendRequest(subRequest
);
1975 // ------------------------------------------------------------------------
1977 // ------------------------------------------------------------------------
1980 * Display an exception in a message box
1982 * @param e the exception
1984 private static void displayException(final Exception e
) {
1985 final MessageBox mb
= new MessageBox(PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell());
1986 mb
.setText(e
.getClass().getName());
1987 mb
.setMessage(e
.getMessage());