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