Commit | Line | Data |
---|---|---|
6503ae0f BH |
1 | /********************************************************************** |
2 | * Copyright (c) 2012 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 | **********************************************************************/ | |
115b4a01 | 12 | package org.eclipse.linuxtools.internal.lttng2.ui.views.control.handlers; |
6503ae0f BH |
13 | |
14 | import java.util.ArrayList; | |
15 | import java.util.Iterator; | |
16 | import java.util.List; | |
17 | ||
6503ae0f 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; | |
9315aeee | 26 | import org.eclipse.linuxtools.internal.lttng2.core.control.model.TraceEnablement; |
115b4a01 BH |
27 | import org.eclipse.linuxtools.internal.lttng2.ui.Activator; |
28 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.ControlView; | |
9315aeee | 29 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.messages.Messages; |
115b4a01 BH |
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.TraceEventComponent; | |
32 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceSessionComponent; | |
6503ae0f | 33 | import org.eclipse.ui.IWorkbenchPage; |
6503ae0f BH |
34 | import org.eclipse.ui.IWorkbenchWindow; |
35 | import 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 | */ |
498704b3 | 44 | abstract public 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 | */ | |
cfdb727a | 60 | abstract protected 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 BH |
69 | * @param monitor - a progress monitor |
70 | * @throws ExecutionException | |
71 | */ | |
cfdb727a | 72 | abstract protected void changeState(TraceChannelComponent channel, List<String> eventNames, IProgressMonitor monitor) throws ExecutionException; |
6503ae0f BH |
73 | |
74 | /* | |
75 | * (non-Javadoc) | |
76 | * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent) | |
77 | */ | |
78 | @Override | |
79 | public Object execute(ExecutionEvent event) throws ExecutionException { | |
80 | ||
81 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
82 | ||
83 | if (window == null) { | |
84 | return false; | |
85 | } | |
86 | ||
c56972bb BH |
87 | fLock.lock(); |
88 | try { | |
cfdb727a | 89 | |
c56972bb BH |
90 | final Parameter param = new Parameter(fParam); |
91 | ||
92 | Job job = new Job(Messages.TraceControl_ChangeChannelStateJob) { | |
93 | @Override | |
94 | protected IStatus run(IProgressMonitor monitor) { | |
f455db37 | 95 | Exception error = null; |
c56972bb BH |
96 | |
97 | TraceSessionComponent session = null; | |
98 | ||
99 | try { | |
100 | boolean isAll = false; | |
101 | if (param.getChannel() != null) { | |
102 | session = param.getChannel().getSession(); | |
103 | List<String> eventNames = new ArrayList<String>(); | |
104 | List<TraceEventComponent> events = param.getEvents(); | |
cfdb727a | 105 | |
c56972bb BH |
106 | for (Iterator<TraceEventComponent> iterator = events.iterator(); iterator.hasNext();) { |
107 | // Enable/disable all selected channels which are disabled | |
ea21cd65 | 108 | TraceEventComponent traceEvent = iterator.next(); |
c56972bb BH |
109 | |
110 | // Workaround for wildcard handling in lttng-tools | |
ea21cd65 | 111 | if ("*".equals(traceEvent.getName())) { //$NON-NLS-1$ |
c56972bb | 112 | isAll = true; |
cfdb727a | 113 | } else { |
ea21cd65 | 114 | eventNames.add(traceEvent.getName()); |
c56972bb BH |
115 | } |
116 | } | |
117 | if (isAll) { | |
118 | changeState(param.getChannel(), null, monitor); | |
3e91c9c0 | 119 | } |
3e91c9c0 | 120 | |
c56972bb BH |
121 | if (!eventNames.isEmpty()) { |
122 | changeState(param.getChannel(), eventNames, monitor); | |
123 | } | |
6503ae0f | 124 | |
c56972bb BH |
125 | for (Iterator<TraceEventComponent> iterator = events.iterator(); iterator.hasNext();) { |
126 | // Enable all selected channels which are disabled | |
cfdb727a | 127 | TraceEventComponent ev = iterator.next(); |
c56972bb BH |
128 | ev.setState(getNewState()); |
129 | } | |
6503ae0f | 130 | } |
c56972bb | 131 | } catch (ExecutionException e) { |
f455db37 | 132 | error = e; |
6503ae0f | 133 | } |
6503ae0f | 134 | |
c56972bb | 135 | if (session != null) { |
cfdb727a | 136 | // In all cases notify listeners |
c56972bb BH |
137 | session.fireComponentChanged(session); |
138 | } | |
6503ae0f | 139 | |
f455db37 | 140 | if (error != null) { |
cfdb727a | 141 | return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_ChangeEventStateFailure, error); |
c56972bb | 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); | |
cfdb727a | 169 | |
c56972bb BH |
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; | |
cfdb727a | 177 | |
6503ae0f | 178 | for (Iterator<?> iterator = structered.iterator(); iterator.hasNext();) { |
cfdb727a AM |
179 | Object element = iterator.next(); |
180 | ||
6503ae0f | 181 | if (element instanceof TraceEventComponent) { |
cfdb727a | 182 | |
3e91c9c0 | 183 | TraceEventComponent event = (TraceEventComponent) element; |
6503ae0f | 184 | if (sessionName == null) { |
3e91c9c0 | 185 | sessionName = String.valueOf(event.getSessionName()); |
6503ae0f | 186 | } |
cfdb727a | 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 | /** | |
cfdb727a | 225 | * Class containing parameter for the command execution. |
6503ae0f | 226 | */ |
5293eb3f | 227 | static protected class Parameter { |
c56972bb BH |
228 | /** |
229 | * Channel component reference. | |
230 | */ | |
231 | final private TraceChannelComponent fChannel; | |
232 | /** | |
cfdb727a | 233 | * The list of kernel channel components the command is to be executed on. |
c56972bb BH |
234 | */ |
235 | final private List<TraceEventComponent> fEvents = new ArrayList<TraceEventComponent>(); | |
cfdb727a | 236 | |
5293eb3f BH |
237 | /** |
238 | * Constructor | |
239 | * @param channel - a channel component | |
240 | * @param events - a list of event components | |
241 | */ | |
c56972bb BH |
242 | public Parameter(TraceChannelComponent channel, List<TraceEventComponent> events) { |
243 | fChannel = channel; | |
244 | fEvents.addAll(events); | |
245 | } | |
cfdb727a | 246 | |
c56972bb BH |
247 | /** |
248 | * Copy constructor | |
249 | * @param other - a parameter to copy | |
250 | */ | |
251 | public Parameter(Parameter other) { | |
252 | this(other.fChannel, other.fEvents); | |
253 | } | |
cfdb727a | 254 | |
c56972bb BH |
255 | /** |
256 | * @return the trace channel component. | |
257 | */ | |
258 | public TraceChannelComponent getChannel() { | |
259 | return fChannel; | |
260 | } | |
cfdb727a | 261 | |
c56972bb BH |
262 | /** |
263 | * @return a list of trace event components. | |
264 | */ | |
265 | public List<TraceEventComponent> getEvents() { | |
266 | return fEvents; | |
267 | } | |
6503ae0f BH |
268 | } |
269 | } |