1 /**********************************************************************
2 * Copyright (c) 2012, 2014 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 public abstract 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 protected abstract 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 If the command fails
72 protected abstract void changeState(TraceDomainComponent domain
, List
<String
> channelNames
, IProgressMonitor monitor
) throws ExecutionException
;
75 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
79 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
85 final Parameter param
= new Parameter(fParam
);
87 Job job
= new Job(Messages
.TraceControl_ChangeChannelStateJob
) {
89 protected IStatus
run(IProgressMonitor monitor
) {
90 Exception error
= null;
92 TraceSessionComponent session
= null;
95 TraceDomainComponent kernelDomain
= param
.getKernelDomain();
96 List
<TraceChannelComponent
> kernelChannels
= param
.getKernelChannels();
98 if (kernelDomain
!= null) {
99 session
= (TraceSessionComponent
)kernelDomain
.getParent();
100 List
<String
> channelNames
= new ArrayList
<>();
101 for (Iterator
<TraceChannelComponent
> iterator
= kernelChannels
.iterator(); iterator
.hasNext();) {
102 // Enable all selected channels which are disabled
103 TraceChannelComponent channel
= iterator
.next();
104 channelNames
.add(channel
.getName());
107 changeState(kernelDomain
, channelNames
, monitor
);
109 for (Iterator
<TraceChannelComponent
> iterator
= kernelChannels
.iterator(); iterator
.hasNext();) {
110 // Enable all selected channels which are disabled
111 TraceChannelComponent channel
= iterator
.next();
112 channel
.setState(getNewState());
116 TraceDomainComponent ustDomain
= param
.getUstDomain();
117 List
<TraceChannelComponent
> ustChannels
= param
.getUstChannels();
118 if (ustDomain
!= null) {
119 if (session
== null) {
120 session
= (TraceSessionComponent
)ustDomain
.getParent();
123 List
<String
> channelNames
= new ArrayList
<>();
124 for (Iterator
<TraceChannelComponent
> iterator
= ustChannels
.iterator(); iterator
.hasNext();) {
125 // Enable all selected channels which are disabled
126 TraceChannelComponent channel
= iterator
.next();
127 channelNames
.add(channel
.getName());
130 changeState(ustDomain
, channelNames
, monitor
);
132 for (Iterator
<TraceChannelComponent
> iterator
= ustChannels
.iterator(); iterator
.hasNext();) {
133 // Enable all selected channels which are disabled
134 TraceChannelComponent channel
= iterator
.next();
135 channel
.setState(getNewState());
138 } catch (ExecutionException e
) {
142 // In all cases notify listeners
143 if (session
!= null) {
144 session
.fireComponentChanged(session
);
148 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.TraceControl_ChangeChannelStateFailure
, error
);
151 return Status
.OK_STATUS
;
164 public boolean isEnabled() {
166 // Get workbench page for the Control View
167 IWorkbenchPage page
= getWorkbenchPage();
172 TraceDomainComponent kernelDomain
= null;
173 TraceDomainComponent ustDomain
= null;
174 List
<TraceChannelComponent
> kernelChannels
= new ArrayList
<>();
175 List
<TraceChannelComponent
> ustChannels
= new ArrayList
<>();
177 // Check if one or more session are selected
178 ISelection selection
= page
.getSelection(ControlView
.ID
);
179 if (selection
instanceof StructuredSelection
) {
180 StructuredSelection structered
= ((StructuredSelection
) selection
);
181 String sessionName
= null;
182 for (Iterator
<?
> iterator
= structered
.iterator(); iterator
.hasNext();) {
183 Object element
= iterator
.next();
185 if (element
instanceof TraceChannelComponent
) {
187 // Add only TraceChannelComponents that are disabled
188 TraceChannelComponent channel
= (TraceChannelComponent
) element
;
189 if (sessionName
== null) {
190 sessionName
= String
.valueOf(channel
.getSessionName());
193 // Enable command only for channels of same session
194 if (!sessionName
.equals(channel
.getSessionName())) {
195 kernelChannels
.clear();
200 if ((channel
.getState() != getNewState())) {
201 if (channel
.isKernel()) {
202 kernelChannels
.add(channel
);
203 if (kernelDomain
== null) {
204 kernelDomain
= (TraceDomainComponent
) channel
.getParent();
207 ustChannels
.add(channel
);
208 if (ustDomain
== null) {
209 ustDomain
= (TraceDomainComponent
) channel
.getParent();
217 boolean isEnabled
= (!kernelChannels
.isEmpty() || !ustChannels
.isEmpty());
221 fParam
= new Parameter(kernelDomain
, ustDomain
, kernelChannels
, ustChannels
);
231 * Class containing parameter for the command execution.
233 protected static class Parameter
{
235 * Kernel domain component reference.
237 protected final TraceDomainComponent fKernelDomain
;
239 * UST domain component reference.
241 protected final TraceDomainComponent fUstDomain
;
243 * The list of kernel channel components the command is to be executed on.
245 protected final List
<TraceChannelComponent
> fKernelChannels
;
247 * The list of UST channel components the command is to be executed on.
249 protected final List
<TraceChannelComponent
> fUstChannels
;
253 * @param kernelDomain - a kernel domain component
254 * @param ustDomain - a UST domain component
255 * @param kernelChannels - list of available kernel channels
256 * @param ustChannels - list of available UST channels
258 public Parameter(TraceDomainComponent kernelDomain
, TraceDomainComponent ustDomain
, List
<TraceChannelComponent
> kernelChannels
, List
<TraceChannelComponent
> ustChannels
) {
259 fKernelDomain
= kernelDomain
;
260 fUstDomain
= ustDomain
;
261 fKernelChannels
= new ArrayList
<>();
262 fKernelChannels
.addAll(kernelChannels
);
263 fUstChannels
= new ArrayList
<>();
264 fUstChannels
.addAll(ustChannels
);
269 * @param other a parameter to copy
271 public Parameter(Parameter other
) {
272 this(other
.fKernelDomain
, other
.fUstDomain
, other
.fKernelChannels
, other
.fUstChannels
);
276 * @return the kernel domain component.
278 public TraceDomainComponent
getKernelDomain() {
279 return fKernelDomain
;
283 * @return the UST domain component.
285 public TraceDomainComponent
getUstDomain() {
290 * @return the list of kernel channel components.
292 public List
<TraceChannelComponent
> getKernelChannels() {
293 return fKernelChannels
;
297 * @return the list of UST channel components.
299 public List
<TraceChannelComponent
> getUstChannels() {