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