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