Commit | Line | Data |
---|---|---|
735b1ca2 AM |
1 | /* |
2 | * Copyright (C) 2017 EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com> | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | */ | |
9 | ||
10 | package org.lttng.scope.tmf2.views.ui.timeline.widgets.timegraph.toolbar.nav; | |
11 | ||
12 | import static java.util.Objects.requireNonNull; | |
13 | ||
14 | import java.util.function.Predicate; | |
15 | ||
16 | import org.eclipse.core.runtime.IProgressMonitor; | |
17 | import org.eclipse.core.runtime.IStatus; | |
18 | import org.eclipse.core.runtime.Status; | |
19 | import org.eclipse.core.runtime.jobs.ISchedulingRule; | |
20 | import org.eclipse.core.runtime.jobs.Job; | |
21 | import org.eclipse.jdt.annotation.Nullable; | |
22 | import org.eclipse.tracecompass.tmf.core.event.ITmfEvent; | |
23 | import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp; | |
24 | import org.eclipse.tracecompass.tmf.core.trace.ITmfContext; | |
25 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; | |
26 | import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager; | |
27 | import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils; | |
28 | import org.lttng.scope.tmf2.views.ui.timeline.widgets.timegraph.StateRectangle; | |
29 | import org.lttng.scope.tmf2.views.ui.timeline.widgets.timegraph.TimeGraphWidget; | |
30 | ||
31 | /** | |
32 | * Navigation mode using the current entry's events. It looks through all events | |
33 | * in the trace belonging to the tree entry of the current selected state, and | |
34 | * navigates through them. This allows stopping at events that may not cause a | |
35 | * state change shown in the view. | |
36 | * | |
37 | * @author Alexandre Montplaisir | |
38 | */ | |
39 | public class NavigationModeFollowEvents extends NavigationMode { | |
40 | ||
41 | private static final String BACK_ICON_PATH = "/icons/toolbar/nav_event_back.gif"; //$NON-NLS-1$ | |
42 | private static final String FWD_ICON_PATH = "/icons/toolbar/nav_event_fwd.gif"; //$NON-NLS-1$ | |
43 | ||
44 | /** | |
45 | * Mutex rule for search action jobs, making sure they execute sequentially | |
46 | */ | |
47 | private final ISchedulingRule fSearchActionMutexRule = new ISchedulingRule() { | |
48 | @Override | |
49 | public boolean isConflicting(@Nullable ISchedulingRule rule) { | |
50 | return (rule == this); | |
51 | } | |
52 | ||
53 | @Override | |
54 | public boolean contains(@Nullable ISchedulingRule rule) { | |
55 | return (rule == this); | |
56 | } | |
57 | }; | |
58 | ||
59 | /** | |
60 | * Constructor | |
61 | */ | |
62 | public NavigationModeFollowEvents() { | |
63 | super(requireNonNull(Messages.sfFollowEventsNavModeName), | |
64 | BACK_ICON_PATH, | |
65 | FWD_ICON_PATH); | |
66 | } | |
67 | ||
68 | @Override | |
69 | public void navigateBackwards(TimeGraphWidget viewer) { | |
70 | navigate(viewer, false); | |
71 | } | |
72 | ||
73 | @Override | |
74 | public void navigateForwards(TimeGraphWidget viewer) { | |
75 | navigate(viewer, true); | |
76 | } | |
77 | ||
78 | private void navigate(TimeGraphWidget viewer, boolean forward) { | |
79 | StateRectangle state = viewer.getSelectedState(); | |
80 | ITmfTrace trace = viewer.getControl().getViewContext().getCurrentTrace(); | |
81 | if (state == null || trace == null) { | |
82 | return; | |
83 | } | |
84 | Predicate<ITmfEvent> predicate = state.getStateInterval().getTreeElement().getEventMatching(); | |
85 | if (predicate == null) { | |
86 | /* The tree element does not support navigating by events. */ | |
87 | return; | |
88 | } | |
89 | ||
90 | String jobName = (forward ? Messages.sfNextEventJobName : Messages.sfPreviousEventJobName); | |
91 | ||
92 | Job job = new Job(jobName) { | |
93 | @Override | |
94 | protected IStatus run(@Nullable IProgressMonitor monitor) { | |
95 | long currentTime = TmfTraceManager.getInstance().getCurrentTraceContext().getSelectionRange().getStartTime().toNanos(); | |
96 | ITmfContext ctx = trace.seekEvent(TmfTimestamp.fromNanos(currentTime)); | |
97 | long rank = ctx.getRank(); | |
98 | ctx.dispose(); | |
99 | ||
100 | ITmfEvent event = (forward ? | |
101 | TmfTraceUtils.getNextEventMatching(trace, rank, predicate, monitor) : | |
102 | TmfTraceUtils.getPreviousEventMatching(trace, rank, predicate, monitor)); | |
103 | if (event != null) { | |
104 | NavUtils.selectNewTimestamp(viewer, event.getTimestamp().toNanos()); | |
105 | } | |
106 | return Status.OK_STATUS; | |
107 | } | |
108 | }; | |
109 | ||
110 | /* | |
111 | * Make subsequent jobs not run concurrently, but wait after one | |
112 | * another. | |
113 | */ | |
114 | job.setRule(fSearchActionMutexRule); | |
115 | job.schedule(); | |
116 | } | |
117 | } |