| 1 | /***************************************************************************** |
| 2 | * Copyright (c) 2007, 2013 Intel Corporation, Ericsson. |
| 3 | * All rights reserved. This program and the accompanying materials |
| 4 | * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | * which accompanies this distribution, and is available at |
| 6 | * http://www.eclipse.org/legal/epl-v10.html |
| 7 | * |
| 8 | * Contributors: |
| 9 | * Intel Corporation - Initial API and implementation |
| 10 | * Ruslan A. Scherbakov, Intel - Initial API and implementation |
| 11 | * Alvaro Sanchez-Leon - Updated for TMF |
| 12 | * Patrick Tasse - Refactoring |
| 13 | *****************************************************************************/ |
| 14 | |
| 15 | package org.eclipse.linuxtools.tmf.ui.widgets.timegraph.widgets; |
| 16 | |
| 17 | import java.util.ArrayList; |
| 18 | import java.util.Iterator; |
| 19 | import java.util.List; |
| 20 | import java.util.Vector; |
| 21 | |
| 22 | import org.eclipse.jface.resource.JFaceResources; |
| 23 | import org.eclipse.jface.resource.LocalResourceManager; |
| 24 | import org.eclipse.jface.viewers.ISelection; |
| 25 | import org.eclipse.jface.viewers.ISelectionChangedListener; |
| 26 | import org.eclipse.jface.viewers.ISelectionProvider; |
| 27 | import org.eclipse.jface.viewers.ViewerFilter; |
| 28 | import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.ITimeGraphPresentationProvider; |
| 29 | import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.ITimeGraphTreeListener; |
| 30 | import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.StateItem; |
| 31 | import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.TimeGraphTreeExpansionEvent; |
| 32 | import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.ITimeEvent; |
| 33 | import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.ITimeGraphEntry; |
| 34 | import org.eclipse.swt.SWT; |
| 35 | import org.eclipse.swt.events.ControlEvent; |
| 36 | import org.eclipse.swt.events.ControlListener; |
| 37 | import org.eclipse.swt.events.FocusEvent; |
| 38 | import org.eclipse.swt.events.FocusListener; |
| 39 | import org.eclipse.swt.events.KeyEvent; |
| 40 | import org.eclipse.swt.events.KeyListener; |
| 41 | import org.eclipse.swt.events.MenuDetectEvent; |
| 42 | import org.eclipse.swt.events.MenuDetectListener; |
| 43 | import org.eclipse.swt.events.MouseEvent; |
| 44 | import org.eclipse.swt.events.MouseListener; |
| 45 | import org.eclipse.swt.events.MouseMoveListener; |
| 46 | import org.eclipse.swt.events.MouseTrackListener; |
| 47 | import org.eclipse.swt.events.MouseWheelListener; |
| 48 | import org.eclipse.swt.events.PaintEvent; |
| 49 | import org.eclipse.swt.events.SelectionEvent; |
| 50 | import org.eclipse.swt.events.SelectionListener; |
| 51 | import org.eclipse.swt.events.TraverseEvent; |
| 52 | import org.eclipse.swt.events.TraverseListener; |
| 53 | import org.eclipse.swt.events.TypedEvent; |
| 54 | import org.eclipse.swt.graphics.Color; |
| 55 | import org.eclipse.swt.graphics.Cursor; |
| 56 | import org.eclipse.swt.graphics.GC; |
| 57 | import org.eclipse.swt.graphics.Image; |
| 58 | import org.eclipse.swt.graphics.Point; |
| 59 | import org.eclipse.swt.graphics.Rectangle; |
| 60 | import org.eclipse.swt.widgets.Composite; |
| 61 | import org.eclipse.swt.widgets.Display; |
| 62 | import org.eclipse.swt.widgets.Event; |
| 63 | import org.eclipse.swt.widgets.Listener; |
| 64 | import org.eclipse.swt.widgets.ScrollBar; |
| 65 | |
| 66 | /** |
| 67 | * Time graph control implementation |
| 68 | * |
| 69 | * @version 1.0 |
| 70 | * @author Alvaro Sanchez-Leon |
| 71 | * @author Patrick Tasse |
| 72 | */ |
| 73 | public class TimeGraphControl extends TimeGraphBaseControl implements FocusListener, KeyListener, MouseMoveListener, MouseListener, MouseWheelListener, ControlListener, SelectionListener, MouseTrackListener, TraverseListener, ISelectionProvider, MenuDetectListener { |
| 74 | |
| 75 | /** Max scrollbar size */ |
| 76 | public static final int H_SCROLLBAR_MAX = Integer.MAX_VALUE - 1; |
| 77 | |
| 78 | /** Resource manager */ |
| 79 | protected LocalResourceManager fResourceManager = new LocalResourceManager(JFaceResources.getResources()); |
| 80 | |
| 81 | /** Color map for event types */ |
| 82 | protected Color[] fEventColorMap = null; |
| 83 | |
| 84 | private static final int DRAG_NONE = 0; |
| 85 | private static final int DRAG_TRACE_ITEM = 1; |
| 86 | private static final int DRAG_SPLIT_LINE = 2; |
| 87 | private static final int DRAG_ZOOM = 3; |
| 88 | |
| 89 | private static final int CUSTOM_ITEM_HEIGHT = -1; // get item height from provider |
| 90 | |
| 91 | private static final double zoomCoeff = 1.5; |
| 92 | |
| 93 | private ITimeDataProvider _timeProvider; |
| 94 | private boolean _isInFocus = false; |
| 95 | private boolean _isDragCursor3 = false; |
| 96 | private boolean _isWaitCursor = true; |
| 97 | private boolean _mouseOverSplitLine = false; |
| 98 | private int _itemHeight = CUSTOM_ITEM_HEIGHT; |
| 99 | private int _minimumItemWidth = 0; |
| 100 | private int _topIndex = 0; |
| 101 | private int _dragState = DRAG_NONE; |
| 102 | private int _dragX0 = 0; |
| 103 | private int _dragX = 0; |
| 104 | private int _idealNameSpace = 0; |
| 105 | // private double _timeStep = 10000000; |
| 106 | private long _time0bak; |
| 107 | private long _time1bak; |
| 108 | private ITimeGraphPresentationProvider fTimeGraphProvider = null; |
| 109 | private ItemData _data = null; |
| 110 | private List<SelectionListener> _selectionListeners; |
| 111 | private final List<ISelectionChangedListener> _selectionChangedListeners = new ArrayList<ISelectionChangedListener>(); |
| 112 | private final List<ITimeGraphTreeListener> _treeListeners = new ArrayList<ITimeGraphTreeListener>(); |
| 113 | private final List<MenuDetectListener> _timeGraphEntryMenuListeners = new ArrayList<MenuDetectListener>(); |
| 114 | private final List<MenuDetectListener> _timeEventMenuListeners = new ArrayList<MenuDetectListener>(); |
| 115 | private final Cursor _dragCursor3; |
| 116 | private final Cursor _WaitCursor; |
| 117 | private final List<ViewerFilter> _filters = new ArrayList<ViewerFilter>(); |
| 118 | |
| 119 | // Vertical formatting formatting for the state control view |
| 120 | private final boolean _visibleVerticalScroll = true; |
| 121 | private int _borderWidth = 0; |
| 122 | private int _headerHeight = 0; |
| 123 | |
| 124 | private Listener mouseScrollFilterListener; |
| 125 | |
| 126 | private MouseScrollNotifier fMouseScrollNotifier; |
| 127 | private final Object fMouseScrollNotifierLock = new Object(); |
| 128 | private class MouseScrollNotifier extends Thread { |
| 129 | private final static long DELAY = 400L; |
| 130 | private final static long POLLING_INTERVAL = 10L; |
| 131 | private long fLastScrollTime = Long.MAX_VALUE; |
| 132 | |
| 133 | @Override |
| 134 | public void run() { |
| 135 | while ((System.currentTimeMillis() - fLastScrollTime) < DELAY) { |
| 136 | try { |
| 137 | Thread.sleep(POLLING_INTERVAL); |
| 138 | } catch (Exception e) { |
| 139 | return; |
| 140 | } |
| 141 | } |
| 142 | if (!isInterrupted()) { |
| 143 | Display.getDefault().asyncExec(new Runnable() { |
| 144 | @Override |
| 145 | public void run() { |
| 146 | if (isDisposed()) { |
| 147 | return; |
| 148 | } |
| 149 | _timeProvider.notifyStartFinishTime(); |
| 150 | } |
| 151 | }); |
| 152 | } |
| 153 | synchronized (fMouseScrollNotifierLock) { |
| 154 | fMouseScrollNotifier = null; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | public void mouseScrolled() { |
| 159 | fLastScrollTime = System.currentTimeMillis(); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Standard constructor |
| 165 | * |
| 166 | * @param parent |
| 167 | * The parent composite object |
| 168 | * @param colors |
| 169 | * The color scheme to use |
| 170 | */ |
| 171 | public TimeGraphControl(Composite parent, TimeGraphColorScheme colors) { |
| 172 | |
| 173 | super(parent, colors, SWT.NO_BACKGROUND | SWT.H_SCROLL | SWT.DOUBLE_BUFFERED); |
| 174 | |
| 175 | _data = new ItemData(); |
| 176 | |
| 177 | addFocusListener(this); |
| 178 | addMouseListener(this); |
| 179 | addMouseMoveListener(this); |
| 180 | addMouseTrackListener(this); |
| 181 | addMouseWheelListener(this); |
| 182 | addTraverseListener(this); |
| 183 | addKeyListener(this); |
| 184 | addControlListener(this); |
| 185 | addMenuDetectListener(this); |
| 186 | ScrollBar scrollHor = getHorizontalBar(); |
| 187 | |
| 188 | if (scrollHor != null) { |
| 189 | scrollHor.addSelectionListener(this); |
| 190 | } |
| 191 | |
| 192 | _dragCursor3 = new Cursor(super.getDisplay(), SWT.CURSOR_SIZEWE); |
| 193 | _WaitCursor = new Cursor(super.getDisplay(), SWT.CURSOR_WAIT); |
| 194 | } |
| 195 | |
| 196 | @Override |
| 197 | public void dispose() { |
| 198 | super.dispose(); |
| 199 | _dragCursor3.dispose(); |
| 200 | _WaitCursor.dispose(); |
| 201 | fResourceManager.dispose(); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Sets the timegraph provider used by this timegraph viewer. |
| 206 | * |
| 207 | * @param timeGraphProvider the timegraph provider |
| 208 | */ |
| 209 | public void setTimeGraphProvider(ITimeGraphPresentationProvider timeGraphProvider) { |
| 210 | fTimeGraphProvider = timeGraphProvider; |
| 211 | _data.provider = timeGraphProvider; |
| 212 | |
| 213 | if (fEventColorMap != null) { |
| 214 | for (Color color : fEventColorMap) { |
| 215 | fResourceManager.destroyColor(color.getRGB()); |
| 216 | } |
| 217 | } |
| 218 | StateItem[] stateItems = fTimeGraphProvider.getStateTable(); |
| 219 | if (stateItems != null) { |
| 220 | fEventColorMap = new Color[stateItems.length]; |
| 221 | for (int i = 0; i < stateItems.length; i++) { |
| 222 | fEventColorMap[i] = fResourceManager.createColor(stateItems[i].getStateColor()); |
| 223 | } |
| 224 | } else { |
| 225 | fEventColorMap = new Color[] { }; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Assign the given time provider |
| 231 | * |
| 232 | * @param timeProvider |
| 233 | * The time provider |
| 234 | */ |
| 235 | public void setTimeProvider(ITimeDataProvider timeProvider) { |
| 236 | _timeProvider = timeProvider; |
| 237 | adjustScrolls(); |
| 238 | redraw(); |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Add a selection listener |
| 243 | * |
| 244 | * @param listener |
| 245 | * The listener to add |
| 246 | */ |
| 247 | public void addSelectionListener(SelectionListener listener) { |
| 248 | if (listener == null) { |
| 249 | SWT.error(SWT.ERROR_NULL_ARGUMENT); |
| 250 | } |
| 251 | if (null == _selectionListeners) { |
| 252 | _selectionListeners = new ArrayList<SelectionListener>(); |
| 253 | } |
| 254 | _selectionListeners.add(listener); |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Remove a selection listener |
| 259 | * |
| 260 | * @param listener |
| 261 | * The listener to remove |
| 262 | */ |
| 263 | public void removeSelectionListener(SelectionListener listener) { |
| 264 | if (null != _selectionListeners) { |
| 265 | _selectionListeners.remove(listener); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Selection changed callback |
| 271 | */ |
| 272 | public void fireSelectionChanged() { |
| 273 | if (null != _selectionListeners) { |
| 274 | Iterator<SelectionListener> it = _selectionListeners.iterator(); |
| 275 | while (it.hasNext()) { |
| 276 | SelectionListener listener = it.next(); |
| 277 | listener.widgetSelected(null); |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Default selection callback |
| 284 | */ |
| 285 | public void fireDefaultSelection() { |
| 286 | if (null != _selectionListeners) { |
| 287 | Iterator<SelectionListener> it = _selectionListeners.iterator(); |
| 288 | while (it.hasNext()) { |
| 289 | SelectionListener listener = it.next(); |
| 290 | listener.widgetDefaultSelected(null); |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Get the traces in the model |
| 297 | * |
| 298 | * @return The array of traces |
| 299 | */ |
| 300 | public ITimeGraphEntry[] getTraces() { |
| 301 | return _data.getTraces(); |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * Get the on/off trace filters |
| 306 | * |
| 307 | * @return The array of filters |
| 308 | */ |
| 309 | public boolean[] getTraceFilter() { |
| 310 | return _data.getTraceFilter(); |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * Refresh the data for the thing |
| 315 | */ |
| 316 | public void refreshData() { |
| 317 | _data.refreshData(); |
| 318 | adjustScrolls(); |
| 319 | redraw(); |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Refresh data for the given traces |
| 324 | * |
| 325 | * @param traces |
| 326 | * The traces to refresh |
| 327 | */ |
| 328 | public void refreshData(ITimeGraphEntry[] traces) { |
| 329 | _data.refreshData(traces); |
| 330 | adjustScrolls(); |
| 331 | redraw(); |
| 332 | } |
| 333 | |
| 334 | /** |
| 335 | * Adjust the scoll bars |
| 336 | */ |
| 337 | public void adjustScrolls() { |
| 338 | if (null == _timeProvider) { |
| 339 | getHorizontalBar().setValues(0, 1, 1, 1, 1, 1); |
| 340 | return; |
| 341 | } |
| 342 | |
| 343 | // HORIZONTAL BAR |
| 344 | // Visible window |
| 345 | long time0 = _timeProvider.getTime0(); |
| 346 | long time1 = _timeProvider.getTime1(); |
| 347 | // Time boundaries |
| 348 | long timeMin = _timeProvider.getMinTime(); |
| 349 | long timeMax = _timeProvider.getMaxTime(); |
| 350 | |
| 351 | long delta = timeMax - timeMin; |
| 352 | |
| 353 | int timePos = 0; |
| 354 | int thumb = H_SCROLLBAR_MAX; |
| 355 | |
| 356 | if (delta != 0) { |
| 357 | // Thumb size (page size) |
| 358 | thumb = Math.max(1, (int) (H_SCROLLBAR_MAX * ((double) (time1 - time0) / delta))); |
| 359 | // At the beginning of visible window |
| 360 | timePos = (int) (H_SCROLLBAR_MAX * ((double) (time0 - timeMin) / delta)); |
| 361 | } |
| 362 | |
| 363 | // position, minimum, maximum, thumb size, increment (half page)t, page |
| 364 | // increment size (full page) |
| 365 | getHorizontalBar().setValues(timePos, 0, H_SCROLLBAR_MAX, thumb, Math.max(1, thumb / 2), Math.max(2, thumb)); |
| 366 | } |
| 367 | |
| 368 | boolean ensureVisibleItem(int idx, boolean redraw) { |
| 369 | boolean changed = false; |
| 370 | int index = idx; |
| 371 | if (index < 0) { |
| 372 | for (index = 0; index < _data._expandedItems.length; index++) { |
| 373 | if (_data._expandedItems[index]._selected) { |
| 374 | break; |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | if (index >= _data._expandedItems.length) { |
| 379 | return changed; |
| 380 | } |
| 381 | if (index < _topIndex) { |
| 382 | setTopIndex(index); |
| 383 | //FIXME:getVerticalBar().setSelection(_topItem); |
| 384 | if (redraw) { |
| 385 | redraw(); |
| 386 | } |
| 387 | changed = true; |
| 388 | } else { |
| 389 | int page = countPerPage(); |
| 390 | if (index >= _topIndex + page) { |
| 391 | setTopIndex(index - page + 1); |
| 392 | //FIXME:getVerticalBar().setSelection(_topItem); |
| 393 | if (redraw) { |
| 394 | redraw(); |
| 395 | } |
| 396 | changed = true; |
| 397 | } |
| 398 | } |
| 399 | return changed; |
| 400 | } |
| 401 | |
| 402 | /** |
| 403 | * Assign the given index as the top one |
| 404 | * |
| 405 | * @param idx |
| 406 | * The index |
| 407 | */ |
| 408 | public void setTopIndex(int idx) { |
| 409 | int index = Math.min(idx, _data._expandedItems.length - countPerPage()); |
| 410 | index = Math.max(0, index); |
| 411 | _topIndex = index; |
| 412 | redraw(); |
| 413 | } |
| 414 | |
| 415 | /** |
| 416 | * Set the expanded state of a given entry |
| 417 | * |
| 418 | * @param entry |
| 419 | * The entry |
| 420 | * @param expanded |
| 421 | * True if expanded, false if collapsed |
| 422 | */ |
| 423 | public void setExpandedState(ITimeGraphEntry entry, boolean expanded) { |
| 424 | Item item = _data.findItem(entry); |
| 425 | if (item != null && item._expanded != expanded) { |
| 426 | item._expanded = expanded; |
| 427 | _data.updateExpandedItems(); |
| 428 | redraw(); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | /** |
| 433 | * Collapses all nodes of the viewer's tree, starting with the root. |
| 434 | * |
| 435 | * @since 2.0 |
| 436 | */ |
| 437 | public void collapseAll() { |
| 438 | for (Item item : _data._items) { |
| 439 | item._expanded = false; |
| 440 | } |
| 441 | _data.updateExpandedItems(); |
| 442 | redraw(); |
| 443 | } |
| 444 | |
| 445 | /** |
| 446 | * Expands all nodes of the viewer's tree, starting with the root. |
| 447 | * |
| 448 | * @since 2.0 |
| 449 | */ |
| 450 | public void expandAll() { |
| 451 | for (Item item : _data._items) { |
| 452 | item._expanded = true; |
| 453 | } |
| 454 | _data.updateExpandedItems(); |
| 455 | redraw(); |
| 456 | } |
| 457 | |
| 458 | /** |
| 459 | * Add a tree listener |
| 460 | * |
| 461 | * @param listener |
| 462 | * The listener to add |
| 463 | */ |
| 464 | public void addTreeListener(ITimeGraphTreeListener listener) { |
| 465 | if (!_treeListeners.contains(listener)) { |
| 466 | _treeListeners.add(listener); |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | /** |
| 471 | * Remove a tree listener |
| 472 | * |
| 473 | * @param listener |
| 474 | * The listener to remove |
| 475 | */ |
| 476 | public void removeTreeListener(ITimeGraphTreeListener listener) { |
| 477 | if (_treeListeners.contains(listener)) { |
| 478 | _treeListeners.remove(listener); |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | /** |
| 483 | * Tree event callback |
| 484 | * |
| 485 | * @param entry |
| 486 | * The affected entry |
| 487 | * @param expanded |
| 488 | * The expanded state (true for expanded, false for collapsed) |
| 489 | */ |
| 490 | public void fireTreeEvent(ITimeGraphEntry entry, boolean expanded) { |
| 491 | TimeGraphTreeExpansionEvent event = new TimeGraphTreeExpansionEvent(this, entry); |
| 492 | for (ITimeGraphTreeListener listener : _treeListeners) { |
| 493 | if (expanded) { |
| 494 | listener.treeExpanded(event); |
| 495 | } else { |
| 496 | listener.treeCollapsed(event); |
| 497 | } |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | /** |
| 502 | * Add a menu listener on {@link ITimeGraphEntry}s |
| 503 | * @param listener |
| 504 | * The listener to add |
| 505 | * @since 1.2 |
| 506 | */ |
| 507 | public void addTimeGraphEntryMenuListener(MenuDetectListener listener) { |
| 508 | if (!_timeGraphEntryMenuListeners.contains(listener)) { |
| 509 | _timeGraphEntryMenuListeners.add(listener); |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | /** |
| 514 | * Remove a menu listener on {@link ITimeGraphEntry}s |
| 515 | * |
| 516 | * @param listener |
| 517 | * The listener to remove |
| 518 | * @since 1.2 |
| 519 | */ |
| 520 | public void removeTimeGraphEntryMenuListener(MenuDetectListener listener) { |
| 521 | if (_timeGraphEntryMenuListeners.contains(listener)) { |
| 522 | _timeGraphEntryMenuListeners.remove(listener); |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | /** |
| 527 | * Menu event callback on {@link ITimeGraphEntry}s |
| 528 | * |
| 529 | * @param event |
| 530 | * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeGraphEntry} |
| 531 | */ |
| 532 | private void fireMenuEventOnTimeGraphEntry(MenuDetectEvent event) { |
| 533 | for (MenuDetectListener listener : _timeGraphEntryMenuListeners) { |
| 534 | listener.menuDetected(event); |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | /** |
| 539 | * Add a menu listener on {@link ITimeEvent}s |
| 540 | * |
| 541 | * @param listener |
| 542 | * The listener to add |
| 543 | * @since 1.2 |
| 544 | */ |
| 545 | public void addTimeEventMenuListener(MenuDetectListener listener) { |
| 546 | if (!_timeEventMenuListeners.contains(listener)) { |
| 547 | _timeEventMenuListeners.add(listener); |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | /** |
| 552 | * Remove a menu listener on {@link ITimeEvent}s |
| 553 | * |
| 554 | * @param listener |
| 555 | * The listener to remove |
| 556 | * @since 1.2 |
| 557 | */ |
| 558 | public void removeTimeEventMenuListener(MenuDetectListener listener) { |
| 559 | if (_timeEventMenuListeners.contains(listener)) { |
| 560 | _timeEventMenuListeners.remove(listener); |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | /** |
| 565 | * Menu event callback on {@link ITimeEvent}s |
| 566 | * |
| 567 | * @param event |
| 568 | * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeEvent} |
| 569 | */ |
| 570 | private void fireMenuEventOnTimeEvent(MenuDetectEvent event) { |
| 571 | for (MenuDetectListener listener : _timeEventMenuListeners) { |
| 572 | listener.menuDetected(event); |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | @Override |
| 577 | public ISelection getSelection() { |
| 578 | TimeGraphSelection sel = new TimeGraphSelection(); |
| 579 | ITimeGraphEntry trace = getSelectedTrace(); |
| 580 | if (null != trace && null != _timeProvider) { |
| 581 | long selectedTime = _timeProvider.getSelectedTime(); |
| 582 | ITimeEvent event = Utils.findEvent(trace, selectedTime, 0); |
| 583 | if (event != null) { |
| 584 | sel.add(event); |
| 585 | } else { |
| 586 | sel.add(trace); |
| 587 | } |
| 588 | } |
| 589 | return sel; |
| 590 | } |
| 591 | |
| 592 | /** |
| 593 | * Get the selection object |
| 594 | * |
| 595 | * @return The selection |
| 596 | */ |
| 597 | public ISelection getSelectionTrace() { |
| 598 | TimeGraphSelection sel = new TimeGraphSelection(); |
| 599 | ITimeGraphEntry trace = getSelectedTrace(); |
| 600 | if (null != trace) { |
| 601 | sel.add(trace); |
| 602 | } |
| 603 | return sel; |
| 604 | } |
| 605 | |
| 606 | /** |
| 607 | * Enable/disable one of the traces in the model |
| 608 | * |
| 609 | * @param n |
| 610 | * 1 to enable it, -1 to disable. The method returns immediately |
| 611 | * if another value is used. |
| 612 | */ |
| 613 | public void selectTrace(int n) { |
| 614 | if ((n != 1) && (n != -1)) { |
| 615 | return; |
| 616 | } |
| 617 | |
| 618 | boolean changed = false; |
| 619 | int lastSelection = -1; |
| 620 | for (int i = 0; i < _data._expandedItems.length; i++) { |
| 621 | Item item = _data._expandedItems[i]; |
| 622 | if (item._selected) { |
| 623 | lastSelection = i; |
| 624 | if ((1 == n) && (i < _data._expandedItems.length - 1)) { |
| 625 | item._selected = false; |
| 626 | item = _data._expandedItems[i + 1]; |
| 627 | item._selected = true; |
| 628 | changed = true; |
| 629 | } else if ((-1 == n) && (i > 0)) { |
| 630 | item._selected = false; |
| 631 | item = _data._expandedItems[i - 1]; |
| 632 | item._selected = true; |
| 633 | changed = true; |
| 634 | } |
| 635 | break; |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | if (lastSelection < 0 && _data._expandedItems.length > 0) { |
| 640 | Item item = _data._expandedItems[0]; |
| 641 | item._selected = true; |
| 642 | changed = true; |
| 643 | } |
| 644 | |
| 645 | if (changed) { |
| 646 | ensureVisibleItem(-1, false); |
| 647 | redraw(); |
| 648 | fireSelectionChanged(); |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | /** |
| 653 | * Select an event |
| 654 | * |
| 655 | * @param n |
| 656 | * 1 for next event, -1 for previous event |
| 657 | */ |
| 658 | public void selectEvent(int n) { |
| 659 | if (null == _timeProvider) { |
| 660 | return; |
| 661 | } |
| 662 | ITimeGraphEntry trace = getSelectedTrace(); |
| 663 | if (trace == null) { |
| 664 | return; |
| 665 | } |
| 666 | long selectedTime = _timeProvider.getSelectedTime(); |
| 667 | long endTime = _timeProvider.getEndTime(); |
| 668 | ITimeEvent nextEvent; |
| 669 | if (-1 == n && selectedTime > endTime) { |
| 670 | nextEvent = Utils.findEvent(trace, selectedTime, 0); |
| 671 | } else { |
| 672 | nextEvent = Utils.findEvent(trace, selectedTime, n); |
| 673 | } |
| 674 | if (null == nextEvent && -1 == n) { |
| 675 | nextEvent = Utils.getFirstEvent(trace); |
| 676 | } |
| 677 | if (null != nextEvent) { |
| 678 | long nextTime = nextEvent.getTime(); |
| 679 | // If last event detected e.g. going back or not moving to a next |
| 680 | // event |
| 681 | if (nextTime <= selectedTime && n == 1) { |
| 682 | // Select to the end of this last event |
| 683 | nextTime = nextEvent.getTime() + nextEvent.getDuration(); |
| 684 | // but not beyond the end of the trace |
| 685 | if (nextTime > endTime) { |
| 686 | nextTime = endTime; |
| 687 | } |
| 688 | } else if (n == -1) { |
| 689 | // for previous event go to its end time unless we were already there |
| 690 | if (nextEvent.getTime() + nextEvent.getDuration() < selectedTime) { |
| 691 | nextTime = nextEvent.getTime() + nextEvent.getDuration(); |
| 692 | } |
| 693 | } |
| 694 | _timeProvider.setSelectedTimeNotify(nextTime, true); |
| 695 | fireSelectionChanged(); |
| 696 | } else if (1 == n) { |
| 697 | _timeProvider.setSelectedTimeNotify(endTime, true); |
| 698 | fireSelectionChanged(); |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | /** |
| 703 | * Select the next event |
| 704 | */ |
| 705 | public void selectNextEvent() { |
| 706 | selectEvent(1); |
| 707 | // Notify if visible time window has been adjusted |
| 708 | _timeProvider.setStartFinishTimeNotify(_timeProvider.getTime0(), _timeProvider.getTime1()); |
| 709 | } |
| 710 | |
| 711 | /** |
| 712 | * Select the previous event |
| 713 | */ |
| 714 | public void selectPrevEvent() { |
| 715 | selectEvent(-1); |
| 716 | // Notify if visible time window has been adjusted |
| 717 | _timeProvider.setStartFinishTimeNotify(_timeProvider.getTime0(), _timeProvider.getTime1()); |
| 718 | } |
| 719 | |
| 720 | /** |
| 721 | * Select the next trace |
| 722 | */ |
| 723 | public void selectNextTrace() { |
| 724 | selectTrace(1); |
| 725 | } |
| 726 | |
| 727 | /** |
| 728 | * Select the previous trace |
| 729 | */ |
| 730 | public void selectPrevTrace() { |
| 731 | selectTrace(-1); |
| 732 | } |
| 733 | |
| 734 | /** |
| 735 | * Zoom based on mouse cursor location with mouse scrolling |
| 736 | * |
| 737 | * @param zoomIn true to zoom in, false to zoom out |
| 738 | */ |
| 739 | public void zoom(boolean zoomIn) { |
| 740 | int globalX = getDisplay().getCursorLocation().x; |
| 741 | Point p = toControl(globalX, 0); |
| 742 | int nameSpace = _timeProvider.getNameSpace(); |
| 743 | int timeSpace = _timeProvider.getTimeSpace(); |
| 744 | int xPos = Math.max(nameSpace, Math.min(nameSpace + timeSpace, p.x)); |
| 745 | long time0 = _timeProvider.getTime0(); |
| 746 | long time1 = _timeProvider.getTime1(); |
| 747 | long interval = time1 - time0; |
| 748 | if (interval == 0) { |
| 749 | interval = 1; |
| 750 | } // to allow getting out of single point interval |
| 751 | long newInterval; |
| 752 | if (zoomIn) { |
| 753 | newInterval = Math.max(Math.round(interval * 0.8), _timeProvider.getMinTimeInterval()); |
| 754 | } else { |
| 755 | newInterval = (long) Math.ceil(interval * 1.25); |
| 756 | } |
| 757 | long center = time0 + Math.round(((double) (xPos - nameSpace) / timeSpace * interval)); |
| 758 | long newTime0 = center - Math.round((double) newInterval * (center - time0) / interval); |
| 759 | long newTime1 = newTime0 + newInterval; |
| 760 | _timeProvider.setStartFinishTime(newTime0, newTime1); |
| 761 | synchronized (fMouseScrollNotifierLock) { |
| 762 | if (fMouseScrollNotifier == null) { |
| 763 | fMouseScrollNotifier = new MouseScrollNotifier(); |
| 764 | fMouseScrollNotifier.start(); |
| 765 | } |
| 766 | fMouseScrollNotifier.mouseScrolled(); |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | /** |
| 771 | * zoom in using single click |
| 772 | */ |
| 773 | public void zoomIn() { |
| 774 | long _time0 = _timeProvider.getTime0(); |
| 775 | long _time1 = _timeProvider.getTime1(); |
| 776 | long _range = _time1 - _time0; |
| 777 | long selTime = _timeProvider.getSelectedTime(); |
| 778 | if (selTime <= _time0 || selTime >= _time1) { |
| 779 | selTime = (_time0 + _time1) / 2; |
| 780 | } |
| 781 | long time0 = selTime - (long) ((selTime - _time0) / zoomCoeff); |
| 782 | long time1 = selTime + (long) ((_time1 - selTime) / zoomCoeff); |
| 783 | |
| 784 | long inaccuracy = (_timeProvider.getMaxTime() - _timeProvider.getMinTime()) - (time1 - time0); |
| 785 | |
| 786 | // Trace.debug("selTime:" + selTime + " time0:" + time0 + " time1:" |
| 787 | // + time1 + " inaccuracy:" + inaccuracy); |
| 788 | |
| 789 | if (inaccuracy > 0 && inaccuracy < 100) { |
| 790 | _timeProvider.setStartFinishTimeNotify(_timeProvider.getMinTime(), _timeProvider.getMaxTime()); |
| 791 | return; |
| 792 | } |
| 793 | |
| 794 | long m = _timeProvider.getMinTimeInterval(); |
| 795 | if ((time1 - time0) < m) { |
| 796 | time0 = selTime - (selTime - _time0) * m / _range; |
| 797 | time1 = time0 + m; |
| 798 | } |
| 799 | |
| 800 | _timeProvider.setStartFinishTimeNotify(time0, time1); |
| 801 | } |
| 802 | |
| 803 | /** |
| 804 | * zoom out using single click |
| 805 | */ |
| 806 | public void zoomOut() { |
| 807 | long _time0 = _timeProvider.getTime0(); |
| 808 | long _time1 = _timeProvider.getTime1(); |
| 809 | long selTime = _timeProvider.getSelectedTime(); |
| 810 | if (selTime <= _time0 || selTime >= _time1) { |
| 811 | selTime = (_time0 + _time1) / 2; |
| 812 | } |
| 813 | long time0 = (long) (selTime - (selTime - _time0) * zoomCoeff); |
| 814 | long time1 = (long) (selTime + (_time1 - selTime) * zoomCoeff); |
| 815 | |
| 816 | long inaccuracy = (_timeProvider.getMaxTime() - _timeProvider.getMinTime()) - (time1 - time0); |
| 817 | if (inaccuracy > 0 && inaccuracy < 100) { |
| 818 | _timeProvider.setStartFinishTimeNotify(_timeProvider.getMinTime(), _timeProvider.getMaxTime()); |
| 819 | return; |
| 820 | } |
| 821 | |
| 822 | _timeProvider.setStartFinishTimeNotify(time0, time1); |
| 823 | } |
| 824 | |
| 825 | /** |
| 826 | * Return the currently selected trace |
| 827 | * |
| 828 | * @return The entry matching the trace |
| 829 | */ |
| 830 | public ITimeGraphEntry getSelectedTrace() { |
| 831 | ITimeGraphEntry trace = null; |
| 832 | int idx = getSelectedIndex(); |
| 833 | if (idx >= 0) { |
| 834 | trace = _data._expandedItems[idx]._trace; |
| 835 | } |
| 836 | return trace; |
| 837 | } |
| 838 | |
| 839 | /** |
| 840 | * Retrieve the index of the currently selected item |
| 841 | * |
| 842 | * @return The index |
| 843 | */ |
| 844 | public int getSelectedIndex() { |
| 845 | int idx = -1; |
| 846 | for (int i = 0; i < _data._expandedItems.length; i++) { |
| 847 | Item item = _data._expandedItems[i]; |
| 848 | if (item._selected) { |
| 849 | idx = i; |
| 850 | break; |
| 851 | } |
| 852 | } |
| 853 | return idx; |
| 854 | } |
| 855 | |
| 856 | boolean toggle(int idx) { |
| 857 | boolean toggled = false; |
| 858 | if (idx >= 0 && idx < _data._expandedItems.length) { |
| 859 | Item item = _data._expandedItems[idx]; |
| 860 | if (item._hasChildren) { |
| 861 | item._expanded = !item._expanded; |
| 862 | _data.updateExpandedItems(); |
| 863 | adjustScrolls(); |
| 864 | redraw(); |
| 865 | toggled = true; |
| 866 | fireTreeEvent(item._trace, item._expanded); |
| 867 | } |
| 868 | } |
| 869 | return toggled; |
| 870 | } |
| 871 | |
| 872 | int getItemIndexAtY(int y) { |
| 873 | if (y < 0) { |
| 874 | return -1; |
| 875 | } |
| 876 | if (_itemHeight == CUSTOM_ITEM_HEIGHT) { |
| 877 | int ySum = 0; |
| 878 | for (int idx = _topIndex; idx < _data._expandedItems.length; idx++) { |
| 879 | ySum += _data._expandedItems[idx].itemHeight; |
| 880 | if (y < ySum) { |
| 881 | return idx; |
| 882 | } |
| 883 | } |
| 884 | return -1; |
| 885 | } |
| 886 | int idx = y / _itemHeight; |
| 887 | idx += _topIndex; |
| 888 | if (idx < _data._expandedItems.length) { |
| 889 | return idx; |
| 890 | } |
| 891 | return -1; |
| 892 | } |
| 893 | |
| 894 | boolean isOverSplitLine(int x) { |
| 895 | if (x < 0 || null == _timeProvider) { |
| 896 | return false; |
| 897 | } |
| 898 | int w = 4; |
| 899 | int nameWidth = _timeProvider.getNameSpace(); |
| 900 | if (x > nameWidth - w && x < nameWidth + w) { |
| 901 | return true; |
| 902 | } |
| 903 | return false; |
| 904 | } |
| 905 | |
| 906 | ITimeGraphEntry getEntry(Point pt) { |
| 907 | int idx = getItemIndexAtY(pt.y); |
| 908 | return idx >= 0 ? _data._expandedItems[idx]._trace : null; |
| 909 | } |
| 910 | |
| 911 | /** |
| 912 | * Return the x coordinate corresponding to a time |
| 913 | * |
| 914 | * @param time the time |
| 915 | * @return the x coordinate corresponding to the time |
| 916 | * |
| 917 | * @since 2.0 |
| 918 | */ |
| 919 | public int getXForTime(long time) { |
| 920 | if (null == _timeProvider) { |
| 921 | return -1; |
| 922 | } |
| 923 | long time0 = _timeProvider.getTime0(); |
| 924 | long time1 = _timeProvider.getTime1(); |
| 925 | int width = getCtrlSize().x; |
| 926 | int nameSpace = _timeProvider.getNameSpace(); |
| 927 | double pixelsPerNanoSec = (width - nameSpace <= RIGHT_MARGIN) ? 0 : (double) (width - nameSpace - RIGHT_MARGIN) / (time1 - time0); |
| 928 | int x = getBounds().x + nameSpace + (int) ((time - time0) * pixelsPerNanoSec); |
| 929 | return x; |
| 930 | } |
| 931 | |
| 932 | /** |
| 933 | * Return the time corresponding to an x coordinate |
| 934 | * |
| 935 | * @param coord The X coordinate |
| 936 | * @return The time corresponding to the x coordinate |
| 937 | * |
| 938 | * @since 2.0 |
| 939 | */ |
| 940 | public long getTimeAtX(int coord) { |
| 941 | if (null == _timeProvider) { |
| 942 | return -1; |
| 943 | } |
| 944 | long hitTime = -1; |
| 945 | Point size = getCtrlSize(); |
| 946 | long time0 = _timeProvider.getTime0(); |
| 947 | long time1 = _timeProvider.getTime1(); |
| 948 | int nameWidth = _timeProvider.getNameSpace(); |
| 949 | final int x = coord - nameWidth; |
| 950 | int timeWidth = size.x - nameWidth - RIGHT_MARGIN; |
| 951 | if (x >= 0 && size.x >= nameWidth) { |
| 952 | if (time1 - time0 > timeWidth) { |
| 953 | // nanosecond smaller than one pixel: use the first integer nanosecond of this pixel's time range |
| 954 | hitTime = time0 + (long) Math.ceil((time1 - time0) * ((double) x / timeWidth)); |
| 955 | } else { |
| 956 | // nanosecond greater than one pixel: use the nanosecond that covers this pixel start position |
| 957 | hitTime = time0 + (long) Math.floor((time1 - time0) * ((double) x / timeWidth)); |
| 958 | } |
| 959 | } |
| 960 | return hitTime; |
| 961 | } |
| 962 | |
| 963 | void selectItem(int idx, boolean addSelection) { |
| 964 | boolean changed = false; |
| 965 | if (addSelection) { |
| 966 | if (idx >= 0 && idx < _data._expandedItems.length) { |
| 967 | Item item = _data._expandedItems[idx]; |
| 968 | changed = (item._selected == false); |
| 969 | item._selected = true; |
| 970 | } |
| 971 | } else { |
| 972 | for (int i = 0; i < _data._expandedItems.length; i++) { |
| 973 | Item item = _data._expandedItems[i]; |
| 974 | if ((i == idx && !item._selected) || (idx == -1 && item._selected)) { |
| 975 | changed = true; |
| 976 | } |
| 977 | item._selected = i == idx; |
| 978 | } |
| 979 | } |
| 980 | changed |= ensureVisibleItem(idx, true); |
| 981 | if (changed) { |
| 982 | redraw(); |
| 983 | } |
| 984 | } |
| 985 | |
| 986 | /** |
| 987 | * Callback for item selection |
| 988 | * |
| 989 | * @param trace |
| 990 | * The entry matching the trace |
| 991 | * @param addSelection |
| 992 | * If the selection is added or removed |
| 993 | */ |
| 994 | public void selectItem(ITimeGraphEntry trace, boolean addSelection) { |
| 995 | int idx = _data.findItemIndex(trace); |
| 996 | selectItem(idx, addSelection); |
| 997 | } |
| 998 | |
| 999 | /** |
| 1000 | * Retrieve the number of entries shown per page. |
| 1001 | * |
| 1002 | * @return The count |
| 1003 | */ |
| 1004 | public int countPerPage() { |
| 1005 | int height = getCtrlSize().y; |
| 1006 | int count = 0; |
| 1007 | if (_itemHeight == CUSTOM_ITEM_HEIGHT) { |
| 1008 | int ySum = 0; |
| 1009 | for (int idx = _topIndex; idx < _data._expandedItems.length; idx++) { |
| 1010 | ySum += _data._expandedItems[idx].itemHeight; |
| 1011 | if (ySum >= height) { |
| 1012 | return count; |
| 1013 | } |
| 1014 | count++; |
| 1015 | } |
| 1016 | for (int idx = _topIndex - 1; idx >= 0; idx--) { |
| 1017 | ySum += _data._expandedItems[idx].itemHeight; |
| 1018 | if (ySum >= height) { |
| 1019 | return count; |
| 1020 | } |
| 1021 | count++; |
| 1022 | } |
| 1023 | return count; |
| 1024 | } |
| 1025 | if (height > 0) { |
| 1026 | count = height / _itemHeight; |
| 1027 | } |
| 1028 | return count; |
| 1029 | } |
| 1030 | |
| 1031 | /** |
| 1032 | * Get the index of the top element |
| 1033 | * |
| 1034 | * @return The index |
| 1035 | */ |
| 1036 | public int getTopIndex() { |
| 1037 | return _topIndex; |
| 1038 | } |
| 1039 | |
| 1040 | /** |
| 1041 | * Get the number of expanded items |
| 1042 | * |
| 1043 | * @return The count of expanded items |
| 1044 | */ |
| 1045 | public int getExpandedElementCount() { |
| 1046 | return _data._expandedItems.length; |
| 1047 | } |
| 1048 | |
| 1049 | /** |
| 1050 | * Get an array of all expanded elements |
| 1051 | * |
| 1052 | * @return The expanded elements |
| 1053 | */ |
| 1054 | public ITimeGraphEntry[] getExpandedElements() { |
| 1055 | ArrayList<ITimeGraphEntry> elements = new ArrayList<ITimeGraphEntry>(); |
| 1056 | for (Item item : _data._expandedItems) { |
| 1057 | elements.add(item._trace); |
| 1058 | } |
| 1059 | return elements.toArray(new ITimeGraphEntry[0]); |
| 1060 | } |
| 1061 | |
| 1062 | Point getCtrlSize() { |
| 1063 | Point size = getSize(); |
| 1064 | if (getHorizontalBar().isVisible()) { |
| 1065 | size.y -= getHorizontalBar().getSize().y; |
| 1066 | } |
| 1067 | return size; |
| 1068 | } |
| 1069 | |
| 1070 | Rectangle getNameRect(Rectangle bound, int idx, int nameWidth) { |
| 1071 | int x = bound.x; |
| 1072 | int y = bound.y + (idx - _topIndex) * _itemHeight; |
| 1073 | int width = nameWidth; |
| 1074 | int height = _itemHeight; |
| 1075 | if (_itemHeight == CUSTOM_ITEM_HEIGHT) { |
| 1076 | int ySum = 0; |
| 1077 | for (int i = _topIndex; i < idx; i++) { |
| 1078 | ySum += _data._expandedItems[i].itemHeight; |
| 1079 | } |
| 1080 | y = bound.y + ySum; |
| 1081 | height = _data._expandedItems[idx].itemHeight; |
| 1082 | } |
| 1083 | return new Rectangle(x, y, width, height); |
| 1084 | } |
| 1085 | |
| 1086 | Rectangle getStatesRect(Rectangle bound, int idx, int nameWidth) { |
| 1087 | int x = bound.x + nameWidth; |
| 1088 | int y = bound.y + (idx - _topIndex) * _itemHeight; |
| 1089 | int width = bound.width - x; |
| 1090 | int height = _itemHeight; |
| 1091 | if (_itemHeight == CUSTOM_ITEM_HEIGHT) { |
| 1092 | int ySum = 0; |
| 1093 | for (int i = _topIndex; i < idx; i++) { |
| 1094 | ySum += _data._expandedItems[i].itemHeight; |
| 1095 | } |
| 1096 | y = bound.y + ySum; |
| 1097 | height = _data._expandedItems[idx].itemHeight; |
| 1098 | } |
| 1099 | return new Rectangle(x, y, width, height); |
| 1100 | } |
| 1101 | |
| 1102 | @Override |
| 1103 | void paint(Rectangle bounds, PaintEvent e) { |
| 1104 | GC gc = e.gc; |
| 1105 | gc.setBackground(_colors.getColor(TimeGraphColorScheme.BACKGROUND)); |
| 1106 | drawBackground(gc, bounds.x, bounds.y, bounds.width, bounds.height); |
| 1107 | |
| 1108 | if (bounds.width < 2 || bounds.height < 2 || null == _timeProvider) { |
| 1109 | return; |
| 1110 | } |
| 1111 | |
| 1112 | _idealNameSpace = 0; |
| 1113 | int nameSpace = _timeProvider.getNameSpace(); |
| 1114 | |
| 1115 | // draw empty name space background |
| 1116 | gc.setBackground(_colors.getBkColor(false, false, true)); |
| 1117 | drawBackground(gc, bounds.x, bounds.y, nameSpace, bounds.height); |
| 1118 | |
| 1119 | if (_dragState == DRAG_ZOOM) { |
| 1120 | // draw selected zoom region background |
| 1121 | gc.setBackground(_colors.getBkColor(false, false, true)); |
| 1122 | if (_dragX0 < _dragX) { |
| 1123 | gc.fillRectangle(new Rectangle(_dragX0, bounds.y, _dragX - _dragX0, bounds.height)); |
| 1124 | } else if (_dragX0 > _dragX) { |
| 1125 | gc.fillRectangle(new Rectangle(_dragX, bounds.y, _dragX0 - _dragX, bounds.height)); |
| 1126 | } |
| 1127 | } |
| 1128 | |
| 1129 | drawItems(bounds, _timeProvider, _data._expandedItems, _topIndex, nameSpace, gc); |
| 1130 | |
| 1131 | // draw selected time |
| 1132 | long time0 = _timeProvider.getTime0(); |
| 1133 | long time1 = _timeProvider.getTime1(); |
| 1134 | long selectedTime = _timeProvider.getSelectedTime(); |
| 1135 | double pixelsPerNanoSec = (bounds.width - nameSpace <= RIGHT_MARGIN) ? 0 : (double) (bounds.width - nameSpace - RIGHT_MARGIN) / (time1 - time0); |
| 1136 | int x = bounds.x + nameSpace + (int) ((selectedTime - time0) * pixelsPerNanoSec); |
| 1137 | if (x >= nameSpace && x < bounds.x + bounds.width) { |
| 1138 | gc.setForeground(_colors.getColor(TimeGraphColorScheme.SELECTED_TIME)); |
| 1139 | gc.drawLine(x, bounds.y, x, bounds.y + bounds.height); |
| 1140 | } |
| 1141 | |
| 1142 | // draw drag line, no line if name space is 0. |
| 1143 | if (DRAG_SPLIT_LINE == _dragState) { |
| 1144 | gc.setForeground(_colors.getColor(TimeGraphColorScheme.BLACK)); |
| 1145 | gc.drawLine(bounds.x + nameSpace, bounds.y, bounds.x + nameSpace, bounds.y + bounds.height - 1); |
| 1146 | } else if (DRAG_ZOOM == _dragState && Math.max(_dragX, _dragX0) > nameSpace) { |
| 1147 | gc.setForeground(_colors.getColor(TimeGraphColorScheme.TOOL_FOREGROUND)); |
| 1148 | gc.drawLine(_dragX0, bounds.y, _dragX0, bounds.y + bounds.height - 1); |
| 1149 | gc.drawLine(_dragX, bounds.y, _dragX, bounds.y + bounds.height - 1); |
| 1150 | } else if (DRAG_NONE == _dragState && _mouseOverSplitLine && _timeProvider.getNameSpace() > 0) { |
| 1151 | gc.setForeground(_colors.getColor(TimeGraphColorScheme.RED)); |
| 1152 | gc.drawLine(bounds.x + nameSpace, bounds.y, bounds.x + nameSpace, bounds.y + bounds.height - 1); |
| 1153 | } |
| 1154 | } |
| 1155 | |
| 1156 | /** |
| 1157 | * Draw many items at once |
| 1158 | * |
| 1159 | * @param bounds |
| 1160 | * The rectangle of the area |
| 1161 | * @param timeProvider |
| 1162 | * The time provider |
| 1163 | * @param items |
| 1164 | * The array items to draw |
| 1165 | * @param topIndex |
| 1166 | * The index of the first element to draw |
| 1167 | * @param nameSpace |
| 1168 | * The width reserved for the names |
| 1169 | * @param gc |
| 1170 | * Reference to the SWT GC object |
| 1171 | */ |
| 1172 | public void drawItems(Rectangle bounds, ITimeDataProvider timeProvider, |
| 1173 | Item[] items, int topIndex, int nameSpace, GC gc) { |
| 1174 | for (int i = topIndex; i < items.length; i++) { |
| 1175 | Item item = items[i]; |
| 1176 | drawItem(item, bounds, timeProvider, i, nameSpace, gc); |
| 1177 | } |
| 1178 | fTimeGraphProvider.postDrawControl(bounds, gc); |
| 1179 | } |
| 1180 | |
| 1181 | /** |
| 1182 | * Draws the item |
| 1183 | * |
| 1184 | * @param item the item to draw |
| 1185 | * @param bounds the container rectangle |
| 1186 | * @param timeProvider Time provider |
| 1187 | * @param i the item index |
| 1188 | * @param nameSpace the name space |
| 1189 | * @param gc Graphics context |
| 1190 | */ |
| 1191 | protected void drawItem(Item item, Rectangle bounds, ITimeDataProvider timeProvider, int i, int nameSpace, GC gc) { |
| 1192 | ITimeGraphEntry entry = item._trace; |
| 1193 | long time0 = timeProvider.getTime0(); |
| 1194 | long time1 = timeProvider.getTime1(); |
| 1195 | long selectedTime = timeProvider.getSelectedTime(); |
| 1196 | |
| 1197 | Rectangle nameRect = getNameRect(bounds, i, nameSpace); |
| 1198 | if (nameRect.y >= bounds.y + bounds.height) { |
| 1199 | return; |
| 1200 | } |
| 1201 | |
| 1202 | if (! item._trace.hasTimeEvents()) { |
| 1203 | Rectangle statesRect = getStatesRect(bounds, i, nameSpace); |
| 1204 | nameRect.width += statesRect.width; |
| 1205 | drawName(item, nameRect, gc); |
| 1206 | } else { |
| 1207 | drawName(item, nameRect, gc); |
| 1208 | } |
| 1209 | Rectangle rect = getStatesRect(bounds, i, nameSpace); |
| 1210 | if (rect.isEmpty()) { |
| 1211 | fTimeGraphProvider.postDrawEntry(entry, rect, gc); |
| 1212 | return; |
| 1213 | } |
| 1214 | if (time1 <= time0) { |
| 1215 | gc.setBackground(_colors.getBkColor(false, false, false)); |
| 1216 | gc.fillRectangle(rect); |
| 1217 | fTimeGraphProvider.postDrawEntry(entry, rect, gc); |
| 1218 | return; |
| 1219 | } |
| 1220 | |
| 1221 | // Initialize _rect1 to same values as enclosing rectangle rect |
| 1222 | Rectangle stateRect = Utils.clone(rect); |
| 1223 | boolean selected = item._selected; |
| 1224 | // K pixels per second |
| 1225 | double pixelsPerNanoSec = (rect.width <= RIGHT_MARGIN) ? 0 : (double) (rect.width - RIGHT_MARGIN) / (time1 - time0); |
| 1226 | |
| 1227 | if (item._trace.hasTimeEvents()) { |
| 1228 | fillSpace(rect, gc, selected); |
| 1229 | // Drawing rectangle is smaller than reserved space |
| 1230 | stateRect.y += 3; |
| 1231 | stateRect.height -= 6; |
| 1232 | |
| 1233 | long maxDuration = (timeProvider.getTimeSpace() == 0) ? Long.MAX_VALUE : 1 * (time1 - time0) / timeProvider.getTimeSpace(); |
| 1234 | Iterator<ITimeEvent> iterator = entry.getTimeEventsIterator(time0, time1, maxDuration); |
| 1235 | |
| 1236 | int lastX = -1; |
| 1237 | while (iterator.hasNext()) { |
| 1238 | ITimeEvent event = iterator.next(); |
| 1239 | int x = rect.x + (int) ((event.getTime() - time0) * pixelsPerNanoSec); |
| 1240 | int xEnd = rect.x + (int) ((event.getTime() + event.getDuration() - time0) * pixelsPerNanoSec); |
| 1241 | if (x >= rect.x + rect.width || xEnd < rect.x) { |
| 1242 | // event is out of bounds |
| 1243 | continue; |
| 1244 | } |
| 1245 | xEnd = Math.min(rect.x + rect.width, xEnd); |
| 1246 | stateRect.x = Math.max(rect.x, x); |
| 1247 | stateRect.width = Math.max(0, xEnd - stateRect.x + 1); |
| 1248 | if (stateRect.x == lastX) { |
| 1249 | stateRect.width -= 1; |
| 1250 | if (stateRect.width > 0) { |
| 1251 | gc.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK)); |
| 1252 | gc.drawPoint(stateRect.x, stateRect.y - 2); |
| 1253 | stateRect.x += 1; |
| 1254 | } |
| 1255 | } |
| 1256 | boolean timeSelected = selectedTime >= event.getTime() && selectedTime < event.getTime() + event.getDuration(); |
| 1257 | if (drawState(_colors, event, stateRect, gc, selected, timeSelected)) { |
| 1258 | lastX = x; |
| 1259 | } |
| 1260 | } |
| 1261 | } |
| 1262 | fTimeGraphProvider.postDrawEntry(entry, rect, gc); |
| 1263 | } |
| 1264 | |
| 1265 | /** |
| 1266 | * Draw the name of an item. |
| 1267 | * |
| 1268 | * @param item |
| 1269 | * Item object |
| 1270 | * @param bounds |
| 1271 | * Where to draw the name |
| 1272 | * @param gc |
| 1273 | * Graphics context |
| 1274 | */ |
| 1275 | protected void drawName(Item item, Rectangle bounds, GC gc) { |
| 1276 | boolean hasTimeEvents = item._trace.hasTimeEvents(); |
| 1277 | if (! hasTimeEvents) { |
| 1278 | gc.setBackground(_colors.getBkColorGroup(item._selected, _isInFocus)); |
| 1279 | gc.fillRectangle(bounds); |
| 1280 | if (item._selected && _isInFocus) { |
| 1281 | gc.setForeground(_colors.getBkColor(item._selected, _isInFocus, false)); |
| 1282 | gc.drawRectangle(bounds.x, bounds.y, bounds.width - 1, bounds.height - 1); |
| 1283 | } |
| 1284 | } else { |
| 1285 | gc.setBackground(_colors.getBkColor(item._selected, _isInFocus, true)); |
| 1286 | gc.setForeground(_colors.getFgColor(item._selected, _isInFocus)); |
| 1287 | gc.fillRectangle(bounds); |
| 1288 | } |
| 1289 | |
| 1290 | // No name to be drawn |
| 1291 | if (_timeProvider.getNameSpace() == 0) { |
| 1292 | return; |
| 1293 | } |
| 1294 | |
| 1295 | int leftMargin = MARGIN + item.level * EXPAND_SIZE; |
| 1296 | if (item._hasChildren) { |
| 1297 | gc.setForeground(_colors.getFgColorGroup(false, false)); |
| 1298 | gc.setBackground(_colors.getBkColor(false, false, false)); |
| 1299 | Rectangle rect = Utils.clone(bounds); |
| 1300 | rect.x += leftMargin; |
| 1301 | rect.y += (bounds.height - EXPAND_SIZE) / 2; |
| 1302 | rect.width = EXPAND_SIZE; |
| 1303 | rect.height = EXPAND_SIZE; |
| 1304 | gc.fillRectangle(rect); |
| 1305 | gc.drawRectangle(rect.x, rect.y, rect.width - 1, rect.height - 1); |
| 1306 | int midy = rect.y + rect.height / 2; |
| 1307 | gc.drawLine(rect.x + 2, midy, rect.x + rect.width - 3, midy); |
| 1308 | if (!item._expanded) { |
| 1309 | int midx = rect.x + rect.width / 2; |
| 1310 | gc.drawLine(midx, rect.y + 2, midx, rect.y + rect.height - 3); |
| 1311 | } |
| 1312 | } |
| 1313 | leftMargin += EXPAND_SIZE + MARGIN; |
| 1314 | |
| 1315 | Image img = fTimeGraphProvider.getItemImage(item._trace); |
| 1316 | if (img != null) { |
| 1317 | // draw icon |
| 1318 | int imgHeight = img.getImageData().height; |
| 1319 | int imgWidth = img.getImageData().width; |
| 1320 | int x = leftMargin; |
| 1321 | int y = bounds.y + (bounds.height - imgHeight) / 2; |
| 1322 | gc.drawImage(img, x, y); |
| 1323 | leftMargin += imgWidth + MARGIN; |
| 1324 | } |
| 1325 | String name = item._name; |
| 1326 | Point size = gc.stringExtent(name); |
| 1327 | if (_idealNameSpace < leftMargin + size.x + MARGIN) { |
| 1328 | _idealNameSpace = leftMargin + size.x + MARGIN; |
| 1329 | } |
| 1330 | if (hasTimeEvents) { |
| 1331 | // cut long string with "..." |
| 1332 | int width = bounds.width - leftMargin; |
| 1333 | int cuts = 0; |
| 1334 | while (size.x > width && name.length() > 1) { |
| 1335 | cuts++; |
| 1336 | name = name.substring(0, name.length() - 1); |
| 1337 | size = gc.stringExtent(name + "..."); //$NON-NLS-1$ |
| 1338 | } |
| 1339 | if (cuts > 0) { |
| 1340 | name += "..."; //$NON-NLS-1$ |
| 1341 | } |
| 1342 | } |
| 1343 | Rectangle rect = Utils.clone(bounds); |
| 1344 | rect.x += leftMargin; |
| 1345 | rect.width -= leftMargin; |
| 1346 | // draw text |
| 1347 | if (rect.width > 0) { |
| 1348 | rect.y += (bounds.height - gc.stringExtent(name).y) / 2; |
| 1349 | gc.setForeground(_colors.getFgColor(item._selected, _isInFocus)); |
| 1350 | int textWidth = Utils.drawText(gc, name, rect, true); |
| 1351 | leftMargin += textWidth + MARGIN; |
| 1352 | rect.y -= 2; |
| 1353 | |
| 1354 | if (hasTimeEvents) { |
| 1355 | // draw middle line |
| 1356 | int x = bounds.x + leftMargin; |
| 1357 | int width = bounds.width - x; |
| 1358 | int midy = bounds.y + bounds.height / 2; |
| 1359 | gc.setForeground(_colors.getColor(TimeGraphColorScheme.MID_LINE)); |
| 1360 | gc.drawLine(x, midy, x + width, midy); |
| 1361 | } |
| 1362 | } |
| 1363 | } |
| 1364 | |
| 1365 | /** |
| 1366 | * Draw the state (color fill) |
| 1367 | * |
| 1368 | * @param colors |
| 1369 | * Color scheme |
| 1370 | * @param event |
| 1371 | * Time event for which we're drawing the state |
| 1372 | * @param rect |
| 1373 | * Where to draw |
| 1374 | * @param gc |
| 1375 | * Graphics context |
| 1376 | * @param selected |
| 1377 | * Is this time event currently selected (so it appears |
| 1378 | * highlighted) |
| 1379 | * @param timeSelected |
| 1380 | * Is the timestamp currently selected |
| 1381 | * @return true if the state was drawn |
| 1382 | * @since 2.0 |
| 1383 | */ |
| 1384 | protected boolean drawState(TimeGraphColorScheme colors, ITimeEvent event, |
| 1385 | Rectangle rect, GC gc, boolean selected, boolean timeSelected) { |
| 1386 | |
| 1387 | int colorIdx = fTimeGraphProvider.getStateTableIndex(event); |
| 1388 | if (colorIdx < 0 && colorIdx != ITimeGraphPresentationProvider.TRANSPARENT) { |
| 1389 | return false; |
| 1390 | } |
| 1391 | boolean visible = rect.width == 0 ? false : true; |
| 1392 | |
| 1393 | if (visible) { |
| 1394 | if (colorIdx == ITimeGraphPresentationProvider.TRANSPARENT) { |
| 1395 | // Only draw the top and bottom borders |
| 1396 | gc.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK)); |
| 1397 | gc.drawLine(rect.x, rect.y, rect.x + rect.width - 1, rect.y); |
| 1398 | gc.drawLine(rect.x, rect.y + rect.height - 1, rect.x + rect.width - 1, rect.y + rect.height - 1); |
| 1399 | if (rect.width == 1) { |
| 1400 | gc.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK)); |
| 1401 | gc.drawPoint(rect.x, rect.y - 2); |
| 1402 | } |
| 1403 | return false; |
| 1404 | } |
| 1405 | Color stateColor = null; |
| 1406 | if (colorIdx < fEventColorMap.length) { |
| 1407 | stateColor = fEventColorMap[colorIdx]; |
| 1408 | } else { |
| 1409 | stateColor = Display.getDefault().getSystemColor(SWT.COLOR_BLACK); |
| 1410 | } |
| 1411 | |
| 1412 | boolean reallySelected = timeSelected && selected; |
| 1413 | if (reallySelected) { |
| 1414 | // modify the color? |
| 1415 | } |
| 1416 | // fill all rect area |
| 1417 | gc.setBackground(stateColor); |
| 1418 | gc.fillRectangle(rect); |
| 1419 | // get the border color? |
| 1420 | gc.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK)); |
| 1421 | |
| 1422 | // draw bounds |
| 1423 | if (!reallySelected) { |
| 1424 | // Draw the top and bottom borders i.e. no side borders |
| 1425 | gc.drawLine(rect.x, rect.y, rect.x + rect.width - 1, rect.y); |
| 1426 | gc.drawLine(rect.x, rect.y + rect.height - 1, rect.x + rect.width - 1, rect.y + rect.height - 1); |
| 1427 | } |
| 1428 | } else { |
| 1429 | gc.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK)); |
| 1430 | gc.drawPoint(rect.x, rect.y - 2); |
| 1431 | } |
| 1432 | fTimeGraphProvider.postDrawEvent(event, rect, gc); |
| 1433 | return visible; |
| 1434 | } |
| 1435 | |
| 1436 | /** |
| 1437 | * Fill the space between two contiguous time events |
| 1438 | * |
| 1439 | * @param rect |
| 1440 | * Rectangle to fill |
| 1441 | * @param gc |
| 1442 | * Graphics context |
| 1443 | * @param selected |
| 1444 | * Is this time event selected or not |
| 1445 | */ |
| 1446 | protected void fillSpace(Rectangle rect, GC gc, boolean selected) { |
| 1447 | gc.setBackground(_colors.getBkColor(selected, _isInFocus, false)); |
| 1448 | gc.fillRectangle(rect); |
| 1449 | if (_dragState == DRAG_ZOOM) { |
| 1450 | gc.setBackground(_colors.getBkColor(selected, _isInFocus, true)); |
| 1451 | if (_dragX0 < _dragX) { |
| 1452 | gc.fillRectangle(new Rectangle(_dragX0, rect.y, _dragX - _dragX0, rect.height)); |
| 1453 | } else if (_dragX0 > _dragX) { |
| 1454 | gc.fillRectangle(new Rectangle(_dragX, rect.y, _dragX0 - _dragX, rect.height)); |
| 1455 | } |
| 1456 | } |
| 1457 | // draw middle line |
| 1458 | gc.setForeground(_colors.getColor(TimeGraphColorScheme.MID_LINE)); |
| 1459 | int midy = rect.y + rect.height / 2; |
| 1460 | gc.drawLine(rect.x, midy, rect.x + rect.width, midy); |
| 1461 | } |
| 1462 | |
| 1463 | @Override |
| 1464 | public void keyTraversed(TraverseEvent e) { |
| 1465 | if ((e.detail == SWT.TRAVERSE_TAB_NEXT) || (e.detail == SWT.TRAVERSE_TAB_PREVIOUS)) { |
| 1466 | e.doit = true; |
| 1467 | } |
| 1468 | } |
| 1469 | |
| 1470 | @Override |
| 1471 | public void keyPressed(KeyEvent e) { |
| 1472 | int idx = -1; |
| 1473 | if (_data._expandedItems.length == 0) { |
| 1474 | return; |
| 1475 | } |
| 1476 | if (SWT.HOME == e.keyCode) { |
| 1477 | idx = 0; |
| 1478 | } else if (SWT.END == e.keyCode) { |
| 1479 | idx = _data._expandedItems.length - 1; |
| 1480 | } else if (SWT.ARROW_DOWN == e.keyCode) { |
| 1481 | idx = getSelectedIndex(); |
| 1482 | if (idx < 0) { |
| 1483 | idx = 0; |
| 1484 | } else if (idx < _data._expandedItems.length - 1) { |
| 1485 | idx++; |
| 1486 | } |
| 1487 | } else if (SWT.ARROW_UP == e.keyCode) { |
| 1488 | idx = getSelectedIndex(); |
| 1489 | if (idx < 0) { |
| 1490 | idx = 0; |
| 1491 | } else if (idx > 0) { |
| 1492 | idx--; |
| 1493 | } |
| 1494 | } else if (SWT.ARROW_LEFT == e.keyCode) { |
| 1495 | selectPrevEvent(); |
| 1496 | } else if (SWT.ARROW_RIGHT == e.keyCode) { |
| 1497 | selectNextEvent(); |
| 1498 | } else if (SWT.PAGE_DOWN == e.keyCode) { |
| 1499 | int page = countPerPage(); |
| 1500 | idx = getSelectedIndex(); |
| 1501 | if (idx < 0) { |
| 1502 | idx = 0; |
| 1503 | } |
| 1504 | idx += page; |
| 1505 | if (idx >= _data._expandedItems.length) { |
| 1506 | idx = _data._expandedItems.length - 1; |
| 1507 | } |
| 1508 | } else if (SWT.PAGE_UP == e.keyCode) { |
| 1509 | int page = countPerPage(); |
| 1510 | idx = getSelectedIndex(); |
| 1511 | if (idx < 0) { |
| 1512 | idx = 0; |
| 1513 | } |
| 1514 | idx -= page; |
| 1515 | if (idx < 0) { |
| 1516 | idx = 0; |
| 1517 | } |
| 1518 | } else if (SWT.CR == e.keyCode) { |
| 1519 | idx = getSelectedIndex(); |
| 1520 | if (idx >= 0) { |
| 1521 | if (_data._expandedItems[idx]._hasChildren) { |
| 1522 | toggle(idx); |
| 1523 | } else { |
| 1524 | fireDefaultSelection(); |
| 1525 | } |
| 1526 | } |
| 1527 | idx = -1; |
| 1528 | } |
| 1529 | if (idx >= 0) { |
| 1530 | selectItem(idx, false); |
| 1531 | fireSelectionChanged(); |
| 1532 | } |
| 1533 | } |
| 1534 | |
| 1535 | @Override |
| 1536 | public void keyReleased(KeyEvent e) { |
| 1537 | } |
| 1538 | |
| 1539 | @Override |
| 1540 | public void focusGained(FocusEvent e) { |
| 1541 | _isInFocus = true; |
| 1542 | if (mouseScrollFilterListener == null) { |
| 1543 | mouseScrollFilterListener = new Listener() { |
| 1544 | // This filter is used to prevent horizontal scrolling of the view |
| 1545 | // when the mouse wheel is used to zoom |
| 1546 | @Override |
| 1547 | public void handleEvent(Event event) { |
| 1548 | event.doit = false; |
| 1549 | } |
| 1550 | }; |
| 1551 | getDisplay().addFilter(SWT.MouseWheel, mouseScrollFilterListener); |
| 1552 | } |
| 1553 | redraw(); |
| 1554 | } |
| 1555 | |
| 1556 | @Override |
| 1557 | public void focusLost(FocusEvent e) { |
| 1558 | _isInFocus = false; |
| 1559 | if (mouseScrollFilterListener != null) { |
| 1560 | getDisplay().removeFilter(SWT.MouseWheel, mouseScrollFilterListener); |
| 1561 | mouseScrollFilterListener = null; |
| 1562 | } |
| 1563 | if (DRAG_NONE != _dragState) { |
| 1564 | setCapture(false); |
| 1565 | _dragState = DRAG_NONE; |
| 1566 | } |
| 1567 | redraw(); |
| 1568 | } |
| 1569 | |
| 1570 | /** |
| 1571 | * @return If the current view is focused |
| 1572 | */ |
| 1573 | public boolean isInFocus() { |
| 1574 | return _isInFocus; |
| 1575 | } |
| 1576 | |
| 1577 | /** |
| 1578 | * Provide the possibility to control the wait cursor externally e.g. data |
| 1579 | * requests in progress |
| 1580 | * |
| 1581 | * @param waitInd Should we wait indefinitely? |
| 1582 | */ |
| 1583 | public void waitCursor(boolean waitInd) { |
| 1584 | // Update cursor as indicated |
| 1585 | if (waitInd) { |
| 1586 | setCursor(_WaitCursor); |
| 1587 | _isWaitCursor = true; |
| 1588 | } else { |
| 1589 | setCursor(null); |
| 1590 | _isWaitCursor = false; |
| 1591 | } |
| 1592 | |
| 1593 | // Get ready for next mouse move |
| 1594 | _isDragCursor3 = false; |
| 1595 | } |
| 1596 | |
| 1597 | /** |
| 1598 | * <p> |
| 1599 | * If the x, y position is over the vertical split line (name to time |
| 1600 | * ranges), then change the cursor to a drag cursor to indicate the user the |
| 1601 | * possibility of resizing |
| 1602 | * </p> |
| 1603 | * |
| 1604 | * @param x |
| 1605 | * @param y |
| 1606 | */ |
| 1607 | void updateCursor(int x, int y) { |
| 1608 | // if Wait cursor not active, check for the need to change to a drag |
| 1609 | // cursor |
| 1610 | if (_isWaitCursor == false) { |
| 1611 | boolean isSplitLine = isOverSplitLine(x); |
| 1612 | // No dragcursor is name space is fixed to zero |
| 1613 | if (isSplitLine && !_isDragCursor3 && _timeProvider.getNameSpace() > 0) { |
| 1614 | setCursor(_dragCursor3); |
| 1615 | _isDragCursor3 = true; |
| 1616 | } else if (!isSplitLine && _isDragCursor3) { |
| 1617 | setCursor(null); |
| 1618 | _isDragCursor3 = false; |
| 1619 | } |
| 1620 | } |
| 1621 | } |
| 1622 | |
| 1623 | @Override |
| 1624 | public void mouseMove(MouseEvent e) { |
| 1625 | if (null == _timeProvider) { |
| 1626 | return; |
| 1627 | } |
| 1628 | Point size = getCtrlSize(); |
| 1629 | if (DRAG_TRACE_ITEM == _dragState) { |
| 1630 | int nameWidth = _timeProvider.getNameSpace(); |
| 1631 | int x = e.x - nameWidth; |
| 1632 | if (x > 0 && size.x > nameWidth && _dragX != x) { |
| 1633 | _dragX = x; |
| 1634 | double pixelsPerNanoSec = (size.x - nameWidth <= RIGHT_MARGIN) ? 0 : (double) (size.x - nameWidth - RIGHT_MARGIN) / (_time1bak - _time0bak); |
| 1635 | long timeDelta = (long) ((pixelsPerNanoSec == 0) ? 0 : ((_dragX - _dragX0) / pixelsPerNanoSec)); |
| 1636 | long time1 = _time1bak - timeDelta; |
| 1637 | long maxTime = _timeProvider.getMaxTime(); |
| 1638 | if (time1 > maxTime) { |
| 1639 | time1 = maxTime; |
| 1640 | } |
| 1641 | long time0 = time1 - (_time1bak - _time0bak); |
| 1642 | if (time0 < _timeProvider.getMinTime()) { |
| 1643 | time0 = _timeProvider.getMinTime(); |
| 1644 | time1 = time0 + (_time1bak - _time0bak); |
| 1645 | } |
| 1646 | _timeProvider.setStartFinishTime(time0, time1); |
| 1647 | } |
| 1648 | } else if (DRAG_SPLIT_LINE == _dragState) { |
| 1649 | _dragX = e.x; |
| 1650 | _timeProvider.setNameSpace(e.x); |
| 1651 | } else if (DRAG_ZOOM == _dragState) { |
| 1652 | _dragX = Math.min(Math.max(e.x, _timeProvider.getNameSpace()), size.x - RIGHT_MARGIN); |
| 1653 | redraw(); |
| 1654 | } else if (DRAG_NONE == _dragState) { |
| 1655 | boolean mouseOverSplitLine = isOverSplitLine(e.x); |
| 1656 | if (_mouseOverSplitLine != mouseOverSplitLine) { |
| 1657 | redraw(); |
| 1658 | } |
| 1659 | _mouseOverSplitLine = mouseOverSplitLine; |
| 1660 | } |
| 1661 | updateCursor(e.x, e.y); |
| 1662 | } |
| 1663 | |
| 1664 | @Override |
| 1665 | public void mouseDoubleClick(MouseEvent e) { |
| 1666 | if (null == _timeProvider) { |
| 1667 | return; |
| 1668 | } |
| 1669 | if (1 == e.button && (e.stateMask & SWT.BUTTON_MASK) == 0) { |
| 1670 | if (isOverSplitLine(e.x) && _timeProvider.getNameSpace() != 0) { |
| 1671 | _timeProvider.setNameSpace(_idealNameSpace); |
| 1672 | boolean mouseOverSplitLine = isOverSplitLine(e.x); |
| 1673 | if (_mouseOverSplitLine != mouseOverSplitLine) { |
| 1674 | redraw(); |
| 1675 | } |
| 1676 | _mouseOverSplitLine = mouseOverSplitLine; |
| 1677 | return; |
| 1678 | } |
| 1679 | int idx = getItemIndexAtY(e.y); |
| 1680 | if (idx >= 0) { |
| 1681 | selectItem(idx, false); |
| 1682 | fireDefaultSelection(); |
| 1683 | } |
| 1684 | } |
| 1685 | } |
| 1686 | |
| 1687 | @Override |
| 1688 | public void mouseDown(MouseEvent e) { |
| 1689 | if (_dragState != DRAG_NONE || null == _timeProvider) { |
| 1690 | return; |
| 1691 | } |
| 1692 | int idx; |
| 1693 | if (1 == e.button) { |
| 1694 | int nameSpace = _timeProvider.getNameSpace(); |
| 1695 | if (nameSpace != 0) { |
| 1696 | if (isOverSplitLine(e.x)) { |
| 1697 | _dragState = DRAG_SPLIT_LINE; |
| 1698 | _dragX = _dragX0 = e.x; |
| 1699 | _time0bak = _timeProvider.getTime0(); |
| 1700 | _time1bak = _timeProvider.getTime1(); |
| 1701 | redraw(); |
| 1702 | return; |
| 1703 | } |
| 1704 | } |
| 1705 | |
| 1706 | idx = getItemIndexAtY(e.y); |
| 1707 | if (idx >= 0) { |
| 1708 | Item item = _data._expandedItems[idx]; |
| 1709 | if (item._hasChildren && e.x < nameSpace && e.x < MARGIN + (item.level + 1) * EXPAND_SIZE) { |
| 1710 | toggle(idx); |
| 1711 | } else { |
| 1712 | long hitTime = getTimeAtX(e.x); |
| 1713 | if (hitTime >= 0) { |
| 1714 | // _timeProvider.setSelectedTimeInt(hitTime, false); |
| 1715 | setCapture(true); |
| 1716 | _dragState = DRAG_TRACE_ITEM; |
| 1717 | _dragX = _dragX0 = e.x - nameSpace; |
| 1718 | _time0bak = _timeProvider.getTime0(); |
| 1719 | _time1bak = _timeProvider.getTime1(); |
| 1720 | } |
| 1721 | } |
| 1722 | selectItem(idx, false); |
| 1723 | fireSelectionChanged(); |
| 1724 | } else { |
| 1725 | selectItem(idx, false); // clear selection |
| 1726 | redraw(); |
| 1727 | fireSelectionChanged(); |
| 1728 | } |
| 1729 | } else if (3 == e.button) { |
| 1730 | if (_timeProvider.getTime0() == _timeProvider.getTime1() || getCtrlSize().x - _timeProvider.getNameSpace() <= 0) { |
| 1731 | return; |
| 1732 | } |
| 1733 | setCapture(true); |
| 1734 | _dragX = _dragX0 = Math.min(Math.max(e.x, _timeProvider.getNameSpace()), getCtrlSize().x - RIGHT_MARGIN); |
| 1735 | _dragState = DRAG_ZOOM; |
| 1736 | } |
| 1737 | } |
| 1738 | |
| 1739 | @Override |
| 1740 | public void mouseUp(MouseEvent e) { |
| 1741 | if (DRAG_NONE != _dragState) { |
| 1742 | setCapture(false); |
| 1743 | if (e.button == 1 && DRAG_TRACE_ITEM == _dragState) { |
| 1744 | if (_dragX == _dragX0) { // click without drag |
| 1745 | long time = getTimeAtX(e.x); |
| 1746 | _timeProvider.setSelectedTimeNotify(time, false); |
| 1747 | } else { |
| 1748 | // Notify time provider to check the need for listener |
| 1749 | // notification |
| 1750 | _timeProvider.notifyStartFinishTime(); |
| 1751 | } |
| 1752 | _dragState = DRAG_NONE; |
| 1753 | } else if (e.button == 1 && DRAG_SPLIT_LINE == _dragState) { |
| 1754 | redraw(); |
| 1755 | _dragState = DRAG_NONE; |
| 1756 | } else if (e.button == 3 && DRAG_ZOOM == _dragState) { |
| 1757 | int nameWidth = _timeProvider.getNameSpace(); |
| 1758 | if (Math.max(_dragX, _dragX0) > nameWidth && _dragX != _dragX0) { |
| 1759 | long time0 = getTimeAtX(_dragX0); |
| 1760 | long time1 = getTimeAtX(_dragX); |
| 1761 | if (time0 < time1) { |
| 1762 | _timeProvider.setStartFinishTimeNotify(time0, time1); |
| 1763 | } else { |
| 1764 | _timeProvider.setStartFinishTimeNotify(time1, time0); |
| 1765 | } |
| 1766 | } else { |
| 1767 | redraw(); |
| 1768 | } |
| 1769 | _dragState = DRAG_NONE; |
| 1770 | } |
| 1771 | } |
| 1772 | } |
| 1773 | |
| 1774 | @Override |
| 1775 | public void mouseEnter(MouseEvent e) { |
| 1776 | } |
| 1777 | |
| 1778 | @Override |
| 1779 | public void mouseExit(MouseEvent e) { |
| 1780 | if (_mouseOverSplitLine) { |
| 1781 | _mouseOverSplitLine = false; |
| 1782 | redraw(); |
| 1783 | } |
| 1784 | } |
| 1785 | |
| 1786 | @Override |
| 1787 | public void mouseHover(MouseEvent e) { |
| 1788 | } |
| 1789 | |
| 1790 | @Override |
| 1791 | public void mouseScrolled(MouseEvent e) { |
| 1792 | if ((mouseScrollFilterListener == null) || _dragState != DRAG_NONE) { |
| 1793 | return; |
| 1794 | } |
| 1795 | boolean zoomScroll = false; |
| 1796 | Point p = getParent().toControl(getDisplay().getCursorLocation()); |
| 1797 | Point parentSize = getParent().getSize(); |
| 1798 | if (p.x >= 0 && p.x < parentSize.x && p.y >= 0 && p.y < parentSize.y) { |
| 1799 | // over the parent control |
| 1800 | if (e.x > getCtrlSize().x) { |
| 1801 | // over the horizontal scroll bar |
| 1802 | zoomScroll = false; |
| 1803 | } else if (e.y >= 0 && e.y < getCtrlSize().y && e.x < _timeProvider.getNameSpace()) { |
| 1804 | // over the name space |
| 1805 | zoomScroll = false; |
| 1806 | } else { |
| 1807 | zoomScroll = true; |
| 1808 | } |
| 1809 | } |
| 1810 | if (zoomScroll && _timeProvider.getTime0() != _timeProvider.getTime1()) { |
| 1811 | if (e.count > 0) { |
| 1812 | zoom(true); |
| 1813 | } else if (e.count < 0) { |
| 1814 | zoom(false); |
| 1815 | } |
| 1816 | } else { |
| 1817 | setTopIndex(getTopIndex() - e.count); |
| 1818 | } |
| 1819 | } |
| 1820 | |
| 1821 | @Override |
| 1822 | public void controlMoved(ControlEvent e) { |
| 1823 | } |
| 1824 | |
| 1825 | @Override |
| 1826 | public void controlResized(ControlEvent e) { |
| 1827 | adjustScrolls(); |
| 1828 | } |
| 1829 | |
| 1830 | @Override |
| 1831 | public void widgetDefaultSelected(SelectionEvent e) { |
| 1832 | } |
| 1833 | |
| 1834 | @Override |
| 1835 | public void widgetSelected(SelectionEvent e) { |
| 1836 | if (e.widget == getVerticalBar()) { |
| 1837 | setTopIndex(getVerticalBar().getSelection()); |
| 1838 | } else if (e.widget == getHorizontalBar() && null != _timeProvider) { |
| 1839 | int start = getHorizontalBar().getSelection(); |
| 1840 | long time0 = _timeProvider.getTime0(); |
| 1841 | long time1 = _timeProvider.getTime1(); |
| 1842 | long timeMin = _timeProvider.getMinTime(); |
| 1843 | long timeMax = _timeProvider.getMaxTime(); |
| 1844 | long delta = timeMax - timeMin; |
| 1845 | |
| 1846 | long range = time1 - time0; |
| 1847 | // _timeRangeFixed = true; |
| 1848 | time0 = timeMin + Math.round(delta * ((double) start / H_SCROLLBAR_MAX)); |
| 1849 | time1 = time0 + range; |
| 1850 | |
| 1851 | // TODO: Follow-up with Bug 310310 |
| 1852 | // In Linux SWT.DRAG is the only value received |
| 1853 | // https://bugs.eclipse.org/bugs/show_bug.cgi?id=310310 |
| 1854 | if (e.detail == SWT.DRAG) { |
| 1855 | _timeProvider.setStartFinishTime(time0, time1); |
| 1856 | } else { |
| 1857 | _timeProvider.setStartFinishTimeNotify(time0, time1); |
| 1858 | } |
| 1859 | } |
| 1860 | } |
| 1861 | |
| 1862 | /** |
| 1863 | * @return The current visibility of the vertical scroll bar |
| 1864 | */ |
| 1865 | public boolean isVisibleVerticalScroll() { |
| 1866 | return _visibleVerticalScroll; |
| 1867 | } |
| 1868 | |
| 1869 | @Override |
| 1870 | public int getBorderWidth() { |
| 1871 | return _borderWidth; |
| 1872 | } |
| 1873 | |
| 1874 | /** |
| 1875 | * Set the border width |
| 1876 | * |
| 1877 | * @param borderWidth |
| 1878 | * The width |
| 1879 | */ |
| 1880 | public void setBorderWidth(int borderWidth) { |
| 1881 | this._borderWidth = borderWidth; |
| 1882 | } |
| 1883 | |
| 1884 | /** |
| 1885 | * @return The current height of the header row |
| 1886 | */ |
| 1887 | public int getHeaderHeight() { |
| 1888 | return _headerHeight; |
| 1889 | } |
| 1890 | |
| 1891 | /** |
| 1892 | * Set the height of the header row |
| 1893 | * |
| 1894 | * @param headerHeight |
| 1895 | * The height |
| 1896 | */ |
| 1897 | public void setHeaderHeight(int headerHeight) { |
| 1898 | this._headerHeight = headerHeight; |
| 1899 | } |
| 1900 | |
| 1901 | /** |
| 1902 | * @return The height of regular item rows |
| 1903 | */ |
| 1904 | public int getItemHeight() { |
| 1905 | return _itemHeight; |
| 1906 | } |
| 1907 | |
| 1908 | /** |
| 1909 | * Set the height of regular itew rows |
| 1910 | * |
| 1911 | * @param rowHeight |
| 1912 | * The height |
| 1913 | */ |
| 1914 | public void setItemHeight(int rowHeight) { |
| 1915 | this._itemHeight = rowHeight; |
| 1916 | } |
| 1917 | |
| 1918 | /** |
| 1919 | * Set the minimum item width |
| 1920 | * |
| 1921 | * @param width The minimum width |
| 1922 | */ |
| 1923 | public void setMinimumItemWidth(int width) { |
| 1924 | this._minimumItemWidth = width; |
| 1925 | } |
| 1926 | |
| 1927 | /** |
| 1928 | * @return The minimum item width |
| 1929 | */ |
| 1930 | public int getMinimumItemWidth() { |
| 1931 | return _minimumItemWidth; |
| 1932 | } |
| 1933 | |
| 1934 | /** |
| 1935 | * @return The entries that are currently filtered out |
| 1936 | */ |
| 1937 | public Vector<ITimeGraphEntry> getFilteredOut() { |
| 1938 | return _data.getFilteredOut(); |
| 1939 | } |
| 1940 | |
| 1941 | // @Override |
| 1942 | @Override |
| 1943 | public void addSelectionChangedListener(ISelectionChangedListener listener) { |
| 1944 | if (listener != null) { |
| 1945 | if (!_selectionChangedListeners.contains(listener)) { |
| 1946 | _selectionChangedListeners.add(listener); |
| 1947 | } |
| 1948 | } |
| 1949 | } |
| 1950 | |
| 1951 | // @Override |
| 1952 | @Override |
| 1953 | public void removeSelectionChangedListener(ISelectionChangedListener listener) { |
| 1954 | if (listener != null) { |
| 1955 | _selectionChangedListeners.remove(listener); |
| 1956 | } |
| 1957 | } |
| 1958 | |
| 1959 | // @Override |
| 1960 | @Override |
| 1961 | public void setSelection(ISelection selection) { |
| 1962 | if (selection instanceof TimeGraphSelection) { |
| 1963 | TimeGraphSelection sel = (TimeGraphSelection) selection; |
| 1964 | Object ob = sel.getFirstElement(); |
| 1965 | if (ob instanceof ITimeGraphEntry) { |
| 1966 | ITimeGraphEntry trace = (ITimeGraphEntry) ob; |
| 1967 | selectItem(trace, false); |
| 1968 | } |
| 1969 | } |
| 1970 | |
| 1971 | } |
| 1972 | |
| 1973 | /** |
| 1974 | * @param filter The filter object to be attached to the view |
| 1975 | * @since 2.0 |
| 1976 | */ |
| 1977 | public void addFilter(ViewerFilter filter) { |
| 1978 | if (!_filters.contains(filter)) { |
| 1979 | _filters.add(filter); |
| 1980 | } |
| 1981 | } |
| 1982 | |
| 1983 | /** |
| 1984 | * @param filter The filter object to be attached to the view |
| 1985 | * @since 2.0 |
| 1986 | */ |
| 1987 | public void removeFilter(ViewerFilter filter) { |
| 1988 | _filters.remove(filter); |
| 1989 | } |
| 1990 | |
| 1991 | private class ItemData { |
| 1992 | public Item[] _expandedItems = new Item[0]; |
| 1993 | public Item[] _items = new Item[0]; |
| 1994 | private ITimeGraphEntry _traces[] = new ITimeGraphEntry[0]; |
| 1995 | private boolean traceFilter[] = new boolean[0]; |
| 1996 | private final Vector<ITimeGraphEntry> filteredOut = new Vector<ITimeGraphEntry>(); |
| 1997 | public ITimeGraphPresentationProvider provider; |
| 1998 | |
| 1999 | public ItemData() { |
| 2000 | } |
| 2001 | |
| 2002 | Item findItem(ITimeGraphEntry entry) { |
| 2003 | if (entry == null) { |
| 2004 | return null; |
| 2005 | } |
| 2006 | |
| 2007 | for (int i = 0; i < _items.length; i++) { |
| 2008 | Item item = _items[i]; |
| 2009 | if (item._trace == entry) { |
| 2010 | return item; |
| 2011 | } |
| 2012 | } |
| 2013 | |
| 2014 | return null; |
| 2015 | } |
| 2016 | |
| 2017 | int findItemIndex(ITimeGraphEntry trace) { |
| 2018 | if (trace == null) { |
| 2019 | return -1; |
| 2020 | } |
| 2021 | |
| 2022 | for (int i = 0; i < _expandedItems.length; i++) { |
| 2023 | Item item = _expandedItems[i]; |
| 2024 | if (item._trace == trace) { |
| 2025 | return i; |
| 2026 | } |
| 2027 | } |
| 2028 | |
| 2029 | return -1; |
| 2030 | } |
| 2031 | |
| 2032 | public void refreshData() { |
| 2033 | List<Item> itemList = new ArrayList<Item>(); |
| 2034 | filteredOut.clear(); |
| 2035 | for (int i = 0; i < _traces.length; i++) { |
| 2036 | ITimeGraphEntry entry = _traces[i]; |
| 2037 | refreshData(itemList, null, 0, entry); |
| 2038 | } |
| 2039 | _items = itemList.toArray(new Item[0]); |
| 2040 | updateExpandedItems(); |
| 2041 | } |
| 2042 | |
| 2043 | private void refreshData(List<Item> itemList, Item parent, int level, ITimeGraphEntry entry) { |
| 2044 | Item item = new Item(entry, entry.getName(), level); |
| 2045 | if (parent != null) { |
| 2046 | parent.children.add(item); |
| 2047 | } |
| 2048 | item.itemHeight = provider.getItemHeight(entry); |
| 2049 | itemList.add(item); |
| 2050 | if (entry.hasChildren()) { |
| 2051 | item._expanded = true; |
| 2052 | item._hasChildren = true; |
| 2053 | for (ITimeGraphEntry child : entry.getChildren()) { |
| 2054 | refreshData(itemList, item, level + 1, child); |
| 2055 | } |
| 2056 | } |
| 2057 | } |
| 2058 | |
| 2059 | public void updateExpandedItems() { |
| 2060 | List<Item> expandedItemList = new ArrayList<Item>(); |
| 2061 | for (int i = 0; i < _traces.length; i++) { |
| 2062 | ITimeGraphEntry entry = _traces[i]; |
| 2063 | Item item = findItem(entry); |
| 2064 | refreshExpanded(expandedItemList, item); |
| 2065 | } |
| 2066 | _expandedItems = expandedItemList.toArray(new Item[0]); |
| 2067 | } |
| 2068 | |
| 2069 | private void refreshExpanded(List<Item> expandedItemList, Item item) { |
| 2070 | // Check for filters |
| 2071 | boolean display = true; |
| 2072 | for (ViewerFilter filter : _filters) { |
| 2073 | if (!filter.select(null, item._trace.getParent(), item._trace)) { |
| 2074 | display = false; |
| 2075 | break; |
| 2076 | } |
| 2077 | } |
| 2078 | if (display) { |
| 2079 | expandedItemList.add(item); |
| 2080 | if (item._hasChildren && item._expanded) { |
| 2081 | for (Item child : item.children) { |
| 2082 | refreshExpanded(expandedItemList, child); |
| 2083 | } |
| 2084 | } |
| 2085 | } |
| 2086 | } |
| 2087 | |
| 2088 | public void refreshData(ITimeGraphEntry traces[]) { |
| 2089 | if (traces == null || traces.length == 0) { |
| 2090 | traceFilter = null; |
| 2091 | } else if (traceFilter == null || traces.length != traceFilter.length) { |
| 2092 | traceFilter = new boolean[traces.length]; |
| 2093 | java.util.Arrays.fill(traceFilter, true); |
| 2094 | } |
| 2095 | |
| 2096 | _traces = traces; |
| 2097 | refreshData(); |
| 2098 | } |
| 2099 | |
| 2100 | public ITimeGraphEntry[] getTraces() { |
| 2101 | return _traces; |
| 2102 | } |
| 2103 | |
| 2104 | public boolean[] getTraceFilter() { |
| 2105 | return traceFilter; |
| 2106 | } |
| 2107 | |
| 2108 | public Vector<ITimeGraphEntry> getFilteredOut() { |
| 2109 | return filteredOut; |
| 2110 | } |
| 2111 | } |
| 2112 | |
| 2113 | private class Item { |
| 2114 | public boolean _expanded; |
| 2115 | public boolean _selected; |
| 2116 | public boolean _hasChildren; |
| 2117 | public int itemHeight; |
| 2118 | public int level; |
| 2119 | public List<Item> children; |
| 2120 | public String _name; |
| 2121 | public ITimeGraphEntry _trace; |
| 2122 | |
| 2123 | public Item(ITimeGraphEntry trace, String name, int level) { |
| 2124 | this._trace = trace; |
| 2125 | this._name = name; |
| 2126 | this.level = level; |
| 2127 | this.children = new ArrayList<Item>(); |
| 2128 | } |
| 2129 | |
| 2130 | @Override |
| 2131 | public String toString() { |
| 2132 | return _name; |
| 2133 | } |
| 2134 | } |
| 2135 | |
| 2136 | /** |
| 2137 | * @since 1.2 |
| 2138 | */ |
| 2139 | @Override |
| 2140 | public void menuDetected(MenuDetectEvent e) { |
| 2141 | if (null == _timeProvider) { |
| 2142 | return; |
| 2143 | } |
| 2144 | Point p = toControl(e.x, e.y); |
| 2145 | int idx = getItemIndexAtY(p.y); |
| 2146 | if (idx >= 0 && idx < _data._expandedItems.length) { |
| 2147 | Item item = _data._expandedItems[idx]; |
| 2148 | ITimeGraphEntry entry = item._trace; |
| 2149 | if (entry.hasTimeEvents()) { |
| 2150 | ITimeEvent event = Utils.findEvent(entry, getTimeAtX(p.x), 2); |
| 2151 | if (event != null) { |
| 2152 | e.data = event; |
| 2153 | fireMenuEventOnTimeEvent(e); |
| 2154 | return; |
| 2155 | } |
| 2156 | } |
| 2157 | e.data = entry; |
| 2158 | fireMenuEventOnTimeGraphEntry(e); |
| 2159 | } |
| 2160 | } |
| 2161 | |
| 2162 | } |
| 2163 | |
| 2164 | |