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