| 1 | /***************************************************************************** |
| 2 | * Copyright (c) 2007, 2016 Intel Corporation and others |
| 3 | * |
| 4 | * All rights reserved. This program and the accompanying materials |
| 5 | * are made available under the terms of the Eclipse Public License v1.0 |
| 6 | * which accompanies this distribution, and is available at |
| 7 | * http://www.eclipse.org/legal/epl-v10.html |
| 8 | * |
| 9 | * Contributors: |
| 10 | * Intel Corporation - Initial API and implementation |
| 11 | * Ruslan A. Scherbakov, Intel - Initial API and implementation |
| 12 | * Alvaro Sanchez-Leon, Ericsson - Updated for TMF |
| 13 | * Patrick Tasse, Ericsson - Refactoring |
| 14 | * Geneviève Bastien, École Polytechnique de Montréal - Move code to |
| 15 | * provide base classes for time graph view |
| 16 | * Add display of links between items |
| 17 | * Xavier Raynaud, Kalray - Code optimization |
| 18 | * Generoso Pagano, Inria - Support for drag selection listeners |
| 19 | *****************************************************************************/ |
| 20 | |
| 21 | package org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets; |
| 22 | |
| 23 | import java.util.AbstractMap.SimpleEntry; |
| 24 | import java.util.ArrayList; |
| 25 | import java.util.Arrays; |
| 26 | import java.util.Collections; |
| 27 | import java.util.HashMap; |
| 28 | import java.util.Iterator; |
| 29 | import java.util.LinkedHashMap; |
| 30 | import java.util.LinkedList; |
| 31 | import java.util.List; |
| 32 | import java.util.Map; |
| 33 | import java.util.Map.Entry; |
| 34 | import java.util.Queue; |
| 35 | |
| 36 | import org.eclipse.jdt.annotation.NonNull; |
| 37 | import org.eclipse.jface.action.IStatusLineManager; |
| 38 | import org.eclipse.jface.resource.JFaceResources; |
| 39 | import org.eclipse.jface.resource.LocalResourceManager; |
| 40 | import org.eclipse.jface.viewers.AbstractTreeViewer; |
| 41 | import org.eclipse.jface.viewers.ISelection; |
| 42 | import org.eclipse.jface.viewers.ISelectionChangedListener; |
| 43 | import org.eclipse.jface.viewers.ISelectionProvider; |
| 44 | import org.eclipse.jface.viewers.IStructuredSelection; |
| 45 | import org.eclipse.jface.viewers.SelectionChangedEvent; |
| 46 | import org.eclipse.jface.viewers.StructuredSelection; |
| 47 | import org.eclipse.jface.viewers.ViewerFilter; |
| 48 | import org.eclipse.osgi.util.NLS; |
| 49 | import org.eclipse.swt.SWT; |
| 50 | import org.eclipse.swt.events.FocusEvent; |
| 51 | import org.eclipse.swt.events.FocusListener; |
| 52 | import org.eclipse.swt.events.KeyEvent; |
| 53 | import org.eclipse.swt.events.KeyListener; |
| 54 | import org.eclipse.swt.events.MenuDetectEvent; |
| 55 | import org.eclipse.swt.events.MenuDetectListener; |
| 56 | import org.eclipse.swt.events.MouseEvent; |
| 57 | import org.eclipse.swt.events.MouseListener; |
| 58 | import org.eclipse.swt.events.MouseMoveListener; |
| 59 | import org.eclipse.swt.events.MouseTrackListener; |
| 60 | import org.eclipse.swt.events.MouseWheelListener; |
| 61 | import org.eclipse.swt.events.PaintEvent; |
| 62 | import org.eclipse.swt.events.SelectionListener; |
| 63 | import org.eclipse.swt.events.TraverseEvent; |
| 64 | import org.eclipse.swt.events.TraverseListener; |
| 65 | import org.eclipse.swt.events.TypedEvent; |
| 66 | import org.eclipse.swt.graphics.Color; |
| 67 | import org.eclipse.swt.graphics.Cursor; |
| 68 | import org.eclipse.swt.graphics.Font; |
| 69 | import org.eclipse.swt.graphics.FontData; |
| 70 | import org.eclipse.swt.graphics.GC; |
| 71 | import org.eclipse.swt.graphics.Image; |
| 72 | import org.eclipse.swt.graphics.Point; |
| 73 | import org.eclipse.swt.graphics.Rectangle; |
| 74 | import org.eclipse.swt.widgets.Composite; |
| 75 | import org.eclipse.swt.widgets.Display; |
| 76 | import org.eclipse.swt.widgets.Event; |
| 77 | import org.eclipse.swt.widgets.Listener; |
| 78 | import org.eclipse.swt.widgets.Menu; |
| 79 | import org.eclipse.tracecompass.common.core.math.SaturatedArithmetic; |
| 80 | import org.eclipse.tracecompass.tmf.core.signal.TmfSignalManager; |
| 81 | import org.eclipse.tracecompass.tmf.ui.signal.TmfTimeViewAlignmentInfo; |
| 82 | import org.eclipse.tracecompass.tmf.ui.signal.TmfTimeViewAlignmentSignal; |
| 83 | import org.eclipse.tracecompass.tmf.ui.views.ITmfTimeAligned; |
| 84 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.ITimeGraphColorListener; |
| 85 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.ITimeGraphPresentationProvider; |
| 86 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.ITimeGraphPresentationProvider2; |
| 87 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.ITimeGraphTimeListener; |
| 88 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.ITimeGraphTreeListener; |
| 89 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.StateItem; |
| 90 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.TimeGraphTimeEvent; |
| 91 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.TimeGraphTreeExpansionEvent; |
| 92 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ILinkEvent; |
| 93 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.IMarkerEvent; |
| 94 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeEvent; |
| 95 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeGraphEntry; |
| 96 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.Utils.Resolution; |
| 97 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.Utils.TimeFormat; |
| 98 | |
| 99 | import com.google.common.collect.Iterables; |
| 100 | |
| 101 | /** |
| 102 | * Time graph control implementation |
| 103 | * |
| 104 | * @author Alvaro Sanchez-Leon |
| 105 | * @author Patrick Tasse |
| 106 | */ |
| 107 | public class TimeGraphControl extends TimeGraphBaseControl |
| 108 | implements FocusListener, KeyListener, MouseMoveListener, MouseListener, |
| 109 | MouseWheelListener, MouseTrackListener, TraverseListener, ISelectionProvider, |
| 110 | MenuDetectListener, ITmfTimeGraphDrawingHelper, ITimeGraphColorListener, Listener { |
| 111 | |
| 112 | /** Constant indicating that all levels of the time graph should be expanded */ |
| 113 | public static final int ALL_LEVELS = AbstractTreeViewer.ALL_LEVELS; |
| 114 | |
| 115 | private static final int DRAG_MARGIN = 5; |
| 116 | |
| 117 | private static final int DRAG_NONE = 0; |
| 118 | private static final int DRAG_TRACE_ITEM = 1; |
| 119 | private static final int DRAG_SPLIT_LINE = 2; |
| 120 | private static final int DRAG_ZOOM = 3; |
| 121 | private static final int DRAG_SELECTION = 4; |
| 122 | |
| 123 | private static final int CUSTOM_ITEM_HEIGHT = -1; // get item height from provider |
| 124 | |
| 125 | private static final double ZOOM_FACTOR = 1.5; |
| 126 | private static final double ZOOM_IN_FACTOR = 0.8; |
| 127 | private static final double ZOOM_OUT_FACTOR = 1.25; |
| 128 | |
| 129 | private static final int SNAP_WIDTH = 2; |
| 130 | private static final int ARROW_HOVER_MAX_DIST = 5; |
| 131 | |
| 132 | private static final int NO_STATUS = -1; |
| 133 | private static final int STATUS_WITHOUT_CURSOR_TIME = -2; |
| 134 | |
| 135 | private static final int MAX_LABEL_LENGTH = 256; |
| 136 | |
| 137 | private static final int PPI = 72; // points per inch |
| 138 | private static final int DPI = Display.getDefault().getDPI().y; |
| 139 | |
| 140 | private static final int VERTICAL_ZOOM_DELAY = 400; |
| 141 | |
| 142 | /** Resource manager */ |
| 143 | private LocalResourceManager fResourceManager = new LocalResourceManager(JFaceResources.getResources()); |
| 144 | |
| 145 | /** Color map for event types */ |
| 146 | private Color[] fEventColorMap = null; |
| 147 | |
| 148 | private ITimeDataProvider fTimeProvider; |
| 149 | private IStatusLineManager fStatusLineManager = null; |
| 150 | private TimeGraphScale fTimeGraphScale = null; |
| 151 | |
| 152 | private boolean fIsInFocus = false; |
| 153 | private boolean fMouseOverSplitLine = false; |
| 154 | private int fGlobalItemHeight = CUSTOM_ITEM_HEIGHT; |
| 155 | private int fHeightAdjustment = 0; |
| 156 | private Map<Integer, Font> fFonts = new HashMap<>(); |
| 157 | private boolean fBlendSubPixelEvents = false; |
| 158 | private int fMinimumItemWidth = 0; |
| 159 | private int fTopIndex = 0; |
| 160 | private int fDragState = DRAG_NONE; |
| 161 | private boolean fDragBeginMarker = false; |
| 162 | private int fDragButton; |
| 163 | private int fDragX0 = 0; |
| 164 | private int fDragX = 0; |
| 165 | private boolean fHasNamespaceFocus = false; |
| 166 | private long fDragTime0 = 0; // used to preserve accuracy of modified selection |
| 167 | private int fIdealNameSpace = 0; |
| 168 | private long fTime0bak; |
| 169 | private long fTime1bak; |
| 170 | private ITimeGraphPresentationProvider fTimeGraphProvider = null; |
| 171 | private ItemData fItemData = null; |
| 172 | private List<IMarkerEvent> fMarkers = null; |
| 173 | private boolean fMarkersVisible = true; |
| 174 | private List<SelectionListener> fSelectionListeners; |
| 175 | private List<ITimeGraphTimeListener> fDragSelectionListeners; |
| 176 | private final List<ISelectionChangedListener> fSelectionChangedListeners = new ArrayList<>(); |
| 177 | private final List<ITimeGraphTreeListener> fTreeListeners = new ArrayList<>(); |
| 178 | private final List<MenuDetectListener> fTimeGraphEntryMenuListeners = new ArrayList<>(); |
| 179 | private final List<MenuDetectListener> fTimeEventMenuListeners = new ArrayList<>(); |
| 180 | private final Cursor fDragCursor = Display.getDefault().getSystemCursor(SWT.CURSOR_HAND); |
| 181 | private final Cursor fResizeCursor = Display.getDefault().getSystemCursor(SWT.CURSOR_IBEAM); |
| 182 | private final Cursor fWaitCursor = Display.getDefault().getSystemCursor(SWT.CURSOR_WAIT); |
| 183 | private final Cursor fZoomCursor = Display.getDefault().getSystemCursor(SWT.CURSOR_SIZEWE); |
| 184 | private final List<@NonNull ViewerFilter> fFilters = new ArrayList<>(); |
| 185 | private MenuDetectEvent fPendingMenuDetectEvent = null; |
| 186 | private boolean fGridLinesVisible = true; |
| 187 | private Color fGridLineColor = Display.getDefault().getSystemColor(SWT.COLOR_GRAY); |
| 188 | private boolean fHideArrows = false; |
| 189 | private int fAutoExpandLevel = ALL_LEVELS; |
| 190 | private Entry<ITimeGraphEntry, Integer> fVerticalZoomAlignEntry = null; |
| 191 | private long fVerticalZoomAlignTime = 0; |
| 192 | private int fBorderWidth = 0; |
| 193 | private int fHeaderHeight = 0; |
| 194 | |
| 195 | /** |
| 196 | * Standard constructor |
| 197 | * |
| 198 | * @param parent |
| 199 | * The parent composite object |
| 200 | * @param colors |
| 201 | * The color scheme to use |
| 202 | */ |
| 203 | public TimeGraphControl(Composite parent, TimeGraphColorScheme colors) { |
| 204 | |
| 205 | super(parent, colors, SWT.NO_BACKGROUND | SWT.DOUBLE_BUFFERED); |
| 206 | |
| 207 | fItemData = new ItemData(); |
| 208 | |
| 209 | addFocusListener(this); |
| 210 | addMouseListener(this); |
| 211 | addMouseMoveListener(this); |
| 212 | addMouseTrackListener(this); |
| 213 | addMouseWheelListener(this); |
| 214 | addTraverseListener(this); |
| 215 | addKeyListener(this); |
| 216 | addMenuDetectListener(this); |
| 217 | addListener(SWT.MouseWheel, this); |
| 218 | addDisposeListener((e) -> { |
| 219 | fResourceManager.dispose(); |
| 220 | for (Font font : fFonts.values()) { |
| 221 | font.dispose(); |
| 222 | } |
| 223 | }); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Sets the timegraph provider used by this timegraph viewer. |
| 228 | * |
| 229 | * @param timeGraphProvider the timegraph provider |
| 230 | */ |
| 231 | public void setTimeGraphProvider(ITimeGraphPresentationProvider timeGraphProvider) { |
| 232 | fTimeGraphProvider = timeGraphProvider; |
| 233 | |
| 234 | if (timeGraphProvider instanceof ITimeGraphPresentationProvider2) { |
| 235 | ((ITimeGraphPresentationProvider2) timeGraphProvider).setDrawingHelper(this); |
| 236 | ((ITimeGraphPresentationProvider2) timeGraphProvider).addColorListener(this); |
| 237 | } |
| 238 | |
| 239 | StateItem[] stateItems = fTimeGraphProvider.getStateTable(); |
| 240 | colorSettingsChanged(stateItems); |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Gets the timegraph provider used by this timegraph viewer. |
| 245 | * |
| 246 | * @return the timegraph provider, or <code>null</code> if not set. |
| 247 | */ |
| 248 | public ITimeGraphPresentationProvider getTimeGraphProvider() { |
| 249 | return fTimeGraphProvider; |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Gets the time data provider used by this viewer. |
| 254 | * |
| 255 | * @return The time data provider, or <code>null</code> if not set |
| 256 | * @since 2.1 |
| 257 | */ |
| 258 | public ITimeDataProvider getTimeDataProvider() { |
| 259 | return fTimeProvider; |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Gets the color map used by this timegraph viewer. |
| 264 | * |
| 265 | * @return a color map, or <code>null</code> if not set. |
| 266 | */ |
| 267 | public Color[] getEventColorMap() { |
| 268 | return fEventColorMap; |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Assign the given time provider |
| 273 | * |
| 274 | * @param timeProvider |
| 275 | * The time provider |
| 276 | */ |
| 277 | public void setTimeProvider(ITimeDataProvider timeProvider) { |
| 278 | fTimeProvider = timeProvider; |
| 279 | redraw(); |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Assign the status line manager |
| 284 | * |
| 285 | * @param statusLineManager |
| 286 | * The status line manager, or null to disable status line messages |
| 287 | */ |
| 288 | public void setStatusLineManager(IStatusLineManager statusLineManager) { |
| 289 | if (fStatusLineManager != null && statusLineManager == null) { |
| 290 | fStatusLineManager.setMessage(""); //$NON-NLS-1$ |
| 291 | } |
| 292 | fStatusLineManager = statusLineManager; |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Assign the time graph scale |
| 297 | * |
| 298 | * @param timeGraphScale |
| 299 | * The time graph scale |
| 300 | */ |
| 301 | public void setTimeGraphScale(TimeGraphScale timeGraphScale) { |
| 302 | fTimeGraphScale = timeGraphScale; |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Add a selection listener |
| 307 | * |
| 308 | * @param listener |
| 309 | * The listener to add |
| 310 | */ |
| 311 | public void addSelectionListener(SelectionListener listener) { |
| 312 | if (listener == null) { |
| 313 | SWT.error(SWT.ERROR_NULL_ARGUMENT); |
| 314 | } |
| 315 | if (null == fSelectionListeners) { |
| 316 | fSelectionListeners = new ArrayList<>(); |
| 317 | } |
| 318 | fSelectionListeners.add(listener); |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * Remove a selection listener |
| 323 | * |
| 324 | * @param listener |
| 325 | * The listener to remove |
| 326 | */ |
| 327 | public void removeSelectionListener(SelectionListener listener) { |
| 328 | if (null != fSelectionListeners) { |
| 329 | fSelectionListeners.remove(listener); |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Selection changed callback |
| 335 | */ |
| 336 | public void fireSelectionChanged() { |
| 337 | if (null != fSelectionListeners) { |
| 338 | Iterator<SelectionListener> it = fSelectionListeners.iterator(); |
| 339 | while (it.hasNext()) { |
| 340 | SelectionListener listener = it.next(); |
| 341 | listener.widgetSelected(null); |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | if (null != fSelectionChangedListeners) { |
| 346 | for (ISelectionChangedListener listener : fSelectionChangedListeners) { |
| 347 | listener.selectionChanged(new SelectionChangedEvent(this, getSelection())); |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * Default selection callback |
| 354 | */ |
| 355 | public void fireDefaultSelection() { |
| 356 | if (null != fSelectionListeners) { |
| 357 | Iterator<SelectionListener> it = fSelectionListeners.iterator(); |
| 358 | while (it.hasNext()) { |
| 359 | SelectionListener listener = it.next(); |
| 360 | listener.widgetDefaultSelected(null); |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | /** |
| 366 | * Add a drag selection listener |
| 367 | * |
| 368 | * @param listener |
| 369 | * The listener to add |
| 370 | */ |
| 371 | public void addDragSelectionListener(ITimeGraphTimeListener listener) { |
| 372 | if (listener == null) { |
| 373 | SWT.error(SWT.ERROR_NULL_ARGUMENT); |
| 374 | } |
| 375 | if (null == fDragSelectionListeners) { |
| 376 | fDragSelectionListeners = new ArrayList<>(); |
| 377 | } |
| 378 | fDragSelectionListeners.add(listener); |
| 379 | } |
| 380 | |
| 381 | /** |
| 382 | * Remove a drag selection listener |
| 383 | * |
| 384 | * @param listener |
| 385 | * The listener to remove |
| 386 | */ |
| 387 | public void removeDragSelectionListener(ITimeGraphTimeListener listener) { |
| 388 | if (null != fDragSelectionListeners) { |
| 389 | fDragSelectionListeners.remove(listener); |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | /** |
| 394 | * Drag Selection changed callback |
| 395 | * |
| 396 | * @param start |
| 397 | * Time interval start |
| 398 | * @param end |
| 399 | * Time interval end |
| 400 | */ |
| 401 | public void fireDragSelectionChanged(long start, long end) { |
| 402 | // check for backward intervals |
| 403 | long beginTime, endTime; |
| 404 | if (start > end) { |
| 405 | beginTime = end; |
| 406 | endTime = start; |
| 407 | } else { |
| 408 | beginTime = start; |
| 409 | endTime = end; |
| 410 | } |
| 411 | // call the listeners |
| 412 | if (null != fDragSelectionListeners) { |
| 413 | Iterator<ITimeGraphTimeListener> it = fDragSelectionListeners.iterator(); |
| 414 | while (it.hasNext()) { |
| 415 | ITimeGraphTimeListener listener = it.next(); |
| 416 | listener.timeSelected(new TimeGraphTimeEvent(this, beginTime, endTime)); |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | /** |
| 422 | * Get the traces in the model |
| 423 | * |
| 424 | * @return The array of traces |
| 425 | */ |
| 426 | public ITimeGraphEntry[] getTraces() { |
| 427 | return fItemData.getEntries(); |
| 428 | } |
| 429 | |
| 430 | /** |
| 431 | * Refresh the data for the thing |
| 432 | */ |
| 433 | public void refreshData() { |
| 434 | fItemData.refreshData(); |
| 435 | redraw(); |
| 436 | } |
| 437 | |
| 438 | /** |
| 439 | * Refresh data for the given traces |
| 440 | * |
| 441 | * @param traces |
| 442 | * The traces to refresh |
| 443 | */ |
| 444 | public void refreshData(ITimeGraphEntry[] traces) { |
| 445 | fItemData.refreshData(traces); |
| 446 | redraw(); |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * Refresh the links (arrows) of this widget |
| 451 | * |
| 452 | * @param events The link event list |
| 453 | */ |
| 454 | public void refreshArrows(List<ILinkEvent> events) { |
| 455 | fItemData.refreshArrows(events); |
| 456 | } |
| 457 | |
| 458 | /** |
| 459 | * Get the links (arrows) of this widget |
| 460 | * |
| 461 | * @return The unmodifiable link event list |
| 462 | * |
| 463 | * @since 1.1 |
| 464 | */ |
| 465 | public List<ILinkEvent> getArrows() { |
| 466 | return Collections.unmodifiableList(fItemData.fLinks); |
| 467 | } |
| 468 | |
| 469 | boolean ensureVisibleItem(int idx, boolean redraw) { |
| 470 | boolean changed = false; |
| 471 | int index = idx; |
| 472 | if (index < 0) { |
| 473 | for (index = 0; index < fItemData.fExpandedItems.length; index++) { |
| 474 | if (fItemData.fExpandedItems[index].fSelected) { |
| 475 | break; |
| 476 | } |
| 477 | } |
| 478 | } |
| 479 | if (index >= fItemData.fExpandedItems.length) { |
| 480 | return changed; |
| 481 | } |
| 482 | if (index < fTopIndex) { |
| 483 | setTopIndex(index); |
| 484 | if (redraw) { |
| 485 | redraw(); |
| 486 | } |
| 487 | changed = true; |
| 488 | } else { |
| 489 | int page = countPerPage(); |
| 490 | if (index >= fTopIndex + page) { |
| 491 | setTopIndex(index - page + 1); |
| 492 | if (redraw) { |
| 493 | redraw(); |
| 494 | } |
| 495 | changed = true; |
| 496 | } |
| 497 | } |
| 498 | return changed; |
| 499 | } |
| 500 | |
| 501 | /** |
| 502 | * Assign the given index as the top one |
| 503 | * |
| 504 | * @param idx |
| 505 | * The index |
| 506 | */ |
| 507 | public void setTopIndex(int idx) { |
| 508 | int index = Math.min(idx, fItemData.fExpandedItems.length - countPerPage()); |
| 509 | index = Math.max(0, index); |
| 510 | fTopIndex = index; |
| 511 | redraw(); |
| 512 | } |
| 513 | |
| 514 | /** |
| 515 | * Set the top index so that the requested element is at the specified |
| 516 | * position. |
| 517 | * |
| 518 | * @param entry |
| 519 | * the time graph entry to be positioned |
| 520 | * @param y |
| 521 | * the requested y-coordinate |
| 522 | * @since 2.0 |
| 523 | */ |
| 524 | public void setElementPosition(ITimeGraphEntry entry, int y) { |
| 525 | Item item = fItemData.fItemMap.get(entry); |
| 526 | if (item == null || item.fExpandedIndex == -1) { |
| 527 | return; |
| 528 | } |
| 529 | int index = item.fExpandedIndex; |
| 530 | Rectangle itemRect = getItemRect(getClientArea(), index); |
| 531 | int delta = itemRect.y + itemRect.height - y; |
| 532 | int topIndex = getItemIndexAtY(delta); |
| 533 | if (topIndex != -1) { |
| 534 | setTopIndex(topIndex); |
| 535 | } else { |
| 536 | if (delta < 0) { |
| 537 | setTopIndex(0); |
| 538 | } else { |
| 539 | setTopIndex(getExpandedElementCount()); |
| 540 | } |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | /** |
| 545 | * Sets the auto-expand level to be used for new entries discovered when |
| 546 | * calling {@link #refreshData()} or {@link #refreshData(ITimeGraphEntry[])} |
| 547 | * . The value 0 means that there is no auto-expand; 1 means that top-level |
| 548 | * entries are expanded, but not their children; 2 means that top-level |
| 549 | * entries are expanded, and their children, but not grand-children; and so |
| 550 | * on. |
| 551 | * <p> |
| 552 | * The value {@link #ALL_LEVELS} means that all subtrees should be expanded. |
| 553 | * </p> |
| 554 | * |
| 555 | * @param level |
| 556 | * non-negative level, or <code>ALL_LEVELS</code> to expand all |
| 557 | * levels of the tree |
| 558 | */ |
| 559 | public void setAutoExpandLevel(int level) { |
| 560 | fAutoExpandLevel = level; |
| 561 | } |
| 562 | |
| 563 | /** |
| 564 | * Returns the auto-expand level. |
| 565 | * |
| 566 | * @return non-negative level, or <code>ALL_LEVELS</code> if all levels of |
| 567 | * the tree are expanded automatically |
| 568 | * @see #setAutoExpandLevel |
| 569 | */ |
| 570 | public int getAutoExpandLevel() { |
| 571 | return fAutoExpandLevel; |
| 572 | } |
| 573 | |
| 574 | /** |
| 575 | * Get the expanded state of a given entry. |
| 576 | * |
| 577 | * @param entry |
| 578 | * The entry |
| 579 | * @return true if the entry is expanded, false if collapsed |
| 580 | * @since 1.1 |
| 581 | */ |
| 582 | public boolean getExpandedState(ITimeGraphEntry entry) { |
| 583 | Item item = fItemData.fItemMap.get(entry); |
| 584 | return (item != null ? item.fExpanded : false); |
| 585 | } |
| 586 | |
| 587 | /** |
| 588 | * Set the expanded state of a given entry |
| 589 | * |
| 590 | * @param entry |
| 591 | * The entry |
| 592 | * @param expanded |
| 593 | * True if expanded, false if collapsed |
| 594 | */ |
| 595 | public void setExpandedState(ITimeGraphEntry entry, boolean expanded) { |
| 596 | Item item = fItemData.findItem(entry); |
| 597 | if (item != null && item.fExpanded != expanded) { |
| 598 | item.fExpanded = expanded; |
| 599 | fItemData.updateExpandedItems(); |
| 600 | redraw(); |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | /** |
| 605 | * Set the expanded state of a given entry to certain relative level. |
| 606 | * It will call fireTreeEvent() for each changed entry. At the end |
| 607 | * it will call redraw(). |
| 608 | * |
| 609 | * @param entry |
| 610 | * The entry |
| 611 | * @param level |
| 612 | * level to expand to or negative for all levels |
| 613 | * @param expanded |
| 614 | * True if expanded, false if collapsed |
| 615 | */ |
| 616 | private void setExpandedState(ITimeGraphEntry entry, int level, boolean expanded) { |
| 617 | setExpandedStateInt(entry, level, expanded); |
| 618 | redraw(); |
| 619 | } |
| 620 | |
| 621 | /** |
| 622 | * Set the expanded state of a given entry and its children to the first |
| 623 | * level that has one collapsed entry. |
| 624 | * |
| 625 | * @param entry |
| 626 | * The entry |
| 627 | */ |
| 628 | private void setExpandedStateLevel(ITimeGraphEntry entry) { |
| 629 | int level = findExpandedLevel(entry); |
| 630 | if (level >= 0) { |
| 631 | setExpandedStateInt(entry, level, true); |
| 632 | redraw(); |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | /* |
| 637 | * Inner class for finding relative level with at least one |
| 638 | * collapsed entry. |
| 639 | */ |
| 640 | private class SearchNode { |
| 641 | SearchNode(ITimeGraphEntry e, int l) { |
| 642 | entry = e; |
| 643 | level = l; |
| 644 | } |
| 645 | ITimeGraphEntry entry; |
| 646 | int level; |
| 647 | } |
| 648 | |
| 649 | /** |
| 650 | * Finds the relative level with at least one collapsed entry. |
| 651 | * |
| 652 | * @param entry |
| 653 | * the start entry |
| 654 | * @return the found level or -1 if all levels are already expanded. |
| 655 | */ |
| 656 | private int findExpandedLevel(ITimeGraphEntry entry) { |
| 657 | Queue<SearchNode> queue = new LinkedList<>(); |
| 658 | SearchNode root = new SearchNode(entry, 0); |
| 659 | SearchNode node = root; |
| 660 | queue.add(root); |
| 661 | |
| 662 | while (!queue.isEmpty()) { |
| 663 | node = queue.remove(); |
| 664 | if (node.entry.hasChildren() && !getExpandedState(node.entry)) { |
| 665 | return node.level; |
| 666 | } |
| 667 | for (ITimeGraphEntry e : node.entry.getChildren()) { |
| 668 | if (e.hasChildren()) { |
| 669 | SearchNode n = new SearchNode(e, node.level + 1); |
| 670 | queue.add(n); |
| 671 | } |
| 672 | } |
| 673 | } |
| 674 | return -1; |
| 675 | } |
| 676 | |
| 677 | /** |
| 678 | * Set the expanded state of a given entry to certain relative level. |
| 679 | * It will call fireTreeEvent() for each changed entry. No redraw is done. |
| 680 | * |
| 681 | * @param entry |
| 682 | * The entry |
| 683 | * @param level |
| 684 | * level to expand to or negative for all levels |
| 685 | * @param expanded |
| 686 | * True if expanded, false if collapsed |
| 687 | */ |
| 688 | private void setExpandedStateInt(ITimeGraphEntry entry, int aLevel, boolean expanded) { |
| 689 | int level = aLevel; |
| 690 | if ((level > 0) || (level < 0)) { |
| 691 | level--; |
| 692 | if (entry.hasChildren()) { |
| 693 | for (ITimeGraphEntry e : entry.getChildren()) { |
| 694 | setExpandedStateInt(e, level, expanded); |
| 695 | } |
| 696 | } |
| 697 | } |
| 698 | Item item = fItemData.findItem(entry); |
| 699 | if (item != null && item.fExpanded != expanded) { |
| 700 | item.fExpanded = expanded; |
| 701 | fItemData.updateExpandedItems(); |
| 702 | fireTreeEvent(item.fEntry, item.fExpanded); |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | /** |
| 707 | * Collapses all nodes of the viewer's tree, starting with the root. |
| 708 | */ |
| 709 | public void collapseAll() { |
| 710 | for (Item item : fItemData.fItems) { |
| 711 | item.fExpanded = false; |
| 712 | } |
| 713 | fItemData.updateExpandedItems(); |
| 714 | redraw(); |
| 715 | } |
| 716 | |
| 717 | /** |
| 718 | * Expands all nodes of the viewer's tree, starting with the root. |
| 719 | */ |
| 720 | public void expandAll() { |
| 721 | for (Item item : fItemData.fItems) { |
| 722 | item.fExpanded = true; |
| 723 | } |
| 724 | fItemData.updateExpandedItems(); |
| 725 | redraw(); |
| 726 | } |
| 727 | |
| 728 | /** |
| 729 | * Add a tree listener |
| 730 | * |
| 731 | * @param listener |
| 732 | * The listener to add |
| 733 | */ |
| 734 | public void addTreeListener(ITimeGraphTreeListener listener) { |
| 735 | if (!fTreeListeners.contains(listener)) { |
| 736 | fTreeListeners.add(listener); |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | /** |
| 741 | * Remove a tree listener |
| 742 | * |
| 743 | * @param listener |
| 744 | * The listener to remove |
| 745 | */ |
| 746 | public void removeTreeListener(ITimeGraphTreeListener listener) { |
| 747 | if (fTreeListeners.contains(listener)) { |
| 748 | fTreeListeners.remove(listener); |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | /** |
| 753 | * Tree event callback |
| 754 | * |
| 755 | * @param entry |
| 756 | * The affected entry |
| 757 | * @param expanded |
| 758 | * The expanded state (true for expanded, false for collapsed) |
| 759 | */ |
| 760 | public void fireTreeEvent(ITimeGraphEntry entry, boolean expanded) { |
| 761 | TimeGraphTreeExpansionEvent event = new TimeGraphTreeExpansionEvent(this, entry); |
| 762 | for (ITimeGraphTreeListener listener : fTreeListeners) { |
| 763 | if (expanded) { |
| 764 | listener.treeExpanded(event); |
| 765 | } else { |
| 766 | listener.treeCollapsed(event); |
| 767 | } |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | /** |
| 772 | * Add a menu listener on {@link ITimeGraphEntry}s |
| 773 | * |
| 774 | * @param listener |
| 775 | * The listener to add |
| 776 | */ |
| 777 | public void addTimeGraphEntryMenuListener(MenuDetectListener listener) { |
| 778 | if (!fTimeGraphEntryMenuListeners.contains(listener)) { |
| 779 | fTimeGraphEntryMenuListeners.add(listener); |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | /** |
| 784 | * Remove a menu listener on {@link ITimeGraphEntry}s |
| 785 | * |
| 786 | * @param listener |
| 787 | * The listener to remove |
| 788 | */ |
| 789 | public void removeTimeGraphEntryMenuListener(MenuDetectListener listener) { |
| 790 | if (fTimeGraphEntryMenuListeners.contains(listener)) { |
| 791 | fTimeGraphEntryMenuListeners.remove(listener); |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | /** |
| 796 | * Menu event callback on {@link ITimeGraphEntry}s |
| 797 | * |
| 798 | * @param event |
| 799 | * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeGraphEntry} |
| 800 | */ |
| 801 | private void fireMenuEventOnTimeGraphEntry(MenuDetectEvent event) { |
| 802 | for (MenuDetectListener listener : fTimeGraphEntryMenuListeners) { |
| 803 | listener.menuDetected(event); |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | /** |
| 808 | * Add a menu listener on {@link ITimeEvent}s |
| 809 | * |
| 810 | * @param listener |
| 811 | * The listener to add |
| 812 | */ |
| 813 | public void addTimeEventMenuListener(MenuDetectListener listener) { |
| 814 | if (!fTimeEventMenuListeners.contains(listener)) { |
| 815 | fTimeEventMenuListeners.add(listener); |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | /** |
| 820 | * Remove a menu listener on {@link ITimeEvent}s |
| 821 | * |
| 822 | * @param listener |
| 823 | * The listener to remove |
| 824 | */ |
| 825 | public void removeTimeEventMenuListener(MenuDetectListener listener) { |
| 826 | if (fTimeEventMenuListeners.contains(listener)) { |
| 827 | fTimeEventMenuListeners.remove(listener); |
| 828 | } |
| 829 | } |
| 830 | |
| 831 | /** |
| 832 | * Menu event callback on {@link ITimeEvent}s |
| 833 | * |
| 834 | * @param event |
| 835 | * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeEvent} |
| 836 | */ |
| 837 | private void fireMenuEventOnTimeEvent(MenuDetectEvent event) { |
| 838 | for (MenuDetectListener listener : fTimeEventMenuListeners) { |
| 839 | listener.menuDetected(event); |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | @Override |
| 844 | public boolean setFocus() { |
| 845 | if ((fTimeProvider != null) && fTimeProvider.getNameSpace() > 0) { |
| 846 | fHasNamespaceFocus = true; |
| 847 | } |
| 848 | return super.setFocus(); |
| 849 | } |
| 850 | |
| 851 | /** |
| 852 | * Returns the current selection for this time graph. If a time graph entry |
| 853 | * is selected, it will be the first element in the selection. If a time |
| 854 | * event is selected, it will be the second element in the selection. |
| 855 | * |
| 856 | * @return the current selection |
| 857 | */ |
| 858 | @Override |
| 859 | public ISelection getSelection() { |
| 860 | ITimeGraphEntry entry = getSelectedTrace(); |
| 861 | if (null != entry && null != fTimeProvider) { |
| 862 | long selectedTime = fTimeProvider.getSelectionBegin(); |
| 863 | ITimeEvent event = Utils.findEvent(entry, selectedTime, 0); |
| 864 | if (event == null) { |
| 865 | return new StructuredSelection(entry); |
| 866 | } |
| 867 | return new StructuredSelection(new Object[] { entry, event }); |
| 868 | } |
| 869 | return StructuredSelection.EMPTY; |
| 870 | } |
| 871 | |
| 872 | /** |
| 873 | * Get the selection object |
| 874 | * |
| 875 | * @return The selection |
| 876 | */ |
| 877 | public ISelection getSelectionTrace() { |
| 878 | ITimeGraphEntry entry = getSelectedTrace(); |
| 879 | if (null != entry) { |
| 880 | return new StructuredSelection(entry); |
| 881 | } |
| 882 | return StructuredSelection.EMPTY; |
| 883 | } |
| 884 | |
| 885 | /** |
| 886 | * Enable/disable one of the traces in the model |
| 887 | * |
| 888 | * @param n |
| 889 | * 1 to enable it, -1 to disable. The method returns immediately |
| 890 | * if another value is used. |
| 891 | */ |
| 892 | public void selectTrace(int n) { |
| 893 | if ((n != 1) && (n != -1)) { |
| 894 | return; |
| 895 | } |
| 896 | |
| 897 | boolean changed = false; |
| 898 | int lastSelection = -1; |
| 899 | for (int i = 0; i < fItemData.fExpandedItems.length; i++) { |
| 900 | Item item = fItemData.fExpandedItems[i]; |
| 901 | if (item.fSelected) { |
| 902 | lastSelection = i; |
| 903 | if ((1 == n) && (i < fItemData.fExpandedItems.length - 1)) { |
| 904 | item.fSelected = false; |
| 905 | item = fItemData.fExpandedItems[i + 1]; |
| 906 | item.fSelected = true; |
| 907 | changed = true; |
| 908 | } else if ((-1 == n) && (i > 0)) { |
| 909 | item.fSelected = false; |
| 910 | item = fItemData.fExpandedItems[i - 1]; |
| 911 | item.fSelected = true; |
| 912 | changed = true; |
| 913 | } |
| 914 | break; |
| 915 | } |
| 916 | } |
| 917 | |
| 918 | if (lastSelection < 0 && fItemData.fExpandedItems.length > 0) { |
| 919 | Item item = fItemData.fExpandedItems[0]; |
| 920 | item.fSelected = true; |
| 921 | changed = true; |
| 922 | } |
| 923 | |
| 924 | if (changed) { |
| 925 | ensureVisibleItem(-1, false); |
| 926 | redraw(); |
| 927 | fireSelectionChanged(); |
| 928 | } |
| 929 | } |
| 930 | |
| 931 | /** |
| 932 | * Select an event |
| 933 | * |
| 934 | * @param n |
| 935 | * 1 for next event, -1 for previous event |
| 936 | * @param extend |
| 937 | * true to extend selection range, false for single selection |
| 938 | * @since 1.0 |
| 939 | */ |
| 940 | public void selectEvent(int n, boolean extend) { |
| 941 | if (null == fTimeProvider) { |
| 942 | return; |
| 943 | } |
| 944 | ITimeGraphEntry trace = getSelectedTrace(); |
| 945 | if (trace == null) { |
| 946 | return; |
| 947 | } |
| 948 | long selectedTime = fTimeProvider.getSelectionEnd(); |
| 949 | long endTime = fTimeProvider.getMaxTime(); |
| 950 | ITimeEvent nextEvent; |
| 951 | if (n == -1 && selectedTime > endTime) { |
| 952 | nextEvent = Utils.findEvent(trace, selectedTime, 0); |
| 953 | } else { |
| 954 | nextEvent = Utils.findEvent(trace, selectedTime, n); |
| 955 | } |
| 956 | if (null == nextEvent && n == -1) { |
| 957 | nextEvent = Utils.getFirstEvent(trace); |
| 958 | } |
| 959 | if (null != nextEvent) { |
| 960 | long nextTime = nextEvent.getTime(); |
| 961 | // If last event detected e.g. going back or not moving to a next |
| 962 | // event |
| 963 | if (nextTime <= selectedTime && n == 1) { |
| 964 | // Select to the end of this last event |
| 965 | nextTime = nextEvent.getTime() + nextEvent.getDuration(); |
| 966 | // but not beyond the end of the trace |
| 967 | if (nextTime > endTime) { |
| 968 | nextTime = endTime; |
| 969 | } |
| 970 | } else if (n == -1 && nextEvent.getTime() + nextEvent.getDuration() < selectedTime) { |
| 971 | // for previous event go to its end time unless we were already there |
| 972 | nextTime = nextEvent.getTime() + nextEvent.getDuration(); |
| 973 | } |
| 974 | if (extend) { |
| 975 | fTimeProvider.setSelectionRangeNotify(fTimeProvider.getSelectionBegin(), nextTime, true); |
| 976 | } else { |
| 977 | fTimeProvider.setSelectedTimeNotify(nextTime, true); |
| 978 | } |
| 979 | fireSelectionChanged(); |
| 980 | } else if (n == 1) { |
| 981 | if (extend) { |
| 982 | fTimeProvider.setSelectionRangeNotify(fTimeProvider.getSelectionBegin(), endTime, true); |
| 983 | } else { |
| 984 | fTimeProvider.setSelectedTimeNotify(endTime, true); |
| 985 | } |
| 986 | fireSelectionChanged(); |
| 987 | } |
| 988 | updateStatusLine(STATUS_WITHOUT_CURSOR_TIME); |
| 989 | } |
| 990 | |
| 991 | /** |
| 992 | * Select the next event |
| 993 | * |
| 994 | * @param extend |
| 995 | * true to extend selection range, false for single selection |
| 996 | * @since 1.0 |
| 997 | */ |
| 998 | public void selectNextEvent(boolean extend) { |
| 999 | selectEvent(1, extend); |
| 1000 | // Notify if visible time window has been adjusted |
| 1001 | fTimeProvider.setStartFinishTimeNotify(fTimeProvider.getTime0(), fTimeProvider.getTime1()); |
| 1002 | } |
| 1003 | |
| 1004 | /** |
| 1005 | * Select the previous event |
| 1006 | * |
| 1007 | * @param extend |
| 1008 | * true to extend selection range, false for single selection |
| 1009 | * @since 1.0 |
| 1010 | */ |
| 1011 | public void selectPrevEvent(boolean extend) { |
| 1012 | selectEvent(-1, extend); |
| 1013 | // Notify if visible time window has been adjusted |
| 1014 | fTimeProvider.setStartFinishTimeNotify(fTimeProvider.getTime0(), fTimeProvider.getTime1()); |
| 1015 | } |
| 1016 | |
| 1017 | /** |
| 1018 | * Select the next trace |
| 1019 | */ |
| 1020 | public void selectNextTrace() { |
| 1021 | selectTrace(1); |
| 1022 | } |
| 1023 | |
| 1024 | /** |
| 1025 | * Select the previous trace |
| 1026 | */ |
| 1027 | public void selectPrevTrace() { |
| 1028 | selectTrace(-1); |
| 1029 | } |
| 1030 | |
| 1031 | /** |
| 1032 | * Scroll left or right by one half window size |
| 1033 | * |
| 1034 | * @param left |
| 1035 | * true to scroll left, false to scroll right |
| 1036 | */ |
| 1037 | public void horizontalScroll(boolean left) { |
| 1038 | long time0 = fTimeProvider.getTime0(); |
| 1039 | long time1 = fTimeProvider.getTime1(); |
| 1040 | long timeMin = fTimeProvider.getMinTime(); |
| 1041 | long timeMax = fTimeProvider.getMaxTime(); |
| 1042 | long range = time1 - time0; |
| 1043 | if (range <= 0) { |
| 1044 | return; |
| 1045 | } |
| 1046 | long increment = Math.max(1, range / 2); |
| 1047 | if (left) { |
| 1048 | time0 = Math.max(time0 - increment, timeMin); |
| 1049 | time1 = time0 + range; |
| 1050 | } else { |
| 1051 | time1 = Math.min(time1 + increment, timeMax); |
| 1052 | time0 = time1 - range; |
| 1053 | } |
| 1054 | fTimeProvider.setStartFinishTimeNotify(time0, time1); |
| 1055 | } |
| 1056 | |
| 1057 | /** |
| 1058 | * Zoom based on mouse cursor location with mouse scrolling |
| 1059 | * |
| 1060 | * @param zoomIn true to zoom in, false to zoom out |
| 1061 | */ |
| 1062 | public void zoom(boolean zoomIn) { |
| 1063 | int globalX = getDisplay().getCursorLocation().x; |
| 1064 | Point p = toControl(globalX, 0); |
| 1065 | int nameSpace = fTimeProvider.getNameSpace(); |
| 1066 | int timeSpace = fTimeProvider.getTimeSpace(); |
| 1067 | int xPos = Math.max(nameSpace, Math.min(nameSpace + timeSpace, p.x)); |
| 1068 | long time0 = fTimeProvider.getTime0(); |
| 1069 | long time1 = fTimeProvider.getTime1(); |
| 1070 | long interval = time1 - time0; |
| 1071 | if (interval == 0) { |
| 1072 | interval = 1; |
| 1073 | } // to allow getting out of single point interval |
| 1074 | long newInterval; |
| 1075 | if (zoomIn) { |
| 1076 | newInterval = Math.max(Math.round(interval * ZOOM_IN_FACTOR), fTimeProvider.getMinTimeInterval()); |
| 1077 | } else { |
| 1078 | newInterval = (long) Math.ceil(interval * ZOOM_OUT_FACTOR); |
| 1079 | } |
| 1080 | long center = time0 + Math.round(((double) (xPos - nameSpace) / timeSpace * interval)); |
| 1081 | long newTime0 = center - Math.round((double) newInterval * (center - time0) / interval); |
| 1082 | long newTime1 = newTime0 + newInterval; |
| 1083 | fTimeProvider.setStartFinishTimeNotify(newTime0, newTime1); |
| 1084 | } |
| 1085 | |
| 1086 | /** |
| 1087 | * zoom in using single click |
| 1088 | */ |
| 1089 | public void zoomIn() { |
| 1090 | long prevTime0 = fTimeProvider.getTime0(); |
| 1091 | long prevTime1 = fTimeProvider.getTime1(); |
| 1092 | long prevRange = prevTime1 - prevTime0; |
| 1093 | if (prevRange == 0) { |
| 1094 | return; |
| 1095 | } |
| 1096 | ITimeDataProvider provider = fTimeProvider; |
| 1097 | long selTime = (provider.getSelectionEnd() + provider.getSelectionBegin()) / 2; |
| 1098 | if (selTime <= prevTime0 || selTime >= prevTime1) { |
| 1099 | selTime = (prevTime0 + prevTime1) / 2; |
| 1100 | } |
| 1101 | long time0 = selTime - (long) ((selTime - prevTime0) / ZOOM_FACTOR); |
| 1102 | long time1 = selTime + (long) ((prevTime1 - selTime) / ZOOM_FACTOR); |
| 1103 | |
| 1104 | long inaccuracy = (fTimeProvider.getMaxTime() - fTimeProvider.getMinTime()) - (time1 - time0); |
| 1105 | |
| 1106 | if (inaccuracy > 0 && inaccuracy < 100) { |
| 1107 | fTimeProvider.setStartFinishTimeNotify(fTimeProvider.getMinTime(), fTimeProvider.getMaxTime()); |
| 1108 | return; |
| 1109 | } |
| 1110 | |
| 1111 | long min = fTimeProvider.getMinTimeInterval(); |
| 1112 | if ((time1 - time0) < min) { |
| 1113 | time0 = selTime - (selTime - prevTime0) * min / prevRange; |
| 1114 | time1 = time0 + min; |
| 1115 | } |
| 1116 | |
| 1117 | fTimeProvider.setStartFinishTimeNotify(time0, time1); |
| 1118 | } |
| 1119 | |
| 1120 | /** |
| 1121 | * zoom out using single click |
| 1122 | */ |
| 1123 | public void zoomOut() { |
| 1124 | long prevTime0 = fTimeProvider.getTime0(); |
| 1125 | long prevTime1 = fTimeProvider.getTime1(); |
| 1126 | ITimeDataProvider provider = fTimeProvider; |
| 1127 | long selTime = (provider.getSelectionEnd() + provider.getSelectionBegin()) / 2; |
| 1128 | if (selTime <= prevTime0 || selTime >= prevTime1) { |
| 1129 | selTime = (prevTime0 + prevTime1) / 2; |
| 1130 | } |
| 1131 | long time0 = (long) (selTime - (selTime - prevTime0) * ZOOM_FACTOR); |
| 1132 | long time1 = (long) (selTime + (prevTime1 - selTime) * ZOOM_FACTOR); |
| 1133 | |
| 1134 | long inaccuracy = (fTimeProvider.getMaxTime() - fTimeProvider.getMinTime()) - (time1 - time0); |
| 1135 | if (inaccuracy > 0 && inaccuracy < 100) { |
| 1136 | fTimeProvider.setStartFinishTimeNotify(fTimeProvider.getMinTime(), fTimeProvider.getMaxTime()); |
| 1137 | return; |
| 1138 | } |
| 1139 | |
| 1140 | fTimeProvider.setStartFinishTimeNotify(time0, time1); |
| 1141 | } |
| 1142 | |
| 1143 | /** |
| 1144 | * Zoom vertically. |
| 1145 | * |
| 1146 | * @param zoomIn |
| 1147 | * true to zoom in, false to zoom out |
| 1148 | * @since 2.0 |
| 1149 | */ |
| 1150 | public void verticalZoom(boolean zoomIn) { |
| 1151 | if (zoomIn) { |
| 1152 | fHeightAdjustment++; |
| 1153 | } else { |
| 1154 | fHeightAdjustment--; |
| 1155 | } |
| 1156 | fItemData.refreshData(); |
| 1157 | redraw(); |
| 1158 | } |
| 1159 | |
| 1160 | /** |
| 1161 | * Reset the vertical zoom to default. |
| 1162 | * |
| 1163 | * @since 2.0 |
| 1164 | */ |
| 1165 | public void resetVerticalZoom() { |
| 1166 | fHeightAdjustment = 0; |
| 1167 | fItemData.refreshData(); |
| 1168 | redraw(); |
| 1169 | } |
| 1170 | |
| 1171 | /** |
| 1172 | * Set the grid lines visibility. The default is true. |
| 1173 | * |
| 1174 | * @param visible |
| 1175 | * true to show the grid lines, false otherwise |
| 1176 | * @since 2.0 |
| 1177 | */ |
| 1178 | public void setGridLinesVisible(boolean visible) { |
| 1179 | fGridLinesVisible = visible; |
| 1180 | } |
| 1181 | |
| 1182 | /** |
| 1183 | * Get the grid lines visibility. |
| 1184 | * |
| 1185 | * @return true if the grid lines are visible, false otherwise |
| 1186 | * @since 2.0 |
| 1187 | */ |
| 1188 | public boolean getGridLinesVisible() { |
| 1189 | return fGridLinesVisible; |
| 1190 | } |
| 1191 | |
| 1192 | /** |
| 1193 | * Set the grid line color. The default is SWT.COLOR_GRAY. |
| 1194 | * |
| 1195 | * @param color |
| 1196 | * the grid line color |
| 1197 | * @since 2.0 |
| 1198 | */ |
| 1199 | public void setGridLineColor(Color color) { |
| 1200 | fGridLineColor = color; |
| 1201 | } |
| 1202 | |
| 1203 | /** |
| 1204 | * Get the grid line color. |
| 1205 | * |
| 1206 | * @return the grid line color |
| 1207 | * @since 2.0 |
| 1208 | */ |
| 1209 | public Color getGridLineColor() { |
| 1210 | return fGridLineColor; |
| 1211 | } |
| 1212 | |
| 1213 | /** |
| 1214 | * Set the markers list. |
| 1215 | * |
| 1216 | * @param markers |
| 1217 | * The markers list, or null |
| 1218 | * @since 2.0 |
| 1219 | */ |
| 1220 | public void setMarkers(List<IMarkerEvent> markers) { |
| 1221 | fMarkers = markers; |
| 1222 | } |
| 1223 | |
| 1224 | /** |
| 1225 | * Get the markers list. |
| 1226 | * |
| 1227 | * @return The markers list, or null |
| 1228 | * @since 2.0 |
| 1229 | */ |
| 1230 | public List<IMarkerEvent> getMarkers() { |
| 1231 | return fMarkers; |
| 1232 | } |
| 1233 | |
| 1234 | /** |
| 1235 | * Set the markers visibility. The default is true. |
| 1236 | * |
| 1237 | * @param visible |
| 1238 | * true to show the markers, false otherwise |
| 1239 | * @since 2.0 |
| 1240 | */ |
| 1241 | public void setMarkersVisible(boolean visible) { |
| 1242 | fMarkersVisible = visible; |
| 1243 | } |
| 1244 | |
| 1245 | /** |
| 1246 | * Get the markers visibility. |
| 1247 | * |
| 1248 | * @return true if the markers are visible, false otherwise |
| 1249 | * @since 2.0 |
| 1250 | */ |
| 1251 | public boolean getMarkersVisible() { |
| 1252 | return fMarkersVisible; |
| 1253 | } |
| 1254 | |
| 1255 | /** |
| 1256 | * Hide arrows |
| 1257 | * |
| 1258 | * @param hideArrows true to hide arrows |
| 1259 | */ |
| 1260 | public void hideArrows(boolean hideArrows) { |
| 1261 | fHideArrows = hideArrows; |
| 1262 | } |
| 1263 | |
| 1264 | /** |
| 1265 | * Follow the arrow forward |
| 1266 | * |
| 1267 | * @param extend |
| 1268 | * true to extend selection range, false for single selection |
| 1269 | * @since 1.0 |
| 1270 | */ |
| 1271 | public void followArrowFwd(boolean extend) { |
| 1272 | ITimeGraphEntry trace = getSelectedTrace(); |
| 1273 | if (trace == null) { |
| 1274 | return; |
| 1275 | } |
| 1276 | long selectedTime = fTimeProvider.getSelectionEnd(); |
| 1277 | for (ILinkEvent link : fItemData.fLinks) { |
| 1278 | if (link.getEntry() == trace && link.getTime() == selectedTime) { |
| 1279 | selectItem(link.getDestinationEntry(), false); |
| 1280 | if (link.getDuration() != 0) { |
| 1281 | if (extend) { |
| 1282 | fTimeProvider.setSelectionRangeNotify(fTimeProvider.getSelectionBegin(), link.getTime() + link.getDuration(), true); |
| 1283 | } else { |
| 1284 | fTimeProvider.setSelectedTimeNotify(link.getTime() + link.getDuration(), true); |
| 1285 | } |
| 1286 | // Notify if visible time window has been adjusted |
| 1287 | fTimeProvider.setStartFinishTimeNotify(fTimeProvider.getTime0(), fTimeProvider.getTime1()); |
| 1288 | } |
| 1289 | fireSelectionChanged(); |
| 1290 | updateStatusLine(STATUS_WITHOUT_CURSOR_TIME); |
| 1291 | return; |
| 1292 | } |
| 1293 | } |
| 1294 | selectNextEvent(extend); |
| 1295 | } |
| 1296 | |
| 1297 | /** |
| 1298 | * Follow the arrow backward |
| 1299 | * |
| 1300 | * @param extend |
| 1301 | * true to extend selection range, false for single selection |
| 1302 | * @since 1.0 |
| 1303 | */ |
| 1304 | public void followArrowBwd(boolean extend) { |
| 1305 | ITimeGraphEntry trace = getSelectedTrace(); |
| 1306 | if (trace == null) { |
| 1307 | return; |
| 1308 | } |
| 1309 | long selectedTime = fTimeProvider.getSelectionEnd(); |
| 1310 | for (ILinkEvent link : fItemData.fLinks) { |
| 1311 | if (link.getDestinationEntry() == trace && link.getTime() + link.getDuration() == selectedTime) { |
| 1312 | selectItem(link.getEntry(), false); |
| 1313 | if (link.getDuration() != 0) { |
| 1314 | if (extend) { |
| 1315 | fTimeProvider.setSelectionRangeNotify(fTimeProvider.getSelectionBegin(), link.getTime(), true); |
| 1316 | } else { |
| 1317 | fTimeProvider.setSelectedTimeNotify(link.getTime(), true); |
| 1318 | } |
| 1319 | // Notify if visible time window has been adjusted |
| 1320 | fTimeProvider.setStartFinishTimeNotify(fTimeProvider.getTime0(), fTimeProvider.getTime1()); |
| 1321 | } |
| 1322 | fireSelectionChanged(); |
| 1323 | updateStatusLine(STATUS_WITHOUT_CURSOR_TIME); |
| 1324 | return; |
| 1325 | } |
| 1326 | } |
| 1327 | selectPrevEvent(extend); |
| 1328 | } |
| 1329 | |
| 1330 | /** |
| 1331 | * Return the currently selected trace |
| 1332 | * |
| 1333 | * @return The entry matching the trace |
| 1334 | */ |
| 1335 | public ITimeGraphEntry getSelectedTrace() { |
| 1336 | ITimeGraphEntry trace = null; |
| 1337 | int idx = getSelectedIndex(); |
| 1338 | if (idx >= 0) { |
| 1339 | trace = fItemData.fExpandedItems[idx].fEntry; |
| 1340 | } |
| 1341 | return trace; |
| 1342 | } |
| 1343 | |
| 1344 | /** |
| 1345 | * Retrieve the index of the currently selected item |
| 1346 | * |
| 1347 | * @return The index |
| 1348 | */ |
| 1349 | public int getSelectedIndex() { |
| 1350 | int idx = -1; |
| 1351 | for (int i = 0; i < fItemData.fExpandedItems.length; i++) { |
| 1352 | Item item = fItemData.fExpandedItems[i]; |
| 1353 | if (item.fSelected) { |
| 1354 | idx = i; |
| 1355 | break; |
| 1356 | } |
| 1357 | } |
| 1358 | return idx; |
| 1359 | } |
| 1360 | |
| 1361 | boolean toggle(int idx) { |
| 1362 | boolean toggled = false; |
| 1363 | if (idx >= 0 && idx < fItemData.fExpandedItems.length) { |
| 1364 | Item item = fItemData.fExpandedItems[idx]; |
| 1365 | if (item.fHasChildren) { |
| 1366 | item.fExpanded = !item.fExpanded; |
| 1367 | fItemData.updateExpandedItems(); |
| 1368 | redraw(); |
| 1369 | toggled = true; |
| 1370 | fireTreeEvent(item.fEntry, item.fExpanded); |
| 1371 | } |
| 1372 | } |
| 1373 | return toggled; |
| 1374 | } |
| 1375 | |
| 1376 | /** |
| 1377 | * Gets the index of the item at the given location. |
| 1378 | * |
| 1379 | * @param y |
| 1380 | * the y coordinate |
| 1381 | * @return the index of the item at the given location, of -1 if none. |
| 1382 | */ |
| 1383 | protected int getItemIndexAtY(int y) { |
| 1384 | int ySum = 0; |
| 1385 | if (y < 0) { |
| 1386 | for (int idx = fTopIndex - 1; idx >= 0; idx--) { |
| 1387 | ySum -= fItemData.fExpandedItems[idx].fItemHeight; |
| 1388 | if (y >= ySum) { |
| 1389 | return idx; |
| 1390 | } |
| 1391 | } |
| 1392 | } else { |
| 1393 | for (int idx = fTopIndex; idx < fItemData.fExpandedItems.length; idx++) { |
| 1394 | ySum += fItemData.fExpandedItems[idx].fItemHeight; |
| 1395 | if (y < ySum) { |
| 1396 | return idx; |
| 1397 | } |
| 1398 | } |
| 1399 | } |
| 1400 | return -1; |
| 1401 | } |
| 1402 | |
| 1403 | boolean isOverSplitLine(int x) { |
| 1404 | if (x < 0 || null == fTimeProvider) { |
| 1405 | return false; |
| 1406 | } |
| 1407 | int nameWidth = fTimeProvider.getNameSpace(); |
| 1408 | return Math.abs(x - nameWidth) < SNAP_WIDTH; |
| 1409 | } |
| 1410 | |
| 1411 | boolean isOverTimeSpace(int x, int y) { |
| 1412 | Point size = getSize(); |
| 1413 | return x >= fTimeProvider.getNameSpace() && x < size.x && y >= 0 && y < size.y; |
| 1414 | } |
| 1415 | /** |
| 1416 | * Gets the {@link ITimeGraphEntry} at the given location. |
| 1417 | * |
| 1418 | * @param pt |
| 1419 | * a point in the widget |
| 1420 | * @return the {@link ITimeGraphEntry} at this point, or <code>null</code> |
| 1421 | * if none. |
| 1422 | * @since 2.0 |
| 1423 | */ |
| 1424 | public ITimeGraphEntry getEntry(Point pt) { |
| 1425 | int idx = getItemIndexAtY(pt.y); |
| 1426 | return idx >= 0 ? fItemData.fExpandedItems[idx].fEntry : null; |
| 1427 | } |
| 1428 | |
| 1429 | /** |
| 1430 | * Return the arrow event closest to the given point that is no further than |
| 1431 | * a maximum distance. |
| 1432 | * |
| 1433 | * @param pt |
| 1434 | * a point in the widget |
| 1435 | * @return The closest arrow event, or null if there is none close enough. |
| 1436 | */ |
| 1437 | protected ILinkEvent getArrow(Point pt) { |
| 1438 | if (fHideArrows) { |
| 1439 | return null; |
| 1440 | } |
| 1441 | ILinkEvent linkEvent = null; |
| 1442 | double minDistance = Double.MAX_VALUE; |
| 1443 | for (ILinkEvent event : fItemData.fLinks) { |
| 1444 | Rectangle rect = getArrowRectangle(new Rectangle(0, 0, 0, 0), event); |
| 1445 | if (rect != null) { |
| 1446 | int x1 = rect.x; |
| 1447 | int y1 = rect.y; |
| 1448 | int x2 = x1 + rect.width; |
| 1449 | int y2 = y1 + rect.height; |
| 1450 | double d = Utils.distance(pt.x, pt.y, x1, y1, x2, y2); |
| 1451 | if (minDistance > d) { |
| 1452 | minDistance = d; |
| 1453 | linkEvent = event; |
| 1454 | } |
| 1455 | } |
| 1456 | } |
| 1457 | if (minDistance <= ARROW_HOVER_MAX_DIST) { |
| 1458 | return linkEvent; |
| 1459 | } |
| 1460 | return null; |
| 1461 | } |
| 1462 | |
| 1463 | @Override |
| 1464 | public int getXForTime(long time) { |
| 1465 | if (null == fTimeProvider) { |
| 1466 | return -1; |
| 1467 | } |
| 1468 | long time0 = fTimeProvider.getTime0(); |
| 1469 | long time1 = fTimeProvider.getTime1(); |
| 1470 | int width = getSize().x; |
| 1471 | int nameSpace = fTimeProvider.getNameSpace(); |
| 1472 | double pixelsPerNanoSec = (width - nameSpace <= RIGHT_MARGIN) ? 0 : (double) (width - nameSpace - RIGHT_MARGIN) / (time1 - time0); |
| 1473 | int x = SaturatedArithmetic.add(getBounds().x + nameSpace, (int) ((time - time0) * pixelsPerNanoSec)); |
| 1474 | return x; |
| 1475 | } |
| 1476 | |
| 1477 | @Override |
| 1478 | public long getTimeAtX(int coord) { |
| 1479 | if (null == fTimeProvider) { |
| 1480 | return -1; |
| 1481 | } |
| 1482 | long hitTime = -1; |
| 1483 | Point size = getSize(); |
| 1484 | long time0 = fTimeProvider.getTime0(); |
| 1485 | long time1 = fTimeProvider.getTime1(); |
| 1486 | int nameWidth = fTimeProvider.getNameSpace(); |
| 1487 | final int x = coord - nameWidth; |
| 1488 | int timeWidth = size.x - nameWidth - RIGHT_MARGIN; |
| 1489 | if (x >= 0 && size.x >= nameWidth) { |
| 1490 | if (time1 - time0 > timeWidth) { |
| 1491 | // nanosecond smaller than one pixel: use the first integer nanosecond of this pixel's time range |
| 1492 | hitTime = time0 + (long) Math.ceil((time1 - time0) * ((double) x / timeWidth)); |
| 1493 | } else { |
| 1494 | // nanosecond greater than one pixel: use the nanosecond that covers this pixel start position |
| 1495 | hitTime = time0 + (long) Math.floor((time1 - time0) * ((double) x / timeWidth)); |
| 1496 | } |
| 1497 | } |
| 1498 | return hitTime; |
| 1499 | } |
| 1500 | |
| 1501 | void selectItem(int idx, boolean addSelection) { |
| 1502 | boolean changed = false; |
| 1503 | if (addSelection) { |
| 1504 | if (idx >= 0 && idx < fItemData.fExpandedItems.length) { |
| 1505 | Item item = fItemData.fExpandedItems[idx]; |
| 1506 | changed = !item.fSelected; |
| 1507 | item.fSelected = true; |
| 1508 | } |
| 1509 | } else { |
| 1510 | for (int i = 0; i < fItemData.fExpandedItems.length; i++) { |
| 1511 | Item item = fItemData.fExpandedItems[i]; |
| 1512 | if ((i == idx && !item.fSelected) || (idx == -1 && item.fSelected)) { |
| 1513 | changed = true; |
| 1514 | } |
| 1515 | item.fSelected = i == idx; |
| 1516 | } |
| 1517 | } |
| 1518 | changed |= ensureVisibleItem(idx, true); |
| 1519 | if (changed) { |
| 1520 | redraw(); |
| 1521 | } |
| 1522 | } |
| 1523 | |
| 1524 | /** |
| 1525 | * Callback for item selection |
| 1526 | * |
| 1527 | * @param trace |
| 1528 | * The entry matching the trace |
| 1529 | * @param addSelection |
| 1530 | * If the selection is added or removed |
| 1531 | */ |
| 1532 | public void selectItem(ITimeGraphEntry trace, boolean addSelection) { |
| 1533 | int idx = fItemData.findItemIndex(trace); |
| 1534 | selectItem(idx, addSelection); |
| 1535 | } |
| 1536 | |
| 1537 | /** |
| 1538 | * Retrieve the number of entries shown per page. |
| 1539 | * |
| 1540 | * @return The count |
| 1541 | */ |
| 1542 | public int countPerPage() { |
| 1543 | int height = getSize().y; |
| 1544 | int count = 0; |
| 1545 | int ySum = 0; |
| 1546 | for (int idx = fTopIndex; idx < fItemData.fExpandedItems.length; idx++) { |
| 1547 | ySum += fItemData.fExpandedItems[idx].fItemHeight; |
| 1548 | if (ySum >= height) { |
| 1549 | return count; |
| 1550 | } |
| 1551 | count++; |
| 1552 | } |
| 1553 | for (int idx = fTopIndex - 1; idx >= 0; idx--) { |
| 1554 | ySum += fItemData.fExpandedItems[idx].fItemHeight; |
| 1555 | if (ySum >= height) { |
| 1556 | return count; |
| 1557 | } |
| 1558 | count++; |
| 1559 | } |
| 1560 | return count; |
| 1561 | } |
| 1562 | |
| 1563 | /** |
| 1564 | * Get the index of the top element |
| 1565 | * |
| 1566 | * @return The index |
| 1567 | */ |
| 1568 | public int getTopIndex() { |
| 1569 | return fTopIndex; |
| 1570 | } |
| 1571 | |
| 1572 | /** |
| 1573 | * Get the number of expanded (visible) items |
| 1574 | * |
| 1575 | * @return The count of expanded (visible) items |
| 1576 | */ |
| 1577 | public int getExpandedElementCount() { |
| 1578 | return fItemData.fExpandedItems.length; |
| 1579 | } |
| 1580 | |
| 1581 | /** |
| 1582 | * Get an array of all expanded (visible) elements |
| 1583 | * |
| 1584 | * @return The expanded (visible) elements |
| 1585 | */ |
| 1586 | public ITimeGraphEntry[] getExpandedElements() { |
| 1587 | ArrayList<ITimeGraphEntry> elements = new ArrayList<>(); |
| 1588 | for (Item item : fItemData.fExpandedItems) { |
| 1589 | elements.add(item.fEntry); |
| 1590 | } |
| 1591 | return elements.toArray(new ITimeGraphEntry[0]); |
| 1592 | } |
| 1593 | |
| 1594 | /** |
| 1595 | * Get the expanded (visible) element at the specified index. |
| 1596 | * |
| 1597 | * @param index |
| 1598 | * the element index |
| 1599 | * @return The expanded (visible) element or null if out of range |
| 1600 | * @since 2.0 |
| 1601 | */ |
| 1602 | public ITimeGraphEntry getExpandedElement(int index) { |
| 1603 | if (index < 0 || index >= fItemData.fExpandedItems.length) { |
| 1604 | return null; |
| 1605 | } |
| 1606 | return fItemData.fExpandedItems[index].fEntry; |
| 1607 | } |
| 1608 | |
| 1609 | Rectangle getNameRect(Rectangle bounds, int idx, int nameWidth) { |
| 1610 | Rectangle rect = getItemRect(bounds, idx); |
| 1611 | rect.width = nameWidth; |
| 1612 | return rect; |
| 1613 | } |
| 1614 | |
| 1615 | Rectangle getStatesRect(Rectangle bounds, int idx, int nameWidth) { |
| 1616 | Rectangle rect = getItemRect(bounds, idx); |
| 1617 | rect.x += nameWidth; |
| 1618 | rect.width -= nameWidth; |
| 1619 | return rect; |
| 1620 | } |
| 1621 | |
| 1622 | Rectangle getItemRect(Rectangle bounds, int idx) { |
| 1623 | int ySum = 0; |
| 1624 | if (idx >= fTopIndex) { |
| 1625 | for (int i = fTopIndex; i < idx; i++) { |
| 1626 | ySum += fItemData.fExpandedItems[i].fItemHeight; |
| 1627 | } |
| 1628 | } else { |
| 1629 | for (int i = fTopIndex - 1; i >= idx; i--) { |
| 1630 | ySum -= fItemData.fExpandedItems[i].fItemHeight; |
| 1631 | } |
| 1632 | } |
| 1633 | int y = bounds.y + ySum; |
| 1634 | int height = fItemData.fExpandedItems[idx].fItemHeight; |
| 1635 | return new Rectangle(bounds.x, y, bounds.width, height); |
| 1636 | } |
| 1637 | |
| 1638 | @Override |
| 1639 | void paint(Rectangle bounds, PaintEvent e) { |
| 1640 | GC gc = e.gc; |
| 1641 | |
| 1642 | if (bounds.width < 2 || bounds.height < 2 || null == fTimeProvider) { |
| 1643 | return; |
| 1644 | } |
| 1645 | |
| 1646 | fIdealNameSpace = 0; |
| 1647 | int nameSpace = fTimeProvider.getNameSpace(); |
| 1648 | |
| 1649 | // draw the background layer |
| 1650 | drawBackground(bounds, nameSpace, gc); |
| 1651 | |
| 1652 | // draw the grid lines |
| 1653 | drawGridLines(bounds, gc); |
| 1654 | |
| 1655 | // draw the background markers |
| 1656 | drawMarkers(bounds, fTimeProvider, fMarkers, false, nameSpace, gc); |
| 1657 | |
| 1658 | // draw the items |
| 1659 | drawItems(bounds, fTimeProvider, fItemData.fExpandedItems, fTopIndex, nameSpace, gc); |
| 1660 | |
| 1661 | // draw the foreground markers |
| 1662 | drawMarkers(bounds, fTimeProvider, fMarkers, true, nameSpace, gc); |
| 1663 | |
| 1664 | // draw the links (arrows) |
| 1665 | drawLinks(bounds, fTimeProvider, fItemData.fLinks, nameSpace, gc); |
| 1666 | |
| 1667 | fTimeGraphProvider.postDrawControl(bounds, gc); |
| 1668 | |
| 1669 | int alpha = gc.getAlpha(); |
| 1670 | gc.setAlpha(100); |
| 1671 | |
| 1672 | long time0 = fTimeProvider.getTime0(); |
| 1673 | long time1 = fTimeProvider.getTime1(); |
| 1674 | long selectionBegin = fTimeProvider.getSelectionBegin(); |
| 1675 | long selectionEnd = fTimeProvider.getSelectionEnd(); |
| 1676 | double pixelsPerNanoSec = (bounds.width - nameSpace <= RIGHT_MARGIN) ? 0 : (double) (bounds.width - nameSpace - RIGHT_MARGIN) / (time1 - time0); |
| 1677 | int x0 = SaturatedArithmetic.add(bounds.x + nameSpace, (int) ((selectionBegin - time0) * pixelsPerNanoSec)); |
| 1678 | int x1 = SaturatedArithmetic.add(bounds.x + nameSpace, (int) ((selectionEnd - time0) * pixelsPerNanoSec)); |
| 1679 | |
| 1680 | // draw selection lines |
| 1681 | if (fDragState != DRAG_SELECTION) { |
| 1682 | gc.setForeground(getColorScheme().getColor(TimeGraphColorScheme.SELECTED_TIME)); |
| 1683 | if (x0 >= nameSpace && x0 < bounds.x + bounds.width) { |
| 1684 | gc.drawLine(x0, bounds.y, x0, bounds.y + bounds.height); |
| 1685 | } |
| 1686 | if (x1 != x0) { |
| 1687 | if (x1 >= nameSpace && x1 < bounds.x + bounds.width) { |
| 1688 | gc.drawLine(x1, bounds.y, x1, bounds.y + bounds.height); |
| 1689 | } |
| 1690 | } |
| 1691 | } |
| 1692 | |
| 1693 | // draw selection background |
| 1694 | if (selectionBegin != 0 && selectionEnd != 0 && fDragState != DRAG_SELECTION) { |
| 1695 | x0 = Math.max(nameSpace, Math.min(bounds.x + bounds.width, x0)); |
| 1696 | x1 = Math.max(nameSpace, Math.min(bounds.x + bounds.width, x1)); |
| 1697 | gc.setBackground(getColorScheme().getBkColor(false, false, true)); |
| 1698 | if (x1 - x0 > 1) { |
| 1699 | gc.fillRectangle(new Rectangle(x0 + 1, bounds.y, x1 - x0 - 1, bounds.height)); |
| 1700 | } else if (x0 - x1 > 1) { |
| 1701 | gc.fillRectangle(new Rectangle(x1 + 1, bounds.y, x0 - x1 - 1, bounds.height)); |
| 1702 | } |
| 1703 | } |
| 1704 | |
| 1705 | // draw drag selection background |
| 1706 | if (fDragState == DRAG_ZOOM || fDragState == DRAG_SELECTION) { |
| 1707 | gc.setBackground(getColorScheme().getBkColor(false, false, true)); |
| 1708 | if (fDragX0 < fDragX) { |
| 1709 | gc.fillRectangle(new Rectangle(fDragX0, bounds.y, fDragX - fDragX0, bounds.height)); |
| 1710 | } else if (fDragX0 > fDragX) { |
| 1711 | gc.fillRectangle(new Rectangle(fDragX, bounds.y, fDragX0 - fDragX, bounds.height)); |
| 1712 | } |
| 1713 | } |
| 1714 | |
| 1715 | // draw drag line |
| 1716 | if (DRAG_SPLIT_LINE == fDragState) { |
| 1717 | gc.setForeground(getColorScheme().getColor(TimeGraphColorScheme.BLACK)); |
| 1718 | gc.drawLine(bounds.x + nameSpace, bounds.y, bounds.x + nameSpace, bounds.y + bounds.height - 1); |
| 1719 | } else if (DRAG_ZOOM == fDragState && Math.max(fDragX, fDragX0) > nameSpace) { |
| 1720 | gc.setForeground(getColorScheme().getColor(TimeGraphColorScheme.TOOL_FOREGROUND)); |
| 1721 | gc.drawLine(fDragX0, bounds.y, fDragX0, bounds.y + bounds.height - 1); |
| 1722 | if (fDragX != fDragX0) { |
| 1723 | gc.drawLine(fDragX, bounds.y, fDragX, bounds.y + bounds.height - 1); |
| 1724 | } |
| 1725 | } else if (DRAG_SELECTION == fDragState && Math.max(fDragX, fDragX0) > nameSpace) { |
| 1726 | gc.setForeground(getColorScheme().getColor(TimeGraphColorScheme.SELECTED_TIME)); |
| 1727 | gc.drawLine(fDragX0, bounds.y, fDragX0, bounds.y + bounds.height - 1); |
| 1728 | if (fDragX != fDragX0) { |
| 1729 | gc.drawLine(fDragX, bounds.y, fDragX, bounds.y + bounds.height - 1); |
| 1730 | } |
| 1731 | } else if (DRAG_NONE == fDragState && fMouseOverSplitLine && fTimeProvider.getNameSpace() > 0) { |
| 1732 | gc.setForeground(getColorScheme().getColor(TimeGraphColorScheme.RED)); |
| 1733 | gc.drawLine(bounds.x + nameSpace, bounds.y, bounds.x + nameSpace, bounds.y + bounds.height - 1); |
| 1734 | } |
| 1735 | |
| 1736 | gc.setAlpha(alpha); |
| 1737 | } |
| 1738 | |
| 1739 | /** |
| 1740 | * Draw the background layer. Fills the background of the control's name |
| 1741 | * space and states space, updates the background of items if necessary, |
| 1742 | * and draws the item's name text and middle line. |
| 1743 | * |
| 1744 | * @param bounds |
| 1745 | * The bounds of the control |
| 1746 | * @param nameSpace |
| 1747 | * The name space width |
| 1748 | * @param gc |
| 1749 | * Graphics context |
| 1750 | * @since 2.0 |
| 1751 | */ |
| 1752 | protected void drawBackground(Rectangle bounds, int nameSpace, GC gc) { |
| 1753 | // draw empty name space background |
| 1754 | gc.setBackground(getColorScheme().getBkColor(false, false, true)); |
| 1755 | drawBackground(gc, bounds.x, bounds.y, nameSpace, bounds.height); |
| 1756 | |
| 1757 | // draw empty states space background |
| 1758 | gc.setBackground(getColorScheme().getColor(TimeGraphColorScheme.BACKGROUND)); |
| 1759 | drawBackground(gc, bounds.x + nameSpace, bounds.y, bounds.width - nameSpace, bounds.height); |
| 1760 | |
| 1761 | for (int i = fTopIndex; i < fItemData.fExpandedItems.length; i++) { |
| 1762 | Rectangle itemRect = getItemRect(bounds, i); |
| 1763 | if (itemRect.y >= bounds.y + bounds.height) { |
| 1764 | break; |
| 1765 | } |
| 1766 | Item item = fItemData.fExpandedItems[i]; |
| 1767 | // draw the background of selected item and items with no time events |
| 1768 | if (! item.fEntry.hasTimeEvents()) { |
| 1769 | gc.setBackground(getColorScheme().getBkColorGroup(item.fSelected, fIsInFocus)); |
| 1770 | gc.fillRectangle(itemRect); |
| 1771 | } else if (item.fSelected) { |
| 1772 | gc.setBackground(getColorScheme().getBkColor(true, fIsInFocus, true)); |
| 1773 | gc.fillRectangle(itemRect.x, itemRect.y, nameSpace, itemRect.height); |
| 1774 | gc.setBackground(getColorScheme().getBkColor(true, fIsInFocus, false)); |
| 1775 | gc.fillRectangle(nameSpace, itemRect.y, itemRect.width - nameSpace, itemRect.height); |
| 1776 | } |
| 1777 | // draw the name and middle line |
| 1778 | if (! item.fEntry.hasTimeEvents()) { |
| 1779 | drawName(item, itemRect, gc); |
| 1780 | } else { |
| 1781 | Rectangle nameRect = new Rectangle(itemRect.x, itemRect.y, nameSpace, itemRect.height); |
| 1782 | drawName(item, nameRect, gc); |
| 1783 | Rectangle rect = new Rectangle(nameSpace, itemRect.y, itemRect.width - nameSpace, itemRect.height); |
| 1784 | drawMidLine(rect, gc); |
| 1785 | } |
| 1786 | } |
| 1787 | } |
| 1788 | |
| 1789 | /** |
| 1790 | * Draw the grid lines |
| 1791 | * |
| 1792 | * @param bounds |
| 1793 | * The bounds of the control |
| 1794 | * @param gc |
| 1795 | * Graphics context |
| 1796 | * @since 2.0 |
| 1797 | */ |
| 1798 | public void drawGridLines(Rectangle bounds, GC gc) { |
| 1799 | if (!fGridLinesVisible) { |
| 1800 | return; |
| 1801 | } |
| 1802 | gc.setForeground(fGridLineColor); |
| 1803 | gc.setAlpha(fGridLineColor.getAlpha()); |
| 1804 | for (int x : fTimeGraphScale.getTickList()) { |
| 1805 | gc.drawLine(x, bounds.y, x, bounds.y + bounds.height); |
| 1806 | } |
| 1807 | gc.setAlpha(255); |
| 1808 | } |
| 1809 | |
| 1810 | /** |
| 1811 | * Draw the markers |
| 1812 | * |
| 1813 | * @param bounds |
| 1814 | * The rectangle of the area |
| 1815 | * @param timeProvider |
| 1816 | * The time provider |
| 1817 | * @param markers |
| 1818 | * The list of markers |
| 1819 | * @param foreground |
| 1820 | * true to draw the foreground markers, false otherwise |
| 1821 | * @param nameSpace |
| 1822 | * The width reserved for the names |
| 1823 | * @param gc |
| 1824 | * Reference to the SWT GC object |
| 1825 | * @since 2.0 |
| 1826 | */ |
| 1827 | protected void drawMarkers(Rectangle bounds, ITimeDataProvider timeProvider, List<IMarkerEvent> markers, boolean foreground, int nameSpace, GC gc) { |
| 1828 | if (!fMarkersVisible || markers == null || markers.isEmpty()) { |
| 1829 | return; |
| 1830 | } |
| 1831 | gc.setClipping(new Rectangle(nameSpace, 0, bounds.width - nameSpace, bounds.height)); |
| 1832 | /* the list can grow concurrently but cannot shrink */ |
| 1833 | for (int i = 0; i < markers.size(); i++) { |
| 1834 | IMarkerEvent marker = markers.get(i); |
| 1835 | if (marker.isForeground() == foreground) { |
| 1836 | drawMarker(marker, bounds, timeProvider, nameSpace, gc); |
| 1837 | } |
| 1838 | } |
| 1839 | gc.setClipping((Rectangle) null); |
| 1840 | } |
| 1841 | |
| 1842 | /** |
| 1843 | * Draw a single marker |
| 1844 | * |
| 1845 | * @param marker |
| 1846 | * The marker event |
| 1847 | * @param bounds |
| 1848 | * The bounds of the control |
| 1849 | * @param timeProvider |
| 1850 | * The time provider |
| 1851 | * @param nameSpace |
| 1852 | * The width reserved for the name |
| 1853 | * @param gc |
| 1854 | * Reference to the SWT GC object |
| 1855 | * @since 2.0 |
| 1856 | */ |
| 1857 | protected void drawMarker(IMarkerEvent marker, Rectangle bounds, ITimeDataProvider timeProvider, int nameSpace, GC gc) { |
| 1858 | Rectangle rect = Utils.clone(bounds); |
| 1859 | if (marker.getEntry() != null) { |
| 1860 | int index = fItemData.findItemIndex(marker.getEntry()); |
| 1861 | if (index == -1) { |
| 1862 | return; |
| 1863 | } |
| 1864 | rect = getStatesRect(bounds, index, nameSpace); |
| 1865 | if (rect.y < 0 || rect.y > bounds.height) { |
| 1866 | return; |
| 1867 | } |
| 1868 | } |
| 1869 | int x0 = getXForTime(marker.getTime()); |
| 1870 | int x1 = getXForTime(marker.getTime() + marker.getDuration()); |
| 1871 | if (x0 > bounds.width || x1 < nameSpace) { |
| 1872 | return; |
| 1873 | } |
| 1874 | rect.x = Math.max(nameSpace, Math.min(bounds.width, x0)); |
| 1875 | rect.width = Math.max(1, Math.min(bounds.width, x1) - rect.x); |
| 1876 | |
| 1877 | Color color = getColorScheme().getColor(marker.getColor()); |
| 1878 | gc.setBackground(color); |
| 1879 | gc.setAlpha(color.getAlpha()); |
| 1880 | gc.fillRectangle(rect); |
| 1881 | gc.setAlpha(255); |
| 1882 | String label = marker.getLabel(); |
| 1883 | if (label != null && marker.getEntry() != null) { |
| 1884 | label = label.substring(0, Math.min(label.indexOf('\n') != -1 ? label.indexOf('\n') : label.length(), MAX_LABEL_LENGTH)); |
| 1885 | gc.setForeground(color); |
| 1886 | Utils.drawText(gc, label, rect.x - gc.textExtent(label).x, rect.y, true); |
| 1887 | } |
| 1888 | } |
| 1889 | |
| 1890 | /** |
| 1891 | * Draw many items at once |
| 1892 | * |
| 1893 | * @param bounds |
| 1894 | * The bounds of the control |
| 1895 | * @param timeProvider |
| 1896 | * The time provider |
| 1897 | * @param items |
| 1898 | * The array items to draw |
| 1899 | * @param topIndex |
| 1900 | * The index of the first element to draw |
| 1901 | * @param nameSpace |
| 1902 | * The name space width |
| 1903 | * @param gc |
| 1904 | * Graphics context |
| 1905 | */ |
| 1906 | public void drawItems(Rectangle bounds, ITimeDataProvider timeProvider, |
| 1907 | Item[] items, int topIndex, int nameSpace, GC gc) { |
| 1908 | for (int i = topIndex; i < items.length; i++) { |
| 1909 | Item item = items[i]; |
| 1910 | drawItem(item, bounds, timeProvider, i, nameSpace, gc); |
| 1911 | } |
| 1912 | } |
| 1913 | |
| 1914 | /** |
| 1915 | * Draws the item |
| 1916 | * |
| 1917 | * @param item |
| 1918 | * The item to draw |
| 1919 | * @param bounds |
| 1920 | * The bounds of the control |
| 1921 | * @param timeProvider |
| 1922 | * The time provider |
| 1923 | * @param i |
| 1924 | * The expanded item index |
| 1925 | * @param nameSpace |
| 1926 | * The name space width |
| 1927 | * @param gc |
| 1928 | * Graphics context |
| 1929 | */ |
| 1930 | protected void drawItem(Item item, Rectangle bounds, ITimeDataProvider timeProvider, int i, int nameSpace, GC gc) { |
| 1931 | Rectangle itemRect = getItemRect(bounds, i); |
| 1932 | if (itemRect.y >= bounds.y + bounds.height) { |
| 1933 | return; |
| 1934 | } |
| 1935 | |
| 1936 | ITimeGraphEntry entry = item.fEntry; |
| 1937 | long time0 = timeProvider.getTime0(); |
| 1938 | long time1 = timeProvider.getTime1(); |
| 1939 | long selectedTime = fTimeProvider.getSelectionEnd(); |
| 1940 | |
| 1941 | Rectangle rect = new Rectangle(nameSpace, itemRect.y, itemRect.width - nameSpace, itemRect.height); |
| 1942 | if (rect.isEmpty() || (time1 <= time0)) { |
| 1943 | fTimeGraphProvider.postDrawEntry(entry, rect, gc); |
| 1944 | return; |
| 1945 | } |
| 1946 | |
| 1947 | boolean selected = item.fSelected; |
| 1948 | // K pixels per second |
| 1949 | double pixelsPerNanoSec = (rect.width <= RIGHT_MARGIN) ? 0 : (double) (rect.width - RIGHT_MARGIN) / (time1 - time0); |
| 1950 | |
| 1951 | if (item.fEntry.hasTimeEvents()) { |
| 1952 | gc.setClipping(new Rectangle(nameSpace, 0, bounds.width - nameSpace, bounds.height)); |
| 1953 | fillSpace(rect, gc, selected); |
| 1954 | |
| 1955 | int margins = getMarginForHeight(rect.height); |
| 1956 | int height = rect.height - margins; |
| 1957 | int topMargin = (margins + 1) / 2; |
| 1958 | Rectangle stateRect = new Rectangle(rect.x, rect.y + topMargin, rect.width, height); |
| 1959 | |
| 1960 | /* Set the font for this item */ |
| 1961 | setFontForHeight(height, gc); |
| 1962 | |
| 1963 | long maxDuration = (timeProvider.getTimeSpace() == 0) ? Long.MAX_VALUE : 1 * (time1 - time0) / timeProvider.getTimeSpace(); |
| 1964 | Iterator<ITimeEvent> iterator = entry.getTimeEventsIterator(time0, time1, maxDuration); |
| 1965 | |
| 1966 | int lastX = -1; |
| 1967 | while (iterator.hasNext()) { |
| 1968 | ITimeEvent event = iterator.next(); |
| 1969 | int x = SaturatedArithmetic.add(rect.x, (int) ((event.getTime() - time0) * pixelsPerNanoSec)); |
| 1970 | int xEnd = SaturatedArithmetic.add(rect.x, (int) ((event.getTime() + event.getDuration() - time0) * pixelsPerNanoSec)); |
| 1971 | if (x >= rect.x + rect.width || xEnd < rect.x) { |
| 1972 | // event is out of bounds |
| 1973 | continue; |
| 1974 | } |
| 1975 | xEnd = Math.min(rect.x + rect.width, xEnd); |
| 1976 | stateRect.x = Math.max(rect.x, x); |
| 1977 | stateRect.width = Math.max(0, xEnd - stateRect.x + 1); |
| 1978 | if (stateRect.x == lastX) { |
| 1979 | stateRect.width -= 1; |
| 1980 | if (stateRect.width > 0) { |
| 1981 | gc.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK)); |
| 1982 | gc.drawPoint(stateRect.x, stateRect.y - 2); |
| 1983 | stateRect.x += 1; |
| 1984 | } |
| 1985 | } |
| 1986 | boolean timeSelected = selectedTime >= event.getTime() && selectedTime < event.getTime() + event.getDuration(); |
| 1987 | if (drawState(getColorScheme(), event, stateRect, gc, selected, timeSelected)) { |
| 1988 | lastX = stateRect.x; |
| 1989 | } |
| 1990 | } |
| 1991 | gc.setClipping((Rectangle) null); |
| 1992 | } |
| 1993 | fTimeGraphProvider.postDrawEntry(entry, rect, gc); |
| 1994 | } |
| 1995 | |
| 1996 | /** |
| 1997 | * Draw the links |
| 1998 | * |
| 1999 | * @param bounds |
| 2000 | * The bounds of the control |
| 2001 | * @param timeProvider |
| 2002 | * The time provider |
| 2003 | * @param links |
| 2004 | * The list of link events |
| 2005 | * @param nameSpace |
| 2006 | * The name space width |
| 2007 | * @param gc |
| 2008 | * Graphics context |
| 2009 | */ |
| 2010 | public void drawLinks(Rectangle bounds, ITimeDataProvider timeProvider, |
| 2011 | List<ILinkEvent> links, int nameSpace, GC gc) { |
| 2012 | if (fHideArrows) { |
| 2013 | return; |
| 2014 | } |
| 2015 | gc.setClipping(new Rectangle(nameSpace, 0, bounds.width - nameSpace, bounds.height)); |
| 2016 | /* the list can grow concurrently but cannot shrink */ |
| 2017 | for (int i = 0; i < links.size(); i++) { |
| 2018 | drawLink(links.get(i), bounds, timeProvider, nameSpace, gc); |
| 2019 | } |
| 2020 | gc.setClipping((Rectangle) null); |
| 2021 | } |
| 2022 | |
| 2023 | /** |
| 2024 | * Draws a link type event |
| 2025 | * |
| 2026 | * @param event |
| 2027 | * The link event to draw |
| 2028 | * @param bounds |
| 2029 | * The bounds of the control |
| 2030 | * @param timeProvider |
| 2031 | * The time provider |
| 2032 | * @param nameSpace |
| 2033 | * The name space width |
| 2034 | * @param gc |
| 2035 | * Graphics context |
| 2036 | */ |
| 2037 | protected void drawLink(ILinkEvent event, Rectangle bounds, ITimeDataProvider timeProvider, int nameSpace, GC gc) { |
| 2038 | drawArrow(getColorScheme(), event, getArrowRectangle(bounds, event), gc); |
| 2039 | } |
| 2040 | |
| 2041 | private Rectangle getArrowRectangle(Rectangle bounds, ILinkEvent event) { |
| 2042 | int srcIndex = fItemData.findItemIndex(event.getEntry()); |
| 2043 | int destIndex = fItemData.findItemIndex(event.getDestinationEntry()); |
| 2044 | |
| 2045 | if ((srcIndex == -1) || (destIndex == -1)) { |
| 2046 | return null; |
| 2047 | } |
| 2048 | |
| 2049 | Rectangle src = getStatesRect(bounds, srcIndex, fTimeProvider.getNameSpace()); |
| 2050 | Rectangle dst = getStatesRect(bounds, destIndex, fTimeProvider.getNameSpace()); |
| 2051 | |
| 2052 | int x0 = getXForTime(event.getTime()); |
| 2053 | int x1 = getXForTime(event.getTime() + event.getDuration()); |
| 2054 | |
| 2055 | // limit the x-coordinates to prevent integer overflow in calculations |
| 2056 | // and also GC.drawLine doesn't draw properly with large coordinates |
| 2057 | final int limit = Integer.MAX_VALUE / 1024; |
| 2058 | x0 = Math.max(-limit, Math.min(x0, limit)); |
| 2059 | x1 = Math.max(-limit, Math.min(x1, limit)); |
| 2060 | |
| 2061 | int y0 = src.y + src.height / 2; |
| 2062 | int y1 = dst.y + dst.height / 2; |
| 2063 | return new Rectangle(x0, y0, x1 - x0, y1 - y0); |
| 2064 | } |
| 2065 | |
| 2066 | /** |
| 2067 | * Draw an arrow |
| 2068 | * |
| 2069 | * @param colors |
| 2070 | * Color scheme |
| 2071 | * @param event |
| 2072 | * Time event for which we're drawing the arrow |
| 2073 | * @param rect |
| 2074 | * The arrow rectangle |
| 2075 | * @param gc |
| 2076 | * Graphics context |
| 2077 | * @return true if the arrow was drawn |
| 2078 | */ |
| 2079 | protected boolean drawArrow(TimeGraphColorScheme colors, ITimeEvent event, |
| 2080 | Rectangle rect, GC gc) { |
| 2081 | |
| 2082 | if (rect == null) { |
| 2083 | return false; |
| 2084 | } |
| 2085 | int colorIdx = fTimeGraphProvider.getStateTableIndex(event); |
| 2086 | if (colorIdx < 0) { |
| 2087 | return false; |
| 2088 | } |
| 2089 | boolean visible = ((rect.height == 0) && (rect.width == 0)) ? false : true; |
| 2090 | |
| 2091 | if (visible) { |
| 2092 | Color stateColor = null; |
| 2093 | if (colorIdx < fEventColorMap.length) { |
| 2094 | stateColor = fEventColorMap[colorIdx]; |
| 2095 | } else { |
| 2096 | stateColor = Display.getDefault().getSystemColor(SWT.COLOR_BLACK); |
| 2097 | } |
| 2098 | |
| 2099 | gc.setForeground(stateColor); |
| 2100 | gc.setBackground(stateColor); |
| 2101 | |
| 2102 | /* Draw the arrow */ |
| 2103 | gc.drawLine(rect.x, rect.y, rect.x + rect.width, rect.y + rect.height); |
| 2104 | drawArrowHead(rect.x, rect.y, rect.x + rect.width, rect.y + rect.height, gc); |
| 2105 | |
| 2106 | } |
| 2107 | fTimeGraphProvider.postDrawEvent(event, rect, gc); |
| 2108 | return visible; |
| 2109 | } |
| 2110 | |
| 2111 | /* |
| 2112 | * @author Francis Giraldeau |
| 2113 | * |
| 2114 | * Inspiration: |
| 2115 | * http://stackoverflow.com/questions/3010803/draw-arrow-on-line-algorithm |
| 2116 | * |
| 2117 | * The algorithm was taken from this site, not the code itself |
| 2118 | */ |
| 2119 | private static void drawArrowHead(int x0, int y0, int x1, int y1, GC gc) |
| 2120 | { |
| 2121 | int factor = 10; |
| 2122 | double cos = 0.9510; |
| 2123 | double sin = 0.3090; |
| 2124 | long lenx = x1 - x0; |
| 2125 | long leny = y1 - y0; |
| 2126 | double len = Math.sqrt(lenx * lenx + leny * leny); |
| 2127 | |
| 2128 | double dx = factor * lenx / len; |
| 2129 | double dy = factor * leny / len; |
| 2130 | int end1X = (int) Math.round((x1 - (dx * cos + dy * -sin))); |
| 2131 | int end1Y = (int) Math.round((y1 - (dx * sin + dy * cos))); |
| 2132 | int end2X = (int) Math.round((x1 - (dx * cos + dy * sin))); |
| 2133 | int end2Y = (int) Math.round((y1 - (dx * -sin + dy * cos))); |
| 2134 | int[] arrow = new int[] { x1, y1, end1X, end1Y, end2X, end2Y, x1, y1 }; |
| 2135 | gc.fillPolygon(arrow); |
| 2136 | } |
| 2137 | |
| 2138 | /** |
| 2139 | * Draw the name of an item. |
| 2140 | * |
| 2141 | * @param item |
| 2142 | * Item object |
| 2143 | * @param bounds |
| 2144 | * The bounds of the item's name space |
| 2145 | * @param gc |
| 2146 | * Graphics context |
| 2147 | */ |
| 2148 | protected void drawName(Item item, Rectangle bounds, GC gc) { |
| 2149 | boolean hasTimeEvents = item.fEntry.hasTimeEvents(); |
| 2150 | |
| 2151 | // No name to be drawn |
| 2152 | if (fTimeProvider.getNameSpace() == 0) { |
| 2153 | return; |
| 2154 | } |
| 2155 | |
| 2156 | int height = bounds.height - getMarginForHeight(bounds.height); |
| 2157 | setFontForHeight(height, gc); |
| 2158 | |
| 2159 | int leftMargin = MARGIN + item.fLevel * EXPAND_SIZE; |
| 2160 | if (item.fHasChildren) { |
| 2161 | gc.setForeground(getColorScheme().getFgColorGroup(false, false)); |
| 2162 | gc.setBackground(getColorScheme().getBkColor(false, false, false)); |
| 2163 | Rectangle rect = Utils.clone(bounds); |
| 2164 | rect.x += leftMargin; |
| 2165 | rect.y += (bounds.height - EXPAND_SIZE) / 2; |
| 2166 | rect.width = EXPAND_SIZE; |
| 2167 | rect.height = EXPAND_SIZE; |
| 2168 | gc.fillRectangle(rect); |
| 2169 | gc.drawRectangle(rect.x, rect.y, rect.width - 1, rect.height - 1); |
| 2170 | int midy = rect.y + rect.height / 2; |
| 2171 | gc.drawLine(rect.x + 2, midy, rect.x + rect.width - 3, midy); |
| 2172 | if (!item.fExpanded) { |
| 2173 | int midx = rect.x + rect.width / 2; |
| 2174 | gc.drawLine(midx, rect.y + 2, midx, rect.y + rect.height - 3); |
| 2175 | } |
| 2176 | } |
| 2177 | leftMargin += EXPAND_SIZE + MARGIN; |
| 2178 | |
| 2179 | Image img = fTimeGraphProvider.getItemImage(item.fEntry); |
| 2180 | if (img != null) { |
| 2181 | // draw icon |
| 2182 | int imgHeight = img.getImageData().height; |
| 2183 | int imgWidth = img.getImageData().width; |
| 2184 | int x = leftMargin; |
| 2185 | int y = bounds.y + (bounds.height - imgHeight) / 2; |
| 2186 | gc.drawImage(img, x, y); |
| 2187 | leftMargin += imgWidth + MARGIN; |
| 2188 | } |
| 2189 | String name = item.fName; |
| 2190 | Point size = gc.stringExtent(name); |
| 2191 | if (fIdealNameSpace < leftMargin + size.x + MARGIN) { |
| 2192 | fIdealNameSpace = leftMargin + size.x + MARGIN; |
| 2193 | } |
| 2194 | if (hasTimeEvents) { |
| 2195 | // cut long string with "..." |
| 2196 | int width = bounds.width - leftMargin; |
| 2197 | int cuts = 0; |
| 2198 | while (size.x > width && name.length() > 1) { |
| 2199 | cuts++; |
| 2200 | name = name.substring(0, name.length() - 1); |
| 2201 | size = gc.stringExtent(name + "..."); //$NON-NLS-1$ |
| 2202 | } |
| 2203 | if (cuts > 0) { |
| 2204 | name += "..."; //$NON-NLS-1$ |
| 2205 | } |
| 2206 | } |
| 2207 | Rectangle rect = Utils.clone(bounds); |
| 2208 | rect.x += leftMargin; |
| 2209 | rect.width -= leftMargin; |
| 2210 | // draw text |
| 2211 | if (rect.width > 0) { |
| 2212 | rect.y += (bounds.height - gc.stringExtent(name).y) / 2; |
| 2213 | gc.setForeground(getColorScheme().getFgColor(item.fSelected, fIsInFocus)); |
| 2214 | int textWidth = Utils.drawText(gc, name, rect, true); |
| 2215 | leftMargin += textWidth + MARGIN; |
| 2216 | |
| 2217 | if (hasTimeEvents) { |
| 2218 | // draw middle line |
| 2219 | rect.x = bounds.x + leftMargin; |
| 2220 | rect.y = bounds.y; |
| 2221 | rect.width = bounds.width - rect.x; |
| 2222 | drawMidLine(rect, gc); |
| 2223 | } |
| 2224 | } |
| 2225 | } |
| 2226 | |
| 2227 | /** |
| 2228 | * Draw the state (color fill) |
| 2229 | * |
| 2230 | * @param colors |
| 2231 | * Color scheme |
| 2232 | * @param event |
| 2233 | * Time event for which we're drawing the state |
| 2234 | * @param rect |
| 2235 | * The state rectangle |
| 2236 | * @param gc |
| 2237 | * Graphics context |
| 2238 | * @param selected |
| 2239 | * Is this time event currently selected (so it appears |
| 2240 | * highlighted) |
| 2241 | * @param timeSelected |
| 2242 | * Is the timestamp currently selected |
| 2243 | * @return true if the state was drawn |
| 2244 | */ |
| 2245 | protected boolean drawState(TimeGraphColorScheme colors, ITimeEvent event, |
| 2246 | Rectangle rect, GC gc, boolean selected, boolean timeSelected) { |
| 2247 | |
| 2248 | int colorIdx = fTimeGraphProvider.getStateTableIndex(event); |
| 2249 | if (colorIdx < 0 && colorIdx != ITimeGraphPresentationProvider.TRANSPARENT) { |
| 2250 | return false; |
| 2251 | } |
| 2252 | boolean visible = rect.width == 0 ? false : true; |
| 2253 | rect.width = Math.max(1, rect.width); |
| 2254 | Color black = Display.getDefault().getSystemColor(SWT.COLOR_BLACK); |
| 2255 | gc.setForeground(black); |
| 2256 | |
| 2257 | if (colorIdx == ITimeGraphPresentationProvider.TRANSPARENT) { |
| 2258 | if (visible) { |
| 2259 | // Only draw the top and bottom borders |
| 2260 | gc.drawLine(rect.x, rect.y, rect.x + rect.width - 1, rect.y); |
| 2261 | gc.drawLine(rect.x, rect.y + rect.height - 1, rect.x + rect.width - 1, rect.y + rect.height - 1); |
| 2262 | if (rect.width == 1) { |
| 2263 | gc.drawPoint(rect.x, rect.y - 2); |
| 2264 | } |
| 2265 | } |
| 2266 | fTimeGraphProvider.postDrawEvent(event, rect, gc); |
| 2267 | return false; |
| 2268 | } |
| 2269 | Color stateColor = null; |
| 2270 | if (colorIdx < fEventColorMap.length) { |
| 2271 | stateColor = fEventColorMap[colorIdx]; |
| 2272 | } else { |
| 2273 | stateColor = black; |
| 2274 | } |
| 2275 | |
| 2276 | boolean reallySelected = timeSelected && selected; |
| 2277 | // fill all rect area |
| 2278 | gc.setBackground(stateColor); |
| 2279 | if (visible) { |
| 2280 | gc.fillRectangle(rect); |
| 2281 | } else if (fBlendSubPixelEvents) { |
| 2282 | gc.setAlpha(128); |
| 2283 | gc.fillRectangle(rect); |
| 2284 | gc.setAlpha(255); |
| 2285 | } |
| 2286 | |
| 2287 | if (reallySelected) { |
| 2288 | gc.drawLine(rect.x, rect.y - 1, rect.x + rect.width - 1, rect.y - 1); |
| 2289 | gc.drawLine(rect.x, rect.y + rect.height, rect.x + rect.width - 1, rect.y + rect.height); |
| 2290 | } |
| 2291 | if (!visible) { |
| 2292 | gc.drawPoint(rect.x, rect.y - 2); |
| 2293 | } |
| 2294 | fTimeGraphProvider.postDrawEvent(event, rect, gc); |
| 2295 | return visible; |
| 2296 | } |
| 2297 | |
| 2298 | /** |
| 2299 | * Fill an item's states rectangle |
| 2300 | * |
| 2301 | * @param rect |
| 2302 | * The states rectangle |
| 2303 | * @param gc |
| 2304 | * Graphics context |
| 2305 | * @param selected |
| 2306 | * true if the item is selected |
| 2307 | */ |
| 2308 | protected void fillSpace(Rectangle rect, GC gc, boolean selected) { |
| 2309 | /* Nothing to draw */ |
| 2310 | } |
| 2311 | |
| 2312 | /** |
| 2313 | * Draw a line at the middle height of a rectangle |
| 2314 | * |
| 2315 | * @param rect |
| 2316 | * The rectangle |
| 2317 | * @param gc |
| 2318 | * Graphics context |
| 2319 | */ |
| 2320 | private void drawMidLine(Rectangle rect, GC gc) { |
| 2321 | gc.setForeground(getColorScheme().getColor(TimeGraphColorScheme.MID_LINE)); |
| 2322 | int midy = rect.y + rect.height / 2; |
| 2323 | gc.drawLine(rect.x, midy, rect.x + rect.width, midy); |
| 2324 | } |
| 2325 | |
| 2326 | private static int getMarginForHeight(int height) { |
| 2327 | /* |
| 2328 | * State rectangle is smaller than the item bounds when height is > 4. |
| 2329 | * Don't use any margin if the height is below or equal that threshold. |
| 2330 | * Use a maximum of 6 pixels for both margins, otherwise try to use 13 |
| 2331 | * pixels for the state height, but with a minimum margin of 1. |
| 2332 | */ |
| 2333 | final int MARGIN_THRESHOLD = 4; |
| 2334 | final int PREFERRED_HEIGHT = 13; |
| 2335 | final int MIN_MARGIN = 1; |
| 2336 | final int MAX_MARGIN = 6; |
| 2337 | return height <= MARGIN_THRESHOLD ? 0 : |
| 2338 | Math.max(Math.min(height - PREFERRED_HEIGHT, MAX_MARGIN), MIN_MARGIN); |
| 2339 | } |
| 2340 | |
| 2341 | private void setFontForHeight(int pixels, GC gc) { |
| 2342 | /* convert font height from pixels to points */ |
| 2343 | int height = Math.max(pixels * PPI / DPI, 1); |
| 2344 | Font font = fFonts.get(height); |
| 2345 | if (font == null) { |
| 2346 | FontData fontData = gc.getFont().getFontData()[0]; |
| 2347 | fontData.setHeight(height); |
| 2348 | font = new Font(gc.getDevice(), fontData); |
| 2349 | fFonts.put(height, font); |
| 2350 | } |
| 2351 | gc.setFont(font); |
| 2352 | } |
| 2353 | |
| 2354 | @Override |
| 2355 | public void keyTraversed(TraverseEvent e) { |
| 2356 | if ((e.detail == SWT.TRAVERSE_TAB_NEXT) || (e.detail == SWT.TRAVERSE_TAB_PREVIOUS)) { |
| 2357 | e.doit = true; |
| 2358 | } |
| 2359 | } |
| 2360 | |
| 2361 | @Override |
| 2362 | public void keyPressed(KeyEvent e) { |
| 2363 | int idx = -1; |
| 2364 | if (fItemData.fExpandedItems.length == 0) { |
| 2365 | return; |
| 2366 | } |
| 2367 | if (SWT.HOME == e.keyCode) { |
| 2368 | idx = 0; |
| 2369 | } else if (SWT.END == e.keyCode) { |
| 2370 | idx = fItemData.fExpandedItems.length - 1; |
| 2371 | } else if (SWT.ARROW_DOWN == e.keyCode) { |
| 2372 | idx = getSelectedIndex(); |
| 2373 | if (idx < 0) { |
| 2374 | idx = 0; |
| 2375 | } else if (idx < fItemData.fExpandedItems.length - 1) { |
| 2376 | idx++; |
| 2377 | } |
| 2378 | } else if (SWT.ARROW_UP == e.keyCode) { |
| 2379 | idx = getSelectedIndex(); |
| 2380 | if (idx < 0) { |
| 2381 | idx = 0; |
| 2382 | } else if (idx > 0) { |
| 2383 | idx--; |
| 2384 | } |
| 2385 | } else if (SWT.ARROW_LEFT == e.keyCode && fDragState == DRAG_NONE) { |
| 2386 | boolean extend = (e.stateMask & SWT.SHIFT) != 0; |
| 2387 | selectPrevEvent(extend); |
| 2388 | } else if (SWT.ARROW_RIGHT == e.keyCode && fDragState == DRAG_NONE) { |
| 2389 | boolean extend = (e.stateMask & SWT.SHIFT) != 0; |
| 2390 | selectNextEvent(extend); |
| 2391 | } else if (SWT.PAGE_DOWN == e.keyCode) { |
| 2392 | int page = countPerPage(); |
| 2393 | idx = getSelectedIndex(); |
| 2394 | if (idx < 0) { |
| 2395 | idx = 0; |
| 2396 | } |
| 2397 | idx += page; |
| 2398 | if (idx >= fItemData.fExpandedItems.length) { |
| 2399 | idx = fItemData.fExpandedItems.length - 1; |
| 2400 | } |
| 2401 | } else if (SWT.PAGE_UP == e.keyCode) { |
| 2402 | int page = countPerPage(); |
| 2403 | idx = getSelectedIndex(); |
| 2404 | if (idx < 0) { |
| 2405 | idx = 0; |
| 2406 | } |
| 2407 | idx -= page; |
| 2408 | if (idx < 0) { |
| 2409 | idx = 0; |
| 2410 | } |
| 2411 | } else if (SWT.CR == e.keyCode) { |
| 2412 | idx = getSelectedIndex(); |
| 2413 | if (idx >= 0) { |
| 2414 | if (fItemData.fExpandedItems[idx].fHasChildren) { |
| 2415 | toggle(idx); |
| 2416 | } else { |
| 2417 | fireDefaultSelection(); |
| 2418 | } |
| 2419 | } |
| 2420 | idx = -1; |
| 2421 | } else if ((e.character == '+' || e.character == '=') && ((e.stateMask & SWT.CTRL) != 0)) { |
| 2422 | fVerticalZoomAlignEntry = getVerticalZoomAlignSelection(); |
| 2423 | verticalZoom(true); |
| 2424 | if (fVerticalZoomAlignEntry != null) { |
| 2425 | setElementPosition(fVerticalZoomAlignEntry.getKey(), fVerticalZoomAlignEntry.getValue()); |
| 2426 | } |
| 2427 | } else if (e.character == '-' && ((e.stateMask & SWT.CTRL) != 0)) { |
| 2428 | fVerticalZoomAlignEntry = getVerticalZoomAlignSelection(); |
| 2429 | verticalZoom(false); |
| 2430 | if (fVerticalZoomAlignEntry != null) { |
| 2431 | setElementPosition(fVerticalZoomAlignEntry.getKey(), fVerticalZoomAlignEntry.getValue()); |
| 2432 | } |
| 2433 | } else if (e.character == '0' && ((e.stateMask & SWT.CTRL) != 0)) { |
| 2434 | fVerticalZoomAlignEntry = getVerticalZoomAlignSelection(); |
| 2435 | resetVerticalZoom(); |
| 2436 | if (fVerticalZoomAlignEntry != null) { |
| 2437 | setElementPosition(fVerticalZoomAlignEntry.getKey(), fVerticalZoomAlignEntry.getValue()); |
| 2438 | } |
| 2439 | } else if ((e.character == '+' || e.character == '=') && ((e.stateMask & SWT.CTRL) == 0)) { |
| 2440 | if (fHasNamespaceFocus) { |
| 2441 | ITimeGraphEntry entry = getSelectedTrace(); |
| 2442 | setExpandedState(entry, 0, true); |
| 2443 | } else { |
| 2444 | zoomIn(); |
| 2445 | } |
| 2446 | } else if (e.character == '-' && ((e.stateMask & SWT.CTRL) == 0)) { |
| 2447 | if (fHasNamespaceFocus) { |
| 2448 | ITimeGraphEntry entry = getSelectedTrace(); |
| 2449 | if ((entry != null) && entry.hasChildren()) { |
| 2450 | setExpandedState(entry, -1, false); |
| 2451 | } |
| 2452 | } else { |
| 2453 | zoomOut(); |
| 2454 | } |
| 2455 | } else if ((e.character == '*') && ((e.stateMask & SWT.CTRL) == 0)) { |
| 2456 | if (fHasNamespaceFocus) { |
| 2457 | ITimeGraphEntry entry = getSelectedTrace(); |
| 2458 | if ((entry != null) && entry.hasChildren()) { |
| 2459 | setExpandedStateLevel(entry); |
| 2460 | } |
| 2461 | } |
| 2462 | } |
| 2463 | if (idx >= 0) { |
| 2464 | selectItem(idx, false); |
| 2465 | fireSelectionChanged(); |
| 2466 | } |
| 2467 | int x = toControl(e.display.getCursorLocation()).x; |
| 2468 | updateCursor(x, e.stateMask | e.keyCode); |
| 2469 | } |
| 2470 | |
| 2471 | @Override |
| 2472 | public void keyReleased(KeyEvent e) { |
| 2473 | int x = toControl(e.display.getCursorLocation()).x; |
| 2474 | updateCursor(x, e.stateMask & ~e.keyCode); |
| 2475 | } |
| 2476 | |
| 2477 | @Override |
| 2478 | public void focusGained(FocusEvent e) { |
| 2479 | fIsInFocus = true; |
| 2480 | redraw(); |
| 2481 | updateStatusLine(STATUS_WITHOUT_CURSOR_TIME); |
| 2482 | } |
| 2483 | |
| 2484 | @Override |
| 2485 | public void focusLost(FocusEvent e) { |
| 2486 | fIsInFocus = false; |
| 2487 | if (DRAG_NONE != fDragState) { |
| 2488 | setCapture(false); |
| 2489 | fDragState = DRAG_NONE; |
| 2490 | } |
| 2491 | redraw(); |
| 2492 | updateStatusLine(NO_STATUS); |
| 2493 | } |
| 2494 | |
| 2495 | /** |
| 2496 | * @return If the current view is focused |
| 2497 | */ |
| 2498 | public boolean isInFocus() { |
| 2499 | return fIsInFocus; |
| 2500 | } |
| 2501 | |
| 2502 | /** |
| 2503 | * Provide the possibility to control the wait cursor externally e.g. data |
| 2504 | * requests in progress |
| 2505 | * |
| 2506 | * @param waitInd Should we wait indefinitely? |
| 2507 | */ |
| 2508 | public void waitCursor(boolean waitInd) { |
| 2509 | // Update cursor as indicated |
| 2510 | if (waitInd) { |
| 2511 | setCursor(fWaitCursor); |
| 2512 | } else { |
| 2513 | setCursor(null); |
| 2514 | } |
| 2515 | } |
| 2516 | |
| 2517 | private void updateCursor(int x, int stateMask) { |
| 2518 | // if Wait cursor not active, check for the need to change the cursor |
| 2519 | if (getCursor() == fWaitCursor) { |
| 2520 | return; |
| 2521 | } |
| 2522 | Cursor cursor = null; |
| 2523 | if (fDragState == DRAG_SPLIT_LINE) { |
| 2524 | } else if (fDragState == DRAG_SELECTION) { |
| 2525 | cursor = fResizeCursor; |
| 2526 | } else if (fDragState == DRAG_TRACE_ITEM) { |
| 2527 | cursor = fDragCursor; |
| 2528 | } else if (fDragState == DRAG_ZOOM) { |
| 2529 | cursor = fZoomCursor; |
| 2530 | } else if ((stateMask & SWT.MODIFIER_MASK) == SWT.CTRL) { |
| 2531 | cursor = fDragCursor; |
| 2532 | } else if ((stateMask & SWT.MODIFIER_MASK) == SWT.SHIFT) { |
| 2533 | cursor = fResizeCursor; |
| 2534 | } else if (!isOverSplitLine(x)) { |
| 2535 | long selectionBegin = fTimeProvider.getSelectionBegin(); |
| 2536 | long selectionEnd = fTimeProvider.getSelectionEnd(); |
| 2537 | int xBegin = getXForTime(selectionBegin); |
| 2538 | int xEnd = getXForTime(selectionEnd); |
| 2539 | if (Math.abs(x - xBegin) < SNAP_WIDTH || Math.abs(x - xEnd) < SNAP_WIDTH) { |
| 2540 | cursor = fResizeCursor; |
| 2541 | } |
| 2542 | } |
| 2543 | if (getCursor() != cursor) { |
| 2544 | setCursor(cursor); |
| 2545 | } |
| 2546 | } |
| 2547 | |
| 2548 | /** |
| 2549 | * Update the status line following a change of selection. |
| 2550 | * |
| 2551 | * @since 2.0 |
| 2552 | */ |
| 2553 | public void updateStatusLine() { |
| 2554 | updateStatusLine(STATUS_WITHOUT_CURSOR_TIME); |
| 2555 | } |
| 2556 | |
| 2557 | private void updateStatusLine(int x) { |
| 2558 | // use the time provider of the time graph scale for the status line |
| 2559 | ITimeDataProvider tdp = fTimeGraphScale.getTimeProvider(); |
| 2560 | if (fStatusLineManager == null || null == tdp || |
| 2561 | tdp.getTime0() == tdp.getTime1()) { |
| 2562 | return; |
| 2563 | } |
| 2564 | TimeFormat tf = tdp.getTimeFormat(); |
| 2565 | Resolution res = Resolution.NANOSEC; |
| 2566 | StringBuilder message = new StringBuilder(); |
| 2567 | if ((x >= 0 || x == STATUS_WITHOUT_CURSOR_TIME) && fDragState == DRAG_NONE) { |
| 2568 | if (x != STATUS_WITHOUT_CURSOR_TIME) { |
| 2569 | long time = getTimeAtX(x); |
| 2570 | if (time >= 0) { |
| 2571 | if (tdp instanceof ITimeDataProviderConverter) { |
| 2572 | time = ((ITimeDataProviderConverter) tdp).convertTime(time); |
| 2573 | } |
| 2574 | message.append(NLS.bind("T: {0}{1} ", //$NON-NLS-1$ |
| 2575 | new Object[] { |
| 2576 | tf == TimeFormat.CALENDAR ? Utils.formatDate(time) + ' ' : "", //$NON-NLS-1$ |
| 2577 | Utils.formatTime(time, tf, res) |
| 2578 | })); |
| 2579 | } |
| 2580 | } |
| 2581 | long selectionBegin = tdp.getSelectionBegin(); |
| 2582 | long selectionEnd = tdp.getSelectionEnd(); |
| 2583 | message.append(NLS.bind("T1: {0}{1}", //$NON-NLS-1$ |
| 2584 | new Object[] { |
| 2585 | tf == TimeFormat.CALENDAR ? Utils.formatDate(selectionBegin) + ' ' : "", //$NON-NLS-1$ |
| 2586 | Utils.formatTime(selectionBegin, tf, res) |
| 2587 | })); |
| 2588 | if (selectionBegin != selectionEnd) { |
| 2589 | message.append(NLS.bind(" T2: {0}{1} \u0394: {2}", //$NON-NLS-1$ |
| 2590 | new Object[] { |
| 2591 | tf == TimeFormat.CALENDAR ? Utils.formatDate(selectionEnd) + ' ' : "", //$NON-NLS-1$ |
| 2592 | Utils.formatTime(selectionEnd, tf, res), |
| 2593 | Utils.formatDelta(selectionEnd - selectionBegin, tf, res) |
| 2594 | })); |
| 2595 | } |
| 2596 | } else if (fDragState == DRAG_SELECTION || fDragState == DRAG_ZOOM) { |
| 2597 | long time0 = fDragBeginMarker ? getTimeAtX(fDragX0) : fDragTime0; |
| 2598 | long time = fDragBeginMarker ? fDragTime0 : getTimeAtX(fDragX); |
| 2599 | if (tdp instanceof ITimeDataProviderConverter) { |
| 2600 | time0 = ((ITimeDataProviderConverter) tdp).convertTime(time0); |
| 2601 | time = ((ITimeDataProviderConverter) tdp).convertTime(time); |
| 2602 | } |
| 2603 | message.append(NLS.bind("T1: {0}{1} T2: {2}{3} \u0394: {4}", //$NON-NLS-1$ |
| 2604 | new Object[] { |
| 2605 | tf == TimeFormat.CALENDAR ? Utils.formatDate(time0) + ' ' : "", //$NON-NLS-1$ |
| 2606 | Utils.formatTime(time0, tf, res), |
| 2607 | tf == TimeFormat.CALENDAR ? Utils.formatDate(time) + ' ' : "", //$NON-NLS-1$ |
| 2608 | Utils.formatTime(time, tf, res), |
| 2609 | Utils.formatDelta(time - time0, tf, res) |
| 2610 | })); |
| 2611 | } |
| 2612 | fStatusLineManager.setMessage(message.toString()); |
| 2613 | } |
| 2614 | |
| 2615 | @Override |
| 2616 | public void mouseMove(MouseEvent e) { |
| 2617 | if (null == fTimeProvider) { |
| 2618 | return; |
| 2619 | } |
| 2620 | Point size = getSize(); |
| 2621 | if (DRAG_TRACE_ITEM == fDragState) { |
| 2622 | int nameWidth = fTimeProvider.getNameSpace(); |
| 2623 | if (e.x > nameWidth && size.x > nameWidth && fDragX != e.x) { |
| 2624 | fDragX = e.x; |
| 2625 | double pixelsPerNanoSec = (size.x - nameWidth <= RIGHT_MARGIN) ? 0 : (double) (size.x - nameWidth - RIGHT_MARGIN) / (fTime1bak - fTime0bak); |
| 2626 | long timeDelta = (long) ((pixelsPerNanoSec == 0) ? 0 : ((fDragX - fDragX0) / pixelsPerNanoSec)); |
| 2627 | long time1 = fTime1bak - timeDelta; |
| 2628 | long maxTime = fTimeProvider.getMaxTime(); |
| 2629 | if (time1 > maxTime) { |
| 2630 | time1 = maxTime; |
| 2631 | } |
| 2632 | long time0 = time1 - (fTime1bak - fTime0bak); |
| 2633 | if (time0 < fTimeProvider.getMinTime()) { |
| 2634 | time0 = fTimeProvider.getMinTime(); |
| 2635 | time1 = time0 + (fTime1bak - fTime0bak); |
| 2636 | } |
| 2637 | fTimeProvider.setStartFinishTimeNotify(time0, time1); |
| 2638 | } |
| 2639 | } else if (DRAG_SPLIT_LINE == fDragState) { |
| 2640 | fDragX = e.x; |
| 2641 | fTimeProvider.setNameSpace(e.x); |
| 2642 | TmfSignalManager.dispatchSignal(new TmfTimeViewAlignmentSignal(this, getTimeViewAlignmentInfo())); |
| 2643 | } else if (DRAG_SELECTION == fDragState) { |
| 2644 | if (fDragBeginMarker) { |
| 2645 | fDragX0 = Math.min(Math.max(e.x, fTimeProvider.getNameSpace()), size.x - RIGHT_MARGIN); |
| 2646 | } else { |
| 2647 | fDragX = Math.min(Math.max(e.x, fTimeProvider.getNameSpace()), size.x - RIGHT_MARGIN); |
| 2648 | } |
| 2649 | redraw(); |
| 2650 | fTimeGraphScale.setDragRange(fDragX0, fDragX); |
| 2651 | fireDragSelectionChanged(getTimeAtX(fDragX0), getTimeAtX(fDragX)); |
| 2652 | } else if (DRAG_ZOOM == fDragState) { |
| 2653 | fDragX = Math.min(Math.max(e.x, fTimeProvider.getNameSpace()), size.x - RIGHT_MARGIN); |
| 2654 | redraw(); |
| 2655 | fTimeGraphScale.setDragRange(fDragX0, fDragX); |
| 2656 | } else if (DRAG_NONE == fDragState) { |
| 2657 | boolean mouseOverSplitLine = isOverSplitLine(e.x); |
| 2658 | if (fMouseOverSplitLine != mouseOverSplitLine) { |
| 2659 | redraw(); |
| 2660 | } |
| 2661 | fMouseOverSplitLine = mouseOverSplitLine; |
| 2662 | } |
| 2663 | |
| 2664 | if (e.x >= fTimeProvider.getNameSpace()) { |
| 2665 | fHasNamespaceFocus = false; |
| 2666 | } else { |
| 2667 | fHasNamespaceFocus = true; |
| 2668 | } |
| 2669 | updateCursor(e.x, e.stateMask); |
| 2670 | updateStatusLine(e.x); |
| 2671 | } |
| 2672 | |
| 2673 | @Override |
| 2674 | public void mouseDoubleClick(MouseEvent e) { |
| 2675 | if (null == fTimeProvider) { |
| 2676 | return; |
| 2677 | } |
| 2678 | if (1 == e.button && (e.stateMask & SWT.BUTTON_MASK) == 0) { |
| 2679 | if (isOverSplitLine(e.x) && fTimeProvider.getNameSpace() != 0) { |
| 2680 | fTimeProvider.setNameSpace(fIdealNameSpace); |
| 2681 | boolean mouseOverSplitLine = isOverSplitLine(e.x); |
| 2682 | if (fMouseOverSplitLine != mouseOverSplitLine) { |
| 2683 | redraw(); |
| 2684 | } |
| 2685 | fMouseOverSplitLine = mouseOverSplitLine; |
| 2686 | TmfSignalManager.dispatchSignal(new TmfTimeViewAlignmentSignal(this, getTimeViewAlignmentInfo())); |
| 2687 | return; |
| 2688 | } |
| 2689 | int idx = getItemIndexAtY(e.y); |
| 2690 | if (idx >= 0) { |
| 2691 | selectItem(idx, false); |
| 2692 | fireDefaultSelection(); |
| 2693 | } |
| 2694 | } |
| 2695 | } |
| 2696 | |
| 2697 | @Override |
| 2698 | public void mouseDown(MouseEvent e) { |
| 2699 | if (fDragState != DRAG_NONE || null == fTimeProvider || |
| 2700 | fTimeProvider.getTime0() == fTimeProvider.getTime1() || |
| 2701 | getSize().x - fTimeProvider.getNameSpace() <= 0) { |
| 2702 | return; |
| 2703 | } |
| 2704 | int idx; |
| 2705 | if (1 == e.button && (e.stateMask & SWT.MODIFIER_MASK) == 0) { |
| 2706 | int nameSpace = fTimeProvider.getNameSpace(); |
| 2707 | if (nameSpace != 0 && isOverSplitLine(e.x)) { |
| 2708 | fDragState = DRAG_SPLIT_LINE; |
| 2709 | fDragButton = e.button; |
| 2710 | fDragX = e.x; |
| 2711 | fDragX0 = fDragX; |
| 2712 | fTime0bak = fTimeProvider.getTime0(); |
| 2713 | fTime1bak = fTimeProvider.getTime1(); |
| 2714 | redraw(); |
| 2715 | updateCursor(e.x, e.stateMask); |
| 2716 | return; |
| 2717 | } |
| 2718 | } |
| 2719 | if (1 == e.button && ((e.stateMask & SWT.MODIFIER_MASK) == 0 || (e.stateMask & SWT.MODIFIER_MASK) == SWT.SHIFT)) { |
| 2720 | int nameSpace = fTimeProvider.getNameSpace(); |
| 2721 | idx = getItemIndexAtY(e.y); |
| 2722 | if (idx >= 0) { |
| 2723 | Item item = fItemData.fExpandedItems[idx]; |
| 2724 | if (item.fHasChildren && e.x < nameSpace && e.x < MARGIN + (item.fLevel + 1) * EXPAND_SIZE) { |
| 2725 | toggle(idx); |
| 2726 | return; |
| 2727 | } |
| 2728 | selectItem(idx, false); |
| 2729 | fireSelectionChanged(); |
| 2730 | } else { |
| 2731 | selectItem(idx, false); // clear selection |
| 2732 | fireSelectionChanged(); |
| 2733 | } |
| 2734 | long hitTime = getTimeAtX(e.x); |
| 2735 | if (hitTime >= 0) { |
| 2736 | setCapture(true); |
| 2737 | |
| 2738 | fDragState = DRAG_SELECTION; |
| 2739 | fDragBeginMarker = false; |
| 2740 | fDragButton = e.button; |
| 2741 | fDragX = e.x; |
| 2742 | fDragX0 = fDragX; |
| 2743 | fDragTime0 = getTimeAtX(fDragX0); |
| 2744 | long selectionBegin = fTimeProvider.getSelectionBegin(); |
| 2745 | long selectionEnd = fTimeProvider.getSelectionEnd(); |
| 2746 | int xBegin = getXForTime(selectionBegin); |
| 2747 | int xEnd = getXForTime(selectionEnd); |
| 2748 | if ((e.stateMask & SWT.MODIFIER_MASK) == SWT.SHIFT) { |
| 2749 | long time = getTimeAtX(e.x); |
| 2750 | if (Math.abs(time - selectionBegin) < Math.abs(time - selectionEnd)) { |
| 2751 | fDragBeginMarker = true; |
| 2752 | fDragX = xEnd; |
| 2753 | fDragX0 = e.x; |
| 2754 | fDragTime0 = selectionEnd; |
| 2755 | } else { |
| 2756 | fDragX0 = xBegin; |
| 2757 | fDragTime0 = selectionBegin; |
| 2758 | } |
| 2759 | } else { |
| 2760 | long time = getTimeAtX(e.x); |
| 2761 | if (Math.abs(e.x - xBegin) < SNAP_WIDTH && Math.abs(time - selectionBegin) <= Math.abs(time - selectionEnd)) { |
| 2762 | fDragBeginMarker = true; |
| 2763 | fDragX = xEnd; |
| 2764 | fDragX0 = e.x; |
| 2765 | fDragTime0 = selectionEnd; |
| 2766 | } else if (Math.abs(e.x - xEnd) < SNAP_WIDTH && Math.abs(time - selectionEnd) <= Math.abs(time - selectionBegin)) { |
| 2767 | fDragX0 = xBegin; |
| 2768 | fDragTime0 = selectionBegin; |
| 2769 | } |
| 2770 | } |
| 2771 | fTime0bak = fTimeProvider.getTime0(); |
| 2772 | fTime1bak = fTimeProvider.getTime1(); |
| 2773 | redraw(); |
| 2774 | updateCursor(e.x, e.stateMask); |
| 2775 | fTimeGraphScale.setDragRange(fDragX0, fDragX); |
| 2776 | } |
| 2777 | } else if (2 == e.button || (1 == e.button && (e.stateMask & SWT.MODIFIER_MASK) == SWT.CTRL)) { |
| 2778 | long hitTime = getTimeAtX(e.x); |
| 2779 | if (hitTime > 0) { |
| 2780 | setCapture(true); |
| 2781 | fDragState = DRAG_TRACE_ITEM; |
| 2782 | fDragButton = e.button; |
| 2783 | fDragX = e.x; |
| 2784 | fDragX0 = fDragX; |
| 2785 | fTime0bak = fTimeProvider.getTime0(); |
| 2786 | fTime1bak = fTimeProvider.getTime1(); |
| 2787 | updateCursor(e.x, e.stateMask); |
| 2788 | } |
| 2789 | } else if (3 == e.button) { |
| 2790 | if (e.x >= fTimeProvider.getNameSpace()) { |
| 2791 | setCapture(true); |
| 2792 | fDragX = Math.min(Math.max(e.x, fTimeProvider.getNameSpace()), getSize().x - RIGHT_MARGIN); |
| 2793 | fDragX0 = fDragX; |
| 2794 | fDragTime0 = getTimeAtX(fDragX0); |
| 2795 | fDragState = DRAG_ZOOM; |
| 2796 | fDragButton = e.button; |
| 2797 | redraw(); |
| 2798 | updateCursor(e.x, e.stateMask); |
| 2799 | fTimeGraphScale.setDragRange(fDragX0, fDragX); |
| 2800 | } else { |
| 2801 | idx = getItemIndexAtY(e.y); |
| 2802 | selectItem(idx, false); |
| 2803 | fireSelectionChanged(); |
| 2804 | } |
| 2805 | } |
| 2806 | } |
| 2807 | |
| 2808 | @Override |
| 2809 | public void mouseUp(MouseEvent e) { |
| 2810 | if (fPendingMenuDetectEvent != null && e.button == 3) { |
| 2811 | if ((fDragState == DRAG_ZOOM) && isInDragZoomMargin()) { |
| 2812 | // Select entry and time event for single click |
| 2813 | long time = getTimeAtX(fDragX0); |
| 2814 | fTimeProvider.setSelectionRangeNotify(time, time, false); |
| 2815 | int idx = getItemIndexAtY(e.y); |
| 2816 | selectItem(idx, false); |
| 2817 | fireSelectionChanged(); |
| 2818 | } |
| 2819 | menuDetected(fPendingMenuDetectEvent); |
| 2820 | } |
| 2821 | if (DRAG_NONE != fDragState) { |
| 2822 | setCapture(false); |
| 2823 | if (e.button == fDragButton && DRAG_TRACE_ITEM == fDragState) { |
| 2824 | fDragState = DRAG_NONE; |
| 2825 | if (fDragX != fDragX0) { |
| 2826 | fTimeProvider.notifyStartFinishTime(); |
| 2827 | } |
| 2828 | } else if (e.button == fDragButton && DRAG_SPLIT_LINE == fDragState) { |
| 2829 | fDragState = DRAG_NONE; |
| 2830 | redraw(); |
| 2831 | } else if (e.button == fDragButton && DRAG_SELECTION == fDragState) { |
| 2832 | fDragState = DRAG_NONE; |
| 2833 | if (fDragX == fDragX0) { // click without selecting anything |
| 2834 | long time = getTimeAtX(e.x); |
| 2835 | fTimeProvider.setSelectedTimeNotify(time, false); |
| 2836 | } else { |
| 2837 | long time0 = fDragBeginMarker ? getTimeAtX(fDragX0) : fDragTime0; |
| 2838 | long time1 = fDragBeginMarker ? fDragTime0 : getTimeAtX(fDragX); |
| 2839 | fTimeProvider.setSelectionRangeNotify(time0, time1, false); |
| 2840 | } |
| 2841 | redraw(); |
| 2842 | fTimeGraphScale.setDragRange(-1, -1); |
| 2843 | } else if (e.button == fDragButton && DRAG_ZOOM == fDragState) { |
| 2844 | fDragState = DRAG_NONE; |
| 2845 | int nameWidth = fTimeProvider.getNameSpace(); |
| 2846 | if ((Math.max(fDragX, fDragX0) > nameWidth) && !isInDragZoomMargin()) { |
| 2847 | long time0 = getTimeAtX(fDragX0); |
| 2848 | long time1 = getTimeAtX(fDragX); |
| 2849 | if (time0 < time1) { |
| 2850 | fTimeProvider.setStartFinishTimeNotify(time0, time1); |
| 2851 | } else { |
| 2852 | fTimeProvider.setStartFinishTimeNotify(time1, time0); |
| 2853 | } |
| 2854 | } else { |
| 2855 | redraw(); |
| 2856 | } |
| 2857 | fTimeGraphScale.setDragRange(-1, -1); |
| 2858 | } |
| 2859 | } |
| 2860 | updateCursor(e.x, e.stateMask); |
| 2861 | updateStatusLine(e.x); |
| 2862 | } |
| 2863 | |
| 2864 | @Override |
| 2865 | public void mouseEnter(MouseEvent e) { |
| 2866 | } |
| 2867 | |
| 2868 | @Override |
| 2869 | public void mouseExit(MouseEvent e) { |
| 2870 | if (fMouseOverSplitLine) { |
| 2871 | fMouseOverSplitLine = false; |
| 2872 | redraw(); |
| 2873 | } |
| 2874 | updateStatusLine(STATUS_WITHOUT_CURSOR_TIME); |
| 2875 | } |
| 2876 | |
| 2877 | @Override |
| 2878 | public void mouseHover(MouseEvent e) { |
| 2879 | } |
| 2880 | |
| 2881 | @Override |
| 2882 | public void mouseScrolled(MouseEvent e) { |
| 2883 | if (fDragState != DRAG_NONE || e.count == 0) { |
| 2884 | return; |
| 2885 | } |
| 2886 | |
| 2887 | /* |
| 2888 | * On some platforms the mouse scroll event is sent to the |
| 2889 | * control that has focus even if it is not under the cursor. |
| 2890 | * Handle the event only if over the time graph control. |
| 2891 | */ |
| 2892 | Point size = getSize(); |
| 2893 | Rectangle bounds = new Rectangle(0, 0, size.x, size.y); |
| 2894 | if (!bounds.contains(e.x, e.y)) { |
| 2895 | return; |
| 2896 | } |
| 2897 | |
| 2898 | boolean horizontalZoom = false; |
| 2899 | boolean horizontalScroll = false; |
| 2900 | boolean verticalZoom = false; |
| 2901 | boolean verticalScroll = false; |
| 2902 | |
| 2903 | // over the time graph control |
| 2904 | if ((e.stateMask & SWT.MODIFIER_MASK) == (SWT.SHIFT | SWT.CTRL)) { |
| 2905 | verticalZoom = true; |
| 2906 | } else if (e.x < fTimeProvider.getNameSpace()) { |
| 2907 | // over the name space |
| 2908 | verticalScroll = true; |
| 2909 | } else { |
| 2910 | // over the state area |
| 2911 | if ((e.stateMask & SWT.MODIFIER_MASK) == SWT.CTRL) { |
| 2912 | // over the state area, CTRL pressed |
| 2913 | horizontalZoom = true; |
| 2914 | } else if ((e.stateMask & SWT.MODIFIER_MASK) == SWT.SHIFT) { |
| 2915 | // over the state area, SHIFT pressed |
| 2916 | horizontalScroll = true; |
| 2917 | } else { |
| 2918 | // over the state area, no modifier pressed |
| 2919 | verticalScroll = true; |
| 2920 | } |
| 2921 | } |
| 2922 | if (verticalZoom) { |
| 2923 | fVerticalZoomAlignEntry = getVerticalZoomAlignCursor(e.y); |
| 2924 | verticalZoom(e.count > 0); |
| 2925 | if (fVerticalZoomAlignEntry != null) { |
| 2926 | setElementPosition(fVerticalZoomAlignEntry.getKey(), fVerticalZoomAlignEntry.getValue()); |
| 2927 | } |
| 2928 | } else if (horizontalZoom && fTimeProvider.getTime0() != fTimeProvider.getTime1()) { |
| 2929 | zoom(e.count > 0); |
| 2930 | } else if (horizontalScroll) { |
| 2931 | horizontalScroll(e.count > 0); |
| 2932 | } else if (verticalScroll){ |
| 2933 | setTopIndex(getTopIndex() - e.count); |
| 2934 | } |
| 2935 | } |
| 2936 | |
| 2937 | /** |
| 2938 | * Get the vertical zoom alignment entry and position based on the current |
| 2939 | * selection. If there is no selection or if the selection is not visible, |
| 2940 | * return an alignment entry with a null time graph entry. |
| 2941 | * |
| 2942 | * @return a map entry where the key is the selection's time graph entry and |
| 2943 | * the value is the center y-coordinate of that entry, or null |
| 2944 | */ |
| 2945 | private Entry<ITimeGraphEntry, Integer> getVerticalZoomAlignSelection() { |
| 2946 | Entry<ITimeGraphEntry, Integer> alignEntry = getVerticalZoomAlignOngoing(); |
| 2947 | if (alignEntry != null) { |
| 2948 | return alignEntry; |
| 2949 | } |
| 2950 | int index = getSelectedIndex(); |
| 2951 | if (index == -1 || index >= getExpandedElementCount()) { |
| 2952 | return new SimpleEntry<>(null, 0); |
| 2953 | } |
| 2954 | Rectangle bounds = getClientArea(); |
| 2955 | Rectangle itemRect = getItemRect(bounds, index); |
| 2956 | if (itemRect.y < bounds.y || itemRect.y > bounds.y + bounds.height) { |
| 2957 | /* selection is not visible */ |
| 2958 | return new SimpleEntry<>(null, 0); |
| 2959 | } |
| 2960 | ITimeGraphEntry entry = getExpandedElement(index); |
| 2961 | int y = itemRect.y + itemRect.height / 2; |
| 2962 | return new SimpleEntry<>(entry, y); |
| 2963 | } |
| 2964 | |
| 2965 | /** |
| 2966 | * Get the vertical zoom alignment entry and position at the specified |
| 2967 | * cursor position. |
| 2968 | * |
| 2969 | * @param y |
| 2970 | * the cursor y-coordinate |
| 2971 | * @return a map entry where the key is the time graph entry under the |
| 2972 | * cursor and the value is the cursor y-coordinate |
| 2973 | */ |
| 2974 | private Entry<ITimeGraphEntry, Integer> getVerticalZoomAlignCursor(int y) { |
| 2975 | Entry<ITimeGraphEntry, Integer> alignEntry = getVerticalZoomAlignOngoing(); |
| 2976 | if (alignEntry != null) { |
| 2977 | return alignEntry; |
| 2978 | } |
| 2979 | int index = getItemIndexAtY(y); |
| 2980 | if (index == -1) { |
| 2981 | index = getExpandedElementCount() - 1; |
| 2982 | } |
| 2983 | ITimeGraphEntry entry = getExpandedElement(index); |
| 2984 | return new SimpleEntry<>(entry, y); |
| 2985 | } |
| 2986 | |
| 2987 | /** |
| 2988 | * Get the vertical zoom alignment entry and position if there is an ongoing |
| 2989 | * one and we are within the vertical zoom delay, or otherwise return null. |
| 2990 | * |
| 2991 | * @return a map entry where the key is a time graph entry and the value is |
| 2992 | * a y-coordinate, or null |
| 2993 | */ |
| 2994 | private Entry<ITimeGraphEntry, Integer> getVerticalZoomAlignOngoing() { |
| 2995 | long currentTimeMillis = System.currentTimeMillis(); |
| 2996 | if (currentTimeMillis < fVerticalZoomAlignTime + VERTICAL_ZOOM_DELAY) { |
| 2997 | /* |
| 2998 | * If the vertical zoom is triggered repeatedly in a short amount of |
| 2999 | * time, use the initial event's entry and position. |
| 3000 | */ |
| 3001 | fVerticalZoomAlignTime = currentTimeMillis; |
| 3002 | return fVerticalZoomAlignEntry; |
| 3003 | } |
| 3004 | fVerticalZoomAlignTime = currentTimeMillis; |
| 3005 | return null; |
| 3006 | } |
| 3007 | |
| 3008 | @Override |
| 3009 | public void handleEvent(Event event) { |
| 3010 | if (event.type == SWT.MouseWheel) { |
| 3011 | // prevent horizontal scrolling when the mouse wheel is used to |
| 3012 | // scroll vertically or zoom |
| 3013 | event.doit = false; |
| 3014 | } |
| 3015 | } |
| 3016 | |
| 3017 | @Override |
| 3018 | public int getBorderWidth() { |
| 3019 | return fBorderWidth; |
| 3020 | } |
| 3021 | |
| 3022 | /** |
| 3023 | * Set the border width |
| 3024 | * |
| 3025 | * @param borderWidth |
| 3026 | * The width |
| 3027 | */ |
| 3028 | public void setBorderWidth(int borderWidth) { |
| 3029 | this.fBorderWidth = borderWidth; |
| 3030 | } |
| 3031 | |
| 3032 | /** |
| 3033 | * @return The current height of the header row |
| 3034 | */ |
| 3035 | public int getHeaderHeight() { |
| 3036 | return fHeaderHeight; |
| 3037 | } |
| 3038 | |
| 3039 | /** |
| 3040 | * Set the height of the header row |
| 3041 | * |
| 3042 | * @param headerHeight |
| 3043 | * The height |
| 3044 | */ |
| 3045 | public void setHeaderHeight(int headerHeight) { |
| 3046 | this.fHeaderHeight = headerHeight; |
| 3047 | } |
| 3048 | |
| 3049 | /** |
| 3050 | * @return The default height of regular item rows |
| 3051 | */ |
| 3052 | public int getItemHeight() { |
| 3053 | return fGlobalItemHeight; |
| 3054 | } |
| 3055 | |
| 3056 | /** |
| 3057 | * Set the default height of regular item rows. |
| 3058 | * |
| 3059 | * @param rowHeight |
| 3060 | * The height |
| 3061 | */ |
| 3062 | public void setItemHeight(int rowHeight) { |
| 3063 | this.fGlobalItemHeight = rowHeight; |
| 3064 | for (Item item : fItemData.fItems) { |
| 3065 | item.fItemHeight = rowHeight; |
| 3066 | } |
| 3067 | } |
| 3068 | |
| 3069 | /** |
| 3070 | * Set the height of a specific item. Overrides the default item height. |
| 3071 | * |
| 3072 | * @param entry |
| 3073 | * A time graph entry |
| 3074 | * @param rowHeight |
| 3075 | * The height |
| 3076 | * @return true if the height is successfully stored, false otherwise |
| 3077 | */ |
| 3078 | public boolean setItemHeight(ITimeGraphEntry entry, int rowHeight) { |
| 3079 | Item item = fItemData.findItem(entry); |
| 3080 | if (item != null) { |
| 3081 | item.fItemHeight = rowHeight; |
| 3082 | return true; |
| 3083 | } |
| 3084 | return false; |
| 3085 | } |
| 3086 | |
| 3087 | /** |
| 3088 | * Set the minimum item width |
| 3089 | * |
| 3090 | * @param width The minimum width |
| 3091 | */ |
| 3092 | public void setMinimumItemWidth(int width) { |
| 3093 | this.fMinimumItemWidth = width; |
| 3094 | } |
| 3095 | |
| 3096 | /** |
| 3097 | * @return The minimum item width |
| 3098 | */ |
| 3099 | public int getMinimumItemWidth() { |
| 3100 | return fMinimumItemWidth; |
| 3101 | } |
| 3102 | |
| 3103 | /** |
| 3104 | * Set whether all time events with a duration shorter than one pixel should |
| 3105 | * be blended in. If false, only the first such time event will be drawn and |
| 3106 | * the subsequent time events in the same pixel will be discarded. The |
| 3107 | * default value is false. |
| 3108 | * |
| 3109 | * @param blend |
| 3110 | * true if sub-pixel events should be blended, false otherwise. |
| 3111 | * @since 1.1 |
| 3112 | */ |
| 3113 | public void setBlendSubPixelEvents(boolean blend) { |
| 3114 | fBlendSubPixelEvents = blend; |
| 3115 | } |
| 3116 | |
| 3117 | @Override |
| 3118 | public void addSelectionChangedListener(ISelectionChangedListener listener) { |
| 3119 | if (listener != null && !fSelectionChangedListeners.contains(listener)) { |
| 3120 | fSelectionChangedListeners.add(listener); |
| 3121 | } |
| 3122 | } |
| 3123 | |
| 3124 | @Override |
| 3125 | public void removeSelectionChangedListener(ISelectionChangedListener listener) { |
| 3126 | if (listener != null) { |
| 3127 | fSelectionChangedListeners.remove(listener); |
| 3128 | } |
| 3129 | } |
| 3130 | |
| 3131 | @Override |
| 3132 | public void setSelection(ISelection selection) { |
| 3133 | if (selection instanceof IStructuredSelection) { |
| 3134 | Object ob = ((IStructuredSelection) selection).getFirstElement(); |
| 3135 | if (ob instanceof ITimeGraphEntry) { |
| 3136 | selectItem((ITimeGraphEntry) ob, false); |
| 3137 | } |
| 3138 | } |
| 3139 | |
| 3140 | } |
| 3141 | |
| 3142 | /** |
| 3143 | * @param filter The filter object to be attached to the view |
| 3144 | */ |
| 3145 | public void addFilter(@NonNull ViewerFilter filter) { |
| 3146 | if (!fFilters.contains(filter)) { |
| 3147 | fFilters.add(filter); |
| 3148 | } |
| 3149 | } |
| 3150 | |
| 3151 | /** |
| 3152 | * @param filter The filter object to be attached to the view |
| 3153 | */ |
| 3154 | public void removeFilter(@NonNull ViewerFilter filter) { |
| 3155 | fFilters.remove(filter); |
| 3156 | } |
| 3157 | |
| 3158 | /** |
| 3159 | * Returns this control's filters. |
| 3160 | * |
| 3161 | * @return an array of viewer filters |
| 3162 | * @since 1.2 |
| 3163 | */ |
| 3164 | public @NonNull ViewerFilter[] getFilters() { |
| 3165 | return Iterables.toArray(fFilters, ViewerFilter.class); |
| 3166 | } |
| 3167 | |
| 3168 | /** |
| 3169 | * Sets the filters, replacing any previous filters. |
| 3170 | * |
| 3171 | * @param filters |
| 3172 | * an array of viewer filters, or null |
| 3173 | * @since 1.2 |
| 3174 | */ |
| 3175 | public void setFilters(@NonNull ViewerFilter[] filters) { |
| 3176 | fFilters.clear(); |
| 3177 | if (filters != null) { |
| 3178 | fFilters.addAll(Arrays.asList(filters)); |
| 3179 | } |
| 3180 | } |
| 3181 | |
| 3182 | @Override |
| 3183 | public void colorSettingsChanged(StateItem[] stateItems) { |
| 3184 | /* Destroy previous colors from the resource manager */ |
| 3185 | if (fEventColorMap != null) { |
| 3186 | for (Color color : fEventColorMap) { |
| 3187 | fResourceManager.destroyColor(color.getRGB()); |
| 3188 | } |
| 3189 | } |
| 3190 | if (stateItems != null) { |
| 3191 | fEventColorMap = new Color[stateItems.length]; |
| 3192 | for (int i = 0; i < stateItems.length; i++) { |
| 3193 | fEventColorMap[i] = fResourceManager.createColor(stateItems[i].getStateColor()); |
| 3194 | } |
| 3195 | } else { |
| 3196 | fEventColorMap = new Color[] { }; |
| 3197 | } |
| 3198 | redraw(); |
| 3199 | } |
| 3200 | |
| 3201 | private class ItemData { |
| 3202 | private Map<ITimeGraphEntry, Item> fItemMap = new LinkedHashMap<>(); |
| 3203 | private Item[] fExpandedItems = new Item[0]; |
| 3204 | private Item[] fItems = new Item[0]; |
| 3205 | private ITimeGraphEntry fRootEntries[] = new ITimeGraphEntry[0]; |
| 3206 | private List<ILinkEvent> fLinks = new ArrayList<>(); |
| 3207 | |
| 3208 | public ItemData() { |
| 3209 | } |
| 3210 | |
| 3211 | public Item findItem(ITimeGraphEntry entry) { |
| 3212 | return fItemMap.get(entry); |
| 3213 | } |
| 3214 | |
| 3215 | public int findItemIndex(ITimeGraphEntry entry) { |
| 3216 | Item item = fItemMap.get(entry); |
| 3217 | if (item == null) { |
| 3218 | return -1; |
| 3219 | } |
| 3220 | return item.fExpandedIndex; |
| 3221 | } |
| 3222 | |
| 3223 | public void refreshData() { |
| 3224 | ITimeGraphEntry selection = getSelectedTrace(); |
| 3225 | Map<ITimeGraphEntry, Item> itemMap = new LinkedHashMap<>(); |
| 3226 | for (int i = 0; i < fRootEntries.length; i++) { |
| 3227 | ITimeGraphEntry entry = fRootEntries[i]; |
| 3228 | refreshData(itemMap, null, 0, entry); |
| 3229 | } |
| 3230 | fItemMap = itemMap; |
| 3231 | fItems = fItemMap.values().toArray(new Item[0]); |
| 3232 | updateExpandedItems(); |
| 3233 | if (selection != null) { |
| 3234 | for (Item item : fExpandedItems) { |
| 3235 | if (item.fEntry == selection) { |
| 3236 | item.fSelected = true; |
| 3237 | break; |
| 3238 | } |
| 3239 | } |
| 3240 | } |
| 3241 | } |
| 3242 | |
| 3243 | private void refreshData(Map<ITimeGraphEntry, Item> itemMap, Item parent, int level, ITimeGraphEntry entry) { |
| 3244 | Item item = new Item(entry, entry.getName(), level); |
| 3245 | if (parent != null) { |
| 3246 | parent.fChildren.add(item); |
| 3247 | } |
| 3248 | if (fGlobalItemHeight == CUSTOM_ITEM_HEIGHT) { |
| 3249 | item.fItemHeight = fTimeGraphProvider.getItemHeight(entry); |
| 3250 | } else { |
| 3251 | item.fItemHeight = fGlobalItemHeight; |
| 3252 | } |
| 3253 | item.fItemHeight = Math.max(1, item.fItemHeight + fHeightAdjustment); |
| 3254 | itemMap.put(entry, item); |
| 3255 | if (entry.hasChildren()) { |
| 3256 | Item oldItem = fItemMap.get(entry); |
| 3257 | if (oldItem != null && oldItem.fHasChildren && level == oldItem.fLevel && entry.getParent() == oldItem.fEntry.getParent()) { |
| 3258 | /* existing items keep their old expanded state */ |
| 3259 | item.fExpanded = oldItem.fExpanded; |
| 3260 | } else { |
| 3261 | /* new items set the expanded state according to auto-expand level */ |
| 3262 | item.fExpanded = fAutoExpandLevel == ALL_LEVELS || level < fAutoExpandLevel; |
| 3263 | } |
| 3264 | item.fHasChildren = true; |
| 3265 | for (ITimeGraphEntry child : entry.getChildren()) { |
| 3266 | refreshData(itemMap, item, level + 1, child); |
| 3267 | } |
| 3268 | } |
| 3269 | } |
| 3270 | |
| 3271 | public void updateExpandedItems() { |
| 3272 | for (Item item : fItems) { |
| 3273 | item.fExpandedIndex = -1; |
| 3274 | } |
| 3275 | List<Item> expandedItemList = new ArrayList<>(); |
| 3276 | for (int i = 0; i < fRootEntries.length; i++) { |
| 3277 | ITimeGraphEntry entry = fRootEntries[i]; |
| 3278 | Item item = findItem(entry); |
| 3279 | refreshExpanded(expandedItemList, item); |
| 3280 | } |
| 3281 | fExpandedItems = expandedItemList.toArray(new Item[0]); |
| 3282 | fTopIndex = Math.min(fTopIndex, Math.max(0, fExpandedItems.length - 1)); |
| 3283 | } |
| 3284 | |
| 3285 | private void refreshExpanded(List<Item> expandedItemList, Item item) { |
| 3286 | // Check for filters |
| 3287 | boolean display = true; |
| 3288 | for (ViewerFilter filter : fFilters) { |
| 3289 | if (!filter.select(null, item.fEntry.getParent(), item.fEntry)) { |
| 3290 | display = false; |
| 3291 | break; |
| 3292 | } |
| 3293 | } |
| 3294 | if (display) { |
| 3295 | item.fExpandedIndex = expandedItemList.size(); |
| 3296 | expandedItemList.add(item); |
| 3297 | if (item.fHasChildren && item.fExpanded) { |
| 3298 | for (Item child : item.fChildren) { |
| 3299 | refreshExpanded(expandedItemList, child); |
| 3300 | } |
| 3301 | } |
| 3302 | } |
| 3303 | } |
| 3304 | |
| 3305 | public void refreshData(ITimeGraphEntry[] entries) { |
| 3306 | if (entries == null) { |
| 3307 | fRootEntries = null; |
| 3308 | } else { |
| 3309 | fRootEntries = Arrays.copyOf(entries, entries.length); |
| 3310 | } |
| 3311 | |
| 3312 | refreshData(); |
| 3313 | } |
| 3314 | |
| 3315 | public void refreshArrows(List<ILinkEvent> events) { |
| 3316 | /* If links are null, reset the list */ |
| 3317 | if (events != null) { |
| 3318 | fLinks = events; |
| 3319 | } else { |
| 3320 | fLinks = new ArrayList<>(); |
| 3321 | } |
| 3322 | } |
| 3323 | |
| 3324 | public ITimeGraphEntry[] getEntries() { |
| 3325 | return fRootEntries; |
| 3326 | } |
| 3327 | } |
| 3328 | |
| 3329 | private class Item { |
| 3330 | private boolean fExpanded; |
| 3331 | private int fExpandedIndex; |
| 3332 | private boolean fSelected; |
| 3333 | private boolean fHasChildren; |
| 3334 | private int fItemHeight; |
| 3335 | private final int fLevel; |
| 3336 | private final List<Item> fChildren; |
| 3337 | private final String fName; |
| 3338 | private final ITimeGraphEntry fEntry; |
| 3339 | |
| 3340 | public Item(ITimeGraphEntry entry, String name, int level) { |
| 3341 | this.fEntry = entry; |
| 3342 | this.fName = name; |
| 3343 | this.fLevel = level; |
| 3344 | this.fChildren = new ArrayList<>(); |
| 3345 | } |
| 3346 | |
| 3347 | @Override |
| 3348 | public String toString() { |
| 3349 | return fName; |
| 3350 | } |
| 3351 | } |
| 3352 | |
| 3353 | @Override |
| 3354 | public void menuDetected(MenuDetectEvent e) { |
| 3355 | if (null == fTimeProvider) { |
| 3356 | return; |
| 3357 | } |
| 3358 | /* |
| 3359 | * This flag indicates if menu was prevented from being shown below and |
| 3360 | * therefore must be made visible on callback from mouseUp(). |
| 3361 | */ |
| 3362 | boolean pendingEventCallback = fPendingMenuDetectEvent != null; |
| 3363 | Point p = toControl(e.x, e.y); |
| 3364 | if (e.detail == SWT.MENU_MOUSE && isOverTimeSpace(p.x, p.y)) { |
| 3365 | if (fPendingMenuDetectEvent == null) { |
| 3366 | /* Feature in Linux. The MenuDetectEvent is received before mouseDown. |
| 3367 | * Store the event and trigger it later just before handling mouseUp. |
| 3368 | * This allows for the method to detect if mouse is used to drag zoom. |
| 3369 | */ |
| 3370 | fPendingMenuDetectEvent = e; |
| 3371 | /* |
| 3372 | * Prevent the platform to show the menu when returning. The |
| 3373 | * menu will be shown (see below) when this method is called |
| 3374 | * again during mouseUp(). |
| 3375 | */ |
| 3376 | e.doit = false; |
| 3377 | return; |
| 3378 | } |
| 3379 | fPendingMenuDetectEvent = null; |
| 3380 | if (fDragState != DRAG_ZOOM || !isInDragZoomMargin()) { |
| 3381 | /* |
| 3382 | * Don't show the menu on mouseUp() if a drag zoom is in |
| 3383 | * progress with a drag range outside of the drag zoom margin, |
| 3384 | * or if any other drag operation, or none, is in progress. |
| 3385 | */ |
| 3386 | e.doit = false; |
| 3387 | return; |
| 3388 | } |
| 3389 | } else { |
| 3390 | if (fDragState != DRAG_NONE) { |
| 3391 | /* |
| 3392 | * Don't show the menu on keyboard menu or mouse menu outside of |
| 3393 | * the time space if any drag operation is in progress. |
| 3394 | */ |
| 3395 | e.doit = false; |
| 3396 | return; |
| 3397 | } |
| 3398 | } |
| 3399 | int idx = getItemIndexAtY(p.y); |
| 3400 | if (idx >= 0 && idx < fItemData.fExpandedItems.length) { |
| 3401 | Item item = fItemData.fExpandedItems[idx]; |
| 3402 | ITimeGraphEntry entry = item.fEntry; |
| 3403 | |
| 3404 | /* Send menu event for the time graph entry */ |
| 3405 | e.doit = true; |
| 3406 | e.data = entry; |
| 3407 | fireMenuEventOnTimeGraphEntry(e); |
| 3408 | Menu menu = getMenu(); |
| 3409 | if (pendingEventCallback && e.doit && (menu != null)) { |
| 3410 | menu.setVisible(true); |
| 3411 | } |
| 3412 | |
| 3413 | /* Send menu event for time event */ |
| 3414 | if (entry.hasTimeEvents()) { |
| 3415 | ITimeEvent event = Utils.findEvent(entry, getTimeAtX(p.x), 2); |
| 3416 | if (event != null) { |
| 3417 | e.doit = true; |
| 3418 | e.data = event; |
| 3419 | fireMenuEventOnTimeEvent(e); |
| 3420 | menu = getMenu(); |
| 3421 | if (pendingEventCallback && e.doit && (menu != null)) { |
| 3422 | menu.setVisible(true); |
| 3423 | } |
| 3424 | } |
| 3425 | } |
| 3426 | } |
| 3427 | } |
| 3428 | |
| 3429 | /** |
| 3430 | * Perform the alignment operation. |
| 3431 | * |
| 3432 | * @param offset |
| 3433 | * the alignment offset |
| 3434 | * |
| 3435 | * @see ITmfTimeAligned |
| 3436 | * |
| 3437 | * @since 1.0 |
| 3438 | */ |
| 3439 | public void performAlign(int offset) { |
| 3440 | fTimeProvider.setNameSpace(offset); |
| 3441 | } |
| 3442 | |
| 3443 | /** |
| 3444 | * Return the time alignment information |
| 3445 | * |
| 3446 | * @return the time alignment information |
| 3447 | * |
| 3448 | * @see ITmfTimeAligned |
| 3449 | * |
| 3450 | * @since 1.0 |
| 3451 | */ |
| 3452 | public TmfTimeViewAlignmentInfo getTimeViewAlignmentInfo() { |
| 3453 | return new TmfTimeViewAlignmentInfo(getShell(), toDisplay(0, 0), fTimeProvider.getNameSpace()); |
| 3454 | } |
| 3455 | |
| 3456 | private boolean isInDragZoomMargin() { |
| 3457 | return (Math.abs(fDragX - fDragX0) < DRAG_MARGIN); |
| 3458 | } |
| 3459 | } |