Commit | Line | Data |
---|---|---|
bbb3538a | 1 | /********************************************************************** |
60ae41e1 | 2 | * Copyright (c) 2012, 2014 Ericsson |
cfdb727a | 3 | * |
bbb3538a 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: | |
bbb3538a BH |
10 | * Bernd Hufmann - Initial API and implementation |
11 | **********************************************************************/ | |
8e8c0226 | 12 | package org.eclipse.linuxtools.internal.lttng2.control.ui.views.handlers; |
bbb3538a BH |
13 | |
14 | import java.util.ArrayList; | |
15 | import java.util.Iterator; | |
16 | import java.util.List; | |
17 | ||
bbb3538a BH |
18 | import org.eclipse.core.commands.ExecutionEvent; |
19 | import org.eclipse.core.commands.ExecutionException; | |
20 | import org.eclipse.core.runtime.IProgressMonitor; | |
21 | import org.eclipse.core.runtime.IStatus; | |
22 | import org.eclipse.core.runtime.Status; | |
23 | import org.eclipse.core.runtime.jobs.Job; | |
24 | import org.eclipse.jface.viewers.ISelection; | |
25 | import org.eclipse.jface.viewers.StructuredSelection; | |
8e8c0226 AM |
26 | import org.eclipse.linuxtools.internal.lttng2.control.core.model.TraceEnablement; |
27 | import org.eclipse.linuxtools.internal.lttng2.control.ui.Activator; | |
28 | import org.eclipse.linuxtools.internal.lttng2.control.ui.views.ControlView; | |
29 | import org.eclipse.linuxtools.internal.lttng2.control.ui.views.messages.Messages; | |
30 | import org.eclipse.linuxtools.internal.lttng2.control.ui.views.model.impl.TraceChannelComponent; | |
31 | import org.eclipse.linuxtools.internal.lttng2.control.ui.views.model.impl.TraceDomainComponent; | |
32 | import org.eclipse.linuxtools.internal.lttng2.control.ui.views.model.impl.TraceSessionComponent; | |
bbb3538a | 33 | import org.eclipse.ui.IWorkbenchPage; |
bbb3538a BH |
34 | import org.eclipse.ui.IWorkbenchWindow; |
35 | import org.eclipse.ui.PlatformUI; | |
36 | ||
37 | /** | |
bbb3538a | 38 | * <p> |
6503ae0f | 39 | * Abstract command handler implementation to enable or disabling a trace channel. |
bbb3538a | 40 | * </p> |
cfdb727a | 41 | * |
dbd4432d | 42 | * @author Bernd Hufmann |
bbb3538a | 43 | */ |
046b6849 | 44 | public abstract class ChangeChannelStateHandler extends BaseControlViewHandler { |
bbb3538a BH |
45 | |
46 | // ------------------------------------------------------------------------ | |
47 | // Attributes | |
48 | // ------------------------------------------------------------------------ | |
49 | /** | |
c56972bb | 50 | * The command execution parameter. |
bbb3538a | 51 | */ |
c56972bb | 52 | protected Parameter fParam; |
bbb3538a BH |
53 | |
54 | // ------------------------------------------------------------------------ | |
55 | // Accessors | |
56 | // ------------------------------------------------------------------------ | |
57 | /** | |
58 | * @return the new state to set | |
59 | */ | |
046b6849 | 60 | protected abstract TraceEnablement getNewState(); |
bbb3538a BH |
61 | |
62 | // ------------------------------------------------------------------------ | |
63 | // Operations | |
64 | // ------------------------------------------------------------------------ | |
65 | /** | |
6503ae0f BH |
66 | * Changes the state of the given channels. |
67 | * @param domain - the domain of the channels. | |
cfdb727a | 68 | * @param channelNames - a list of channel names |
6503ae0f | 69 | * @param monitor - a progress monitor |
6f4e8ec0 | 70 | * @throws ExecutionException If the command fails |
bbb3538a | 71 | */ |
046b6849 | 72 | protected abstract void changeState(TraceDomainComponent domain, List<String> channelNames, IProgressMonitor monitor) throws ExecutionException; |
bbb3538a | 73 | |
bbb3538a BH |
74 | @Override |
75 | public Object execute(ExecutionEvent event) throws ExecutionException { | |
76 | ||
c56972bb BH |
77 | fLock.lock(); |
78 | try { | |
79 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
bbb3538a | 80 | |
c56972bb BH |
81 | if (window == null) { |
82 | return false; | |
83 | } | |
bbb3538a | 84 | |
c56972bb | 85 | final Parameter param = new Parameter(fParam); |
bbb3538a | 86 | |
c56972bb BH |
87 | Job job = new Job(Messages.TraceControl_ChangeChannelStateJob) { |
88 | @Override | |
89 | protected IStatus run(IProgressMonitor monitor) { | |
f455db37 | 90 | Exception error = null; |
bbb3538a | 91 | |
c56972bb BH |
92 | TraceSessionComponent session = null; |
93 | ||
94 | try { | |
95 | TraceDomainComponent kernelDomain = param.getKernelDomain(); | |
96 | List<TraceChannelComponent> kernelChannels = param.getKernelChannels(); | |
97 | ||
98 | if (kernelDomain != null) { | |
99 | session = (TraceSessionComponent)kernelDomain.getParent(); | |
e0838ca1 | 100 | List<String> channelNames = new ArrayList<>(); |
c56972bb BH |
101 | for (Iterator<TraceChannelComponent> iterator = kernelChannels.iterator(); iterator.hasNext();) { |
102 | // Enable all selected channels which are disabled | |
cfdb727a | 103 | TraceChannelComponent channel = iterator.next(); |
c56972bb BH |
104 | channelNames.add(channel.getName()); |
105 | } | |
106 | ||
107 | changeState(kernelDomain, channelNames, monitor); | |
bbb3538a | 108 | |
c56972bb BH |
109 | for (Iterator<TraceChannelComponent> iterator = kernelChannels.iterator(); iterator.hasNext();) { |
110 | // Enable all selected channels which are disabled | |
cfdb727a | 111 | TraceChannelComponent channel = iterator.next(); |
c56972bb BH |
112 | channel.setState(getNewState()); |
113 | } | |
bbb3538a BH |
114 | } |
115 | ||
c56972bb BH |
116 | TraceDomainComponent ustDomain = param.getUstDomain(); |
117 | List<TraceChannelComponent> ustChannels = param.getUstChannels(); | |
118 | if (ustDomain != null) { | |
119 | if (session == null) { | |
120 | session = (TraceSessionComponent)ustDomain.getParent(); | |
121 | } | |
122 | ||
e0838ca1 | 123 | List<String> channelNames = new ArrayList<>(); |
c56972bb BH |
124 | for (Iterator<TraceChannelComponent> iterator = ustChannels.iterator(); iterator.hasNext();) { |
125 | // Enable all selected channels which are disabled | |
cfdb727a | 126 | TraceChannelComponent channel = iterator.next(); |
c56972bb BH |
127 | channelNames.add(channel.getName()); |
128 | } | |
129 | ||
130 | changeState(ustDomain, channelNames, monitor); | |
131 | ||
132 | for (Iterator<TraceChannelComponent> iterator = ustChannels.iterator(); iterator.hasNext();) { | |
133 | // Enable all selected channels which are disabled | |
cfdb727a | 134 | TraceChannelComponent channel = iterator.next(); |
c56972bb BH |
135 | channel.setState(getNewState()); |
136 | } | |
bbb3538a | 137 | } |
c56972bb | 138 | } catch (ExecutionException e) { |
f455db37 | 139 | error = e; |
bbb3538a | 140 | } |
bbb3538a | 141 | |
cfdb727a | 142 | // In all cases notify listeners |
0a78d11a AM |
143 | if (session != null) { |
144 | session.fireComponentChanged(session); | |
145 | } | |
bbb3538a | 146 | |
f455db37 | 147 | if (error != null) { |
cfdb727a | 148 | return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_ChangeChannelStateFailure, error); |
c56972bb | 149 | } |
bbb3538a | 150 | |
c56972bb BH |
151 | return Status.OK_STATUS; |
152 | } | |
153 | }; | |
154 | job.setUser(true); | |
155 | job.schedule(); | |
156 | } finally { | |
157 | fLock.unlock(); | |
158 | } | |
bbb3538a BH |
159 | |
160 | return null; | |
161 | } | |
162 | ||
bbb3538a BH |
163 | @Override |
164 | public boolean isEnabled() { | |
bbb3538a | 165 | |
498704b3 BH |
166 | // Get workbench page for the Control View |
167 | IWorkbenchPage page = getWorkbenchPage(); | |
bbb3538a BH |
168 | if (page == null) { |
169 | return false; | |
170 | } | |
c56972bb BH |
171 | |
172 | TraceDomainComponent kernelDomain = null; | |
173 | TraceDomainComponent ustDomain = null; | |
e0838ca1 AM |
174 | List<TraceChannelComponent> kernelChannels = new ArrayList<>(); |
175 | List<TraceChannelComponent> ustChannels = new ArrayList<>(); | |
cfdb727a | 176 | |
bbb3538a BH |
177 | // Check if one or more session are selected |
178 | ISelection selection = page.getSelection(ControlView.ID); | |
179 | if (selection instanceof StructuredSelection) { | |
180 | StructuredSelection structered = ((StructuredSelection) selection); | |
181 | String sessionName = null; | |
182 | for (Iterator<?> iterator = structered.iterator(); iterator.hasNext();) { | |
cfdb727a AM |
183 | Object element = iterator.next(); |
184 | ||
bbb3538a | 185 | if (element instanceof TraceChannelComponent) { |
cfdb727a | 186 | |
bbb3538a BH |
187 | // Add only TraceChannelComponents that are disabled |
188 | TraceChannelComponent channel = (TraceChannelComponent) element; | |
189 | if (sessionName == null) { | |
190 | sessionName = String.valueOf(channel.getSessionName()); | |
191 | } | |
192 | ||
193 | // Enable command only for channels of same session | |
194 | if (!sessionName.equals(channel.getSessionName())) { | |
c56972bb BH |
195 | kernelChannels.clear(); |
196 | ustChannels.clear(); | |
bbb3538a BH |
197 | break; |
198 | } | |
199 | ||
200 | if ((channel.getState() != getNewState())) { | |
201 | if (channel.isKernel()) { | |
c56972bb BH |
202 | kernelChannels.add(channel); |
203 | if (kernelDomain == null) { | |
204 | kernelDomain = (TraceDomainComponent) channel.getParent(); | |
bbb3538a BH |
205 | } |
206 | } else { | |
c56972bb BH |
207 | ustChannels.add(channel); |
208 | if (ustDomain == null) { | |
209 | ustDomain = (TraceDomainComponent) channel.getParent(); | |
bbb3538a BH |
210 | } |
211 | } | |
212 | } | |
213 | } | |
214 | } | |
215 | } | |
cfdb727a | 216 | |
c56972bb BH |
217 | boolean isEnabled = (!kernelChannels.isEmpty() || !ustChannels.isEmpty()); |
218 | fLock.lock(); | |
219 | try { | |
220 | if (isEnabled) { | |
221 | fParam = new Parameter(kernelDomain, ustDomain, kernelChannels, ustChannels); | |
222 | } | |
223 | } finally { | |
224 | fLock.unlock(); | |
225 | } | |
cfdb727a | 226 | |
c56972bb | 227 | return isEnabled; |
bbb3538a | 228 | } |
cfdb727a | 229 | |
bbb3538a | 230 | /** |
cfdb727a | 231 | * Class containing parameter for the command execution. |
bbb3538a | 232 | */ |
046b6849 | 233 | protected static class Parameter { |
c56972bb BH |
234 | /** |
235 | * Kernel domain component reference. | |
236 | */ | |
046b6849 | 237 | protected final TraceDomainComponent fKernelDomain; |
c56972bb BH |
238 | /** |
239 | * UST domain component reference. | |
240 | */ | |
046b6849 | 241 | protected final TraceDomainComponent fUstDomain; |
c56972bb | 242 | /** |
cfdb727a | 243 | * The list of kernel channel components the command is to be executed on. |
c56972bb | 244 | */ |
046b6849 | 245 | protected final List<TraceChannelComponent> fKernelChannels; |
c56972bb | 246 | /** |
cfdb727a | 247 | * The list of UST channel components the command is to be executed on. |
c56972bb | 248 | */ |
046b6849 | 249 | protected final List<TraceChannelComponent> fUstChannels; |
cfdb727a | 250 | |
c56972bb BH |
251 | /** |
252 | * Constructor | |
253 | * @param kernelDomain - a kernel domain component | |
254 | * @param ustDomain - a UST domain component | |
255 | * @param kernelChannels - list of available kernel channels | |
256 | * @param ustChannels - list of available UST channels | |
257 | */ | |
258 | public Parameter(TraceDomainComponent kernelDomain, TraceDomainComponent ustDomain, List<TraceChannelComponent> kernelChannels, List<TraceChannelComponent> ustChannels) { | |
259 | fKernelDomain = kernelDomain; | |
260 | fUstDomain = ustDomain; | |
e0838ca1 | 261 | fKernelChannels = new ArrayList<>(); |
c56972bb | 262 | fKernelChannels.addAll(kernelChannels); |
e0838ca1 | 263 | fUstChannels = new ArrayList<>(); |
c56972bb BH |
264 | fUstChannels.addAll(ustChannels); |
265 | } | |
cfdb727a | 266 | |
c56972bb BH |
267 | /** |
268 | * Copy constructor | |
269 | * @param other a parameter to copy | |
270 | */ | |
271 | public Parameter(Parameter other) { | |
272 | this(other.fKernelDomain, other.fUstDomain, other.fKernelChannels, other.fUstChannels); | |
273 | } | |
cfdb727a | 274 | |
c56972bb BH |
275 | /** |
276 | * @return the kernel domain component. | |
277 | */ | |
278 | public TraceDomainComponent getKernelDomain() { | |
279 | return fKernelDomain; | |
280 | } | |
cfdb727a | 281 | |
c56972bb BH |
282 | /** |
283 | * @return the UST domain component. | |
284 | */ | |
285 | public TraceDomainComponent getUstDomain() { | |
286 | return fUstDomain; | |
cfdb727a AM |
287 | } |
288 | ||
c56972bb BH |
289 | /** |
290 | * @return the list of kernel channel components. | |
291 | */ | |
292 | public List<TraceChannelComponent> getKernelChannels() { | |
293 | return fKernelChannels; | |
294 | } | |
cfdb727a | 295 | |
c56972bb BH |
296 | /** |
297 | * @return the list of UST channel components. | |
298 | */ | |
299 | public List<TraceChannelComponent> getUstChannels() { | |
300 | return fUstChannels; | |
301 | } | |
bbb3538a BH |
302 | } |
303 | } |