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
.HashMap
;
18 import java
.util
.List
;
21 import org
.eclipse
.core
.resources
.IFolder
;
22 import org
.eclipse
.core
.resources
.IResource
;
23 import org
.eclipse
.core
.resources
.ResourcesPlugin
;
24 import org
.eclipse
.core
.runtime
.IPath
;
25 import org
.eclipse
.jdt
.annotation
.NonNull
;
26 import org
.eclipse
.jface
.viewers
.StyledString
.Styler
;
27 import org
.eclipse
.swt
.graphics
.TextStyle
;
28 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.IAnalysisModule
;
29 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.IAnalysisModuleHelper
;
30 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.IAnalysisOutput
;
31 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
32 import org
.osgi
.framework
.Bundle
;
35 * Class for project elements of type analysis modules
37 * @author Geneviève Bastien
39 public class TmfAnalysisElement
extends TmfProjectModelElement
implements ITmfStyledProjectModelElement
{
41 private static final Styler ANALYSIS_CANT_EXECUTE_STYLER
= new Styler() {
43 public void applyStyles(TextStyle textStyle
) {
44 textStyle
.strikeout
= true;
48 private final @NonNull IAnalysisModuleHelper fAnalysisHelper
;
49 private boolean fCanExecute
= true;
55 * Name of the analysis
59 * Parent of the analysis
61 * The analysis module helper
63 protected TmfAnalysisElement(String name
, IResource resource
, ITmfProjectModelElement parent
, @NonNull IAnalysisModuleHelper module
) {
64 super(name
, resource
, parent
);
65 fAnalysisHelper
= module
;
66 parent
.addChild(this);
69 // ------------------------------------------------------------------------
70 // TmfProjectModelElement
71 // ------------------------------------------------------------------------
74 void refreshChildren() {
77 /* Refresh the outputs of this analysis */
78 Map
<String
, TmfAnalysisOutputElement
> childrenMap
= new HashMap
<>();
79 for (TmfAnalysisOutputElement output
: getAvailableOutputs()) {
80 childrenMap
.put(output
.getName(), output
);
83 /** Get base path for resource */
84 IPath path
= getProject().getTracesFolder().getPath();
85 if (fResource
instanceof IFolder
) {
86 path
= ((IFolder
) fResource
).getFullPath();
90 * We can get a list of available outputs once the analysis is
91 * instantiated when the trace is opened
93 ITmfProjectModelElement parent
= getParent();
94 if (parent
instanceof TmfCommonProjectElement
) {
95 ITmfTrace trace
= ((TmfCommonProjectElement
) parent
).getTrace();
101 IAnalysisModule module
= trace
.getAnalysisModule(fAnalysisHelper
.getId());
102 if (module
== null) {
105 * Trace is opened, but the analysis is null, so it does not
112 for (IAnalysisOutput output
: module
.getOutputs()) {
113 TmfAnalysisOutputElement outputElement
= childrenMap
.remove(output
.getName());
114 if (outputElement
== null) {
115 IFolder newresource
= ResourcesPlugin
.getWorkspace().getRoot().getFolder(path
.append(output
.getName()));
116 outputElement
= new TmfAnalysisOutputElement(output
.getName(), newresource
, this, output
);
118 outputElement
.refreshChildren();
122 /* Remove outputs that are not children of this analysis anymore */
123 for (TmfAnalysisOutputElement output
: childrenMap
.values()) {
128 // ------------------------------------------------------------------------
129 // TmfProjectModelElement
130 // ------------------------------------------------------------------------
133 public Styler
getStyler() {
135 return ANALYSIS_CANT_EXECUTE_STYLER
;
140 // ------------------------------------------------------------------------
142 // ------------------------------------------------------------------------
145 * Get the list of analysis output model elements under this analysis
147 * @return Array of analysis output elements
149 public List
<TmfAnalysisOutputElement
> getAvailableOutputs() {
150 List
<ITmfProjectModelElement
> children
= getChildren();
151 List
<TmfAnalysisOutputElement
> outputs
= new ArrayList
<>();
152 for (ITmfProjectModelElement child
: children
) {
153 if (child
instanceof TmfAnalysisOutputElement
) {
154 outputs
.add((TmfAnalysisOutputElement
) child
);
161 * Gets the analysis id of this module
163 * @return The analysis id
165 public String
getAnalysisId() {
166 return fAnalysisHelper
.getId();
170 * Gets the help message for this analysis
172 * @return The help message
174 public String
getHelpMessage() {
175 ITmfProjectModelElement parent
= getParent();
177 ITmfTrace trace
= null;
178 if (parent
instanceof TmfTraceElement
) {
179 TmfTraceElement traceElement
= (TmfTraceElement
) parent
;
180 trace
= traceElement
.getTrace();
182 IAnalysisModule module
= trace
.getAnalysisModule(fAnalysisHelper
.getId());
183 if (module
!= null) {
184 return module
.getHelpText(trace
);
190 return fAnalysisHelper
.getHelpText(trace
);
193 return fAnalysisHelper
.getHelpText();
197 * Gets the icon file name for the analysis
199 * @return The analysis icon file name
201 public String
getIconFile() {
202 return fAnalysisHelper
.getIcon();
206 * Gets the bundle this analysis is from
208 * @return The analysis bundle
210 public Bundle
getBundle() {
211 return fAnalysisHelper
.getBundle();
214 /** Delete all outputs under this analysis element */
215 private void deleteOutputs() {
216 for (TmfAnalysisOutputElement output
: getAvailableOutputs()) {
222 * Make sure the trace this analysis is associated to is the currently
225 public void activateParent() {
226 ITmfProjectModelElement parent
= getParent();
228 if (parent
instanceof TmfTraceElement
) {
229 TmfTraceElement traceElement
= (TmfTraceElement
) parent
;
230 TmfOpenTraceHelper
.openTraceFromElement(traceElement
);