Commit | Line | Data |
---|---|---|
c068a752 | 1 | /******************************************************************************* |
60ae41e1 | 2 | * Copyright (c) 2013, 2014 École Polytechnique de Montréal |
c068a752 GB |
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 | * Geneviève Bastien - Initial API and implementation | |
339d539c | 11 | * Patrick Tasse - Add support for folder elements |
c068a752 GB |
12 | *******************************************************************************/ |
13 | ||
2bdf0193 | 14 | package org.eclipse.tracecompass.tmf.ui.project.model; |
c068a752 GB |
15 | |
16 | import java.util.ArrayList; | |
82bdc349 | 17 | import java.util.Collections; |
94227c30 | 18 | import java.util.HashMap; |
c068a752 | 19 | import java.util.List; |
94227c30 | 20 | import java.util.Map; |
c068a752 GB |
21 | |
22 | import org.eclipse.core.resources.IFolder; | |
23 | import org.eclipse.core.resources.IResource; | |
24 | import org.eclipse.core.resources.ResourcesPlugin; | |
25 | import org.eclipse.core.runtime.IPath; | |
ba27dd38 | 26 | import org.eclipse.jdt.annotation.NonNull; |
d5278aa5 | 27 | import org.eclipse.jface.viewers.StyledString.Styler; |
d5278aa5 | 28 | import org.eclipse.swt.graphics.TextStyle; |
2bdf0193 AM |
29 | import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule; |
30 | import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModuleHelper; | |
31 | import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisOutput; | |
82bdc349 | 32 | import org.eclipse.tracecompass.tmf.core.project.model.ITmfPropertiesProvider; |
2bdf0193 | 33 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; |
82bdc349 GB |
34 | import org.eclipse.tracecompass.tmf.ui.properties.ReadOnlyTextPropertyDescriptor; |
35 | import org.eclipse.ui.views.properties.IPropertyDescriptor; | |
36 | import org.eclipse.ui.views.properties.IPropertySource2; | |
c068a752 GB |
37 | import org.osgi.framework.Bundle; |
38 | ||
39 | /** | |
40 | * Class for project elements of type analysis modules | |
41 | * | |
42 | * @author Geneviève Bastien | |
82bdc349 | 43 | * @since 2.0 |
c068a752 | 44 | */ |
82bdc349 | 45 | public class TmfAnalysisElement extends TmfProjectModelElement implements ITmfStyledProjectModelElement, IPropertySource2 { |
d5278aa5 GB |
46 | |
47 | private static final Styler ANALYSIS_CANT_EXECUTE_STYLER = new Styler() { | |
48 | @Override | |
49 | public void applyStyles(TextStyle textStyle) { | |
50 | textStyle.strikeout = true; | |
51 | } | |
52 | }; | |
c068a752 | 53 | |
9897c39c | 54 | private final @NonNull IAnalysisModuleHelper fAnalysisHelper; |
d5278aa5 | 55 | private boolean fCanExecute = true; |
c068a752 | 56 | |
82bdc349 GB |
57 | private static final String ANALYSIS_PROPERTIES_CATEGORY = Messages.TmfAnalysisElement_AnalysisProperties; |
58 | private static final String HELPER_PROPERTIES_CATEGORY = Messages.TmfAnalysisElement_HelperProperties; | |
59 | ||
c068a752 GB |
60 | /** |
61 | * Constructor | |
62 | * | |
63 | * @param name | |
64 | * Name of the analysis | |
65 | * @param resource | |
66 | * The resource | |
67 | * @param parent | |
68 | * Parent of the analysis | |
9897c39c GB |
69 | * @param module |
70 | * The analysis module helper | |
dbc7991d | 71 | * @since 1.0 |
c068a752 | 72 | */ |
9897c39c | 73 | protected TmfAnalysisElement(String name, IResource resource, ITmfProjectModelElement parent, @NonNull IAnalysisModuleHelper module) { |
c068a752 | 74 | super(name, resource, parent); |
9897c39c | 75 | fAnalysisHelper = module; |
c068a752 GB |
76 | } |
77 | ||
94227c30 GB |
78 | // ------------------------------------------------------------------------ |
79 | // TmfProjectModelElement | |
80 | // ------------------------------------------------------------------------ | |
c068a752 | 81 | |
b3e4798c AM |
82 | /** |
83 | * @since 2.0 | |
84 | */ | |
94227c30 | 85 | @Override |
b3e4798c | 86 | protected void refreshChildren() { |
d5278aa5 GB |
87 | fCanExecute = true; |
88 | ||
94227c30 GB |
89 | /* Refresh the outputs of this analysis */ |
90 | Map<String, TmfAnalysisOutputElement> childrenMap = new HashMap<>(); | |
91 | for (TmfAnalysisOutputElement output : getAvailableOutputs()) { | |
92 | childrenMap.put(output.getName(), output); | |
c068a752 GB |
93 | } |
94 | ||
c068a752 GB |
95 | /** Get base path for resource */ |
96 | IPath path = getProject().getTracesFolder().getPath(); | |
b3e4798c AM |
97 | IResource resource = getResource(); |
98 | if (resource instanceof IFolder) { | |
99 | path = ((IFolder) resource).getFullPath(); | |
c068a752 GB |
100 | } |
101 | ||
94227c30 GB |
102 | /* |
103 | * We can get a list of available outputs once the analysis is | |
104 | * instantiated when the trace is opened | |
105 | */ | |
c068a752 | 106 | ITmfProjectModelElement parent = getParent(); |
8f5221c2 GB |
107 | if (parent instanceof TmfCommonProjectElement) { |
108 | ITmfTrace trace = ((TmfCommonProjectElement) parent).getTrace(); | |
c068a752 | 109 | if (trace == null) { |
94227c30 GB |
110 | deleteOutputs(); |
111 | return; | |
c068a752 GB |
112 | } |
113 | ||
9897c39c | 114 | IAnalysisModule module = trace.getAnalysisModule(fAnalysisHelper.getId()); |
c068a752 | 115 | if (module == null) { |
94227c30 | 116 | deleteOutputs(); |
d5278aa5 GB |
117 | /* |
118 | * Trace is opened, but the analysis is null, so it does not | |
119 | * apply | |
120 | */ | |
121 | fCanExecute = false; | |
94227c30 | 122 | return; |
c068a752 GB |
123 | } |
124 | ||
125 | for (IAnalysisOutput output : module.getOutputs()) { | |
94227c30 GB |
126 | TmfAnalysisOutputElement outputElement = childrenMap.remove(output.getName()); |
127 | if (outputElement == null) { | |
c068a752 | 128 | IFolder newresource = ResourcesPlugin.getWorkspace().getRoot().getFolder(path.append(output.getName())); |
94227c30 | 129 | outputElement = new TmfAnalysisOutputElement(output.getName(), newresource, this, output); |
b3e4798c | 130 | addChild(outputElement); |
c068a752 | 131 | } |
94227c30 GB |
132 | outputElement.refreshChildren(); |
133 | } | |
d5278aa5 | 134 | |
94227c30 GB |
135 | } |
136 | /* Remove outputs that are not children of this analysis anymore */ | |
137 | for (TmfAnalysisOutputElement output : childrenMap.values()) { | |
138 | removeChild(output); | |
139 | } | |
140 | } | |
141 | ||
d5278aa5 GB |
142 | // ------------------------------------------------------------------------ |
143 | // TmfProjectModelElement | |
144 | // ------------------------------------------------------------------------ | |
145 | ||
146 | @Override | |
147 | public Styler getStyler() { | |
148 | if (!fCanExecute) { | |
149 | return ANALYSIS_CANT_EXECUTE_STYLER; | |
150 | } | |
151 | return null; | |
152 | } | |
153 | ||
94227c30 GB |
154 | // ------------------------------------------------------------------------ |
155 | // Operations | |
156 | // ------------------------------------------------------------------------ | |
157 | ||
158 | /** | |
159 | * Get the list of analysis output model elements under this analysis | |
160 | * | |
161 | * @return Array of analysis output elements | |
162 | */ | |
163 | public List<TmfAnalysisOutputElement> getAvailableOutputs() { | |
164 | List<ITmfProjectModelElement> children = getChildren(); | |
165 | List<TmfAnalysisOutputElement> outputs = new ArrayList<>(); | |
166 | for (ITmfProjectModelElement child : children) { | |
167 | if (child instanceof TmfAnalysisOutputElement) { | |
168 | outputs.add((TmfAnalysisOutputElement) child); | |
c068a752 GB |
169 | } |
170 | } | |
94227c30 | 171 | return outputs; |
c068a752 GB |
172 | } |
173 | ||
c068a752 GB |
174 | /** |
175 | * Gets the analysis id of this module | |
176 | * | |
177 | * @return The analysis id | |
178 | */ | |
179 | public String getAnalysisId() { | |
9897c39c | 180 | return fAnalysisHelper.getId(); |
c068a752 GB |
181 | } |
182 | ||
183 | /** | |
184 | * Gets the help message for this analysis | |
185 | * | |
186 | * @return The help message | |
187 | */ | |
188 | public String getHelpMessage() { | |
4bc53929 GB |
189 | ITmfProjectModelElement parent = getParent(); |
190 | ||
d5278aa5 | 191 | ITmfTrace trace = null; |
4bc53929 GB |
192 | if (parent instanceof TmfTraceElement) { |
193 | TmfTraceElement traceElement = (TmfTraceElement) parent; | |
d5278aa5 | 194 | trace = traceElement.getTrace(); |
4bc53929 | 195 | if (trace != null) { |
9897c39c | 196 | IAnalysisModule module = trace.getAnalysisModule(fAnalysisHelper.getId()); |
ff3f02c8 | 197 | if (module != null) { |
d5278aa5 | 198 | return module.getHelpText(trace); |
ff3f02c8 | 199 | } |
4bc53929 GB |
200 | } |
201 | } | |
202 | ||
54eae41f | 203 | if (trace != null) { |
9897c39c | 204 | return fAnalysisHelper.getHelpText(trace); |
54eae41f GB |
205 | } |
206 | ||
9897c39c | 207 | return fAnalysisHelper.getHelpText(); |
c068a752 GB |
208 | } |
209 | ||
210 | /** | |
211 | * Gets the icon file name for the analysis | |
212 | * | |
213 | * @return The analysis icon file name | |
214 | */ | |
215 | public String getIconFile() { | |
9897c39c | 216 | return fAnalysisHelper.getIcon(); |
c068a752 GB |
217 | } |
218 | ||
219 | /** | |
220 | * Gets the bundle this analysis is from | |
221 | * | |
222 | * @return The analysis bundle | |
223 | */ | |
224 | public Bundle getBundle() { | |
9897c39c | 225 | return fAnalysisHelper.getBundle(); |
c068a752 GB |
226 | } |
227 | ||
94227c30 GB |
228 | /** Delete all outputs under this analysis element */ |
229 | private void deleteOutputs() { | |
230 | for (TmfAnalysisOutputElement output : getAvailableOutputs()) { | |
231 | removeChild(output); | |
232 | } | |
233 | } | |
234 | ||
c068a752 | 235 | /** |
94227c30 GB |
236 | * Make sure the trace this analysis is associated to is the currently |
237 | * selected one | |
c068a752 GB |
238 | */ |
239 | public void activateParent() { | |
240 | ITmfProjectModelElement parent = getParent(); | |
241 | ||
242 | if (parent instanceof TmfTraceElement) { | |
243 | TmfTraceElement traceElement = (TmfTraceElement) parent; | |
244 | TmfOpenTraceHelper.openTraceFromElement(traceElement); | |
245 | } | |
246 | } | |
d5278aa5 | 247 | |
82bdc349 GB |
248 | // ------------------------------------------------------------------------ |
249 | // IPropertySource2 | |
250 | // ------------------------------------------------------------------------ | |
251 | ||
252 | /** | |
253 | * @since 2.0 | |
254 | */ | |
255 | @Override | |
256 | public Object getEditableValue() { | |
257 | return null; | |
258 | } | |
259 | ||
260 | /** | |
261 | * Get the analysis properties of this analysisElement if the corresponding | |
262 | * analysis exists for the current trace | |
263 | * | |
264 | * @return a map with the names and values of the trace properties | |
265 | * respectively as keys and values | |
266 | */ | |
267 | private Map<String, String> getAnalysisProperties() { | |
268 | ITmfProjectModelElement parent = getParent(); | |
269 | ||
270 | if (parent instanceof TmfCommonProjectElement) { | |
271 | ITmfTrace trace = ((TmfCommonProjectElement) parent).getTrace(); | |
272 | if (trace == null) { | |
273 | return Collections.EMPTY_MAP; | |
274 | } | |
275 | IAnalysisModule module = trace.getAnalysisModule(fAnalysisHelper.getId()); | |
276 | if (module instanceof ITmfPropertiesProvider) { | |
277 | return ((ITmfPropertiesProvider) module).getProperties(); | |
278 | } | |
279 | } | |
280 | ||
281 | return Collections.EMPTY_MAP; | |
282 | } | |
283 | ||
284 | private Map<String, String> getAnalysisHelperProperties() { | |
285 | if (fAnalysisHelper instanceof ITmfPropertiesProvider) { | |
286 | ITmfPropertiesProvider analysisProperties = (ITmfPropertiesProvider) fAnalysisHelper; | |
287 | return analysisProperties.getProperties(); | |
288 | } | |
289 | return Collections.EMPTY_MAP; | |
290 | } | |
291 | ||
292 | /** | |
293 | * @since 2.0 | |
294 | */ | |
295 | @Override | |
296 | public IPropertyDescriptor[] getPropertyDescriptors() { | |
297 | Map<String, String> helperProperties = getAnalysisHelperProperties(); | |
298 | Map<String, String> analysisProperties = getAnalysisProperties(); | |
299 | if (!analysisProperties.isEmpty() || !helperProperties.isEmpty()) { | |
300 | List<IPropertyDescriptor> propertyDescriptorArray = new ArrayList<>(analysisProperties.size() + helperProperties.size()); | |
301 | for (Map.Entry<String, String> varName : helperProperties.entrySet()) { | |
302 | ReadOnlyTextPropertyDescriptor descriptor = new ReadOnlyTextPropertyDescriptor(this.getName() + '_' + varName.getKey(), varName.getKey()); | |
303 | descriptor.setCategory(HELPER_PROPERTIES_CATEGORY); | |
304 | propertyDescriptorArray.add(descriptor); | |
305 | } | |
306 | for (Map.Entry<String, String> varName : analysisProperties.entrySet()) { | |
307 | ReadOnlyTextPropertyDescriptor descriptor = new ReadOnlyTextPropertyDescriptor(this.getName() + '_' + varName.getKey(), varName.getKey()); | |
308 | descriptor.setCategory(ANALYSIS_PROPERTIES_CATEGORY); | |
309 | propertyDescriptorArray.add(descriptor); | |
310 | } | |
311 | return propertyDescriptorArray.toArray(new IPropertyDescriptor[analysisProperties.size() + helperProperties.size()]); | |
312 | } | |
313 | return new IPropertyDescriptor[0]; | |
314 | } | |
315 | ||
316 | /** | |
317 | * @since 2.0 | |
318 | */ | |
319 | @Override | |
320 | public Object getPropertyValue(Object id) { | |
321 | if (id == null) { | |
322 | return null; | |
323 | } | |
324 | Map<String, String> properties = getAnalysisHelperProperties(); | |
325 | String key = (String) id; | |
326 | /* Remove name from key */ | |
327 | key = key.substring(this.getName().length() + 1); | |
328 | if (properties.containsKey(key)) { | |
329 | String value = properties.get(key); | |
330 | return value; | |
331 | } | |
332 | ||
333 | properties = getAnalysisProperties(); | |
334 | if (properties.containsKey(key)) { | |
335 | String value = properties.get(key); | |
336 | return value; | |
337 | } | |
338 | ||
339 | return null; | |
340 | } | |
341 | ||
342 | /** | |
343 | * @since 2.0 | |
344 | */ | |
345 | @Override | |
346 | public final void resetPropertyValue(Object id) { | |
347 | } | |
348 | ||
349 | /** | |
350 | * @since 2.0 | |
351 | */ | |
352 | @Override | |
353 | public final void setPropertyValue(Object id, Object value) { | |
354 | } | |
355 | ||
356 | /** | |
357 | * @since 2.0 | |
358 | */ | |
359 | @Override | |
360 | public final boolean isPropertyResettable(Object id) { | |
361 | return false; | |
362 | } | |
363 | ||
364 | /** | |
365 | * @since 2.0 | |
366 | */ | |
367 | @Override | |
368 | public final boolean isPropertySet(Object id) { | |
369 | return false; | |
370 | } | |
371 | ||
c068a752 | 372 | } |