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
.linuxtools
.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
.jface
.viewers
.StyledString
.Styler
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.analysis
.IAnalysisModule
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.analysis
.IAnalysisModuleHelper
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.analysis
.IAnalysisOutput
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.analysis
.TmfAnalysisManager
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
31 import org
.eclipse
.swt
.graphics
.TextStyle
;
32 import org
.osgi
.framework
.Bundle
;
35 * Class for project elements of type analysis modules
37 * @author Geneviève Bastien
40 public class TmfAnalysisElement
extends TmfProjectModelElement
implements ITmfStyledProjectModelElement
{
42 private static final Styler ANALYSIS_CANT_EXECUTE_STYLER
= new Styler() {
44 public void applyStyles(TextStyle textStyle
) {
45 textStyle
.strikeout
= true;
49 private final String fAnalysisId
;
50 private boolean fCanExecute
= true;
56 * Name of the analysis
60 * Parent of the analysis
62 * The analysis module id
64 protected TmfAnalysisElement(String name
, IResource resource
, ITmfProjectModelElement parent
, String id
) {
65 super(name
, resource
, parent
);
67 parent
.addChild(this);
70 // ------------------------------------------------------------------------
71 // TmfProjectModelElement
72 // ------------------------------------------------------------------------
75 void refreshChildren() {
78 /* Refresh the outputs of this analysis */
79 Map
<String
, TmfAnalysisOutputElement
> childrenMap
= new HashMap
<>();
80 for (TmfAnalysisOutputElement output
: getAvailableOutputs()) {
81 childrenMap
.put(output
.getName(), output
);
84 IAnalysisModuleHelper helper
= TmfAnalysisManager
.getAnalysisModule(fAnalysisId
);
90 /** Get base path for resource */
91 IPath path
= getProject().getTracesFolder().getPath();
92 if (fResource
instanceof IFolder
) {
93 path
= ((IFolder
) fResource
).getFullPath();
97 * We can get a list of available outputs once the analysis is
98 * instantiated when the trace is opened
100 ITmfProjectModelElement parent
= getParent();
101 if (parent
instanceof TmfCommonProjectElement
) {
102 ITmfTrace trace
= ((TmfCommonProjectElement
) parent
).getTrace();
108 IAnalysisModule module
= trace
.getAnalysisModule(fAnalysisId
);
109 if (module
== null) {
112 * Trace is opened, but the analysis is null, so it does not
119 for (IAnalysisOutput output
: module
.getOutputs()) {
120 TmfAnalysisOutputElement outputElement
= childrenMap
.remove(output
.getName());
121 if (outputElement
== null) {
122 IFolder newresource
= ResourcesPlugin
.getWorkspace().getRoot().getFolder(path
.append(output
.getName()));
123 outputElement
= new TmfAnalysisOutputElement(output
.getName(), newresource
, this, output
);
125 outputElement
.refreshChildren();
129 /* Remove outputs that are not children of this analysis anymore */
130 for (TmfAnalysisOutputElement output
: childrenMap
.values()) {
135 // ------------------------------------------------------------------------
136 // TmfProjectModelElement
137 // ------------------------------------------------------------------------
140 public Styler
getStyler() {
142 return ANALYSIS_CANT_EXECUTE_STYLER
;
147 // ------------------------------------------------------------------------
149 // ------------------------------------------------------------------------
152 * Get the list of analysis output model elements under this analysis
154 * @return Array of analysis output elements
156 public List
<TmfAnalysisOutputElement
> getAvailableOutputs() {
157 List
<ITmfProjectModelElement
> children
= getChildren();
158 List
<TmfAnalysisOutputElement
> outputs
= new ArrayList
<>();
159 for (ITmfProjectModelElement child
: children
) {
160 if (child
instanceof TmfAnalysisOutputElement
) {
161 outputs
.add((TmfAnalysisOutputElement
) child
);
168 * Gets the analysis id of this module
170 * @return The analysis id
172 public String
getAnalysisId() {
177 * Gets the help message for this analysis
179 * @return The help message
181 public String
getHelpMessage() {
182 ITmfProjectModelElement parent
= getParent();
184 ITmfTrace trace
= null;
185 if (parent
instanceof TmfTraceElement
) {
186 TmfTraceElement traceElement
= (TmfTraceElement
) parent
;
187 trace
= traceElement
.getTrace();
189 IAnalysisModule module
= trace
.getAnalysisModule(fAnalysisId
);
190 if (module
!= null) {
191 return module
.getHelpText(trace
);
196 IAnalysisModuleHelper helper
= TmfAnalysisManager
.getAnalysisModule(fAnalysisId
);
197 if (helper
== null) {
201 return helper
.getHelpText();
205 * Gets the icon file name for the analysis
207 * @return The analysis icon file name
209 public String
getIconFile() {
210 IAnalysisModuleHelper helper
= TmfAnalysisManager
.getAnalysisModule(fAnalysisId
);
211 if (helper
== null) {
214 return helper
.getIcon();
218 * Gets the bundle this analysis is from
220 * @return The analysis bundle
222 public Bundle
getBundle() {
223 IAnalysisModuleHelper helper
= TmfAnalysisManager
.getAnalysisModule(fAnalysisId
);
224 if (helper
== null) {
227 return helper
.getBundle();
230 /** Delete all outputs under this analysis element */
231 private void deleteOutputs() {
232 for (TmfAnalysisOutputElement output
: getAvailableOutputs()) {
238 * Make sure the trace this analysis is associated to is the currently
241 public void activateParent() {
242 ITmfProjectModelElement parent
= getParent();
244 if (parent
instanceof TmfTraceElement
) {
245 TmfTraceElement traceElement
= (TmfTraceElement
) parent
;
246 TmfOpenTraceHelper
.openTraceFromElement(traceElement
);