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