Added some more JUnit tests
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.ui / src / org / eclipse / linuxtools / lttng / ui / views / control / handlers / AssignEventHandler.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 **********************************************************************/
12package org.eclipse.linuxtools.lttng.ui.views.control.handlers;
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;
26import org.eclipse.jface.window.Window;
27import org.eclipse.linuxtools.lttng.ui.LTTngUiPlugin;
28import org.eclipse.linuxtools.lttng.ui.views.control.ControlView;
29import org.eclipse.linuxtools.lttng.ui.views.control.Messages;
6503ae0f 30import org.eclipse.linuxtools.lttng.ui.views.control.dialogs.IGetEventInfoDialog;
d132bcc7 31import org.eclipse.linuxtools.lttng.ui.views.control.dialogs.TraceControlDialogFactory;
6503ae0f
BH
32import org.eclipse.linuxtools.lttng.ui.views.control.model.ITraceControlComponent;
33import org.eclipse.linuxtools.lttng.ui.views.control.model.impl.BaseEventComponent;
34import org.eclipse.linuxtools.lttng.ui.views.control.model.impl.KernelProviderComponent;
35import org.eclipse.linuxtools.lttng.ui.views.control.model.impl.TargetNodeComponent;
36import org.eclipse.linuxtools.lttng.ui.views.control.model.impl.TraceChannelComponent;
37import org.eclipse.linuxtools.lttng.ui.views.control.model.impl.TraceSessionComponent;
38import org.eclipse.linuxtools.lttng.ui.views.control.model.impl.UstProviderComponent;
39import org.eclipse.ui.IWorkbenchPage;
6503ae0f
BH
40
41/**
42 * <b><u>EnableEventHandler</u></b>
43 * <p>
44 * Command handler implementation to assign events to a session and channel and enable/configure them.
45 * This is done on the trace provider level.
46 * </p>
47 */
d132bcc7 48public class AssignEventHandler extends BaseControlViewHandler {
6503ae0f
BH
49
50 // ------------------------------------------------------------------------
51 // Attributes
52 // ------------------------------------------------------------------------
53 /**
54 * The list of event components the command is to be executed on.
55 */
56 private List<BaseEventComponent> fEvents = new ArrayList<BaseEventComponent>();
57
58 /**
59 * The list of available sessions.
60 */
61 private TraceSessionComponent[] fSessions;
62
63 /**
64 * Flag for indicating Kernel or UST.
65 */
66 Boolean fIsKernel = null;
67
68 // ------------------------------------------------------------------------
69 // Operations
70 // ------------------------------------------------------------------------
71
72 /*
73 * (non-Javadoc)
74 * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
75 */
76 @Override
77 public Object execute(ExecutionEvent event) throws ExecutionException {
78
6503ae0f 79 // Open dialog box to retrieve the session and channel where the events should be enabled in.
d132bcc7
BH
80 final IGetEventInfoDialog dialog = TraceControlDialogFactory.getInstance().getGetEventInfoDialog();
81 dialog.setIsKernel(fIsKernel);
82 dialog.setSessions(fSessions);
6503ae0f
BH
83
84 if (dialog.open() != Window.OK) {
85 return null;
86 }
87
88 Job job = new Job(Messages.TraceControl_EnableEventsJob) {
89 @Override
90 protected IStatus run(IProgressMonitor monitor) {
91
92 String errorString = null;
93 try {
94 List<String> eventNames = new ArrayList<String>();
95 // Create list of event names
96 for (Iterator<BaseEventComponent> iterator = fEvents.iterator(); iterator.hasNext();) {
97 BaseEventComponent event = (BaseEventComponent) iterator.next();
98 eventNames.add(event.getName());
99 }
100
101 TraceChannelComponent channel = dialog.getChannel();
102 if (channel == null) {
103 // enable events on default channel (which will be created by lttng-tools)
498704b3 104 dialog.getSession().enableEvents(eventNames, fIsKernel, monitor);
6503ae0f 105 } else {
498704b3 106 channel.enableEvents(eventNames, monitor);
6503ae0f
BH
107 }
108
109 } catch (ExecutionException e) {
110 errorString = e.toString() + "\n"; //$NON-NLS-1$
111 }
112
113 // get session configuration in all cases
114 try {
115 dialog.getSession().getConfigurationFromNode(monitor);
116 } catch (ExecutionException e) {
117 if (errorString == null) {
118 errorString = new String();
119 }
120 errorString += Messages.TraceControl_ListSessionFailure + ": " + e.toString(); //$NON-NLS-1$
121 }
122
123 if (errorString != null) {
124 return new Status(Status.ERROR, LTTngUiPlugin.PLUGIN_ID, errorString);
125 }
126 return Status.OK_STATUS;
127 }
128 };
129 job.setUser(true);
130 job.schedule();
131 return null;
132 }
133
134 /*
135 * (non-Javadoc)
136 * @see org.eclipse.core.commands.AbstractHandler#isEnabled()
137 */
138 @Override
139 public boolean isEnabled() {
140 fEvents.clear();
141 fSessions = null;
142 fIsKernel = null;
143
d132bcc7
BH
144 // Get workbench page for the Control View
145 IWorkbenchPage page = getWorkbenchPage();
6503ae0f
BH
146 if (page == null) {
147 return false;
148 }
149
6503ae0f
BH
150 // Check if one or more session are selected
151 ISelection selection = page.getSelection(ControlView.ID);
152 if (selection instanceof StructuredSelection) {
153
154 StructuredSelection structered = ((StructuredSelection) selection);
155 for (Iterator<?> iterator = structered.iterator(); iterator.hasNext();) {
156 Object element = (Object) iterator.next();
157 if (element instanceof BaseEventComponent) {
158 BaseEventComponent event = (BaseEventComponent) element;
159 ITraceControlComponent provider = event.getParent();
160
161 // check for kernel or UST provider
162 boolean temp = false;
163 if (provider instanceof KernelProviderComponent) {
164 temp = true;
165 } else if (provider instanceof UstProviderComponent) {
166 temp = false;
167 } else {
168 return false;
169 }
170 if (fIsKernel == null) {
171 fIsKernel = Boolean.valueOf(temp);
172 } else {
173 // don't mix events from Kernel and UST provider
174 if (fIsKernel.booleanValue() != temp) {
175 return false;
176 }
177 }
178
179 // Add BaseEventComponents
180 fEvents.add(event);
181
182 if (fSessions == null) {
183 TargetNodeComponent root = (TargetNodeComponent)event.getParent().getParent().getParent();
184 fSessions = root.getSessions();
185 }
186 }
187 }
188 }
189 return ((fEvents.size() > 0) && (fSessions != null) && (fSessions.length > 0));
190 }
191}
This page took 0.032826 seconds and 5 git commands to generate.