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
.lttng
.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
.lttng
.ui
.Activator
;
27 import org
.eclipse
.linuxtools
.internal
.lttng
.ui
.views
.control
.ControlView
;
28 import org
.eclipse
.linuxtools
.internal
.lttng
.ui
.views
.control
.Messages
;
29 import org
.eclipse
.linuxtools
.internal
.lttng
.ui
.views
.control
.model
.TraceEnablement
;
30 import org
.eclipse
.linuxtools
.internal
.lttng
.ui
.views
.control
.model
.impl
.TraceChannelComponent
;
31 import org
.eclipse
.linuxtools
.internal
.lttng
.ui
.views
.control
.model
.impl
.TraceDomainComponent
;
32 import org
.eclipse
.linuxtools
.internal
.lttng
.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 * Kernel domain component reference.
51 protected TraceDomainComponent fKernelDomain
= null;
53 * UST domain component reference.
55 protected TraceDomainComponent fUstDomain
= null;
57 * The list of kernel channel components the command is to be executed on.
59 protected List
<TraceChannelComponent
> fKernelChannels
= new ArrayList
<TraceChannelComponent
>();
61 * The list of UST channel components the command is to be executed on.
63 protected List
<TraceChannelComponent
> fUstChannels
= new ArrayList
<TraceChannelComponent
>();
65 // ------------------------------------------------------------------------
67 // ------------------------------------------------------------------------
69 * @return the new state to set
71 abstract protected TraceEnablement
getNewState();
73 // ------------------------------------------------------------------------
75 // ------------------------------------------------------------------------
77 * Changes the state of the given channels.
78 * @param domain - the domain of the channels.
79 * @param channelNames - a list of channel names
80 * @param monitor - a progress monitor
81 * @throws ExecutionException
83 abstract protected void changeState(TraceDomainComponent domain
, List
<String
> channelNames
, IProgressMonitor monitor
) throws ExecutionException
;
87 * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
90 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
92 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
98 Job job
= new Job(Messages
.TraceControl_ChangeChannelStateJob
) {
100 protected IStatus
run(IProgressMonitor monitor
) {
101 String errorString
= null;
103 TraceSessionComponent session
= null;
106 if (fKernelDomain
!= null) {
107 session
= (TraceSessionComponent
)fKernelDomain
.getParent();
108 List
<String
> channelNames
= new ArrayList
<String
>();
109 for (Iterator
<TraceChannelComponent
> iterator
= fKernelChannels
.iterator(); iterator
.hasNext();) {
110 // Enable all selected channels which are disabled
111 TraceChannelComponent channel
= (TraceChannelComponent
) iterator
.next();
112 channelNames
.add(channel
.getName());
115 changeState(fKernelDomain
, channelNames
, monitor
);
117 for (Iterator
<TraceChannelComponent
> iterator
= fKernelChannels
.iterator(); iterator
.hasNext();) {
118 // Enable all selected channels which are disabled
119 TraceChannelComponent channel
= (TraceChannelComponent
) iterator
.next();
120 channel
.setState(getNewState());
124 if (fUstDomain
!= null) {
125 if (session
== null) {
126 session
= (TraceSessionComponent
)fUstDomain
.getParent();
129 List
<String
> channelNames
= new ArrayList
<String
>();
130 for (Iterator
<TraceChannelComponent
> iterator
= fUstChannels
.iterator(); iterator
.hasNext();) {
131 // Enable all selected channels which are disabled
132 TraceChannelComponent channel
= (TraceChannelComponent
) iterator
.next();
133 channelNames
.add(channel
.getName());
136 changeState(fUstDomain
, channelNames
, monitor
);
138 for (Iterator
<TraceChannelComponent
> iterator
= fUstChannels
.iterator(); iterator
.hasNext();) {
139 // Enable all selected channels which are disabled
140 TraceChannelComponent channel
= (TraceChannelComponent
) iterator
.next();
141 channel
.setState(getNewState());
144 } catch (ExecutionException e
) {
145 errorString
= e
.toString() + "\n"; //$NON-NLS-1$
148 // In all cases notify listeners
149 session
.fireComponentChanged(session
);
151 if (errorString
!= null) {
152 return new Status(Status
.ERROR
, Activator
.PLUGIN_ID
, errorString
);
155 return Status
.OK_STATUS
;
166 * @see org.eclipse.core.commands.AbstractHandler#isEnabled()
169 public boolean isEnabled() {
172 // Get workbench page for the Control View
173 IWorkbenchPage page
= getWorkbenchPage();
178 // Check if one or more session are selected
179 ISelection selection
= page
.getSelection(ControlView
.ID
);
180 if (selection
instanceof StructuredSelection
) {
181 StructuredSelection structered
= ((StructuredSelection
) selection
);
182 String sessionName
= null;
183 for (Iterator
<?
> iterator
= structered
.iterator(); iterator
.hasNext();) {
184 Object element
= (Object
) iterator
.next();
186 if (element
instanceof TraceChannelComponent
) {
188 // Add only TraceChannelComponents that are disabled
189 TraceChannelComponent channel
= (TraceChannelComponent
) element
;
190 if (sessionName
== null) {
191 sessionName
= String
.valueOf(channel
.getSessionName());
194 // Enable command only for channels of same session
195 if (!sessionName
.equals(channel
.getSessionName())) {
200 if ((channel
.getState() != getNewState())) {
201 if (channel
.isKernel()) {
202 fKernelChannels
.add(channel
);
203 if (fKernelDomain
== null) {
204 fKernelDomain
= (TraceDomainComponent
) channel
.getParent();
207 fUstChannels
.add(channel
);
208 if (fUstDomain
== null) {
209 fUstDomain
= (TraceDomainComponent
) channel
.getParent();
216 return fKernelChannels
.size() + fUstChannels
.size() > 0;
222 private void reset() {
223 fKernelDomain
= null;
225 fKernelChannels
.clear();
226 fUstChannels
.clear();