| 1 | /*******************************************************************************\r |
| 2 | * Copyright (c) 2010 Ericsson\r |
| 3 | * \r |
| 4 | * All rights reserved. This program and the accompanying materials are\r |
| 5 | * made available under the terms of the Eclipse Public License v1.0 which\r |
| 6 | * accompanies this distribution, and is available at\r |
| 7 | * http://www.eclipse.org/legal/epl-v10.html\r |
| 8 | * \r |
| 9 | * Contributors:\r |
| 10 | * Francois Chouinard - Initial API and implementation\r |
| 11 | * Patrick Tasse - Factored out from events view\r |
| 12 | * Francois Chouinard - Replaced Table by TmfVirtualTable\r |
| 13 | * Patrick Tasse - Filter implementation (inspired by www.eclipse.org/mat)\r |
| 14 | *******************************************************************************/\r |
| 15 | \r |
| 16 | package org.eclipse.linuxtools.tmf.ui.viewers.events;\r |
| 17 | \r |
| 18 | import java.util.ArrayList;\r |
| 19 | import java.util.Arrays;\r |
| 20 | import java.util.HashMap;\r |
| 21 | import java.util.Map;\r |
| 22 | import java.util.Map.Entry;\r |
| 23 | import java.util.regex.Pattern;\r |
| 24 | import java.util.regex.PatternSyntaxException;\r |
| 25 | \r |
| 26 | import org.eclipse.core.resources.IFile;\r |
| 27 | import org.eclipse.core.resources.IMarker;\r |
| 28 | import org.eclipse.core.resources.IResource;\r |
| 29 | import org.eclipse.core.runtime.CoreException;\r |
| 30 | import org.eclipse.core.runtime.IProgressMonitor;\r |
| 31 | import org.eclipse.core.runtime.IStatus;\r |
| 32 | import org.eclipse.core.runtime.Status;\r |
| 33 | import org.eclipse.core.runtime.jobs.Job;\r |
| 34 | import org.eclipse.jface.action.Action;\r |
| 35 | import org.eclipse.jface.action.IAction;\r |
| 36 | import org.eclipse.jface.action.IMenuListener;\r |
| 37 | import org.eclipse.jface.action.IMenuManager;\r |
| 38 | import org.eclipse.jface.action.MenuManager;\r |
| 39 | import org.eclipse.jface.action.Separator;\r |
| 40 | import org.eclipse.jface.dialogs.Dialog;\r |
| 41 | import org.eclipse.jface.dialogs.InputDialog;\r |
| 42 | import org.eclipse.jface.dialogs.MessageDialog;\r |
| 43 | import org.eclipse.jface.resource.FontDescriptor;\r |
| 44 | import org.eclipse.jface.resource.JFaceResources;\r |
| 45 | import org.eclipse.jface.resource.LocalResourceManager;\r |
| 46 | import org.eclipse.linuxtools.tmf.core.component.ITmfDataProvider;\r |
| 47 | import org.eclipse.linuxtools.tmf.core.component.TmfComponent;\r |
| 48 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;\r |
| 49 | import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;\r |
| 50 | import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;\r |
| 51 | import org.eclipse.linuxtools.tmf.core.event.TmfEvent;\r |
| 52 | import org.eclipse.linuxtools.tmf.core.event.TmfEventField;\r |
| 53 | import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;\r |
| 54 | import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;\r |
| 55 | import org.eclipse.linuxtools.tmf.core.filter.ITmfFilter;\r |
| 56 | import org.eclipse.linuxtools.tmf.core.filter.model.ITmfFilterTreeNode;\r |
| 57 | import org.eclipse.linuxtools.tmf.core.filter.model.TmfFilterAndNode;\r |
| 58 | import org.eclipse.linuxtools.tmf.core.filter.model.TmfFilterMatchesNode;\r |
| 59 | import org.eclipse.linuxtools.tmf.core.filter.model.TmfFilterNode;\r |
| 60 | import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest.ExecutionType;\r |
| 61 | import org.eclipse.linuxtools.tmf.core.request.TmfDataRequest;\r |
| 62 | import org.eclipse.linuxtools.tmf.core.request.TmfEventRequest;\r |
| 63 | import org.eclipse.linuxtools.tmf.core.signal.TmfExperimentUpdatedSignal;\r |
| 64 | import org.eclipse.linuxtools.tmf.core.signal.TmfSignalHandler;\r |
| 65 | import org.eclipse.linuxtools.tmf.core.signal.TmfTimeSynchSignal;\r |
| 66 | import org.eclipse.linuxtools.tmf.core.signal.TmfTraceUpdatedSignal;\r |
| 67 | import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;\r |
| 68 | import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;\r |
| 69 | import org.eclipse.linuxtools.tmf.ui.TmfUiPlugin;\r |
| 70 | import org.eclipse.linuxtools.tmf.ui.internal.Messages;\r |
| 71 | import org.eclipse.linuxtools.tmf.ui.viewers.events.TmfEventsCache.CachedEvent;\r |
| 72 | import org.eclipse.linuxtools.tmf.ui.views.colors.ColorSetting;\r |
| 73 | import org.eclipse.linuxtools.tmf.ui.views.colors.ColorSettingsManager;\r |
| 74 | import org.eclipse.linuxtools.tmf.ui.views.colors.IColorSettingsListener;\r |
| 75 | import org.eclipse.linuxtools.tmf.ui.views.filter.FilterManager;\r |
| 76 | import org.eclipse.linuxtools.tmf.ui.widgets.ColumnData;\r |
| 77 | import org.eclipse.linuxtools.tmf.ui.widgets.TmfRawEventViewer;\r |
| 78 | import org.eclipse.linuxtools.tmf.ui.widgets.TmfVirtualTable;\r |
| 79 | import org.eclipse.swt.SWT;\r |
| 80 | import org.eclipse.swt.custom.SashForm;\r |
| 81 | import org.eclipse.swt.custom.TableEditor;\r |
| 82 | import org.eclipse.swt.events.FocusAdapter;\r |
| 83 | import org.eclipse.swt.events.FocusEvent;\r |
| 84 | import org.eclipse.swt.events.KeyAdapter;\r |
| 85 | import org.eclipse.swt.events.KeyEvent;\r |
| 86 | import org.eclipse.swt.events.MouseAdapter;\r |
| 87 | import org.eclipse.swt.events.MouseEvent;\r |
| 88 | import org.eclipse.swt.events.SelectionAdapter;\r |
| 89 | import org.eclipse.swt.events.SelectionEvent;\r |
| 90 | import org.eclipse.swt.graphics.Color;\r |
| 91 | import org.eclipse.swt.graphics.Font;\r |
| 92 | import org.eclipse.swt.graphics.Image;\r |
| 93 | import org.eclipse.swt.graphics.Point;\r |
| 94 | import org.eclipse.swt.graphics.Rectangle;\r |
| 95 | import org.eclipse.swt.layout.FillLayout;\r |
| 96 | import org.eclipse.swt.layout.GridData;\r |
| 97 | import org.eclipse.swt.layout.GridLayout;\r |
| 98 | import org.eclipse.swt.widgets.Composite;\r |
| 99 | import org.eclipse.swt.widgets.Display;\r |
| 100 | import org.eclipse.swt.widgets.Event;\r |
| 101 | import org.eclipse.swt.widgets.Label;\r |
| 102 | import org.eclipse.swt.widgets.Listener;\r |
| 103 | import org.eclipse.swt.widgets.Menu;\r |
| 104 | import org.eclipse.swt.widgets.MessageBox;\r |
| 105 | import org.eclipse.swt.widgets.Shell;\r |
| 106 | import org.eclipse.swt.widgets.TableColumn;\r |
| 107 | import org.eclipse.swt.widgets.TableItem;\r |
| 108 | import org.eclipse.swt.widgets.Text;\r |
| 109 | import org.eclipse.ui.PlatformUI;\r |
| 110 | import org.eclipse.ui.ide.IGotoMarker;\r |
| 111 | import org.eclipse.ui.themes.ColorUtil;\r |
| 112 | \r |
| 113 | /**\r |
| 114 | * <b><u>TmfEventsTable</u></b>\r |
| 115 | */\r |
| 116 | public class TmfEventsTable extends TmfComponent implements IGotoMarker, IColorSettingsListener,\r |
| 117 | ITmfEventsFilterProvider {\r |
| 118 | \r |
| 119 | private static final Image BOOKMARK_IMAGE = TmfUiPlugin.getDefault().getImageFromPath(\r |
| 120 | "icons/elcl16/bookmark_obj.gif"); //$NON-NLS-1$\r |
| 121 | private static final Image SEARCH_IMAGE = TmfUiPlugin.getDefault().getImageFromPath("icons/elcl16/search.gif"); //$NON-NLS-1$\r |
| 122 | private static final Image SEARCH_MATCH_IMAGE = TmfUiPlugin.getDefault().getImageFromPath(\r |
| 123 | "icons/elcl16/search_match.gif"); //$NON-NLS-1$\r |
| 124 | private static final Image SEARCH_MATCH_BOOKMARK_IMAGE = TmfUiPlugin.getDefault().getImageFromPath(\r |
| 125 | "icons/elcl16/search_match_bookmark.gif"); //$NON-NLS-1$\r |
| 126 | private static final Image FILTER_IMAGE = TmfUiPlugin.getDefault()\r |
| 127 | .getImageFromPath("icons/elcl16/filter_items.gif"); //$NON-NLS-1$\r |
| 128 | private static final Image STOP_IMAGE = TmfUiPlugin.getDefault().getImageFromPath("icons/elcl16/stop.gif"); //$NON-NLS-1$\r |
| 129 | private static final String SEARCH_HINT = Messages.TmfEventsTable_SearchHint;\r |
| 130 | private static final String FILTER_HINT = Messages.TmfEventsTable_FilterHint;\r |
| 131 | private static final int MAX_CACHE_SIZE = 1000;\r |
| 132 | \r |
| 133 | public interface Key {\r |
| 134 | String SEARCH_TXT = "$srch_txt"; //$NON-NLS-1$\r |
| 135 | String SEARCH_OBJ = "$srch_obj"; //$NON-NLS-1$\r |
| 136 | String FILTER_TXT = "$fltr_txt"; //$NON-NLS-1$\r |
| 137 | String FILTER_OBJ = "$fltr_obj"; //$NON-NLS-1$\r |
| 138 | String TIMESTAMP = "$time"; //$NON-NLS-1$\r |
| 139 | String RANK = "$rank"; //$NON-NLS-1$\r |
| 140 | String FIELD_ID = "$field_id"; //$NON-NLS-1$\r |
| 141 | String BOOKMARK = "$bookmark"; //$NON-NLS-1$\r |
| 142 | }\r |
| 143 | \r |
| 144 | public static enum HeaderState {\r |
| 145 | SEARCH, FILTER\r |
| 146 | }\r |
| 147 | \r |
| 148 | interface Direction {\r |
| 149 | int FORWARD = +1;\r |
| 150 | int BACKWARD = -1;\r |
| 151 | }\r |
| 152 | \r |
| 153 | // ------------------------------------------------------------------------\r |
| 154 | // Table data\r |
| 155 | // ------------------------------------------------------------------------\r |
| 156 | \r |
| 157 | protected Composite fComposite;\r |
| 158 | protected SashForm fSashForm;\r |
| 159 | protected TmfVirtualTable fTable;\r |
| 160 | protected TmfRawEventViewer fRawViewer;\r |
| 161 | protected ITmfTrace<?> fTrace;\r |
| 162 | protected boolean fPackDone = false;\r |
| 163 | protected HeaderState fHeaderState = HeaderState.SEARCH;\r |
| 164 | protected long fSelectedRank = 0;\r |
| 165 | \r |
| 166 | // Filter data\r |
| 167 | protected long fFilterMatchCount;\r |
| 168 | protected long fFilterCheckCount;\r |
| 169 | protected FilterThread fFilterThread;\r |
| 170 | protected final Object fFilterSyncObj = new Object();\r |
| 171 | protected SearchThread fSearchThread;\r |
| 172 | protected final Object fSearchSyncObj = new Object();\r |
| 173 | protected ArrayList<ITmfEventsFilterListener> fEventsFilterListeners = new ArrayList<ITmfEventsFilterListener>();\r |
| 174 | \r |
| 175 | // Bookmark map <Rank, MarkerId>\r |
| 176 | protected Map<Long, Long> fBookmarksMap = new HashMap<Long, Long>();\r |
| 177 | protected IFile fBookmarksFile;\r |
| 178 | protected long fPendingGotoRank = -1;\r |
| 179 | \r |
| 180 | // SWT resources\r |
| 181 | protected LocalResourceManager fResourceManager = new LocalResourceManager(JFaceResources.getResources());\r |
| 182 | protected Color fGrayColor;\r |
| 183 | protected Color fGreenColor;\r |
| 184 | protected Font fBoldFont;\r |
| 185 | \r |
| 186 | // Table column names\r |
| 187 | static private final String[] COLUMN_NAMES = new String[] { Messages.TmfEventsTable_TimestampColumnHeader,\r |
| 188 | Messages.TmfEventsTable_SourceColumnHeader, Messages.TmfEventsTable_TypeColumnHeader,\r |
| 189 | Messages.TmfEventsTable_ReferenceColumnHeader, Messages.TmfEventsTable_ContentColumnHeader };\r |
| 190 | \r |
| 191 | static private ColumnData[] COLUMN_DATA = new ColumnData[] { new ColumnData(COLUMN_NAMES[0], 100, SWT.LEFT),\r |
| 192 | new ColumnData(COLUMN_NAMES[1], 100, SWT.LEFT), new ColumnData(COLUMN_NAMES[2], 100, SWT.LEFT),\r |
| 193 | new ColumnData(COLUMN_NAMES[3], 100, SWT.LEFT), new ColumnData(COLUMN_NAMES[4], 100, SWT.LEFT) };\r |
| 194 | \r |
| 195 | // Event cache\r |
| 196 | private final TmfEventsCache fCache;\r |
| 197 | private boolean fCacheUpdateBusy = false;\r |
| 198 | private boolean fCacheUpdatePending = false;\r |
| 199 | private boolean fCacheUpdateCompleted = false;\r |
| 200 | private Object fCacheUpdateSyncObj = new Object();\r |
| 201 | \r |
| 202 | private boolean fDisposeOnClose;\r |
| 203 | \r |
| 204 | // ------------------------------------------------------------------------\r |
| 205 | // Constructor\r |
| 206 | // ------------------------------------------------------------------------\r |
| 207 | \r |
| 208 | public TmfEventsTable(Composite parent, int cacheSize) {\r |
| 209 | this(parent, cacheSize, COLUMN_DATA);\r |
| 210 | }\r |
| 211 | \r |
| 212 | public TmfEventsTable(Composite parent, int cacheSize, ColumnData[] columnData) {\r |
| 213 | super("TmfEventsTable"); //$NON-NLS-1$\r |
| 214 | \r |
| 215 | fComposite = new Composite(parent, SWT.NONE);\r |
| 216 | GridLayout gl = new GridLayout(1, false);\r |
| 217 | gl.marginHeight = 0;\r |
| 218 | gl.marginWidth = 0;\r |
| 219 | gl.verticalSpacing = 0;\r |
| 220 | fComposite.setLayout(gl);\r |
| 221 | \r |
| 222 | fSashForm = new SashForm(fComposite, SWT.HORIZONTAL);\r |
| 223 | fSashForm.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));\r |
| 224 | \r |
| 225 | // Create a virtual table\r |
| 226 | final int style = SWT.H_SCROLL | SWT.V_SCROLL | SWT.SINGLE | SWT.FULL_SELECTION;\r |
| 227 | fTable = new TmfVirtualTable(fSashForm, style);\r |
| 228 | \r |
| 229 | // Set the table layout\r |
| 230 | GridData layoutData = new GridData(SWT.FILL, SWT.FILL, true, true);\r |
| 231 | fTable.setLayoutData(layoutData);\r |
| 232 | \r |
| 233 | // Some cosmetic enhancements\r |
| 234 | fTable.setHeaderVisible(true);\r |
| 235 | fTable.setLinesVisible(true);\r |
| 236 | \r |
| 237 | // Set the columns\r |
| 238 | setColumnHeaders(columnData);\r |
| 239 | \r |
| 240 | // Set the default column field ids if this is not a subclass\r |
| 241 | if (Arrays.equals(columnData, COLUMN_DATA)) {\r |
| 242 | fTable.getColumns()[0].setData(Key.FIELD_ID, ITmfEvent.EVENT_FIELD_TIMESTAMP);\r |
| 243 | fTable.getColumns()[1].setData(Key.FIELD_ID, ITmfEvent.EVENT_FIELD_SOURCE);\r |
| 244 | fTable.getColumns()[2].setData(Key.FIELD_ID, ITmfEvent.EVENT_FIELD_TYPE);\r |
| 245 | fTable.getColumns()[3].setData(Key.FIELD_ID, ITmfEvent.EVENT_FIELD_REFERENCE);\r |
| 246 | fTable.getColumns()[4].setData(Key.FIELD_ID, ITmfEvent.EVENT_FIELD_CONTENT);\r |
| 247 | }\r |
| 248 | \r |
| 249 | // Set the frozen row for header row\r |
| 250 | fTable.setFrozenRowCount(1);\r |
| 251 | \r |
| 252 | // Create the header row cell editor\r |
| 253 | createHeaderEditor();\r |
| 254 | \r |
| 255 | // Handle the table item selection\r |
| 256 | fTable.addSelectionListener(new SelectionAdapter() {\r |
| 257 | @Override\r |
| 258 | public void widgetSelected(SelectionEvent e) {\r |
| 259 | TableItem[] selection = fTable.getSelection();\r |
| 260 | if (selection.length > 0) {\r |
| 261 | TableItem selectedTableItem = selection[0];\r |
| 262 | if (selectedTableItem != null) {\r |
| 263 | if (selectedTableItem.getData(Key.RANK) instanceof Long) {\r |
| 264 | fSelectedRank = (Long) selectedTableItem.getData(Key.RANK);\r |
| 265 | fRawViewer.selectAndReveal((Long) selectedTableItem.getData(Key.RANK));\r |
| 266 | }\r |
| 267 | if (selectedTableItem.getData(Key.TIMESTAMP) instanceof TmfTimestamp) {\r |
| 268 | TmfTimestamp ts = (TmfTimestamp) selectedTableItem.getData(Key.TIMESTAMP);\r |
| 269 | broadcast(new TmfTimeSynchSignal(fTable, ts));\r |
| 270 | }\r |
| 271 | }\r |
| 272 | }\r |
| 273 | }\r |
| 274 | });\r |
| 275 | \r |
| 276 | cacheSize = Math.max(cacheSize, Display.getDefault().getBounds().height / fTable.getItemHeight());\r |
| 277 | cacheSize = Math.min(cacheSize, MAX_CACHE_SIZE);\r |
| 278 | fCache = new TmfEventsCache(cacheSize, this);\r |
| 279 | \r |
| 280 | // Handle the table item requests\r |
| 281 | fTable.addListener(SWT.SetData, new Listener() {\r |
| 282 | \r |
| 283 | @Override\r |
| 284 | public void handleEvent(Event event) {\r |
| 285 | \r |
| 286 | final TableItem item = (TableItem) event.item;\r |
| 287 | int index = event.index - 1; // -1 for the header row\r |
| 288 | \r |
| 289 | if (event.index == 0) {\r |
| 290 | setHeaderRowItemData(item);\r |
| 291 | return;\r |
| 292 | }\r |
| 293 | \r |
| 294 | if (fTable.getData(Key.FILTER_OBJ) != null) {\r |
| 295 | if (event.index == 1 || event.index == fTable.getItemCount() - 1) {\r |
| 296 | setFilterStatusRowItemData(item);\r |
| 297 | return;\r |
| 298 | }\r |
| 299 | index = index - 1; // -1 for top filter status row\r |
| 300 | }\r |
| 301 | \r |
| 302 | CachedEvent cachedEvent = fCache.getEvent(index);\r |
| 303 | if (cachedEvent != null) {\r |
| 304 | setItemData(item, cachedEvent.event, cachedEvent.rank);\r |
| 305 | return;\r |
| 306 | }\r |
| 307 | \r |
| 308 | // Else, fill the cache asynchronously (and off the UI thread)\r |
| 309 | event.doit = false;\r |
| 310 | }\r |
| 311 | });\r |
| 312 | \r |
| 313 | fTable.addMouseListener(new MouseAdapter() {\r |
| 314 | @Override\r |
| 315 | public void mouseDoubleClick(MouseEvent event) {\r |
| 316 | if (event.button != 1) {\r |
| 317 | return;\r |
| 318 | }\r |
| 319 | // Identify the selected row\r |
| 320 | Point point = new Point(event.x, event.y);\r |
| 321 | TableItem item = fTable.getItem(point);\r |
| 322 | if (item != null) {\r |
| 323 | Rectangle imageBounds = item.getImageBounds(0);\r |
| 324 | imageBounds.width = BOOKMARK_IMAGE.getBounds().width;\r |
| 325 | if (imageBounds.contains(point)) {\r |
| 326 | Long rank = (Long) item.getData(Key.RANK);\r |
| 327 | if (rank != null) {\r |
| 328 | toggleBookmark(rank);\r |
| 329 | }\r |
| 330 | }\r |
| 331 | }\r |
| 332 | }\r |
| 333 | });\r |
| 334 | \r |
| 335 | final Listener bookmarkListener = new Listener () {\r |
| 336 | Shell tooltipShell = null;\r |
| 337 | @Override\r |
| 338 | public void handleEvent(Event event) {\r |
| 339 | switch (event.type) {\r |
| 340 | case SWT.MouseHover:\r |
| 341 | TableItem item = fTable.getItem(new Point(event.x, event.y));\r |
| 342 | if (item == null) {\r |
| 343 | return;\r |
| 344 | }\r |
| 345 | String tooltipText = (String) item.getData(Key.BOOKMARK);\r |
| 346 | if (tooltipText == null) {\r |
| 347 | return;\r |
| 348 | };\r |
| 349 | Rectangle bounds = item.getImageBounds(0);\r |
| 350 | if (!bounds.contains(event.x,event.y)) {\r |
| 351 | return;\r |
| 352 | }\r |
| 353 | if (tooltipShell != null && !tooltipShell.isDisposed()) {\r |
| 354 | tooltipShell.dispose();\r |
| 355 | }\r |
| 356 | tooltipShell = new Shell(fTable.getShell(), SWT.ON_TOP | SWT.NO_FOCUS | SWT.TOOL);\r |
| 357 | tooltipShell.setBackground(PlatformUI.getWorkbench().getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND));\r |
| 358 | FillLayout layout = new FillLayout();\r |
| 359 | layout.marginWidth = 2;\r |
| 360 | tooltipShell.setLayout(layout);\r |
| 361 | Label label = new Label(tooltipShell, SWT.WRAP);\r |
| 362 | label.setForeground(PlatformUI.getWorkbench().getDisplay().getSystemColor(SWT.COLOR_INFO_FOREGROUND));\r |
| 363 | label.setBackground(PlatformUI.getWorkbench().getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND));\r |
| 364 | label.setText(tooltipText);\r |
| 365 | label.addListener(SWT.MouseExit, this);\r |
| 366 | label.addListener(SWT.MouseDown, this);\r |
| 367 | label.addListener(SWT.MouseWheel, this);\r |
| 368 | Point size = tooltipShell.computeSize(SWT.DEFAULT, SWT.DEFAULT);\r |
| 369 | Point pt = fTable.toDisplay(event.x, event.y);\r |
| 370 | tooltipShell.setBounds(pt.x, pt.y, size.x, size.y);\r |
| 371 | tooltipShell.setVisible(true);\r |
| 372 | break;\r |
| 373 | case SWT.Dispose:\r |
| 374 | case SWT.KeyDown:\r |
| 375 | case SWT.MouseMove:\r |
| 376 | case SWT.MouseExit:\r |
| 377 | case SWT.MouseDown:\r |
| 378 | case SWT.MouseWheel:\r |
| 379 | if (tooltipShell != null) {\r |
| 380 | tooltipShell.dispose();\r |
| 381 | tooltipShell = null;\r |
| 382 | }\r |
| 383 | break;\r |
| 384 | }\r |
| 385 | }\r |
| 386 | };\r |
| 387 | \r |
| 388 | fTable.addListener(SWT.MouseHover, bookmarkListener);\r |
| 389 | fTable.addListener(SWT.Dispose, bookmarkListener);\r |
| 390 | fTable.addListener(SWT.KeyDown, bookmarkListener);\r |
| 391 | fTable.addListener(SWT.MouseMove, bookmarkListener);\r |
| 392 | fTable.addListener(SWT.MouseExit, bookmarkListener);\r |
| 393 | fTable.addListener(SWT.MouseDown, bookmarkListener);\r |
| 394 | fTable.addListener(SWT.MouseWheel, bookmarkListener);\r |
| 395 | \r |
| 396 | // Create resources\r |
| 397 | createResources();\r |
| 398 | \r |
| 399 | ColorSettingsManager.addColorSettingsListener(this);\r |
| 400 | \r |
| 401 | fTable.setItemCount(1); // +1 for header row\r |
| 402 | \r |
| 403 | fRawViewer = new TmfRawEventViewer(fSashForm, SWT.H_SCROLL | SWT.V_SCROLL);\r |
| 404 | \r |
| 405 | fRawViewer.addSelectionListener(new Listener() {\r |
| 406 | @Override\r |
| 407 | public void handleEvent(Event e) {\r |
| 408 | if (e.data instanceof Long) {\r |
| 409 | long rank = (Long) e.data;\r |
| 410 | int index = (int) rank;\r |
| 411 | if (fTable.getData(Key.FILTER_OBJ) != null) {\r |
| 412 | index = fCache.getFilteredEventIndex(rank) + 1; // +1 for top filter status row\r |
| 413 | }\r |
| 414 | fTable.setSelection(index + 1); // +1 for header row\r |
| 415 | fSelectedRank = rank;\r |
| 416 | } else if (e.data instanceof ITmfLocation<?>) {\r |
| 417 | // DOES NOT WORK: rank undefined in context from seekLocation()\r |
| 418 | // ITmfLocation<?> location = (ITmfLocation<?>) e.data;\r |
| 419 | // TmfContext context = fTrace.seekLocation(location);\r |
| 420 | // fTable.setSelection((int) context.getRank());\r |
| 421 | return;\r |
| 422 | } else {\r |
| 423 | return;\r |
| 424 | }\r |
| 425 | TableItem[] selection = fTable.getSelection();\r |
| 426 | if (selection != null && selection.length > 0) {\r |
| 427 | TmfTimestamp ts = (TmfTimestamp) fTable.getSelection()[0].getData(Key.TIMESTAMP);\r |
| 428 | if (ts != null) {\r |
| 429 | broadcast(new TmfTimeSynchSignal(fTable, ts));\r |
| 430 | }\r |
| 431 | }\r |
| 432 | }\r |
| 433 | });\r |
| 434 | \r |
| 435 | fSashForm.setWeights(new int[] { 1, 1 });\r |
| 436 | fRawViewer.setVisible(false);\r |
| 437 | \r |
| 438 | createPopupMenu();\r |
| 439 | }\r |
| 440 | \r |
| 441 | protected void createPopupMenu() {\r |
| 442 | final IAction showTableAction = new Action(Messages.TmfEventsTable_ShowTableActionText) {\r |
| 443 | @Override\r |
| 444 | public void run() {\r |
| 445 | fTable.setVisible(true);\r |
| 446 | fSashForm.layout();\r |
| 447 | }\r |
| 448 | };\r |
| 449 | \r |
| 450 | final IAction hideTableAction = new Action(Messages.TmfEventsTable_HideTableActionText) {\r |
| 451 | @Override\r |
| 452 | public void run() {\r |
| 453 | fTable.setVisible(false);\r |
| 454 | fSashForm.layout();\r |
| 455 | }\r |
| 456 | };\r |
| 457 | \r |
| 458 | final IAction showRawAction = new Action(Messages.TmfEventsTable_ShowRawActionText) {\r |
| 459 | @Override\r |
| 460 | public void run() {\r |
| 461 | fRawViewer.setVisible(true);\r |
| 462 | fSashForm.layout();\r |
| 463 | int index = fTable.getSelectionIndex();\r |
| 464 | if (index >= +1) { // +1 for header row\r |
| 465 | fRawViewer.selectAndReveal(index - 1);\r |
| 466 | }\r |
| 467 | }\r |
| 468 | };\r |
| 469 | \r |
| 470 | final IAction hideRawAction = new Action(Messages.TmfEventsTable_HideRawActionText) {\r |
| 471 | @Override\r |
| 472 | public void run() {\r |
| 473 | fRawViewer.setVisible(false);\r |
| 474 | fSashForm.layout();\r |
| 475 | }\r |
| 476 | };\r |
| 477 | \r |
| 478 | final IAction showSearchBarAction = new Action(Messages.TmfEventsTable_ShowSearchBarActionText) {\r |
| 479 | @Override\r |
| 480 | public void run() {\r |
| 481 | fHeaderState = HeaderState.SEARCH;\r |
| 482 | fTable.refresh();\r |
| 483 | }\r |
| 484 | };\r |
| 485 | \r |
| 486 | final IAction showFilterBarAction = new Action(Messages.TmfEventsTable_ShowFilterBarActionText) {\r |
| 487 | @Override\r |
| 488 | public void run() {\r |
| 489 | fHeaderState = HeaderState.FILTER;\r |
| 490 | fTable.refresh();\r |
| 491 | }\r |
| 492 | };\r |
| 493 | \r |
| 494 | final IAction clearFiltersAction = new Action(Messages.TmfEventsTable_ClearFiltersActionText) {\r |
| 495 | @Override\r |
| 496 | public void run() {\r |
| 497 | stopFilterThread();\r |
| 498 | stopSearchThread();\r |
| 499 | clearFilters();\r |
| 500 | }\r |
| 501 | };\r |
| 502 | \r |
| 503 | class ToggleBookmarkAction extends Action {\r |
| 504 | long fRank;\r |
| 505 | \r |
| 506 | public ToggleBookmarkAction(String text, long rank) {\r |
| 507 | super(text);\r |
| 508 | fRank = rank;\r |
| 509 | }\r |
| 510 | \r |
| 511 | @Override\r |
| 512 | public void run() {\r |
| 513 | toggleBookmark(fRank);\r |
| 514 | }\r |
| 515 | }\r |
| 516 | \r |
| 517 | final MenuManager tablePopupMenu = new MenuManager();\r |
| 518 | tablePopupMenu.setRemoveAllWhenShown(true);\r |
| 519 | tablePopupMenu.addMenuListener(new IMenuListener() {\r |
| 520 | @Override\r |
| 521 | public void menuAboutToShow(IMenuManager manager) {\r |
| 522 | if (fTable.getSelectionIndex() == 0) {\r |
| 523 | // Right-click on header row\r |
| 524 | if (fHeaderState == HeaderState.FILTER) {\r |
| 525 | tablePopupMenu.add(showSearchBarAction);\r |
| 526 | } else {\r |
| 527 | tablePopupMenu.add(showFilterBarAction);\r |
| 528 | }\r |
| 529 | return;\r |
| 530 | }\r |
| 531 | Point point = fTable.toControl(Display.getDefault().getCursorLocation());\r |
| 532 | TableItem item = fTable.getSelection().length > 0 ? fTable.getSelection()[0] : null;\r |
| 533 | if (item != null) {\r |
| 534 | Rectangle imageBounds = item.getImageBounds(0);\r |
| 535 | imageBounds.width = BOOKMARK_IMAGE.getBounds().width;\r |
| 536 | if (point.x <= imageBounds.x + imageBounds.width) {\r |
| 537 | // Right-click on left margin\r |
| 538 | Long rank = (Long) item.getData(Key.RANK);\r |
| 539 | if (rank != null && fBookmarksFile != null) {\r |
| 540 | if (fBookmarksMap.containsKey(rank)) {\r |
| 541 | tablePopupMenu.add(new ToggleBookmarkAction(\r |
| 542 | Messages.TmfEventsTable_RemoveBookmarkActionText, rank));\r |
| 543 | } else {\r |
| 544 | tablePopupMenu.add(new ToggleBookmarkAction(\r |
| 545 | Messages.TmfEventsTable_AddBookmarkActionText, rank));\r |
| 546 | }\r |
| 547 | }\r |
| 548 | return;\r |
| 549 | }\r |
| 550 | }\r |
| 551 | // Right-click on table\r |
| 552 | if (fTable.isVisible() && fRawViewer.isVisible()) {\r |
| 553 | tablePopupMenu.add(hideTableAction);\r |
| 554 | tablePopupMenu.add(hideRawAction);\r |
| 555 | } else if (!fTable.isVisible()) {\r |
| 556 | tablePopupMenu.add(showTableAction);\r |
| 557 | } else if (!fRawViewer.isVisible()) {\r |
| 558 | tablePopupMenu.add(showRawAction);\r |
| 559 | }\r |
| 560 | tablePopupMenu.add(new Separator());\r |
| 561 | tablePopupMenu.add(clearFiltersAction);\r |
| 562 | ITmfFilterTreeNode[] savedFilters = FilterManager.getSavedFilters();\r |
| 563 | if (savedFilters.length > 0) {\r |
| 564 | MenuManager subMenu = new MenuManager(Messages.TmfEventsTable_ApplyPresetFilterMenuName);\r |
| 565 | for (ITmfFilterTreeNode node : savedFilters) {\r |
| 566 | if (node instanceof TmfFilterNode) {\r |
| 567 | final TmfFilterNode filter = (TmfFilterNode) node;\r |
| 568 | subMenu.add(new Action(filter.getFilterName()) {\r |
| 569 | @Override\r |
| 570 | public void run() {\r |
| 571 | stopFilterThread();\r |
| 572 | fFilterMatchCount = 0;\r |
| 573 | fFilterCheckCount = 0;\r |
| 574 | fCache.applyFilter(filter);\r |
| 575 | fTable.clearAll();\r |
| 576 | fTable.setData(Key.FILTER_OBJ, filter);\r |
| 577 | fTable.setItemCount(3); // +1 for header row, +2 for top and bottom filter status\r |
| 578 | // rows\r |
| 579 | startFilterThread();\r |
| 580 | fireFilterApplied(filter);\r |
| 581 | }\r |
| 582 | });\r |
| 583 | }\r |
| 584 | }\r |
| 585 | tablePopupMenu.add(subMenu);\r |
| 586 | }\r |
| 587 | appendToTablePopupMenu(tablePopupMenu, item);\r |
| 588 | }\r |
| 589 | });\r |
| 590 | \r |
| 591 | final MenuManager rawViewerPopupMenu = new MenuManager();\r |
| 592 | rawViewerPopupMenu.setRemoveAllWhenShown(true);\r |
| 593 | rawViewerPopupMenu.addMenuListener(new IMenuListener() {\r |
| 594 | @Override\r |
| 595 | public void menuAboutToShow(IMenuManager manager) {\r |
| 596 | if (fTable.isVisible() && fRawViewer.isVisible()) {\r |
| 597 | rawViewerPopupMenu.add(hideTableAction);\r |
| 598 | rawViewerPopupMenu.add(hideRawAction);\r |
| 599 | } else if (!fTable.isVisible()) {\r |
| 600 | rawViewerPopupMenu.add(showTableAction);\r |
| 601 | } else if (!fRawViewer.isVisible()) {\r |
| 602 | rawViewerPopupMenu.add(showRawAction);\r |
| 603 | }\r |
| 604 | appendToRawPopupMenu(tablePopupMenu);\r |
| 605 | }\r |
| 606 | });\r |
| 607 | \r |
| 608 | Menu menu = tablePopupMenu.createContextMenu(fTable);\r |
| 609 | fTable.setMenu(menu);\r |
| 610 | \r |
| 611 | menu = rawViewerPopupMenu.createContextMenu(fRawViewer);\r |
| 612 | fRawViewer.setMenu(menu);\r |
| 613 | }\r |
| 614 | \r |
| 615 | protected void appendToTablePopupMenu(MenuManager tablePopupMenu, TableItem selectedItem) {\r |
| 616 | // override to append more actions\r |
| 617 | }\r |
| 618 | \r |
| 619 | protected void appendToRawPopupMenu(MenuManager rawViewerPopupMenu) {\r |
| 620 | // override to append more actions\r |
| 621 | }\r |
| 622 | \r |
| 623 | @Override\r |
| 624 | public void dispose() {\r |
| 625 | stopSearchThread();\r |
| 626 | stopFilterThread();\r |
| 627 | ColorSettingsManager.removeColorSettingsListener(this);\r |
| 628 | fComposite.dispose();\r |
| 629 | if (fTrace != null && fDisposeOnClose) {\r |
| 630 | fTrace.dispose();\r |
| 631 | }\r |
| 632 | fResourceManager.dispose();\r |
| 633 | super.dispose();\r |
| 634 | }\r |
| 635 | \r |
| 636 | public void setLayoutData(Object layoutData) {\r |
| 637 | fComposite.setLayoutData(layoutData);\r |
| 638 | }\r |
| 639 | \r |
| 640 | public TmfVirtualTable getTable() {\r |
| 641 | return fTable;\r |
| 642 | }\r |
| 643 | \r |
| 644 | /**\r |
| 645 | * @param table\r |
| 646 | * \r |
| 647 | * FIXME: Add support for column selection\r |
| 648 | */\r |
| 649 | protected void setColumnHeaders(ColumnData[] columnData) {\r |
| 650 | fTable.setColumnHeaders(columnData);\r |
| 651 | }\r |
| 652 | \r |
| 653 | protected void setItemData(TableItem item, TmfEvent event, long rank) {\r |
| 654 | ITmfEventField[] fields = extractItemFields(event);\r |
| 655 | String[] content = new String[fields.length];\r |
| 656 | for (int i = 0; i < fields.length; i++) {\r |
| 657 | content[i] = fields[i].getValue() != null ? fields[i].getValue().toString() : ""; //$NON-NLS-1$\r |
| 658 | }\r |
| 659 | item.setText(content);\r |
| 660 | item.setData(Key.TIMESTAMP, new TmfTimestamp(event.getTimestamp()));\r |
| 661 | item.setData(Key.RANK, rank);\r |
| 662 | \r |
| 663 | boolean bookmark = false;\r |
| 664 | Long markerId = fBookmarksMap.get(rank);\r |
| 665 | if (markerId != null) {\r |
| 666 | bookmark = true;\r |
| 667 | try {\r |
| 668 | IMarker marker = fBookmarksFile.findMarker(markerId);\r |
| 669 | item.setData(Key.BOOKMARK, marker.getAttribute(IMarker.MESSAGE));\r |
| 670 | } catch (CoreException e) {\r |
| 671 | displayException(e);\r |
| 672 | }\r |
| 673 | } else {\r |
| 674 | item.setData(Key.BOOKMARK, null);\r |
| 675 | }\r |
| 676 | \r |
| 677 | boolean searchMatch = false;\r |
| 678 | boolean searchNoMatch = false;\r |
| 679 | ITmfFilter searchFilter = (ITmfFilter) fTable.getData(Key.SEARCH_OBJ);\r |
| 680 | if (searchFilter != null) {\r |
| 681 | if (searchFilter.matches(event)) {\r |
| 682 | searchMatch = true;\r |
| 683 | } else {\r |
| 684 | searchNoMatch = true;\r |
| 685 | }\r |
| 686 | }\r |
| 687 | \r |
| 688 | ColorSetting colorSetting = ColorSettingsManager.getColorSetting(event);\r |
| 689 | if (searchNoMatch) {\r |
| 690 | item.setForeground(colorSetting.getDimmedForegroundColor());\r |
| 691 | item.setBackground(colorSetting.getDimmedBackgroundColor());\r |
| 692 | } else {\r |
| 693 | item.setForeground(colorSetting.getForegroundColor());\r |
| 694 | item.setBackground(colorSetting.getBackgroundColor());\r |
| 695 | }\r |
| 696 | \r |
| 697 | if (searchMatch) {\r |
| 698 | if (bookmark) {\r |
| 699 | item.setImage(SEARCH_MATCH_BOOKMARK_IMAGE);\r |
| 700 | } else {\r |
| 701 | item.setImage(SEARCH_MATCH_IMAGE);\r |
| 702 | }\r |
| 703 | } else if (bookmark) {\r |
| 704 | item.setImage(BOOKMARK_IMAGE);\r |
| 705 | } else {\r |
| 706 | item.setImage((Image) null);\r |
| 707 | }\r |
| 708 | }\r |
| 709 | \r |
| 710 | protected void setHeaderRowItemData(TableItem item) {\r |
| 711 | String txtKey = null;\r |
| 712 | if (fHeaderState == HeaderState.SEARCH) {\r |
| 713 | item.setImage(SEARCH_IMAGE);\r |
| 714 | txtKey = Key.SEARCH_TXT;\r |
| 715 | } else if (fHeaderState == HeaderState.FILTER) {\r |
| 716 | item.setImage(FILTER_IMAGE);\r |
| 717 | txtKey = Key.FILTER_TXT;\r |
| 718 | }\r |
| 719 | item.setForeground(fGrayColor);\r |
| 720 | for (int i = 0; i < fTable.getColumns().length; i++) {\r |
| 721 | TableColumn column = fTable.getColumns()[i];\r |
| 722 | String filter = (String) column.getData(txtKey);\r |
| 723 | if (filter == null) {\r |
| 724 | if (fHeaderState == HeaderState.SEARCH) {\r |
| 725 | item.setText(i, SEARCH_HINT);\r |
| 726 | } else if (fHeaderState == HeaderState.FILTER) {\r |
| 727 | item.setText(i, FILTER_HINT);\r |
| 728 | }\r |
| 729 | item.setForeground(i, fGrayColor);\r |
| 730 | item.setFont(i, fTable.getFont());\r |
| 731 | } else {\r |
| 732 | item.setText(i, filter);\r |
| 733 | item.setForeground(i, fGreenColor);\r |
| 734 | item.setFont(i, fBoldFont);\r |
| 735 | }\r |
| 736 | }\r |
| 737 | }\r |
| 738 | \r |
| 739 | protected void setFilterStatusRowItemData(TableItem item) {\r |
| 740 | for (int i = 0; i < fTable.getColumns().length; i++) {\r |
| 741 | if (i == 0) {\r |
| 742 | if (fTrace == null || fFilterCheckCount == fTrace.getNbEvents()) {\r |
| 743 | item.setImage(FILTER_IMAGE);\r |
| 744 | } else {\r |
| 745 | item.setImage(STOP_IMAGE);\r |
| 746 | }\r |
| 747 | item.setText(0, fFilterMatchCount + "/" + fFilterCheckCount); //$NON-NLS-1$\r |
| 748 | } else {\r |
| 749 | item.setText(i, ""); //$NON-NLS-1$\r |
| 750 | }\r |
| 751 | }\r |
| 752 | item.setData(Key.TIMESTAMP, null);\r |
| 753 | item.setData(Key.RANK, null);\r |
| 754 | item.setForeground(null);\r |
| 755 | item.setBackground(null);\r |
| 756 | }\r |
| 757 | \r |
| 758 | protected void createHeaderEditor() {\r |
| 759 | final TableEditor tableEditor = fTable.createTableEditor();\r |
| 760 | tableEditor.horizontalAlignment = SWT.LEFT;\r |
| 761 | tableEditor.verticalAlignment = SWT.CENTER;\r |
| 762 | tableEditor.grabHorizontal = true;\r |
| 763 | tableEditor.minimumWidth = 50;\r |
| 764 | \r |
| 765 | // Handle the header row selection\r |
| 766 | fTable.addMouseListener(new MouseAdapter() {\r |
| 767 | int columnIndex;\r |
| 768 | TableColumn column;\r |
| 769 | TableItem item;\r |
| 770 | \r |
| 771 | @Override\r |
| 772 | public void mouseDown(MouseEvent event) {\r |
| 773 | if (event.button != 1) {\r |
| 774 | return;\r |
| 775 | }\r |
| 776 | // Identify the selected row\r |
| 777 | Point point = new Point(event.x, event.y);\r |
| 778 | item = fTable.getItem(point);\r |
| 779 | \r |
| 780 | // Header row selected\r |
| 781 | if (item != null && fTable.indexOf(item) == 0) {\r |
| 782 | \r |
| 783 | // Icon selected\r |
| 784 | if (item.getImageBounds(0).contains(point)) {\r |
| 785 | if (fHeaderState == HeaderState.SEARCH) {\r |
| 786 | fHeaderState = HeaderState.FILTER;\r |
| 787 | } else if (fHeaderState == HeaderState.FILTER) {\r |
| 788 | fHeaderState = HeaderState.SEARCH;\r |
| 789 | }\r |
| 790 | fTable.refresh();\r |
| 791 | return;\r |
| 792 | }\r |
| 793 | \r |
| 794 | // Identify the selected column\r |
| 795 | columnIndex = -1;\r |
| 796 | for (int i = 0; i < fTable.getColumns().length; i++) {\r |
| 797 | Rectangle rect = item.getBounds(i);\r |
| 798 | if (rect.contains(point)) {\r |
| 799 | columnIndex = i;\r |
| 800 | break;\r |
| 801 | }\r |
| 802 | }\r |
| 803 | \r |
| 804 | if (columnIndex == -1) {\r |
| 805 | return;\r |
| 806 | }\r |
| 807 | \r |
| 808 | column = fTable.getColumns()[columnIndex];\r |
| 809 | \r |
| 810 | String txtKey = null;\r |
| 811 | if (fHeaderState == HeaderState.SEARCH) {\r |
| 812 | txtKey = Key.SEARCH_TXT;\r |
| 813 | } else if (fHeaderState == HeaderState.FILTER) {\r |
| 814 | txtKey = Key.FILTER_TXT;\r |
| 815 | }\r |
| 816 | \r |
| 817 | // The control that will be the editor must be a child of the Table\r |
| 818 | final Text newEditor = (Text) fTable.createTableEditorControl(Text.class);\r |
| 819 | String headerString = (String) column.getData(txtKey);\r |
| 820 | if (headerString != null) {\r |
| 821 | newEditor.setText(headerString);\r |
| 822 | }\r |
| 823 | newEditor.addFocusListener(new FocusAdapter() {\r |
| 824 | @Override\r |
| 825 | public void focusLost(FocusEvent e) {\r |
| 826 | boolean changed = updateHeader(newEditor.getText());\r |
| 827 | if (changed) {\r |
| 828 | applyHeader();\r |
| 829 | }\r |
| 830 | }\r |
| 831 | });\r |
| 832 | newEditor.addKeyListener(new KeyAdapter() {\r |
| 833 | @Override\r |
| 834 | public void keyPressed(KeyEvent e) {\r |
| 835 | if (e.character == SWT.CR) {\r |
| 836 | updateHeader(newEditor.getText());\r |
| 837 | applyHeader();\r |
| 838 | } else if (e.character == SWT.ESC) {\r |
| 839 | tableEditor.getEditor().dispose();\r |
| 840 | }\r |
| 841 | }\r |
| 842 | });\r |
| 843 | newEditor.selectAll();\r |
| 844 | newEditor.setFocus();\r |
| 845 | tableEditor.setEditor(newEditor, item, columnIndex);\r |
| 846 | }\r |
| 847 | }\r |
| 848 | \r |
| 849 | /*\r |
| 850 | * returns true is value was changed\r |
| 851 | */\r |
| 852 | private boolean updateHeader(String text) {\r |
| 853 | String objKey = null;\r |
| 854 | String txtKey = null;\r |
| 855 | if (fHeaderState == HeaderState.SEARCH) {\r |
| 856 | objKey = Key.SEARCH_OBJ;\r |
| 857 | txtKey = Key.SEARCH_TXT;\r |
| 858 | } else if (fHeaderState == HeaderState.FILTER) {\r |
| 859 | objKey = Key.FILTER_OBJ;\r |
| 860 | txtKey = Key.FILTER_TXT;\r |
| 861 | }\r |
| 862 | if (text.trim().length() > 0) {\r |
| 863 | try {\r |
| 864 | String regex = TmfFilterMatchesNode.regexFix(text);\r |
| 865 | Pattern.compile(regex);\r |
| 866 | if (regex.equals(column.getData(txtKey))) {\r |
| 867 | tableEditor.getEditor().dispose();\r |
| 868 | return false;\r |
| 869 | }\r |
| 870 | TmfFilterMatchesNode filter = new TmfFilterMatchesNode(null);\r |
| 871 | String fieldId = (String) column.getData(Key.FIELD_ID);\r |
| 872 | if (fieldId == null) {\r |
| 873 | fieldId = column.getText();\r |
| 874 | }\r |
| 875 | filter.setField(fieldId);\r |
| 876 | filter.setRegex(regex);\r |
| 877 | column.setData(objKey, filter);\r |
| 878 | column.setData(txtKey, regex);\r |
| 879 | } catch (PatternSyntaxException ex) {\r |
| 880 | tableEditor.getEditor().dispose();\r |
| 881 | MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),\r |
| 882 | ex.getDescription(), ex.getMessage());\r |
| 883 | return false;\r |
| 884 | }\r |
| 885 | } else {\r |
| 886 | if (column.getData(txtKey) == null) {\r |
| 887 | tableEditor.getEditor().dispose();\r |
| 888 | return false;\r |
| 889 | }\r |
| 890 | column.setData(objKey, null);\r |
| 891 | column.setData(txtKey, null);\r |
| 892 | }\r |
| 893 | return true;\r |
| 894 | }\r |
| 895 | \r |
| 896 | private void applyHeader() {\r |
| 897 | stopSearchThread();\r |
| 898 | if (fHeaderState == HeaderState.SEARCH) {\r |
| 899 | final TmfFilterAndNode filter = new TmfFilterAndNode(null);\r |
| 900 | for (TableColumn column : fTable.getColumns()) {\r |
| 901 | Object filterObj = column.getData(Key.SEARCH_OBJ);\r |
| 902 | if (filterObj instanceof ITmfFilterTreeNode) {\r |
| 903 | filter.addChild((ITmfFilterTreeNode) filterObj);\r |
| 904 | }\r |
| 905 | }\r |
| 906 | if (filter.getChildrenCount() > 0) {\r |
| 907 | fTable.setData(Key.SEARCH_OBJ, filter);\r |
| 908 | fTable.refresh();\r |
| 909 | searchNext();\r |
| 910 | fireSearchApplied(filter);\r |
| 911 | } else {\r |
| 912 | fTable.setData(Key.SEARCH_OBJ, null);\r |
| 913 | fTable.refresh();\r |
| 914 | fireSearchApplied(null);\r |
| 915 | }\r |
| 916 | } else if (fHeaderState == HeaderState.FILTER) {\r |
| 917 | stopFilterThread();\r |
| 918 | fFilterMatchCount = 0;\r |
| 919 | fFilterCheckCount = 0;\r |
| 920 | TmfFilterAndNode filter = new TmfFilterAndNode(null);\r |
| 921 | for (TableColumn column : fTable.getColumns()) {\r |
| 922 | Object filterObj = column.getData(Key.FILTER_OBJ);\r |
| 923 | if (filterObj instanceof ITmfFilterTreeNode) {\r |
| 924 | filter.addChild((ITmfFilterTreeNode) filterObj);\r |
| 925 | }\r |
| 926 | }\r |
| 927 | if (filter.getChildrenCount() > 0) {\r |
| 928 | fCache.applyFilter(filter);\r |
| 929 | fTable.clearAll();\r |
| 930 | fTable.setData(Key.FILTER_OBJ, filter);\r |
| 931 | fTable.setItemCount(3); // +1 for header row, +2 for top and bottom filter status rows\r |
| 932 | startFilterThread();\r |
| 933 | fireFilterApplied(filter);\r |
| 934 | } else {\r |
| 935 | fCache.clearFilter();\r |
| 936 | stopFilterThread();\r |
| 937 | fTable.clearAll();\r |
| 938 | fTable.setData(Key.FILTER_OBJ, null);\r |
| 939 | if (fTrace != null) {\r |
| 940 | fTable.setItemCount((int) fTrace.getNbEvents() + 1); // +1 for header row\r |
| 941 | } else {\r |
| 942 | fTable.setItemCount(1); // +1 for header row\r |
| 943 | }\r |
| 944 | fireFilterApplied(null);\r |
| 945 | }\r |
| 946 | }\r |
| 947 | \r |
| 948 | tableEditor.getEditor().dispose();\r |
| 949 | }\r |
| 950 | });\r |
| 951 | \r |
| 952 | fTable.addKeyListener(new KeyAdapter() {\r |
| 953 | @Override\r |
| 954 | public void keyPressed(KeyEvent e) {\r |
| 955 | e.doit = false;\r |
| 956 | if (e.character == SWT.ESC) {\r |
| 957 | stopFilterThread();\r |
| 958 | stopSearchThread();\r |
| 959 | fTable.refresh();\r |
| 960 | } else if (e.character == SWT.DEL) {\r |
| 961 | stopFilterThread();\r |
| 962 | stopSearchThread();\r |
| 963 | if (fHeaderState == HeaderState.SEARCH) {\r |
| 964 | for (TableColumn column : fTable.getColumns()) {\r |
| 965 | column.setData(Key.SEARCH_OBJ, null);\r |
| 966 | column.setData(Key.SEARCH_TXT, null);\r |
| 967 | }\r |
| 968 | fTable.setData(Key.SEARCH_OBJ, null);\r |
| 969 | fTable.refresh();\r |
| 970 | fireSearchApplied(null);\r |
| 971 | } else if (fHeaderState == HeaderState.FILTER) {\r |
| 972 | clearFilters();\r |
| 973 | }\r |
| 974 | } else if (e.character == SWT.CR) {\r |
| 975 | if ((e.stateMask & SWT.SHIFT) == 0) {\r |
| 976 | searchNext();\r |
| 977 | } else {\r |
| 978 | searchPrevious();\r |
| 979 | }\r |
| 980 | }\r |
| 981 | }\r |
| 982 | });\r |
| 983 | }\r |
| 984 | \r |
| 985 | protected void fireFilterApplied(ITmfFilter filter) {\r |
| 986 | for (ITmfEventsFilterListener listener : fEventsFilterListeners) {\r |
| 987 | listener.filterApplied(filter, fTrace);\r |
| 988 | }\r |
| 989 | }\r |
| 990 | \r |
| 991 | protected void fireSearchApplied(ITmfFilter filter) {\r |
| 992 | for (ITmfEventsFilterListener listener : fEventsFilterListeners) {\r |
| 993 | listener.searchApplied(filter, fTrace);\r |
| 994 | }\r |
| 995 | }\r |
| 996 | \r |
| 997 | protected void startFilterThread() {\r |
| 998 | synchronized (fFilterSyncObj) {\r |
| 999 | if (fFilterThread != null) {\r |
| 1000 | fFilterThread.cancel();\r |
| 1001 | }\r |
| 1002 | final ITmfFilterTreeNode filter = (ITmfFilterTreeNode) fTable.getData(Key.FILTER_OBJ);\r |
| 1003 | fFilterThread = new FilterThread(filter);\r |
| 1004 | fFilterThread.start();\r |
| 1005 | }\r |
| 1006 | }\r |
| 1007 | \r |
| 1008 | protected void stopFilterThread() {\r |
| 1009 | synchronized (fFilterSyncObj) {\r |
| 1010 | if (fFilterThread != null) {\r |
| 1011 | fFilterThread.cancel();\r |
| 1012 | }\r |
| 1013 | }\r |
| 1014 | }\r |
| 1015 | \r |
| 1016 | protected void clearFilters() {\r |
| 1017 | if (fTable.getData(Key.FILTER_OBJ) == null) {\r |
| 1018 | return;\r |
| 1019 | }\r |
| 1020 | fCache.clearFilter();\r |
| 1021 | fTable.clearAll();\r |
| 1022 | for (TableColumn column : fTable.getColumns()) {\r |
| 1023 | column.setData(Key.FILTER_OBJ, null);\r |
| 1024 | column.setData(Key.FILTER_TXT, null);\r |
| 1025 | }\r |
| 1026 | fTable.setData(Key.FILTER_OBJ, null);\r |
| 1027 | if (fTrace != null) {\r |
| 1028 | fTable.setItemCount((int) fTrace.getNbEvents() + 1); // +1 for header row\r |
| 1029 | } else {\r |
| 1030 | fTable.setItemCount(1); // +1 for header row\r |
| 1031 | }\r |
| 1032 | fFilterMatchCount = 0;\r |
| 1033 | fFilterCheckCount = 0;\r |
| 1034 | if (fSelectedRank >= 0) {\r |
| 1035 | fTable.setSelection((int) fSelectedRank + 1); // +1 for header row\r |
| 1036 | } else {\r |
| 1037 | fTable.setSelection(0);\r |
| 1038 | }\r |
| 1039 | fireFilterApplied(null);\r |
| 1040 | }\r |
| 1041 | \r |
| 1042 | protected class FilterThread extends Thread {\r |
| 1043 | private final ITmfFilterTreeNode filter;\r |
| 1044 | private TmfEventRequest<TmfEvent> request;\r |
| 1045 | private boolean refreshBusy = false;\r |
| 1046 | private boolean refreshPending = false;\r |
| 1047 | private Object syncObj = new Object();\r |
| 1048 | \r |
| 1049 | public FilterThread(ITmfFilterTreeNode filter) {\r |
| 1050 | super("Filter Thread"); //$NON-NLS-1$\r |
| 1051 | this.filter = filter;\r |
| 1052 | }\r |
| 1053 | \r |
| 1054 | @SuppressWarnings("unchecked")\r |
| 1055 | @Override\r |
| 1056 | public void run() {\r |
| 1057 | if (fTrace == null) {\r |
| 1058 | return;\r |
| 1059 | }\r |
| 1060 | int nbRequested = (int) (fTrace.getNbEvents() - fFilterCheckCount);\r |
| 1061 | if (nbRequested <= 0) {\r |
| 1062 | return;\r |
| 1063 | }\r |
| 1064 | request = new TmfEventRequest<TmfEvent>(TmfEvent.class, TmfTimeRange.ETERNITY, (int) fFilterCheckCount,\r |
| 1065 | nbRequested, fTrace.getCacheSize(), ExecutionType.BACKGROUND) {\r |
| 1066 | @Override\r |
| 1067 | public void handleData(TmfEvent event) {\r |
| 1068 | super.handleData(event);\r |
| 1069 | if (request.isCancelled())\r |
| 1070 | return;\r |
| 1071 | if (filter.matches(event)) {\r |
| 1072 | long rank = fFilterCheckCount;\r |
| 1073 | int index = (int) fFilterMatchCount;\r |
| 1074 | fFilterMatchCount++;\r |
| 1075 | fCache.storeEvent(event.clone(), rank, index);\r |
| 1076 | refreshTable();\r |
| 1077 | } else if (fFilterCheckCount % 100 == 0) {\r |
| 1078 | refreshTable();\r |
| 1079 | }\r |
| 1080 | fFilterCheckCount++;\r |
| 1081 | }\r |
| 1082 | };\r |
| 1083 | ((ITmfDataProvider<TmfEvent>) fTrace).sendRequest(request);\r |
| 1084 | try {\r |
| 1085 | request.waitForCompletion();\r |
| 1086 | } catch (InterruptedException e) {\r |
| 1087 | }\r |
| 1088 | refreshTable();\r |
| 1089 | }\r |
| 1090 | \r |
| 1091 | public void refreshTable() {\r |
| 1092 | synchronized (syncObj) {\r |
| 1093 | if (refreshBusy) {\r |
| 1094 | refreshPending = true;\r |
| 1095 | return;\r |
| 1096 | } else {\r |
| 1097 | refreshBusy = true;\r |
| 1098 | }\r |
| 1099 | }\r |
| 1100 | Display.getDefault().asyncExec(new Runnable() {\r |
| 1101 | @Override\r |
| 1102 | public void run() {\r |
| 1103 | if (request.isCancelled())\r |
| 1104 | return;\r |
| 1105 | if (fTable.isDisposed())\r |
| 1106 | return;\r |
| 1107 | fTable.setItemCount((int) fFilterMatchCount + 3); // +1 for header row, +2 for top and bottom filter\r |
| 1108 | // status rows\r |
| 1109 | fTable.refresh();\r |
| 1110 | synchronized (syncObj) {\r |
| 1111 | refreshBusy = false;\r |
| 1112 | if (refreshPending) {\r |
| 1113 | refreshPending = false;\r |
| 1114 | refreshTable();\r |
| 1115 | }\r |
| 1116 | }\r |
| 1117 | }\r |
| 1118 | });\r |
| 1119 | }\r |
| 1120 | \r |
| 1121 | public void cancel() {\r |
| 1122 | if (request != null) {\r |
| 1123 | request.cancel();\r |
| 1124 | }\r |
| 1125 | }\r |
| 1126 | }\r |
| 1127 | \r |
| 1128 | protected void searchNext() {\r |
| 1129 | synchronized (fSearchSyncObj) {\r |
| 1130 | if (fSearchThread != null) {\r |
| 1131 | return;\r |
| 1132 | }\r |
| 1133 | final ITmfFilterTreeNode searchFilter = (ITmfFilterTreeNode) fTable.getData(Key.SEARCH_OBJ);\r |
| 1134 | if (searchFilter == null) {\r |
| 1135 | return;\r |
| 1136 | }\r |
| 1137 | int selectionIndex = fTable.getSelectionIndex();\r |
| 1138 | int startIndex;\r |
| 1139 | if (selectionIndex > 0) {\r |
| 1140 | startIndex = selectionIndex; // -1 for header row, +1 for next event\r |
| 1141 | } else {\r |
| 1142 | // header row is selected, start at top event\r |
| 1143 | startIndex = Math.max(0, fTable.getTopIndex() - 1); // -1 for header row\r |
| 1144 | }\r |
| 1145 | final ITmfFilterTreeNode eventFilter = (ITmfFilterTreeNode) fTable.getData(Key.FILTER_OBJ);\r |
| 1146 | if (eventFilter != null) {\r |
| 1147 | startIndex = Math.max(0, startIndex - 1); // -1 for top filter status row\r |
| 1148 | }\r |
| 1149 | fSearchThread = new SearchThread(searchFilter, eventFilter, startIndex, fSelectedRank, Direction.FORWARD);\r |
| 1150 | fSearchThread.schedule();\r |
| 1151 | }\r |
| 1152 | }\r |
| 1153 | \r |
| 1154 | protected void searchPrevious() {\r |
| 1155 | synchronized (fSearchSyncObj) {\r |
| 1156 | if (fSearchThread != null) {\r |
| 1157 | return;\r |
| 1158 | }\r |
| 1159 | final ITmfFilterTreeNode searchFilter = (ITmfFilterTreeNode) fTable.getData(Key.SEARCH_OBJ);\r |
| 1160 | if (searchFilter == null) {\r |
| 1161 | return;\r |
| 1162 | }\r |
| 1163 | int selectionIndex = fTable.getSelectionIndex();\r |
| 1164 | int startIndex;\r |
| 1165 | if (selectionIndex > 0) {\r |
| 1166 | startIndex = selectionIndex - 2; // -1 for header row, -1 for previous event\r |
| 1167 | } else {\r |
| 1168 | // header row is selected, start at precedent of top event\r |
| 1169 | startIndex = fTable.getTopIndex() - 2; // -1 for header row, -1 for previous event\r |
| 1170 | }\r |
| 1171 | final ITmfFilterTreeNode eventFilter = (ITmfFilterTreeNode) fTable.getData(Key.FILTER_OBJ);\r |
| 1172 | if (eventFilter != null) {\r |
| 1173 | startIndex = startIndex - 1; // -1 for top filter status row\r |
| 1174 | }\r |
| 1175 | fSearchThread = new SearchThread(searchFilter, eventFilter, startIndex, fSelectedRank, Direction.BACKWARD);\r |
| 1176 | fSearchThread.schedule();\r |
| 1177 | }\r |
| 1178 | }\r |
| 1179 | \r |
| 1180 | protected void stopSearchThread() {\r |
| 1181 | fPendingGotoRank = -1;\r |
| 1182 | synchronized (fSearchSyncObj) {\r |
| 1183 | if (fSearchThread != null) {\r |
| 1184 | fSearchThread.cancel();\r |
| 1185 | fSearchThread = null;\r |
| 1186 | }\r |
| 1187 | }\r |
| 1188 | }\r |
| 1189 | \r |
| 1190 | protected class SearchThread extends Job {\r |
| 1191 | protected ITmfFilterTreeNode searchFilter;\r |
| 1192 | protected ITmfFilterTreeNode eventFilter;\r |
| 1193 | protected int startIndex;\r |
| 1194 | protected int direction;\r |
| 1195 | protected long rank;\r |
| 1196 | protected long foundRank = -1;\r |
| 1197 | protected TmfDataRequest<TmfEvent> request;\r |
| 1198 | \r |
| 1199 | public SearchThread(ITmfFilterTreeNode searchFilter, ITmfFilterTreeNode eventFilter, int startIndex,\r |
| 1200 | long currentRank, int direction) {\r |
| 1201 | super(Messages.TmfEventsTable_SearchingJobName);\r |
| 1202 | this.searchFilter = searchFilter;\r |
| 1203 | this.eventFilter = eventFilter;\r |
| 1204 | this.startIndex = startIndex;\r |
| 1205 | this.rank = currentRank;\r |
| 1206 | this.direction = direction;\r |
| 1207 | }\r |
| 1208 | \r |
| 1209 | @SuppressWarnings("unchecked")\r |
| 1210 | @Override\r |
| 1211 | protected IStatus run(final IProgressMonitor monitor) {\r |
| 1212 | if (fTrace == null) {\r |
| 1213 | return Status.OK_STATUS;\r |
| 1214 | }\r |
| 1215 | final Display display = Display.getDefault();\r |
| 1216 | if (startIndex < 0) {\r |
| 1217 | rank = (int) fTrace.getNbEvents() - 1;\r |
| 1218 | } else if (startIndex >= fTable.getItemCount() - (eventFilter == null ? 1 : 3)) { // -1 for header row, -2\r |
| 1219 | // for top and bottom\r |
| 1220 | // filter status rows\r |
| 1221 | rank = 0;\r |
| 1222 | } else {\r |
| 1223 | int idx = startIndex;\r |
| 1224 | while (foundRank == -1) {\r |
| 1225 | CachedEvent event = fCache.peekEvent(idx);\r |
| 1226 | if (event == null) {\r |
| 1227 | break;\r |
| 1228 | }\r |
| 1229 | rank = event.rank;\r |
| 1230 | if (searchFilter.matches(event.event) && (eventFilter == null || eventFilter.matches(event.event))) {\r |
| 1231 | foundRank = event.rank;\r |
| 1232 | break;\r |
| 1233 | }\r |
| 1234 | if (direction == Direction.FORWARD) {\r |
| 1235 | idx++;\r |
| 1236 | } else {\r |
| 1237 | idx--;\r |
| 1238 | }\r |
| 1239 | }\r |
| 1240 | if (foundRank == -1) {\r |
| 1241 | if (direction == Direction.FORWARD) {\r |
| 1242 | rank++;\r |
| 1243 | if (rank > fTrace.getNbEvents() - 1) {\r |
| 1244 | rank = 0;\r |
| 1245 | }\r |
| 1246 | } else {\r |
| 1247 | rank--;\r |
| 1248 | if (rank < 0) {\r |
| 1249 | rank = (int) fTrace.getNbEvents() - 1;\r |
| 1250 | }\r |
| 1251 | }\r |
| 1252 | }\r |
| 1253 | }\r |
| 1254 | int startRank = (int) rank;\r |
| 1255 | boolean wrapped = false;\r |
| 1256 | while (!monitor.isCanceled() && foundRank == -1 && fTrace != null) {\r |
| 1257 | int nbRequested = (direction == Direction.FORWARD ? Integer.MAX_VALUE : Math.min((int) rank + 1,\r |
| 1258 | fTrace.getCacheSize()));\r |
| 1259 | if (direction == Direction.BACKWARD) {\r |
| 1260 | rank = Math.max(0, rank - fTrace.getCacheSize() + 1);\r |
| 1261 | }\r |
| 1262 | request = new TmfDataRequest<TmfEvent>(TmfEvent.class, (int) rank, nbRequested) {\r |
| 1263 | long currentRank = rank;\r |
| 1264 | \r |
| 1265 | @Override\r |
| 1266 | public void handleData(TmfEvent event) {\r |
| 1267 | super.handleData(event);\r |
| 1268 | if (searchFilter.matches(event) && (eventFilter == null || eventFilter.matches(event))) {\r |
| 1269 | foundRank = currentRank;\r |
| 1270 | if (direction == Direction.FORWARD) {\r |
| 1271 | done();\r |
| 1272 | return;\r |
| 1273 | }\r |
| 1274 | }\r |
| 1275 | currentRank++;\r |
| 1276 | }\r |
| 1277 | };\r |
| 1278 | ((ITmfDataProvider<TmfEvent>) fTrace).sendRequest(request);\r |
| 1279 | try {\r |
| 1280 | request.waitForCompletion();\r |
| 1281 | if (request.isCancelled()) {\r |
| 1282 | return Status.OK_STATUS;\r |
| 1283 | }\r |
| 1284 | } catch (InterruptedException e) {\r |
| 1285 | synchronized (fSearchSyncObj) {\r |
| 1286 | fSearchThread = null;\r |
| 1287 | }\r |
| 1288 | return Status.OK_STATUS;\r |
| 1289 | }\r |
| 1290 | if (foundRank == -1) {\r |
| 1291 | if (direction == Direction.FORWARD) {\r |
| 1292 | if (rank == 0) {\r |
| 1293 | synchronized (fSearchSyncObj) {\r |
| 1294 | fSearchThread = null;\r |
| 1295 | }\r |
| 1296 | return Status.OK_STATUS;\r |
| 1297 | } else {\r |
| 1298 | nbRequested = (int) rank;\r |
| 1299 | rank = 0;\r |
| 1300 | wrapped = true;\r |
| 1301 | }\r |
| 1302 | } else {\r |
| 1303 | rank--;\r |
| 1304 | if (rank < 0) {\r |
| 1305 | rank = (int) fTrace.getNbEvents() - 1;\r |
| 1306 | wrapped = true;\r |
| 1307 | }\r |
| 1308 | if (rank <= startRank && wrapped) {\r |
| 1309 | synchronized (fSearchSyncObj) {\r |
| 1310 | fSearchThread = null;\r |
| 1311 | }\r |
| 1312 | return Status.OK_STATUS;\r |
| 1313 | }\r |
| 1314 | }\r |
| 1315 | }\r |
| 1316 | }\r |
| 1317 | int index = (int) foundRank;\r |
| 1318 | if (eventFilter != null) {\r |
| 1319 | index = fCache.getFilteredEventIndex(foundRank);\r |
| 1320 | }\r |
| 1321 | final int selection = index + 1 + (eventFilter != null ? +1 : 0); // +1 for header row, +1 for top filter\r |
| 1322 | // status row\r |
| 1323 | \r |
| 1324 | display.asyncExec(new Runnable() {\r |
| 1325 | @Override\r |
| 1326 | public void run() {\r |
| 1327 | if (monitor.isCanceled())\r |
| 1328 | return;\r |
| 1329 | if (fTable.isDisposed())\r |
| 1330 | return;\r |
| 1331 | fTable.setSelection(selection);\r |
| 1332 | fSelectedRank = foundRank;\r |
| 1333 | synchronized (fSearchSyncObj) {\r |
| 1334 | fSearchThread = null;\r |
| 1335 | }\r |
| 1336 | }\r |
| 1337 | });\r |
| 1338 | return Status.OK_STATUS;\r |
| 1339 | }\r |
| 1340 | \r |
| 1341 | @Override\r |
| 1342 | protected void canceling() {\r |
| 1343 | request.cancel();\r |
| 1344 | synchronized (fSearchSyncObj) {\r |
| 1345 | fSearchThread = null;\r |
| 1346 | }\r |
| 1347 | }\r |
| 1348 | }\r |
| 1349 | \r |
| 1350 | protected void createResources() {\r |
| 1351 | fGrayColor = fResourceManager.createColor(ColorUtil.blend(fTable.getBackground().getRGB(), fTable\r |
| 1352 | .getForeground().getRGB()));\r |
| 1353 | fGreenColor = fTable.getDisplay().getSystemColor(SWT.COLOR_DARK_GREEN);\r |
| 1354 | fBoldFont = fResourceManager.createFont(FontDescriptor.createFrom(fTable.getFont()).setStyle(SWT.BOLD));\r |
| 1355 | }\r |
| 1356 | \r |
| 1357 | protected void packColumns() {\r |
| 1358 | if (fPackDone)\r |
| 1359 | return;\r |
| 1360 | for (TableColumn column : fTable.getColumns()) {\r |
| 1361 | int headerWidth = column.getWidth();\r |
| 1362 | column.pack();\r |
| 1363 | if (column.getWidth() < headerWidth) {\r |
| 1364 | column.setWidth(headerWidth);\r |
| 1365 | }\r |
| 1366 | }\r |
| 1367 | fPackDone = true;\r |
| 1368 | }\r |
| 1369 | \r |
| 1370 | /**\r |
| 1371 | * @param event\r |
| 1372 | * @return\r |
| 1373 | * \r |
| 1374 | * FIXME: Add support for column selection\r |
| 1375 | */\r |
| 1376 | protected ITmfEventField[] extractItemFields(TmfEvent event) {\r |
| 1377 | ITmfEventField[] fields = new TmfEventField[0];\r |
| 1378 | if (event != null) {\r |
| 1379 | String timestamp = ((Long) event.getTimestamp().getValue()).toString();\r |
| 1380 | String source = event.getSource();\r |
| 1381 | String type = event.getType().getName();\r |
| 1382 | String reference = event.getReference();\r |
| 1383 | ITmfEventField content = event.getContent();\r |
| 1384 | String value = (content.getValue() != null) ? content.getValue().toString() : content.toString();\r |
| 1385 | fields = new TmfEventField[] {\r |
| 1386 | new TmfEventField(ITmfEvent.EVENT_FIELD_TIMESTAMP, timestamp),\r |
| 1387 | new TmfEventField(ITmfEvent.EVENT_FIELD_SOURCE, source),\r |
| 1388 | new TmfEventField(ITmfEvent.EVENT_FIELD_TYPE, type),\r |
| 1389 | new TmfEventField(ITmfEvent.EVENT_FIELD_REFERENCE, reference),\r |
| 1390 | new TmfEventField(ITmfEvent.EVENT_FIELD_CONTENT, value)\r |
| 1391 | };\r |
| 1392 | }\r |
| 1393 | return fields;\r |
| 1394 | }\r |
| 1395 | \r |
| 1396 | public void setFocus() {\r |
| 1397 | fTable.setFocus();\r |
| 1398 | }\r |
| 1399 | \r |
| 1400 | /**\r |
| 1401 | * @param trace\r |
| 1402 | * @param disposeOnClose\r |
| 1403 | * true if the trace should be disposed when the table is disposed\r |
| 1404 | */\r |
| 1405 | public void setTrace(ITmfTrace<?> trace, boolean disposeOnClose) {\r |
| 1406 | if (fTrace != null && fDisposeOnClose) {\r |
| 1407 | fTrace.dispose();\r |
| 1408 | }\r |
| 1409 | fTrace = trace;\r |
| 1410 | fPackDone = false;\r |
| 1411 | fSelectedRank = 0;\r |
| 1412 | fDisposeOnClose = disposeOnClose;\r |
| 1413 | \r |
| 1414 | // Perform the updates on the UI thread\r |
| 1415 | fTable.getDisplay().syncExec(new Runnable() {\r |
| 1416 | @Override\r |
| 1417 | public void run() {\r |
| 1418 | fTable.removeAll();\r |
| 1419 | fCache.setTrace(fTrace); // Clear the cache\r |
| 1420 | if (fTrace != null) {\r |
| 1421 | if (!fTable.isDisposed() && fTrace != null) {\r |
| 1422 | if (fTable.getData(Key.FILTER_OBJ) == null) {\r |
| 1423 | fTable.setItemCount((int) fTrace.getNbEvents() + 1); // +1 for header row\r |
| 1424 | } else {\r |
| 1425 | stopFilterThread();\r |
| 1426 | fFilterMatchCount = 0;\r |
| 1427 | fFilterCheckCount = 0;\r |
| 1428 | fTable.setItemCount(3); // +1 for header row, +2 for top and bottom filter status rows\r |
| 1429 | startFilterThread();\r |
| 1430 | }\r |
| 1431 | }\r |
| 1432 | fRawViewer.setTrace(fTrace);\r |
| 1433 | }\r |
| 1434 | }\r |
| 1435 | });\r |
| 1436 | }\r |
| 1437 | \r |
| 1438 | // ------------------------------------------------------------------------\r |
| 1439 | // Event cache\r |
| 1440 | // ------------------------------------------------------------------------\r |
| 1441 | \r |
| 1442 | public void cacheUpdated(final boolean completed) {\r |
| 1443 | synchronized (fCacheUpdateSyncObj) {\r |
| 1444 | if (fCacheUpdateBusy) {\r |
| 1445 | fCacheUpdatePending = true;\r |
| 1446 | fCacheUpdateCompleted = completed;\r |
| 1447 | return;\r |
| 1448 | } else {\r |
| 1449 | fCacheUpdateBusy = true;\r |
| 1450 | }\r |
| 1451 | }\r |
| 1452 | // Event cache is now updated. Perform update on the UI thread\r |
| 1453 | if (!fTable.isDisposed()) {\r |
| 1454 | fTable.getDisplay().asyncExec(new Runnable() {\r |
| 1455 | @Override\r |
| 1456 | public void run() {\r |
| 1457 | if (!fTable.isDisposed()) {\r |
| 1458 | fTable.refresh();\r |
| 1459 | packColumns();\r |
| 1460 | }\r |
| 1461 | if (completed) {\r |
| 1462 | populateCompleted();\r |
| 1463 | }\r |
| 1464 | synchronized (fCacheUpdateSyncObj) {\r |
| 1465 | fCacheUpdateBusy = false;\r |
| 1466 | if (fCacheUpdatePending) {\r |
| 1467 | fCacheUpdatePending = false;\r |
| 1468 | cacheUpdated(fCacheUpdateCompleted);\r |
| 1469 | }\r |
| 1470 | }\r |
| 1471 | }\r |
| 1472 | });\r |
| 1473 | }\r |
| 1474 | }\r |
| 1475 | \r |
| 1476 | protected void populateCompleted() {\r |
| 1477 | // Nothing by default;\r |
| 1478 | }\r |
| 1479 | \r |
| 1480 | // ------------------------------------------------------------------------\r |
| 1481 | // Bookmark handling\r |
| 1482 | // ------------------------------------------------------------------------\r |
| 1483 | \r |
| 1484 | public void addBookmark(IFile bookmarksFile) {\r |
| 1485 | fBookmarksFile = bookmarksFile;\r |
| 1486 | TableItem[] selection = fTable.getSelection();\r |
| 1487 | if (selection.length > 0) {\r |
| 1488 | TableItem tableItem = selection[0];\r |
| 1489 | if (tableItem.getData(Key.RANK) != null) {\r |
| 1490 | StringBuffer defaultMessage = new StringBuffer();\r |
| 1491 | for (int i = 0; i < fTable.getColumns().length; i++) {\r |
| 1492 | if (i > 0) {\r |
| 1493 | defaultMessage.append(", "); //$NON-NLS-1$\r |
| 1494 | }\r |
| 1495 | defaultMessage.append(tableItem.getText(i));\r |
| 1496 | }\r |
| 1497 | InputDialog dialog = new InputDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),\r |
| 1498 | Messages.TmfEventsTable_AddBookmarkDialogTitle, Messages.TmfEventsTable_AddBookmarkDialogText,\r |
| 1499 | defaultMessage.toString(), null);\r |
| 1500 | if (dialog.open() == Dialog.OK) {\r |
| 1501 | String message = dialog.getValue();\r |
| 1502 | try {\r |
| 1503 | IMarker bookmark = bookmarksFile.createMarker(IMarker.BOOKMARK);\r |
| 1504 | if (bookmark.exists()) {\r |
| 1505 | bookmark.setAttribute(IMarker.MESSAGE, message.toString());\r |
| 1506 | long rank = (Long) tableItem.getData(Key.RANK);\r |
| 1507 | int location = (int) rank;\r |
| 1508 | bookmark.setAttribute(IMarker.LOCATION, (Integer) location);\r |
| 1509 | fBookmarksMap.put(rank, bookmark.getId());\r |
| 1510 | fTable.refresh();\r |
| 1511 | }\r |
| 1512 | } catch (CoreException e) {\r |
| 1513 | displayException(e);\r |
| 1514 | }\r |
| 1515 | }\r |
| 1516 | }\r |
| 1517 | }\r |
| 1518 | \r |
| 1519 | }\r |
| 1520 | \r |
| 1521 | public void removeBookmark(IMarker bookmark) {\r |
| 1522 | for (Entry<Long, Long> entry : fBookmarksMap.entrySet()) {\r |
| 1523 | if (entry.getValue().equals(bookmark.getId())) {\r |
| 1524 | fBookmarksMap.remove(entry.getKey());\r |
| 1525 | fTable.refresh();\r |
| 1526 | return;\r |
| 1527 | }\r |
| 1528 | }\r |
| 1529 | }\r |
| 1530 | \r |
| 1531 | private void toggleBookmark(long rank) {\r |
| 1532 | if (fBookmarksFile == null) {\r |
| 1533 | return;\r |
| 1534 | }\r |
| 1535 | if (fBookmarksMap.containsKey(rank)) {\r |
| 1536 | Long markerId = fBookmarksMap.remove(rank);\r |
| 1537 | fTable.refresh();\r |
| 1538 | try {\r |
| 1539 | IMarker bookmark = fBookmarksFile.findMarker(markerId);\r |
| 1540 | if (bookmark != null) {\r |
| 1541 | bookmark.delete();\r |
| 1542 | }\r |
| 1543 | } catch (CoreException e) {\r |
| 1544 | displayException(e);\r |
| 1545 | }\r |
| 1546 | } else {\r |
| 1547 | addBookmark(fBookmarksFile);\r |
| 1548 | }\r |
| 1549 | }\r |
| 1550 | \r |
| 1551 | public void refreshBookmarks(IFile bookmarksFile) {\r |
| 1552 | fBookmarksFile = bookmarksFile;\r |
| 1553 | try {\r |
| 1554 | fBookmarksMap.clear();\r |
| 1555 | for (IMarker bookmark : bookmarksFile.findMarkers(IMarker.BOOKMARK, false, IResource.DEPTH_ZERO)) {\r |
| 1556 | int location = bookmark.getAttribute(IMarker.LOCATION, -1);\r |
| 1557 | if (location != -1) {\r |
| 1558 | long rank = location;\r |
| 1559 | fBookmarksMap.put(rank, bookmark.getId());\r |
| 1560 | }\r |
| 1561 | }\r |
| 1562 | fTable.refresh();\r |
| 1563 | } catch (CoreException e) {\r |
| 1564 | displayException(e);\r |
| 1565 | }\r |
| 1566 | }\r |
| 1567 | \r |
| 1568 | @Override\r |
| 1569 | public void gotoMarker(IMarker marker) {\r |
| 1570 | int rank = marker.getAttribute(IMarker.LOCATION, -1);\r |
| 1571 | if (rank != -1) {\r |
| 1572 | int index = rank;\r |
| 1573 | if (fTable.getData(Key.FILTER_OBJ) != null) {\r |
| 1574 | index = fCache.getFilteredEventIndex(rank) + 1; // +1 for top filter status row\r |
| 1575 | } else if (rank >= fTable.getItemCount()) {\r |
| 1576 | fPendingGotoRank = rank;\r |
| 1577 | }\r |
| 1578 | fTable.setSelection(index + 1); // +1 for header row\r |
| 1579 | }\r |
| 1580 | }\r |
| 1581 | \r |
| 1582 | // ------------------------------------------------------------------------\r |
| 1583 | // Listeners\r |
| 1584 | // ------------------------------------------------------------------------\r |
| 1585 | \r |
| 1586 | /*\r |
| 1587 | * (non-Javadoc)\r |
| 1588 | * \r |
| 1589 | * @see\r |
| 1590 | * org.eclipse.linuxtools.tmf.ui.views.colors.IColorSettingsListener#colorSettingsChanged(org.eclipse.linuxtools\r |
| 1591 | * .tmf.ui.views.colors.ColorSetting[])\r |
| 1592 | */\r |
| 1593 | @Override\r |
| 1594 | public void colorSettingsChanged(ColorSetting[] colorSettings) {\r |
| 1595 | fTable.refresh();\r |
| 1596 | }\r |
| 1597 | \r |
| 1598 | @Override\r |
| 1599 | public void addEventsFilterListener(ITmfEventsFilterListener listener) {\r |
| 1600 | if (!fEventsFilterListeners.contains(listener)) {\r |
| 1601 | fEventsFilterListeners.add(listener);\r |
| 1602 | }\r |
| 1603 | }\r |
| 1604 | \r |
| 1605 | @Override\r |
| 1606 | public void removeEventsFilterListener(ITmfEventsFilterListener listener) {\r |
| 1607 | fEventsFilterListeners.remove(listener);\r |
| 1608 | }\r |
| 1609 | \r |
| 1610 | // ------------------------------------------------------------------------\r |
| 1611 | // Signal handlers\r |
| 1612 | // ------------------------------------------------------------------------\r |
| 1613 | \r |
| 1614 | @TmfSignalHandler\r |
| 1615 | public void experimentUpdated(TmfExperimentUpdatedSignal signal) {\r |
| 1616 | if ((signal.getExperiment() != fTrace) || fTable.isDisposed())\r |
| 1617 | return;\r |
| 1618 | // Perform the refresh on the UI thread\r |
| 1619 | Display.getDefault().asyncExec(new Runnable() {\r |
| 1620 | @Override\r |
| 1621 | public void run() {\r |
| 1622 | if (!fTable.isDisposed() && fTrace != null) {\r |
| 1623 | if (fTable.getData(Key.FILTER_OBJ) == null) {\r |
| 1624 | fTable.setItemCount((int) fTrace.getNbEvents() + 1); // +1 for header row\r |
| 1625 | if (fPendingGotoRank != -1 && fPendingGotoRank + 1 < fTable.getItemCount()) { // +1 for header row\r |
| 1626 | fTable.setSelection((int) fPendingGotoRank + 1); // +1 for header row\r |
| 1627 | fPendingGotoRank = -1;\r |
| 1628 | }\r |
| 1629 | } else {\r |
| 1630 | startFilterThread();\r |
| 1631 | }\r |
| 1632 | }\r |
| 1633 | if (!fRawViewer.isDisposed() && fTrace != null) {\r |
| 1634 | fRawViewer.refreshEventCount();\r |
| 1635 | }\r |
| 1636 | }\r |
| 1637 | });\r |
| 1638 | }\r |
| 1639 | \r |
| 1640 | @TmfSignalHandler\r |
| 1641 | public void traceUpdated(TmfTraceUpdatedSignal signal) {\r |
| 1642 | if ((signal.getTrace() != fTrace) || fTable.isDisposed())\r |
| 1643 | return;\r |
| 1644 | // Perform the refresh on the UI thread\r |
| 1645 | Display.getDefault().asyncExec(new Runnable() {\r |
| 1646 | @Override\r |
| 1647 | public void run() {\r |
| 1648 | if (!fTable.isDisposed() && fTrace != null) {\r |
| 1649 | if (fTable.getData(Key.FILTER_OBJ) == null) {\r |
| 1650 | fTable.setItemCount((int) fTrace.getNbEvents() + 1); // +1 for header row\r |
| 1651 | if (fPendingGotoRank != -1 && fPendingGotoRank + 1 < fTable.getItemCount()) { // +1 for header row\r |
| 1652 | fTable.setSelection((int) fPendingGotoRank + 1); // +1 for header row\r |
| 1653 | fPendingGotoRank = -1;\r |
| 1654 | }\r |
| 1655 | } else {\r |
| 1656 | startFilterThread();\r |
| 1657 | }\r |
| 1658 | }\r |
| 1659 | if (!fRawViewer.isDisposed() && fTrace != null) {\r |
| 1660 | fRawViewer.refreshEventCount();\r |
| 1661 | }\r |
| 1662 | }\r |
| 1663 | });\r |
| 1664 | }\r |
| 1665 | \r |
| 1666 | @SuppressWarnings("unchecked")\r |
| 1667 | @TmfSignalHandler\r |
| 1668 | public void currentTimeUpdated(final TmfTimeSynchSignal signal) {\r |
| 1669 | if ((signal.getSource() != fTable) && (fTrace != null) && (!fTable.isDisposed())) {\r |
| 1670 | \r |
| 1671 | // Create a request for one event that will be queued after other ongoing requests. When this request is\r |
| 1672 | // completed\r |
| 1673 | // do the work to select the actual event with the timestamp specified in the signal. This procedure\r |
| 1674 | // prevents\r |
| 1675 | // the method fTrace.getRank() from interfering and delaying ongoing requests.\r |
| 1676 | final TmfDataRequest<TmfEvent> subRequest = new TmfDataRequest<TmfEvent>(TmfEvent.class, 0, 1,\r |
| 1677 | ExecutionType.FOREGROUND) {\r |
| 1678 | \r |
| 1679 | TmfTimestamp ts = new TmfTimestamp(signal.getCurrentTime());\r |
| 1680 | \r |
| 1681 | @Override\r |
| 1682 | public void handleData(TmfEvent event) {\r |
| 1683 | super.handleData(event);\r |
| 1684 | }\r |
| 1685 | \r |
| 1686 | @Override\r |
| 1687 | public void handleCompleted() {\r |
| 1688 | super.handleCompleted();\r |
| 1689 | if (fTrace == null) {\r |
| 1690 | return;\r |
| 1691 | }\r |
| 1692 | // Verify if event is within the trace range\r |
| 1693 | final ITmfTimestamp timestamp[] = new TmfTimestamp[1];\r |
| 1694 | timestamp[0] = ts; // signal.getCurrentTime();\r |
| 1695 | if (timestamp[0].compareTo(fTrace.getStartTime(), true) == -1) {\r |
| 1696 | timestamp[0] = fTrace.getStartTime();\r |
| 1697 | }\r |
| 1698 | if (timestamp[0].compareTo(fTrace.getEndTime(), true) == 1) {\r |
| 1699 | timestamp[0] = fTrace.getEndTime();\r |
| 1700 | }\r |
| 1701 | \r |
| 1702 | // Get the rank for the event selection in the table\r |
| 1703 | final long rank = fTrace.getRank(timestamp[0]);\r |
| 1704 | fSelectedRank = rank;\r |
| 1705 | \r |
| 1706 | fTable.getDisplay().asyncExec(new Runnable() {\r |
| 1707 | @Override\r |
| 1708 | public void run() {\r |
| 1709 | // Return if table is disposed\r |
| 1710 | if (fTable.isDisposed())\r |
| 1711 | return;\r |
| 1712 | \r |
| 1713 | int index = (int) rank;\r |
| 1714 | if (fTable.isDisposed())\r |
| 1715 | return;\r |
| 1716 | if (fTable.getData(Key.FILTER_OBJ) != null) {\r |
| 1717 | index = fCache.getFilteredEventIndex(rank) + 1; // +1 for top filter status row\r |
| 1718 | }\r |
| 1719 | fTable.setSelection(index + 1); // +1 for header row\r |
| 1720 | fRawViewer.selectAndReveal(rank);\r |
| 1721 | }\r |
| 1722 | });\r |
| 1723 | }\r |
| 1724 | };\r |
| 1725 | \r |
| 1726 | ((ITmfDataProvider<TmfEvent>) fTrace).sendRequest(subRequest);\r |
| 1727 | }\r |
| 1728 | }\r |
| 1729 | \r |
| 1730 | // ------------------------------------------------------------------------\r |
| 1731 | // Error handling\r |
| 1732 | // ------------------------------------------------------------------------\r |
| 1733 | \r |
| 1734 | /**\r |
| 1735 | * Display an exception in a message box\r |
| 1736 | * \r |
| 1737 | * @param e the exception\r |
| 1738 | */\r |
| 1739 | private void displayException(Exception e) {\r |
| 1740 | MessageBox mb = new MessageBox(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());\r |
| 1741 | mb.setText(e.getClass().getName());\r |
| 1742 | mb.setMessage(e.getMessage());\r |
| 1743 | mb.open();\r |
| 1744 | }\r |
| 1745 | \r |
| 1746 | }\r |