1
/**********************************************************************
2
* Copyright (c) 2012 Ericsson
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
10
* Bernd Hufmann - Initial API and implementation
11
**********************************************************************/
12
package org.eclipse.linuxtools.internal.lttng2.ui.views.control.handlers;
14
import java.util.ArrayList;
15
import java.util.Iterator;
16
import java.util.List;
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.core.control.model.TraceEnablement;
27
import org.eclipse.linuxtools.internal.lttng2.ui.Activator;
28
import org.eclipse.linuxtools.internal.lttng2.ui.views.control.ControlView;
29
import org.eclipse.linuxtools.internal.lttng2.ui.views.control.messages.Messages;
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.TraceDomainComponent;
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;
39
* Abstract command handler implementation to enable or disabling a trace channel.
42
* @author Bernd Hufmann
44
abstract public class ChangeChannelStateHandler extends BaseControlViewHandler {
46
// ------------------------------------------------------------------------
48
// ------------------------------------------------------------------------
50
* The command execution parameter.
52
protected Parameter fParam;
54
// ------------------------------------------------------------------------
56
// ------------------------------------------------------------------------
58
* @return the new state to set
60
abstract protected TraceEnablement getNewState();
62
// ------------------------------------------------------------------------
64
// ------------------------------------------------------------------------
66
* Changes the state of the given channels.
67
* @param domain - the domain of the channels.
68
* @param channelNames - a list of channel names
69
* @param monitor - a progress monitor
70
* @throws ExecutionException
72
abstract protected void changeState(TraceDomainComponent domain, List<String> channelNames, IProgressMonitor monitor) throws ExecutionException;
76
* @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
79
public Object execute(ExecutionEvent event) throws ExecutionException {
83
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
89
final Parameter param = new Parameter(fParam);
91
Job job = new Job(Messages.TraceControl_ChangeChannelStateJob) {
93
protected IStatus run(IProgressMonitor monitor) {
94
Exception error = null;
96
TraceSessionComponent session = null;
99
TraceDomainComponent kernelDomain = param.getKernelDomain();
100
List<TraceChannelComponent> kernelChannels = param.getKernelChannels();
102
if (kernelDomain != null) {
103
session = (TraceSessionComponent)kernelDomain.getParent();
104
List<String> channelNames = new ArrayList<String>();
105
for (Iterator<TraceChannelComponent> iterator = kernelChannels.iterator(); iterator.hasNext();) {
106
// Enable all selected channels which are disabled
107
TraceChannelComponent channel = (TraceChannelComponent) iterator.next();
108
channelNames.add(channel.getName());
111
changeState(kernelDomain, channelNames, monitor);
113
for (Iterator<TraceChannelComponent> iterator = kernelChannels.iterator(); iterator.hasNext();) {
114
// Enable all selected channels which are disabled
115
TraceChannelComponent channel = (TraceChannelComponent) iterator.next();
116
channel.setState(getNewState());
120
TraceDomainComponent ustDomain = param.getUstDomain();
121
List<TraceChannelComponent> ustChannels = param.getUstChannels();
122
if (ustDomain != null) {
123
if (session == null) {
124
session = (TraceSessionComponent)ustDomain.getParent();
127
List<String> channelNames = new ArrayList<String>();
128
for (Iterator<TraceChannelComponent> iterator = ustChannels.iterator(); iterator.hasNext();) {
129
// Enable all selected channels which are disabled
130
TraceChannelComponent channel = (TraceChannelComponent) iterator.next();
131
channelNames.add(channel.getName());
134
changeState(ustDomain, channelNames, monitor);
136
for (Iterator<TraceChannelComponent> iterator = ustChannels.iterator(); iterator.hasNext();) {
137
// Enable all selected channels which are disabled
138
TraceChannelComponent channel = (TraceChannelComponent) iterator.next();
139
channel.setState(getNewState());
142
} catch (ExecutionException e) {
146
// In all cases notify listeners
147
session.fireComponentChanged(session);
150
return new Status(Status.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_ChangeChannelStateFailure, error);
153
return Status.OK_STATUS;
167
* @see org.eclipse.core.commands.AbstractHandler#isEnabled()
170
public boolean isEnabled() {
172
// Get workbench page for the Control View
173
IWorkbenchPage page = getWorkbenchPage();
178
TraceDomainComponent kernelDomain = null;
179
TraceDomainComponent ustDomain = null;
180
List<TraceChannelComponent> kernelChannels = new ArrayList<TraceChannelComponent>();
181
List<TraceChannelComponent> ustChannels = new ArrayList<TraceChannelComponent>();
183
// Check if one or more session are selected
184
ISelection selection = page.getSelection(ControlView.ID);
185
if (selection instanceof StructuredSelection) {
186
StructuredSelection structered = ((StructuredSelection) selection);
187
String sessionName = null;
188
for (Iterator<?> iterator = structered.iterator(); iterator.hasNext();) {
189
Object element = (Object) iterator.next();
191
if (element instanceof TraceChannelComponent) {
193
// Add only TraceChannelComponents that are disabled
194
TraceChannelComponent channel = (TraceChannelComponent) element;
195
if (sessionName == null) {
196
sessionName = String.valueOf(channel.getSessionName());
199
// Enable command only for channels of same session
200
if (!sessionName.equals(channel.getSessionName())) {
201
kernelChannels.clear();
206
if ((channel.getState() != getNewState())) {
207
if (channel.isKernel()) {
208
kernelChannels.add(channel);
209
if (kernelDomain == null) {
210
kernelDomain = (TraceDomainComponent) channel.getParent();
213
ustChannels.add(channel);
214
if (ustDomain == null) {
215
ustDomain = (TraceDomainComponent) channel.getParent();
223
boolean isEnabled = (!kernelChannels.isEmpty() || !ustChannels.isEmpty());
227
fParam = new Parameter(kernelDomain, ustDomain, kernelChannels, ustChannels);
237
* Class containing parameter for the command execution.
239
static protected class Parameter {
241
* Kernel domain component reference.
243
final protected TraceDomainComponent fKernelDomain;
245
* UST domain component reference.
247
final protected TraceDomainComponent fUstDomain;
249
* The list of kernel channel components the command is to be executed on.
251
final protected List<TraceChannelComponent> fKernelChannels;
253
* The list of UST channel components the command is to be executed on.
255
final protected List<TraceChannelComponent> fUstChannels;
259
* @param kernelDomain - a kernel domain component
260
* @param ustDomain - a UST domain component
261
* @param kernelChannels - list of available kernel channels
262
* @param ustChannels - list of available UST channels
264
public Parameter(TraceDomainComponent kernelDomain, TraceDomainComponent ustDomain, List<TraceChannelComponent> kernelChannels, List<TraceChannelComponent> ustChannels) {
265
fKernelDomain = kernelDomain;
266
fUstDomain = ustDomain;
267
fKernelChannels = new ArrayList<TraceChannelComponent>();
268
fKernelChannels.addAll(kernelChannels);
269
fUstChannels = new ArrayList<TraceChannelComponent>();
270
fUstChannels.addAll(ustChannels);
275
* @param other a parameter to copy
277
public Parameter(Parameter other) {
278
this(other.fKernelDomain, other.fUstDomain, other.fKernelChannels, other.fUstChannels);
282
* @return the kernel domain component.
284
public TraceDomainComponent getKernelDomain() {
285
return fKernelDomain;
289
* @return the UST domain component.
291
public TraceDomainComponent getUstDomain() {
296
* @return the list of kernel channel components.
298
public List<TraceChannelComponent> getKernelChannels() {
299
return fKernelChannels;
303
* @return the list of UST channel components.
305
public List<TraceChannelComponent> getUstChannels() {