Add support for importing traces to tracing project
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.ui / src / org / eclipse / linuxtools / internal / lttng2 / ui / views / control / handlers / AssignEventHandler.java
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 **********************************************************************/
12 package org.eclipse.linuxtools.internal.lttng2.ui.views.control.handlers;
13
14 import java.util.ArrayList;
15 import java.util.Arrays;
16 import java.util.Iterator;
17 import java.util.List;
18
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;
28 import org.eclipse.linuxtools.internal.lttng2.ui.Activator;
29 import org.eclipse.linuxtools.internal.lttng2.ui.views.control.ControlView;
30 import org.eclipse.linuxtools.internal.lttng2.ui.views.control.Messages;
31 import org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.IGetEventInfoDialog;
32 import org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.TraceControlDialogFactory;
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;
40 import org.eclipse.ui.IWorkbenchPage;
41
42 /**
43 * <b><u>AssignEventHandler</u></b>
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 public class AssignEventHandler extends BaseControlViewHandler {
50
51 // ------------------------------------------------------------------------
52 // Attributes
53 // ------------------------------------------------------------------------
54 /**
55 * The command execution parameter.
56 */
57 private Parameter fParam;
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
70 fLock.lock();
71 try {
72 // Make a copy for thread safety
73 final Parameter param = new Parameter(fParam);
74
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());
79
80 if (dialog.open() != Window.OK) {
81 return null;
82 }
83
84 Job job = new Job(Messages.TraceControl_EnableEventsJob) {
85 @Override
86 protected IStatus run(IProgressMonitor monitor) {
87
88 StringBuffer errorString = new StringBuffer();
89 try {
90 List<String> eventNames = new ArrayList<String>();
91 List<BaseEventComponent> events = param.getEvents();
92 // Create list of event names
93 for (Iterator<BaseEventComponent> iterator = events.iterator(); iterator.hasNext();) {
94 BaseEventComponent event = (BaseEventComponent) iterator.next();
95 eventNames.add(event.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(), monitor);
102 } else {
103 channel.enableEvents(eventNames, monitor);
104 }
105
106 } catch (ExecutionException e) {
107 errorString.append(e.toString());
108 errorString.append('\n');
109 }
110
111 // get session configuration in all cases
112 try {
113 dialog.getSession().getConfigurationFromNode(monitor);
114 } catch (ExecutionException e) {
115 errorString.append(Messages.TraceControl_ListSessionFailure);
116 errorString.append(": "); //$NON-NLS-1$
117 errorString.append(e.toString());
118 }
119
120 if (errorString.length() > 0) {
121 return new Status(Status.ERROR, Activator.PLUGIN_ID, errorString.toString());
122 }
123 return Status.OK_STATUS;
124 }
125 };
126 job.setUser(true);
127 job.schedule();
128 } finally {
129 fLock.unlock();
130 }
131
132 return null;
133 }
134
135 /*
136 * (non-Javadoc)
137 * @see org.eclipse.core.commands.AbstractHandler#isEnabled()
138 */
139 @Override
140 public boolean isEnabled() {
141 ArrayList<BaseEventComponent> events = new ArrayList<BaseEventComponent>();
142 TraceSessionComponent[] sessions = null;
143 Boolean isKernel = null;
144
145 // Get workbench page for the Control View
146 IWorkbenchPage page = getWorkbenchPage();
147 if (page == null) {
148 return false;
149 }
150
151 // Check if one or more session are selected
152 ISelection selection = page.getSelection(ControlView.ID);
153 if (selection instanceof StructuredSelection) {
154
155 StructuredSelection structered = ((StructuredSelection) selection);
156 for (Iterator<?> iterator = structered.iterator(); iterator.hasNext();) {
157 Object element = (Object) iterator.next();
158 if (element instanceof BaseEventComponent) {
159 BaseEventComponent event = (BaseEventComponent) element;
160 ITraceControlComponent provider = event.getParent();
161
162 // check for kernel or UST provider
163 boolean temp = false;
164 if (provider instanceof KernelProviderComponent) {
165 temp = true;
166 } else if (provider instanceof UstProviderComponent) {
167 temp = false;
168 } else {
169 return false;
170 }
171 if (isKernel == null) {
172 isKernel = Boolean.valueOf(temp);
173 } else {
174 // don't mix events from Kernel and UST provider
175 if (isKernel.booleanValue() != temp) {
176 return false;
177 }
178 }
179
180 // Add BaseEventComponents
181 events.add(event);
182
183 if (sessions == null) {
184 TargetNodeComponent root = (TargetNodeComponent)event.getParent().getParent().getParent();
185 sessions = root.getSessions();
186 }
187 }
188 }
189 }
190
191 boolean isEnabled = ((!events.isEmpty()) && (sessions != null) && (sessions.length > 0));
192 fLock.lock();
193 try {
194 fParam = null;
195 if(isEnabled) {
196 fParam = new Parameter(sessions, events, isKernel);
197 }
198 } finally {
199 fLock.unlock();
200 }
201 return isEnabled;
202 }
203
204 /**
205 * Class containing parameter for the command execution.
206 */
207 final static private class Parameter {
208
209 /**
210 * The list of event components the command is to be executed on.
211 */
212 private List<BaseEventComponent> fEvents;
213
214 /**
215 * The list of available sessions.
216 */
217 final private TraceSessionComponent[] fSessions;
218
219 /**
220 * Flag for indicating Kernel or UST.
221 */
222 final private boolean fIsKernel;
223
224 /**
225 * Constructor
226 *
227 * @param sessions - a array of trace sessions
228 * @param events - a lists of events to enable
229 * @param isKernel - domain (true for kernel or UST)
230 */
231 public Parameter(TraceSessionComponent[] sessions, List<BaseEventComponent> events, boolean isKernel) {
232 fSessions = Arrays.copyOf(sessions, sessions.length);
233 fEvents = new ArrayList<BaseEventComponent>();
234 fEvents.addAll(events);
235 fIsKernel = isKernel;
236 }
237
238 /**
239 * Copy constructor
240 * @param other - a parameter to copy
241 */
242 public Parameter(Parameter other) {
243 this(other.fSessions, other.fEvents, other.fIsKernel);
244 }
245
246 public TraceSessionComponent[] getSessions() {
247 return fSessions;
248 }
249
250 public List<BaseEventComponent> getEvents() {
251 return fEvents;
252 }
253
254 public boolean isKernel() {
255 return fIsKernel;
256 }
257 }
258 }
This page took 0.035712 seconds and 5 git commands to generate.