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