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