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 volatile 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 final TmfTraceFolder tracesFolder
= getProject().getTracesFolder();
108 if (tracesFolder
== null) {
111 IPath path
= tracesFolder
.getPath();
112 IResource resource
= getResource();
113 if (resource
instanceof IFolder
) {
114 path
= ((IFolder
) resource
).getFullPath();
118 * We can get a list of available outputs once the analysis is
119 * instantiated when the trace is opened
121 TmfCommonProjectElement parentTraceElement
= getParent().getParent();
123 ITmfTrace trace
= parentTraceElement
.getTrace();
129 IAnalysisModule module
= trace
.getAnalysisModule(fAnalysisHelper
.getId());
130 if (module
== null) {
133 * Trace is opened, but the analysis is null, so it does not
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
);
147 outputElement
.refreshChildren();
150 /* Remove outputs that are not children of this analysis anymore */
151 for (TmfAnalysisOutputElement output
: childrenMap
.values()) {
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
);
171 return TmfProjectModelIcons
.DEFAULT_ANALYSIS_ICON
;
174 // ------------------------------------------------------------------------
175 // TmfProjectModelElement
176 // ------------------------------------------------------------------------
179 public Styler
getStyler() {
181 return ANALYSIS_CANT_EXECUTE_STYLER
;
186 // ------------------------------------------------------------------------
188 // ------------------------------------------------------------------------
191 * Get the list of analysis output model elements under this analysis
193 * @return Array of analysis output elements
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
);
207 * Gets the analysis id of this module
209 * @return The analysis id
211 public String
getAnalysisId() {
212 return fAnalysisHelper
.getId();
216 * Gets the help message for this analysis
218 * @return The help message
220 public String
getHelpMessage() {
221 TmfCommonProjectElement parentTrace
= getParent().getParent();
223 ITmfTrace trace
= null;
224 if (parentTrace
instanceof TmfTraceElement
) {
225 TmfTraceElement traceElement
= (TmfTraceElement
) parentTrace
;
226 trace
= traceElement
.getTrace();
228 IAnalysisModule module
= trace
.getAnalysisModule(fAnalysisHelper
.getId());
229 if (module
!= null) {
230 return module
.getHelpText(trace
);
236 return fAnalysisHelper
.getHelpText(trace
);
239 return fAnalysisHelper
.getHelpText();
243 * Gets the icon file name for the analysis
245 * @return The analysis icon file name
247 public String
getIconFile() {
248 return fAnalysisHelper
.getIcon();
252 * Gets the bundle this analysis is from
254 * @return The analysis bundle
256 public Bundle
getBundle() {
257 return fAnalysisHelper
.getBundle();
260 /** Delete all outputs under this analysis element */
261 private void deleteOutputs() {
262 for (TmfAnalysisOutputElement output
: getAvailableOutputs()) {
268 * Make sure the trace this analysis is associated to is the currently
272 public void activateParentTrace() {
273 TmfCommonProjectElement parentTrace
= getParent().getParent();
275 if (parentTrace
instanceof TmfTraceElement
) {
276 TmfTraceElement traceElement
= (TmfTraceElement
) parentTrace
;
277 TmfOpenTraceHelper
.openTraceFromElement(traceElement
);
282 * Checks whether the analysis can be executed or not.
284 * @return <code>true</code> if analysis can be executed else
288 public boolean canExecute() {
293 * Gets the analysis helper for this analysis.
295 * @return the analysis module helper
298 @NonNull protected IAnalysisModuleHelper
getAnalysisHelper() {
299 return fAnalysisHelper
;
302 // ------------------------------------------------------------------------
304 // ------------------------------------------------------------------------
310 public Object
getEditableValue() {
315 * Get the analysis properties of this analysisElement if the corresponding
316 * analysis exists for the current trace
318 * @return a map with the names and values of the trace properties
319 * respectively as keys and values
321 private Map
<String
, String
> getAnalysisProperties() {
322 ITmfProjectModelElement parent
= getParent();
323 if (!(parent
instanceof TmfViewsElement
)) {
324 return Collections
.EMPTY_MAP
;
326 parent
= parent
.getParent();
327 if (parent
instanceof TmfCommonProjectElement
) {
328 ITmfTrace trace
= ((TmfCommonProjectElement
) parent
).getTrace();
330 return Collections
.EMPTY_MAP
;
332 IAnalysisModule module
= trace
.getAnalysisModule(fAnalysisHelper
.getId());
333 if (module
instanceof ITmfPropertiesProvider
) {
334 return ((ITmfPropertiesProvider
) module
).getProperties();
338 return Collections
.EMPTY_MAP
;
341 private Map
<String
, String
> getAnalysisHelperProperties() {
342 if (fAnalysisHelper
instanceof ITmfPropertiesProvider
) {
343 ITmfPropertiesProvider analysisProperties
= (ITmfPropertiesProvider
) fAnalysisHelper
;
344 return analysisProperties
.getProperties();
346 return Collections
.EMPTY_MAP
;
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
);
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
);
368 return propertyDescriptorArray
.toArray(new IPropertyDescriptor
[analysisProperties
.size() + helperProperties
.size()]);
370 return new IPropertyDescriptor
[0];
377 public Object
getPropertyValue(Object id
) {
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
);
390 properties
= getAnalysisProperties();
391 if (properties
.containsKey(key
)) {
392 String value
= properties
.get(key
);
403 public final void resetPropertyValue(Object id
) {
410 public final void setPropertyValue(Object id
, Object value
) {
417 public final boolean isPropertyResettable(Object id
) {
425 public final boolean isPropertySet(Object id
) {