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