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