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