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
.TraceEventComponent
;
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 * Base Command handler implementation to enable or disabling a trace channel.
42 * @author Bernd Hufmann
44 abstract public 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 abstract protected 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 abstract protected void changeState(TraceChannelComponent channel
, List
<String
> eventNames
, 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
{
81 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
90 final Parameter param
= new Parameter(fParam
);
92 Job job
= new Job(Messages
.TraceControl_ChangeChannelStateJob
) {
94 protected IStatus
run(IProgressMonitor monitor
) {
95 Exception error
= null;
97 TraceSessionComponent session
= null;
100 boolean isAll
= false;
101 if (param
.getChannel() != null) {
102 session
= param
.getChannel().getSession();
103 List
<String
> eventNames
= new ArrayList
<String
>();
104 List
<TraceEventComponent
> events
= param
.getEvents();
106 for (Iterator
<TraceEventComponent
> iterator
= events
.iterator(); iterator
.hasNext();) {
107 // Enable/disable all selected channels which are disabled
108 TraceEventComponent traceEvent
= iterator
.next();
110 // Workaround for wildcard handling in lttng-tools
111 if ("*".equals(traceEvent
.getName())) { //$NON-NLS-1$
114 eventNames
.add(traceEvent
.getName());
118 changeState(param
.getChannel(), null, monitor
);
121 if (!eventNames
.isEmpty()) {
122 changeState(param
.getChannel(), eventNames
, monitor
);
125 for (Iterator
<TraceEventComponent
> iterator
= events
.iterator(); iterator
.hasNext();) {
126 // Enable all selected channels which are disabled
127 TraceEventComponent ev
= iterator
.next();
128 ev
.setState(getNewState());
131 } catch (ExecutionException e
) {
135 if (session
!= null) {
136 // In all cases notify listeners
137 session
.fireComponentChanged(session
);
141 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.TraceControl_ChangeEventStateFailure
, error
);
144 return Status
.OK_STATUS
;
157 * @see org.eclipse.core.commands.AbstractHandler#isEnabled()
160 public boolean isEnabled() {
161 // Get workbench page for the Control View
162 IWorkbenchPage page
= getWorkbenchPage();
167 // Check if one or more session are selected
168 ISelection selection
= page
.getSelection(ControlView
.ID
);
170 TraceChannelComponent channel
= null;
171 List
<TraceEventComponent
> events
= new ArrayList
<TraceEventComponent
>();
173 if (selection
instanceof StructuredSelection
) {
174 StructuredSelection structered
= ((StructuredSelection
) selection
);
175 String sessionName
= null;
176 String channelName
= null;
178 for (Iterator
<?
> iterator
= structered
.iterator(); iterator
.hasNext();) {
179 Object element
= iterator
.next();
181 if (element
instanceof TraceEventComponent
) {
183 TraceEventComponent event
= (TraceEventComponent
) element
;
184 if (sessionName
== null) {
185 sessionName
= String
.valueOf(event
.getSessionName());
188 if (channel
== null) {
189 channel
= (TraceChannelComponent
)event
.getParent();
192 if (channelName
== null) {
193 channelName
= event
.getChannelName();
196 // Enable command only for events of same session, same channel and domain
197 if ((!sessionName
.equals(event
.getSessionName())) ||
198 (!channelName
.equals(event
.getChannelName())) ||
199 (channel
.isKernel() != event
.isKernel())) {
204 if ((event
.getState() != getNewState())) {
210 boolean isEnabled
= !events
.isEmpty();
216 fParam
= new Parameter(channel
, events
);
225 * Class containing parameter for the command execution.
227 static protected class Parameter
{
229 * Channel component reference.
231 final private TraceChannelComponent fChannel
;
233 * The list of kernel channel components the command is to be executed on.
235 final private List
<TraceEventComponent
> fEvents
= new ArrayList
<TraceEventComponent
>();
239 * @param channel - a channel component
240 * @param events - a list of event components
242 public Parameter(TraceChannelComponent channel
, List
<TraceEventComponent
> events
) {
244 fEvents
.addAll(events
);
249 * @param other - a parameter to copy
251 public Parameter(Parameter other
) {
252 this(other
.fChannel
, other
.fEvents
);
256 * @return the trace channel component.
258 public TraceChannelComponent
getChannel() {
263 * @return a list of trace event components.
265 public List
<TraceEventComponent
> getEvents() {