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