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