Commit | Line | Data |
---|---|---|
837a2f8c | 1 | /***************************************************************************** |
8910dea2 | 2 | * Copyright (c) 2007, 2016 Intel Corporation and others |
4999a196 | 3 | * |
837a2f8c PT |
4 | * All rights reserved. This program and the accompanying materials |
5 | * are made available under the terms of the Eclipse Public License v1.0 | |
6 | * which accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * Intel Corporation - Initial API and implementation | |
11 | * Ruslan A. Scherbakov, Intel - Initial API and implementation | |
4999a196 GB |
12 | * Alvaro Sanchez-Leon, Ericsson - Updated for TMF |
13 | * Patrick Tasse, Ericsson - Refactoring | |
14 | * Geneviève Bastien, École Polytechnique de Montréal - Move code to | |
15 | * provide base classes for time graph view | |
bec1f1ac | 16 | * Add display of links between items |
351a2391 | 17 | * Xavier Raynaud, Kalray - Code optimization |
f2ca0f69 | 18 | * Generoso Pagano, Inria - Support for drag selection listeners |
837a2f8c PT |
19 | *****************************************************************************/ |
20 | ||
2bdf0193 | 21 | package org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets; |
837a2f8c | 22 | |
48072ae3 | 23 | import java.util.AbstractMap.SimpleEntry; |
837a2f8c | 24 | import java.util.ArrayList; |
f1fae91f | 25 | import java.util.Arrays; |
2fe6a9ea | 26 | import java.util.Collections; |
83d0971d | 27 | import java.util.HashMap; |
837a2f8c | 28 | import java.util.Iterator; |
70e10acc | 29 | import java.util.LinkedHashMap; |
837a2f8c | 30 | import java.util.List; |
70e10acc | 31 | import java.util.Map; |
48072ae3 | 32 | import java.util.Map.Entry; |
837a2f8c | 33 | |
367e2932 | 34 | import org.eclipse.jdt.annotation.NonNull; |
0fcf3b09 | 35 | import org.eclipse.jface.action.IStatusLineManager; |
837a2f8c PT |
36 | import org.eclipse.jface.resource.JFaceResources; |
37 | import org.eclipse.jface.resource.LocalResourceManager; | |
f4617471 | 38 | import org.eclipse.jface.viewers.AbstractTreeViewer; |
837a2f8c PT |
39 | import org.eclipse.jface.viewers.ISelection; |
40 | import org.eclipse.jface.viewers.ISelectionChangedListener; | |
41 | import org.eclipse.jface.viewers.ISelectionProvider; | |
6ac5a950 | 42 | import org.eclipse.jface.viewers.ViewerFilter; |
0fab12b0 | 43 | import org.eclipse.osgi.util.NLS; |
837a2f8c | 44 | import org.eclipse.swt.SWT; |
837a2f8c PT |
45 | import org.eclipse.swt.events.FocusEvent; |
46 | import org.eclipse.swt.events.FocusListener; | |
47 | import org.eclipse.swt.events.KeyEvent; | |
48 | import org.eclipse.swt.events.KeyListener; | |
27df1564 XR |
49 | import org.eclipse.swt.events.MenuDetectEvent; |
50 | import org.eclipse.swt.events.MenuDetectListener; | |
837a2f8c PT |
51 | import org.eclipse.swt.events.MouseEvent; |
52 | import org.eclipse.swt.events.MouseListener; | |
53 | import org.eclipse.swt.events.MouseMoveListener; | |
54 | import org.eclipse.swt.events.MouseTrackListener; | |
55 | import org.eclipse.swt.events.MouseWheelListener; | |
56 | import org.eclipse.swt.events.PaintEvent; | |
837a2f8c PT |
57 | import org.eclipse.swt.events.SelectionListener; |
58 | import org.eclipse.swt.events.TraverseEvent; | |
59 | import org.eclipse.swt.events.TraverseListener; | |
27df1564 | 60 | import org.eclipse.swt.events.TypedEvent; |
837a2f8c PT |
61 | import org.eclipse.swt.graphics.Color; |
62 | import org.eclipse.swt.graphics.Cursor; | |
3bd20aa6 PT |
63 | import org.eclipse.swt.graphics.Font; |
64 | import org.eclipse.swt.graphics.FontData; | |
837a2f8c PT |
65 | import org.eclipse.swt.graphics.GC; |
66 | import org.eclipse.swt.graphics.Image; | |
67 | import org.eclipse.swt.graphics.Point; | |
68 | import org.eclipse.swt.graphics.Rectangle; | |
69 | import org.eclipse.swt.widgets.Composite; | |
70 | import org.eclipse.swt.widgets.Display; | |
71 | import org.eclipse.swt.widgets.Event; | |
72 | import org.eclipse.swt.widgets.Listener; | |
d2e4afa7 MAL |
73 | import org.eclipse.tracecompass.tmf.core.signal.TmfSignalManager; |
74 | import org.eclipse.tracecompass.tmf.ui.signal.TmfTimeViewAlignmentInfo; | |
75 | import org.eclipse.tracecompass.tmf.ui.signal.TmfTimeViewAlignmentSignal; | |
76 | import org.eclipse.tracecompass.tmf.ui.views.ITmfTimeAligned; | |
2bdf0193 AM |
77 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.ITimeGraphColorListener; |
78 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.ITimeGraphPresentationProvider; | |
79 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.ITimeGraphPresentationProvider2; | |
80 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.ITimeGraphTimeListener; | |
81 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.ITimeGraphTreeListener; | |
82 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.StateItem; | |
83 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.TimeGraphTimeEvent; | |
84 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.TimeGraphTreeExpansionEvent; | |
85 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ILinkEvent; | |
56b43ab2 | 86 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.IMarkerEvent; |
2bdf0193 AM |
87 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeEvent; |
88 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeGraphEntry; | |
89 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.Utils.Resolution; | |
90 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.Utils.TimeFormat; | |
837a2f8c | 91 | |
4923d7b9 PT |
92 | import com.google.common.collect.Iterables; |
93 | ||
837a2f8c PT |
94 | /** |
95 | * Time graph control implementation | |
96 | * | |
837a2f8c PT |
97 | * @author Alvaro Sanchez-Leon |
98 | * @author Patrick Tasse | |
99 | */ | |
496f76d3 | 100 | public class TimeGraphControl extends TimeGraphBaseControl |
b698ec63 PT |
101 | implements FocusListener, KeyListener, MouseMoveListener, MouseListener, |
102 | MouseWheelListener, MouseTrackListener, TraverseListener, ISelectionProvider, | |
6b11be52 | 103 | MenuDetectListener, ITmfTimeGraphDrawingHelper, ITimeGraphColorListener, Listener { |
f1fae91f | 104 | |
ae09c4ad | 105 | /** Constant indicating that all levels of the time graph should be expanded */ |
f4617471 PT |
106 | public static final int ALL_LEVELS = AbstractTreeViewer.ALL_LEVELS; |
107 | ||
837a2f8c PT |
108 | private static final int DRAG_NONE = 0; |
109 | private static final int DRAG_TRACE_ITEM = 1; | |
110 | private static final int DRAG_SPLIT_LINE = 2; | |
5b2b9bd7 | 111 | private static final int DRAG_ZOOM = 3; |
0fcf3b09 | 112 | private static final int DRAG_SELECTION = 4; |
a0a88f65 | 113 | |
837a2f8c PT |
114 | private static final int CUSTOM_ITEM_HEIGHT = -1; // get item height from provider |
115 | ||
f1fae91f PT |
116 | private static final double ZOOM_FACTOR = 1.5; |
117 | private static final double ZOOM_IN_FACTOR = 0.8; | |
118 | private static final double ZOOM_OUT_FACTOR = 1.25; | |
119 | ||
0fcf3b09 | 120 | private static final int SNAP_WIDTH = 2; |
1053eaa6 | 121 | private static final int ARROW_HOVER_MAX_DIST = 5; |
0fcf3b09 PT |
122 | |
123 | private static final int NO_STATUS = -1; | |
33fa1fc7 | 124 | private static final int STATUS_WITHOUT_CURSOR_TIME = -2; |
0fcf3b09 | 125 | |
1d012443 PT |
126 | private static final int MAX_LABEL_LENGTH = 256; |
127 | ||
83d0971d PT |
128 | private static final int PPI = 72; // points per inch |
129 | private static final int DPI = Display.getDefault().getDPI().y; | |
130 | ||
48072ae3 PT |
131 | private static final int VERTICAL_ZOOM_DELAY = 400; |
132 | ||
f1fae91f PT |
133 | /** Resource manager */ |
134 | private LocalResourceManager fResourceManager = new LocalResourceManager(JFaceResources.getResources()); | |
135 | ||
136 | /** Color map for event types */ | |
137 | private Color[] fEventColorMap = null; | |
138 | ||
139 | private ITimeDataProvider fTimeProvider; | |
0fcf3b09 PT |
140 | private IStatusLineManager fStatusLineManager = null; |
141 | private TimeGraphScale fTimeGraphScale = null; | |
142 | ||
f1fae91f | 143 | private boolean fIsInFocus = false; |
f1fae91f PT |
144 | private boolean fMouseOverSplitLine = false; |
145 | private int fGlobalItemHeight = CUSTOM_ITEM_HEIGHT; | |
3bd20aa6 | 146 | private int fHeightAdjustment = 0; |
83d0971d | 147 | private Map<Integer, Font> fFonts = new HashMap<>(); |
b052914f | 148 | private boolean fBlendSubPixelEvents = false; |
f1fae91f PT |
149 | private int fMinimumItemWidth = 0; |
150 | private int fTopIndex = 0; | |
151 | private int fDragState = DRAG_NONE; | |
85203d74 | 152 | private boolean fDragBeginMarker = false; |
0fcf3b09 | 153 | private int fDragButton; |
f1fae91f PT |
154 | private int fDragX0 = 0; |
155 | private int fDragX = 0; | |
0fcf3b09 | 156 | private long fDragTime0 = 0; // used to preserve accuracy of modified selection |
f1fae91f PT |
157 | private int fIdealNameSpace = 0; |
158 | private long fTime0bak; | |
159 | private long fTime1bak; | |
837a2f8c | 160 | private ITimeGraphPresentationProvider fTimeGraphProvider = null; |
f1fae91f | 161 | private ItemData fItemData = null; |
56b43ab2 PT |
162 | private List<IMarkerEvent> fMarkers = null; |
163 | private boolean fMarkersVisible = true; | |
f1fae91f | 164 | private List<SelectionListener> fSelectionListeners; |
f2ca0f69 | 165 | private List<ITimeGraphTimeListener> fDragSelectionListeners; |
507b1336 AM |
166 | private final List<ISelectionChangedListener> fSelectionChangedListeners = new ArrayList<>(); |
167 | private final List<ITimeGraphTreeListener> fTreeListeners = new ArrayList<>(); | |
168 | private final List<MenuDetectListener> fTimeGraphEntryMenuListeners = new ArrayList<>(); | |
169 | private final List<MenuDetectListener> fTimeEventMenuListeners = new ArrayList<>(); | |
0fcf3b09 PT |
170 | private final Cursor fDragCursor = Display.getDefault().getSystemCursor(SWT.CURSOR_HAND); |
171 | private final Cursor fResizeCursor = Display.getDefault().getSystemCursor(SWT.CURSOR_IBEAM); | |
172 | private final Cursor fWaitCursor = Display.getDefault().getSystemCursor(SWT.CURSOR_WAIT); | |
173 | private final Cursor fZoomCursor = Display.getDefault().getSystemCursor(SWT.CURSOR_SIZEWE); | |
367e2932 | 174 | private final List<@NonNull ViewerFilter> fFilters = new ArrayList<>(); |
0b5a90a0 | 175 | private MenuDetectEvent fPendingMenuDetectEvent = null; |
55d1fc96 PT |
176 | private boolean fGridLinesVisible = true; |
177 | private Color fGridLineColor = Display.getDefault().getSystemColor(SWT.COLOR_GRAY); | |
79ec0b89 | 178 | private boolean fHideArrows = false; |
f4617471 | 179 | private int fAutoExpandLevel = ALL_LEVELS; |
48072ae3 PT |
180 | private Entry<ITimeGraphEntry, Integer> fVerticalZoomAlignEntry = null; |
181 | private long fVerticalZoomAlignTime = 0; | |
f1fae91f PT |
182 | private int fBorderWidth = 0; |
183 | private int fHeaderHeight = 0; | |
837a2f8c | 184 | |
837a2f8c PT |
185 | /** |
186 | * Standard constructor | |
187 | * | |
188 | * @param parent | |
189 | * The parent composite object | |
190 | * @param colors | |
191 | * The color scheme to use | |
192 | */ | |
193 | public TimeGraphControl(Composite parent, TimeGraphColorScheme colors) { | |
194 | ||
b698ec63 | 195 | super(parent, colors, SWT.NO_BACKGROUND | SWT.DOUBLE_BUFFERED); |
837a2f8c | 196 | |
f1fae91f | 197 | fItemData = new ItemData(); |
837a2f8c PT |
198 | |
199 | addFocusListener(this); | |
200 | addMouseListener(this); | |
201 | addMouseMoveListener(this); | |
202 | addMouseTrackListener(this); | |
203 | addMouseWheelListener(this); | |
204 | addTraverseListener(this); | |
205 | addKeyListener(this); | |
27df1564 | 206 | addMenuDetectListener(this); |
6b11be52 | 207 | addListener(SWT.MouseWheel, this); |
837a2f8c PT |
208 | } |
209 | ||
210 | @Override | |
211 | public void dispose() { | |
212 | super.dispose(); | |
837a2f8c | 213 | fResourceManager.dispose(); |
83d0971d PT |
214 | for (Font font : fFonts.values()) { |
215 | font.dispose(); | |
3bd20aa6 | 216 | } |
837a2f8c PT |
217 | } |
218 | ||
219 | /** | |
220 | * Sets the timegraph provider used by this timegraph viewer. | |
221 | * | |
222 | * @param timeGraphProvider the timegraph provider | |
223 | */ | |
224 | public void setTimeGraphProvider(ITimeGraphPresentationProvider timeGraphProvider) { | |
225 | fTimeGraphProvider = timeGraphProvider; | |
837a2f8c | 226 | |
4999a196 GB |
227 | if (timeGraphProvider instanceof ITimeGraphPresentationProvider2) { |
228 | ((ITimeGraphPresentationProvider2) timeGraphProvider).setDrawingHelper(this); | |
496f76d3 | 229 | ((ITimeGraphPresentationProvider2) timeGraphProvider).addColorListener(this); |
4999a196 GB |
230 | } |
231 | ||
837a2f8c | 232 | StateItem[] stateItems = fTimeGraphProvider.getStateTable(); |
496f76d3 | 233 | colorSettingsChanged(stateItems); |
837a2f8c PT |
234 | } |
235 | ||
5a66cf9c XR |
236 | /** |
237 | * Gets the timegraph provider used by this timegraph viewer. | |
238 | * | |
239 | * @return the timegraph provider, or <code>null</code> if not set. | |
5a66cf9c XR |
240 | */ |
241 | public ITimeGraphPresentationProvider getTimeGraphProvider() { | |
242 | return fTimeGraphProvider; | |
243 | } | |
244 | ||
245 | /** | |
246 | * Gets the color map used by this timegraph viewer. | |
247 | * | |
248 | * @return a color map, or <code>null</code> if not set. | |
5a66cf9c XR |
249 | */ |
250 | public Color[] getEventColorMap() { | |
251 | return fEventColorMap; | |
252 | } | |
253 | ||
837a2f8c PT |
254 | /** |
255 | * Assign the given time provider | |
256 | * | |
257 | * @param timeProvider | |
258 | * The time provider | |
259 | */ | |
260 | public void setTimeProvider(ITimeDataProvider timeProvider) { | |
f1fae91f | 261 | fTimeProvider = timeProvider; |
837a2f8c PT |
262 | redraw(); |
263 | } | |
264 | ||
0fcf3b09 PT |
265 | /** |
266 | * Assign the status line manager | |
267 | * | |
268 | * @param statusLineManager | |
269 | * The status line manager, or null to disable status line messages | |
0fcf3b09 PT |
270 | */ |
271 | public void setStatusLineManager(IStatusLineManager statusLineManager) { | |
272 | if (fStatusLineManager != null && statusLineManager == null) { | |
273 | fStatusLineManager.setMessage(""); //$NON-NLS-1$ | |
274 | } | |
275 | fStatusLineManager = statusLineManager; | |
276 | } | |
277 | ||
278 | /** | |
279 | * Assign the time graph scale | |
280 | * | |
281 | * @param timeGraphScale | |
282 | * The time graph scale | |
0fcf3b09 PT |
283 | */ |
284 | public void setTimeGraphScale(TimeGraphScale timeGraphScale) { | |
285 | fTimeGraphScale = timeGraphScale; | |
286 | } | |
287 | ||
837a2f8c PT |
288 | /** |
289 | * Add a selection listener | |
290 | * | |
291 | * @param listener | |
292 | * The listener to add | |
293 | */ | |
294 | public void addSelectionListener(SelectionListener listener) { | |
295 | if (listener == null) { | |
296 | SWT.error(SWT.ERROR_NULL_ARGUMENT); | |
297 | } | |
f1fae91f | 298 | if (null == fSelectionListeners) { |
507b1336 | 299 | fSelectionListeners = new ArrayList<>(); |
837a2f8c | 300 | } |
f1fae91f | 301 | fSelectionListeners.add(listener); |
837a2f8c PT |
302 | } |
303 | ||
304 | /** | |
305 | * Remove a selection listener | |
306 | * | |
307 | * @param listener | |
308 | * The listener to remove | |
309 | */ | |
310 | public void removeSelectionListener(SelectionListener listener) { | |
f1fae91f PT |
311 | if (null != fSelectionListeners) { |
312 | fSelectionListeners.remove(listener); | |
837a2f8c PT |
313 | } |
314 | } | |
315 | ||
316 | /** | |
317 | * Selection changed callback | |
318 | */ | |
319 | public void fireSelectionChanged() { | |
f1fae91f PT |
320 | if (null != fSelectionListeners) { |
321 | Iterator<SelectionListener> it = fSelectionListeners.iterator(); | |
837a2f8c PT |
322 | while (it.hasNext()) { |
323 | SelectionListener listener = it.next(); | |
324 | listener.widgetSelected(null); | |
325 | } | |
326 | } | |
327 | } | |
328 | ||
329 | /** | |
330 | * Default selection callback | |
331 | */ | |
332 | public void fireDefaultSelection() { | |
f1fae91f PT |
333 | if (null != fSelectionListeners) { |
334 | Iterator<SelectionListener> it = fSelectionListeners.iterator(); | |
837a2f8c PT |
335 | while (it.hasNext()) { |
336 | SelectionListener listener = it.next(); | |
337 | listener.widgetDefaultSelected(null); | |
338 | } | |
339 | } | |
340 | } | |
341 | ||
f2ca0f69 GP |
342 | /** |
343 | * Add a drag selection listener | |
344 | * | |
345 | * @param listener | |
346 | * The listener to add | |
f2ca0f69 GP |
347 | */ |
348 | public void addDragSelectionListener(ITimeGraphTimeListener listener) { | |
349 | if (listener == null) { | |
350 | SWT.error(SWT.ERROR_NULL_ARGUMENT); | |
351 | } | |
352 | if (null == fDragSelectionListeners) { | |
353 | fDragSelectionListeners = new ArrayList<>(); | |
354 | } | |
355 | fDragSelectionListeners.add(listener); | |
356 | } | |
357 | ||
358 | /** | |
359 | * Remove a drag selection listener | |
360 | * | |
361 | * @param listener | |
362 | * The listener to remove | |
f2ca0f69 GP |
363 | */ |
364 | public void removeDragSelectionListener(ITimeGraphTimeListener listener) { | |
365 | if (null != fDragSelectionListeners) { | |
366 | fDragSelectionListeners.remove(listener); | |
367 | } | |
368 | } | |
369 | ||
370 | /** | |
371 | * Drag Selection changed callback | |
372 | * | |
373 | * @param start | |
374 | * Time interval start | |
375 | * @param end | |
376 | * Time interval end | |
f2ca0f69 GP |
377 | */ |
378 | public void fireDragSelectionChanged(long start, long end) { | |
379 | // check for backward intervals | |
380 | long beginTime, endTime; | |
381 | if (start > end) { | |
382 | beginTime = end; | |
383 | endTime = start; | |
384 | } else { | |
385 | beginTime = start; | |
386 | endTime = end; | |
387 | } | |
388 | // call the listeners | |
389 | if (null != fDragSelectionListeners) { | |
390 | Iterator<ITimeGraphTimeListener> it = fDragSelectionListeners.iterator(); | |
391 | while (it.hasNext()) { | |
392 | ITimeGraphTimeListener listener = it.next(); | |
393 | listener.timeSelected(new TimeGraphTimeEvent(this, beginTime, endTime)); | |
394 | } | |
395 | } | |
396 | } | |
397 | ||
837a2f8c PT |
398 | /** |
399 | * Get the traces in the model | |
400 | * | |
401 | * @return The array of traces | |
402 | */ | |
403 | public ITimeGraphEntry[] getTraces() { | |
70e10acc | 404 | return fItemData.getEntries(); |
837a2f8c PT |
405 | } |
406 | ||
837a2f8c PT |
407 | /** |
408 | * Refresh the data for the thing | |
409 | */ | |
410 | public void refreshData() { | |
f1fae91f | 411 | fItemData.refreshData(); |
837a2f8c PT |
412 | redraw(); |
413 | } | |
414 | ||
415 | /** | |
416 | * Refresh data for the given traces | |
417 | * | |
418 | * @param traces | |
419 | * The traces to refresh | |
420 | */ | |
421 | public void refreshData(ITimeGraphEntry[] traces) { | |
f1fae91f | 422 | fItemData.refreshData(traces); |
837a2f8c PT |
423 | redraw(); |
424 | } | |
425 | ||
bec1f1ac GB |
426 | /** |
427 | * Refresh the links (arrows) of this widget | |
428 | * | |
2fe6a9ea | 429 | * @param events The link event list |
bec1f1ac GB |
430 | */ |
431 | public void refreshArrows(List<ILinkEvent> events) { | |
432 | fItemData.refreshArrows(events); | |
433 | } | |
434 | ||
2fe6a9ea PT |
435 | /** |
436 | * Get the links (arrows) of this widget | |
437 | * | |
438 | * @return The unmodifiable link event list | |
439 | * | |
0336f981 | 440 | * @since 1.1 |
2fe6a9ea PT |
441 | */ |
442 | public List<ILinkEvent> getArrows() { | |
443 | return Collections.unmodifiableList(fItemData.fLinks); | |
444 | } | |
445 | ||
837a2f8c PT |
446 | boolean ensureVisibleItem(int idx, boolean redraw) { |
447 | boolean changed = false; | |
41b5c37f AM |
448 | int index = idx; |
449 | if (index < 0) { | |
f1fae91f PT |
450 | for (index = 0; index < fItemData.fExpandedItems.length; index++) { |
451 | if (fItemData.fExpandedItems[index].fSelected) { | |
837a2f8c PT |
452 | break; |
453 | } | |
454 | } | |
455 | } | |
f1fae91f | 456 | if (index >= fItemData.fExpandedItems.length) { |
837a2f8c PT |
457 | return changed; |
458 | } | |
f1fae91f | 459 | if (index < fTopIndex) { |
41b5c37f | 460 | setTopIndex(index); |
837a2f8c PT |
461 | if (redraw) { |
462 | redraw(); | |
463 | } | |
464 | changed = true; | |
465 | } else { | |
466 | int page = countPerPage(); | |
f1fae91f | 467 | if (index >= fTopIndex + page) { |
41b5c37f | 468 | setTopIndex(index - page + 1); |
837a2f8c PT |
469 | if (redraw) { |
470 | redraw(); | |
471 | } | |
472 | changed = true; | |
473 | } | |
474 | } | |
475 | return changed; | |
476 | } | |
477 | ||
478 | /** | |
479 | * Assign the given index as the top one | |
480 | * | |
481 | * @param idx | |
482 | * The index | |
483 | */ | |
484 | public void setTopIndex(int idx) { | |
f1fae91f | 485 | int index = Math.min(idx, fItemData.fExpandedItems.length - countPerPage()); |
41b5c37f | 486 | index = Math.max(0, index); |
f1fae91f | 487 | fTopIndex = index; |
837a2f8c PT |
488 | redraw(); |
489 | } | |
490 | ||
48072ae3 PT |
491 | /** |
492 | * Set the top index so that the requested element is at the specified | |
493 | * position. | |
494 | * | |
495 | * @param entry | |
496 | * the time graph entry to be positioned | |
497 | * @param y | |
498 | * the requested y-coordinate | |
499 | * @since 2.0 | |
500 | */ | |
501 | public void setElementPosition(ITimeGraphEntry entry, int y) { | |
502 | Item item = fItemData.fItemMap.get(entry); | |
503 | if (item == null || item.fExpandedIndex == -1) { | |
504 | return; | |
505 | } | |
506 | int index = item.fExpandedIndex; | |
507 | Rectangle itemRect = getItemRect(getClientArea(), index); | |
508 | int delta = itemRect.y + itemRect.height - y; | |
509 | int topIndex = getItemIndexAtY(delta); | |
510 | if (topIndex != -1) { | |
511 | setTopIndex(topIndex); | |
512 | } else { | |
513 | if (delta < 0) { | |
514 | setTopIndex(0); | |
515 | } else { | |
516 | setTopIndex(getExpandedElementCount()); | |
517 | } | |
518 | } | |
519 | } | |
520 | ||
f4617471 | 521 | /** |
df0e3d5f PT |
522 | * Sets the auto-expand level to be used for new entries discovered when |
523 | * calling {@link #refreshData()} or {@link #refreshData(ITimeGraphEntry[])} | |
524 | * . The value 0 means that there is no auto-expand; 1 means that top-level | |
f4617471 PT |
525 | * entries are expanded, but not their children; 2 means that top-level |
526 | * entries are expanded, and their children, but not grand-children; and so | |
527 | * on. | |
528 | * <p> | |
529 | * The value {@link #ALL_LEVELS} means that all subtrees should be expanded. | |
530 | * </p> | |
df0e3d5f | 531 | * |
f4617471 PT |
532 | * @param level |
533 | * non-negative level, or <code>ALL_LEVELS</code> to expand all | |
534 | * levels of the tree | |
f4617471 PT |
535 | */ |
536 | public void setAutoExpandLevel(int level) { | |
537 | fAutoExpandLevel = level; | |
538 | } | |
539 | ||
540 | /** | |
541 | * Returns the auto-expand level. | |
542 | * | |
543 | * @return non-negative level, or <code>ALL_LEVELS</code> if all levels of | |
544 | * the tree are expanded automatically | |
545 | * @see #setAutoExpandLevel | |
f4617471 PT |
546 | */ |
547 | public int getAutoExpandLevel() { | |
548 | return fAutoExpandLevel; | |
549 | } | |
550 | ||
df0e3d5f PT |
551 | /** |
552 | * Get the expanded state of a given entry. | |
553 | * | |
554 | * @param entry | |
555 | * The entry | |
556 | * @return true if the entry is expanded, false if collapsed | |
0336f981 | 557 | * @since 1.1 |
df0e3d5f PT |
558 | */ |
559 | public boolean getExpandedState(ITimeGraphEntry entry) { | |
560 | Item item = fItemData.fItemMap.get(entry); | |
561 | return (item != null ? item.fExpanded : false); | |
562 | } | |
563 | ||
837a2f8c PT |
564 | /** |
565 | * Set the expanded state of a given entry | |
566 | * | |
567 | * @param entry | |
568 | * The entry | |
569 | * @param expanded | |
570 | * True if expanded, false if collapsed | |
571 | */ | |
572 | public void setExpandedState(ITimeGraphEntry entry, boolean expanded) { | |
f1fae91f PT |
573 | Item item = fItemData.findItem(entry); |
574 | if (item != null && item.fExpanded != expanded) { | |
575 | item.fExpanded = expanded; | |
576 | fItemData.updateExpandedItems(); | |
837a2f8c PT |
577 | redraw(); |
578 | } | |
579 | } | |
580 | ||
581 | /** | |
582 | * Collapses all nodes of the viewer's tree, starting with the root. | |
837a2f8c PT |
583 | */ |
584 | public void collapseAll() { | |
f1fae91f PT |
585 | for (Item item : fItemData.fItems) { |
586 | item.fExpanded = false; | |
837a2f8c | 587 | } |
f1fae91f | 588 | fItemData.updateExpandedItems(); |
837a2f8c PT |
589 | redraw(); |
590 | } | |
591 | ||
592 | /** | |
593 | * Expands all nodes of the viewer's tree, starting with the root. | |
837a2f8c PT |
594 | */ |
595 | public void expandAll() { | |
f1fae91f PT |
596 | for (Item item : fItemData.fItems) { |
597 | item.fExpanded = true; | |
837a2f8c | 598 | } |
f1fae91f | 599 | fItemData.updateExpandedItems(); |
837a2f8c PT |
600 | redraw(); |
601 | } | |
602 | ||
603 | /** | |
604 | * Add a tree listener | |
605 | * | |
606 | * @param listener | |
607 | * The listener to add | |
608 | */ | |
609 | public void addTreeListener(ITimeGraphTreeListener listener) { | |
f1fae91f PT |
610 | if (!fTreeListeners.contains(listener)) { |
611 | fTreeListeners.add(listener); | |
837a2f8c PT |
612 | } |
613 | } | |
614 | ||
615 | /** | |
616 | * Remove a tree listener | |
617 | * | |
618 | * @param listener | |
619 | * The listener to remove | |
620 | */ | |
621 | public void removeTreeListener(ITimeGraphTreeListener listener) { | |
f1fae91f PT |
622 | if (fTreeListeners.contains(listener)) { |
623 | fTreeListeners.remove(listener); | |
837a2f8c PT |
624 | } |
625 | } | |
626 | ||
627 | /** | |
628 | * Tree event callback | |
629 | * | |
630 | * @param entry | |
631 | * The affected entry | |
632 | * @param expanded | |
633 | * The expanded state (true for expanded, false for collapsed) | |
634 | */ | |
635 | public void fireTreeEvent(ITimeGraphEntry entry, boolean expanded) { | |
636 | TimeGraphTreeExpansionEvent event = new TimeGraphTreeExpansionEvent(this, entry); | |
f1fae91f | 637 | for (ITimeGraphTreeListener listener : fTreeListeners) { |
837a2f8c PT |
638 | if (expanded) { |
639 | listener.treeExpanded(event); | |
640 | } else { | |
641 | listener.treeCollapsed(event); | |
642 | } | |
643 | } | |
644 | } | |
645 | ||
27df1564 XR |
646 | /** |
647 | * Add a menu listener on {@link ITimeGraphEntry}s | |
ae09c4ad | 648 | * |
27df1564 XR |
649 | * @param listener |
650 | * The listener to add | |
27df1564 XR |
651 | */ |
652 | public void addTimeGraphEntryMenuListener(MenuDetectListener listener) { | |
f1fae91f PT |
653 | if (!fTimeGraphEntryMenuListeners.contains(listener)) { |
654 | fTimeGraphEntryMenuListeners.add(listener); | |
27df1564 XR |
655 | } |
656 | } | |
657 | ||
658 | /** | |
659 | * Remove a menu listener on {@link ITimeGraphEntry}s | |
660 | * | |
661 | * @param listener | |
662 | * The listener to remove | |
27df1564 XR |
663 | */ |
664 | public void removeTimeGraphEntryMenuListener(MenuDetectListener listener) { | |
f1fae91f PT |
665 | if (fTimeGraphEntryMenuListeners.contains(listener)) { |
666 | fTimeGraphEntryMenuListeners.remove(listener); | |
27df1564 XR |
667 | } |
668 | } | |
669 | ||
670 | /** | |
671 | * Menu event callback on {@link ITimeGraphEntry}s | |
672 | * | |
673 | * @param event | |
674 | * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeGraphEntry} | |
675 | */ | |
676 | private void fireMenuEventOnTimeGraphEntry(MenuDetectEvent event) { | |
f1fae91f | 677 | for (MenuDetectListener listener : fTimeGraphEntryMenuListeners) { |
27df1564 XR |
678 | listener.menuDetected(event); |
679 | } | |
680 | } | |
681 | ||
682 | /** | |
683 | * Add a menu listener on {@link ITimeEvent}s | |
684 | * | |
685 | * @param listener | |
686 | * The listener to add | |
27df1564 XR |
687 | */ |
688 | public void addTimeEventMenuListener(MenuDetectListener listener) { | |
f1fae91f PT |
689 | if (!fTimeEventMenuListeners.contains(listener)) { |
690 | fTimeEventMenuListeners.add(listener); | |
27df1564 XR |
691 | } |
692 | } | |
693 | ||
694 | /** | |
695 | * Remove a menu listener on {@link ITimeEvent}s | |
696 | * | |
697 | * @param listener | |
698 | * The listener to remove | |
27df1564 XR |
699 | */ |
700 | public void removeTimeEventMenuListener(MenuDetectListener listener) { | |
f1fae91f PT |
701 | if (fTimeEventMenuListeners.contains(listener)) { |
702 | fTimeEventMenuListeners.remove(listener); | |
27df1564 XR |
703 | } |
704 | } | |
705 | ||
706 | /** | |
707 | * Menu event callback on {@link ITimeEvent}s | |
708 | * | |
709 | * @param event | |
710 | * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeEvent} | |
711 | */ | |
712 | private void fireMenuEventOnTimeEvent(MenuDetectEvent event) { | |
f1fae91f | 713 | for (MenuDetectListener listener : fTimeEventMenuListeners) { |
27df1564 XR |
714 | listener.menuDetected(event); |
715 | } | |
716 | } | |
717 | ||
837a2f8c PT |
718 | @Override |
719 | public ISelection getSelection() { | |
720 | TimeGraphSelection sel = new TimeGraphSelection(); | |
721 | ITimeGraphEntry trace = getSelectedTrace(); | |
f1fae91f | 722 | if (null != trace && null != fTimeProvider) { |
baf92cac | 723 | long selectedTime = fTimeProvider.getSelectionBegin(); |
837a2f8c PT |
724 | ITimeEvent event = Utils.findEvent(trace, selectedTime, 0); |
725 | if (event != null) { | |
726 | sel.add(event); | |
727 | } else { | |
728 | sel.add(trace); | |
729 | } | |
730 | } | |
731 | return sel; | |
732 | } | |
733 | ||
734 | /** | |
735 | * Get the selection object | |
736 | * | |
737 | * @return The selection | |
738 | */ | |
739 | public ISelection getSelectionTrace() { | |
740 | TimeGraphSelection sel = new TimeGraphSelection(); | |
741 | ITimeGraphEntry trace = getSelectedTrace(); | |
742 | if (null != trace) { | |
743 | sel.add(trace); | |
744 | } | |
745 | return sel; | |
746 | } | |
747 | ||
748 | /** | |
749 | * Enable/disable one of the traces in the model | |
750 | * | |
751 | * @param n | |
752 | * 1 to enable it, -1 to disable. The method returns immediately | |
753 | * if another value is used. | |
754 | */ | |
755 | public void selectTrace(int n) { | |
756 | if ((n != 1) && (n != -1)) { | |
757 | return; | |
758 | } | |
759 | ||
760 | boolean changed = false; | |
761 | int lastSelection = -1; | |
f1fae91f PT |
762 | for (int i = 0; i < fItemData.fExpandedItems.length; i++) { |
763 | Item item = fItemData.fExpandedItems[i]; | |
764 | if (item.fSelected) { | |
837a2f8c | 765 | lastSelection = i; |
f1fae91f PT |
766 | if ((1 == n) && (i < fItemData.fExpandedItems.length - 1)) { |
767 | item.fSelected = false; | |
768 | item = fItemData.fExpandedItems[i + 1]; | |
769 | item.fSelected = true; | |
837a2f8c PT |
770 | changed = true; |
771 | } else if ((-1 == n) && (i > 0)) { | |
f1fae91f PT |
772 | item.fSelected = false; |
773 | item = fItemData.fExpandedItems[i - 1]; | |
774 | item.fSelected = true; | |
837a2f8c PT |
775 | changed = true; |
776 | } | |
777 | break; | |
778 | } | |
779 | } | |
780 | ||
f1fae91f PT |
781 | if (lastSelection < 0 && fItemData.fExpandedItems.length > 0) { |
782 | Item item = fItemData.fExpandedItems[0]; | |
783 | item.fSelected = true; | |
837a2f8c PT |
784 | changed = true; |
785 | } | |
786 | ||
787 | if (changed) { | |
788 | ensureVisibleItem(-1, false); | |
789 | redraw(); | |
790 | fireSelectionChanged(); | |
791 | } | |
792 | } | |
793 | ||
794 | /** | |
795 | * Select an event | |
796 | * | |
797 | * @param n | |
798 | * 1 for next event, -1 for previous event | |
33fa1fc7 PT |
799 | * @param extend |
800 | * true to extend selection range, false for single selection | |
801 | * @since 1.0 | |
837a2f8c | 802 | */ |
33fa1fc7 | 803 | public void selectEvent(int n, boolean extend) { |
f1fae91f | 804 | if (null == fTimeProvider) { |
837a2f8c PT |
805 | return; |
806 | } | |
807 | ITimeGraphEntry trace = getSelectedTrace(); | |
808 | if (trace == null) { | |
809 | return; | |
810 | } | |
33fa1fc7 | 811 | long selectedTime = fTimeProvider.getSelectionEnd(); |
50d36521 | 812 | long endTime = fTimeProvider.getMaxTime(); |
837a2f8c | 813 | ITimeEvent nextEvent; |
33fa1fc7 | 814 | if (n == -1 && selectedTime > endTime) { |
837a2f8c PT |
815 | nextEvent = Utils.findEvent(trace, selectedTime, 0); |
816 | } else { | |
817 | nextEvent = Utils.findEvent(trace, selectedTime, n); | |
818 | } | |
33fa1fc7 | 819 | if (null == nextEvent && n == -1) { |
837a2f8c PT |
820 | nextEvent = Utils.getFirstEvent(trace); |
821 | } | |
822 | if (null != nextEvent) { | |
823 | long nextTime = nextEvent.getTime(); | |
824 | // If last event detected e.g. going back or not moving to a next | |
825 | // event | |
826 | if (nextTime <= selectedTime && n == 1) { | |
827 | // Select to the end of this last event | |
828 | nextTime = nextEvent.getTime() + nextEvent.getDuration(); | |
829 | // but not beyond the end of the trace | |
830 | if (nextTime > endTime) { | |
831 | nextTime = endTime; | |
832 | } | |
f1fae91f | 833 | } else if (n == -1 && nextEvent.getTime() + nextEvent.getDuration() < selectedTime) { |
837a2f8c | 834 | // for previous event go to its end time unless we were already there |
f1fae91f | 835 | nextTime = nextEvent.getTime() + nextEvent.getDuration(); |
837a2f8c | 836 | } |
33fa1fc7 | 837 | if (extend) { |
84c8aef7 | 838 | fTimeProvider.setSelectionRangeNotify(fTimeProvider.getSelectionBegin(), nextTime, true); |
33fa1fc7 PT |
839 | } else { |
840 | fTimeProvider.setSelectedTimeNotify(nextTime, true); | |
841 | } | |
837a2f8c | 842 | fireSelectionChanged(); |
33fa1fc7 PT |
843 | } else if (n == 1) { |
844 | if (extend) { | |
84c8aef7 | 845 | fTimeProvider.setSelectionRangeNotify(fTimeProvider.getSelectionBegin(), endTime, true); |
33fa1fc7 PT |
846 | } else { |
847 | fTimeProvider.setSelectedTimeNotify(endTime, true); | |
848 | } | |
837a2f8c PT |
849 | fireSelectionChanged(); |
850 | } | |
33fa1fc7 | 851 | updateStatusLine(STATUS_WITHOUT_CURSOR_TIME); |
837a2f8c PT |
852 | } |
853 | ||
854 | /** | |
855 | * Select the next event | |
33fa1fc7 PT |
856 | * |
857 | * @param extend | |
858 | * true to extend selection range, false for single selection | |
859 | * @since 1.0 | |
837a2f8c | 860 | */ |
33fa1fc7 PT |
861 | public void selectNextEvent(boolean extend) { |
862 | selectEvent(1, extend); | |
837a2f8c | 863 | // Notify if visible time window has been adjusted |
f1fae91f | 864 | fTimeProvider.setStartFinishTimeNotify(fTimeProvider.getTime0(), fTimeProvider.getTime1()); |
837a2f8c PT |
865 | } |
866 | ||
867 | /** | |
868 | * Select the previous event | |
33fa1fc7 PT |
869 | * |
870 | * @param extend | |
871 | * true to extend selection range, false for single selection | |
872 | * @since 1.0 | |
837a2f8c | 873 | */ |
33fa1fc7 PT |
874 | public void selectPrevEvent(boolean extend) { |
875 | selectEvent(-1, extend); | |
837a2f8c | 876 | // Notify if visible time window has been adjusted |
f1fae91f | 877 | fTimeProvider.setStartFinishTimeNotify(fTimeProvider.getTime0(), fTimeProvider.getTime1()); |
837a2f8c PT |
878 | } |
879 | ||
880 | /** | |
881 | * Select the next trace | |
882 | */ | |
883 | public void selectNextTrace() { | |
884 | selectTrace(1); | |
885 | } | |
886 | ||
887 | /** | |
888 | * Select the previous trace | |
889 | */ | |
890 | public void selectPrevTrace() { | |
891 | selectTrace(-1); | |
892 | } | |
893 | ||
b698ec63 PT |
894 | /** |
895 | * Scroll left or right by one half window size | |
896 | * | |
897 | * @param left | |
898 | * true to scroll left, false to scroll right | |
b698ec63 PT |
899 | */ |
900 | public void horizontalScroll(boolean left) { | |
901 | long time0 = fTimeProvider.getTime0(); | |
902 | long time1 = fTimeProvider.getTime1(); | |
903 | long timeMin = fTimeProvider.getMinTime(); | |
904 | long timeMax = fTimeProvider.getMaxTime(); | |
905 | long range = time1 - time0; | |
906 | if (range <= 0) { | |
907 | return; | |
908 | } | |
909 | long increment = Math.max(1, range / 2); | |
910 | if (left) { | |
911 | time0 = Math.max(time0 - increment, timeMin); | |
912 | time1 = time0 + range; | |
913 | } else { | |
914 | time1 = Math.min(time1 + increment, timeMax); | |
915 | time0 = time1 - range; | |
916 | } | |
917 | fTimeProvider.setStartFinishTimeNotify(time0, time1); | |
918 | } | |
919 | ||
837a2f8c PT |
920 | /** |
921 | * Zoom based on mouse cursor location with mouse scrolling | |
922 | * | |
923 | * @param zoomIn true to zoom in, false to zoom out | |
924 | */ | |
925 | public void zoom(boolean zoomIn) { | |
926 | int globalX = getDisplay().getCursorLocation().x; | |
927 | Point p = toControl(globalX, 0); | |
f1fae91f PT |
928 | int nameSpace = fTimeProvider.getNameSpace(); |
929 | int timeSpace = fTimeProvider.getTimeSpace(); | |
837a2f8c | 930 | int xPos = Math.max(nameSpace, Math.min(nameSpace + timeSpace, p.x)); |
f1fae91f PT |
931 | long time0 = fTimeProvider.getTime0(); |
932 | long time1 = fTimeProvider.getTime1(); | |
837a2f8c PT |
933 | long interval = time1 - time0; |
934 | if (interval == 0) { | |
935 | interval = 1; | |
936 | } // to allow getting out of single point interval | |
937 | long newInterval; | |
938 | if (zoomIn) { | |
f1fae91f | 939 | newInterval = Math.max(Math.round(interval * ZOOM_IN_FACTOR), fTimeProvider.getMinTimeInterval()); |
837a2f8c | 940 | } else { |
f1fae91f | 941 | newInterval = (long) Math.ceil(interval * ZOOM_OUT_FACTOR); |
837a2f8c PT |
942 | } |
943 | long center = time0 + Math.round(((double) (xPos - nameSpace) / timeSpace * interval)); | |
944 | long newTime0 = center - Math.round((double) newInterval * (center - time0) / interval); | |
945 | long newTime1 = newTime0 + newInterval; | |
10ad9fa6 | 946 | fTimeProvider.setStartFinishTimeNotify(newTime0, newTime1); |
837a2f8c PT |
947 | } |
948 | ||
949 | /** | |
950 | * zoom in using single click | |
951 | */ | |
952 | public void zoomIn() { | |
f1fae91f PT |
953 | long prevTime0 = fTimeProvider.getTime0(); |
954 | long prevTime1 = fTimeProvider.getTime1(); | |
955 | long prevRange = prevTime1 - prevTime0; | |
956 | if (prevRange == 0) { | |
3e9a3685 PT |
957 | return; |
958 | } | |
baf92cac AM |
959 | ITimeDataProvider provider = fTimeProvider; |
960 | long selTime = (provider.getSelectionEnd() + provider.getSelectionBegin()) / 2; | |
f1fae91f PT |
961 | if (selTime <= prevTime0 || selTime >= prevTime1) { |
962 | selTime = (prevTime0 + prevTime1) / 2; | |
837a2f8c | 963 | } |
f1fae91f PT |
964 | long time0 = selTime - (long) ((selTime - prevTime0) / ZOOM_FACTOR); |
965 | long time1 = selTime + (long) ((prevTime1 - selTime) / ZOOM_FACTOR); | |
837a2f8c | 966 | |
f1fae91f | 967 | long inaccuracy = (fTimeProvider.getMaxTime() - fTimeProvider.getMinTime()) - (time1 - time0); |
837a2f8c PT |
968 | |
969 | if (inaccuracy > 0 && inaccuracy < 100) { | |
f1fae91f | 970 | fTimeProvider.setStartFinishTimeNotify(fTimeProvider.getMinTime(), fTimeProvider.getMaxTime()); |
837a2f8c PT |
971 | return; |
972 | } | |
973 | ||
f1fae91f PT |
974 | long min = fTimeProvider.getMinTimeInterval(); |
975 | if ((time1 - time0) < min) { | |
976 | time0 = selTime - (selTime - prevTime0) * min / prevRange; | |
977 | time1 = time0 + min; | |
837a2f8c PT |
978 | } |
979 | ||
f1fae91f | 980 | fTimeProvider.setStartFinishTimeNotify(time0, time1); |
837a2f8c PT |
981 | } |
982 | ||
983 | /** | |
984 | * zoom out using single click | |
985 | */ | |
986 | public void zoomOut() { | |
f1fae91f PT |
987 | long prevTime0 = fTimeProvider.getTime0(); |
988 | long prevTime1 = fTimeProvider.getTime1(); | |
baf92cac AM |
989 | ITimeDataProvider provider = fTimeProvider; |
990 | long selTime = (provider.getSelectionEnd() + provider.getSelectionBegin()) / 2; | |
f1fae91f PT |
991 | if (selTime <= prevTime0 || selTime >= prevTime1) { |
992 | selTime = (prevTime0 + prevTime1) / 2; | |
837a2f8c | 993 | } |
f1fae91f PT |
994 | long time0 = (long) (selTime - (selTime - prevTime0) * ZOOM_FACTOR); |
995 | long time1 = (long) (selTime + (prevTime1 - selTime) * ZOOM_FACTOR); | |
837a2f8c | 996 | |
f1fae91f | 997 | long inaccuracy = (fTimeProvider.getMaxTime() - fTimeProvider.getMinTime()) - (time1 - time0); |
837a2f8c | 998 | if (inaccuracy > 0 && inaccuracy < 100) { |
f1fae91f | 999 | fTimeProvider.setStartFinishTimeNotify(fTimeProvider.getMinTime(), fTimeProvider.getMaxTime()); |
837a2f8c PT |
1000 | return; |
1001 | } | |
1002 | ||
f1fae91f | 1003 | fTimeProvider.setStartFinishTimeNotify(time0, time1); |
837a2f8c PT |
1004 | } |
1005 | ||
3bd20aa6 PT |
1006 | /** |
1007 | * Zoom vertically. | |
1008 | * | |
1009 | * @param zoomIn | |
1010 | * true to zoom in, false to zoom out | |
3bd20aa6 PT |
1011 | * @since 2.0 |
1012 | */ | |
83d0971d | 1013 | public void verticalZoom(boolean zoomIn) { |
3bd20aa6 PT |
1014 | if (zoomIn) { |
1015 | fHeightAdjustment++; | |
1016 | } else { | |
1017 | fHeightAdjustment--; | |
1018 | } | |
83d0971d | 1019 | fItemData.refreshData(); |
3bd20aa6 PT |
1020 | redraw(); |
1021 | } | |
1022 | ||
1023 | /** | |
1024 | * Reset the vertical zoom to default. | |
1025 | * | |
3bd20aa6 PT |
1026 | * @since 2.0 |
1027 | */ | |
83d0971d | 1028 | public void resetVerticalZoom() { |
3bd20aa6 | 1029 | fHeightAdjustment = 0; |
83d0971d | 1030 | fItemData.refreshData(); |
3bd20aa6 PT |
1031 | redraw(); |
1032 | } | |
1033 | ||
55d1fc96 PT |
1034 | /** |
1035 | * Set the grid lines visibility. The default is true. | |
1036 | * | |
1037 | * @param visible | |
1038 | * true to show the grid lines, false otherwise | |
1039 | * @since 2.0 | |
1040 | */ | |
1041 | public void setGridLinesVisible(boolean visible) { | |
1042 | fGridLinesVisible = visible; | |
1043 | } | |
1044 | ||
1045 | /** | |
1046 | * Get the grid lines visibility. | |
1047 | * | |
1048 | * @return true if the grid lines are visible, false otherwise | |
1049 | * @since 2.0 | |
1050 | */ | |
1051 | public boolean getGridLinesVisible() { | |
1052 | return fGridLinesVisible; | |
1053 | } | |
1054 | ||
1055 | /** | |
1056 | * Set the grid line color. The default is SWT.COLOR_GRAY. | |
1057 | * | |
1058 | * @param color | |
1059 | * the grid line color | |
1060 | * @since 2.0 | |
1061 | */ | |
1062 | public void setGridLineColor(Color color) { | |
1063 | fGridLineColor = color; | |
1064 | } | |
1065 | ||
1066 | /** | |
1067 | * Get the grid line color. | |
1068 | * | |
1069 | * @return the grid line color | |
1070 | * @since 2.0 | |
1071 | */ | |
1072 | public Color getGridLineColor() { | |
1073 | return fGridLineColor; | |
1074 | } | |
1075 | ||
56b43ab2 PT |
1076 | /** |
1077 | * Set the markers list. | |
1078 | * | |
1079 | * @param markers | |
1080 | * The markers list, or null | |
1081 | * @since 2.0 | |
1082 | */ | |
1083 | public void setMarkers(List<IMarkerEvent> markers) { | |
1084 | fMarkers = markers; | |
56b43ab2 PT |
1085 | } |
1086 | ||
1087 | /** | |
1088 | * Get the markers list. | |
1089 | * | |
1090 | * @return The markers list, or null | |
1091 | * @since 2.0 | |
1092 | */ | |
1093 | public List<IMarkerEvent> getMarkers() { | |
1094 | return fMarkers; | |
1095 | } | |
1096 | ||
1097 | /** | |
1098 | * Set the markers visibility. The default is true. | |
1099 | * | |
1100 | * @param visible | |
1101 | * true to show the markers, false otherwise | |
1102 | * @since 2.0 | |
1103 | */ | |
1104 | public void setMarkersVisible(boolean visible) { | |
1105 | fMarkersVisible = visible; | |
56b43ab2 PT |
1106 | } |
1107 | ||
1108 | /** | |
1109 | * Get the markers visibility. | |
1110 | * | |
1111 | * @return true if the markers are visible, false otherwise | |
1112 | * @since 2.0 | |
1113 | */ | |
1114 | public boolean getMarkersVisible() { | |
1115 | return fMarkersVisible; | |
1116 | } | |
1117 | ||
79ec0b89 PT |
1118 | /** |
1119 | * Hide arrows | |
1120 | * | |
1121 | * @param hideArrows true to hide arrows | |
79ec0b89 PT |
1122 | */ |
1123 | public void hideArrows(boolean hideArrows) { | |
1124 | fHideArrows = hideArrows; | |
1125 | } | |
1126 | ||
086f21ae PT |
1127 | /** |
1128 | * Follow the arrow forward | |
33fa1fc7 PT |
1129 | * |
1130 | * @param extend | |
1131 | * true to extend selection range, false for single selection | |
1132 | * @since 1.0 | |
086f21ae | 1133 | */ |
33fa1fc7 | 1134 | public void followArrowFwd(boolean extend) { |
086f21ae PT |
1135 | ITimeGraphEntry trace = getSelectedTrace(); |
1136 | if (trace == null) { | |
1137 | return; | |
1138 | } | |
33fa1fc7 | 1139 | long selectedTime = fTimeProvider.getSelectionEnd(); |
086f21ae PT |
1140 | for (ILinkEvent link : fItemData.fLinks) { |
1141 | if (link.getEntry() == trace && link.getTime() == selectedTime) { | |
1142 | selectItem(link.getDestinationEntry(), false); | |
1143 | if (link.getDuration() != 0) { | |
33fa1fc7 | 1144 | if (extend) { |
84c8aef7 | 1145 | fTimeProvider.setSelectionRangeNotify(fTimeProvider.getSelectionBegin(), link.getTime() + link.getDuration(), true); |
33fa1fc7 PT |
1146 | } else { |
1147 | fTimeProvider.setSelectedTimeNotify(link.getTime() + link.getDuration(), true); | |
1148 | } | |
086f21ae PT |
1149 | // Notify if visible time window has been adjusted |
1150 | fTimeProvider.setStartFinishTimeNotify(fTimeProvider.getTime0(), fTimeProvider.getTime1()); | |
1151 | } | |
1152 | fireSelectionChanged(); | |
33fa1fc7 | 1153 | updateStatusLine(STATUS_WITHOUT_CURSOR_TIME); |
086f21ae PT |
1154 | return; |
1155 | } | |
1156 | } | |
33fa1fc7 | 1157 | selectNextEvent(extend); |
086f21ae PT |
1158 | } |
1159 | ||
1160 | /** | |
1161 | * Follow the arrow backward | |
33fa1fc7 PT |
1162 | * |
1163 | * @param extend | |
1164 | * true to extend selection range, false for single selection | |
1165 | * @since 1.0 | |
086f21ae | 1166 | */ |
33fa1fc7 | 1167 | public void followArrowBwd(boolean extend) { |
086f21ae PT |
1168 | ITimeGraphEntry trace = getSelectedTrace(); |
1169 | if (trace == null) { | |
1170 | return; | |
1171 | } | |
33fa1fc7 | 1172 | long selectedTime = fTimeProvider.getSelectionEnd(); |
086f21ae PT |
1173 | for (ILinkEvent link : fItemData.fLinks) { |
1174 | if (link.getDestinationEntry() == trace && link.getTime() + link.getDuration() == selectedTime) { | |
1175 | selectItem(link.getEntry(), false); | |
1176 | if (link.getDuration() != 0) { | |
33fa1fc7 | 1177 | if (extend) { |
84c8aef7 | 1178 | fTimeProvider.setSelectionRangeNotify(fTimeProvider.getSelectionBegin(), link.getTime(), true); |
33fa1fc7 PT |
1179 | } else { |
1180 | fTimeProvider.setSelectedTimeNotify(link.getTime(), true); | |
1181 | } | |
086f21ae PT |
1182 | // Notify if visible time window has been adjusted |
1183 | fTimeProvider.setStartFinishTimeNotify(fTimeProvider.getTime0(), fTimeProvider.getTime1()); | |
1184 | } | |
1185 | fireSelectionChanged(); | |
33fa1fc7 | 1186 | updateStatusLine(STATUS_WITHOUT_CURSOR_TIME); |
086f21ae PT |
1187 | return; |
1188 | } | |
1189 | } | |
33fa1fc7 | 1190 | selectPrevEvent(extend); |
086f21ae PT |
1191 | } |
1192 | ||
837a2f8c PT |
1193 | /** |
1194 | * Return the currently selected trace | |
1195 | * | |
1196 | * @return The entry matching the trace | |
1197 | */ | |
1198 | public ITimeGraphEntry getSelectedTrace() { | |
1199 | ITimeGraphEntry trace = null; | |
1200 | int idx = getSelectedIndex(); | |
1201 | if (idx >= 0) { | |
70e10acc | 1202 | trace = fItemData.fExpandedItems[idx].fEntry; |
837a2f8c PT |
1203 | } |
1204 | return trace; | |
1205 | } | |
1206 | ||
1207 | /** | |
1208 | * Retrieve the index of the currently selected item | |
1209 | * | |
1210 | * @return The index | |
1211 | */ | |
1212 | public int getSelectedIndex() { | |
1213 | int idx = -1; | |
f1fae91f PT |
1214 | for (int i = 0; i < fItemData.fExpandedItems.length; i++) { |
1215 | Item item = fItemData.fExpandedItems[i]; | |
1216 | if (item.fSelected) { | |
837a2f8c PT |
1217 | idx = i; |
1218 | break; | |
1219 | } | |
1220 | } | |
1221 | return idx; | |
1222 | } | |
1223 | ||
1224 | boolean toggle(int idx) { | |
1225 | boolean toggled = false; | |
f1fae91f PT |
1226 | if (idx >= 0 && idx < fItemData.fExpandedItems.length) { |
1227 | Item item = fItemData.fExpandedItems[idx]; | |
1228 | if (item.fHasChildren) { | |
1229 | item.fExpanded = !item.fExpanded; | |
1230 | fItemData.updateExpandedItems(); | |
837a2f8c PT |
1231 | redraw(); |
1232 | toggled = true; | |
70e10acc | 1233 | fireTreeEvent(item.fEntry, item.fExpanded); |
837a2f8c PT |
1234 | } |
1235 | } | |
1236 | return toggled; | |
1237 | } | |
1238 | ||
bedfbbb5 XR |
1239 | /** |
1240 | * Gets the index of the item at the given location. | |
1241 | * | |
1242 | * @param y | |
1243 | * the y coordinate | |
1244 | * @return the index of the item at the given location, of -1 if none. | |
bedfbbb5 XR |
1245 | */ |
1246 | protected int getItemIndexAtY(int y) { | |
c004295c | 1247 | int ySum = 0; |
48072ae3 PT |
1248 | if (y < 0) { |
1249 | for (int idx = fTopIndex - 1; idx >= 0; idx--) { | |
1250 | ySum -= fItemData.fExpandedItems[idx].fItemHeight; | |
1251 | if (y >= ySum) { | |
1252 | return idx; | |
1253 | } | |
1254 | } | |
1255 | } else { | |
1256 | for (int idx = fTopIndex; idx < fItemData.fExpandedItems.length; idx++) { | |
1257 | ySum += fItemData.fExpandedItems[idx].fItemHeight; | |
1258 | if (y < ySum) { | |
1259 | return idx; | |
1260 | } | |
837a2f8c | 1261 | } |
837a2f8c PT |
1262 | } |
1263 | return -1; | |
1264 | } | |
1265 | ||
1266 | boolean isOverSplitLine(int x) { | |
f1fae91f | 1267 | if (x < 0 || null == fTimeProvider) { |
837a2f8c PT |
1268 | return false; |
1269 | } | |
f1fae91f | 1270 | int nameWidth = fTimeProvider.getNameSpace(); |
0fcf3b09 | 1271 | return Math.abs(x - nameWidth) < SNAP_WIDTH; |
837a2f8c PT |
1272 | } |
1273 | ||
bedfbbb5 XR |
1274 | /** |
1275 | * Gets the {@link ITimeGraphEntry} at the given location. | |
1276 | * | |
1277 | * @param pt | |
1278 | * a point in the widget | |
1279 | * @return the {@link ITimeGraphEntry} at this point, or <code>null</code> | |
1280 | * if none. | |
bedfbbb5 XR |
1281 | */ |
1282 | protected ITimeGraphEntry getEntry(Point pt) { | |
837a2f8c | 1283 | int idx = getItemIndexAtY(pt.y); |
70e10acc | 1284 | return idx >= 0 ? fItemData.fExpandedItems[idx].fEntry : null; |
837a2f8c PT |
1285 | } |
1286 | ||
1053eaa6 PT |
1287 | /** |
1288 | * Return the arrow event closest to the given point that is no further than | |
1289 | * a maximum distance. | |
1290 | * | |
1291 | * @param pt | |
1292 | * a point in the widget | |
1293 | * @return The closest arrow event, or null if there is none close enough. | |
1053eaa6 PT |
1294 | */ |
1295 | protected ILinkEvent getArrow(Point pt) { | |
1296 | if (fHideArrows) { | |
1297 | return null; | |
1298 | } | |
1299 | ILinkEvent linkEvent = null; | |
1300 | double minDistance = Double.MAX_VALUE; | |
1301 | for (ILinkEvent event : fItemData.fLinks) { | |
1302 | Rectangle rect = getArrowRectangle(new Rectangle(0, 0, 0, 0), event); | |
1303 | if (rect != null) { | |
1304 | int x1 = rect.x; | |
1305 | int y1 = rect.y; | |
1306 | int x2 = x1 + rect.width; | |
1307 | int y2 = y1 + rect.height; | |
1308 | double d = Utils.distance(pt.x, pt.y, x1, y1, x2, y2); | |
1309 | if (minDistance > d) { | |
1310 | minDistance = d; | |
1311 | linkEvent = event; | |
1312 | } | |
1313 | } | |
1314 | } | |
1315 | if (minDistance <= ARROW_HOVER_MAX_DIST) { | |
1316 | return linkEvent; | |
1317 | } | |
1318 | return null; | |
1319 | } | |
1320 | ||
4999a196 | 1321 | @Override |
713a70ae | 1322 | public int getXForTime(long time) { |
f1fae91f | 1323 | if (null == fTimeProvider) { |
713a70ae PT |
1324 | return -1; |
1325 | } | |
f1fae91f PT |
1326 | long time0 = fTimeProvider.getTime0(); |
1327 | long time1 = fTimeProvider.getTime1(); | |
b698ec63 | 1328 | int width = getSize().x; |
f1fae91f | 1329 | int nameSpace = fTimeProvider.getNameSpace(); |
713a70ae PT |
1330 | double pixelsPerNanoSec = (width - nameSpace <= RIGHT_MARGIN) ? 0 : (double) (width - nameSpace - RIGHT_MARGIN) / (time1 - time0); |
1331 | int x = getBounds().x + nameSpace + (int) ((time - time0) * pixelsPerNanoSec); | |
1332 | return x; | |
1333 | } | |
1334 | ||
4999a196 | 1335 | @Override |
41b5c37f | 1336 | public long getTimeAtX(int coord) { |
f1fae91f | 1337 | if (null == fTimeProvider) { |
837a2f8c PT |
1338 | return -1; |
1339 | } | |
1340 | long hitTime = -1; | |
b698ec63 | 1341 | Point size = getSize(); |
f1fae91f PT |
1342 | long time0 = fTimeProvider.getTime0(); |
1343 | long time1 = fTimeProvider.getTime1(); | |
1344 | int nameWidth = fTimeProvider.getNameSpace(); | |
41b5c37f | 1345 | final int x = coord - nameWidth; |
837a2f8c PT |
1346 | int timeWidth = size.x - nameWidth - RIGHT_MARGIN; |
1347 | if (x >= 0 && size.x >= nameWidth) { | |
1348 | if (time1 - time0 > timeWidth) { | |
1349 | // nanosecond smaller than one pixel: use the first integer nanosecond of this pixel's time range | |
1350 | hitTime = time0 + (long) Math.ceil((time1 - time0) * ((double) x / timeWidth)); | |
1351 | } else { | |
1352 | // nanosecond greater than one pixel: use the nanosecond that covers this pixel start position | |
1353 | hitTime = time0 + (long) Math.floor((time1 - time0) * ((double) x / timeWidth)); | |
1354 | } | |
1355 | } | |
1356 | return hitTime; | |
1357 | } | |
1358 | ||
1359 | void selectItem(int idx, boolean addSelection) { | |
1360 | boolean changed = false; | |
1361 | if (addSelection) { | |
f1fae91f PT |
1362 | if (idx >= 0 && idx < fItemData.fExpandedItems.length) { |
1363 | Item item = fItemData.fExpandedItems[idx]; | |
1364 | changed = !item.fSelected; | |
1365 | item.fSelected = true; | |
837a2f8c PT |
1366 | } |
1367 | } else { | |
f1fae91f PT |
1368 | for (int i = 0; i < fItemData.fExpandedItems.length; i++) { |
1369 | Item item = fItemData.fExpandedItems[i]; | |
1370 | if ((i == idx && !item.fSelected) || (idx == -1 && item.fSelected)) { | |
837a2f8c PT |
1371 | changed = true; |
1372 | } | |
f1fae91f | 1373 | item.fSelected = i == idx; |
837a2f8c PT |
1374 | } |
1375 | } | |
1376 | changed |= ensureVisibleItem(idx, true); | |
1377 | if (changed) { | |
1378 | redraw(); | |
1379 | } | |
1380 | } | |
1381 | ||
1382 | /** | |
1383 | * Callback for item selection | |
1384 | * | |
1385 | * @param trace | |
1386 | * The entry matching the trace | |
1387 | * @param addSelection | |
1388 | * If the selection is added or removed | |
1389 | */ | |
1390 | public void selectItem(ITimeGraphEntry trace, boolean addSelection) { | |
f1fae91f | 1391 | int idx = fItemData.findItemIndex(trace); |
837a2f8c PT |
1392 | selectItem(idx, addSelection); |
1393 | } | |
1394 | ||
1395 | /** | |
1396 | * Retrieve the number of entries shown per page. | |
1397 | * | |
1398 | * @return The count | |
1399 | */ | |
1400 | public int countPerPage() { | |
b698ec63 | 1401 | int height = getSize().y; |
837a2f8c | 1402 | int count = 0; |
c004295c PT |
1403 | int ySum = 0; |
1404 | for (int idx = fTopIndex; idx < fItemData.fExpandedItems.length; idx++) { | |
1405 | ySum += fItemData.fExpandedItems[idx].fItemHeight; | |
1406 | if (ySum >= height) { | |
1407 | return count; | |
837a2f8c | 1408 | } |
c004295c | 1409 | count++; |
837a2f8c | 1410 | } |
c004295c PT |
1411 | for (int idx = fTopIndex - 1; idx >= 0; idx--) { |
1412 | ySum += fItemData.fExpandedItems[idx].fItemHeight; | |
1413 | if (ySum >= height) { | |
1414 | return count; | |
1415 | } | |
1416 | count++; | |
837a2f8c PT |
1417 | } |
1418 | return count; | |
1419 | } | |
1420 | ||
1421 | /** | |
1422 | * Get the index of the top element | |
1423 | * | |
1424 | * @return The index | |
1425 | */ | |
1426 | public int getTopIndex() { | |
f1fae91f | 1427 | return fTopIndex; |
837a2f8c PT |
1428 | } |
1429 | ||
1430 | /** | |
df0e3d5f | 1431 | * Get the number of expanded (visible) items |
837a2f8c | 1432 | * |
df0e3d5f | 1433 | * @return The count of expanded (visible) items |
837a2f8c PT |
1434 | */ |
1435 | public int getExpandedElementCount() { | |
f1fae91f | 1436 | return fItemData.fExpandedItems.length; |
837a2f8c PT |
1437 | } |
1438 | ||
1439 | /** | |
df0e3d5f | 1440 | * Get an array of all expanded (visible) elements |
837a2f8c | 1441 | * |
df0e3d5f | 1442 | * @return The expanded (visible) elements |
837a2f8c PT |
1443 | */ |
1444 | public ITimeGraphEntry[] getExpandedElements() { | |
507b1336 | 1445 | ArrayList<ITimeGraphEntry> elements = new ArrayList<>(); |
f1fae91f | 1446 | for (Item item : fItemData.fExpandedItems) { |
70e10acc | 1447 | elements.add(item.fEntry); |
837a2f8c PT |
1448 | } |
1449 | return elements.toArray(new ITimeGraphEntry[0]); | |
1450 | } | |
1451 | ||
48072ae3 PT |
1452 | /** |
1453 | * Get the expanded (visible) element at the specified index. | |
1454 | * | |
1455 | * @param index | |
1456 | * the element index | |
1457 | * @return The expanded (visible) element or null if out of range | |
1458 | * @since 2.0 | |
1459 | */ | |
1460 | public ITimeGraphEntry getExpandedElement(int index) { | |
1461 | if (index < 0 || index >= fItemData.fExpandedItems.length) { | |
1462 | return null; | |
1463 | } | |
1464 | return fItemData.fExpandedItems[index].fEntry; | |
1465 | } | |
1466 | ||
83b36bb4 PT |
1467 | Rectangle getNameRect(Rectangle bounds, int idx, int nameWidth) { |
1468 | Rectangle rect = getItemRect(bounds, idx); | |
c004295c PT |
1469 | rect.width = nameWidth; |
1470 | return rect; | |
837a2f8c PT |
1471 | } |
1472 | ||
83b36bb4 PT |
1473 | Rectangle getStatesRect(Rectangle bounds, int idx, int nameWidth) { |
1474 | Rectangle rect = getItemRect(bounds, idx); | |
1475 | rect.x += nameWidth; | |
1476 | rect.width -= nameWidth; | |
1477 | return rect; | |
1478 | } | |
1479 | ||
1480 | Rectangle getItemRect(Rectangle bounds, int idx) { | |
c004295c PT |
1481 | int ySum = 0; |
1482 | if (idx >= fTopIndex) { | |
f1fae91f PT |
1483 | for (int i = fTopIndex; i < idx; i++) { |
1484 | ySum += fItemData.fExpandedItems[i].fItemHeight; | |
837a2f8c | 1485 | } |
c004295c PT |
1486 | } else { |
1487 | for (int i = fTopIndex - 1; i >= idx; i--) { | |
1488 | ySum -= fItemData.fExpandedItems[i].fItemHeight; | |
1489 | } | |
837a2f8c | 1490 | } |
83b36bb4 | 1491 | int y = bounds.y + ySum; |
c004295c | 1492 | int height = fItemData.fExpandedItems[idx].fItemHeight; |
83b36bb4 | 1493 | return new Rectangle(bounds.x, y, bounds.width, height); |
837a2f8c PT |
1494 | } |
1495 | ||
1496 | @Override | |
1497 | void paint(Rectangle bounds, PaintEvent e) { | |
1498 | GC gc = e.gc; | |
837a2f8c | 1499 | |
f1fae91f | 1500 | if (bounds.width < 2 || bounds.height < 2 || null == fTimeProvider) { |
837a2f8c PT |
1501 | return; |
1502 | } | |
1503 | ||
f1fae91f PT |
1504 | fIdealNameSpace = 0; |
1505 | int nameSpace = fTimeProvider.getNameSpace(); | |
837a2f8c | 1506 | |
1c67fff3 | 1507 | // draw the background layer |
83b36bb4 | 1508 | drawBackground(bounds, nameSpace, gc); |
837a2f8c | 1509 | |
55d1fc96 PT |
1510 | // draw the grid lines |
1511 | drawGridLines(bounds, gc); | |
1512 | ||
56b43ab2 PT |
1513 | // draw the background markers |
1514 | drawMarkers(bounds, fTimeProvider, fMarkers, false, nameSpace, gc); | |
1515 | ||
1c67fff3 | 1516 | // draw the items |
f1fae91f | 1517 | drawItems(bounds, fTimeProvider, fItemData.fExpandedItems, fTopIndex, nameSpace, gc); |
1c67fff3 | 1518 | |
56b43ab2 PT |
1519 | // draw the foreground markers |
1520 | drawMarkers(bounds, fTimeProvider, fMarkers, true, nameSpace, gc); | |
1521 | ||
1c67fff3 | 1522 | // draw the links (arrows) |
bec1f1ac | 1523 | drawLinks(bounds, fTimeProvider, fItemData.fLinks, nameSpace, gc); |
1c67fff3 | 1524 | |
bec1f1ac | 1525 | fTimeGraphProvider.postDrawControl(bounds, gc); |
837a2f8c | 1526 | |
0fcf3b09 PT |
1527 | int alpha = gc.getAlpha(); |
1528 | gc.setAlpha(100); | |
1529 | ||
f1fae91f PT |
1530 | long time0 = fTimeProvider.getTime0(); |
1531 | long time1 = fTimeProvider.getTime1(); | |
baf92cac AM |
1532 | long selectionBegin = fTimeProvider.getSelectionBegin(); |
1533 | long selectionEnd = fTimeProvider.getSelectionEnd(); | |
837a2f8c | 1534 | double pixelsPerNanoSec = (bounds.width - nameSpace <= RIGHT_MARGIN) ? 0 : (double) (bounds.width - nameSpace - RIGHT_MARGIN) / (time1 - time0); |
0fcf3b09 PT |
1535 | int x0 = bounds.x + nameSpace + (int) ((selectionBegin - time0) * pixelsPerNanoSec); |
1536 | int x1 = bounds.x + nameSpace + (int) ((selectionEnd - time0) * pixelsPerNanoSec); | |
1537 | ||
1538 | // draw selection lines | |
1539 | if (fDragState != DRAG_SELECTION) { | |
f1fae91f | 1540 | gc.setForeground(getColorScheme().getColor(TimeGraphColorScheme.SELECTED_TIME)); |
0fcf3b09 PT |
1541 | if (x0 >= nameSpace && x0 < bounds.x + bounds.width) { |
1542 | gc.drawLine(x0, bounds.y, x0, bounds.y + bounds.height); | |
1543 | } | |
1544 | if (x1 != x0) { | |
1545 | if (x1 >= nameSpace && x1 < bounds.x + bounds.width) { | |
1546 | gc.drawLine(x1, bounds.y, x1, bounds.y + bounds.height); | |
1547 | } | |
1548 | } | |
837a2f8c PT |
1549 | } |
1550 | ||
0fcf3b09 PT |
1551 | // draw selection background |
1552 | if (selectionBegin != 0 && selectionEnd != 0 && fDragState != DRAG_SELECTION) { | |
1553 | x0 = Math.max(nameSpace, Math.min(bounds.x + bounds.width, x0)); | |
1554 | x1 = Math.max(nameSpace, Math.min(bounds.x + bounds.width, x1)); | |
1555 | gc.setBackground(getColorScheme().getBkColor(false, false, true)); | |
1556 | if (x1 - x0 > 1) { | |
1557 | gc.fillRectangle(new Rectangle(x0 + 1, bounds.y, x1 - x0 - 1, bounds.height)); | |
1558 | } else if (x0 - x1 > 1) { | |
1559 | gc.fillRectangle(new Rectangle(x1 + 1, bounds.y, x0 - x1 - 1, bounds.height)); | |
1560 | } | |
1561 | } | |
1562 | ||
1563 | // draw drag selection background | |
1564 | if (fDragState == DRAG_ZOOM || fDragState == DRAG_SELECTION) { | |
1565 | gc.setBackground(getColorScheme().getBkColor(false, false, true)); | |
1566 | if (fDragX0 < fDragX) { | |
1567 | gc.fillRectangle(new Rectangle(fDragX0, bounds.y, fDragX - fDragX0, bounds.height)); | |
1568 | } else if (fDragX0 > fDragX) { | |
1569 | gc.fillRectangle(new Rectangle(fDragX, bounds.y, fDragX0 - fDragX, bounds.height)); | |
1570 | } | |
1571 | } | |
1572 | ||
1573 | // draw drag line | |
f1fae91f PT |
1574 | if (DRAG_SPLIT_LINE == fDragState) { |
1575 | gc.setForeground(getColorScheme().getColor(TimeGraphColorScheme.BLACK)); | |
837a2f8c | 1576 | gc.drawLine(bounds.x + nameSpace, bounds.y, bounds.x + nameSpace, bounds.y + bounds.height - 1); |
0fcf3b09 | 1577 | } else if (DRAG_ZOOM == fDragState && Math.max(fDragX, fDragX0) > nameSpace) { |
f1fae91f PT |
1578 | gc.setForeground(getColorScheme().getColor(TimeGraphColorScheme.TOOL_FOREGROUND)); |
1579 | gc.drawLine(fDragX0, bounds.y, fDragX0, bounds.y + bounds.height - 1); | |
0fcf3b09 PT |
1580 | if (fDragX != fDragX0) { |
1581 | gc.drawLine(fDragX, bounds.y, fDragX, bounds.y + bounds.height - 1); | |
1582 | } | |
1583 | } else if (DRAG_SELECTION == fDragState && Math.max(fDragX, fDragX0) > nameSpace) { | |
1584 | gc.setForeground(getColorScheme().getColor(TimeGraphColorScheme.SELECTED_TIME)); | |
1585 | gc.drawLine(fDragX0, bounds.y, fDragX0, bounds.y + bounds.height - 1); | |
1586 | if (fDragX != fDragX0) { | |
1587 | gc.drawLine(fDragX, bounds.y, fDragX, bounds.y + bounds.height - 1); | |
1588 | } | |
f1fae91f PT |
1589 | } else if (DRAG_NONE == fDragState && fMouseOverSplitLine && fTimeProvider.getNameSpace() > 0) { |
1590 | gc.setForeground(getColorScheme().getColor(TimeGraphColorScheme.RED)); | |
837a2f8c PT |
1591 | gc.drawLine(bounds.x + nameSpace, bounds.y, bounds.x + nameSpace, bounds.y + bounds.height - 1); |
1592 | } | |
0fcf3b09 PT |
1593 | |
1594 | gc.setAlpha(alpha); | |
837a2f8c PT |
1595 | } |
1596 | ||
83b36bb4 | 1597 | /** |
1c67fff3 PT |
1598 | * Draw the background layer. Fills the background of the control's name |
1599 | * space and states space, updates the background of items if necessary, | |
1600 | * and draws the item's name text and middle line. | |
83b36bb4 PT |
1601 | * |
1602 | * @param bounds | |
1c67fff3 | 1603 | * The bounds of the control |
83b36bb4 | 1604 | * @param nameSpace |
1c67fff3 | 1605 | * The name space width |
83b36bb4 | 1606 | * @param gc |
1c67fff3 | 1607 | * Graphics context |
83b36bb4 PT |
1608 | * @since 2.0 |
1609 | */ | |
1610 | protected void drawBackground(Rectangle bounds, int nameSpace, GC gc) { | |
1611 | // draw empty name space background | |
1612 | gc.setBackground(getColorScheme().getBkColor(false, false, true)); | |
1613 | drawBackground(gc, bounds.x, bounds.y, nameSpace, bounds.height); | |
1614 | ||
1615 | // draw empty states space background | |
1616 | gc.setBackground(getColorScheme().getColor(TimeGraphColorScheme.BACKGROUND)); | |
1617 | drawBackground(gc, bounds.x + nameSpace, bounds.y, bounds.width - nameSpace, bounds.height); | |
1618 | ||
83b36bb4 PT |
1619 | for (int i = fTopIndex; i < fItemData.fExpandedItems.length; i++) { |
1620 | Rectangle itemRect = getItemRect(bounds, i); | |
1621 | if (itemRect.y >= bounds.y + bounds.height) { | |
1622 | break; | |
1623 | } | |
1624 | Item item = fItemData.fExpandedItems[i]; | |
1c67fff3 | 1625 | // draw the background of selected item and items with no time events |
83b36bb4 PT |
1626 | if (! item.fEntry.hasTimeEvents()) { |
1627 | gc.setBackground(getColorScheme().getBkColorGroup(item.fSelected, fIsInFocus)); | |
1628 | gc.fillRectangle(itemRect); | |
1629 | } else if (item.fSelected) { | |
1630 | gc.setBackground(getColorScheme().getBkColor(true, fIsInFocus, true)); | |
1631 | gc.fillRectangle(itemRect.x, itemRect.y, nameSpace, itemRect.height); | |
1632 | gc.setBackground(getColorScheme().getBkColor(true, fIsInFocus, false)); | |
1633 | gc.fillRectangle(nameSpace, itemRect.y, itemRect.width - nameSpace, itemRect.height); | |
1634 | } | |
1c67fff3 PT |
1635 | // draw the name and middle line |
1636 | if (! item.fEntry.hasTimeEvents()) { | |
1637 | drawName(item, itemRect, gc); | |
1638 | } else { | |
1639 | Rectangle nameRect = new Rectangle(itemRect.x, itemRect.y, nameSpace, itemRect.height); | |
1640 | drawName(item, nameRect, gc); | |
1641 | Rectangle rect = new Rectangle(nameSpace, itemRect.y, itemRect.width - nameSpace, itemRect.height); | |
1642 | drawMidLine(rect, gc); | |
1643 | } | |
83b36bb4 PT |
1644 | } |
1645 | } | |
1646 | ||
55d1fc96 PT |
1647 | /** |
1648 | * Draw the grid lines | |
1649 | * | |
1650 | * @param bounds | |
1c67fff3 | 1651 | * The bounds of the control |
55d1fc96 | 1652 | * @param gc |
1c67fff3 | 1653 | * Graphics context |
55d1fc96 PT |
1654 | * @since 2.0 |
1655 | */ | |
1656 | public void drawGridLines(Rectangle bounds, GC gc) { | |
1657 | if (!fGridLinesVisible) { | |
1658 | return; | |
1659 | } | |
1660 | gc.setForeground(fGridLineColor); | |
56b43ab2 | 1661 | gc.setAlpha(fGridLineColor.getAlpha()); |
55d1fc96 PT |
1662 | for (int x : fTimeGraphScale.getTickList()) { |
1663 | gc.drawLine(x, bounds.y, x, bounds.y + bounds.height); | |
1664 | } | |
56b43ab2 PT |
1665 | gc.setAlpha(255); |
1666 | } | |
1667 | ||
1668 | /** | |
1669 | * Draw the markers | |
1670 | * | |
1671 | * @param bounds | |
1672 | * The rectangle of the area | |
1673 | * @param timeProvider | |
1674 | * The time provider | |
1675 | * @param markers | |
1676 | * The list of markers | |
1677 | * @param foreground | |
1678 | * true to draw the foreground markers, false otherwise | |
1679 | * @param nameSpace | |
1680 | * The width reserved for the names | |
1681 | * @param gc | |
1682 | * Reference to the SWT GC object | |
1683 | * @since 2.0 | |
1684 | */ | |
1685 | protected void drawMarkers(Rectangle bounds, ITimeDataProvider timeProvider, List<IMarkerEvent> markers, boolean foreground, int nameSpace, GC gc) { | |
1686 | if (!fMarkersVisible || markers == null || markers.isEmpty()) { | |
1687 | return; | |
1688 | } | |
1689 | gc.setClipping(new Rectangle(nameSpace, 0, bounds.width - nameSpace, bounds.height)); | |
1690 | /* the list can grow concurrently but cannot shrink */ | |
1691 | for (int i = 0; i < markers.size(); i++) { | |
1692 | IMarkerEvent marker = markers.get(i); | |
1693 | if (marker.isForeground() == foreground) { | |
1694 | drawMarker(marker, bounds, timeProvider, nameSpace, gc); | |
1695 | } | |
1696 | } | |
1697 | gc.setClipping((Rectangle) null); | |
1698 | } | |
1699 | ||
1700 | /** | |
1701 | * Draw a single marker | |
1702 | * | |
1703 | * @param marker | |
1704 | * The marker event | |
1705 | * @param bounds | |
1706 | * The bounds of the control | |
1707 | * @param timeProvider | |
1708 | * The time provider | |
1709 | * @param nameSpace | |
1710 | * The width reserved for the name | |
1711 | * @param gc | |
1712 | * Reference to the SWT GC object | |
1713 | * @since 2.0 | |
1714 | */ | |
1715 | protected void drawMarker(IMarkerEvent marker, Rectangle bounds, ITimeDataProvider timeProvider, int nameSpace, GC gc) { | |
1716 | Rectangle rect = Utils.clone(bounds); | |
1717 | if (marker.getEntry() != null) { | |
1718 | int index = fItemData.findItemIndex(marker.getEntry()); | |
1719 | if (index == -1) { | |
1720 | return; | |
1721 | } | |
1722 | rect = getStatesRect(bounds, index, nameSpace); | |
1723 | if (rect.y < 0 || rect.y > bounds.height) { | |
1724 | return; | |
1725 | } | |
1726 | } | |
1727 | int x0 = getXForTime(marker.getTime()); | |
1728 | int x1 = getXForTime(marker.getTime() + marker.getDuration()); | |
1729 | if (x0 > bounds.width || x1 < nameSpace) { | |
1730 | return; | |
1731 | } | |
1732 | rect.x = Math.max(nameSpace, Math.min(bounds.width, x0)); | |
1733 | rect.width = Math.max(1, Math.min(bounds.width, x1) - rect.x); | |
1734 | ||
8910dea2 PT |
1735 | Color color = getColorScheme().getColor(marker.getColor()); |
1736 | gc.setBackground(color); | |
1737 | gc.setAlpha(color.getAlpha()); | |
56b43ab2 PT |
1738 | gc.fillRectangle(rect); |
1739 | gc.setAlpha(255); | |
1740 | String label = marker.getLabel(); | |
1741 | if (label != null && marker.getEntry() != null) { | |
1d012443 | 1742 | label = label.substring(0, Math.min(label.indexOf('\n') != -1 ? label.indexOf('\n') : label.length(), MAX_LABEL_LENGTH)); |
8910dea2 | 1743 | gc.setForeground(color); |
1d012443 | 1744 | Utils.drawText(gc, label, rect.x - gc.textExtent(label).x, rect.y, true); |
56b43ab2 | 1745 | } |
55d1fc96 PT |
1746 | } |
1747 | ||
837a2f8c PT |
1748 | /** |
1749 | * Draw many items at once | |
1750 | * | |
1751 | * @param bounds | |
1c67fff3 | 1752 | * The bounds of the control |
837a2f8c PT |
1753 | * @param timeProvider |
1754 | * The time provider | |
1755 | * @param items | |
1756 | * The array items to draw | |
1757 | * @param topIndex | |
1758 | * The index of the first element to draw | |
1759 | * @param nameSpace | |
1c67fff3 | 1760 | * The name space width |
837a2f8c | 1761 | * @param gc |
1c67fff3 | 1762 | * Graphics context |
837a2f8c PT |
1763 | */ |
1764 | public void drawItems(Rectangle bounds, ITimeDataProvider timeProvider, | |
1765 | Item[] items, int topIndex, int nameSpace, GC gc) { | |
1766 | for (int i = topIndex; i < items.length; i++) { | |
1767 | Item item = items[i]; | |
1768 | drawItem(item, bounds, timeProvider, i, nameSpace, gc); | |
1769 | } | |
837a2f8c PT |
1770 | } |
1771 | ||
1772 | /** | |
1773 | * Draws the item | |
1774 | * | |
1c67fff3 PT |
1775 | * @param item |
1776 | * The item to draw | |
1777 | * @param bounds | |
1778 | * The bounds of the control | |
1779 | * @param timeProvider | |
1780 | * The time provider | |
1781 | * @param i | |
1782 | * The expanded item index | |
1783 | * @param nameSpace | |
1784 | * The name space width | |
1785 | * @param gc | |
1786 | * Graphics context | |
837a2f8c PT |
1787 | */ |
1788 | protected void drawItem(Item item, Rectangle bounds, ITimeDataProvider timeProvider, int i, int nameSpace, GC gc) { | |
83b36bb4 PT |
1789 | Rectangle itemRect = getItemRect(bounds, i); |
1790 | if (itemRect.y >= bounds.y + bounds.height) { | |
1791 | return; | |
1792 | } | |
1793 | ||
70e10acc | 1794 | ITimeGraphEntry entry = item.fEntry; |
837a2f8c PT |
1795 | long time0 = timeProvider.getTime0(); |
1796 | long time1 = timeProvider.getTime1(); | |
33fa1fc7 | 1797 | long selectedTime = fTimeProvider.getSelectionEnd(); |
837a2f8c | 1798 | |
83b36bb4 | 1799 | Rectangle rect = new Rectangle(nameSpace, itemRect.y, itemRect.width - nameSpace, itemRect.height); |
1c67fff3 | 1800 | if (rect.isEmpty() || (time1 <= time0)) { |
837a2f8c PT |
1801 | fTimeGraphProvider.postDrawEntry(entry, rect, gc); |
1802 | return; | |
1803 | } | |
1804 | ||
f1fae91f | 1805 | boolean selected = item.fSelected; |
837a2f8c PT |
1806 | // K pixels per second |
1807 | double pixelsPerNanoSec = (rect.width <= RIGHT_MARGIN) ? 0 : (double) (rect.width - RIGHT_MARGIN) / (time1 - time0); | |
1808 | ||
70e10acc | 1809 | if (item.fEntry.hasTimeEvents()) { |
ab0a54d5 | 1810 | gc.setClipping(new Rectangle(nameSpace, 0, bounds.width - nameSpace, bounds.height)); |
837a2f8c | 1811 | fillSpace(rect, gc, selected); |
83d0971d PT |
1812 | |
1813 | int margins = getMarginForHeight(rect.height); | |
1814 | int height = rect.height - margins; | |
1815 | int topMargin = (margins + 1) / 2; | |
1816 | Rectangle stateRect = new Rectangle(rect.x, rect.y + topMargin, rect.width, height); | |
1817 | ||
1818 | /* Set the font for this item */ | |
1819 | setFontForHeight(height, gc); | |
837a2f8c PT |
1820 | |
1821 | long maxDuration = (timeProvider.getTimeSpace() == 0) ? Long.MAX_VALUE : 1 * (time1 - time0) / timeProvider.getTimeSpace(); | |
1822 | Iterator<ITimeEvent> iterator = entry.getTimeEventsIterator(time0, time1, maxDuration); | |
1823 | ||
1824 | int lastX = -1; | |
1825 | while (iterator.hasNext()) { | |
1826 | ITimeEvent event = iterator.next(); | |
1827 | int x = rect.x + (int) ((event.getTime() - time0) * pixelsPerNanoSec); | |
1828 | int xEnd = rect.x + (int) ((event.getTime() + event.getDuration() - time0) * pixelsPerNanoSec); | |
1829 | if (x >= rect.x + rect.width || xEnd < rect.x) { | |
1830 | // event is out of bounds | |
1831 | continue; | |
1832 | } | |
1833 | xEnd = Math.min(rect.x + rect.width, xEnd); | |
1834 | stateRect.x = Math.max(rect.x, x); | |
1835 | stateRect.width = Math.max(0, xEnd - stateRect.x + 1); | |
1836 | if (stateRect.x == lastX) { | |
1837 | stateRect.width -= 1; | |
1838 | if (stateRect.width > 0) { | |
1839 | gc.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK)); | |
1840 | gc.drawPoint(stateRect.x, stateRect.y - 2); | |
1841 | stateRect.x += 1; | |
1842 | } | |
837a2f8c PT |
1843 | } |
1844 | boolean timeSelected = selectedTime >= event.getTime() && selectedTime < event.getTime() + event.getDuration(); | |
f1fae91f | 1845 | if (drawState(getColorScheme(), event, stateRect, gc, selected, timeSelected)) { |
b052914f | 1846 | lastX = stateRect.x; |
ad128fd8 | 1847 | } |
837a2f8c | 1848 | } |
ab0a54d5 | 1849 | gc.setClipping((Rectangle) null); |
837a2f8c PT |
1850 | } |
1851 | fTimeGraphProvider.postDrawEntry(entry, rect, gc); | |
1852 | } | |
1853 | ||
bec1f1ac GB |
1854 | /** |
1855 | * Draw the links | |
1856 | * | |
1857 | * @param bounds | |
1c67fff3 | 1858 | * The bounds of the control |
bec1f1ac GB |
1859 | * @param timeProvider |
1860 | * The time provider | |
1861 | * @param links | |
6ae6c5bd | 1862 | * The list of link events |
bec1f1ac | 1863 | * @param nameSpace |
1c67fff3 | 1864 | * The name space width |
bec1f1ac | 1865 | * @param gc |
1c67fff3 | 1866 | * Graphics context |
bec1f1ac GB |
1867 | */ |
1868 | public void drawLinks(Rectangle bounds, ITimeDataProvider timeProvider, | |
1869 | List<ILinkEvent> links, int nameSpace, GC gc) { | |
79ec0b89 PT |
1870 | if (fHideArrows) { |
1871 | return; | |
1872 | } | |
ab0a54d5 | 1873 | gc.setClipping(new Rectangle(nameSpace, 0, bounds.width - nameSpace, bounds.height)); |
6ae6c5bd PT |
1874 | /* the list can grow concurrently but cannot shrink */ |
1875 | for (int i = 0; i < links.size(); i++) { | |
1876 | drawLink(links.get(i), bounds, timeProvider, nameSpace, gc); | |
bec1f1ac | 1877 | } |
ab0a54d5 | 1878 | gc.setClipping((Rectangle) null); |
bec1f1ac GB |
1879 | } |
1880 | ||
1881 | /** | |
1c67fff3 | 1882 | * Draws a link type event |
bec1f1ac GB |
1883 | * |
1884 | * @param event | |
1c67fff3 | 1885 | * The link event to draw |
bec1f1ac | 1886 | * @param bounds |
1c67fff3 | 1887 | * The bounds of the control |
bec1f1ac | 1888 | * @param timeProvider |
1c67fff3 | 1889 | * The time provider |
bec1f1ac | 1890 | * @param nameSpace |
1c67fff3 | 1891 | * The name space width |
bec1f1ac GB |
1892 | * @param gc |
1893 | * Graphics context | |
bec1f1ac GB |
1894 | */ |
1895 | protected void drawLink(ILinkEvent event, Rectangle bounds, ITimeDataProvider timeProvider, int nameSpace, GC gc) { | |
1053eaa6 PT |
1896 | drawArrow(getColorScheme(), event, getArrowRectangle(bounds, event), gc); |
1897 | } | |
1898 | ||
1899 | private Rectangle getArrowRectangle(Rectangle bounds, ILinkEvent event) { | |
bec1f1ac GB |
1900 | int srcIndex = fItemData.findItemIndex(event.getEntry()); |
1901 | int destIndex = fItemData.findItemIndex(event.getDestinationEntry()); | |
1902 | ||
1903 | if ((srcIndex == -1) || (destIndex == -1)) { | |
1053eaa6 | 1904 | return null; |
bec1f1ac GB |
1905 | } |
1906 | ||
1053eaa6 PT |
1907 | Rectangle src = getStatesRect(bounds, srcIndex, fTimeProvider.getNameSpace()); |
1908 | Rectangle dst = getStatesRect(bounds, destIndex, fTimeProvider.getNameSpace()); | |
bec1f1ac GB |
1909 | |
1910 | int x0 = getXForTime(event.getTime()); | |
1911 | int x1 = getXForTime(event.getTime() + event.getDuration()); | |
03ab8eeb PT |
1912 | |
1913 | // limit the x-coordinates to prevent integer overflow in calculations | |
1914 | // and also GC.drawLine doesn't draw properly with large coordinates | |
1915 | final int limit = Integer.MAX_VALUE / 1024; | |
1916 | x0 = Math.max(-limit, Math.min(x0, limit)); | |
1917 | x1 = Math.max(-limit, Math.min(x1, limit)); | |
1918 | ||
bec1f1ac GB |
1919 | int y0 = src.y + src.height / 2; |
1920 | int y1 = dst.y + dst.height / 2; | |
1053eaa6 | 1921 | return new Rectangle(x0, y0, x1 - x0, y1 - y0); |
bec1f1ac GB |
1922 | } |
1923 | ||
1924 | /** | |
1c67fff3 | 1925 | * Draw an arrow |
bec1f1ac GB |
1926 | * |
1927 | * @param colors | |
1928 | * Color scheme | |
1929 | * @param event | |
1c67fff3 | 1930 | * Time event for which we're drawing the arrow |
bec1f1ac | 1931 | * @param rect |
1c67fff3 | 1932 | * The arrow rectangle |
bec1f1ac GB |
1933 | * @param gc |
1934 | * Graphics context | |
1c67fff3 | 1935 | * @return true if the arrow was drawn |
bec1f1ac GB |
1936 | */ |
1937 | protected boolean drawArrow(TimeGraphColorScheme colors, ITimeEvent event, | |
1938 | Rectangle rect, GC gc) { | |
1939 | ||
a5e66312 PT |
1940 | if (rect == null) { |
1941 | return false; | |
1942 | } | |
bec1f1ac GB |
1943 | int colorIdx = fTimeGraphProvider.getStateTableIndex(event); |
1944 | if (colorIdx < 0) { | |
1945 | return false; | |
1946 | } | |
1947 | boolean visible = ((rect.height == 0) && (rect.width == 0)) ? false : true; | |
1948 | ||
1949 | if (visible) { | |
1950 | Color stateColor = null; | |
1951 | if (colorIdx < fEventColorMap.length) { | |
1952 | stateColor = fEventColorMap[colorIdx]; | |
1953 | } else { | |
1954 | stateColor = Display.getDefault().getSystemColor(SWT.COLOR_BLACK); | |
1955 | } | |
1956 | ||
1957 | gc.setForeground(stateColor); | |
1958 | gc.setBackground(stateColor); | |
1959 | ||
1960 | /* Draw the arrow */ | |
1961 | gc.drawLine(rect.x, rect.y, rect.x + rect.width, rect.y + rect.height); | |
1962 | drawArrowHead(rect.x, rect.y, rect.x + rect.width, rect.y + rect.height, gc); | |
1963 | ||
1964 | } | |
1965 | fTimeGraphProvider.postDrawEvent(event, rect, gc); | |
1966 | return visible; | |
1967 | } | |
1968 | ||
1969 | /* | |
1970 | * @author Francis Giraldeau | |
1971 | * | |
1972 | * Inspiration: | |
1973 | * http://stackoverflow.com/questions/3010803/draw-arrow-on-line-algorithm | |
1974 | * | |
1975 | * The algorithm was taken from this site, not the code itself | |
1976 | */ | |
1977 | private static void drawArrowHead(int x0, int y0, int x1, int y1, GC gc) | |
1978 | { | |
1979 | int factor = 10; | |
1980 | double cos = 0.9510; | |
1981 | double sin = 0.3090; | |
03ab8eeb PT |
1982 | long lenx = x1 - x0; |
1983 | long leny = y1 - y0; | |
bec1f1ac GB |
1984 | double len = Math.sqrt(lenx * lenx + leny * leny); |
1985 | ||
1986 | double dx = factor * lenx / len; | |
1987 | double dy = factor * leny / len; | |
1988 | int end1X = (int) Math.round((x1 - (dx * cos + dy * -sin))); | |
1989 | int end1Y = (int) Math.round((y1 - (dx * sin + dy * cos))); | |
1990 | int end2X = (int) Math.round((x1 - (dx * cos + dy * sin))); | |
1991 | int end2Y = (int) Math.round((y1 - (dx * -sin + dy * cos))); | |
1992 | int[] arrow = new int[] { x1, y1, end1X, end1Y, end2X, end2Y, x1, y1 }; | |
1993 | gc.fillPolygon(arrow); | |
1994 | } | |
1995 | ||
a0a88f65 AM |
1996 | /** |
1997 | * Draw the name of an item. | |
1998 | * | |
1999 | * @param item | |
2000 | * Item object | |
2001 | * @param bounds | |
1c67fff3 | 2002 | * The bounds of the item's name space |
a0a88f65 AM |
2003 | * @param gc |
2004 | * Graphics context | |
2005 | */ | |
837a2f8c | 2006 | protected void drawName(Item item, Rectangle bounds, GC gc) { |
70e10acc | 2007 | boolean hasTimeEvents = item.fEntry.hasTimeEvents(); |
837a2f8c PT |
2008 | |
2009 | // No name to be drawn | |
f1fae91f | 2010 | if (fTimeProvider.getNameSpace() == 0) { |
837a2f8c PT |
2011 | return; |
2012 | } | |
2013 | ||
83d0971d PT |
2014 | int height = bounds.height - getMarginForHeight(bounds.height); |
2015 | setFontForHeight(height, gc); | |
2016 | ||
f1fae91f PT |
2017 | int leftMargin = MARGIN + item.fLevel * EXPAND_SIZE; |
2018 | if (item.fHasChildren) { | |
2019 | gc.setForeground(getColorScheme().getFgColorGroup(false, false)); | |
2020 | gc.setBackground(getColorScheme().getBkColor(false, false, false)); | |
837a2f8c PT |
2021 | Rectangle rect = Utils.clone(bounds); |
2022 | rect.x += leftMargin; | |
2023 | rect.y += (bounds.height - EXPAND_SIZE) / 2; | |
2024 | rect.width = EXPAND_SIZE; | |
2025 | rect.height = EXPAND_SIZE; | |
2026 | gc.fillRectangle(rect); | |
2027 | gc.drawRectangle(rect.x, rect.y, rect.width - 1, rect.height - 1); | |
2028 | int midy = rect.y + rect.height / 2; | |
2029 | gc.drawLine(rect.x + 2, midy, rect.x + rect.width - 3, midy); | |
f1fae91f | 2030 | if (!item.fExpanded) { |
837a2f8c PT |
2031 | int midx = rect.x + rect.width / 2; |
2032 | gc.drawLine(midx, rect.y + 2, midx, rect.y + rect.height - 3); | |
2033 | } | |
2034 | } | |
2035 | leftMargin += EXPAND_SIZE + MARGIN; | |
2036 | ||
70e10acc | 2037 | Image img = fTimeGraphProvider.getItemImage(item.fEntry); |
837a2f8c PT |
2038 | if (img != null) { |
2039 | // draw icon | |
2040 | int imgHeight = img.getImageData().height; | |
2041 | int imgWidth = img.getImageData().width; | |
2042 | int x = leftMargin; | |
2043 | int y = bounds.y + (bounds.height - imgHeight) / 2; | |
2044 | gc.drawImage(img, x, y); | |
2045 | leftMargin += imgWidth + MARGIN; | |
2046 | } | |
f1fae91f | 2047 | String name = item.fName; |
837a2f8c | 2048 | Point size = gc.stringExtent(name); |
f1fae91f PT |
2049 | if (fIdealNameSpace < leftMargin + size.x + MARGIN) { |
2050 | fIdealNameSpace = leftMargin + size.x + MARGIN; | |
837a2f8c PT |
2051 | } |
2052 | if (hasTimeEvents) { | |
2053 | // cut long string with "..." | |
2054 | int width = bounds.width - leftMargin; | |
2055 | int cuts = 0; | |
2056 | while (size.x > width && name.length() > 1) { | |
2057 | cuts++; | |
2058 | name = name.substring(0, name.length() - 1); | |
2059 | size = gc.stringExtent(name + "..."); //$NON-NLS-1$ | |
2060 | } | |
2061 | if (cuts > 0) { | |
2062 | name += "..."; //$NON-NLS-1$ | |
2063 | } | |
2064 | } | |
2065 | Rectangle rect = Utils.clone(bounds); | |
2066 | rect.x += leftMargin; | |
2067 | rect.width -= leftMargin; | |
2068 | // draw text | |
2069 | if (rect.width > 0) { | |
2070 | rect.y += (bounds.height - gc.stringExtent(name).y) / 2; | |
f1fae91f | 2071 | gc.setForeground(getColorScheme().getFgColor(item.fSelected, fIsInFocus)); |
837a2f8c PT |
2072 | int textWidth = Utils.drawText(gc, name, rect, true); |
2073 | leftMargin += textWidth + MARGIN; | |
837a2f8c PT |
2074 | |
2075 | if (hasTimeEvents) { | |
2076 | // draw middle line | |
1c67fff3 PT |
2077 | rect.x = bounds.x + leftMargin; |
2078 | rect.y = bounds.y; | |
2079 | rect.width = bounds.width - rect.x; | |
2080 | drawMidLine(rect, gc); | |
837a2f8c PT |
2081 | } |
2082 | } | |
2083 | } | |
2084 | ||
a0a88f65 AM |
2085 | /** |
2086 | * Draw the state (color fill) | |
2087 | * | |
2088 | * @param colors | |
2089 | * Color scheme | |
2090 | * @param event | |
2091 | * Time event for which we're drawing the state | |
2092 | * @param rect | |
1c67fff3 | 2093 | * The state rectangle |
a0a88f65 AM |
2094 | * @param gc |
2095 | * Graphics context | |
2096 | * @param selected | |
2097 | * Is this time event currently selected (so it appears | |
2098 | * highlighted) | |
2099 | * @param timeSelected | |
2100 | * Is the timestamp currently selected | |
ad128fd8 | 2101 | * @return true if the state was drawn |
a0a88f65 | 2102 | */ |
ad128fd8 | 2103 | protected boolean drawState(TimeGraphColorScheme colors, ITimeEvent event, |
837a2f8c PT |
2104 | Rectangle rect, GC gc, boolean selected, boolean timeSelected) { |
2105 | ||
2106 | int colorIdx = fTimeGraphProvider.getStateTableIndex(event); | |
ad128fd8 PT |
2107 | if (colorIdx < 0 && colorIdx != ITimeGraphPresentationProvider.TRANSPARENT) { |
2108 | return false; | |
837a2f8c PT |
2109 | } |
2110 | boolean visible = rect.width == 0 ? false : true; | |
b052914f | 2111 | rect.width = Math.max(1, rect.width); |
351a2391 XR |
2112 | Color black = Display.getDefault().getSystemColor(SWT.COLOR_BLACK); |
2113 | gc.setForeground(black); | |
837a2f8c | 2114 | |
b052914f PT |
2115 | if (colorIdx == ITimeGraphPresentationProvider.TRANSPARENT) { |
2116 | if (visible) { | |
ad128fd8 | 2117 | // Only draw the top and bottom borders |
ad128fd8 PT |
2118 | gc.drawLine(rect.x, rect.y, rect.x + rect.width - 1, rect.y); |
2119 | gc.drawLine(rect.x, rect.y + rect.height - 1, rect.x + rect.width - 1, rect.y + rect.height - 1); | |
2120 | if (rect.width == 1) { | |
ad128fd8 PT |
2121 | gc.drawPoint(rect.x, rect.y - 2); |
2122 | } | |
837a2f8c | 2123 | } |
b052914f PT |
2124 | fTimeGraphProvider.postDrawEvent(event, rect, gc); |
2125 | return false; | |
2126 | } | |
2127 | Color stateColor = null; | |
2128 | if (colorIdx < fEventColorMap.length) { | |
2129 | stateColor = fEventColorMap[colorIdx]; | |
2130 | } else { | |
2131 | stateColor = black; | |
2132 | } | |
837a2f8c | 2133 | |
b052914f PT |
2134 | boolean reallySelected = timeSelected && selected; |
2135 | // fill all rect area | |
2136 | gc.setBackground(stateColor); | |
2137 | if (visible) { | |
837a2f8c | 2138 | gc.fillRectangle(rect); |
b052914f PT |
2139 | } else if (fBlendSubPixelEvents) { |
2140 | gc.setAlpha(128); | |
2141 | gc.fillRectangle(rect); | |
2142 | gc.setAlpha(255); | |
2143 | } | |
837a2f8c | 2144 | |
b052914f PT |
2145 | if (reallySelected) { |
2146 | gc.drawLine(rect.x, rect.y - 1, rect.x + rect.width - 1, rect.y - 1); | |
2147 | gc.drawLine(rect.x, rect.y + rect.height, rect.x + rect.width - 1, rect.y + rect.height); | |
2148 | } | |
2149 | if (!visible) { | |
837a2f8c | 2150 | gc.drawPoint(rect.x, rect.y - 2); |
837a2f8c PT |
2151 | } |
2152 | fTimeGraphProvider.postDrawEvent(event, rect, gc); | |
ad128fd8 | 2153 | return visible; |
837a2f8c PT |
2154 | } |
2155 | ||
a0a88f65 | 2156 | /** |
83b36bb4 | 2157 | * Fill an item's states rectangle |
a0a88f65 AM |
2158 | * |
2159 | * @param rect | |
1c67fff3 | 2160 | * The states rectangle |
a0a88f65 AM |
2161 | * @param gc |
2162 | * Graphics context | |
2163 | * @param selected | |
83b36bb4 | 2164 | * true if the item is selected |
a0a88f65 | 2165 | */ |
837a2f8c | 2166 | protected void fillSpace(Rectangle rect, GC gc, boolean selected) { |
1c67fff3 PT |
2167 | /* Nothing to draw */ |
2168 | } | |
2169 | ||
2170 | /** | |
2171 | * Draw a line at the middle height of a rectangle | |
2172 | * | |
2173 | * @param rect | |
2174 | * The rectangle | |
2175 | * @param gc | |
2176 | * Graphics context | |
2177 | */ | |
2178 | private void drawMidLine(Rectangle rect, GC gc) { | |
f1fae91f | 2179 | gc.setForeground(getColorScheme().getColor(TimeGraphColorScheme.MID_LINE)); |
837a2f8c PT |
2180 | int midy = rect.y + rect.height / 2; |
2181 | gc.drawLine(rect.x, midy, rect.x + rect.width, midy); | |
2182 | } | |
2183 | ||
83d0971d PT |
2184 | private static int getMarginForHeight(int height) { |
2185 | /* | |
2186 | * State rectangle is smaller than the item bounds when height is > 4. | |
2187 | * Don't use any margin if the height is below or equal that threshold. | |
2188 | * Use a maximum of 6 pixels for both margins, otherwise try to use 13 | |
2189 | * pixels for the state height, but with a minimum margin of 1. | |
2190 | */ | |
2191 | final int MARGIN_THRESHOLD = 4; | |
2192 | final int PREFERRED_HEIGHT = 13; | |
2193 | final int MIN_MARGIN = 1; | |
2194 | final int MAX_MARGIN = 6; | |
2195 | return height <= MARGIN_THRESHOLD ? 0 : | |
2196 | Math.max(Math.min(height - PREFERRED_HEIGHT, MAX_MARGIN), MIN_MARGIN); | |
2197 | } | |
2198 | ||
2199 | private void setFontForHeight(int pixels, GC gc) { | |
2200 | /* convert font height from pixels to points */ | |
2201 | int height = Math.max(pixels * PPI / DPI, 1); | |
2202 | Font font = fFonts.get(height); | |
2203 | if (font == null) { | |
2204 | FontData fontData = gc.getFont().getFontData()[0]; | |
2205 | fontData.setHeight(height); | |
2206 | font = new Font(gc.getDevice(), fontData); | |
2207 | fFonts.put(height, font); | |
2208 | } | |
2209 | gc.setFont(font); | |
2210 | } | |
2211 | ||
837a2f8c PT |
2212 | @Override |
2213 | public void keyTraversed(TraverseEvent e) { | |
2214 | if ((e.detail == SWT.TRAVERSE_TAB_NEXT) || (e.detail == SWT.TRAVERSE_TAB_PREVIOUS)) { | |
2215 | e.doit = true; | |
2216 | } | |
2217 | } | |
2218 | ||
2219 | @Override | |
2220 | public void keyPressed(KeyEvent e) { | |
2221 | int idx = -1; | |
f1fae91f | 2222 | if (fItemData.fExpandedItems.length == 0) { |
837a2f8c PT |
2223 | return; |
2224 | } | |
2225 | if (SWT.HOME == e.keyCode) { | |
2226 | idx = 0; | |
2227 | } else if (SWT.END == e.keyCode) { | |
f1fae91f | 2228 | idx = fItemData.fExpandedItems.length - 1; |
837a2f8c PT |
2229 | } else if (SWT.ARROW_DOWN == e.keyCode) { |
2230 | idx = getSelectedIndex(); | |
2231 | if (idx < 0) { | |
2232 | idx = 0; | |
f1fae91f | 2233 | } else if (idx < fItemData.fExpandedItems.length - 1) { |
837a2f8c PT |
2234 | idx++; |
2235 | } | |
2236 | } else if (SWT.ARROW_UP == e.keyCode) { | |
2237 | idx = getSelectedIndex(); | |
2238 | if (idx < 0) { | |
2239 | idx = 0; | |
2240 | } else if (idx > 0) { | |
2241 | idx--; | |
2242 | } | |
33fa1fc7 PT |
2243 | } else if (SWT.ARROW_LEFT == e.keyCode && fDragState == DRAG_NONE) { |
2244 | boolean extend = (e.stateMask & SWT.SHIFT) != 0; | |
2245 | selectPrevEvent(extend); | |
2246 | } else if (SWT.ARROW_RIGHT == e.keyCode && fDragState == DRAG_NONE) { | |
2247 | boolean extend = (e.stateMask & SWT.SHIFT) != 0; | |
2248 | selectNextEvent(extend); | |
837a2f8c PT |
2249 | } else if (SWT.PAGE_DOWN == e.keyCode) { |
2250 | int page = countPerPage(); | |
2251 | idx = getSelectedIndex(); | |
2252 | if (idx < 0) { | |
2253 | idx = 0; | |
2254 | } | |
2255 | idx += page; | |
f1fae91f PT |
2256 | if (idx >= fItemData.fExpandedItems.length) { |
2257 | idx = fItemData.fExpandedItems.length - 1; | |
837a2f8c PT |
2258 | } |
2259 | } else if (SWT.PAGE_UP == e.keyCode) { | |
2260 | int page = countPerPage(); | |
2261 | idx = getSelectedIndex(); | |
2262 | if (idx < 0) { | |
2263 | idx = 0; | |
2264 | } | |
2265 | idx -= page; | |
2266 | if (idx < 0) { | |
2267 | idx = 0; | |
2268 | } | |
2269 | } else if (SWT.CR == e.keyCode) { | |
2270 | idx = getSelectedIndex(); | |
2271 | if (idx >= 0) { | |
f1fae91f | 2272 | if (fItemData.fExpandedItems[idx].fHasChildren) { |
837a2f8c PT |
2273 | toggle(idx); |
2274 | } else { | |
2275 | fireDefaultSelection(); | |
2276 | } | |
2277 | } | |
2278 | idx = -1; | |
3bd20aa6 | 2279 | } else if ((e.character == '+' || e.character == '=') && ((e.stateMask & SWT.CTRL) != 0)) { |
48072ae3 | 2280 | fVerticalZoomAlignEntry = getVerticalZoomAlignSelection(); |
83d0971d | 2281 | verticalZoom(true); |
48072ae3 PT |
2282 | if (fVerticalZoomAlignEntry != null) { |
2283 | setElementPosition(fVerticalZoomAlignEntry.getKey(), fVerticalZoomAlignEntry.getValue()); | |
2284 | } | |
3bd20aa6 | 2285 | } else if (e.character == '-' && ((e.stateMask & SWT.CTRL) != 0)) { |
48072ae3 | 2286 | fVerticalZoomAlignEntry = getVerticalZoomAlignSelection(); |
83d0971d | 2287 | verticalZoom(false); |
48072ae3 PT |
2288 | if (fVerticalZoomAlignEntry != null) { |
2289 | setElementPosition(fVerticalZoomAlignEntry.getKey(), fVerticalZoomAlignEntry.getValue()); | |
2290 | } | |
3bd20aa6 | 2291 | } else if (e.character == '0' && ((e.stateMask & SWT.CTRL) != 0)) { |
48072ae3 | 2292 | fVerticalZoomAlignEntry = getVerticalZoomAlignSelection(); |
83d0971d | 2293 | resetVerticalZoom(); |
48072ae3 PT |
2294 | if (fVerticalZoomAlignEntry != null) { |
2295 | setElementPosition(fVerticalZoomAlignEntry.getKey(), fVerticalZoomAlignEntry.getValue()); | |
2296 | } | |
837a2f8c PT |
2297 | } |
2298 | if (idx >= 0) { | |
2299 | selectItem(idx, false); | |
2300 | fireSelectionChanged(); | |
2301 | } | |
0fcf3b09 PT |
2302 | int x = toControl(e.display.getCursorLocation()).x; |
2303 | updateCursor(x, e.stateMask | e.keyCode); | |
837a2f8c PT |
2304 | } |
2305 | ||
2306 | @Override | |
2307 | public void keyReleased(KeyEvent e) { | |
0fcf3b09 PT |
2308 | int x = toControl(e.display.getCursorLocation()).x; |
2309 | updateCursor(x, e.stateMask & ~e.keyCode); | |
837a2f8c PT |
2310 | } |
2311 | ||
2312 | @Override | |
2313 | public void focusGained(FocusEvent e) { | |
f1fae91f | 2314 | fIsInFocus = true; |
837a2f8c | 2315 | redraw(); |
33fa1fc7 | 2316 | updateStatusLine(STATUS_WITHOUT_CURSOR_TIME); |
837a2f8c PT |
2317 | } |
2318 | ||
2319 | @Override | |
2320 | public void focusLost(FocusEvent e) { | |
f1fae91f | 2321 | fIsInFocus = false; |
f1fae91f | 2322 | if (DRAG_NONE != fDragState) { |
837a2f8c | 2323 | setCapture(false); |
f1fae91f | 2324 | fDragState = DRAG_NONE; |
837a2f8c PT |
2325 | } |
2326 | redraw(); | |
0fcf3b09 | 2327 | updateStatusLine(NO_STATUS); |
837a2f8c PT |
2328 | } |
2329 | ||
2330 | /** | |
2331 | * @return If the current view is focused | |
2332 | */ | |
2333 | public boolean isInFocus() { | |
f1fae91f | 2334 | return fIsInFocus; |
837a2f8c PT |
2335 | } |
2336 | ||
2337 | /** | |
2338 | * Provide the possibility to control the wait cursor externally e.g. data | |
2339 | * requests in progress | |
2340 | * | |
2341 | * @param waitInd Should we wait indefinitely? | |
2342 | */ | |
2343 | public void waitCursor(boolean waitInd) { | |
2344 | // Update cursor as indicated | |
2345 | if (waitInd) { | |
f1fae91f | 2346 | setCursor(fWaitCursor); |
837a2f8c PT |
2347 | } else { |
2348 | setCursor(null); | |
837a2f8c | 2349 | } |
0fcf3b09 | 2350 | } |
837a2f8c | 2351 | |
0fcf3b09 PT |
2352 | private void updateCursor(int x, int stateMask) { |
2353 | // if Wait cursor not active, check for the need to change the cursor | |
2354 | if (getCursor() == fWaitCursor) { | |
2355 | return; | |
2356 | } | |
2357 | Cursor cursor = null; | |
2358 | if (fDragState == DRAG_SPLIT_LINE) { | |
2359 | } else if (fDragState == DRAG_SELECTION) { | |
2360 | cursor = fResizeCursor; | |
2361 | } else if (fDragState == DRAG_TRACE_ITEM) { | |
2362 | cursor = fDragCursor; | |
2363 | } else if (fDragState == DRAG_ZOOM) { | |
2364 | cursor = fZoomCursor; | |
2365 | } else if ((stateMask & SWT.MODIFIER_MASK) == SWT.CTRL) { | |
2366 | cursor = fDragCursor; | |
2367 | } else if ((stateMask & SWT.MODIFIER_MASK) == SWT.SHIFT) { | |
2368 | cursor = fResizeCursor; | |
baf92cac AM |
2369 | } else if (!isOverSplitLine(x)) { |
2370 | long selectionBegin = fTimeProvider.getSelectionBegin(); | |
2371 | long selectionEnd = fTimeProvider.getSelectionEnd(); | |
2372 | int xBegin = getXForTime(selectionBegin); | |
2373 | int xEnd = getXForTime(selectionEnd); | |
2374 | if (Math.abs(x - xBegin) < SNAP_WIDTH || Math.abs(x - xEnd) < SNAP_WIDTH) { | |
2375 | cursor = fResizeCursor; | |
0fcf3b09 PT |
2376 | } |
2377 | } | |
2378 | if (getCursor() != cursor) { | |
2379 | setCursor(cursor); | |
2380 | } | |
837a2f8c PT |
2381 | } |
2382 | ||
4aa2593c PT |
2383 | /** |
2384 | * Update the status line following a change of selection. | |
2385 | * | |
2386 | * @since 2.0 | |
2387 | */ | |
2388 | public void updateStatusLine() { | |
2389 | updateStatusLine(STATUS_WITHOUT_CURSOR_TIME); | |
2390 | } | |
2391 | ||
0fcf3b09 | 2392 | private void updateStatusLine(int x) { |
0fab12b0 PT |
2393 | // use the time provider of the time graph scale for the status line |
2394 | ITimeDataProvider tdp = fTimeGraphScale.getTimeProvider(); | |
2395 | if (fStatusLineManager == null || null == tdp || | |
2396 | tdp.getTime0() == tdp.getTime1()) { | |
0fcf3b09 PT |
2397 | return; |
2398 | } | |
0fab12b0 PT |
2399 | TimeFormat tf = tdp.getTimeFormat(); |
2400 | Resolution res = Resolution.NANOSEC; | |
0fcf3b09 | 2401 | StringBuilder message = new StringBuilder(); |
33fa1fc7 PT |
2402 | if ((x >= 0 || x == STATUS_WITHOUT_CURSOR_TIME) && fDragState == DRAG_NONE) { |
2403 | if (x != STATUS_WITHOUT_CURSOR_TIME) { | |
2404 | long time = getTimeAtX(x); | |
2405 | if (time >= 0) { | |
2406 | if (tdp instanceof ITimeDataProviderConverter) { | |
2407 | time = ((ITimeDataProviderConverter) tdp).convertTime(time); | |
2408 | } | |
2409 | message.append(NLS.bind("T: {0}{1} ", //$NON-NLS-1$ | |
0fab12b0 | 2410 | new Object[] { |
33fa1fc7 PT |
2411 | tf == TimeFormat.CALENDAR ? Utils.formatDate(time) + ' ' : "", //$NON-NLS-1$ |
2412 | Utils.formatTime(time, tf, res) | |
0fab12b0 | 2413 | })); |
0fcf3b09 PT |
2414 | } |
2415 | } | |
33fa1fc7 PT |
2416 | long selectionBegin = tdp.getSelectionBegin(); |
2417 | long selectionEnd = tdp.getSelectionEnd(); | |
2418 | message.append(NLS.bind("T1: {0}{1}", //$NON-NLS-1$ | |
2419 | new Object[] { | |
2420 | tf == TimeFormat.CALENDAR ? Utils.formatDate(selectionBegin) + ' ' : "", //$NON-NLS-1$ | |
2421 | Utils.formatTime(selectionBegin, tf, res) | |
2422 | })); | |
2423 | if (selectionBegin != selectionEnd) { | |
2424 | message.append(NLS.bind(" T2: {0}{1} \u0394: {2}", //$NON-NLS-1$ | |
2425 | new Object[] { | |
2426 | tf == TimeFormat.CALENDAR ? Utils.formatDate(selectionEnd) + ' ' : "", //$NON-NLS-1$ | |
2427 | Utils.formatTime(selectionEnd, tf, res), | |
2428 | Utils.formatDelta(selectionEnd - selectionBegin, tf, res) | |
2429 | })); | |
2430 | } | |
0fcf3b09 | 2431 | } else if (fDragState == DRAG_SELECTION || fDragState == DRAG_ZOOM) { |
85203d74 PT |
2432 | long time0 = fDragBeginMarker ? getTimeAtX(fDragX0) : fDragTime0; |
2433 | long time = fDragBeginMarker ? fDragTime0 : getTimeAtX(fDragX); | |
0fab12b0 PT |
2434 | if (tdp instanceof ITimeDataProviderConverter) { |
2435 | time0 = ((ITimeDataProviderConverter) tdp).convertTime(time0); | |
2436 | time = ((ITimeDataProviderConverter) tdp).convertTime(time); | |
837a2f8c | 2437 | } |
0fab12b0 PT |
2438 | message.append(NLS.bind("T1: {0}{1} T2: {2}{3} \u0394: {4}", //$NON-NLS-1$ |
2439 | new Object[] { | |
85203d74 PT |
2440 | tf == TimeFormat.CALENDAR ? Utils.formatDate(time0) + ' ' : "", //$NON-NLS-1$ |
2441 | Utils.formatTime(time0, tf, res), | |
2442 | tf == TimeFormat.CALENDAR ? Utils.formatDate(time) + ' ' : "", //$NON-NLS-1$ | |
2443 | Utils.formatTime(time, tf, res), | |
2444 | Utils.formatDelta(time - time0, tf, res) | |
0fab12b0 | 2445 | })); |
837a2f8c | 2446 | } |
0fcf3b09 | 2447 | fStatusLineManager.setMessage(message.toString()); |
837a2f8c PT |
2448 | } |
2449 | ||
2450 | @Override | |
2451 | public void mouseMove(MouseEvent e) { | |
f1fae91f | 2452 | if (null == fTimeProvider) { |
837a2f8c PT |
2453 | return; |
2454 | } | |
b698ec63 | 2455 | Point size = getSize(); |
f1fae91f PT |
2456 | if (DRAG_TRACE_ITEM == fDragState) { |
2457 | int nameWidth = fTimeProvider.getNameSpace(); | |
0fcf3b09 PT |
2458 | if (e.x > nameWidth && size.x > nameWidth && fDragX != e.x) { |
2459 | fDragX = e.x; | |
f1fae91f PT |
2460 | double pixelsPerNanoSec = (size.x - nameWidth <= RIGHT_MARGIN) ? 0 : (double) (size.x - nameWidth - RIGHT_MARGIN) / (fTime1bak - fTime0bak); |
2461 | long timeDelta = (long) ((pixelsPerNanoSec == 0) ? 0 : ((fDragX - fDragX0) / pixelsPerNanoSec)); | |
2462 | long time1 = fTime1bak - timeDelta; | |
2463 | long maxTime = fTimeProvider.getMaxTime(); | |
837a2f8c PT |
2464 | if (time1 > maxTime) { |
2465 | time1 = maxTime; | |
2466 | } | |
f1fae91f PT |
2467 | long time0 = time1 - (fTime1bak - fTime0bak); |
2468 | if (time0 < fTimeProvider.getMinTime()) { | |
2469 | time0 = fTimeProvider.getMinTime(); | |
2470 | time1 = time0 + (fTime1bak - fTime0bak); | |
837a2f8c | 2471 | } |
88de10c6 | 2472 | fTimeProvider.setStartFinishTimeNotify(time0, time1); |
837a2f8c | 2473 | } |
f1fae91f PT |
2474 | } else if (DRAG_SPLIT_LINE == fDragState) { |
2475 | fDragX = e.x; | |
2476 | fTimeProvider.setNameSpace(e.x); | |
d2e4afa7 | 2477 | TmfSignalManager.dispatchSignal(new TmfTimeViewAlignmentSignal(this, getTimeViewAlignmentInfo())); |
0fcf3b09 | 2478 | } else if (DRAG_SELECTION == fDragState) { |
85203d74 PT |
2479 | if (fDragBeginMarker) { |
2480 | fDragX0 = Math.min(Math.max(e.x, fTimeProvider.getNameSpace()), size.x - RIGHT_MARGIN); | |
2481 | } else { | |
2482 | fDragX = Math.min(Math.max(e.x, fTimeProvider.getNameSpace()), size.x - RIGHT_MARGIN); | |
2483 | } | |
baf92cac AM |
2484 | redraw(); |
2485 | fTimeGraphScale.setDragRange(fDragX0, fDragX); | |
f2ca0f69 | 2486 | fireDragSelectionChanged(getTimeAtX(fDragX0), getTimeAtX(fDragX)); |
f1fae91f PT |
2487 | } else if (DRAG_ZOOM == fDragState) { |
2488 | fDragX = Math.min(Math.max(e.x, fTimeProvider.getNameSpace()), size.x - RIGHT_MARGIN); | |
5b2b9bd7 | 2489 | redraw(); |
0fcf3b09 | 2490 | fTimeGraphScale.setDragRange(fDragX0, fDragX); |
f1fae91f | 2491 | } else if (DRAG_NONE == fDragState) { |
837a2f8c | 2492 | boolean mouseOverSplitLine = isOverSplitLine(e.x); |
f1fae91f | 2493 | if (fMouseOverSplitLine != mouseOverSplitLine) { |
837a2f8c PT |
2494 | redraw(); |
2495 | } | |
f1fae91f | 2496 | fMouseOverSplitLine = mouseOverSplitLine; |
837a2f8c | 2497 | } |
0fcf3b09 PT |
2498 | updateCursor(e.x, e.stateMask); |
2499 | updateStatusLine(e.x); | |
837a2f8c PT |
2500 | } |
2501 | ||
2502 | @Override | |
2503 | public void mouseDoubleClick(MouseEvent e) { | |
f1fae91f | 2504 | if (null == fTimeProvider) { |
837a2f8c PT |
2505 | return; |
2506 | } | |
5b2b9bd7 | 2507 | if (1 == e.button && (e.stateMask & SWT.BUTTON_MASK) == 0) { |
f1fae91f PT |
2508 | if (isOverSplitLine(e.x) && fTimeProvider.getNameSpace() != 0) { |
2509 | fTimeProvider.setNameSpace(fIdealNameSpace); | |
837a2f8c | 2510 | boolean mouseOverSplitLine = isOverSplitLine(e.x); |
f1fae91f | 2511 | if (fMouseOverSplitLine != mouseOverSplitLine) { |
837a2f8c PT |
2512 | redraw(); |
2513 | } | |
f1fae91f | 2514 | fMouseOverSplitLine = mouseOverSplitLine; |
d2e4afa7 | 2515 | TmfSignalManager.dispatchSignal(new TmfTimeViewAlignmentSignal(this, getTimeViewAlignmentInfo())); |
837a2f8c PT |
2516 | return; |
2517 | } | |
2518 | int idx = getItemIndexAtY(e.y); | |
2519 | if (idx >= 0) { | |
2520 | selectItem(idx, false); | |
2521 | fireDefaultSelection(); | |
2522 | } | |
2523 | } | |
2524 | } | |
2525 | ||
2526 | @Override | |
2527 | public void mouseDown(MouseEvent e) { | |
407bfdd5 PT |
2528 | if (fDragState != DRAG_NONE || null == fTimeProvider || |
2529 | fTimeProvider.getTime0() == fTimeProvider.getTime1() || | |
b698ec63 | 2530 | getSize().x - fTimeProvider.getNameSpace() <= 0) { |
837a2f8c PT |
2531 | return; |
2532 | } | |
2533 | int idx; | |
0fcf3b09 | 2534 | if (1 == e.button && (e.stateMask & SWT.MODIFIER_MASK) == 0) { |
f1fae91f PT |
2535 | int nameSpace = fTimeProvider.getNameSpace(); |
2536 | if (nameSpace != 0 && isOverSplitLine(e.x)) { | |
2537 | fDragState = DRAG_SPLIT_LINE; | |
0fcf3b09 | 2538 | fDragButton = e.button; |
f1fae91f PT |
2539 | fDragX = e.x; |
2540 | fDragX0 = fDragX; | |
2541 | fTime0bak = fTimeProvider.getTime0(); | |
2542 | fTime1bak = fTimeProvider.getTime1(); | |
2543 | redraw(); | |
0fcf3b09 | 2544 | updateCursor(e.x, e.stateMask); |
f1fae91f | 2545 | return; |
837a2f8c | 2546 | } |
0fcf3b09 PT |
2547 | } |
2548 | if (1 == e.button && ((e.stateMask & SWT.MODIFIER_MASK) == 0 || (e.stateMask & SWT.MODIFIER_MASK) == SWT.SHIFT)) { | |
2549 | int nameSpace = fTimeProvider.getNameSpace(); | |
837a2f8c PT |
2550 | idx = getItemIndexAtY(e.y); |
2551 | if (idx >= 0) { | |
f1fae91f PT |
2552 | Item item = fItemData.fExpandedItems[idx]; |
2553 | if (item.fHasChildren && e.x < nameSpace && e.x < MARGIN + (item.fLevel + 1) * EXPAND_SIZE) { | |
837a2f8c | 2554 | toggle(idx); |
0fcf3b09 | 2555 | return; |
837a2f8c PT |
2556 | } |
2557 | selectItem(idx, false); | |
2558 | fireSelectionChanged(); | |
2559 | } else { | |
2560 | selectItem(idx, false); // clear selection | |
837a2f8c PT |
2561 | fireSelectionChanged(); |
2562 | } | |
0fcf3b09 PT |
2563 | long hitTime = getTimeAtX(e.x); |
2564 | if (hitTime >= 0) { | |
2565 | setCapture(true); | |
2566 | ||
2567 | fDragState = DRAG_SELECTION; | |
85203d74 | 2568 | fDragBeginMarker = false; |
0fcf3b09 PT |
2569 | fDragButton = e.button; |
2570 | fDragX = e.x; | |
2571 | fDragX0 = fDragX; | |
2572 | fDragTime0 = getTimeAtX(fDragX0); | |
baf92cac AM |
2573 | long selectionBegin = fTimeProvider.getSelectionBegin(); |
2574 | long selectionEnd = fTimeProvider.getSelectionEnd(); | |
2575 | int xBegin = getXForTime(selectionBegin); | |
2576 | int xEnd = getXForTime(selectionEnd); | |
2577 | if ((e.stateMask & SWT.MODIFIER_MASK) == SWT.SHIFT) { | |
2578 | long time = getTimeAtX(e.x); | |
2579 | if (Math.abs(time - selectionBegin) < Math.abs(time - selectionEnd)) { | |
85203d74 PT |
2580 | fDragBeginMarker = true; |
2581 | fDragX = xEnd; | |
2582 | fDragX0 = e.x; | |
baf92cac | 2583 | fDragTime0 = selectionEnd; |
0fcf3b09 | 2584 | } else { |
baf92cac AM |
2585 | fDragX0 = xBegin; |
2586 | fDragTime0 = selectionBegin; | |
2587 | } | |
2588 | } else { | |
2589 | long time = getTimeAtX(e.x); | |
2590 | if (Math.abs(e.x - xBegin) < SNAP_WIDTH && Math.abs(time - selectionBegin) <= Math.abs(time - selectionEnd)) { | |
85203d74 PT |
2591 | fDragBeginMarker = true; |
2592 | fDragX = xEnd; | |
2593 | fDragX0 = e.x; | |
baf92cac AM |
2594 | fDragTime0 = selectionEnd; |
2595 | } else if (Math.abs(e.x - xEnd) < SNAP_WIDTH && Math.abs(time - selectionEnd) <= Math.abs(time - selectionBegin)) { | |
2596 | fDragX0 = xBegin; | |
2597 | fDragTime0 = selectionBegin; | |
0fcf3b09 PT |
2598 | } |
2599 | } | |
2600 | fTime0bak = fTimeProvider.getTime0(); | |
2601 | fTime1bak = fTimeProvider.getTime1(); | |
2602 | redraw(); | |
2603 | updateCursor(e.x, e.stateMask); | |
2604 | fTimeGraphScale.setDragRange(fDragX0, fDragX); | |
2605 | } | |
2606 | } else if (2 == e.button || (1 == e.button && (e.stateMask & SWT.MODIFIER_MASK) == SWT.CTRL)) { | |
2607 | long hitTime = getTimeAtX(e.x); | |
2608 | if (hitTime > 0) { | |
2609 | setCapture(true); | |
2610 | fDragState = DRAG_TRACE_ITEM; | |
2611 | fDragButton = e.button; | |
2612 | fDragX = e.x; | |
2613 | fDragX0 = fDragX; | |
2614 | fTime0bak = fTimeProvider.getTime0(); | |
2615 | fTime1bak = fTimeProvider.getTime1(); | |
2616 | updateCursor(e.x, e.stateMask); | |
2617 | } | |
5b2b9bd7 | 2618 | } else if (3 == e.button) { |
14b7dd60 MK |
2619 | if (e.x >= fTimeProvider.getNameSpace()) { |
2620 | setCapture(true); | |
2621 | fDragX = Math.min(Math.max(e.x, fTimeProvider.getNameSpace()), getSize().x - RIGHT_MARGIN); | |
2622 | fDragX0 = fDragX; | |
2623 | fDragTime0 = getTimeAtX(fDragX0); | |
2624 | fDragState = DRAG_ZOOM; | |
2625 | fDragButton = e.button; | |
2626 | redraw(); | |
2627 | updateCursor(e.x, e.stateMask); | |
2628 | fTimeGraphScale.setDragRange(fDragX0, fDragX); | |
2629 | } | |
837a2f8c PT |
2630 | } |
2631 | } | |
2632 | ||
2633 | @Override | |
2634 | public void mouseUp(MouseEvent e) { | |
0b5a90a0 PT |
2635 | if (fPendingMenuDetectEvent != null && e.button == 3) { |
2636 | menuDetected(fPendingMenuDetectEvent); | |
2637 | } | |
f1fae91f | 2638 | if (DRAG_NONE != fDragState) { |
837a2f8c | 2639 | setCapture(false); |
0fcf3b09 PT |
2640 | if (e.button == fDragButton && DRAG_TRACE_ITEM == fDragState) { |
2641 | if (fDragX != fDragX0) { | |
2642 | fTimeProvider.notifyStartFinishTime(); | |
2643 | } | |
2644 | fDragState = DRAG_NONE; | |
2645 | } else if (e.button == fDragButton && DRAG_SPLIT_LINE == fDragState) { | |
2646 | fDragState = DRAG_NONE; | |
2647 | redraw(); | |
2648 | } else if (e.button == fDragButton && DRAG_SELECTION == fDragState) { | |
2649 | if (fDragX == fDragX0) { // click without selecting anything | |
837a2f8c | 2650 | long time = getTimeAtX(e.x); |
f1fae91f | 2651 | fTimeProvider.setSelectedTimeNotify(time, false); |
e1725a05 | 2652 | } else { |
85203d74 PT |
2653 | long time0 = fDragBeginMarker ? getTimeAtX(fDragX0) : fDragTime0; |
2654 | long time1 = fDragBeginMarker ? fDragTime0 : getTimeAtX(fDragX); | |
84c8aef7 | 2655 | fTimeProvider.setSelectionRangeNotify(time0, time1, false); |
837a2f8c | 2656 | } |
f1fae91f | 2657 | fDragState = DRAG_NONE; |
837a2f8c | 2658 | redraw(); |
0fcf3b09 PT |
2659 | fTimeGraphScale.setDragRange(-1, -1); |
2660 | } else if (e.button == fDragButton && DRAG_ZOOM == fDragState) { | |
f1fae91f PT |
2661 | int nameWidth = fTimeProvider.getNameSpace(); |
2662 | if (Math.max(fDragX, fDragX0) > nameWidth && fDragX != fDragX0) { | |
2663 | long time0 = getTimeAtX(fDragX0); | |
2664 | long time1 = getTimeAtX(fDragX); | |
5b2b9bd7 | 2665 | if (time0 < time1) { |
f1fae91f | 2666 | fTimeProvider.setStartFinishTimeNotify(time0, time1); |
5b2b9bd7 | 2667 | } else { |
f1fae91f | 2668 | fTimeProvider.setStartFinishTimeNotify(time1, time0); |
5b2b9bd7 PT |
2669 | } |
2670 | } else { | |
2671 | redraw(); | |
2672 | } | |
f1fae91f | 2673 | fDragState = DRAG_NONE; |
0fcf3b09 | 2674 | fTimeGraphScale.setDragRange(-1, -1); |
837a2f8c | 2675 | } |
837a2f8c | 2676 | } |
0fcf3b09 PT |
2677 | updateCursor(e.x, e.stateMask); |
2678 | updateStatusLine(e.x); | |
837a2f8c PT |
2679 | } |
2680 | ||
2681 | @Override | |
2682 | public void mouseEnter(MouseEvent e) { | |
2683 | } | |
2684 | ||
2685 | @Override | |
2686 | public void mouseExit(MouseEvent e) { | |
f1fae91f PT |
2687 | if (fMouseOverSplitLine) { |
2688 | fMouseOverSplitLine = false; | |
837a2f8c PT |
2689 | redraw(); |
2690 | } | |
33fa1fc7 | 2691 | updateStatusLine(STATUS_WITHOUT_CURSOR_TIME); |
837a2f8c PT |
2692 | } |
2693 | ||
2694 | @Override | |
2695 | public void mouseHover(MouseEvent e) { | |
2696 | } | |
2697 | ||
2698 | @Override | |
2699 | public void mouseScrolled(MouseEvent e) { | |
6b11be52 | 2700 | if (fDragState != DRAG_NONE) { |
837a2f8c PT |
2701 | return; |
2702 | } | |
494d213d | 2703 | boolean horizontalZoom = false; |
b698ec63 | 2704 | boolean horizontalScroll = false; |
494d213d | 2705 | boolean verticalZoom = false; |
837a2f8c PT |
2706 | Point p = getParent().toControl(getDisplay().getCursorLocation()); |
2707 | Point parentSize = getParent().getSize(); | |
2708 | if (p.x >= 0 && p.x < parentSize.x && p.y >= 0 && p.y < parentSize.y) { | |
2709 | // over the parent control | |
b698ec63 | 2710 | if (e.x > getSize().x) { |
e5552166 | 2711 | // over the vertical scroll bar |
494d213d PT |
2712 | if ((e.stateMask & SWT.MODIFIER_MASK) == (SWT.SHIFT | SWT.CTRL)) { |
2713 | verticalZoom = true; | |
2714 | } | |
b698ec63 PT |
2715 | } else if (e.y < 0) { |
2716 | // over the time scale | |
494d213d | 2717 | horizontalZoom = true; |
b698ec63 | 2718 | } else if (e.y >= getSize().y) { |
dc4fa715 PT |
2719 | // over the marker axis |
2720 | horizontalZoom = true; | |
e5552166 | 2721 | } else { |
494d213d PT |
2722 | if ((e.stateMask & SWT.MODIFIER_MASK) == (SWT.SHIFT | SWT.CTRL)) { |
2723 | verticalZoom = true; | |
2724 | } else if (e.x < fTimeProvider.getNameSpace()) { | |
e5552166 | 2725 | // over the name space |
494d213d | 2726 | horizontalZoom = false; |
e5552166 PT |
2727 | } else { |
2728 | // over the state area | |
2729 | if ((e.stateMask & SWT.MODIFIER_MASK) == SWT.CTRL) { | |
2730 | // over the state area, CTRL pressed | |
494d213d | 2731 | horizontalZoom = true; |
e5552166 PT |
2732 | } else { |
2733 | // over the state area, CTRL not pressed | |
494d213d | 2734 | horizontalZoom = false; |
e5552166 PT |
2735 | } |
2736 | } | |
837a2f8c PT |
2737 | } |
2738 | } | |
494d213d | 2739 | if (verticalZoom) { |
48072ae3 | 2740 | fVerticalZoomAlignEntry = getVerticalZoomAlignCursor(e.y); |
494d213d | 2741 | if (e.count > 0) { |
83d0971d | 2742 | verticalZoom(true); |
494d213d | 2743 | } else if (e.count < 0) { |
83d0971d | 2744 | verticalZoom(false); |
494d213d | 2745 | } |
48072ae3 PT |
2746 | if (fVerticalZoomAlignEntry != null) { |
2747 | setElementPosition(fVerticalZoomAlignEntry.getKey(), fVerticalZoomAlignEntry.getValue()); | |
2748 | } | |
494d213d | 2749 | } else if (horizontalZoom && fTimeProvider.getTime0() != fTimeProvider.getTime1()) { |
837a2f8c PT |
2750 | if (e.count > 0) { |
2751 | zoom(true); | |
2752 | } else if (e.count < 0) { | |
2753 | zoom(false); | |
2754 | } | |
b698ec63 PT |
2755 | } else if (horizontalScroll) { |
2756 | horizontalScroll(e.count > 0); | |
837a2f8c PT |
2757 | } else { |
2758 | setTopIndex(getTopIndex() - e.count); | |
2759 | } | |
2760 | } | |
2761 | ||
48072ae3 PT |
2762 | /** |
2763 | * Get the vertical zoom alignment entry and position based on the current | |
2764 | * selection. If there is no selection or if the selection is not visible, | |
2765 | * return an alignment entry with a null time graph entry. | |
2766 | * | |
2767 | * @return a map entry where the key is the selection's time graph entry and | |
2768 | * the value is the center y-coordinate of that entry, or null | |
2769 | */ | |
2770 | private Entry<ITimeGraphEntry, Integer> getVerticalZoomAlignSelection() { | |
2771 | Entry<ITimeGraphEntry, Integer> alignEntry = getVerticalZoomAlignOngoing(); | |
2772 | if (alignEntry != null) { | |
2773 | return alignEntry; | |
2774 | } | |
2775 | int index = getSelectedIndex(); | |
2776 | if (index == -1 || index >= getExpandedElementCount()) { | |
2777 | return new SimpleEntry<>(null, 0); | |
2778 | } | |
2779 | Rectangle bounds = getClientArea(); | |
2780 | Rectangle itemRect = getItemRect(bounds, index); | |
2781 | if (itemRect.y < bounds.y || itemRect.y > bounds.y + bounds.height) { | |
2782 | /* selection is not visible */ | |
2783 | return new SimpleEntry<>(null, 0); | |
2784 | } | |
2785 | ITimeGraphEntry entry = getExpandedElement(index); | |
2786 | int y = itemRect.y + itemRect.height / 2; | |
2787 | return new SimpleEntry<>(entry, y); | |
2788 | } | |
2789 | ||
2790 | /** | |
2791 | * Get the vertical zoom alignment entry and position at the specified | |
2792 | * cursor position. | |
2793 | * | |
2794 | * @param y | |
2795 | * the cursor y-coordinate | |
2796 | * @return a map entry where the key is the time graph entry under the | |
2797 | * cursor and the value is the cursor y-coordinate | |
2798 | */ | |
2799 | private Entry<ITimeGraphEntry, Integer> getVerticalZoomAlignCursor(int y) { | |
2800 | Entry<ITimeGraphEntry, Integer> alignEntry = getVerticalZoomAlignOngoing(); | |
2801 | if (alignEntry != null) { | |
2802 | return alignEntry; | |
2803 | } | |
2804 | int index = getItemIndexAtY(y); | |
2805 | if (index == -1) { | |
2806 | index = getExpandedElementCount() - 1; | |
2807 | } | |
2808 | ITimeGraphEntry entry = getExpandedElement(index); | |
2809 | return new SimpleEntry<>(entry, y); | |
2810 | } | |
2811 | ||
2812 | /** | |
2813 | * Get the vertical zoom alignment entry and position if there is an ongoing | |
2814 | * one and we are within the vertical zoom delay, or otherwise return null. | |
2815 | * | |
2816 | * @return a map entry where the key is a time graph entry and the value is | |
2817 | * a y-coordinate, or null | |
2818 | */ | |
2819 | private Entry<ITimeGraphEntry, Integer> getVerticalZoomAlignOngoing() { | |
2820 | long currentTimeMillis = System.currentTimeMillis(); | |
2821 | if (currentTimeMillis < fVerticalZoomAlignTime + VERTICAL_ZOOM_DELAY) { | |
2822 | /* | |
2823 | * If the vertical zoom is triggered repeatedly in a short amount of | |
2824 | * time, use the initial event's entry and position. | |
2825 | */ | |
2826 | fVerticalZoomAlignTime = currentTimeMillis; | |
2827 | return fVerticalZoomAlignEntry; | |
2828 | } | |
2829 | fVerticalZoomAlignTime = currentTimeMillis; | |
2830 | return null; | |
2831 | } | |
2832 | ||
6b11be52 PT |
2833 | @Override |
2834 | public void handleEvent(Event event) { | |
2835 | if (event.type == SWT.MouseWheel) { | |
2836 | // prevent horizontal scrolling when the mouse wheel is used to | |
2837 | // scroll vertically or zoom | |
2838 | event.doit = false; | |
2839 | } | |
2840 | } | |
2841 | ||
837a2f8c PT |
2842 | @Override |
2843 | public int getBorderWidth() { | |
f1fae91f | 2844 | return fBorderWidth; |
837a2f8c PT |
2845 | } |
2846 | ||
2847 | /** | |
2848 | * Set the border width | |
2849 | * | |
2850 | * @param borderWidth | |
2851 | * The width | |
2852 | */ | |
2853 | public void setBorderWidth(int borderWidth) { | |
f1fae91f | 2854 | this.fBorderWidth = borderWidth; |
837a2f8c PT |
2855 | } |
2856 | ||
2857 | /** | |
2858 | * @return The current height of the header row | |
2859 | */ | |
2860 | public int getHeaderHeight() { | |
f1fae91f | 2861 | return fHeaderHeight; |
837a2f8c PT |
2862 | } |
2863 | ||
2864 | /** | |
2865 | * Set the height of the header row | |
2866 | * | |
2867 | * @param headerHeight | |
2868 | * The height | |
2869 | */ | |
2870 | public void setHeaderHeight(int headerHeight) { | |
f1fae91f | 2871 | this.fHeaderHeight = headerHeight; |
837a2f8c PT |
2872 | } |
2873 | ||
2874 | /** | |
c004295c | 2875 | * @return The default height of regular item rows |
837a2f8c PT |
2876 | */ |
2877 | public int getItemHeight() { | |
f1fae91f | 2878 | return fGlobalItemHeight; |
837a2f8c PT |
2879 | } |
2880 | ||
2881 | /** | |
c004295c | 2882 | * Set the default height of regular item rows. |
837a2f8c PT |
2883 | * |
2884 | * @param rowHeight | |
2885 | * The height | |
2886 | */ | |
2887 | public void setItemHeight(int rowHeight) { | |
f1fae91f | 2888 | this.fGlobalItemHeight = rowHeight; |
63b93cd6 PT |
2889 | for (Item item : fItemData.fItems) { |
2890 | item.fItemHeight = rowHeight; | |
2891 | } | |
837a2f8c PT |
2892 | } |
2893 | ||
c004295c PT |
2894 | /** |
2895 | * Set the height of a specific item. Overrides the default item height. | |
2896 | * | |
2897 | * @param entry | |
2898 | * A time graph entry | |
2899 | * @param rowHeight | |
2900 | * The height | |
2901 | * @return true if the height is successfully stored, false otherwise | |
c004295c PT |
2902 | */ |
2903 | public boolean setItemHeight(ITimeGraphEntry entry, int rowHeight) { | |
2904 | Item item = fItemData.findItem(entry); | |
2905 | if (item != null) { | |
2906 | item.fItemHeight = rowHeight; | |
2907 | return true; | |
2908 | } | |
2909 | return false; | |
2910 | } | |
2911 | ||
837a2f8c PT |
2912 | /** |
2913 | * Set the minimum item width | |
2914 | * | |
2915 | * @param width The minimum width | |
2916 | */ | |
2917 | public void setMinimumItemWidth(int width) { | |
f1fae91f | 2918 | this.fMinimumItemWidth = width; |
837a2f8c PT |
2919 | } |
2920 | ||
2921 | /** | |
2922 | * @return The minimum item width | |
2923 | */ | |
2924 | public int getMinimumItemWidth() { | |
f1fae91f | 2925 | return fMinimumItemWidth; |
837a2f8c PT |
2926 | } |
2927 | ||
b052914f PT |
2928 | /** |
2929 | * Set whether all time events with a duration shorter than one pixel should | |
2930 | * be blended in. If false, only the first such time event will be drawn and | |
2931 | * the subsequent time events in the same pixel will be discarded. The | |
2932 | * default value is false. | |
2933 | * | |
2934 | * @param blend | |
2935 | * true if sub-pixel events should be blended, false otherwise. | |
0336f981 | 2936 | * @since 1.1 |
b052914f PT |
2937 | */ |
2938 | public void setBlendSubPixelEvents(boolean blend) { | |
2939 | fBlendSubPixelEvents = blend; | |
2940 | } | |
2941 | ||
837a2f8c PT |
2942 | @Override |
2943 | public void addSelectionChangedListener(ISelectionChangedListener listener) { | |
f1fae91f PT |
2944 | if (listener != null && !fSelectionChangedListeners.contains(listener)) { |
2945 | fSelectionChangedListeners.add(listener); | |
837a2f8c PT |
2946 | } |
2947 | } | |
2948 | ||
837a2f8c PT |
2949 | @Override |
2950 | public void removeSelectionChangedListener(ISelectionChangedListener listener) { | |
2951 | if (listener != null) { | |
f1fae91f | 2952 | fSelectionChangedListeners.remove(listener); |
837a2f8c PT |
2953 | } |
2954 | } | |
2955 | ||
837a2f8c PT |
2956 | @Override |
2957 | public void setSelection(ISelection selection) { | |
2958 | if (selection instanceof TimeGraphSelection) { | |
2959 | TimeGraphSelection sel = (TimeGraphSelection) selection; | |
2960 | Object ob = sel.getFirstElement(); | |
2961 | if (ob instanceof ITimeGraphEntry) { | |
2962 | ITimeGraphEntry trace = (ITimeGraphEntry) ob; | |
2963 | selectItem(trace, false); | |
2964 | } | |
2965 | } | |
2966 | ||
2967 | } | |
2968 | ||
6ac5a950 AM |
2969 | /** |
2970 | * @param filter The filter object to be attached to the view | |
6ac5a950 | 2971 | */ |
367e2932 | 2972 | public void addFilter(@NonNull ViewerFilter filter) { |
f1fae91f PT |
2973 | if (!fFilters.contains(filter)) { |
2974 | fFilters.add(filter); | |
6ac5a950 AM |
2975 | } |
2976 | } | |
2977 | ||
2978 | /** | |
2979 | * @param filter The filter object to be attached to the view | |
6ac5a950 | 2980 | */ |
367e2932 | 2981 | public void removeFilter(@NonNull ViewerFilter filter) { |
f1fae91f | 2982 | fFilters.remove(filter); |
6ac5a950 AM |
2983 | } |
2984 | ||
4923d7b9 PT |
2985 | /** |
2986 | * Returns this control's filters. | |
2987 | * | |
2988 | * @return an array of viewer filters | |
2989 | * @since 2.0 | |
2990 | */ | |
367e2932 | 2991 | public @NonNull ViewerFilter[] getFilters() { |
4923d7b9 PT |
2992 | return Iterables.toArray(fFilters, ViewerFilter.class); |
2993 | } | |
2994 | ||
2995 | /** | |
2996 | * Sets the filters, replacing any previous filters. | |
2997 | * | |
2998 | * @param filters | |
2999 | * an array of viewer filters, or null | |
3000 | * @since 2.0 | |
3001 | */ | |
367e2932 | 3002 | public void setFilters(@NonNull ViewerFilter[] filters) { |
4923d7b9 PT |
3003 | fFilters.clear(); |
3004 | if (filters != null) { | |
3005 | fFilters.addAll(Arrays.asList(filters)); | |
3006 | } | |
3007 | } | |
3008 | ||
496f76d3 GB |
3009 | @Override |
3010 | public void colorSettingsChanged(StateItem[] stateItems) { | |
3011 | /* Destroy previous colors from the resource manager */ | |
517fe01b EB |
3012 | if (fEventColorMap != null) { |
3013 | for (Color color : fEventColorMap) { | |
3014 | fResourceManager.destroyColor(color.getRGB()); | |
3015 | } | |
496f76d3 GB |
3016 | } |
3017 | if (stateItems != null) { | |
3018 | fEventColorMap = new Color[stateItems.length]; | |
3019 | for (int i = 0; i < stateItems.length; i++) { | |
3020 | fEventColorMap[i] = fResourceManager.createColor(stateItems[i].getStateColor()); | |
3021 | } | |
3022 | } else { | |
3023 | fEventColorMap = new Color[] { }; | |
3024 | } | |
3025 | redraw(); | |
3026 | } | |
3027 | ||
837a2f8c | 3028 | private class ItemData { |
df0e3d5f | 3029 | private Map<ITimeGraphEntry, Item> fItemMap = new LinkedHashMap<>(); |
f1fae91f PT |
3030 | private Item[] fExpandedItems = new Item[0]; |
3031 | private Item[] fItems = new Item[0]; | |
70e10acc | 3032 | private ITimeGraphEntry fRootEntries[] = new ITimeGraphEntry[0]; |
507b1336 | 3033 | private List<ILinkEvent> fLinks = new ArrayList<>(); |
837a2f8c PT |
3034 | |
3035 | public ItemData() { | |
3036 | } | |
3037 | ||
70e10acc PT |
3038 | public Item findItem(ITimeGraphEntry entry) { |
3039 | return fItemMap.get(entry); | |
837a2f8c PT |
3040 | } |
3041 | ||
70e10acc PT |
3042 | public int findItemIndex(ITimeGraphEntry entry) { |
3043 | Item item = fItemMap.get(entry); | |
3044 | if (item == null) { | |
837a2f8c PT |
3045 | return -1; |
3046 | } | |
70e10acc | 3047 | return item.fExpandedIndex; |
837a2f8c PT |
3048 | } |
3049 | ||
3050 | public void refreshData() { | |
3e9a3685 | 3051 | ITimeGraphEntry selection = getSelectedTrace(); |
df0e3d5f | 3052 | Map<ITimeGraphEntry, Item> itemMap = new LinkedHashMap<>(); |
70e10acc PT |
3053 | for (int i = 0; i < fRootEntries.length; i++) { |
3054 | ITimeGraphEntry entry = fRootEntries[i]; | |
df0e3d5f | 3055 | refreshData(itemMap, null, 0, entry); |
837a2f8c | 3056 | } |
df0e3d5f | 3057 | fItemMap = itemMap; |
70e10acc | 3058 | fItems = fItemMap.values().toArray(new Item[0]); |
837a2f8c | 3059 | updateExpandedItems(); |
3e9a3685 | 3060 | if (selection != null) { |
f1fae91f | 3061 | for (Item item : fExpandedItems) { |
70e10acc | 3062 | if (item.fEntry == selection) { |
f1fae91f | 3063 | item.fSelected = true; |
3e9a3685 PT |
3064 | break; |
3065 | } | |
3066 | } | |
3067 | } | |
837a2f8c PT |
3068 | } |
3069 | ||
70e10acc | 3070 | private void refreshData(Map<ITimeGraphEntry, Item> itemMap, Item parent, int level, ITimeGraphEntry entry) { |
837a2f8c PT |
3071 | Item item = new Item(entry, entry.getName(), level); |
3072 | if (parent != null) { | |
f1fae91f | 3073 | parent.fChildren.add(item); |
837a2f8c | 3074 | } |
c004295c PT |
3075 | if (fGlobalItemHeight == CUSTOM_ITEM_HEIGHT) { |
3076 | item.fItemHeight = fTimeGraphProvider.getItemHeight(entry); | |
3077 | } else { | |
3078 | item.fItemHeight = fGlobalItemHeight; | |
3079 | } | |
3bd20aa6 | 3080 | item.fItemHeight = Math.max(1, item.fItemHeight + fHeightAdjustment); |
70e10acc | 3081 | itemMap.put(entry, item); |
837a2f8c | 3082 | if (entry.hasChildren()) { |
df0e3d5f PT |
3083 | Item oldItem = fItemMap.get(entry); |
3084 | if (oldItem != null && oldItem.fHasChildren && level == oldItem.fLevel && entry.getParent() == oldItem.fEntry.getParent()) { | |
3085 | /* existing items keep their old expanded state */ | |
3086 | item.fExpanded = oldItem.fExpanded; | |
3087 | } else { | |
3088 | /* new items set the expanded state according to auto-expand level */ | |
3089 | item.fExpanded = fAutoExpandLevel == ALL_LEVELS || level < fAutoExpandLevel; | |
3090 | } | |
f1fae91f | 3091 | item.fHasChildren = true; |
837a2f8c | 3092 | for (ITimeGraphEntry child : entry.getChildren()) { |
70e10acc | 3093 | refreshData(itemMap, item, level + 1, child); |
837a2f8c PT |
3094 | } |
3095 | } | |
3096 | } | |
3097 | ||
3098 | public void updateExpandedItems() { | |
70e10acc PT |
3099 | for (Item item : fItems) { |
3100 | item.fExpandedIndex = -1; | |
3101 | } | |
507b1336 | 3102 | List<Item> expandedItemList = new ArrayList<>(); |
70e10acc PT |
3103 | for (int i = 0; i < fRootEntries.length; i++) { |
3104 | ITimeGraphEntry entry = fRootEntries[i]; | |
837a2f8c PT |
3105 | Item item = findItem(entry); |
3106 | refreshExpanded(expandedItemList, item); | |
3107 | } | |
f1fae91f | 3108 | fExpandedItems = expandedItemList.toArray(new Item[0]); |
19ed1845 | 3109 | fTopIndex = Math.min(fTopIndex, Math.max(0, fExpandedItems.length - 1)); |
837a2f8c PT |
3110 | } |
3111 | ||
3112 | private void refreshExpanded(List<Item> expandedItemList, Item item) { | |
6ac5a950 AM |
3113 | // Check for filters |
3114 | boolean display = true; | |
f1fae91f | 3115 | for (ViewerFilter filter : fFilters) { |
70e10acc | 3116 | if (!filter.select(null, item.fEntry.getParent(), item.fEntry)) { |
6ac5a950 AM |
3117 | display = false; |
3118 | break; | |
3119 | } | |
3120 | } | |
3121 | if (display) { | |
70e10acc | 3122 | item.fExpandedIndex = expandedItemList.size(); |
6ac5a950 | 3123 | expandedItemList.add(item); |
f1fae91f PT |
3124 | if (item.fHasChildren && item.fExpanded) { |
3125 | for (Item child : item.fChildren) { | |
6ac5a950 AM |
3126 | refreshExpanded(expandedItemList, child); |
3127 | } | |
837a2f8c PT |
3128 | } |
3129 | } | |
3130 | } | |
3131 | ||
70e10acc PT |
3132 | public void refreshData(ITimeGraphEntry[] entries) { |
3133 | if (entries == null) { | |
70e10acc | 3134 | fRootEntries = null; |
f1fae91f | 3135 | } else { |
70e10acc | 3136 | fRootEntries = Arrays.copyOf(entries, entries.length); |
837a2f8c PT |
3137 | } |
3138 | ||
837a2f8c PT |
3139 | refreshData(); |
3140 | } | |
3141 | ||
bec1f1ac GB |
3142 | public void refreshArrows(List<ILinkEvent> events) { |
3143 | /* If links are null, reset the list */ | |
3144 | if (events != null) { | |
3145 | fLinks = events; | |
3146 | } else { | |
507b1336 | 3147 | fLinks = new ArrayList<>(); |
bec1f1ac GB |
3148 | } |
3149 | } | |
3150 | ||
70e10acc PT |
3151 | public ITimeGraphEntry[] getEntries() { |
3152 | return fRootEntries; | |
837a2f8c | 3153 | } |
837a2f8c PT |
3154 | } |
3155 | ||
3156 | private class Item { | |
f1fae91f | 3157 | private boolean fExpanded; |
70e10acc | 3158 | private int fExpandedIndex; |
f1fae91f PT |
3159 | private boolean fSelected; |
3160 | private boolean fHasChildren; | |
3161 | private int fItemHeight; | |
70e10acc PT |
3162 | private final int fLevel; |
3163 | private final List<Item> fChildren; | |
3164 | private final String fName; | |
3165 | private final ITimeGraphEntry fEntry; | |
837a2f8c | 3166 | |
70e10acc PT |
3167 | public Item(ITimeGraphEntry entry, String name, int level) { |
3168 | this.fEntry = entry; | |
f1fae91f PT |
3169 | this.fName = name; |
3170 | this.fLevel = level; | |
507b1336 | 3171 | this.fChildren = new ArrayList<>(); |
837a2f8c PT |
3172 | } |
3173 | ||
3174 | @Override | |
3175 | public String toString() { | |
f1fae91f | 3176 | return fName; |
837a2f8c PT |
3177 | } |
3178 | } | |
3179 | ||
27df1564 XR |
3180 | @Override |
3181 | public void menuDetected(MenuDetectEvent e) { | |
f1fae91f | 3182 | if (null == fTimeProvider) { |
27df1564 XR |
3183 | return; |
3184 | } | |
0b5a90a0 PT |
3185 | if (e.detail == SWT.MENU_MOUSE) { |
3186 | if (fPendingMenuDetectEvent == null) { | |
3187 | /* Feature in Linux. The MenuDetectEvent is received before mouseDown. | |
3188 | * Store the event and trigger it later just before handling mouseUp. | |
3189 | * This allows for the method to detect if mouse is used to drag zoom. | |
3190 | */ | |
3191 | fPendingMenuDetectEvent = e; | |
3192 | return; | |
3193 | } | |
3194 | fPendingMenuDetectEvent = null; | |
f1fae91f | 3195 | if (fDragState != DRAG_ZOOM || fDragX != fDragX0) { |
0b5a90a0 PT |
3196 | return; |
3197 | } | |
3198 | } else { | |
f1fae91f | 3199 | if (fDragState != DRAG_NONE) { |
0b5a90a0 PT |
3200 | return; |
3201 | } | |
3202 | } | |
27df1564 XR |
3203 | Point p = toControl(e.x, e.y); |
3204 | int idx = getItemIndexAtY(p.y); | |
f1fae91f PT |
3205 | if (idx >= 0 && idx < fItemData.fExpandedItems.length) { |
3206 | Item item = fItemData.fExpandedItems[idx]; | |
70e10acc | 3207 | ITimeGraphEntry entry = item.fEntry; |
27df1564 XR |
3208 | if (entry.hasTimeEvents()) { |
3209 | ITimeEvent event = Utils.findEvent(entry, getTimeAtX(p.x), 2); | |
3210 | if (event != null) { | |
3211 | e.data = event; | |
3212 | fireMenuEventOnTimeEvent(e); | |
3213 | return; | |
3214 | } | |
3215 | } | |
3216 | e.data = entry; | |
3217 | fireMenuEventOnTimeGraphEntry(e); | |
3218 | } | |
3219 | } | |
3220 | ||
d2e4afa7 MAL |
3221 | /** |
3222 | * Perform the alignment operation. | |
3223 | * | |
3224 | * @param offset | |
3225 | * the alignment offset | |
3226 | * | |
3227 | * @see ITmfTimeAligned | |
3228 | * | |
3229 | * @since 1.0 | |
3230 | */ | |
3231 | public void performAlign(int offset) { | |
3232 | fTimeProvider.setNameSpace(offset); | |
3233 | } | |
3234 | ||
3235 | /** | |
3236 | * Return the time alignment information | |
3237 | * | |
3238 | * @return the time alignment information | |
3239 | * | |
3240 | * @see ITmfTimeAligned | |
3241 | * | |
3242 | * @since 1.0 | |
3243 | */ | |
3244 | public TmfTimeViewAlignmentInfo getTimeViewAlignmentInfo() { | |
3245 | return new TmfTimeViewAlignmentInfo(getShell(), toDisplay(0, 0), fTimeProvider.getNameSpace()); | |
3246 | } | |
837a2f8c | 3247 | } |