Improve Exception handling for LTTng 2.0 trace control
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.ui / src / org / eclipse / linuxtools / internal / lttng2 / ui / views / control / handlers / ChangeEventStateHandler.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.Iterator;
16 import java.util.List;
17
18 import org.eclipse.core.commands.ExecutionEvent;
19 import org.eclipse.core.commands.ExecutionException;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.core.runtime.Status;
23 import org.eclipse.core.runtime.jobs.Job;
24 import org.eclipse.jface.viewers.ISelection;
25 import org.eclipse.jface.viewers.StructuredSelection;
26 import org.eclipse.linuxtools.internal.lttng2.ui.Activator;
27 import org.eclipse.linuxtools.internal.lttng2.ui.views.control.ControlView;
28 import org.eclipse.linuxtools.internal.lttng2.ui.views.control.Messages;
29 import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.TraceEnablement;
30 import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceChannelComponent;
31 import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceEventComponent;
32 import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceSessionComponent;
33 import org.eclipse.ui.IWorkbenchPage;
34 import org.eclipse.ui.IWorkbenchWindow;
35 import org.eclipse.ui.PlatformUI;
36
37 /**
38 * <b><u>EnableChannelHandler</u></b>
39 * <p>
40 * Base Command handler implementation to enable or disabling a trace channel.
41 * </p>
42 */
43 abstract public class ChangeEventStateHandler extends BaseControlViewHandler {
44
45 // ------------------------------------------------------------------------
46 // Attributes
47 // ------------------------------------------------------------------------
48 /**
49 * The command execution parameter.
50 */
51 protected Parameter fParam;
52
53 // ------------------------------------------------------------------------
54 // Accessors
55 // ------------------------------------------------------------------------
56 /**
57 * @return the new state to set
58 */
59 abstract protected TraceEnablement getNewState();
60
61 // ------------------------------------------------------------------------
62 // Operations
63 // ------------------------------------------------------------------------
64 /**
65 * Change the state
66 * @param channel - channel of events to be enabled
67 * @param eventNames - list event names
68 * @param monitor - a progress monitor
69 * @throws ExecutionException
70 */
71 abstract protected void changeState(TraceChannelComponent channel, List<String> eventNames, IProgressMonitor monitor) throws ExecutionException;
72
73 /*
74 * (non-Javadoc)
75 * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
76 */
77 @Override
78 public Object execute(ExecutionEvent event) throws ExecutionException {
79
80 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
81
82 if (window == null) {
83 return false;
84 }
85
86 fLock.lock();
87 try {
88
89 final Parameter param = new Parameter(fParam);
90
91 Job job = new Job(Messages.TraceControl_ChangeChannelStateJob) {
92 @Override
93 protected IStatus run(IProgressMonitor monitor) {
94 Exception error = null;
95
96 TraceSessionComponent session = null;
97
98 try {
99 boolean isAll = false;
100 if (param.getChannel() != null) {
101 session = param.getChannel().getSession();
102 List<String> eventNames = new ArrayList<String>();
103 List<TraceEventComponent> events = param.getEvents();
104
105 for (Iterator<TraceEventComponent> iterator = events.iterator(); iterator.hasNext();) {
106 // Enable/disable all selected channels which are disabled
107 TraceEventComponent event = (TraceEventComponent) iterator.next();
108
109 // Workaround for wildcard handling in lttng-tools
110 if ("*".equals(event.getName())) { //$NON-NLS-1$
111 isAll = true;
112 } else {
113 eventNames.add(event.getName());
114 }
115 }
116 if (isAll) {
117 changeState(param.getChannel(), null, monitor);
118 }
119
120 if (!eventNames.isEmpty()) {
121 changeState(param.getChannel(), eventNames, monitor);
122 }
123
124 for (Iterator<TraceEventComponent> iterator = events.iterator(); iterator.hasNext();) {
125 // Enable all selected channels which are disabled
126 TraceEventComponent ev = (TraceEventComponent) iterator.next();
127 ev.setState(getNewState());
128 }
129 }
130 } catch (ExecutionException e) {
131 error = e;
132 }
133
134 if (session != null) {
135 // In all cases notify listeners
136 session.fireComponentChanged(session);
137 }
138
139 if (error != null) {
140 return new Status(Status.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_ChangeEventStateFailure, error);
141 }
142
143 return Status.OK_STATUS;
144 }
145 };
146 job.setUser(true);
147 job.schedule();
148 } finally {
149 fLock.unlock();
150 }
151 return null;
152 }
153
154 /*
155 * (non-Javadoc)
156 * @see org.eclipse.core.commands.AbstractHandler#isEnabled()
157 */
158 @Override
159 public boolean isEnabled() {
160 // Get workbench page for the Control View
161 IWorkbenchPage page = getWorkbenchPage();
162 if (page == null) {
163 return false;
164 }
165
166 // Check if one or more session are selected
167 ISelection selection = page.getSelection(ControlView.ID);
168
169 TraceChannelComponent channel = null;
170 List<TraceEventComponent> events = new ArrayList<TraceEventComponent>();
171
172 if (selection instanceof StructuredSelection) {
173 StructuredSelection structered = ((StructuredSelection) selection);
174 String sessionName = null;
175 String channelName = null;
176
177 for (Iterator<?> iterator = structered.iterator(); iterator.hasNext();) {
178 Object element = (Object) iterator.next();
179
180 if (element instanceof TraceEventComponent) {
181
182 TraceEventComponent event = (TraceEventComponent) element;
183 if (sessionName == null) {
184 sessionName = String.valueOf(event.getSessionName());
185 }
186
187 if (channel == null) {
188 channel = (TraceChannelComponent)event.getParent();
189 }
190
191 if (channelName == null) {
192 channelName = event.getChannelName();
193 }
194
195 // Enable command only for events of same session, same channel and domain
196 if ((!sessionName.equals(event.getSessionName())) ||
197 (!channelName.equals(event.getChannelName())) ||
198 (channel.isKernel() != event.isKernel())) {
199 events.clear();
200 break;
201 }
202
203 if ((event.getState() != getNewState())) {
204 events.add(event);
205 }
206 }
207 }
208 }
209 boolean isEnabled = !events.isEmpty();
210
211 fLock.lock();
212 try {
213 fParam = null;
214 if (isEnabled) {
215 fParam = new Parameter(channel, events);
216 }
217 } finally {
218 fLock.unlock();
219 }
220 return isEnabled;
221 }
222
223 /**
224 * Class containing parameter for the command execution.
225 */
226 static protected class Parameter {
227 /**
228 * Channel component reference.
229 */
230 final private TraceChannelComponent fChannel;
231 /**
232 * The list of kernel channel components the command is to be executed on.
233 */
234 final private List<TraceEventComponent> fEvents = new ArrayList<TraceEventComponent>();
235
236 /**
237 * Constructor
238 * @param channel - a channel component
239 * @param events - a list of event components
240 */
241 public Parameter(TraceChannelComponent channel, List<TraceEventComponent> events) {
242 fChannel = channel;
243 fEvents.addAll(events);
244 }
245
246 /**
247 * Copy constructor
248 * @param other - a parameter to copy
249 */
250 public Parameter(Parameter other) {
251 this(other.fChannel, other.fEvents);
252 }
253
254 /**
255 * @return the trace channel component.
256 */
257 public TraceChannelComponent getChannel() {
258 return fChannel;
259 }
260
261 /**
262 * @return a list of trace event components.
263 */
264 public List<TraceEventComponent> getEvents() {
265 return fEvents;
266 }
267 }
268 }
This page took 0.037162 seconds and 6 git commands to generate.