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