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