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
.TextStyle
;
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
;
32 import org
.eclipse
.tracecompass
.tmf
.core
.project
.model
.ITmfPropertiesProvider
;
33 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
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
;
37 import org
.osgi
.framework
.Bundle
;
40 * Class for project elements of type analysis modules
42 * @author Geneviève Bastien
45 public class TmfAnalysisElement
extends TmfProjectModelElement
implements ITmfStyledProjectModelElement
, IPropertySource2
{
47 private static final Styler ANALYSIS_CANT_EXECUTE_STYLER
= new Styler() {
49 public void applyStyles(TextStyle textStyle
) {
50 textStyle
.strikeout
= true;
54 private final @NonNull IAnalysisModuleHelper fAnalysisHelper
;
55 private boolean fCanExecute
= true;
57 private static final String ANALYSIS_PROPERTIES_CATEGORY
= Messages
.TmfAnalysisElement_AnalysisProperties
;
58 private static final String HELPER_PROPERTIES_CATEGORY
= Messages
.TmfAnalysisElement_HelperProperties
;
64 * Name of the analysis
68 * Parent of the analysis
70 * The analysis module helper
73 protected TmfAnalysisElement(String name
, IResource resource
, ITmfProjectModelElement parent
, @NonNull IAnalysisModuleHelper module
) {
74 super(name
, resource
, parent
);
75 fAnalysisHelper
= module
;
78 // ------------------------------------------------------------------------
79 // TmfProjectModelElement
80 // ------------------------------------------------------------------------
86 protected void refreshChildren() {
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
);
95 /** Get base path for resource */
96 IPath path
= getProject().getTracesFolder().getPath();
97 IResource resource
= getResource();
98 if (resource
instanceof IFolder
) {
99 path
= ((IFolder
) resource
).getFullPath();
103 * We can get a list of available outputs once the analysis is
104 * instantiated when the trace is opened
106 ITmfProjectModelElement parent
= getParent();
107 if (parent
instanceof TmfCommonProjectElement
) {
108 ITmfTrace trace
= ((TmfCommonProjectElement
) parent
).getTrace();
114 IAnalysisModule module
= trace
.getAnalysisModule(fAnalysisHelper
.getId());
115 if (module
== null) {
118 * Trace is opened, but the analysis is null, so it does not
125 for (IAnalysisOutput output
: module
.getOutputs()) {
126 TmfAnalysisOutputElement outputElement
= childrenMap
.remove(output
.getName());
127 if (outputElement
== null) {
128 IFolder newresource
= ResourcesPlugin
.getWorkspace().getRoot().getFolder(path
.append(output
.getName()));
129 outputElement
= new TmfAnalysisOutputElement(output
.getName(), newresource
, this, output
);
130 addChild(outputElement
);
132 outputElement
.refreshChildren();
136 /* Remove outputs that are not children of this analysis anymore */
137 for (TmfAnalysisOutputElement output
: childrenMap
.values()) {
142 // ------------------------------------------------------------------------
143 // TmfProjectModelElement
144 // ------------------------------------------------------------------------
147 public Styler
getStyler() {
149 return ANALYSIS_CANT_EXECUTE_STYLER
;
154 // ------------------------------------------------------------------------
156 // ------------------------------------------------------------------------
159 * Get the list of analysis output model elements under this analysis
161 * @return Array of analysis output elements
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
);
175 * Gets the analysis id of this module
177 * @return The analysis id
179 public String
getAnalysisId() {
180 return fAnalysisHelper
.getId();
184 * Gets the help message for this analysis
186 * @return The help message
188 public String
getHelpMessage() {
189 ITmfProjectModelElement parent
= getParent();
191 ITmfTrace trace
= null;
192 if (parent
instanceof TmfTraceElement
) {
193 TmfTraceElement traceElement
= (TmfTraceElement
) parent
;
194 trace
= traceElement
.getTrace();
196 IAnalysisModule module
= trace
.getAnalysisModule(fAnalysisHelper
.getId());
197 if (module
!= null) {
198 return module
.getHelpText(trace
);
204 return fAnalysisHelper
.getHelpText(trace
);
207 return fAnalysisHelper
.getHelpText();
211 * Gets the icon file name for the analysis
213 * @return The analysis icon file name
215 public String
getIconFile() {
216 return fAnalysisHelper
.getIcon();
220 * Gets the bundle this analysis is from
222 * @return The analysis bundle
224 public Bundle
getBundle() {
225 return fAnalysisHelper
.getBundle();
228 /** Delete all outputs under this analysis element */
229 private void deleteOutputs() {
230 for (TmfAnalysisOutputElement output
: getAvailableOutputs()) {
236 * Make sure the trace this analysis is associated to is the currently
239 public void activateParent() {
240 ITmfProjectModelElement parent
= getParent();
242 if (parent
instanceof TmfTraceElement
) {
243 TmfTraceElement traceElement
= (TmfTraceElement
) parent
;
244 TmfOpenTraceHelper
.openTraceFromElement(traceElement
);
248 // ------------------------------------------------------------------------
250 // ------------------------------------------------------------------------
256 public Object
getEditableValue() {
261 * Get the analysis properties of this analysisElement if the corresponding
262 * analysis exists for the current trace
264 * @return a map with the names and values of the trace properties
265 * respectively as keys and values
267 private Map
<String
, String
> getAnalysisProperties() {
268 ITmfProjectModelElement parent
= getParent();
270 if (parent
instanceof TmfCommonProjectElement
) {
271 ITmfTrace trace
= ((TmfCommonProjectElement
) parent
).getTrace();
273 return Collections
.EMPTY_MAP
;
275 IAnalysisModule module
= trace
.getAnalysisModule(fAnalysisHelper
.getId());
276 if (module
instanceof ITmfPropertiesProvider
) {
277 return ((ITmfPropertiesProvider
) module
).getProperties();
281 return Collections
.EMPTY_MAP
;
284 private Map
<String
, String
> getAnalysisHelperProperties() {
285 if (fAnalysisHelper
instanceof ITmfPropertiesProvider
) {
286 ITmfPropertiesProvider analysisProperties
= (ITmfPropertiesProvider
) fAnalysisHelper
;
287 return analysisProperties
.getProperties();
289 return Collections
.EMPTY_MAP
;
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
);
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
);
311 return propertyDescriptorArray
.toArray(new IPropertyDescriptor
[analysisProperties
.size() + helperProperties
.size()]);
313 return new IPropertyDescriptor
[0];
320 public Object
getPropertyValue(Object id
) {
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
);
333 properties
= getAnalysisProperties();
334 if (properties
.containsKey(key
)) {
335 String value
= properties
.get(key
);
346 public final void resetPropertyValue(Object id
) {
353 public final void setPropertyValue(Object id
, Object value
) {
360 public final boolean isPropertyResettable(Object id
) {
368 public final boolean isPropertySet(Object id
) {