Commit | Line | Data |
---|---|---|
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 | **********************************************************************/ | |
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 | ||
6503ae0f BH |
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.lttng.ui.LTTngUiPlugin; | |
27 | import org.eclipse.linuxtools.lttng.ui.views.control.ControlView; | |
28 | import org.eclipse.linuxtools.lttng.ui.views.control.Messages; | |
29 | import org.eclipse.linuxtools.lttng.ui.views.control.model.TraceEnablement; | |
30 | import org.eclipse.linuxtools.lttng.ui.views.control.model.impl.TraceChannelComponent; | |
31 | import org.eclipse.linuxtools.lttng.ui.views.control.model.impl.TraceEventComponent; | |
32 | import org.eclipse.linuxtools.lttng.ui.views.control.model.impl.TraceSessionComponent; | |
33 | import org.eclipse.ui.IWorkbenchPage; | |
6503ae0f BH |
34 | import org.eclipse.ui.IWorkbenchWindow; |
35 | import org.eclipse.ui.PlatformUI; | |
36 | ||
37 | /** | |
38 | * <b><u>EnableChannelHandler</u></b> | |
39 | * <p> | |
40 | * Base Command handler implementation to enable or disabling a trace channel. | |
41 | * </p> | |
42 | */ | |
498704b3 | 43 | abstract public class ChangeEventStateHandler extends BaseControlViewHandler { |
6503ae0f BH |
44 | |
45 | // ------------------------------------------------------------------------ | |
46 | // Attributes | |
47 | // ------------------------------------------------------------------------ | |
48 | /** | |
49 | * Channel component reference. | |
50 | */ | |
51 | protected TraceChannelComponent fChannel = null; | |
52 | /** | |
53 | * The list of kernel channel components the command is to be executed on. | |
54 | */ | |
55 | protected List<TraceEventComponent> fEvents = new ArrayList<TraceEventComponent>(); | |
56 | ||
57 | // ------------------------------------------------------------------------ | |
58 | // Accessors | |
59 | // ------------------------------------------------------------------------ | |
60 | /** | |
61 | * @return the new state to set | |
62 | */ | |
63 | abstract protected TraceEnablement getNewState(); | |
64 | ||
65 | // ------------------------------------------------------------------------ | |
66 | // Operations | |
67 | // ------------------------------------------------------------------------ | |
68 | /** | |
69 | * Change the state | |
70 | * @param channel - channel of events to be enabled | |
71 | * @param eventNames - list event names | |
72 | * @param monitor - a progress monitor | |
73 | * @throws ExecutionException | |
74 | */ | |
75 | abstract protected void changeState(TraceChannelComponent channel, List<String> eventNames, IProgressMonitor monitor) throws ExecutionException; | |
76 | ||
77 | /* | |
78 | * (non-Javadoc) | |
79 | * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent) | |
80 | */ | |
81 | @Override | |
82 | public Object execute(ExecutionEvent event) throws ExecutionException { | |
83 | ||
84 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
85 | ||
86 | if (window == null) { | |
87 | return false; | |
88 | } | |
89 | ||
90 | Job job = new Job(Messages.TraceControl_ChangeChannelStateJob) { | |
91 | @Override | |
92 | protected IStatus run(IProgressMonitor monitor) { | |
93 | String errorString = null; | |
94 | ||
95 | TraceSessionComponent session = null; | |
3e91c9c0 | 96 | |
6503ae0f | 97 | try { |
3e91c9c0 | 98 | boolean isAll = false; |
6503ae0f BH |
99 | if (fChannel != null) { |
100 | session = fChannel.getSession(); | |
3e91c9c0 | 101 | List<String> eventNames = new ArrayList<String>(); |
6503ae0f | 102 | for (Iterator<TraceEventComponent> iterator = fEvents.iterator(); iterator.hasNext();) { |
3e91c9c0 BH |
103 | // Enable/disable all selected channels which are disabled |
104 | TraceEventComponent event = (TraceEventComponent) iterator.next(); | |
105 | ||
106 | // Workaround for wildcard handling in lttng-tools | |
107 | if ("*".equals(event.getName())) { //$NON-NLS-1$ | |
108 | isAll = true; | |
109 | } else { | |
110 | eventNames.add(event.getName()); | |
111 | } | |
112 | } | |
113 | if (isAll) { | |
114 | changeState(fChannel, null, monitor); | |
115 | } | |
116 | ||
117 | if (eventNames.size() > 0) { | |
118 | changeState(fChannel, eventNames, monitor); | |
6503ae0f | 119 | } |
6503ae0f BH |
120 | |
121 | for (Iterator<TraceEventComponent> iterator = fEvents.iterator(); iterator.hasNext();) { | |
122 | // Enable all selected channels which are disabled | |
123 | TraceEventComponent channel = (TraceEventComponent) iterator.next(); | |
124 | channel.setState(getNewState()); | |
125 | } | |
126 | } | |
127 | } catch (ExecutionException e) { | |
128 | errorString = e.toString() + "\n"; //$NON-NLS-1$ | |
129 | } | |
130 | ||
131 | // In all cases notify listeners | |
132 | session.fireComponentChanged(session); | |
133 | ||
134 | if (errorString != null) { | |
135 | return new Status(Status.ERROR, LTTngUiPlugin.PLUGIN_ID, errorString); | |
136 | } | |
137 | ||
138 | return Status.OK_STATUS; | |
139 | } | |
140 | }; | |
141 | job.setUser(true); | |
142 | job.schedule(); | |
143 | ||
144 | return null; | |
145 | } | |
146 | ||
147 | /* | |
148 | * (non-Javadoc) | |
149 | * @see org.eclipse.core.commands.AbstractHandler#isEnabled() | |
150 | */ | |
151 | @Override | |
152 | public boolean isEnabled() { | |
498704b3 BH |
153 | // Get workbench page for the Control View |
154 | IWorkbenchPage page = getWorkbenchPage(); | |
6503ae0f BH |
155 | if (page == null) { |
156 | return false; | |
157 | } | |
158 | ||
498704b3 | 159 | reset(); |
6503ae0f BH |
160 | |
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; | |
167 | ||
168 | for (Iterator<?> iterator = structered.iterator(); iterator.hasNext();) { | |
169 | Object element = (Object) iterator.next(); | |
170 | ||
171 | if (element instanceof TraceEventComponent) { | |
172 | ||
3e91c9c0 | 173 | TraceEventComponent event = (TraceEventComponent) element; |
6503ae0f | 174 | if (sessionName == null) { |
3e91c9c0 | 175 | sessionName = String.valueOf(event.getSessionName()); |
6503ae0f BH |
176 | } |
177 | ||
178 | if (fChannel == null) { | |
3e91c9c0 | 179 | fChannel = (TraceChannelComponent)event.getParent(); |
6503ae0f BH |
180 | } |
181 | ||
182 | if (channelName == null) { | |
3e91c9c0 | 183 | channelName = event.getChannelName(); |
6503ae0f | 184 | } |
3e91c9c0 | 185 | |
6503ae0f | 186 | // Enable command only for events of same session, same channel and domain |
3e91c9c0 BH |
187 | if ((!sessionName.equals(event.getSessionName())) || |
188 | (!channelName.equals(event.getChannelName())) || | |
189 | (fChannel.isKernel() != event.isKernel())) { | |
6503ae0f BH |
190 | reset(); |
191 | break; | |
192 | } | |
193 | ||
3e91c9c0 BH |
194 | if ((event.getState() != getNewState())) { |
195 | fEvents.add(event); | |
6503ae0f BH |
196 | } |
197 | } | |
198 | } | |
199 | } | |
200 | return fEvents.size() > 0; | |
201 | } | |
202 | ||
203 | /** | |
204 | * Reset members | |
205 | */ | |
206 | private void reset() { | |
207 | fChannel = null; | |
208 | fEvents.clear(); | |
209 | } | |
210 | } |