Merge branch 'master' into lttng_2_0_control_dev
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.ui / src / org / eclipse / linuxtools / internal / lttng2 / ui / views / control / handlers / ChangeChannelStateHandler.java
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 **********************************************************************/
12 package org.eclipse.linuxtools.internal.lttng2.ui.views.control.handlers;
13
14 import java.util.ArrayList;
15 import java.util.Iterator;
16 import java.util.List;
17
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;
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;
33 import org.eclipse.ui.IWorkbenchPage;
34 import org.eclipse.ui.IWorkbenchWindow;
35 import org.eclipse.ui.PlatformUI;
36
37 /**
38 * <b><u>ChangeChannelStateHandler</u></b>
39 * <p>
40 * Abstract command handler implementation to enable or disabling a trace channel.
41 * </p>
42 */
43 abstract public class ChangeChannelStateHandler extends BaseControlViewHandler {
44
45 // ------------------------------------------------------------------------
46 // Attributes
47 // ------------------------------------------------------------------------
48 /**
49 * The command execution parameter.
50 */
51 protected Parameter fParam;
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 /**
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
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
80 fLock.lock();
81 try {
82 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
83
84 if (window == null) {
85 return false;
86 }
87
88 final Parameter param = new Parameter(fParam);
89
90 Job job = new Job(Messages.TraceControl_ChangeChannelStateJob) {
91 @Override
92 protected IStatus run(IProgressMonitor monitor) {
93 String errorString = null;
94
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);
111
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 }
117 }
118
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 }
140 }
141 } catch (ExecutionException e) {
142 errorString = e.toString() + "\n"; //$NON-NLS-1$
143 }
144
145 // In all cases notify listeners
146 session.fireComponentChanged(session);
147
148 if (errorString != null) {
149 return new Status(Status.ERROR, Activator.PLUGIN_ID, errorString);
150 }
151
152 return Status.OK_STATUS;
153 }
154 };
155 job.setUser(true);
156 job.schedule();
157 } finally {
158 fLock.unlock();
159 }
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() {
170
171 // Get workbench page for the Control View
172 IWorkbenchPage page = getWorkbenchPage();
173 if (page == null) {
174 return false;
175 }
176
177 TraceDomainComponent kernelDomain = null;
178 TraceDomainComponent ustDomain = null;
179 List<TraceChannelComponent> kernelChannels = new ArrayList<TraceChannelComponent>();
180 List<TraceChannelComponent> ustChannels = new ArrayList<TraceChannelComponent>();
181
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())) {
200 kernelChannels.clear();
201 ustChannels.clear();
202 break;
203 }
204
205 if ((channel.getState() != getNewState())) {
206 if (channel.isKernel()) {
207 kernelChannels.add(channel);
208 if (kernelDomain == null) {
209 kernelDomain = (TraceDomainComponent) channel.getParent();
210 }
211 } else {
212 ustChannels.add(channel);
213 if (ustDomain == null) {
214 ustDomain = (TraceDomainComponent) channel.getParent();
215 }
216 }
217 }
218 }
219 }
220 }
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;
233 }
234
235 /**
236 * Class containing parameter for the command execution.
237 */
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 }
307 }
308 }
This page took 0.041905 seconds and 6 git commands to generate.