1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 École Polytechnique de Montréal
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
10 * Geneviève Bastien - Initial API and implementation
11 * Patrick Tasse - Add support for folder elements
12 *******************************************************************************/
14 package org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
;
16 import java
.util
.ArrayList
;
17 import java
.util
.Collections
;
18 import java
.util
.HashMap
;
19 import java
.util
.List
;
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
;
26 import org
.eclipse
.jdt
.annotation
.NonNull
;
27 import org
.eclipse
.jface
.viewers
.StyledString
.Styler
;
28 import org
.eclipse
.swt
.graphics
.Image
;
29 import org
.eclipse
.swt
.graphics
.TextStyle
;
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
;
33 import org
.eclipse
.tracecompass
.tmf
.core
.project
.model
.ITmfPropertiesProvider
;
34 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
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
;
38 import org
.osgi
.framework
.Bundle
;
41 * Class for project elements of type analysis modules
43 * @author Geneviève Bastien
46 public class TmfAnalysisElement
extends TmfProjectModelElement
implements ITmfStyledProjectModelElement
, IPropertySource2
{
48 private static final Styler ANALYSIS_CANT_EXECUTE_STYLER
= new Styler() {
50 public void applyStyles(TextStyle textStyle
) {
51 textStyle
.strikeout
= true;
55 private final @NonNull IAnalysisModuleHelper fAnalysisHelper
;
56 private boolean fCanExecute
= true;
58 private static final String ANALYSIS_PROPERTIES_CATEGORY
= Messages
.TmfAnalysisElement_AnalysisProperties
;
59 private static final String HELPER_PROPERTIES_CATEGORY
= Messages
.TmfAnalysisElement_HelperProperties
;
65 * Name of the analysis
69 * Parent of the analysis
71 * The analysis module helper
74 protected TmfAnalysisElement(String name
, IResource resource
,
75 TmfViewsElement parent
, @NonNull IAnalysisModuleHelper module
) {
76 super(name
, resource
, parent
);
77 fAnalysisHelper
= module
;
80 // ------------------------------------------------------------------------
81 // TmfProjectModelElement
82 // ------------------------------------------------------------------------
88 public TmfViewsElement
getParent() {
89 /* Type enforced at constructor */
90 return (TmfViewsElement
) super.getParent();
97 protected void refreshChildren() {
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
);
106 /** Get base path for resource */
107 IPath path
= getProject().getTracesFolder().getPath();
108 IResource resource
= getResource();
109 if (resource
instanceof IFolder
) {
110 path
= ((IFolder
) resource
).getFullPath();
114 * We can get a list of available outputs once the analysis is
115 * instantiated when the trace is opened
117 TmfCommonProjectElement parentTraceElement
= getParent().getParent();
119 ITmfTrace trace
= parentTraceElement
.getTrace();
125 IAnalysisModule module
= trace
.getAnalysisModule(fAnalysisHelper
.getId());
126 if (module
== null) {
129 * Trace is opened, but the analysis is null, so it does not
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
);
143 outputElement
.refreshChildren();
146 /* Remove outputs that are not children of this analysis anymore */
147 for (TmfAnalysisOutputElement output
: childrenMap
.values()) {
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
);
167 return TmfProjectModelIcons
.DEFAULT_ANALYSIS_ICON
;
170 // ------------------------------------------------------------------------
171 // TmfProjectModelElement
172 // ------------------------------------------------------------------------
175 public Styler
getStyler() {
177 return ANALYSIS_CANT_EXECUTE_STYLER
;
182 // ------------------------------------------------------------------------
184 // ------------------------------------------------------------------------
187 * Get the list of analysis output model elements under this analysis
189 * @return Array of analysis output elements
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
);
203 * Gets the analysis id of this module
205 * @return The analysis id
207 public String
getAnalysisId() {
208 return fAnalysisHelper
.getId();
212 * Gets the help message for this analysis
214 * @return The help message
216 public String
getHelpMessage() {
217 TmfCommonProjectElement parentTrace
= getParent().getParent();
219 ITmfTrace trace
= null;
220 if (parentTrace
instanceof TmfTraceElement
) {
221 TmfTraceElement traceElement
= (TmfTraceElement
) parentTrace
;
222 trace
= traceElement
.getTrace();
224 IAnalysisModule module
= trace
.getAnalysisModule(fAnalysisHelper
.getId());
225 if (module
!= null) {
226 return module
.getHelpText(trace
);
232 return fAnalysisHelper
.getHelpText(trace
);
235 return fAnalysisHelper
.getHelpText();
239 * Gets the icon file name for the analysis
241 * @return The analysis icon file name
243 public String
getIconFile() {
244 return fAnalysisHelper
.getIcon();
248 * Gets the bundle this analysis is from
250 * @return The analysis bundle
252 public Bundle
getBundle() {
253 return fAnalysisHelper
.getBundle();
256 /** Delete all outputs under this analysis element */
257 private void deleteOutputs() {
258 for (TmfAnalysisOutputElement output
: getAvailableOutputs()) {
264 * Make sure the trace this analysis is associated to is the currently
268 public void activateParentTrace() {
269 TmfCommonProjectElement parentTrace
= getParent().getParent();
271 if (parentTrace
instanceof TmfTraceElement
) {
272 TmfTraceElement traceElement
= (TmfTraceElement
) parentTrace
;
273 TmfOpenTraceHelper
.openTraceFromElement(traceElement
);
277 // ------------------------------------------------------------------------
279 // ------------------------------------------------------------------------
285 public Object
getEditableValue() {
290 * Get the analysis properties of this analysisElement if the corresponding
291 * analysis exists for the current trace
293 * @return a map with the names and values of the trace properties
294 * respectively as keys and values
296 private Map
<String
, String
> getAnalysisProperties() {
297 ITmfProjectModelElement parent
= getParent();
299 if (parent
instanceof TmfCommonProjectElement
) {
300 ITmfTrace trace
= ((TmfCommonProjectElement
) parent
).getTrace();
302 return Collections
.EMPTY_MAP
;
304 IAnalysisModule module
= trace
.getAnalysisModule(fAnalysisHelper
.getId());
305 if (module
instanceof ITmfPropertiesProvider
) {
306 return ((ITmfPropertiesProvider
) module
).getProperties();
310 return Collections
.EMPTY_MAP
;
313 private Map
<String
, String
> getAnalysisHelperProperties() {
314 if (fAnalysisHelper
instanceof ITmfPropertiesProvider
) {
315 ITmfPropertiesProvider analysisProperties
= (ITmfPropertiesProvider
) fAnalysisHelper
;
316 return analysisProperties
.getProperties();
318 return Collections
.EMPTY_MAP
;
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
);
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
);
340 return propertyDescriptorArray
.toArray(new IPropertyDescriptor
[analysisProperties
.size() + helperProperties
.size()]);
342 return new IPropertyDescriptor
[0];
349 public Object
getPropertyValue(Object id
) {
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
);
362 properties
= getAnalysisProperties();
363 if (properties
.containsKey(key
)) {
364 String value
= properties
.get(key
);
375 public final void resetPropertyValue(Object id
) {
382 public final void setPropertyValue(Object id
, Object value
) {
389 public final boolean isPropertyResettable(Object id
) {
397 public final boolean isPropertySet(Object id
) {