Fix concurrency issue for TMF Sequence Diagram Loader
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.ui / src / org / eclipse / linuxtools / internal / lttng2 / ui / views / control / handlers / ChangeEventStateHandler.java
CommitLineData
6503ae0f
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 12package org.eclipse.linuxtools.internal.lttng2.ui.views.control.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;
115b4a01
BH
26import org.eclipse.linuxtools.internal.lttng2.ui.Activator;
27import org.eclipse.linuxtools.internal.lttng2.ui.views.control.ControlView;
28import org.eclipse.linuxtools.internal.lttng2.ui.views.control.Messages;
29import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.TraceEnablement;
30import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceChannelComponent;
31import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceEventComponent;
32import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceSessionComponent;
6503ae0f 33import org.eclipse.ui.IWorkbenchPage;
6503ae0f
BH
34import org.eclipse.ui.IWorkbenchWindow;
35import org.eclipse.ui.PlatformUI;
36
37/**
38 * <b><u>EnableChannelHandler</u></b>
39 * <p>
40 * Base Command handler implementation to enable or disabling a trace channel.
41 * </p>
42 */
498704b3 43abstract public class ChangeEventStateHandler extends BaseControlViewHandler {
6503ae0f
BH
44
45 // ------------------------------------------------------------------------
46 // Attributes
47 // ------------------------------------------------------------------------
48 /**
c56972bb 49 * The command execution parameter.
6503ae0f 50 */
c56972bb 51 protected Parameter fParam;
6503ae0f
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 /**
65 * Change the state
66 * @param channel - channel of events to be enabled
67 * @param eventNames - list event names
68 * @param monitor - a progress monitor
69 * @throws ExecutionException
70 */
71 abstract protected void changeState(TraceChannelComponent channel, List<String> eventNames, 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 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
81
82 if (window == null) {
83 return false;
84 }
85
c56972bb
BH
86 fLock.lock();
87 try {
88
89 final Parameter param = new Parameter(fParam);
90
91 Job job = new Job(Messages.TraceControl_ChangeChannelStateJob) {
92 @Override
93 protected IStatus run(IProgressMonitor monitor) {
94 StringBuffer errorString = new StringBuffer();
95
96 TraceSessionComponent session = null;
97
98 try {
99 boolean isAll = false;
100 if (param.getChannel() != null) {
101 session = param.getChannel().getSession();
102 List<String> eventNames = new ArrayList<String>();
103 List<TraceEventComponent> events = param.getEvents();
3e91c9c0 104
c56972bb
BH
105 for (Iterator<TraceEventComponent> iterator = events.iterator(); iterator.hasNext();) {
106 // Enable/disable all selected channels which are disabled
107 TraceEventComponent event = (TraceEventComponent) iterator.next();
108
109 // Workaround for wildcard handling in lttng-tools
110 if ("*".equals(event.getName())) { //$NON-NLS-1$
111 isAll = true;
112 } else {
113 eventNames.add(event.getName());
114 }
115 }
116 if (isAll) {
117 changeState(param.getChannel(), null, monitor);
3e91c9c0 118 }
3e91c9c0 119
c56972bb
BH
120 if (!eventNames.isEmpty()) {
121 changeState(param.getChannel(), eventNames, monitor);
122 }
6503ae0f 123
c56972bb
BH
124 for (Iterator<TraceEventComponent> iterator = events.iterator(); iterator.hasNext();) {
125 // Enable all selected channels which are disabled
126 TraceEventComponent ev = (TraceEventComponent) iterator.next();
127 ev.setState(getNewState());
128 }
6503ae0f 129 }
c56972bb
BH
130 } catch (ExecutionException e) {
131 errorString.append(e.toString());
132 errorString.append('\n');
6503ae0f 133 }
6503ae0f 134
c56972bb
BH
135 if (session != null) {
136 // In all cases notify listeners
137 session.fireComponentChanged(session);
138 }
6503ae0f 139
c56972bb
BH
140 if (errorString.length() > 0) {
141 return new Status(Status.ERROR, Activator.PLUGIN_ID, errorString.toString());
142 }
6503ae0f 143
c56972bb
BH
144 return Status.OK_STATUS;
145 }
146 };
147 job.setUser(true);
148 job.schedule();
149 } finally {
150 fLock.unlock();
151 }
6503ae0f
BH
152 return null;
153 }
154
155 /*
156 * (non-Javadoc)
157 * @see org.eclipse.core.commands.AbstractHandler#isEnabled()
158 */
159 @Override
160 public boolean isEnabled() {
498704b3
BH
161 // Get workbench page for the Control View
162 IWorkbenchPage page = getWorkbenchPage();
6503ae0f
BH
163 if (page == null) {
164 return false;
165 }
166
6503ae0f
BH
167 // Check if one or more session are selected
168 ISelection selection = page.getSelection(ControlView.ID);
c56972bb
BH
169
170 TraceChannelComponent channel = null;
171 List<TraceEventComponent> events = new ArrayList<TraceEventComponent>();
172
6503ae0f
BH
173 if (selection instanceof StructuredSelection) {
174 StructuredSelection structered = ((StructuredSelection) selection);
175 String sessionName = null;
176 String channelName = null;
177
178 for (Iterator<?> iterator = structered.iterator(); iterator.hasNext();) {
179 Object element = (Object) iterator.next();
180
181 if (element instanceof TraceEventComponent) {
182
3e91c9c0 183 TraceEventComponent event = (TraceEventComponent) element;
6503ae0f 184 if (sessionName == null) {
3e91c9c0 185 sessionName = String.valueOf(event.getSessionName());
6503ae0f
BH
186 }
187
c56972bb
BH
188 if (channel == null) {
189 channel = (TraceChannelComponent)event.getParent();
6503ae0f
BH
190 }
191
192 if (channelName == null) {
3e91c9c0 193 channelName = event.getChannelName();
6503ae0f 194 }
3e91c9c0 195
6503ae0f 196 // Enable command only for events of same session, same channel and domain
3e91c9c0
BH
197 if ((!sessionName.equals(event.getSessionName())) ||
198 (!channelName.equals(event.getChannelName())) ||
c56972bb
BH
199 (channel.isKernel() != event.isKernel())) {
200 events.clear();
6503ae0f
BH
201 break;
202 }
203
3e91c9c0 204 if ((event.getState() != getNewState())) {
c56972bb 205 events.add(event);
6503ae0f
BH
206 }
207 }
208 }
209 }
c56972bb
BH
210 boolean isEnabled = !events.isEmpty();
211
212 fLock.lock();
213 try {
214 fParam = null;
215 if (isEnabled) {
216 fParam = new Parameter(channel, events);
217 }
218 } finally {
219 fLock.unlock();
220 }
221 return isEnabled;
6503ae0f
BH
222 }
223
224 /**
c56972bb 225 * Class containing parameter for the command execution.
6503ae0f 226 */
c56972bb
BH
227 protected class Parameter {
228 /**
229 * Channel component reference.
230 */
231 final private TraceChannelComponent fChannel;
232 /**
233 * The list of kernel channel components the command is to be executed on.
234 */
235 final private List<TraceEventComponent> fEvents = new ArrayList<TraceEventComponent>();
236
237 public Parameter(TraceChannelComponent channel, List<TraceEventComponent> events) {
238 fChannel = channel;
239 fEvents.addAll(events);
240 }
241
242 /**
243 * Copy constructor
244 * @param other - a parameter to copy
245 */
246 public Parameter(Parameter other) {
247 this(other.fChannel, other.fEvents);
248 }
249
250 /**
251 * @return the trace channel component.
252 */
253 public TraceChannelComponent getChannel() {
254 return fChannel;
255 }
256
257 /**
258 * @return a list of trace event components.
259 */
260 public List<TraceEventComponent> getEvents() {
261 return fEvents;
262 }
6503ae0f
BH
263 }
264}
This page took 0.037212 seconds and 5 git commands to generate.