Commit | Line | Data |
---|---|---|
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 | 12 | package org.eclipse.linuxtools.internal.lttng2.ui.views.control.handlers; |
6503ae0f BH |
13 | |
14 | import java.util.ArrayList; | |
c56972bb | 15 | import java.util.Arrays; |
6503ae0f BH |
16 | import java.util.Iterator; |
17 | import java.util.List; | |
18 | ||
6503ae0f BH |
19 | import org.eclipse.core.commands.ExecutionEvent; |
20 | import org.eclipse.core.commands.ExecutionException; | |
21 | import org.eclipse.core.runtime.IProgressMonitor; | |
22 | import org.eclipse.core.runtime.IStatus; | |
23 | import org.eclipse.core.runtime.Status; | |
24 | import org.eclipse.core.runtime.jobs.Job; | |
25 | import org.eclipse.jface.viewers.ISelection; | |
26 | import org.eclipse.jface.viewers.StructuredSelection; | |
27 | import org.eclipse.jface.window.Window; | |
115b4a01 BH |
28 | import org.eclipse.linuxtools.internal.lttng2.ui.Activator; |
29 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.ControlView; | |
115b4a01 BH |
30 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.IGetEventInfoDialog; |
31 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.TraceControlDialogFactory; | |
9315aeee | 32 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.messages.Messages; |
115b4a01 BH |
33 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent; |
34 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.BaseEventComponent; | |
35 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.KernelProviderComponent; | |
36 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TargetNodeComponent; | |
37 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceChannelComponent; | |
38 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceSessionComponent; | |
39 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.UstProviderComponent; | |
6503ae0f | 40 | import org.eclipse.ui.IWorkbenchPage; |
6503ae0f BH |
41 | |
42 | /** | |
c56972bb | 43 | * <b><u>AssignEventHandler</u></b> |
6503ae0f BH |
44 | * <p> |
45 | * Command handler implementation to assign events to a session and channel and enable/configure them. | |
46 | * This is done on the trace provider level. | |
47 | * </p> | |
48 | */ | |
d132bcc7 | 49 | public class AssignEventHandler extends BaseControlViewHandler { |
6503ae0f BH |
50 | |
51 | // ------------------------------------------------------------------------ | |
52 | // Attributes | |
53 | // ------------------------------------------------------------------------ | |
54 | /** | |
c56972bb | 55 | * The command execution parameter. |
6503ae0f | 56 | */ |
c56972bb | 57 | private Parameter fParam; |
6503ae0f BH |
58 | |
59 | // ------------------------------------------------------------------------ | |
60 | // Operations | |
61 | // ------------------------------------------------------------------------ | |
62 | ||
63 | /* | |
64 | * (non-Javadoc) | |
65 | * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent) | |
66 | */ | |
67 | @Override | |
68 | public Object execute(ExecutionEvent event) throws ExecutionException { | |
69 | ||
c56972bb BH |
70 | fLock.lock(); |
71 | try { | |
72 | // Make a copy for thread safety | |
73 | final Parameter param = new Parameter(fParam); | |
6503ae0f | 74 | |
c56972bb BH |
75 | // Open dialog box to retrieve the session and channel where the events should be enabled in. |
76 | final IGetEventInfoDialog dialog = TraceControlDialogFactory.getInstance().getGetEventInfoDialog(); | |
77 | dialog.setIsKernel(param.isKernel()); | |
78 | dialog.setSessions(param.getSessions()); | |
6503ae0f | 79 | |
c56972bb BH |
80 | if (dialog.open() != Window.OK) { |
81 | return null; | |
82 | } | |
6503ae0f | 83 | |
c56972bb BH |
84 | Job job = new Job(Messages.TraceControl_EnableEventsJob) { |
85 | @Override | |
86 | protected IStatus run(IProgressMonitor monitor) { | |
6503ae0f | 87 | |
f455db37 BH |
88 | Exception error = null; |
89 | ||
c56972bb BH |
90 | try { |
91 | List<String> eventNames = new ArrayList<String>(); | |
92 | List<BaseEventComponent> events = param.getEvents(); | |
93 | // Create list of event names | |
94 | for (Iterator<BaseEventComponent> iterator = events.iterator(); iterator.hasNext();) { | |
95 | BaseEventComponent event = (BaseEventComponent) iterator.next(); | |
96 | eventNames.add(event.getName()); | |
97 | } | |
6503ae0f | 98 | |
c56972bb BH |
99 | TraceChannelComponent channel = dialog.getChannel(); |
100 | if (channel == null) { | |
101 | // enable events on default channel (which will be created by lttng-tools) | |
102 | dialog.getSession().enableEvents(eventNames, param.isKernel(), monitor); | |
103 | } else { | |
104 | channel.enableEvents(eventNames, monitor); | |
105 | } | |
106 | ||
107 | } catch (ExecutionException e) { | |
f455db37 | 108 | error = e; |
6503ae0f | 109 | } |
6503ae0f | 110 | |
f455db37 BH |
111 | // refresh in all cases |
112 | refresh(new CommandParameter(dialog.getSession())); | |
c56972bb | 113 | |
f455db37 BH |
114 | if (error != null) { |
115 | return new Status(Status.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_EnableEventsFailure, error); | |
c56972bb BH |
116 | } |
117 | return Status.OK_STATUS; | |
6503ae0f | 118 | } |
c56972bb BH |
119 | }; |
120 | job.setUser(true); | |
121 | job.schedule(); | |
122 | } finally { | |
123 | fLock.unlock(); | |
124 | } | |
125 | ||
6503ae0f BH |
126 | return null; |
127 | } | |
128 | ||
129 | /* | |
130 | * (non-Javadoc) | |
131 | * @see org.eclipse.core.commands.AbstractHandler#isEnabled() | |
132 | */ | |
133 | @Override | |
134 | public boolean isEnabled() { | |
c56972bb BH |
135 | ArrayList<BaseEventComponent> events = new ArrayList<BaseEventComponent>(); |
136 | TraceSessionComponent[] sessions = null; | |
137 | Boolean isKernel = null; | |
6503ae0f | 138 | |
c56972bb | 139 | // Get workbench page for the Control View |
d132bcc7 | 140 | IWorkbenchPage page = getWorkbenchPage(); |
6503ae0f BH |
141 | if (page == null) { |
142 | return false; | |
143 | } | |
144 | ||
6503ae0f BH |
145 | // Check if one or more session are selected |
146 | ISelection selection = page.getSelection(ControlView.ID); | |
147 | if (selection instanceof StructuredSelection) { | |
148 | ||
149 | StructuredSelection structered = ((StructuredSelection) selection); | |
150 | for (Iterator<?> iterator = structered.iterator(); iterator.hasNext();) { | |
151 | Object element = (Object) iterator.next(); | |
152 | if (element instanceof BaseEventComponent) { | |
153 | BaseEventComponent event = (BaseEventComponent) element; | |
154 | ITraceControlComponent provider = event.getParent(); | |
155 | ||
156 | // check for kernel or UST provider | |
157 | boolean temp = false; | |
158 | if (provider instanceof KernelProviderComponent) { | |
159 | temp = true; | |
160 | } else if (provider instanceof UstProviderComponent) { | |
161 | temp = false; | |
162 | } else { | |
163 | return false; | |
164 | } | |
c56972bb BH |
165 | if (isKernel == null) { |
166 | isKernel = Boolean.valueOf(temp); | |
6503ae0f BH |
167 | } else { |
168 | // don't mix events from Kernel and UST provider | |
c56972bb | 169 | if (isKernel.booleanValue() != temp) { |
6503ae0f BH |
170 | return false; |
171 | } | |
172 | } | |
173 | ||
174 | // Add BaseEventComponents | |
c56972bb | 175 | events.add(event); |
6503ae0f | 176 | |
c56972bb | 177 | if (sessions == null) { |
6503ae0f | 178 | TargetNodeComponent root = (TargetNodeComponent)event.getParent().getParent().getParent(); |
c56972bb | 179 | sessions = root.getSessions(); |
6503ae0f BH |
180 | } |
181 | } | |
182 | } | |
183 | } | |
c56972bb BH |
184 | |
185 | boolean isEnabled = ((!events.isEmpty()) && (sessions != null) && (sessions.length > 0)); | |
186 | fLock.lock(); | |
187 | try { | |
188 | fParam = null; | |
189 | if(isEnabled) { | |
190 | fParam = new Parameter(sessions, events, isKernel); | |
191 | } | |
192 | } finally { | |
193 | fLock.unlock(); | |
194 | } | |
195 | return isEnabled; | |
196 | } | |
197 | ||
198 | /** | |
199 | * Class containing parameter for the command execution. | |
200 | */ | |
201 | final static private class Parameter { | |
202 | ||
203 | /** | |
204 | * The list of event components the command is to be executed on. | |
205 | */ | |
206 | private List<BaseEventComponent> fEvents; | |
207 | ||
208 | /** | |
209 | * The list of available sessions. | |
210 | */ | |
211 | final private TraceSessionComponent[] fSessions; | |
212 | ||
213 | /** | |
214 | * Flag for indicating Kernel or UST. | |
215 | */ | |
216 | final private boolean fIsKernel; | |
217 | ||
218 | /** | |
219 | * Constructor | |
220 | * | |
221 | * @param sessions - a array of trace sessions | |
222 | * @param events - a lists of events to enable | |
223 | * @param isKernel - domain (true for kernel or UST) | |
224 | */ | |
225 | public Parameter(TraceSessionComponent[] sessions, List<BaseEventComponent> events, boolean isKernel) { | |
226 | fSessions = Arrays.copyOf(sessions, sessions.length); | |
227 | fEvents = new ArrayList<BaseEventComponent>(); | |
228 | fEvents.addAll(events); | |
229 | fIsKernel = isKernel; | |
230 | } | |
231 | ||
232 | /** | |
233 | * Copy constructor | |
234 | * @param other - a parameter to copy | |
235 | */ | |
236 | public Parameter(Parameter other) { | |
237 | this(other.fSessions, other.fEvents, other.fIsKernel); | |
238 | } | |
239 | ||
240 | public TraceSessionComponent[] getSessions() { | |
241 | return fSessions; | |
242 | } | |
243 | ||
244 | public List<BaseEventComponent> getEvents() { | |
245 | return fEvents; | |
246 | } | |
247 | ||
248 | public boolean isKernel() { | |
249 | return fIsKernel; | |
250 | } | |
6503ae0f BH |
251 | } |
252 | } |