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