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