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; |
d5278aa5 | 56 | private 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 GB |
106 | /** Get base path for resource */ |
107 | IPath path = getProject().getTracesFolder().getPath(); | |
b3e4798c AM |
108 | IResource resource = getResource(); |
109 | if (resource instanceof IFolder) { | |
110 | path = ((IFolder) resource).getFullPath(); | |
c068a752 GB |
111 | } |
112 | ||
94227c30 GB |
113 | /* |
114 | * We can get a list of available outputs once the analysis is | |
115 | * instantiated when the trace is opened | |
116 | */ | |
5c727157 | 117 | TmfCommonProjectElement parentTraceElement = getParent().getParent(); |
c068a752 | 118 | |
5c727157 AM |
119 | ITmfTrace trace = parentTraceElement.getTrace(); |
120 | if (trace == null) { | |
121 | deleteOutputs(); | |
122 | return; | |
123 | } | |
c068a752 | 124 | |
5c727157 AM |
125 | IAnalysisModule module = trace.getAnalysisModule(fAnalysisHelper.getId()); |
126 | if (module == null) { | |
127 | deleteOutputs(); | |
128 | /* | |
129 | * Trace is opened, but the analysis is null, so it does not | |
130 | * apply | |
131 | */ | |
132 | fCanExecute = false; | |
133 | return; | |
134 | } | |
d5278aa5 | 135 | |
5c727157 AM |
136 | for (IAnalysisOutput output : module.getOutputs()) { |
137 | TmfAnalysisOutputElement outputElement = childrenMap.remove(output.getName()); | |
138 | if (outputElement == null) { | |
139 | IFolder newresource = ResourcesPlugin.getWorkspace().getRoot().getFolder(path.append(output.getName())); | |
140 | outputElement = new TmfAnalysisOutputElement(output.getName(), newresource, this, output); | |
141 | addChild(outputElement); | |
142 | } | |
143 | outputElement.refreshChildren(); | |
94227c30 | 144 | } |
5c727157 | 145 | |
94227c30 GB |
146 | /* Remove outputs that are not children of this analysis anymore */ |
147 | for (TmfAnalysisOutputElement output : childrenMap.values()) { | |
148 | removeChild(output); | |
149 | } | |
150 | } | |
151 | ||
dff70ccd AM |
152 | /** |
153 | * @since 2.0 | |
154 | */ | |
155 | @Override | |
156 | public Image getIcon() { | |
157 | String iconFile = getIconFile(); | |
158 | if (iconFile != null) { | |
159 | Bundle bundle = getBundle(); | |
160 | if (bundle != null) { | |
161 | Image icon = TmfProjectModelIcons.loadIcon(bundle, iconFile); | |
162 | if (icon != null) { | |
163 | return icon; | |
164 | } | |
165 | } | |
166 | } | |
167 | return TmfProjectModelIcons.DEFAULT_ANALYSIS_ICON; | |
168 | } | |
169 | ||
d5278aa5 GB |
170 | // ------------------------------------------------------------------------ |
171 | // TmfProjectModelElement | |
172 | // ------------------------------------------------------------------------ | |
173 | ||
174 | @Override | |
175 | public Styler getStyler() { | |
176 | if (!fCanExecute) { | |
177 | return ANALYSIS_CANT_EXECUTE_STYLER; | |
178 | } | |
179 | return null; | |
180 | } | |
181 | ||
94227c30 GB |
182 | // ------------------------------------------------------------------------ |
183 | // Operations | |
184 | // ------------------------------------------------------------------------ | |
185 | ||
186 | /** | |
187 | * Get the list of analysis output model elements under this analysis | |
188 | * | |
189 | * @return Array of analysis output elements | |
190 | */ | |
191 | public List<TmfAnalysisOutputElement> getAvailableOutputs() { | |
192 | List<ITmfProjectModelElement> children = getChildren(); | |
193 | List<TmfAnalysisOutputElement> outputs = new ArrayList<>(); | |
194 | for (ITmfProjectModelElement child : children) { | |
195 | if (child instanceof TmfAnalysisOutputElement) { | |
196 | outputs.add((TmfAnalysisOutputElement) child); | |
c068a752 GB |
197 | } |
198 | } | |
94227c30 | 199 | return outputs; |
c068a752 GB |
200 | } |
201 | ||
c068a752 GB |
202 | /** |
203 | * Gets the analysis id of this module | |
204 | * | |
205 | * @return The analysis id | |
206 | */ | |
207 | public String getAnalysisId() { | |
9897c39c | 208 | return fAnalysisHelper.getId(); |
c068a752 GB |
209 | } |
210 | ||
211 | /** | |
212 | * Gets the help message for this analysis | |
213 | * | |
214 | * @return The help message | |
215 | */ | |
216 | public String getHelpMessage() { | |
5c727157 | 217 | TmfCommonProjectElement parentTrace = getParent().getParent(); |
4bc53929 | 218 | |
d5278aa5 | 219 | ITmfTrace trace = null; |
5c727157 AM |
220 | if (parentTrace instanceof TmfTraceElement) { |
221 | TmfTraceElement traceElement = (TmfTraceElement) parentTrace; | |
d5278aa5 | 222 | trace = traceElement.getTrace(); |
4bc53929 | 223 | if (trace != null) { |
9897c39c | 224 | IAnalysisModule module = trace.getAnalysisModule(fAnalysisHelper.getId()); |
ff3f02c8 | 225 | if (module != null) { |
d5278aa5 | 226 | return module.getHelpText(trace); |
ff3f02c8 | 227 | } |
4bc53929 GB |
228 | } |
229 | } | |
230 | ||
54eae41f | 231 | if (trace != null) { |
9897c39c | 232 | return fAnalysisHelper.getHelpText(trace); |
54eae41f GB |
233 | } |
234 | ||
9897c39c | 235 | return fAnalysisHelper.getHelpText(); |
c068a752 GB |
236 | } |
237 | ||
238 | /** | |
239 | * Gets the icon file name for the analysis | |
240 | * | |
241 | * @return The analysis icon file name | |
242 | */ | |
243 | public String getIconFile() { | |
9897c39c | 244 | return fAnalysisHelper.getIcon(); |
c068a752 GB |
245 | } |
246 | ||
247 | /** | |
248 | * Gets the bundle this analysis is from | |
249 | * | |
250 | * @return The analysis bundle | |
251 | */ | |
252 | public Bundle getBundle() { | |
9897c39c | 253 | return fAnalysisHelper.getBundle(); |
c068a752 GB |
254 | } |
255 | ||
94227c30 GB |
256 | /** Delete all outputs under this analysis element */ |
257 | private void deleteOutputs() { | |
258 | for (TmfAnalysisOutputElement output : getAvailableOutputs()) { | |
259 | removeChild(output); | |
260 | } | |
261 | } | |
262 | ||
c068a752 | 263 | /** |
94227c30 GB |
264 | * Make sure the trace this analysis is associated to is the currently |
265 | * selected one | |
5c727157 | 266 | * @since 2.0 |
c068a752 | 267 | */ |
5c727157 AM |
268 | public void activateParentTrace() { |
269 | TmfCommonProjectElement parentTrace = getParent().getParent(); | |
c068a752 | 270 | |
5c727157 AM |
271 | if (parentTrace instanceof TmfTraceElement) { |
272 | TmfTraceElement traceElement = (TmfTraceElement) parentTrace; | |
c068a752 GB |
273 | TmfOpenTraceHelper.openTraceFromElement(traceElement); |
274 | } | |
275 | } | |
d5278aa5 | 276 | |
82bdc349 GB |
277 | // ------------------------------------------------------------------------ |
278 | // IPropertySource2 | |
279 | // ------------------------------------------------------------------------ | |
280 | ||
281 | /** | |
282 | * @since 2.0 | |
283 | */ | |
284 | @Override | |
285 | public Object getEditableValue() { | |
286 | return null; | |
287 | } | |
288 | ||
289 | /** | |
290 | * Get the analysis properties of this analysisElement if the corresponding | |
291 | * analysis exists for the current trace | |
292 | * | |
293 | * @return a map with the names and values of the trace properties | |
294 | * respectively as keys and values | |
295 | */ | |
296 | private Map<String, String> getAnalysisProperties() { | |
297 | ITmfProjectModelElement parent = getParent(); | |
298 | ||
299 | if (parent instanceof TmfCommonProjectElement) { | |
300 | ITmfTrace trace = ((TmfCommonProjectElement) parent).getTrace(); | |
301 | if (trace == null) { | |
302 | return Collections.EMPTY_MAP; | |
303 | } | |
304 | IAnalysisModule module = trace.getAnalysisModule(fAnalysisHelper.getId()); | |
305 | if (module instanceof ITmfPropertiesProvider) { | |
306 | return ((ITmfPropertiesProvider) module).getProperties(); | |
307 | } | |
308 | } | |
309 | ||
310 | return Collections.EMPTY_MAP; | |
311 | } | |
312 | ||
313 | private Map<String, String> getAnalysisHelperProperties() { | |
314 | if (fAnalysisHelper instanceof ITmfPropertiesProvider) { | |
315 | ITmfPropertiesProvider analysisProperties = (ITmfPropertiesProvider) fAnalysisHelper; | |
316 | return analysisProperties.getProperties(); | |
317 | } | |
318 | return Collections.EMPTY_MAP; | |
319 | } | |
320 | ||
321 | /** | |
322 | * @since 2.0 | |
323 | */ | |
324 | @Override | |
325 | public IPropertyDescriptor[] getPropertyDescriptors() { | |
326 | Map<String, String> helperProperties = getAnalysisHelperProperties(); | |
327 | Map<String, String> analysisProperties = getAnalysisProperties(); | |
328 | if (!analysisProperties.isEmpty() || !helperProperties.isEmpty()) { | |
329 | List<IPropertyDescriptor> propertyDescriptorArray = new ArrayList<>(analysisProperties.size() + helperProperties.size()); | |
330 | for (Map.Entry<String, String> varName : helperProperties.entrySet()) { | |
331 | ReadOnlyTextPropertyDescriptor descriptor = new ReadOnlyTextPropertyDescriptor(this.getName() + '_' + varName.getKey(), varName.getKey()); | |
332 | descriptor.setCategory(HELPER_PROPERTIES_CATEGORY); | |
333 | propertyDescriptorArray.add(descriptor); | |
334 | } | |
335 | for (Map.Entry<String, String> varName : analysisProperties.entrySet()) { | |
336 | ReadOnlyTextPropertyDescriptor descriptor = new ReadOnlyTextPropertyDescriptor(this.getName() + '_' + varName.getKey(), varName.getKey()); | |
337 | descriptor.setCategory(ANALYSIS_PROPERTIES_CATEGORY); | |
338 | propertyDescriptorArray.add(descriptor); | |
339 | } | |
340 | return propertyDescriptorArray.toArray(new IPropertyDescriptor[analysisProperties.size() + helperProperties.size()]); | |
341 | } | |
342 | return new IPropertyDescriptor[0]; | |
343 | } | |
344 | ||
345 | /** | |
346 | * @since 2.0 | |
347 | */ | |
348 | @Override | |
349 | public Object getPropertyValue(Object id) { | |
350 | if (id == null) { | |
351 | return null; | |
352 | } | |
353 | Map<String, String> properties = getAnalysisHelperProperties(); | |
354 | String key = (String) id; | |
355 | /* Remove name from key */ | |
356 | key = key.substring(this.getName().length() + 1); | |
357 | if (properties.containsKey(key)) { | |
358 | String value = properties.get(key); | |
359 | return value; | |
360 | } | |
361 | ||
362 | properties = getAnalysisProperties(); | |
363 | if (properties.containsKey(key)) { | |
364 | String value = properties.get(key); | |
365 | return value; | |
366 | } | |
367 | ||
368 | return null; | |
369 | } | |
370 | ||
371 | /** | |
372 | * @since 2.0 | |
373 | */ | |
374 | @Override | |
375 | public final void resetPropertyValue(Object id) { | |
376 | } | |
377 | ||
378 | /** | |
379 | * @since 2.0 | |
380 | */ | |
381 | @Override | |
382 | public final void setPropertyValue(Object id, Object value) { | |
383 | } | |
384 | ||
385 | /** | |
386 | * @since 2.0 | |
387 | */ | |
388 | @Override | |
389 | public final boolean isPropertyResettable(Object id) { | |
390 | return false; | |
391 | } | |
392 | ||
393 | /** | |
394 | * @since 2.0 | |
395 | */ | |
396 | @Override | |
397 | public final boolean isPropertySet(Object id) { | |
398 | return false; | |
399 | } | |
400 | ||
c068a752 | 401 | } |