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