Commit | Line | Data |
---|---|---|
bbb3538a 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 | ||
18 | import org.eclipse.core.commands.AbstractHandler; | |
19 | import org.eclipse.core.commands.ExecutionEvent; | |
20 | import org.eclipse.core.commands.ExecutionException; | |
21 | import org.eclipse.core.runtime.IProgressMonitor; | |
22 | import org.eclipse.core.runtime.IStatus; | |
23 | import org.eclipse.core.runtime.Status; | |
24 | import org.eclipse.core.runtime.jobs.Job; | |
25 | import org.eclipse.jface.viewers.ISelection; | |
26 | import org.eclipse.jface.viewers.StructuredSelection; | |
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.model.TraceEnablement; | |
31 | import org.eclipse.linuxtools.lttng.ui.views.control.model.impl.TraceChannelComponent; | |
32 | import org.eclipse.linuxtools.lttng.ui.views.control.model.impl.TraceDomainComponent; | |
33 | import org.eclipse.linuxtools.lttng.ui.views.control.model.impl.TraceSessionComponent; | |
34 | import org.eclipse.ui.IWorkbenchPage; | |
35 | import org.eclipse.ui.IWorkbenchPart; | |
36 | import org.eclipse.ui.IWorkbenchWindow; | |
37 | import 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 | */ | |
45 | abstract public class ChangeChannelStateHandler extends AbstractHandler { | |
46 | ||
47 | // ------------------------------------------------------------------------ | |
48 | // Attributes | |
49 | // ------------------------------------------------------------------------ | |
50 | /** | |
51 | * Kernel domain component reference. | |
52 | */ | |
53 | protected TraceDomainComponent fKernelDomain = null; | |
54 | /** | |
55 | * UST domain component reference. | |
56 | */ | |
57 | protected TraceDomainComponent fUstDomain = null; | |
58 | /** | |
59 | * The list of kernel channel components the command is to be executed on. | |
60 | */ | |
61 | protected List<TraceChannelComponent> fKernelChannels = new ArrayList<TraceChannelComponent>(); | |
62 | /** | |
63 | * The list of USTl channel components the command is to be executed on. | |
64 | */ | |
65 | protected List<TraceChannelComponent> fUstChannels = new ArrayList<TraceChannelComponent>(); | |
66 | ||
67 | // ------------------------------------------------------------------------ | |
68 | // Accessors | |
69 | // ------------------------------------------------------------------------ | |
70 | /** | |
71 | * @return the new state to set | |
72 | */ | |
73 | abstract protected TraceEnablement getNewState(); | |
74 | ||
75 | // ------------------------------------------------------------------------ | |
76 | // Operations | |
77 | // ------------------------------------------------------------------------ | |
78 | /** | |
79 | * @return the new state to set | |
80 | */ | |
81 | abstract protected void changeState(TraceDomainComponent domain, List<String> channelNames, IProgressMonitor monitor) throws ExecutionException; | |
82 | ||
83 | /* | |
84 | * (non-Javadoc) | |
85 | * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent) | |
86 | */ | |
87 | @Override | |
88 | public Object execute(ExecutionEvent event) throws ExecutionException { | |
89 | ||
90 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
91 | ||
92 | if (window == null) { | |
93 | return false; | |
94 | } | |
95 | ||
96 | Job job = new Job(Messages.TraceControl_EnableChannelJob) { // TODO | |
97 | @Override | |
98 | protected IStatus run(IProgressMonitor monitor) { | |
99 | String errorString = null; | |
100 | ||
101 | TraceSessionComponent session = null; | |
102 | ||
103 | try { | |
104 | if (fKernelDomain != null) { | |
105 | session = (TraceSessionComponent)fKernelDomain.getParent(); | |
106 | List<String> channelNames = new ArrayList<String>(); | |
107 | for (Iterator<TraceChannelComponent> iterator = fKernelChannels.iterator(); iterator.hasNext();) { | |
108 | // Enable all selected channels which are disabled | |
109 | TraceChannelComponent channel = (TraceChannelComponent) iterator.next(); | |
110 | channelNames.add(channel.getName()); | |
111 | } | |
112 | ||
113 | changeState(fKernelDomain, channelNames, monitor); | |
114 | ||
115 | for (Iterator<TraceChannelComponent> iterator = fKernelChannels.iterator(); iterator.hasNext();) { | |
116 | // Enable all selected channels which are disabled | |
117 | TraceChannelComponent channel = (TraceChannelComponent) iterator.next(); | |
118 | channel.setState(getNewState()); | |
119 | } | |
120 | } | |
121 | ||
122 | if (fUstDomain != null) { | |
123 | if (session == null) { | |
124 | session = (TraceSessionComponent)fUstDomain.getParent(); | |
125 | } | |
126 | ||
127 | List<String> channelNames = new ArrayList<String>(); | |
128 | for (Iterator<TraceChannelComponent> iterator = fUstChannels.iterator(); iterator.hasNext();) { | |
129 | // Enable all selected channels which are disabled | |
130 | TraceChannelComponent channel = (TraceChannelComponent) iterator.next(); | |
131 | channelNames.add(channel.getName()); | |
132 | } | |
133 | ||
134 | changeState(fUstDomain, channelNames, monitor); | |
135 | ||
136 | for (Iterator<TraceChannelComponent> iterator = fUstChannels.iterator(); iterator.hasNext();) { | |
137 | // Enable all selected channels which are disabled | |
138 | TraceChannelComponent channel = (TraceChannelComponent) iterator.next(); | |
139 | channel.setState(getNewState()); | |
140 | } | |
141 | } | |
142 | } catch (ExecutionException e) { | |
143 | errorString = e.toString() + "\n"; //$NON-NLS-1$ | |
144 | } | |
145 | ||
146 | // In all cases notify listeners | |
147 | session.fireComponentChanged(session); | |
148 | ||
149 | if (errorString != null) { | |
150 | return new Status(Status.ERROR, LTTngUiPlugin.PLUGIN_ID, errorString); | |
151 | } | |
152 | ||
153 | return Status.OK_STATUS; | |
154 | }}; | |
155 | job.setUser(true); | |
156 | job.schedule(); | |
157 | ||
158 | return null; | |
159 | } | |
160 | ||
161 | /* | |
162 | * (non-Javadoc) | |
163 | * @see org.eclipse.core.commands.AbstractHandler#isEnabled() | |
164 | */ | |
165 | @Override | |
166 | public boolean isEnabled() { | |
167 | reset(); | |
168 | ||
169 | // Check if we are closing down | |
170 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
171 | if (window == null) { | |
172 | return false; | |
173 | } | |
174 | ||
175 | // Check if we are in the Project View | |
176 | IWorkbenchPage page = window.getActivePage(); | |
177 | if (page == null) { | |
178 | return false; | |
179 | } | |
180 | ||
181 | IWorkbenchPart part = page.getActivePart(); | |
182 | if (!(part instanceof ControlView)) { | |
183 | return false; | |
184 | } | |
185 | ||
186 | // Check if one or more session are selected | |
187 | ISelection selection = page.getSelection(ControlView.ID); | |
188 | if (selection instanceof StructuredSelection) { | |
189 | StructuredSelection structered = ((StructuredSelection) selection); | |
190 | String sessionName = null; | |
191 | for (Iterator<?> iterator = structered.iterator(); iterator.hasNext();) { | |
192 | Object element = (Object) iterator.next(); | |
193 | ||
194 | if (element instanceof TraceChannelComponent) { | |
195 | ||
196 | // Add only TraceChannelComponents that are disabled | |
197 | TraceChannelComponent channel = (TraceChannelComponent) element; | |
198 | if (sessionName == null) { | |
199 | sessionName = String.valueOf(channel.getSessionName()); | |
200 | } | |
201 | ||
202 | // Enable command only for channels of same session | |
203 | if (!sessionName.equals(channel.getSessionName())) { | |
204 | reset(); | |
205 | break; | |
206 | } | |
207 | ||
208 | if ((channel.getState() != getNewState())) { | |
209 | if (channel.isKernel()) { | |
210 | fKernelChannels.add(channel); | |
211 | if (fKernelDomain == null) { | |
212 | fKernelDomain = (TraceDomainComponent) channel.getParent(); | |
213 | } | |
214 | } else { | |
215 | fUstChannels.add(channel); | |
216 | if (fUstDomain == null) { | |
217 | fUstDomain = (TraceDomainComponent) channel.getParent(); | |
218 | } | |
219 | } | |
220 | } | |
221 | } | |
222 | } | |
223 | } | |
224 | return fKernelChannels.size() + fUstChannels.size() > 0; | |
225 | } | |
226 | ||
227 | /** | |
228 | * Reset members | |
229 | */ | |
230 | private void reset() { | |
231 | fKernelDomain = null; | |
232 | fUstDomain = null; | |
233 | fKernelChannels.clear(); | |
234 | fUstChannels.clear(); | |
235 | } | |
236 | } |