| 1 | /********************************************************************** |
| 2 | * Copyright (c) 2012, 2013 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 | **********************************************************************/ |
| 12 | package org.eclipse.linuxtools.internal.lttng2.ui.views.control.handlers; |
| 13 | |
| 14 | import java.util.ArrayList; |
| 15 | import java.util.Iterator; |
| 16 | import java.util.List; |
| 17 | |
| 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.TraceDomainComponent; |
| 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; |
| 36 | |
| 37 | /** |
| 38 | * <p> |
| 39 | * Abstract command handler implementation to enable or disabling a trace channel. |
| 40 | * </p> |
| 41 | * |
| 42 | * @author Bernd Hufmann |
| 43 | */ |
| 44 | abstract public class ChangeChannelStateHandler extends BaseControlViewHandler { |
| 45 | |
| 46 | // ------------------------------------------------------------------------ |
| 47 | // Attributes |
| 48 | // ------------------------------------------------------------------------ |
| 49 | /** |
| 50 | * The command execution parameter. |
| 51 | */ |
| 52 | protected Parameter fParam; |
| 53 | |
| 54 | // ------------------------------------------------------------------------ |
| 55 | // Accessors |
| 56 | // ------------------------------------------------------------------------ |
| 57 | /** |
| 58 | * @return the new state to set |
| 59 | */ |
| 60 | abstract protected TraceEnablement getNewState(); |
| 61 | |
| 62 | // ------------------------------------------------------------------------ |
| 63 | // Operations |
| 64 | // ------------------------------------------------------------------------ |
| 65 | /** |
| 66 | * Changes the state of the given channels. |
| 67 | * @param domain - the domain of the channels. |
| 68 | * @param channelNames - a list of channel names |
| 69 | * @param monitor - a progress monitor |
| 70 | * @throws ExecutionException If the command fails |
| 71 | */ |
| 72 | abstract protected void changeState(TraceDomainComponent domain, List<String> channelNames, IProgressMonitor monitor) throws ExecutionException; |
| 73 | |
| 74 | @Override |
| 75 | public Object execute(ExecutionEvent event) throws ExecutionException { |
| 76 | |
| 77 | fLock.lock(); |
| 78 | try { |
| 79 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); |
| 80 | |
| 81 | if (window == null) { |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | final Parameter param = new Parameter(fParam); |
| 86 | |
| 87 | Job job = new Job(Messages.TraceControl_ChangeChannelStateJob) { |
| 88 | @Override |
| 89 | protected IStatus run(IProgressMonitor monitor) { |
| 90 | Exception error = null; |
| 91 | |
| 92 | TraceSessionComponent session = null; |
| 93 | |
| 94 | try { |
| 95 | TraceDomainComponent kernelDomain = param.getKernelDomain(); |
| 96 | List<TraceChannelComponent> kernelChannels = param.getKernelChannels(); |
| 97 | |
| 98 | if (kernelDomain != null) { |
| 99 | session = (TraceSessionComponent)kernelDomain.getParent(); |
| 100 | List<String> channelNames = new ArrayList<String>(); |
| 101 | for (Iterator<TraceChannelComponent> iterator = kernelChannels.iterator(); iterator.hasNext();) { |
| 102 | // Enable all selected channels which are disabled |
| 103 | TraceChannelComponent channel = iterator.next(); |
| 104 | channelNames.add(channel.getName()); |
| 105 | } |
| 106 | |
| 107 | changeState(kernelDomain, channelNames, monitor); |
| 108 | |
| 109 | for (Iterator<TraceChannelComponent> iterator = kernelChannels.iterator(); iterator.hasNext();) { |
| 110 | // Enable all selected channels which are disabled |
| 111 | TraceChannelComponent channel = iterator.next(); |
| 112 | channel.setState(getNewState()); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | TraceDomainComponent ustDomain = param.getUstDomain(); |
| 117 | List<TraceChannelComponent> ustChannels = param.getUstChannels(); |
| 118 | if (ustDomain != null) { |
| 119 | if (session == null) { |
| 120 | session = (TraceSessionComponent)ustDomain.getParent(); |
| 121 | } |
| 122 | |
| 123 | List<String> channelNames = new ArrayList<String>(); |
| 124 | for (Iterator<TraceChannelComponent> iterator = ustChannels.iterator(); iterator.hasNext();) { |
| 125 | // Enable all selected channels which are disabled |
| 126 | TraceChannelComponent channel = iterator.next(); |
| 127 | channelNames.add(channel.getName()); |
| 128 | } |
| 129 | |
| 130 | changeState(ustDomain, channelNames, monitor); |
| 131 | |
| 132 | for (Iterator<TraceChannelComponent> iterator = ustChannels.iterator(); iterator.hasNext();) { |
| 133 | // Enable all selected channels which are disabled |
| 134 | TraceChannelComponent channel = iterator.next(); |
| 135 | channel.setState(getNewState()); |
| 136 | } |
| 137 | } |
| 138 | } catch (ExecutionException e) { |
| 139 | error = e; |
| 140 | } |
| 141 | |
| 142 | // In all cases notify listeners |
| 143 | if (session != null) { |
| 144 | session.fireComponentChanged(session); |
| 145 | } |
| 146 | |
| 147 | if (error != null) { |
| 148 | return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_ChangeChannelStateFailure, error); |
| 149 | } |
| 150 | |
| 151 | return Status.OK_STATUS; |
| 152 | } |
| 153 | }; |
| 154 | job.setUser(true); |
| 155 | job.schedule(); |
| 156 | } finally { |
| 157 | fLock.unlock(); |
| 158 | } |
| 159 | |
| 160 | return null; |
| 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public boolean isEnabled() { |
| 165 | |
| 166 | // Get workbench page for the Control View |
| 167 | IWorkbenchPage page = getWorkbenchPage(); |
| 168 | if (page == null) { |
| 169 | return false; |
| 170 | } |
| 171 | |
| 172 | TraceDomainComponent kernelDomain = null; |
| 173 | TraceDomainComponent ustDomain = null; |
| 174 | List<TraceChannelComponent> kernelChannels = new ArrayList<TraceChannelComponent>(); |
| 175 | List<TraceChannelComponent> ustChannels = new ArrayList<TraceChannelComponent>(); |
| 176 | |
| 177 | // Check if one or more session are selected |
| 178 | ISelection selection = page.getSelection(ControlView.ID); |
| 179 | if (selection instanceof StructuredSelection) { |
| 180 | StructuredSelection structered = ((StructuredSelection) selection); |
| 181 | String sessionName = null; |
| 182 | for (Iterator<?> iterator = structered.iterator(); iterator.hasNext();) { |
| 183 | Object element = iterator.next(); |
| 184 | |
| 185 | if (element instanceof TraceChannelComponent) { |
| 186 | |
| 187 | // Add only TraceChannelComponents that are disabled |
| 188 | TraceChannelComponent channel = (TraceChannelComponent) element; |
| 189 | if (sessionName == null) { |
| 190 | sessionName = String.valueOf(channel.getSessionName()); |
| 191 | } |
| 192 | |
| 193 | // Enable command only for channels of same session |
| 194 | if (!sessionName.equals(channel.getSessionName())) { |
| 195 | kernelChannels.clear(); |
| 196 | ustChannels.clear(); |
| 197 | break; |
| 198 | } |
| 199 | |
| 200 | if ((channel.getState() != getNewState())) { |
| 201 | if (channel.isKernel()) { |
| 202 | kernelChannels.add(channel); |
| 203 | if (kernelDomain == null) { |
| 204 | kernelDomain = (TraceDomainComponent) channel.getParent(); |
| 205 | } |
| 206 | } else { |
| 207 | ustChannels.add(channel); |
| 208 | if (ustDomain == null) { |
| 209 | ustDomain = (TraceDomainComponent) channel.getParent(); |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | boolean isEnabled = (!kernelChannels.isEmpty() || !ustChannels.isEmpty()); |
| 218 | fLock.lock(); |
| 219 | try { |
| 220 | if (isEnabled) { |
| 221 | fParam = new Parameter(kernelDomain, ustDomain, kernelChannels, ustChannels); |
| 222 | } |
| 223 | } finally { |
| 224 | fLock.unlock(); |
| 225 | } |
| 226 | |
| 227 | return isEnabled; |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Class containing parameter for the command execution. |
| 232 | */ |
| 233 | static protected class Parameter { |
| 234 | /** |
| 235 | * Kernel domain component reference. |
| 236 | */ |
| 237 | final protected TraceDomainComponent fKernelDomain; |
| 238 | /** |
| 239 | * UST domain component reference. |
| 240 | */ |
| 241 | final protected TraceDomainComponent fUstDomain; |
| 242 | /** |
| 243 | * The list of kernel channel components the command is to be executed on. |
| 244 | */ |
| 245 | final protected List<TraceChannelComponent> fKernelChannels; |
| 246 | /** |
| 247 | * The list of UST channel components the command is to be executed on. |
| 248 | */ |
| 249 | final protected List<TraceChannelComponent> fUstChannels; |
| 250 | |
| 251 | /** |
| 252 | * Constructor |
| 253 | * @param kernelDomain - a kernel domain component |
| 254 | * @param ustDomain - a UST domain component |
| 255 | * @param kernelChannels - list of available kernel channels |
| 256 | * @param ustChannels - list of available UST channels |
| 257 | */ |
| 258 | public Parameter(TraceDomainComponent kernelDomain, TraceDomainComponent ustDomain, List<TraceChannelComponent> kernelChannels, List<TraceChannelComponent> ustChannels) { |
| 259 | fKernelDomain = kernelDomain; |
| 260 | fUstDomain = ustDomain; |
| 261 | fKernelChannels = new ArrayList<TraceChannelComponent>(); |
| 262 | fKernelChannels.addAll(kernelChannels); |
| 263 | fUstChannels = new ArrayList<TraceChannelComponent>(); |
| 264 | fUstChannels.addAll(ustChannels); |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Copy constructor |
| 269 | * @param other a parameter to copy |
| 270 | */ |
| 271 | public Parameter(Parameter other) { |
| 272 | this(other.fKernelDomain, other.fUstDomain, other.fKernelChannels, other.fUstChannels); |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * @return the kernel domain component. |
| 277 | */ |
| 278 | public TraceDomainComponent getKernelDomain() { |
| 279 | return fKernelDomain; |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * @return the UST domain component. |
| 284 | */ |
| 285 | public TraceDomainComponent getUstDomain() { |
| 286 | return fUstDomain; |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * @return the list of kernel channel components. |
| 291 | */ |
| 292 | public List<TraceChannelComponent> getKernelChannels() { |
| 293 | return fKernelChannels; |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * @return the list of UST channel components. |
| 298 | */ |
| 299 | public List<TraceChannelComponent> getUstChannels() { |
| 300 | return fUstChannels; |
| 301 | } |
| 302 | } |
| 303 | } |