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
.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
.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
;
38 * <b><u>ChangeChannelStateHandler</u></b>
40 * Abstract command handler implementation to enable or disabling a trace channel.
43 abstract public class ChangeChannelStateHandler
extends BaseControlViewHandler
{
45 // ------------------------------------------------------------------------
47 // ------------------------------------------------------------------------
49 * The command execution parameter.
51 protected Parameter fParam
;
53 // ------------------------------------------------------------------------
55 // ------------------------------------------------------------------------
57 * @return the new state to set
59 abstract protected TraceEnablement
getNewState();
61 // ------------------------------------------------------------------------
63 // ------------------------------------------------------------------------
65 * Changes the state of the given channels.
66 * @param domain - the domain of the channels.
67 * @param channelNames - a list of channel names
68 * @param monitor - a progress monitor
69 * @throws ExecutionException
71 abstract protected void changeState(TraceDomainComponent domain
, List
<String
> channelNames
, IProgressMonitor monitor
) throws ExecutionException
;
75 * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
78 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
82 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
88 final Parameter param
= new Parameter(fParam
);
90 Job job
= new Job(Messages
.TraceControl_ChangeChannelStateJob
) {
92 protected IStatus
run(IProgressMonitor monitor
) {
93 String errorString
= null;
95 TraceSessionComponent session
= null;
98 TraceDomainComponent kernelDomain
= param
.getKernelDomain();
99 List
<TraceChannelComponent
> kernelChannels
= param
.getKernelChannels();
101 if (kernelDomain
!= null) {
102 session
= (TraceSessionComponent
)kernelDomain
.getParent();
103 List
<String
> channelNames
= new ArrayList
<String
>();
104 for (Iterator
<TraceChannelComponent
> iterator
= kernelChannels
.iterator(); iterator
.hasNext();) {
105 // Enable all selected channels which are disabled
106 TraceChannelComponent channel
= (TraceChannelComponent
) iterator
.next();
107 channelNames
.add(channel
.getName());
110 changeState(kernelDomain
, channelNames
, monitor
);
112 for (Iterator
<TraceChannelComponent
> iterator
= kernelChannels
.iterator(); iterator
.hasNext();) {
113 // Enable all selected channels which are disabled
114 TraceChannelComponent channel
= (TraceChannelComponent
) iterator
.next();
115 channel
.setState(getNewState());
119 TraceDomainComponent ustDomain
= param
.getUstDomain();
120 List
<TraceChannelComponent
> ustChannels
= param
.getUstChannels();
121 if (ustDomain
!= null) {
122 if (session
== null) {
123 session
= (TraceSessionComponent
)ustDomain
.getParent();
126 List
<String
> channelNames
= new ArrayList
<String
>();
127 for (Iterator
<TraceChannelComponent
> iterator
= ustChannels
.iterator(); iterator
.hasNext();) {
128 // Enable all selected channels which are disabled
129 TraceChannelComponent channel
= (TraceChannelComponent
) iterator
.next();
130 channelNames
.add(channel
.getName());
133 changeState(ustDomain
, channelNames
, monitor
);
135 for (Iterator
<TraceChannelComponent
> iterator
= ustChannels
.iterator(); iterator
.hasNext();) {
136 // Enable all selected channels which are disabled
137 TraceChannelComponent channel
= (TraceChannelComponent
) iterator
.next();
138 channel
.setState(getNewState());
141 } catch (ExecutionException e
) {
142 errorString
= e
.toString() + "\n"; //$NON-NLS-1$
145 // In all cases notify listeners
146 session
.fireComponentChanged(session
);
148 if (errorString
!= null) {
149 return new Status(Status
.ERROR
, Activator
.PLUGIN_ID
, errorString
);
152 return Status
.OK_STATUS
;
166 * @see org.eclipse.core.commands.AbstractHandler#isEnabled()
169 public boolean isEnabled() {
171 // Get workbench page for the Control View
172 IWorkbenchPage page
= getWorkbenchPage();
177 TraceDomainComponent kernelDomain
= null;
178 TraceDomainComponent ustDomain
= null;
179 List
<TraceChannelComponent
> kernelChannels
= new ArrayList
<TraceChannelComponent
>();
180 List
<TraceChannelComponent
> ustChannels
= new ArrayList
<TraceChannelComponent
>();
182 // Check if one or more session are selected
183 ISelection selection
= page
.getSelection(ControlView
.ID
);
184 if (selection
instanceof StructuredSelection
) {
185 StructuredSelection structered
= ((StructuredSelection
) selection
);
186 String sessionName
= null;
187 for (Iterator
<?
> iterator
= structered
.iterator(); iterator
.hasNext();) {
188 Object element
= (Object
) iterator
.next();
190 if (element
instanceof TraceChannelComponent
) {
192 // Add only TraceChannelComponents that are disabled
193 TraceChannelComponent channel
= (TraceChannelComponent
) element
;
194 if (sessionName
== null) {
195 sessionName
= String
.valueOf(channel
.getSessionName());
198 // Enable command only for channels of same session
199 if (!sessionName
.equals(channel
.getSessionName())) {
200 kernelChannels
.clear();
205 if ((channel
.getState() != getNewState())) {
206 if (channel
.isKernel()) {
207 kernelChannels
.add(channel
);
208 if (kernelDomain
== null) {
209 kernelDomain
= (TraceDomainComponent
) channel
.getParent();
212 ustChannels
.add(channel
);
213 if (ustDomain
== null) {
214 ustDomain
= (TraceDomainComponent
) channel
.getParent();
222 boolean isEnabled
= (!kernelChannels
.isEmpty() || !ustChannels
.isEmpty());
226 fParam
= new Parameter(kernelDomain
, ustDomain
, kernelChannels
, ustChannels
);
236 * Class containing parameter for the command execution.
238 static protected class Parameter
{
240 * Kernel domain component reference.
242 final protected TraceDomainComponent fKernelDomain
;
244 * UST domain component reference.
246 final protected TraceDomainComponent fUstDomain
;
248 * The list of kernel channel components the command is to be executed on.
250 final protected List
<TraceChannelComponent
> fKernelChannels
;
252 * The list of UST channel components the command is to be executed on.
254 final protected List
<TraceChannelComponent
> fUstChannels
;
258 * @param kernelDomain - a kernel domain component
259 * @param ustDomain - a UST domain component
260 * @param kernelChannels - list of available kernel channels
261 * @param ustChannels - list of available UST channels
263 public Parameter(TraceDomainComponent kernelDomain
, TraceDomainComponent ustDomain
, List
<TraceChannelComponent
> kernelChannels
, List
<TraceChannelComponent
> ustChannels
) {
264 fKernelDomain
= kernelDomain
;
265 fUstDomain
= ustDomain
;
266 fKernelChannels
= new ArrayList
<TraceChannelComponent
>();
267 fKernelChannels
.addAll(kernelChannels
);
268 fUstChannels
= new ArrayList
<TraceChannelComponent
>();
269 fUstChannels
.addAll(ustChannels
);
274 * @param other a parameter to copy
276 public Parameter(Parameter other
) {
277 this(other
.fKernelDomain
, other
.fUstDomain
, other
.fKernelChannels
, other
.fUstChannels
);
281 * @return the kernel domain component.
283 public TraceDomainComponent
getKernelDomain() {
284 return fKernelDomain
;
288 * @return the UST domain component.
290 public TraceDomainComponent
getUstDomain() {
295 * @return the list of kernel channel components.
297 public List
<TraceChannelComponent
> getKernelChannels() {
298 return fKernelChannels
;
302 * @return the list of UST channel components.
304 public List
<TraceChannelComponent
> getUstChannels() {