Fix for bug 385437: Control Flow view's "Previous event" button skips
[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 * Add a tree listener
422 *
423 * @param listener
424 * The listener to add
425 */
426 public void addTreeListener(ITimeGraphTreeListener listener) {
427 if (!_treeListeners.contains(listener)) {
428 _treeListeners.add(listener);
429 }
430 }
431
432 /**
433 * Remove a tree listener
434 *
435 * @param listener
436 * The listener to remove
437 */
438 public void removeTreeListener(ITimeGraphTreeListener listener) {
439 if (_treeListeners.contains(listener)) {
440 _treeListeners.remove(listener);
441 }
442 }
443
444 /**
445 * Tree event callback
446 *
447 * @param entry
448 * The affected entry
449 * @param expanded
450 * The expanded state (true for expanded, false for collapsed)
451 */
452 public void fireTreeEvent(ITimeGraphEntry entry, boolean expanded) {
453 TimeGraphTreeExpansionEvent event = new TimeGraphTreeExpansionEvent(this, entry);
454 for (ITimeGraphTreeListener listener : _treeListeners) {
455 if (expanded) {
456 listener.treeExpanded(event);
457 } else {
458 listener.treeCollapsed(event);
459 }
460 }
461 }
462
463 @Override
464 public ISelection getSelection() {
465 TimeGraphSelection sel = new TimeGraphSelection();
466 ITimeGraphEntry trace = getSelectedTrace();
467 if (null != trace && null != _timeProvider) {
468 long selectedTime = _timeProvider.getSelectedTime();
469 ITimeEvent event = Utils.findEvent(trace, selectedTime, 0);
470 if (event != null) {
471 sel.add(event);
472 } else {
473 sel.add(trace);
474 }
475 }
476 return sel;
477 }
478
479 /**
480 * Get the selection object
481 *
482 * @return The selection
483 */
484 public ISelection getSelectionTrace() {
485 TimeGraphSelection sel = new TimeGraphSelection();
486 ITimeGraphEntry trace = getSelectedTrace();
487 if (null != trace) {
488 sel.add(trace);
489 }
490 return sel;
491 }
492
493 /**
494 * Enable/disable one of the traces in the model
495 *
496 * @param n
497 * 1 to enable it, -1 to disable. The method returns immediately
498 * if another value is used.
499 */
500 public void selectTrace(int n) {
501 if ((n != 1) && (n != -1)) {
502 return;
503 }
504
505 boolean changed = false;
506 int lastSelection = -1;
507 for (int i = 0; i < _data._expandedItems.length; i++) {
508 Item item = _data._expandedItems[i];
509 if (item._selected) {
510 lastSelection = i;
511 if ((1 == n) && (i < _data._expandedItems.length - 1)) {
512 item._selected = false;
513 item = _data._expandedItems[i + 1];
514 item._selected = true;
515 changed = true;
516 } else if ((-1 == n) && (i > 0)) {
517 item._selected = false;
518 item = _data._expandedItems[i - 1];
519 item._selected = true;
520 changed = true;
521 }
522 break;
523 }
524 }
525
526 if (lastSelection < 0 && _data._expandedItems.length > 0) {
527 Item item = _data._expandedItems[0];
528 item._selected = true;
529 changed = true;
530 }
531
532 if (changed) {
533 ensureVisibleItem(-1, false);
534 redraw();
535 fireSelectionChanged();
536 }
537 }
538
539 /**
540 * Select an event
541 *
542 * @param n
543 * 1 for next event, -1 for previous event
544 */
545 public void selectEvent(int n) {
546 if (null == _timeProvider) {
547 return;
548 }
549 ITimeGraphEntry trace = getSelectedTrace();
550 if (trace == null) {
551 return;
552 }
553 long selectedTime = _timeProvider.getSelectedTime();
554 long endTime = _timeProvider.getEndTime();
555 ITimeEvent nextEvent;
556 if (-1 == n && selectedTime > endTime) {
557 nextEvent = Utils.findEvent(trace, selectedTime, 0);
558 } else {
559 nextEvent = Utils.findEvent(trace, selectedTime, n);
560 }
561 if (null == nextEvent && -1 == n) {
562 nextEvent = Utils.getFirstEvent(trace);
563 }
564 if (null != nextEvent) {
565 long nextTime = nextEvent.getTime();
566 // If last event detected e.g. going back or not moving to a next
567 // event
568 if (nextTime <= selectedTime && n == 1) {
569 // Select to the end of this last event
570 nextTime = nextEvent.getTime() + nextEvent.getDuration();
571 // but not beyond the end of the trace
572 if (nextTime > endTime) {
573 nextTime = endTime;
574 }
575 } else if (n == -1) {
576 // for previous event go to its end time unless we were already there
577 if (nextEvent.getTime() + nextEvent.getDuration() < selectedTime) {
578 nextTime = nextEvent.getTime() + nextEvent.getDuration();
579 }
580 }
581 _timeProvider.setSelectedTimeNotify(nextTime, true);
582 fireSelectionChanged();
583 } else if (1 == n) {
584 _timeProvider.setSelectedTimeNotify(endTime, true);
585 fireSelectionChanged();
586 }
587 }
588
589 /**
590 * Select the next event
591 */
592 public void selectNextEvent() {
593 selectEvent(1);
594 // Notify if visible time window has been adjusted
595 _timeProvider.setStartFinishTimeNotify(_timeProvider.getTime0(), _timeProvider.getTime1());
596 }
597
598 /**
599 * Select the previous event
600 */
601 public void selectPrevEvent() {
602 selectEvent(-1);
603 // Notify if visible time window has been adjusted
604 _timeProvider.setStartFinishTimeNotify(_timeProvider.getTime0(), _timeProvider.getTime1());
605 }
606
607 /**
608 * Select the next trace
609 */
610 public void selectNextTrace() {
611 selectTrace(1);
612 }
613
614 /**
615 * Select the previous trace
616 */
617 public void selectPrevTrace() {
618 selectTrace(-1);
619 }
620
621 /**
622 * Zoom based on mouse cursor location with mouse scrolling
623 *
624 * @param zoomIn true to zoom in, false to zoom out
625 */
626 public void zoom(boolean zoomIn) {
627 int globalX = getDisplay().getCursorLocation().x;
628 Point p = toControl(globalX, 0);
629 int nameSpace = _timeProvider.getNameSpace();
630 int timeSpace = _timeProvider.getTimeSpace();
631 int xPos = Math.max(nameSpace, Math.min(nameSpace + timeSpace, p.x));
632 long time0 = _timeProvider.getTime0();
633 long time1 = _timeProvider.getTime1();
634 long interval = time1 - time0;
635 if (interval == 0) {
636 interval = 1;
637 } // to allow getting out of single point interval
638 long newInterval;
639 if (zoomIn) {
640 newInterval = Math.max(Math.round(interval * 0.8), _timeProvider.getMinTimeInterval());
641 } else {
642 newInterval = (long) Math.ceil(interval * 1.25);
643 }
644 long center = time0 + Math.round(((double) (xPos - nameSpace) / timeSpace * interval));
645 long newTime0 = center - Math.round((double) newInterval * (center - time0) / interval);
646 long newTime1 = newTime0 + newInterval;
647 _timeProvider.setStartFinishTime(newTime0, newTime1);
648 synchronized (fMouseScrollNotifierLock) {
649 if (fMouseScrollNotifier == null) {
650 fMouseScrollNotifier = new MouseScrollNotifier();
651 fMouseScrollNotifier.start();
652 }
653 fMouseScrollNotifier.mouseScrolled();
654 }
655 }
656
657 /**
658 * zoom in using single click
659 */
660 public void zoomIn() {
661 long _time0 = _timeProvider.getTime0();
662 long _time1 = _timeProvider.getTime1();
663 long _range = _time1 - _time0;
664 long selTime = _timeProvider.getSelectedTime();
665 if (selTime <= _time0 || selTime >= _time1) {
666 selTime = (_time0 + _time1) / 2;
667 }
668 long time0 = selTime - (long) ((selTime - _time0) / zoomCoeff);
669 long time1 = selTime + (long) ((_time1 - selTime) / zoomCoeff);
670
671 long inaccuracy = (_timeProvider.getMaxTime() - _timeProvider.getMinTime()) - (time1 - time0);
672
673 // Trace.debug("selTime:" + selTime + " time0:" + time0 + " time1:"
674 // + time1 + " inaccuracy:" + inaccuracy);
675
676 if (inaccuracy > 0 && inaccuracy < 100) {
677 _timeProvider.setStartFinishTimeNotify(_timeProvider.getMinTime(), _timeProvider.getMaxTime());
678 return;
679 }
680
681 long m = _timeProvider.getMinTimeInterval();
682 if ((time1 - time0) < m) {
683 time0 = selTime - (selTime - _time0) * m / _range;
684 time1 = time0 + m;
685 }
686
687 _timeProvider.setStartFinishTimeNotify(time0, time1);
688 }
689
690 /**
691 * zoom out using single click
692 */
693 public void zoomOut() {
694 long _time0 = _timeProvider.getTime0();
695 long _time1 = _timeProvider.getTime1();
696 long selTime = _timeProvider.getSelectedTime();
697 if (selTime <= _time0 || selTime >= _time1) {
698 selTime = (_time0 + _time1) / 2;
699 }
700 long time0 = (long) (selTime - (selTime - _time0) * zoomCoeff);
701 long time1 = (long) (selTime + (_time1 - selTime) * zoomCoeff);
702
703 long inaccuracy = (_timeProvider.getMaxTime() - _timeProvider.getMinTime()) - (time1 - time0);
704 if (inaccuracy > 0 && inaccuracy < 100) {
705 _timeProvider.setStartFinishTimeNotify(_timeProvider.getMinTime(), _timeProvider.getMaxTime());
706 return;
707 }
708
709 _timeProvider.setStartFinishTimeNotify(time0, time1);
710 }
711
712 /**
713 * Return the currently selected trace
714 *
715 * @return The entry matching the trace
716 */
717 public ITimeGraphEntry getSelectedTrace() {
718 ITimeGraphEntry trace = null;
719 int idx = getSelectedIndex();
720 if (idx >= 0) {
721 trace = _data._expandedItems[idx]._trace;
722 }
723 return trace;
724 }
725
726 /**
727 * Retrieve the index of the currently selected item
728 *
729 * @return The index
730 */
731 public int getSelectedIndex() {
732 int idx = -1;
733 for (int i = 0; i < _data._expandedItems.length; i++) {
734 Item item = _data._expandedItems[i];
735 if (item._selected) {
736 idx = i;
737 break;
738 }
739 }
740 return idx;
741 }
742
743 boolean toggle(int idx) {
744 boolean toggled = false;
745 if (idx >= 0 && idx < _data._expandedItems.length) {
746 Item item = _data._expandedItems[idx];
747 if (item._hasChildren) {
748 item._expanded = !item._expanded;
749 _data.updateExpandedItems();
750 adjustScrolls();
751 redraw();
752 toggled = true;
753 fireTreeEvent(item._trace, item._expanded);
754 }
755 }
756 return toggled;
757 }
758
759 int getItemIndexAtY(int y) {
760 if (y < 0) {
761 return -1;
762 }
763 if (_itemHeight == CUSTOM_ITEM_HEIGHT) {
764 int ySum = 0;
765 for (int idx = _topIndex; idx < _data._expandedItems.length; idx++) {
766 ySum += _data._expandedItems[idx].itemHeight;
767 if (y < ySum) {
768 return idx;
769 }
770 }
771 return -1;
772 }
773 int idx = y / _itemHeight;
774 idx += _topIndex;
775 if (idx < _data._expandedItems.length) {
776 return idx;
777 }
778 return -1;
779 }
780
781 boolean isOverSplitLine(int x) {
782 if (x < 0 || null == _timeProvider) {
783 return false;
784 }
785 int w = 4;
786 int nameWidth = _timeProvider.getNameSpace();
787 if (x > nameWidth - w && x < nameWidth + w) {
788 return true;
789 }
790 return false;
791 }
792
793 ITimeGraphEntry getEntry(Point pt) {
794 int idx = getItemIndexAtY(pt.y);
795 return idx >= 0 ? _data._expandedItems[idx]._trace : null;
796 }
797
798 long getTimeAtX(int x) {
799 if (null == _timeProvider) {
800 return -1;
801 }
802 long hitTime = -1;
803 Point size = getCtrlSize();
804 long time0 = _timeProvider.getTime0();
805 long time1 = _timeProvider.getTime1();
806 int nameWidth = _timeProvider.getNameSpace();
807 x -= nameWidth;
808 int timeWidth = size.x - nameWidth - RIGHT_MARGIN;
809 if (x >= 0 && size.x >= nameWidth) {
810 if (time1 - time0 > timeWidth) {
811 // nanosecond smaller than one pixel: use the first integer nanosecond of this pixel's time range
812 hitTime = time0 + (long) Math.ceil((time1 - time0) * ((double) x / timeWidth));
813 } else {
814 // nanosecond greater than one pixel: use the nanosecond that covers this pixel start position
815 hitTime = time0 + (long) Math.floor((time1 - time0) * ((double) x / timeWidth));
816 }
817 }
818 return hitTime;
819 }
820
821 void selectItem(int idx, boolean addSelection) {
822 boolean changed = false;
823 if (addSelection) {
824 if (idx >= 0 && idx < _data._expandedItems.length) {
825 Item item = _data._expandedItems[idx];
826 changed = (item._selected == false);
827 item._selected = true;
828 }
829 } else {
830 for (int i = 0; i < _data._expandedItems.length; i++) {
831 Item item = _data._expandedItems[i];
832 if ((i == idx && !item._selected) || (idx == -1 && item._selected)) {
833 changed = true;
834 }
835 item._selected = i == idx;
836 }
837 }
838 changed |= ensureVisibleItem(idx, true);
839 if (changed) {
840 redraw();
841 }
842 }
843
844 /**
845 * Callback for item selection
846 *
847 * @param trace
848 * The entry matching the trace
849 * @param addSelection
850 * If the selection is added or removed
851 */
852 public void selectItem(ITimeGraphEntry trace, boolean addSelection) {
853 int idx = _data.findItemIndex(trace);
854 selectItem(idx, addSelection);
855 }
856
857 /**
858 * Retrieve the number of entries shown per page.
859 *
860 * @return The count
861 */
862 public int countPerPage() {
863 int height = getCtrlSize().y;
864 int count = 0;
865 if (_itemHeight == CUSTOM_ITEM_HEIGHT) {
866 int ySum = 0;
867 for (int idx = _topIndex; idx < _data._expandedItems.length; idx++) {
868 ySum += _data._expandedItems[idx].itemHeight;
869 if (ySum >= height) {
870 return count;
871 }
872 count++;
873 }
874 for (int idx = _topIndex - 1; idx >= 0; idx--) {
875 ySum += _data._expandedItems[idx].itemHeight;
876 if (ySum >= height) {
877 return count;
878 }
879 count++;
880 }
881 return count;
882 }
883 if (height > 0) {
884 count = height / _itemHeight;
885 }
886 return count;
887 }
888
889 /**
890 * Get the index of the top element
891 *
892 * @return The index
893 */
894 public int getTopIndex() {
895 return _topIndex;
896 }
897
898 /**
899 * Get the number of expanded items
900 *
901 * @return The count of expanded items
902 */
903 public int getExpandedElementCount() {
904 return _data._expandedItems.length;
905 }
906
907 /**
908 * Get an array of all expanded elements
909 *
910 * @return The expanded elements
911 */
912 public ITimeGraphEntry[] getExpandedElements() {
913 ArrayList<ITimeGraphEntry> elements = new ArrayList<ITimeGraphEntry>();
914 for (Item item : _data._expandedItems) {
915 elements.add(item._trace);
916 }
917 return elements.toArray(new ITimeGraphEntry[0]);
918 }
919
920 Point getCtrlSize() {
921 Point size = getSize();
922 if (getHorizontalBar().isVisible()) {
923 size.y -= getHorizontalBar().getSize().y;
924 }
925 return size;
926 }
927
928 Rectangle getNameRect(Rectangle bound, int idx, int nameWidth) {
929 int x = bound.x;
930 int y = bound.y + (idx - _topIndex) * _itemHeight;
931 int width = nameWidth;
932 int height = _itemHeight;
933 if (_itemHeight == CUSTOM_ITEM_HEIGHT) {
934 int ySum = 0;
935 for (int i = _topIndex; i < idx; i++) {
936 ySum += _data._expandedItems[i].itemHeight;
937 }
938 y = bound.y + ySum;
939 height = _data._expandedItems[idx].itemHeight;
940 }
941 return new Rectangle(x, y, width, height);
942 }
943
944 Rectangle getStatesRect(Rectangle bound, int idx, int nameWidth) {
945 int x = bound.x + nameWidth;
946 int y = bound.y + (idx - _topIndex) * _itemHeight;
947 int width = bound.width - x;
948 int height = _itemHeight;
949 if (_itemHeight == CUSTOM_ITEM_HEIGHT) {
950 int ySum = 0;
951 for (int i = _topIndex; i < idx; i++) {
952 ySum += _data._expandedItems[i].itemHeight;
953 }
954 y = bound.y + ySum;
955 height = _data._expandedItems[idx].itemHeight;
956 }
957 return new Rectangle(x, y, width, height);
958 }
959
960 @Override
961 void paint(Rectangle bounds, PaintEvent e) {
962 GC gc = e.gc;
963 gc.setBackground(_colors.getColor(TimeGraphColorScheme.BACKGROUND));
964 drawBackground(gc, bounds.x, bounds.y, bounds.width, bounds.height);
965
966 if (bounds.width < 2 || bounds.height < 2 || null == _timeProvider) {
967 return;
968 }
969
970 _idealNameSpace = 0;
971 int nameSpace = _timeProvider.getNameSpace();
972
973 // draw empty name space background
974 gc.setBackground(_colors.getBkColor(false, false, true));
975 drawBackground(gc, bounds.x, bounds.y, nameSpace, bounds.height);
976
977 drawItems(bounds, _timeProvider, _data._expandedItems, _topIndex, nameSpace, gc);
978
979 // draw selected time
980 long time0 = _timeProvider.getTime0();
981 long time1 = _timeProvider.getTime1();
982 long selectedTime = _timeProvider.getSelectedTime();
983 double pixelsPerNanoSec = (bounds.width - nameSpace <= RIGHT_MARGIN) ? 0 : (double) (bounds.width - nameSpace - RIGHT_MARGIN) / (time1 - time0);
984 int x = bounds.x + nameSpace + (int) ((selectedTime - time0) * pixelsPerNanoSec);
985 if (x >= nameSpace && x < bounds.x + bounds.width) {
986 gc.setForeground(_colors.getColor(TimeGraphColorScheme.SELECTED_TIME));
987 gc.drawLine(x, bounds.y, x, bounds.y + bounds.height);
988 }
989
990 // draw drag line, no line if name space is 0.
991 if (DRAG_SPLIT_LINE == _dragState) {
992 gc.setForeground(_colors.getColor(TimeGraphColorScheme.BLACK));
993 gc.drawLine(bounds.x + nameSpace, bounds.y, bounds.x + nameSpace, bounds.y + bounds.height - 1);
994 } else if (DRAG_NONE == _dragState && _mouseOverSplitLine && _timeProvider.getNameSpace() > 0) {
995 gc.setForeground(_colors.getColor(TimeGraphColorScheme.RED));
996 gc.drawLine(bounds.x + nameSpace, bounds.y, bounds.x + nameSpace, bounds.y + bounds.height - 1);
997 }
998 }
999
1000 /**
1001 * Draw many items at once
1002 *
1003 * @param bounds
1004 * The rectangle of the area
1005 * @param timeProvider
1006 * The time provider
1007 * @param items
1008 * The array items to draw
1009 * @param topIndex
1010 * The index of the first element to draw
1011 * @param nameSpace
1012 * The width reserved for the names
1013 * @param gc
1014 * Reference to the SWT GC object
1015 */
1016 public void drawItems(Rectangle bounds, ITimeDataProvider timeProvider,
1017 Item[] items, int topIndex, int nameSpace, GC gc) {
1018 for (int i = topIndex; i < items.length; i++) {
1019 Item item = items[i];
1020 drawItem(item, bounds, timeProvider, i, nameSpace, gc);
1021 }
1022 fTimeGraphProvider.postDrawControl(bounds, gc);
1023 }
1024
1025 /**
1026 * Draws the item
1027 *
1028 * @param item the item to draw
1029 * @param bounds the container rectangle
1030 * @param i the item index
1031 * @param nameSpace the name space
1032 * @param gc
1033 */
1034 protected void drawItem(Item item, Rectangle bounds, ITimeDataProvider timeProvider, int i, int nameSpace, GC gc) {
1035 ITimeGraphEntry entry = item._trace;
1036 long time0 = timeProvider.getTime0();
1037 long time1 = timeProvider.getTime1();
1038 long selectedTime = timeProvider.getSelectedTime();
1039
1040 Rectangle nameRect = getNameRect(bounds, i, nameSpace);
1041 if (nameRect.y >= bounds.y + bounds.height) {
1042 return;
1043 }
1044
1045 if (! item._trace.hasTimeEvents()) {
1046 Rectangle statesRect = getStatesRect(bounds, i, nameSpace);
1047 nameRect.width += statesRect.width;
1048 drawName(item, nameRect, gc);
1049 } else {
1050 drawName(item, nameRect, gc);
1051 }
1052 Rectangle rect = getStatesRect(bounds, i, nameSpace);
1053 if (rect.isEmpty()) {
1054 fTimeGraphProvider.postDrawEntry(entry, rect, gc);
1055 return;
1056 }
1057 if (time1 <= time0) {
1058 gc.setBackground(_colors.getBkColor(false, false, false));
1059 gc.fillRectangle(rect);
1060 fTimeGraphProvider.postDrawEntry(entry, rect, gc);
1061 return;
1062 }
1063
1064 // Initialize _rect1 to same values as enclosing rectangle rect
1065 Rectangle stateRect = Utils.clone(rect);
1066 boolean selected = item._selected;
1067 // K pixels per second
1068 double pixelsPerNanoSec = (rect.width <= RIGHT_MARGIN) ? 0 : (double) (rect.width - RIGHT_MARGIN) / (time1 - time0);
1069
1070 if (item._trace.hasTimeEvents()) {
1071 fillSpace(rect, gc, selected);
1072 // Drawing rectangle is smaller than reserved space
1073 stateRect.y += 3;
1074 stateRect.height -= 6;
1075
1076 long maxDuration = (timeProvider.getTimeSpace() == 0) ? Long.MAX_VALUE : 1 * (time1 - time0) / timeProvider.getTimeSpace();
1077 Iterator<ITimeEvent> iterator = entry.getTimeEventsIterator(time0, time1, maxDuration);
1078
1079 int lastX = -1;
1080 while (iterator.hasNext()) {
1081 ITimeEvent event = iterator.next();
1082 int x = rect.x + (int) ((event.getTime() - time0) * pixelsPerNanoSec);
1083 int xEnd = rect.x + (int) ((event.getTime() + event.getDuration() - time0) * pixelsPerNanoSec);
1084 if (x >= rect.x + rect.width || xEnd < rect.x) {
1085 // event is out of bounds
1086 continue;
1087 }
1088 xEnd = Math.min(rect.x + rect.width, xEnd);
1089 stateRect.x = Math.max(rect.x, x);
1090 stateRect.width = Math.max(0, xEnd - stateRect.x + 1);
1091 if (stateRect.x == lastX) {
1092 stateRect.width -= 1;
1093 if (stateRect.width > 0) {
1094 gc.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK));
1095 gc.drawPoint(stateRect.x, stateRect.y - 2);
1096 stateRect.x += 1;
1097 }
1098 } else {
1099 lastX = x;
1100 }
1101 boolean timeSelected = selectedTime >= event.getTime() && selectedTime < event.getTime() + event.getDuration();
1102 drawState(_colors, event, stateRect, gc, selected, timeSelected);
1103 }
1104 }
1105 fTimeGraphProvider.postDrawEntry(entry, rect, gc);
1106 }
1107
1108 protected void drawName(Item item, Rectangle bounds, GC gc) {
1109 boolean hasTimeEvents = item._trace.hasTimeEvents();
1110 if (! hasTimeEvents) {
1111 gc.setBackground(_colors.getBkColorGroup(item._selected, _isInFocus));
1112 gc.fillRectangle(bounds);
1113 if (item._selected && _isInFocus) {
1114 gc.setForeground(_colors.getBkColor(item._selected, _isInFocus, false));
1115 gc.drawRectangle(bounds.x, bounds.y, bounds.width - 1, bounds.height - 1);
1116 }
1117 } else {
1118 gc.setBackground(_colors.getBkColor(item._selected, _isInFocus, true));
1119 gc.setForeground(_colors.getFgColor(item._selected, _isInFocus));
1120 gc.fillRectangle(bounds);
1121 }
1122
1123 // No name to be drawn
1124 if (_timeProvider.getNameSpace() == 0) {
1125 return;
1126 }
1127
1128 int leftMargin = MARGIN + item.level * EXPAND_SIZE;
1129 if (item._hasChildren) {
1130 gc.setForeground(_colors.getFgColorGroup(false, false));
1131 gc.setBackground(_colors.getBkColor(false, false, false));
1132 Rectangle rect = Utils.clone(bounds);
1133 rect.x += leftMargin;
1134 rect.y += (bounds.height - EXPAND_SIZE) / 2;
1135 rect.width = EXPAND_SIZE;
1136 rect.height = EXPAND_SIZE;
1137 gc.fillRectangle(rect);
1138 gc.drawRectangle(rect.x, rect.y, rect.width - 1, rect.height - 1);
1139 int midy = rect.y + rect.height / 2;
1140 gc.drawLine(rect.x + 2, midy, rect.x + rect.width - 3, midy);
1141 if (!item._expanded) {
1142 int midx = rect.x + rect.width / 2;
1143 gc.drawLine(midx, rect.y + 2, midx, rect.y + rect.height - 3);
1144 }
1145 }
1146 leftMargin += EXPAND_SIZE + MARGIN;
1147
1148 Image img = fTimeGraphProvider.getItemImage(item._trace);
1149 if (img != null) {
1150 // draw icon
1151 int imgHeight = img.getImageData().height;
1152 int imgWidth = img.getImageData().width;
1153 int x = leftMargin;
1154 int y = bounds.y + (bounds.height - imgHeight) / 2;
1155 gc.drawImage(img, x, y);
1156 leftMargin += imgWidth + MARGIN;
1157 }
1158 String name = item._name;
1159 Point size = gc.stringExtent(name);
1160 if (_idealNameSpace < leftMargin + size.x + MARGIN) {
1161 _idealNameSpace = leftMargin + size.x + MARGIN;
1162 }
1163 if (hasTimeEvents) {
1164 // cut long string with "..."
1165 int width = bounds.width - leftMargin;
1166 int cuts = 0;
1167 while (size.x > width && name.length() > 1) {
1168 cuts++;
1169 name = name.substring(0, name.length() - 1);
1170 size = gc.stringExtent(name + "..."); //$NON-NLS-1$
1171 }
1172 if (cuts > 0) {
1173 name += "..."; //$NON-NLS-1$
1174 }
1175 }
1176 Rectangle rect = Utils.clone(bounds);
1177 rect.x += leftMargin;
1178 rect.width -= leftMargin;
1179 // draw text
1180 if (rect.width > 0) {
1181 rect.y += (bounds.height - gc.stringExtent(name).y) / 2;
1182 gc.setForeground(_colors.getFgColor(item._selected, _isInFocus));
1183 int textWidth = Utils.drawText(gc, name, rect, true);
1184 leftMargin += textWidth + MARGIN;
1185 rect.y -= 2;
1186
1187 if (hasTimeEvents) {
1188 // draw middle line
1189 int x = bounds.x + leftMargin;
1190 int width = bounds.width - x;
1191 int midy = bounds.y + bounds.height / 2;
1192 gc.setForeground(_colors.getColor(TimeGraphColorScheme.MID_LINE));
1193 gc.drawLine(x, midy, x + width, midy);
1194 }
1195 }
1196 }
1197
1198 protected void drawState(TimeGraphColorScheme colors, ITimeEvent event,
1199 Rectangle rect, GC gc, boolean selected, boolean timeSelected) {
1200
1201 int colorIdx = fTimeGraphProvider.getStateTableIndex(event);
1202 if (colorIdx < 0) {
1203 return;
1204 }
1205 boolean visible = rect.width == 0 ? false : true;
1206
1207 if (visible) {
1208 Color stateColor = null;
1209 if (colorIdx < fEventColorMap.length) {
1210 stateColor = fEventColorMap[colorIdx];
1211 } else {
1212 stateColor = Display.getDefault().getSystemColor(SWT.COLOR_BLACK);
1213 }
1214
1215 timeSelected = timeSelected && selected;
1216 if (timeSelected) {
1217 // modify the color?
1218 }
1219 // fill all rect area
1220 gc.setBackground(stateColor);
1221 gc.fillRectangle(rect);
1222 // get the border color?
1223 gc.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK));
1224
1225 // draw bounds
1226 if (!timeSelected) {
1227 // Draw the top and bottom borders i.e. no side borders
1228 // top
1229 gc.drawLine(rect.x, rect.y, rect.x + rect.width - 1, rect.y);
1230 // bottom
1231 gc.drawLine(rect.x, rect.y + rect.height - 1, rect.x + rect.width - 1, rect.y + rect.height - 1);
1232 }
1233 } else {
1234 gc.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK));
1235 gc.drawPoint(rect.x, rect.y - 2);
1236 /*
1237 // selected rectangle area is not visible but can be represented
1238 // with a broken vertical line of specified width.
1239 int width = 1;
1240 rect.width = width;
1241 gc.setForeground(stateColor);
1242 int s = gc.getLineStyle();
1243 int w = gc.getLineWidth();
1244 gc.setLineStyle(SWT.LINE_DOT);
1245 gc.setLineWidth(width);
1246 // Trace.debug("Rectangle not visible, drawing vertical line with: "
1247 // + rect.x + "," + rect.y + "," + rect.x + "," + rect.y
1248 // + rect.height);
1249 gc.drawLine(rect.x, rect.y, rect.x, rect.y + rect.height - 1);
1250 gc.setLineStyle(s);
1251 gc.setLineWidth(w);
1252 if (!timeSelected) {
1253 gc.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK));
1254 gc.drawPoint(rect.x, rect.y);
1255 gc.drawPoint(rect.x, rect.y + rect.height - 1);
1256 }
1257 */
1258 }
1259 fTimeGraphProvider.postDrawEvent(event, rect, gc);
1260 }
1261
1262 protected void fillSpace(Rectangle rect, GC gc, boolean selected) {
1263 gc.setBackground(_colors.getBkColor(selected, _isInFocus, false));
1264 gc.fillRectangle(rect);
1265 // draw middle line
1266 gc.setForeground(_colors.getColor(TimeGraphColorScheme.MID_LINE));
1267 int midy = rect.y + rect.height / 2;
1268 gc.drawLine(rect.x, midy, rect.x + rect.width, midy);
1269 }
1270
1271 @Override
1272 public void keyTraversed(TraverseEvent e) {
1273 if ((e.detail == SWT.TRAVERSE_TAB_NEXT) || (e.detail == SWT.TRAVERSE_TAB_PREVIOUS)) {
1274 e.doit = true;
1275 }
1276 }
1277
1278 @Override
1279 public void keyPressed(KeyEvent e) {
1280 int idx = -1;
1281 if (_data._expandedItems.length == 0) {
1282 return;
1283 }
1284 if (SWT.HOME == e.keyCode) {
1285 idx = 0;
1286 } else if (SWT.END == e.keyCode) {
1287 idx = _data._expandedItems.length - 1;
1288 } else if (SWT.ARROW_DOWN == e.keyCode) {
1289 idx = getSelectedIndex();
1290 if (idx < 0) {
1291 idx = 0;
1292 } else if (idx < _data._expandedItems.length - 1) {
1293 idx++;
1294 }
1295 } else if (SWT.ARROW_UP == e.keyCode) {
1296 idx = getSelectedIndex();
1297 if (idx < 0) {
1298 idx = 0;
1299 } else if (idx > 0) {
1300 idx--;
1301 }
1302 } else if (SWT.ARROW_LEFT == e.keyCode) {
1303 selectPrevEvent();
1304 } else if (SWT.ARROW_RIGHT == e.keyCode) {
1305 selectNextEvent();
1306 } else if (SWT.PAGE_DOWN == e.keyCode) {
1307 int page = countPerPage();
1308 idx = getSelectedIndex();
1309 if (idx < 0) {
1310 idx = 0;
1311 }
1312 idx += page;
1313 if (idx >= _data._expandedItems.length) {
1314 idx = _data._expandedItems.length - 1;
1315 }
1316 } else if (SWT.PAGE_UP == e.keyCode) {
1317 int page = countPerPage();
1318 idx = getSelectedIndex();
1319 if (idx < 0) {
1320 idx = 0;
1321 }
1322 idx -= page;
1323 if (idx < 0) {
1324 idx = 0;
1325 }
1326 } else if (SWT.CR == e.keyCode) {
1327 idx = getSelectedIndex();
1328 if (idx >= 0) {
1329 if (_data._expandedItems[idx]._hasChildren) {
1330 toggle(idx);
1331 } else {
1332 fireDefaultSelection();
1333 }
1334 }
1335 idx = -1;
1336 }
1337 if (idx >= 0) {
1338 selectItem(idx, false);
1339 fireSelectionChanged();
1340 }
1341 }
1342
1343 @Override
1344 public void keyReleased(KeyEvent e) {
1345 }
1346
1347 @Override
1348 public void focusGained(FocusEvent e) {
1349 _isInFocus = true;
1350 if (mouseScrollFilterListener == null) {
1351 mouseScrollFilterListener = new Listener() {
1352 // This filter is used to prevent horizontal scrolling of the view
1353 // when the mouse wheel is used to zoom
1354 @Override
1355 public void handleEvent(Event event) {
1356 event.doit = false;
1357 }
1358 };
1359 getDisplay().addFilter(SWT.MouseWheel, mouseScrollFilterListener);
1360 }
1361 redraw();
1362 }
1363
1364 @Override
1365 public void focusLost(FocusEvent e) {
1366 _isInFocus = false;
1367 if (mouseScrollFilterListener != null) {
1368 getDisplay().removeFilter(SWT.MouseWheel, mouseScrollFilterListener);
1369 mouseScrollFilterListener = null;
1370 }
1371 if (DRAG_NONE != _dragState) {
1372 setCapture(false);
1373 _dragState = DRAG_NONE;
1374 }
1375 redraw();
1376 }
1377
1378 /**
1379 * @return If the current view is focused
1380 */
1381 public boolean isInFocus() {
1382 return _isInFocus;
1383 }
1384
1385 /**
1386 * Provide the possibility to control the wait cursor externally e.g. data
1387 * requests in progress
1388 *
1389 * @param waitInd Should we wait indefinitely?
1390 */
1391 public void waitCursor(boolean waitInd) {
1392 // Update cursor as indicated
1393 if (waitInd) {
1394 setCursor(_WaitCursor);
1395 _isWaitCursor = true;
1396 } else {
1397 setCursor(null);
1398 _isWaitCursor = false;
1399 }
1400
1401 // Get ready for next mouse move
1402 _isDragCursor3 = false;
1403 }
1404
1405 /**
1406 * <p>
1407 * If the x, y position is over the vertical split line (name to time
1408 * ranges), then change the cursor to a drag cursor to indicate the user the
1409 * possibility of resizing
1410 * </p>
1411 *
1412 * @param x
1413 * @param y
1414 */
1415 void updateCursor(int x, int y) {
1416 // if Wait cursor not active, check for the need to change to a drag
1417 // cursor
1418 if (_isWaitCursor == false) {
1419 boolean isSplitLine = isOverSplitLine(x);
1420 // No dragcursor is name space is fixed to zero
1421 if (isSplitLine && !_isDragCursor3 && _timeProvider.getNameSpace() > 0) {
1422 setCursor(_dragCursor3);
1423 _isDragCursor3 = true;
1424 } else if (!isSplitLine && _isDragCursor3) {
1425 setCursor(null);
1426 _isDragCursor3 = false;
1427 }
1428 }
1429 }
1430
1431 @Override
1432 public void mouseMove(MouseEvent e) {
1433 if (null == _timeProvider) {
1434 return;
1435 }
1436 Point size = getCtrlSize();
1437 if (DRAG_TRACE_ITEM == _dragState) {
1438 int nameWidth = _timeProvider.getNameSpace();
1439 int x = e.x - nameWidth;
1440 if (x > 0 && size.x > nameWidth && _dragX != x) {
1441 _dragX = x;
1442 double pixelsPerNanoSec = (size.x - nameWidth <= RIGHT_MARGIN) ? 0 : (double) (size.x - nameWidth - RIGHT_MARGIN) / (_time1bak - _time0bak);
1443 long timeDelta = (long) ((pixelsPerNanoSec == 0) ? 0 : ((_dragX - _dragX0) / pixelsPerNanoSec));
1444 long time1 = _time1bak - timeDelta;
1445 long maxTime = _timeProvider.getMaxTime();
1446 if (time1 > maxTime) {
1447 time1 = maxTime;
1448 }
1449 long time0 = time1 - (_time1bak - _time0bak);
1450 if (time0 < _timeProvider.getMinTime()) {
1451 time0 = _timeProvider.getMinTime();
1452 time1 = time0 + (_time1bak - _time0bak);
1453 }
1454 _timeProvider.setStartFinishTime(time0, time1);
1455 }
1456 } else if (DRAG_SPLIT_LINE == _dragState) {
1457 _dragX = e.x;
1458 _timeProvider.setNameSpace(e.x);
1459 } else if (DRAG_NONE == _dragState) {
1460 boolean mouseOverSplitLine = isOverSplitLine(e.x);
1461 if (_mouseOverSplitLine != mouseOverSplitLine) {
1462 redraw();
1463 }
1464 _mouseOverSplitLine = mouseOverSplitLine;
1465 }
1466 updateCursor(e.x, e.y);
1467 }
1468
1469 @Override
1470 public void mouseDoubleClick(MouseEvent e) {
1471 if (null == _timeProvider) {
1472 return;
1473 }
1474 if (1 == e.button) {
1475 if (isOverSplitLine(e.x) && _timeProvider.getNameSpace() != 0) {
1476 _timeProvider.setNameSpace(_idealNameSpace);
1477 boolean mouseOverSplitLine = isOverSplitLine(e.x);
1478 if (_mouseOverSplitLine != mouseOverSplitLine) {
1479 redraw();
1480 }
1481 _mouseOverSplitLine = mouseOverSplitLine;
1482 return;
1483 }
1484 int idx = getItemIndexAtY(e.y);
1485 if (idx >= 0) {
1486 selectItem(idx, false);
1487 fireDefaultSelection();
1488 }
1489 }
1490 }
1491
1492 @Override
1493 public void mouseDown(MouseEvent e) {
1494 if (null == _timeProvider) {
1495 return;
1496 }
1497 int idx;
1498 if (1 == e.button) {
1499 int nameSpace = _timeProvider.getNameSpace();
1500 if (nameSpace != 0) {
1501 if (isOverSplitLine(e.x)) {
1502 _dragState = DRAG_SPLIT_LINE;
1503 _dragX = _dragX0 = e.x;
1504 _time0bak = _timeProvider.getTime0();
1505 _time1bak = _timeProvider.getTime1();
1506 redraw();
1507 return;
1508 }
1509 }
1510
1511 idx = getItemIndexAtY(e.y);
1512 if (idx >= 0) {
1513 Item item = _data._expandedItems[idx];
1514 if (item._hasChildren && e.x < nameSpace && e.x < MARGIN + (item.level + 1) * EXPAND_SIZE) {
1515 toggle(idx);
1516 } else {
1517 long hitTime = getTimeAtX(e.x);
1518 if (hitTime >= 0) {
1519 // _timeProvider.setSelectedTimeInt(hitTime, false);
1520 setCapture(true);
1521 _dragState = DRAG_TRACE_ITEM;
1522 _dragX = _dragX0 = e.x - nameSpace;
1523 _time0bak = _timeProvider.getTime0();
1524 _time1bak = _timeProvider.getTime1();
1525 }
1526 }
1527 selectItem(idx, false);
1528 fireSelectionChanged();
1529 } else {
1530 selectItem(idx, false); // clear selection
1531 redraw();
1532 fireSelectionChanged();
1533 }
1534 }
1535 }
1536
1537 @Override
1538 public void mouseUp(MouseEvent e) {
1539 if (DRAG_NONE != _dragState) {
1540 setCapture(false);
1541 if (DRAG_TRACE_ITEM == _dragState) {
1542 // Notify time provider to check the need for listener
1543 // notification
1544 _timeProvider.notifyStartFinishTime();
1545 if (_dragX == _dragX0) { // click without drag
1546 long time = getTimeAtX(e.x);
1547 _timeProvider.setSelectedTimeNotify(time, false);
1548 }
1549 } else if (DRAG_SPLIT_LINE == _dragState) {
1550 redraw();
1551 }
1552 _dragState = DRAG_NONE;
1553 }
1554 }
1555
1556 @Override
1557 public void mouseEnter(MouseEvent e) {
1558 }
1559
1560 @Override
1561 public void mouseExit(MouseEvent e) {
1562 if (_mouseOverSplitLine) {
1563 _mouseOverSplitLine = false;
1564 redraw();
1565 }
1566 }
1567
1568 @Override
1569 public void mouseHover(MouseEvent e) {
1570 }
1571
1572 @Override
1573 public void mouseScrolled(MouseEvent e) {
1574 if ((mouseScrollFilterListener == null) || _dragState != DRAG_NONE) {
1575 return;
1576 }
1577 boolean zoomScroll = false;
1578 Point p = getParent().toControl(getDisplay().getCursorLocation());
1579 Point parentSize = getParent().getSize();
1580 if (p.x >= 0 && p.x < parentSize.x && p.y >= 0 && p.y < parentSize.y) {
1581 // over the parent control
1582 if (e.x > getCtrlSize().x) {
1583 // over the horizontal scroll bar
1584 zoomScroll = false;
1585 } else if (e.y >= 0 && e.y < getCtrlSize().y && e.x < _timeProvider.getNameSpace()) {
1586 // over the name space
1587 zoomScroll = false;
1588 } else {
1589 zoomScroll = true;
1590 }
1591 }
1592 if (zoomScroll && _timeProvider.getTime0() != _timeProvider.getTime1()) {
1593 if (e.count > 0) {
1594 zoom(true);
1595 } else if (e.count < 0) {
1596 zoom(false);
1597 }
1598 } else {
1599 setTopIndex(getTopIndex() - e.count);
1600 }
1601 }
1602
1603 @Override
1604 public void controlMoved(ControlEvent e) {
1605 }
1606
1607 @Override
1608 public void controlResized(ControlEvent e) {
1609 adjustScrolls();
1610 }
1611
1612 @Override
1613 public void widgetDefaultSelected(SelectionEvent e) {
1614 }
1615
1616 @Override
1617 public void widgetSelected(SelectionEvent e) {
1618 if (e.widget == getVerticalBar()) {
1619 setTopIndex(getVerticalBar().getSelection());
1620 } else if (e.widget == getHorizontalBar() && null != _timeProvider) {
1621 int start = getHorizontalBar().getSelection();
1622 long time0 = _timeProvider.getTime0();
1623 long time1 = _timeProvider.getTime1();
1624 long timeMin = _timeProvider.getMinTime();
1625 long timeMax = _timeProvider.getMaxTime();
1626 long delta = timeMax - timeMin;
1627
1628 long range = time1 - time0;
1629 // _timeRangeFixed = true;
1630 time0 = timeMin + Math.round(delta * ((double) start / H_SCROLLBAR_MAX));
1631 time1 = time0 + range;
1632
1633 // TODO: Follow-up with Bug 310310
1634 // In Linux SWT.DRAG is the only value received
1635 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=310310
1636 if (e.detail == SWT.DRAG) {
1637 _timeProvider.setStartFinishTime(time0, time1);
1638 } else {
1639 _timeProvider.setStartFinishTimeNotify(time0, time1);
1640 }
1641 }
1642 }
1643
1644 /**
1645 * @return The current visibility of the vertical scroll bar
1646 */
1647 public boolean isVisibleVerticalScroll() {
1648 return _visibleVerticalScroll;
1649 }
1650
1651 @Override
1652 public int getBorderWidth() {
1653 return _borderWidth;
1654 }
1655
1656 /**
1657 * Set the border width
1658 *
1659 * @param borderWidth
1660 * The width
1661 */
1662 public void setBorderWidth(int borderWidth) {
1663 this._borderWidth = borderWidth;
1664 }
1665
1666 /**
1667 * @return The current height of the header row
1668 */
1669 public int getHeaderHeight() {
1670 return _headerHeight;
1671 }
1672
1673 /**
1674 * Set the height of the header row
1675 *
1676 * @param headerHeight
1677 * The height
1678 */
1679 public void setHeaderHeight(int headerHeight) {
1680 this._headerHeight = headerHeight;
1681 }
1682
1683 /**
1684 * @return The height of regular item rows
1685 */
1686 public int getItemHeight() {
1687 return _itemHeight;
1688 }
1689
1690 /**
1691 * Set the height of regular itew rows
1692 *
1693 * @param rowHeight
1694 * The height
1695 */
1696 public void setItemHeight(int rowHeight) {
1697 this._itemHeight = rowHeight;
1698 }
1699
1700 /**
1701 * Set the minimum item width
1702 *
1703 * @param width The minimum width
1704 */
1705 public void setMinimumItemWidth(int width) {
1706 this._minimumItemWidth = width;
1707 }
1708
1709 /**
1710 * @return The minimum item width
1711 */
1712 public int getMinimumItemWidth() {
1713 return _minimumItemWidth;
1714 }
1715
1716 /**
1717 * @return The entries that are currently filtered out
1718 */
1719 public Vector<ITimeGraphEntry> getFilteredOut() {
1720 return _data.getFilteredOut();
1721 }
1722
1723 // @Override
1724 @Override
1725 public void addSelectionChangedListener(ISelectionChangedListener listener) {
1726 if (listener != null) {
1727 if (!_selectionChangedListeners.contains(listener)) {
1728 _selectionChangedListeners.add(listener);
1729 }
1730 }
1731 }
1732
1733 // @Override
1734 @Override
1735 public void removeSelectionChangedListener(ISelectionChangedListener listener) {
1736 if (listener != null) {
1737 _selectionChangedListeners.remove(listener);
1738 }
1739 }
1740
1741 // @Override
1742 @Override
1743 public void setSelection(ISelection selection) {
1744 if (selection instanceof TimeGraphSelection) {
1745 TimeGraphSelection sel = (TimeGraphSelection) selection;
1746 Object ob = sel.getFirstElement();
1747 if (ob instanceof ITimeGraphEntry) {
1748 ITimeGraphEntry trace = (ITimeGraphEntry) ob;
1749 selectItem(trace, false);
1750 }
1751 }
1752
1753 }
1754
1755 private class ItemData {
1756 public Item[] _expandedItems = new Item[0];
1757 public Item[] _items = new Item[0];
1758 private ITimeGraphEntry _traces[] = new ITimeGraphEntry[0];
1759 private boolean traceFilter[] = new boolean[0];
1760 private final Vector<ITimeGraphEntry> filteredOut = new Vector<ITimeGraphEntry>();
1761 public ITimeGraphPresentationProvider provider;
1762
1763 public ItemData() {
1764 }
1765
1766 Item findItem(ITimeGraphEntry entry) {
1767 if (entry == null) {
1768 return null;
1769 }
1770
1771 for (int i = 0; i < _items.length; i++) {
1772 Item item = _items[i];
1773 if (item._trace == entry) {
1774 return item;
1775 }
1776 }
1777
1778 return null;
1779 }
1780
1781 int findItemIndex(ITimeGraphEntry trace) {
1782 if (trace == null) {
1783 return -1;
1784 }
1785
1786 for (int i = 0; i < _expandedItems.length; i++) {
1787 Item item = _expandedItems[i];
1788 if (item._trace == trace) {
1789 return i;
1790 }
1791 }
1792
1793 return -1;
1794 }
1795
1796 public void refreshData() {
1797 List<Item> itemList = new ArrayList<Item>();
1798 filteredOut.clear();
1799 for (int i = 0; i < _traces.length; i++) {
1800 ITimeGraphEntry entry = _traces[i];
1801 refreshData(itemList, null, 0, entry);
1802 }
1803 _items = itemList.toArray(new Item[0]);
1804 updateExpandedItems();
1805 }
1806
1807 private void refreshData(List<Item> itemList, Item parent, int level, ITimeGraphEntry entry) {
1808 Item item = new Item(entry, entry.getName(), level);
1809 if (parent != null) {
1810 parent.children.add(item);
1811 }
1812 item.itemHeight = provider.getItemHeight(entry);
1813 itemList.add(item);
1814 if (entry.hasChildren()) {
1815 item._expanded = true;
1816 item._hasChildren = true;
1817 for (ITimeGraphEntry child : entry.getChildren()) {
1818 refreshData(itemList, item, level + 1, child);
1819 }
1820 }
1821 }
1822
1823 public void updateExpandedItems() {
1824 List<Item> expandedItemList = new ArrayList<Item>();
1825 for (int i = 0; i < _traces.length; i++) {
1826 ITimeGraphEntry entry = _traces[i];
1827 Item item = findItem(entry);
1828 refreshExpanded(expandedItemList, item);
1829 }
1830 _expandedItems = expandedItemList.toArray(new Item[0]);
1831 }
1832
1833 private void refreshExpanded(List<Item> expandedItemList, Item item) {
1834 expandedItemList.add(item);
1835 if (item._hasChildren && item._expanded) {
1836 for (Item child : item.children) {
1837 refreshExpanded(expandedItemList, child);
1838 }
1839 }
1840 }
1841
1842 public void refreshData(ITimeGraphEntry traces[]) {
1843 if (traces == null || traces.length == 0) {
1844 traceFilter = null;
1845 } else if (traceFilter == null || traces.length != traceFilter.length) {
1846 traceFilter = new boolean[traces.length];
1847 java.util.Arrays.fill(traceFilter, true);
1848 }
1849
1850 _traces = traces;
1851 refreshData();
1852 }
1853
1854 public ITimeGraphEntry[] getTraces() {
1855 return _traces;
1856 }
1857
1858 public boolean[] getTraceFilter() {
1859 return traceFilter;
1860 }
1861
1862 public Vector<ITimeGraphEntry> getFilteredOut() {
1863 return filteredOut;
1864 }
1865 }
1866
1867 private class Item {
1868 public boolean _expanded;
1869 public boolean _selected;
1870 public boolean _hasChildren;
1871 public int itemHeight;
1872 public int level;
1873 public List<Item> children;
1874 public String _name;
1875 public ITimeGraphEntry _trace;
1876
1877 public Item(ITimeGraphEntry trace, String name, int level) {
1878 this._trace = trace;
1879 this._name = name;
1880 this.level = level;
1881 this.children = new ArrayList<Item>();
1882 }
1883
1884 @Override
1885 public String toString() {
1886 return _name;
1887 }
1888 }
1889
1890 }
1891
This page took 0.074614 seconds and 6 git commands to generate.