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
.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
;
38 * <b><u>EnableChannelHandler</u></b>
40 * Base Command handler implementation to enable or disabling a trace channel.
43 abstract public class ChangeEventStateHandler
extends BaseControlViewHandler
{
45 // ------------------------------------------------------------------------
47 // ------------------------------------------------------------------------
49 * Channel component reference.
51 protected TraceChannelComponent fChannel
= null;
53 * The list of kernel channel components the command is to be executed on.
55 protected List
<TraceEventComponent
> fEvents
= new ArrayList
<TraceEventComponent
>();
57 // ------------------------------------------------------------------------
59 // ------------------------------------------------------------------------
61 * @return the new state to set
63 abstract protected TraceEnablement
getNewState();
65 // ------------------------------------------------------------------------
67 // ------------------------------------------------------------------------
70 * @param channel - channel of events to be enabled
71 * @param eventNames - list event names
72 * @param monitor - a progress monitor
73 * @throws ExecutionException
75 abstract protected void changeState(TraceChannelComponent channel
, List
<String
> eventNames
, IProgressMonitor monitor
) throws ExecutionException
;
79 * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
82 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
84 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
90 Job job
= new Job(Messages
.TraceControl_ChangeChannelStateJob
) {
92 protected IStatus
run(IProgressMonitor monitor
) {
93 String errorString
= null;
95 TraceSessionComponent session
= null;
98 boolean isAll
= false;
99 if (fChannel
!= null) {
100 session
= fChannel
.getSession();
101 List
<String
> eventNames
= new ArrayList
<String
>();
102 for (Iterator
<TraceEventComponent
> iterator
= fEvents
.iterator(); iterator
.hasNext();) {
103 // Enable/disable all selected channels which are disabled
104 TraceEventComponent event
= (TraceEventComponent
) iterator
.next();
106 // Workaround for wildcard handling in lttng-tools
107 if ("*".equals(event
.getName())) { //$NON-NLS-1$
110 eventNames
.add(event
.getName());
114 changeState(fChannel
, null, monitor
);
117 if (eventNames
.size() > 0) {
118 changeState(fChannel
, eventNames
, monitor
);
121 for (Iterator
<TraceEventComponent
> iterator
= fEvents
.iterator(); iterator
.hasNext();) {
122 // Enable all selected channels which are disabled
123 TraceEventComponent ev
= (TraceEventComponent
) iterator
.next();
124 ev
.setState(getNewState());
127 } catch (ExecutionException e
) {
128 errorString
= e
.toString() + "\n"; //$NON-NLS-1$
131 // In all cases notify listeners
132 session
.fireComponentChanged(session
);
134 if (errorString
!= null) {
135 return new Status(Status
.ERROR
, Activator
.PLUGIN_ID
, errorString
);
138 return Status
.OK_STATUS
;
149 * @see org.eclipse.core.commands.AbstractHandler#isEnabled()
152 public boolean isEnabled() {
153 // Get workbench page for the Control View
154 IWorkbenchPage page
= getWorkbenchPage();
161 // Check if one or more session are selected
162 ISelection selection
= page
.getSelection(ControlView
.ID
);
163 if (selection
instanceof StructuredSelection
) {
164 StructuredSelection structered
= ((StructuredSelection
) selection
);
165 String sessionName
= null;
166 String channelName
= null;
168 for (Iterator
<?
> iterator
= structered
.iterator(); iterator
.hasNext();) {
169 Object element
= (Object
) iterator
.next();
171 if (element
instanceof TraceEventComponent
) {
173 TraceEventComponent event
= (TraceEventComponent
) element
;
174 if (sessionName
== null) {
175 sessionName
= String
.valueOf(event
.getSessionName());
178 if (fChannel
== null) {
179 fChannel
= (TraceChannelComponent
)event
.getParent();
182 if (channelName
== null) {
183 channelName
= event
.getChannelName();
186 // Enable command only for events of same session, same channel and domain
187 if ((!sessionName
.equals(event
.getSessionName())) ||
188 (!channelName
.equals(event
.getChannelName())) ||
189 (fChannel
.isKernel() != event
.isKernel())) {
194 if ((event
.getState() != getNewState())) {
200 return fEvents
.size() > 0;
206 private void reset() {