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