| 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 | **********************************************************************/ |
| 12 | package org.eclipse.linuxtools.lttng.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.jface.window.Window; |
| 27 | import org.eclipse.linuxtools.lttng.ui.LTTngUiPlugin; |
| 28 | import org.eclipse.linuxtools.lttng.ui.views.control.ControlView; |
| 29 | import org.eclipse.linuxtools.lttng.ui.views.control.Messages; |
| 30 | import org.eclipse.linuxtools.lttng.ui.views.control.dialogs.IGetEventInfoDialog; |
| 31 | import org.eclipse.linuxtools.lttng.ui.views.control.dialogs.TraceControlDialogFactory; |
| 32 | import org.eclipse.linuxtools.lttng.ui.views.control.model.ITraceControlComponent; |
| 33 | import org.eclipse.linuxtools.lttng.ui.views.control.model.impl.BaseEventComponent; |
| 34 | import org.eclipse.linuxtools.lttng.ui.views.control.model.impl.KernelProviderComponent; |
| 35 | import org.eclipse.linuxtools.lttng.ui.views.control.model.impl.TargetNodeComponent; |
| 36 | import org.eclipse.linuxtools.lttng.ui.views.control.model.impl.TraceChannelComponent; |
| 37 | import org.eclipse.linuxtools.lttng.ui.views.control.model.impl.TraceSessionComponent; |
| 38 | import org.eclipse.linuxtools.lttng.ui.views.control.model.impl.UstProviderComponent; |
| 39 | import org.eclipse.ui.IWorkbenchPage; |
| 40 | |
| 41 | /** |
| 42 | * <b><u>EnableEventHandler</u></b> |
| 43 | * <p> |
| 44 | * Command handler implementation to assign events to a session and channel and enable/configure them. |
| 45 | * This is done on the trace provider level. |
| 46 | * </p> |
| 47 | */ |
| 48 | public class AssignEventHandler extends BaseControlViewHandler { |
| 49 | |
| 50 | // ------------------------------------------------------------------------ |
| 51 | // Attributes |
| 52 | // ------------------------------------------------------------------------ |
| 53 | /** |
| 54 | * The list of event components the command is to be executed on. |
| 55 | */ |
| 56 | private List<BaseEventComponent> fEvents = new ArrayList<BaseEventComponent>(); |
| 57 | |
| 58 | /** |
| 59 | * The list of available sessions. |
| 60 | */ |
| 61 | private TraceSessionComponent[] fSessions; |
| 62 | |
| 63 | /** |
| 64 | * Flag for indicating Kernel or UST. |
| 65 | */ |
| 66 | Boolean fIsKernel = null; |
| 67 | |
| 68 | // ------------------------------------------------------------------------ |
| 69 | // Operations |
| 70 | // ------------------------------------------------------------------------ |
| 71 | |
| 72 | /* |
| 73 | * (non-Javadoc) |
| 74 | * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent) |
| 75 | */ |
| 76 | @Override |
| 77 | public Object execute(ExecutionEvent event) throws ExecutionException { |
| 78 | |
| 79 | // Open dialog box to retrieve the session and channel where the events should be enabled in. |
| 80 | final IGetEventInfoDialog dialog = TraceControlDialogFactory.getInstance().getGetEventInfoDialog(); |
| 81 | dialog.setIsKernel(fIsKernel); |
| 82 | dialog.setSessions(fSessions); |
| 83 | |
| 84 | if (dialog.open() != Window.OK) { |
| 85 | return null; |
| 86 | } |
| 87 | |
| 88 | Job job = new Job(Messages.TraceControl_EnableEventsJob) { |
| 89 | @Override |
| 90 | protected IStatus run(IProgressMonitor monitor) { |
| 91 | |
| 92 | String errorString = null; |
| 93 | try { |
| 94 | List<String> eventNames = new ArrayList<String>(); |
| 95 | // Create list of event names |
| 96 | for (Iterator<BaseEventComponent> iterator = fEvents.iterator(); iterator.hasNext();) { |
| 97 | BaseEventComponent event = (BaseEventComponent) iterator.next(); |
| 98 | eventNames.add(event.getName()); |
| 99 | } |
| 100 | |
| 101 | TraceChannelComponent channel = dialog.getChannel(); |
| 102 | if (channel == null) { |
| 103 | // enable events on default channel (which will be created by lttng-tools) |
| 104 | dialog.getSession().enableEvents(eventNames, fIsKernel, monitor); |
| 105 | } else { |
| 106 | channel.enableEvents(eventNames, monitor); |
| 107 | } |
| 108 | |
| 109 | } catch (ExecutionException e) { |
| 110 | errorString = e.toString() + "\n"; //$NON-NLS-1$ |
| 111 | } |
| 112 | |
| 113 | // get session configuration in all cases |
| 114 | try { |
| 115 | dialog.getSession().getConfigurationFromNode(monitor); |
| 116 | } catch (ExecutionException e) { |
| 117 | if (errorString == null) { |
| 118 | errorString = new String(); |
| 119 | } |
| 120 | errorString += Messages.TraceControl_ListSessionFailure + ": " + e.toString(); //$NON-NLS-1$ |
| 121 | } |
| 122 | |
| 123 | if (errorString != null) { |
| 124 | return new Status(Status.ERROR, LTTngUiPlugin.PLUGIN_ID, errorString); |
| 125 | } |
| 126 | return Status.OK_STATUS; |
| 127 | } |
| 128 | }; |
| 129 | job.setUser(true); |
| 130 | job.schedule(); |
| 131 | return null; |
| 132 | } |
| 133 | |
| 134 | /* |
| 135 | * (non-Javadoc) |
| 136 | * @see org.eclipse.core.commands.AbstractHandler#isEnabled() |
| 137 | */ |
| 138 | @Override |
| 139 | public boolean isEnabled() { |
| 140 | fEvents.clear(); |
| 141 | fSessions = null; |
| 142 | fIsKernel = null; |
| 143 | |
| 144 | // Get workbench page for the Control View |
| 145 | IWorkbenchPage page = getWorkbenchPage(); |
| 146 | if (page == null) { |
| 147 | return false; |
| 148 | } |
| 149 | |
| 150 | // Check if one or more session are selected |
| 151 | ISelection selection = page.getSelection(ControlView.ID); |
| 152 | if (selection instanceof StructuredSelection) { |
| 153 | |
| 154 | StructuredSelection structered = ((StructuredSelection) selection); |
| 155 | for (Iterator<?> iterator = structered.iterator(); iterator.hasNext();) { |
| 156 | Object element = (Object) iterator.next(); |
| 157 | if (element instanceof BaseEventComponent) { |
| 158 | BaseEventComponent event = (BaseEventComponent) element; |
| 159 | ITraceControlComponent provider = event.getParent(); |
| 160 | |
| 161 | // check for kernel or UST provider |
| 162 | boolean temp = false; |
| 163 | if (provider instanceof KernelProviderComponent) { |
| 164 | temp = true; |
| 165 | } else if (provider instanceof UstProviderComponent) { |
| 166 | temp = false; |
| 167 | } else { |
| 168 | return false; |
| 169 | } |
| 170 | if (fIsKernel == null) { |
| 171 | fIsKernel = Boolean.valueOf(temp); |
| 172 | } else { |
| 173 | // don't mix events from Kernel and UST provider |
| 174 | if (fIsKernel.booleanValue() != temp) { |
| 175 | return false; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | // Add BaseEventComponents |
| 180 | fEvents.add(event); |
| 181 | |
| 182 | if (fSessions == null) { |
| 183 | TargetNodeComponent root = (TargetNodeComponent)event.getParent().getParent().getParent(); |
| 184 | fSessions = root.getSessions(); |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | return ((fEvents.size() > 0) && (fSessions != null) && (fSessions.length > 0)); |
| 190 | } |
| 191 | } |