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