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