tmf: Move TmfExperiment to its own package
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / project / model / TmfAnalysisElement.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 École Polytechnique de Montréal
3 *
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
8 *
9 * Contributors:
10 * Geneviève Bastien - Initial API and implementation
11 * Patrick Tasse - Add support for folder elements
12 *******************************************************************************/
13
14 package org.eclipse.tracecompass.tmf.ui.project.model;
15
16 import java.util.ArrayList;
17 import java.util.HashMap;
18 import java.util.List;
19 import java.util.Map;
20
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.swt.graphics.TextStyle;
27 import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule;
28 import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModuleHelper;
29 import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisOutput;
30 import org.eclipse.tracecompass.tmf.core.analysis.TmfAnalysisManager;
31 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
32 import org.osgi.framework.Bundle;
33
34 /**
35 * Class for project elements of type analysis modules
36 *
37 * @author Geneviève Bastien
38 * @since 3.0
39 */
40 public class TmfAnalysisElement extends TmfProjectModelElement implements ITmfStyledProjectModelElement {
41
42 private static final Styler ANALYSIS_CANT_EXECUTE_STYLER = new Styler() {
43 @Override
44 public void applyStyles(TextStyle textStyle) {
45 textStyle.strikeout = true;
46 }
47 };
48
49 private final String fAnalysisId;
50 private boolean fCanExecute = true;
51
52 /**
53 * Constructor
54 *
55 * @param name
56 * Name of the analysis
57 * @param resource
58 * The resource
59 * @param parent
60 * Parent of the analysis
61 * @param id
62 * The analysis module id
63 */
64 protected TmfAnalysisElement(String name, IResource resource, ITmfProjectModelElement parent, String id) {
65 super(name, resource, parent);
66 fAnalysisId = id;
67 parent.addChild(this);
68 }
69
70 // ------------------------------------------------------------------------
71 // TmfProjectModelElement
72 // ------------------------------------------------------------------------
73
74 @Override
75 void refreshChildren() {
76 fCanExecute = true;
77
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);
82 }
83
84 IAnalysisModuleHelper helper = TmfAnalysisManager.getAnalysisModule(fAnalysisId);
85 if (helper == null) {
86 deleteOutputs();
87 return;
88 }
89
90 /** Get base path for resource */
91 IPath path = getProject().getTracesFolder().getPath();
92 if (fResource instanceof IFolder) {
93 path = ((IFolder) fResource).getFullPath();
94 }
95
96 /*
97 * We can get a list of available outputs once the analysis is
98 * instantiated when the trace is opened
99 */
100 ITmfProjectModelElement parent = getParent();
101 if (parent instanceof TmfCommonProjectElement) {
102 ITmfTrace trace = ((TmfCommonProjectElement) parent).getTrace();
103 if (trace == null) {
104 deleteOutputs();
105 return;
106 }
107
108 IAnalysisModule module = trace.getAnalysisModule(fAnalysisId);
109 if (module == null) {
110 deleteOutputs();
111 /*
112 * Trace is opened, but the analysis is null, so it does not
113 * apply
114 */
115 fCanExecute = false;
116 return;
117 }
118
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);
124 }
125 outputElement.refreshChildren();
126 }
127
128 }
129 /* Remove outputs that are not children of this analysis anymore */
130 for (TmfAnalysisOutputElement output : childrenMap.values()) {
131 removeChild(output);
132 }
133 }
134
135 // ------------------------------------------------------------------------
136 // TmfProjectModelElement
137 // ------------------------------------------------------------------------
138
139 @Override
140 public Styler getStyler() {
141 if (!fCanExecute) {
142 return ANALYSIS_CANT_EXECUTE_STYLER;
143 }
144 return null;
145 }
146
147 // ------------------------------------------------------------------------
148 // Operations
149 // ------------------------------------------------------------------------
150
151 /**
152 * Get the list of analysis output model elements under this analysis
153 *
154 * @return Array of analysis output elements
155 */
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);
162 }
163 }
164 return outputs;
165 }
166
167 /**
168 * Gets the analysis id of this module
169 *
170 * @return The analysis id
171 */
172 public String getAnalysisId() {
173 return fAnalysisId;
174 }
175
176 /**
177 * Gets the help message for this analysis
178 *
179 * @return The help message
180 */
181 public String getHelpMessage() {
182 ITmfProjectModelElement parent = getParent();
183
184 ITmfTrace trace = null;
185 if (parent instanceof TmfTraceElement) {
186 TmfTraceElement traceElement = (TmfTraceElement) parent;
187 trace = traceElement.getTrace();
188 if (trace != null) {
189 IAnalysisModule module = trace.getAnalysisModule(fAnalysisId);
190 if (module != null) {
191 return module.getHelpText(trace);
192 }
193 }
194 }
195
196 IAnalysisModuleHelper helper = TmfAnalysisManager.getAnalysisModule(fAnalysisId);
197 if (helper == null) {
198 return new String();
199 }
200
201 if (trace != null) {
202 return helper.getHelpText(trace);
203 }
204
205 return helper.getHelpText();
206 }
207
208 /**
209 * Gets the icon file name for the analysis
210 *
211 * @return The analysis icon file name
212 */
213 public String getIconFile() {
214 IAnalysisModuleHelper helper = TmfAnalysisManager.getAnalysisModule(fAnalysisId);
215 if (helper == null) {
216 return null;
217 }
218 return helper.getIcon();
219 }
220
221 /**
222 * Gets the bundle this analysis is from
223 *
224 * @return The analysis bundle
225 */
226 public Bundle getBundle() {
227 IAnalysisModuleHelper helper = TmfAnalysisManager.getAnalysisModule(fAnalysisId);
228 if (helper == null) {
229 return null;
230 }
231 return helper.getBundle();
232 }
233
234 /** Delete all outputs under this analysis element */
235 private void deleteOutputs() {
236 for (TmfAnalysisOutputElement output : getAvailableOutputs()) {
237 removeChild(output);
238 }
239 }
240
241 /**
242 * Make sure the trace this analysis is associated to is the currently
243 * selected one
244 */
245 public void activateParent() {
246 ITmfProjectModelElement parent = getParent();
247
248 if (parent instanceof TmfTraceElement) {
249 TmfTraceElement traceElement = (TmfTraceElement) parent;
250 TmfOpenTraceHelper.openTraceFromElement(traceElement);
251 }
252 }
253
254 }
This page took 0.037071 seconds and 5 git commands to generate.