pcap: Rename packages to org.eclipse.tracecompass.*
[deliverable/tracecompass.git] / org.eclipse.tracecompass.lttng2.control.ui / src / org / eclipse / linuxtools / internal / lttng2 / control / ui / views / handlers / ChangeEventStateHandler.java
CommitLineData
6503ae0f 1/**********************************************************************
60ae41e1 2 * Copyright (c) 2012, 2014 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 **********************************************************************/
8e8c0226 12package org.eclipse.linuxtools.internal.lttng2.control.ui.views.handlers;
6503ae0f
BH
13
14import java.util.ArrayList;
15import java.util.Iterator;
16import java.util.List;
17
6503ae0f
BH
18import org.eclipse.core.commands.ExecutionEvent;
19import org.eclipse.core.commands.ExecutionException;
20import org.eclipse.core.runtime.IProgressMonitor;
21import org.eclipse.core.runtime.IStatus;
22import org.eclipse.core.runtime.Status;
23import org.eclipse.core.runtime.jobs.Job;
24import org.eclipse.jface.viewers.ISelection;
25import org.eclipse.jface.viewers.StructuredSelection;
8e8c0226
AM
26import org.eclipse.linuxtools.internal.lttng2.control.core.model.TraceEnablement;
27import org.eclipse.linuxtools.internal.lttng2.control.ui.Activator;
28import org.eclipse.linuxtools.internal.lttng2.control.ui.views.ControlView;
29import org.eclipse.linuxtools.internal.lttng2.control.ui.views.messages.Messages;
30import org.eclipse.linuxtools.internal.lttng2.control.ui.views.model.impl.TraceChannelComponent;
31import org.eclipse.linuxtools.internal.lttng2.control.ui.views.model.impl.TraceEventComponent;
32import org.eclipse.linuxtools.internal.lttng2.control.ui.views.model.impl.TraceSessionComponent;
6503ae0f 33import org.eclipse.ui.IWorkbenchPage;
6503ae0f
BH
34import org.eclipse.ui.IWorkbenchWindow;
35import org.eclipse.ui.PlatformUI;
36
37/**
6503ae0f
BH
38 * <p>
39 * Base Command handler implementation to enable or disabling a trace channel.
40 * </p>
cfdb727a 41 *
dbd4432d 42 * @author Bernd Hufmann
6503ae0f 43 */
77735e82 44public abstract class ChangeEventStateHandler extends BaseControlViewHandler {
6503ae0f
BH
45
46 // ------------------------------------------------------------------------
47 // Attributes
48 // ------------------------------------------------------------------------
49 /**
c56972bb 50 * The command execution parameter.
6503ae0f 51 */
c56972bb 52 protected Parameter fParam;
cfdb727a 53
6503ae0f
BH
54 // ------------------------------------------------------------------------
55 // Accessors
56 // ------------------------------------------------------------------------
57 /**
58 * @return the new state to set
59 */
77735e82 60 protected abstract TraceEnablement getNewState();
6503ae0f
BH
61
62 // ------------------------------------------------------------------------
63 // Operations
64 // ------------------------------------------------------------------------
65 /**
66 * Change the state
67 * @param channel - channel of events to be enabled
cfdb727a 68 * @param eventNames - list event names
6503ae0f 69 * @param monitor - a progress monitor
6f4e8ec0 70 * @throws ExecutionException If the command fails
6503ae0f 71 */
77735e82 72 protected abstract void changeState(TraceChannelComponent channel, List<String> eventNames, IProgressMonitor monitor) throws ExecutionException;
6503ae0f 73
6503ae0f
BH
74 @Override
75 public Object execute(ExecutionEvent event) throws ExecutionException {
76
77 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
78
79 if (window == null) {
80 return false;
81 }
82
c56972bb
BH
83 fLock.lock();
84 try {
cfdb727a 85
c56972bb
BH
86 final Parameter param = new Parameter(fParam);
87
88 Job job = new Job(Messages.TraceControl_ChangeChannelStateJob) {
89 @Override
90 protected IStatus run(IProgressMonitor monitor) {
f455db37 91 Exception error = null;
c56972bb
BH
92
93 TraceSessionComponent session = null;
94
95 try {
96 boolean isAll = false;
97 if (param.getChannel() != null) {
98 session = param.getChannel().getSession();
e0838ca1 99 List<String> eventNames = new ArrayList<>();
c56972bb 100 List<TraceEventComponent> events = param.getEvents();
cfdb727a 101
c56972bb
BH
102 for (Iterator<TraceEventComponent> iterator = events.iterator(); iterator.hasNext();) {
103 // Enable/disable all selected channels which are disabled
ea21cd65 104 TraceEventComponent traceEvent = iterator.next();
c56972bb
BH
105
106 // Workaround for wildcard handling in lttng-tools
ea21cd65 107 if ("*".equals(traceEvent.getName())) { //$NON-NLS-1$
c56972bb 108 isAll = true;
cfdb727a 109 } else {
ea21cd65 110 eventNames.add(traceEvent.getName());
c56972bb
BH
111 }
112 }
113 if (isAll) {
114 changeState(param.getChannel(), null, monitor);
3e91c9c0 115 }
3e91c9c0 116
c56972bb
BH
117 if (!eventNames.isEmpty()) {
118 changeState(param.getChannel(), eventNames, monitor);
119 }
6503ae0f 120
c56972bb
BH
121 for (Iterator<TraceEventComponent> iterator = events.iterator(); iterator.hasNext();) {
122 // Enable all selected channels which are disabled
cfdb727a 123 TraceEventComponent ev = iterator.next();
c56972bb
BH
124 ev.setState(getNewState());
125 }
6503ae0f 126 }
c56972bb 127 } catch (ExecutionException e) {
f455db37 128 error = e;
6503ae0f 129 }
6503ae0f 130
c56972bb 131 if (session != null) {
cfdb727a 132 // In all cases notify listeners
c56972bb
BH
133 session.fireComponentChanged(session);
134 }
6503ae0f 135
f455db37 136 if (error != null) {
cfdb727a 137 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_ChangeEventStateFailure, error);
c56972bb 138 }
6503ae0f 139
c56972bb
BH
140 return Status.OK_STATUS;
141 }
142 };
143 job.setUser(true);
144 job.schedule();
145 } finally {
146 fLock.unlock();
147 }
6503ae0f
BH
148 return null;
149 }
150
6503ae0f
BH
151 @Override
152 public boolean isEnabled() {
498704b3
BH
153 // Get workbench page for the Control View
154 IWorkbenchPage page = getWorkbenchPage();
6503ae0f
BH
155 if (page == null) {
156 return false;
157 }
158
6503ae0f
BH
159 // Check if one or more session are selected
160 ISelection selection = page.getSelection(ControlView.ID);
cfdb727a 161
c56972bb 162 TraceChannelComponent channel = null;
e0838ca1 163 List<TraceEventComponent> events = new ArrayList<>();
c56972bb 164
6503ae0f
BH
165 if (selection instanceof StructuredSelection) {
166 StructuredSelection structered = ((StructuredSelection) selection);
167 String sessionName = null;
168 String channelName = null;
cfdb727a 169
6503ae0f 170 for (Iterator<?> iterator = structered.iterator(); iterator.hasNext();) {
cfdb727a
AM
171 Object element = iterator.next();
172
6503ae0f 173 if (element instanceof TraceEventComponent) {
cfdb727a 174
3e91c9c0 175 TraceEventComponent event = (TraceEventComponent) element;
6503ae0f 176 if (sessionName == null) {
3e91c9c0 177 sessionName = String.valueOf(event.getSessionName());
6503ae0f 178 }
cfdb727a 179
c56972bb
BH
180 if (channel == null) {
181 channel = (TraceChannelComponent)event.getParent();
6503ae0f
BH
182 }
183
184 if (channelName == null) {
3e91c9c0 185 channelName = event.getChannelName();
6503ae0f 186 }
3e91c9c0 187
6503ae0f 188 // Enable command only for events of same session, same channel and domain
3e91c9c0
BH
189 if ((!sessionName.equals(event.getSessionName())) ||
190 (!channelName.equals(event.getChannelName())) ||
c56972bb
BH
191 (channel.isKernel() != event.isKernel())) {
192 events.clear();
6503ae0f
BH
193 break;
194 }
195
3e91c9c0 196 if ((event.getState() != getNewState())) {
c56972bb 197 events.add(event);
6503ae0f
BH
198 }
199 }
200 }
201 }
c56972bb
BH
202 boolean isEnabled = !events.isEmpty();
203
204 fLock.lock();
205 try {
206 fParam = null;
207 if (isEnabled) {
208 fParam = new Parameter(channel, events);
209 }
210 } finally {
211 fLock.unlock();
212 }
213 return isEnabled;
6503ae0f
BH
214 }
215
216 /**
cfdb727a 217 * Class containing parameter for the command execution.
6503ae0f 218 */
77735e82 219 protected static class Parameter {
c56972bb
BH
220 /**
221 * Channel component reference.
222 */
77735e82 223 private final TraceChannelComponent fChannel;
c56972bb 224 /**
cfdb727a 225 * The list of kernel channel components the command is to be executed on.
c56972bb 226 */
e0838ca1 227 private final List<TraceEventComponent> fEvents = new ArrayList<>();
cfdb727a 228
5293eb3f
BH
229 /**
230 * Constructor
231 * @param channel - a channel component
232 * @param events - a list of event components
233 */
c56972bb
BH
234 public Parameter(TraceChannelComponent channel, List<TraceEventComponent> events) {
235 fChannel = channel;
236 fEvents.addAll(events);
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.fChannel, other.fEvents);
245 }
cfdb727a 246
c56972bb
BH
247 /**
248 * @return the trace channel component.
249 */
250 public TraceChannelComponent getChannel() {
251 return fChannel;
252 }
cfdb727a 253
c56972bb
BH
254 /**
255 * @return a list of trace event components.
256 */
257 public List<TraceEventComponent> getEvents() {
258 return fEvents;
259 }
6503ae0f
BH
260 }
261}
This page took 0.100853 seconds and 5 git commands to generate.