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
.control
.ui
.views
.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
.control
.core
.model
.TraceEnablement
;
27 import org
.eclipse
.linuxtools
.internal
.lttng2
.control
.ui
.Activator
;
28 import org
.eclipse
.linuxtools
.internal
.lttng2
.control
.ui
.views
.ControlView
;
29 import org
.eclipse
.linuxtools
.internal
.lttng2
.control
.ui
.views
.messages
.Messages
;
30 import org
.eclipse
.linuxtools
.internal
.lttng2
.control
.ui
.views
.model
.impl
.TraceChannelComponent
;
31 import org
.eclipse
.linuxtools
.internal
.lttng2
.control
.ui
.views
.model
.impl
.TraceEventComponent
;
32 import org
.eclipse
.linuxtools
.internal
.lttng2
.control
.ui
.views
.model
.impl
.TraceSessionComponent
;
33 import org
.eclipse
.ui
.IWorkbenchPage
;
34 import org
.eclipse
.ui
.IWorkbenchWindow
;
35 import org
.eclipse
.ui
.PlatformUI
;
39 * Base Command handler implementation to enable or disabling a trace channel.
42 * @author Bernd Hufmann
44 public abstract class ChangeEventStateHandler
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 // ------------------------------------------------------------------------
67 * @param channel - channel of events to be enabled
68 * @param eventNames - list event names
69 * @param monitor - a progress monitor
70 * @throws ExecutionException If the command fails
72 protected abstract void changeState(TraceChannelComponent channel
, List
<String
> eventNames
, IProgressMonitor monitor
) throws ExecutionException
;
75 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
77 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
86 final Parameter param
= new Parameter(fParam
);
88 Job job
= new Job(Messages
.TraceControl_ChangeChannelStateJob
) {
90 protected IStatus
run(IProgressMonitor monitor
) {
91 Exception error
= null;
93 TraceSessionComponent session
= null;
96 boolean isAll
= false;
97 if (param
.getChannel() != null) {
98 session
= param
.getChannel().getSession();
99 List
<String
> eventNames
= new ArrayList
<>();
100 List
<TraceEventComponent
> events
= param
.getEvents();
102 for (Iterator
<TraceEventComponent
> iterator
= events
.iterator(); iterator
.hasNext();) {
103 // Enable/disable all selected channels which are disabled
104 TraceEventComponent traceEvent
= iterator
.next();
106 // Workaround for wildcard handling in lttng-tools
107 if ("*".equals(traceEvent
.getName())) { //$NON-NLS-1$
110 eventNames
.add(traceEvent
.getName());
114 changeState(param
.getChannel(), null, monitor
);
117 if (!eventNames
.isEmpty()) {
118 changeState(param
.getChannel(), eventNames
, monitor
);
121 for (Iterator
<TraceEventComponent
> iterator
= events
.iterator(); iterator
.hasNext();) {
122 // Enable all selected channels which are disabled
123 TraceEventComponent ev
= iterator
.next();
124 ev
.setState(getNewState());
127 } catch (ExecutionException e
) {
131 if (session
!= null) {
132 // In all cases notify listeners
133 session
.fireComponentChanged(session
);
137 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.TraceControl_ChangeEventStateFailure
, error
);
140 return Status
.OK_STATUS
;
152 public boolean isEnabled() {
153 // Get workbench page for the Control View
154 IWorkbenchPage page
= getWorkbenchPage();
159 // Check if one or more session are selected
160 ISelection selection
= page
.getSelection(ControlView
.ID
);
162 TraceChannelComponent channel
= null;
163 List
<TraceEventComponent
> events
= new ArrayList
<>();
165 if (selection
instanceof StructuredSelection
) {
166 StructuredSelection structered
= ((StructuredSelection
) selection
);
167 String sessionName
= null;
168 String channelName
= null;
170 for (Iterator
<?
> iterator
= structered
.iterator(); iterator
.hasNext();) {
171 Object element
= iterator
.next();
173 if (element
instanceof TraceEventComponent
) {
175 TraceEventComponent event
= (TraceEventComponent
) element
;
176 if (sessionName
== null) {
177 sessionName
= String
.valueOf(event
.getSessionName());
180 if (channel
== null) {
181 channel
= (TraceChannelComponent
)event
.getParent();
184 if (channelName
== null) {
185 channelName
= event
.getChannelName();
188 // Enable command only for events of same session, same channel and domain
189 if ((!sessionName
.equals(event
.getSessionName())) ||
190 (!channelName
.equals(event
.getChannelName())) ||
191 (channel
.isKernel() != event
.isKernel())) {
196 if ((event
.getState() != getNewState())) {
202 boolean isEnabled
= !events
.isEmpty();
208 fParam
= new Parameter(channel
, events
);
217 * Class containing parameter for the command execution.
219 protected static class Parameter
{
221 * Channel component reference.
223 private final TraceChannelComponent fChannel
;
225 * The list of kernel channel components the command is to be executed on.
227 private final List
<TraceEventComponent
> fEvents
= new ArrayList
<>();
231 * @param channel - a channel component
232 * @param events - a list of event components
234 public Parameter(TraceChannelComponent channel
, List
<TraceEventComponent
> events
) {
236 fEvents
.addAll(events
);
241 * @param other - a parameter to copy
243 public Parameter(Parameter other
) {
244 this(other
.fChannel
, other
.fEvents
);
248 * @return the trace channel component.
250 public TraceChannelComponent
getChannel() {
255 * @return a list of trace event components.
257 public List
<TraceEventComponent
> getEvents() {