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