lttng: Don't call PlatformUI.getWorkbench() if the workbench is not running
[deliverable/tracecompass.git] / lttng / org.eclipse.tracecompass.lttng2.kernel.ui / src / org / eclipse / tracecompass / internal / lttng2 / kernel / ui / criticalpath / CriticalPathParameterProvider.java
CommitLineData
ace2121d
FG
1/*******************************************************************************
2 * Copyright (c) 2015 École Polytechnique de Montréal
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
10package org.eclipse.tracecompass.internal.lttng2.kernel.ui.criticalpath;
11
12import org.eclipse.jface.viewers.ISelection;
13import org.eclipse.jface.viewers.IStructuredSelection;
14import org.eclipse.tracecompass.analysis.graph.core.criticalpath.CriticalPathModule;
15import org.eclipse.tracecompass.analysis.os.linux.core.model.HostThread;
16import org.eclipse.tracecompass.analysis.os.linux.ui.views.controlflow.ControlFlowEntry;
17import org.eclipse.tracecompass.analysis.os.linux.ui.views.controlflow.ControlFlowView;
18import org.eclipse.tracecompass.common.core.NonNullUtils;
19import org.eclipse.tracecompass.internal.analysis.graph.ui.criticalpath.view.CriticalPathView;
20import org.eclipse.tracecompass.internal.lttng2.kernel.core.analysis.graph.model.LttngWorker;
21import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule;
22import org.eclipse.tracecompass.tmf.core.analysis.TmfAbstractAnalysisParamProvider;
23import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
24import org.eclipse.ui.IPartListener2;
25import org.eclipse.ui.ISelectionListener;
26import org.eclipse.ui.IViewPart;
27import org.eclipse.ui.IWorkbench;
28import org.eclipse.ui.IWorkbenchPage;
29import org.eclipse.ui.IWorkbenchPart;
30import org.eclipse.ui.IWorkbenchPartReference;
8e2e3288 31import org.eclipse.ui.IWorkbenchWindow;
ace2121d
FG
32import org.eclipse.ui.PlatformUI;
33
34/**
35 * Class that provides parameters to the critical path analysis for lttng kernel
36 * traces
37 *
38 * @author Geneviève Bastien
39 */
40public class CriticalPathParameterProvider extends TmfAbstractAnalysisParamProvider {
41
42 private final class IPartListener2Impl implements IPartListener2 {
43
44 private final Class<?> fType;
45
46 IPartListener2Impl(Class<?> type) {
47 fType = type;
48 }
49
50 private void toggleState(IWorkbenchPartReference partRef, boolean state) {
51 if (fType.isInstance(partRef.getPart(false))) {
52 toggleActive(state);
53 }
54 }
55
56 @Override
57 public void partActivated(IWorkbenchPartReference partRef) {
58 toggleState(partRef, true);
59 }
60
61 @Override
62 public void partBroughtToTop(IWorkbenchPartReference partRef) {
63
64 }
65
66 @Override
67 public void partClosed(IWorkbenchPartReference partRef) {
68 toggleState(partRef, false);
69 }
70
71 @Override
72 public void partDeactivated(IWorkbenchPartReference partRef) {
73
74 }
75
76 @Override
77 public void partOpened(IWorkbenchPartReference partRef) {
78 toggleState(partRef, true);
79 }
80
81 @Override
82 public void partHidden(IWorkbenchPartReference partRef) {
83 toggleState(partRef, false);
84 }
85
86 @Override
87 public void partVisible(IWorkbenchPartReference partRef) {
88 toggleState(partRef, true);
89 }
90
91 @Override
92 public void partInputChanged(IWorkbenchPartReference partRef) {
93
94 }
95 }
96
97 private ISelectionListener fSelListener = new ISelectionListener() {
98 @Override
99 public void selectionChanged(IWorkbenchPart part, ISelection selection) {
100 if (selection instanceof IStructuredSelection) {
101 Object element = ((IStructuredSelection) selection).getFirstElement();
102 if (element instanceof ControlFlowEntry) {
103 ControlFlowEntry entry = (ControlFlowEntry) element;
104 setCurrentThreadId(entry);
105 }
106 }
107 }
108 };
109
110 private static final String NAME = "Critical Path Lttng kernel parameter provider"; //$NON-NLS-1$
111
112 private final IPartListener2 fPartListener = new IPartListener2Impl(CriticalPathView.class);
113
114 private ControlFlowEntry fCurrentEntry = null;
115
116 private boolean fActive = false;
117 private boolean fEntryChanged = false;
118
119 /**
120 * Constructor
121 */
122 public CriticalPathParameterProvider() {
123 super();
124 registerListener();
125 }
126
127 @Override
128 public String getName() {
129 return NAME;
130 }
131
132 @Override
133 public Object getParameter(String name) {
134 if (fCurrentEntry == null) {
135 return null;
136 }
137 if (name.equals(CriticalPathModule.PARAM_WORKER)) {
138 /* Try to find the worker for the critical path */
139 IAnalysisModule mod = getModule();
140 if ((mod != null) && (mod instanceof CriticalPathModule)) {
141 Integer threadId = NonNullUtils.checkNotNull(fCurrentEntry.getThreadId());
142 HostThread ht = new HostThread(fCurrentEntry.getTrace().getHostId(), threadId);
143 LttngWorker worker = new LttngWorker(ht, "", 0); //$NON-NLS-1$
144 return worker;
145 }
146 return fCurrentEntry;
147 }
148 return null;
149 }
150
151 @Override
152 public boolean appliesToTrace(ITmfTrace trace) {
153 return true;
154 }
155
156 private void setCurrentThreadId(ControlFlowEntry entry) {
157 if (!entry.equals(fCurrentEntry)) {
158 fCurrentEntry = entry;
159 if (fActive) {
160 this.notifyParameterChanged(CriticalPathModule.PARAM_WORKER);
161 } else {
162 fEntryChanged = true;
163 }
164 }
165 }
166
167 private void toggleActive(boolean active) {
168 if (active != fActive) {
169 fActive = active;
170 if (fActive && fEntryChanged) {
171 this.notifyParameterChanged(CriticalPathModule.PARAM_WORKER);
172 fEntryChanged = false;
173 }
174 }
175 }
176
177 private void registerListener() {
8e2e3288
AM
178 if (!PlatformUI.isWorkbenchRunning()) {
179 return;
180 }
181 IWorkbench wb = PlatformUI.getWorkbench();
182 if (wb == null) {
183 return;
184 }
185 IWorkbenchWindow wbw = wb.getActiveWorkbenchWindow();
186 if (wbw == null) {
187 return;
188 }
189 final IWorkbenchPage activePage = wbw.getActivePage();
190 if (activePage == null) {
191 return;
192 }
ace2121d
FG
193
194 /* Activate the update if critical path view visible */
195 IViewPart view = activePage.findView(CriticalPathView.ID);
196 if (view != null) {
197 if (activePage.isPartVisible(view)) {
198 toggleActive(true);
199 }
200 }
201
202 /* Add the listener to the control flow view */
203 view = activePage.findView(ControlFlowView.ID);
204 if (view != null) {
205 view.getSite().getWorkbenchWindow().getSelectionService().addPostSelectionListener(fSelListener);
206 view.getSite().getWorkbenchWindow().getPartService().addPartListener(fPartListener);
207 }
208 }
209
210}
This page took 0.034452 seconds and 5 git commands to generate.