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
, ITmfProjectModelElement parent
, @NonNull IAnalysisModuleHelper module
) {
75 super(name
, resource
, parent
);
76 fAnalysisHelper
= module
;
79 // ------------------------------------------------------------------------
80 // TmfProjectModelElement
81 // ------------------------------------------------------------------------
87 protected void refreshChildren() {
90 /* Refresh the outputs of this analysis */
91 Map
<String
, TmfAnalysisOutputElement
> childrenMap
= new HashMap
<>();
92 for (TmfAnalysisOutputElement output
: getAvailableOutputs()) {
93 childrenMap
.put(output
.getName(), output
);
96 /** Get base path for resource */
97 IPath path
= getProject().getTracesFolder().getPath();
98 IResource resource
= getResource();
99 if (resource
instanceof IFolder
) {
100 path
= ((IFolder
) resource
).getFullPath();
104 * We can get a list of available outputs once the analysis is
105 * instantiated when the trace is opened
107 ITmfProjectModelElement parent
= getParent();
108 if (parent
instanceof TmfCommonProjectElement
) {
109 ITmfTrace trace
= ((TmfCommonProjectElement
) parent
).getTrace();
115 IAnalysisModule module
= trace
.getAnalysisModule(fAnalysisHelper
.getId());
116 if (module
== null) {
119 * Trace is opened, but the analysis is null, so it does not
126 for (IAnalysisOutput output
: module
.getOutputs()) {
127 TmfAnalysisOutputElement outputElement
= childrenMap
.remove(output
.getName());
128 if (outputElement
== null) {
129 IFolder newresource
= ResourcesPlugin
.getWorkspace().getRoot().getFolder(path
.append(output
.getName()));
130 outputElement
= new TmfAnalysisOutputElement(output
.getName(), newresource
, this, output
);
131 addChild(outputElement
);
133 outputElement
.refreshChildren();
137 /* Remove outputs that are not children of this analysis anymore */
138 for (TmfAnalysisOutputElement output
: childrenMap
.values()) {
147 public Image
getIcon() {
148 String iconFile
= getIconFile();
149 if (iconFile
!= null) {
150 Bundle bundle
= getBundle();
151 if (bundle
!= null) {
152 Image icon
= TmfProjectModelIcons
.loadIcon(bundle
, iconFile
);
158 return TmfProjectModelIcons
.DEFAULT_ANALYSIS_ICON
;
161 // ------------------------------------------------------------------------
162 // TmfProjectModelElement
163 // ------------------------------------------------------------------------
166 public Styler
getStyler() {
168 return ANALYSIS_CANT_EXECUTE_STYLER
;
173 // ------------------------------------------------------------------------
175 // ------------------------------------------------------------------------
178 * Get the list of analysis output model elements under this analysis
180 * @return Array of analysis output elements
182 public List
<TmfAnalysisOutputElement
> getAvailableOutputs() {
183 List
<ITmfProjectModelElement
> children
= getChildren();
184 List
<TmfAnalysisOutputElement
> outputs
= new ArrayList
<>();
185 for (ITmfProjectModelElement child
: children
) {
186 if (child
instanceof TmfAnalysisOutputElement
) {
187 outputs
.add((TmfAnalysisOutputElement
) child
);
194 * Gets the analysis id of this module
196 * @return The analysis id
198 public String
getAnalysisId() {
199 return fAnalysisHelper
.getId();
203 * Gets the help message for this analysis
205 * @return The help message
207 public String
getHelpMessage() {
208 ITmfProjectModelElement parent
= getParent();
210 ITmfTrace trace
= null;
211 if (parent
instanceof TmfTraceElement
) {
212 TmfTraceElement traceElement
= (TmfTraceElement
) parent
;
213 trace
= traceElement
.getTrace();
215 IAnalysisModule module
= trace
.getAnalysisModule(fAnalysisHelper
.getId());
216 if (module
!= null) {
217 return module
.getHelpText(trace
);
223 return fAnalysisHelper
.getHelpText(trace
);
226 return fAnalysisHelper
.getHelpText();
230 * Gets the icon file name for the analysis
232 * @return The analysis icon file name
234 public String
getIconFile() {
235 return fAnalysisHelper
.getIcon();
239 * Gets the bundle this analysis is from
241 * @return The analysis bundle
243 public Bundle
getBundle() {
244 return fAnalysisHelper
.getBundle();
247 /** Delete all outputs under this analysis element */
248 private void deleteOutputs() {
249 for (TmfAnalysisOutputElement output
: getAvailableOutputs()) {
255 * Make sure the trace this analysis is associated to is the currently
258 public void activateParent() {
259 ITmfProjectModelElement parent
= getParent();
261 if (parent
instanceof TmfTraceElement
) {
262 TmfTraceElement traceElement
= (TmfTraceElement
) parent
;
263 TmfOpenTraceHelper
.openTraceFromElement(traceElement
);
267 // ------------------------------------------------------------------------
269 // ------------------------------------------------------------------------
275 public Object
getEditableValue() {
280 * Get the analysis properties of this analysisElement if the corresponding
281 * analysis exists for the current trace
283 * @return a map with the names and values of the trace properties
284 * respectively as keys and values
286 private Map
<String
, String
> getAnalysisProperties() {
287 ITmfProjectModelElement parent
= getParent();
289 if (parent
instanceof TmfCommonProjectElement
) {
290 ITmfTrace trace
= ((TmfCommonProjectElement
) parent
).getTrace();
292 return Collections
.EMPTY_MAP
;
294 IAnalysisModule module
= trace
.getAnalysisModule(fAnalysisHelper
.getId());
295 if (module
instanceof ITmfPropertiesProvider
) {
296 return ((ITmfPropertiesProvider
) module
).getProperties();
300 return Collections
.EMPTY_MAP
;
303 private Map
<String
, String
> getAnalysisHelperProperties() {
304 if (fAnalysisHelper
instanceof ITmfPropertiesProvider
) {
305 ITmfPropertiesProvider analysisProperties
= (ITmfPropertiesProvider
) fAnalysisHelper
;
306 return analysisProperties
.getProperties();
308 return Collections
.EMPTY_MAP
;
315 public IPropertyDescriptor
[] getPropertyDescriptors() {
316 Map
<String
, String
> helperProperties
= getAnalysisHelperProperties();
317 Map
<String
, String
> analysisProperties
= getAnalysisProperties();
318 if (!analysisProperties
.isEmpty() || !helperProperties
.isEmpty()) {
319 List
<IPropertyDescriptor
> propertyDescriptorArray
= new ArrayList
<>(analysisProperties
.size() + helperProperties
.size());
320 for (Map
.Entry
<String
, String
> varName
: helperProperties
.entrySet()) {
321 ReadOnlyTextPropertyDescriptor descriptor
= new ReadOnlyTextPropertyDescriptor(this.getName() + '_' + varName
.getKey(), varName
.getKey());
322 descriptor
.setCategory(HELPER_PROPERTIES_CATEGORY
);
323 propertyDescriptorArray
.add(descriptor
);
325 for (Map
.Entry
<String
, String
> varName
: analysisProperties
.entrySet()) {
326 ReadOnlyTextPropertyDescriptor descriptor
= new ReadOnlyTextPropertyDescriptor(this.getName() + '_' + varName
.getKey(), varName
.getKey());
327 descriptor
.setCategory(ANALYSIS_PROPERTIES_CATEGORY
);
328 propertyDescriptorArray
.add(descriptor
);
330 return propertyDescriptorArray
.toArray(new IPropertyDescriptor
[analysisProperties
.size() + helperProperties
.size()]);
332 return new IPropertyDescriptor
[0];
339 public Object
getPropertyValue(Object id
) {
343 Map
<String
, String
> properties
= getAnalysisHelperProperties();
344 String key
= (String
) id
;
345 /* Remove name from key */
346 key
= key
.substring(this.getName().length() + 1);
347 if (properties
.containsKey(key
)) {
348 String value
= properties
.get(key
);
352 properties
= getAnalysisProperties();
353 if (properties
.containsKey(key
)) {
354 String value
= properties
.get(key
);
365 public final void resetPropertyValue(Object id
) {
372 public final void setPropertyValue(Object id
, Object value
) {
379 public final boolean isPropertyResettable(Object id
) {
387 public final boolean isPropertySet(Object id
) {