Merge branch 'master' into lttng_2_0_control_dev
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.ui / src / org / eclipse / linuxtools / lttng / ui / views / control / handlers / ChangeEventStateHandler.java
CommitLineData
6503ae0f
BH
1/**********************************************************************
2 * Copyright (c) 2012 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Bernd Hufmann - Initial API and implementation
11 **********************************************************************/
12package org.eclipse.linuxtools.lttng.ui.views.control.handlers;
13
14import java.util.ArrayList;
15import java.util.Iterator;
16import java.util.List;
17
18import org.eclipse.core.commands.AbstractHandler;
19import org.eclipse.core.commands.ExecutionEvent;
20import org.eclipse.core.commands.ExecutionException;
21import org.eclipse.core.runtime.IProgressMonitor;
22import org.eclipse.core.runtime.IStatus;
23import org.eclipse.core.runtime.Status;
24import org.eclipse.core.runtime.jobs.Job;
25import org.eclipse.jface.viewers.ISelection;
26import org.eclipse.jface.viewers.StructuredSelection;
27import org.eclipse.linuxtools.lttng.ui.LTTngUiPlugin;
28import org.eclipse.linuxtools.lttng.ui.views.control.ControlView;
29import org.eclipse.linuxtools.lttng.ui.views.control.Messages;
30import org.eclipse.linuxtools.lttng.ui.views.control.model.TraceEnablement;
31import org.eclipse.linuxtools.lttng.ui.views.control.model.impl.TraceChannelComponent;
32import org.eclipse.linuxtools.lttng.ui.views.control.model.impl.TraceEventComponent;
33import org.eclipse.linuxtools.lttng.ui.views.control.model.impl.TraceSessionComponent;
34import org.eclipse.ui.IWorkbenchPage;
35import org.eclipse.ui.IWorkbenchPart;
36import org.eclipse.ui.IWorkbenchWindow;
37import org.eclipse.ui.PlatformUI;
38
39/**
40 * <b><u>EnableChannelHandler</u></b>
41 * <p>
42 * Base Command handler implementation to enable or disabling a trace channel.
43 * </p>
44 */
45abstract public class ChangeEventStateHandler extends AbstractHandler {
46
47 // ------------------------------------------------------------------------
48 // Attributes
49 // ------------------------------------------------------------------------
50 /**
51 * Channel component reference.
52 */
53 protected TraceChannelComponent fChannel = null;
54 /**
55 * The list of kernel channel components the command is to be executed on.
56 */
57 protected List<TraceEventComponent> fEvents = new ArrayList<TraceEventComponent>();
58
59 // ------------------------------------------------------------------------
60 // Accessors
61 // ------------------------------------------------------------------------
62 /**
63 * @return the new state to set
64 */
65 abstract protected TraceEnablement getNewState();
66
67 // ------------------------------------------------------------------------
68 // Operations
69 // ------------------------------------------------------------------------
70 /**
71 * Change the state
72 * @param channel - channel of events to be enabled
73 * @param eventNames - list event names
74 * @param monitor - a progress monitor
75 * @throws ExecutionException
76 */
77 abstract protected void changeState(TraceChannelComponent channel, List<String> eventNames, IProgressMonitor monitor) throws ExecutionException;
78
79 /*
80 * (non-Javadoc)
81 * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
82 */
83 @Override
84 public Object execute(ExecutionEvent event) throws ExecutionException {
85
86 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
87
88 if (window == null) {
89 return false;
90 }
91
92 Job job = new Job(Messages.TraceControl_ChangeChannelStateJob) {
93 @Override
94 protected IStatus run(IProgressMonitor monitor) {
95 String errorString = null;
96
97 TraceSessionComponent session = null;
98
99 try {
100 if (fChannel != null) {
101 session = fChannel.getSession();
102 List<String> channelNames = new ArrayList<String>();
103 for (Iterator<TraceEventComponent> iterator = fEvents.iterator(); iterator.hasNext();) {
104 // Enable all selected channels which are disabled
105 TraceEventComponent channel = (TraceEventComponent) iterator.next();
106 channelNames.add(channel.getName());
107 }
108
109 changeState(fChannel, channelNames, monitor);
110
111 for (Iterator<TraceEventComponent> iterator = fEvents.iterator(); iterator.hasNext();) {
112 // Enable all selected channels which are disabled
113 TraceEventComponent channel = (TraceEventComponent) iterator.next();
114 channel.setState(getNewState());
115 }
116 }
117 } catch (ExecutionException e) {
118 errorString = e.toString() + "\n"; //$NON-NLS-1$
119 }
120
121 // In all cases notify listeners
122 session.fireComponentChanged(session);
123
124 if (errorString != null) {
125 return new Status(Status.ERROR, LTTngUiPlugin.PLUGIN_ID, errorString);
126 }
127
128 return Status.OK_STATUS;
129 }
130 };
131 job.setUser(true);
132 job.schedule();
133
134 return null;
135 }
136
137 /*
138 * (non-Javadoc)
139 * @see org.eclipse.core.commands.AbstractHandler#isEnabled()
140 */
141 @Override
142 public boolean isEnabled() {
143 reset();
144
145 // Check if we are closing down
146 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
147 if (window == null) {
148 return false;
149 }
150
151 // Check if we are in the Project View
152 IWorkbenchPage page = window.getActivePage();
153 if (page == null) {
154 return false;
155 }
156
157 IWorkbenchPart part = page.getActivePart();
158 if (!(part instanceof ControlView)) {
159 return false;
160 }
161
162 // Check if one or more session are selected
163 ISelection selection = page.getSelection(ControlView.ID);
164 if (selection instanceof StructuredSelection) {
165 StructuredSelection structered = ((StructuredSelection) selection);
166 String sessionName = null;
167 String channelName = null;
168
169 for (Iterator<?> iterator = structered.iterator(); iterator.hasNext();) {
170 Object element = (Object) iterator.next();
171
172 if (element instanceof TraceEventComponent) {
173
174 // Add only TraceChannelComponents that are disabled
175 TraceEventComponent channel = (TraceEventComponent) element;
176 if (sessionName == null) {
177 sessionName = String.valueOf(channel.getSessionName());
178 }
179
180 if (fChannel == null) {
181 fChannel = (TraceChannelComponent)channel.getParent();
182 }
183
184 if (channelName == null) {
185 channelName = channel.getChannelName();
186 }
187
188 // Enable command only for events of same session, same channel and domain
189 if ((!sessionName.equals(channel.getSessionName())) ||
190 (!channelName.equals(channel.getChannelName())) ||
191 (fChannel.isKernel() != channel.isKernel())) {
192 reset();
193 break;
194 }
195
196 if ((channel.getState() != getNewState())) {
197 fEvents.add(channel);
198 }
199 }
200 }
201 }
202 return fEvents.size() > 0;
203 }
204
205 /**
206 * Reset members
207 */
208 private void reset() {
209 fChannel = null;
210 fEvents.clear();
211 }
212}
This page took 0.031607 seconds and 5 git commands to generate.