Commit | Line | Data |
---|---|---|
6503ae0f | 1 | /********************************************************************** |
ba3a9bd2 | 2 | * Copyright (c) 2012, 2013 Ericsson |
cfdb727a | 3 | * |
6503ae0f BH |
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 | |
cfdb727a AM |
8 | * |
9 | * Contributors: | |
6503ae0f | 10 | * Bernd Hufmann - Initial API and implementation |
ba3a9bd2 | 11 | * Bernd Hufmann - Updated for support of LTTng Tools 2.1 |
6503ae0f | 12 | **********************************************************************/ |
115b4a01 | 13 | package org.eclipse.linuxtools.internal.lttng2.ui.views.control.handlers; |
6503ae0f BH |
14 | |
15 | import java.util.ArrayList; | |
c56972bb | 16 | import java.util.Arrays; |
6503ae0f BH |
17 | import java.util.Iterator; |
18 | import java.util.List; | |
19 | ||
6503ae0f BH |
20 | import org.eclipse.core.commands.ExecutionEvent; |
21 | import org.eclipse.core.commands.ExecutionException; | |
22 | import org.eclipse.core.runtime.IProgressMonitor; | |
23 | import org.eclipse.core.runtime.IStatus; | |
24 | import org.eclipse.core.runtime.Status; | |
25 | import org.eclipse.core.runtime.jobs.Job; | |
26 | import org.eclipse.jface.viewers.ISelection; | |
27 | import org.eclipse.jface.viewers.StructuredSelection; | |
28 | import org.eclipse.jface.window.Window; | |
115b4a01 BH |
29 | import org.eclipse.linuxtools.internal.lttng2.ui.Activator; |
30 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.ControlView; | |
115b4a01 BH |
31 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.IGetEventInfoDialog; |
32 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.TraceControlDialogFactory; | |
9315aeee | 33 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.messages.Messages; |
115b4a01 BH |
34 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent; |
35 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.BaseEventComponent; | |
36 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.KernelProviderComponent; | |
37 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TargetNodeComponent; | |
38 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceChannelComponent; | |
39 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceSessionComponent; | |
40 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.UstProviderComponent; | |
6503ae0f | 41 | import org.eclipse.ui.IWorkbenchPage; |
6503ae0f BH |
42 | |
43 | /** | |
6503ae0f BH |
44 | * <p> |
45 | * Command handler implementation to assign events to a session and channel and enable/configure them. | |
46 | * This is done on the trace provider level. | |
47 | * </p> | |
cfdb727a | 48 | * |
dbd4432d | 49 | * @author Bernd Hufmann |
6503ae0f | 50 | */ |
d132bcc7 | 51 | public class AssignEventHandler extends BaseControlViewHandler { |
6503ae0f BH |
52 | |
53 | // ------------------------------------------------------------------------ | |
54 | // Attributes | |
55 | // ------------------------------------------------------------------------ | |
56 | /** | |
c56972bb | 57 | * The command execution parameter. |
6503ae0f | 58 | */ |
c56972bb | 59 | private Parameter fParam; |
cfdb727a | 60 | |
6503ae0f BH |
61 | // ------------------------------------------------------------------------ |
62 | // Operations | |
63 | // ------------------------------------------------------------------------ | |
64 | ||
65 | /* | |
66 | * (non-Javadoc) | |
67 | * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent) | |
68 | */ | |
69 | @Override | |
70 | public Object execute(ExecutionEvent event) throws ExecutionException { | |
71 | ||
c56972bb BH |
72 | fLock.lock(); |
73 | try { | |
74 | // Make a copy for thread safety | |
75 | final Parameter param = new Parameter(fParam); | |
6503ae0f | 76 | |
c56972bb BH |
77 | // Open dialog box to retrieve the session and channel where the events should be enabled in. |
78 | final IGetEventInfoDialog dialog = TraceControlDialogFactory.getInstance().getGetEventInfoDialog(); | |
79 | dialog.setIsKernel(param.isKernel()); | |
80 | dialog.setSessions(param.getSessions()); | |
6503ae0f | 81 | |
c56972bb BH |
82 | if (dialog.open() != Window.OK) { |
83 | return null; | |
84 | } | |
6503ae0f | 85 | |
c56972bb BH |
86 | Job job = new Job(Messages.TraceControl_EnableEventsJob) { |
87 | @Override | |
88 | protected IStatus run(IProgressMonitor monitor) { | |
6503ae0f | 89 | |
f455db37 BH |
90 | Exception error = null; |
91 | ||
c56972bb BH |
92 | try { |
93 | List<String> eventNames = new ArrayList<String>(); | |
94 | List<BaseEventComponent> events = param.getEvents(); | |
95 | // Create list of event names | |
96 | for (Iterator<BaseEventComponent> iterator = events.iterator(); iterator.hasNext();) { | |
ea21cd65 AM |
97 | BaseEventComponent baseEvent = iterator.next(); |
98 | eventNames.add(baseEvent.getName()); | |
c56972bb | 99 | } |
6503ae0f | 100 | |
c56972bb BH |
101 | TraceChannelComponent channel = dialog.getChannel(); |
102 | if (channel == null) { | |
103 | // enable events on default channel (which will be created by lttng-tools) | |
d4514365 | 104 | dialog.getSession().enableEvents(eventNames, param.isKernel(), dialog.getFilterExpression(), monitor); |
c56972bb | 105 | } else { |
d4514365 | 106 | channel.enableEvents(eventNames, dialog.getFilterExpression(), monitor); |
c56972bb BH |
107 | } |
108 | ||
109 | } catch (ExecutionException e) { | |
f455db37 | 110 | error = e; |
6503ae0f | 111 | } |
6503ae0f | 112 | |
f455db37 BH |
113 | // refresh in all cases |
114 | refresh(new CommandParameter(dialog.getSession())); | |
c56972bb | 115 | |
f455db37 | 116 | if (error != null) { |
cfdb727a | 117 | return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_EnableEventsFailure, error); |
c56972bb BH |
118 | } |
119 | return Status.OK_STATUS; | |
6503ae0f | 120 | } |
c56972bb BH |
121 | }; |
122 | job.setUser(true); | |
123 | job.schedule(); | |
124 | } finally { | |
125 | fLock.unlock(); | |
126 | } | |
127 | ||
6503ae0f BH |
128 | return null; |
129 | } | |
130 | ||
131 | /* | |
132 | * (non-Javadoc) | |
133 | * @see org.eclipse.core.commands.AbstractHandler#isEnabled() | |
134 | */ | |
135 | @Override | |
136 | public boolean isEnabled() { | |
c56972bb BH |
137 | ArrayList<BaseEventComponent> events = new ArrayList<BaseEventComponent>(); |
138 | TraceSessionComponent[] sessions = null; | |
139 | Boolean isKernel = null; | |
6503ae0f | 140 | |
c56972bb | 141 | // Get workbench page for the Control View |
d132bcc7 | 142 | IWorkbenchPage page = getWorkbenchPage(); |
6503ae0f BH |
143 | if (page == null) { |
144 | return false; | |
145 | } | |
146 | ||
6503ae0f BH |
147 | // Check if one or more session are selected |
148 | ISelection selection = page.getSelection(ControlView.ID); | |
149 | if (selection instanceof StructuredSelection) { | |
cfdb727a | 150 | |
6503ae0f BH |
151 | StructuredSelection structered = ((StructuredSelection) selection); |
152 | for (Iterator<?> iterator = structered.iterator(); iterator.hasNext();) { | |
cfdb727a | 153 | Object element = iterator.next(); |
6503ae0f BH |
154 | if (element instanceof BaseEventComponent) { |
155 | BaseEventComponent event = (BaseEventComponent) element; | |
156 | ITraceControlComponent provider = event.getParent(); | |
cfdb727a | 157 | |
6503ae0f BH |
158 | // check for kernel or UST provider |
159 | boolean temp = false; | |
160 | if (provider instanceof KernelProviderComponent) { | |
161 | temp = true; | |
162 | } else if (provider instanceof UstProviderComponent) { | |
163 | temp = false; | |
164 | } else { | |
165 | return false; | |
166 | } | |
c56972bb BH |
167 | if (isKernel == null) { |
168 | isKernel = Boolean.valueOf(temp); | |
6503ae0f BH |
169 | } else { |
170 | // don't mix events from Kernel and UST provider | |
c56972bb | 171 | if (isKernel.booleanValue() != temp) { |
6503ae0f BH |
172 | return false; |
173 | } | |
174 | } | |
175 | ||
176 | // Add BaseEventComponents | |
c56972bb | 177 | events.add(event); |
cfdb727a | 178 | |
c56972bb | 179 | if (sessions == null) { |
6503ae0f | 180 | TargetNodeComponent root = (TargetNodeComponent)event.getParent().getParent().getParent(); |
c56972bb | 181 | sessions = root.getSessions(); |
6503ae0f BH |
182 | } |
183 | } | |
184 | } | |
185 | } | |
c56972bb BH |
186 | |
187 | boolean isEnabled = ((!events.isEmpty()) && (sessions != null) && (sessions.length > 0)); | |
64636df8 BH |
188 | |
189 | // To avoid compiler warnings check for null even if isKernel is always not null when used below | |
190 | if (isKernel == null) { | |
191 | return false; | |
192 | } | |
193 | ||
c56972bb BH |
194 | fLock.lock(); |
195 | try { | |
196 | fParam = null; | |
197 | if(isEnabled) { | |
198 | fParam = new Parameter(sessions, events, isKernel); | |
199 | } | |
200 | } finally { | |
201 | fLock.unlock(); | |
202 | } | |
203 | return isEnabled; | |
204 | } | |
205 | ||
206 | /** | |
cfdb727a | 207 | * Class containing parameter for the command execution. |
c56972bb BH |
208 | */ |
209 | final static private class Parameter { | |
210 | ||
211 | /** | |
cfdb727a | 212 | * The list of event components the command is to be executed on. |
c56972bb | 213 | */ |
cfdb727a AM |
214 | private final List<BaseEventComponent> fEvents; |
215 | ||
c56972bb BH |
216 | /** |
217 | * The list of available sessions. | |
218 | */ | |
219 | final private TraceSessionComponent[] fSessions; | |
cfdb727a | 220 | |
c56972bb BH |
221 | /** |
222 | * Flag for indicating Kernel or UST. | |
223 | */ | |
224 | final private boolean fIsKernel; | |
cfdb727a | 225 | |
c56972bb BH |
226 | /** |
227 | * Constructor | |
cfdb727a | 228 | * |
c56972bb BH |
229 | * @param sessions - a array of trace sessions |
230 | * @param events - a lists of events to enable | |
231 | * @param isKernel - domain (true for kernel or UST) | |
232 | */ | |
233 | public Parameter(TraceSessionComponent[] sessions, List<BaseEventComponent> events, boolean isKernel) { | |
234 | fSessions = Arrays.copyOf(sessions, sessions.length); | |
235 | fEvents = new ArrayList<BaseEventComponent>(); | |
236 | fEvents.addAll(events); | |
237 | fIsKernel = isKernel; | |
238 | } | |
cfdb727a | 239 | |
c56972bb BH |
240 | /** |
241 | * Copy constructor | |
242 | * @param other - a parameter to copy | |
243 | */ | |
244 | public Parameter(Parameter other) { | |
245 | this(other.fSessions, other.fEvents, other.fIsKernel); | |
246 | } | |
cfdb727a | 247 | |
c56972bb BH |
248 | public TraceSessionComponent[] getSessions() { |
249 | return fSessions; | |
250 | } | |
cfdb727a | 251 | |
c56972bb BH |
252 | public List<BaseEventComponent> getEvents() { |
253 | return fEvents; | |
254 | } | |
cfdb727a | 255 | |
c56972bb BH |
256 | public boolean isKernel() { |
257 | return fIsKernel; | |
258 | } | |
6503ae0f BH |
259 | } |
260 | } |