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