TMF: Strike-out analyses that cannot be executed
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / 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.linuxtools.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.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;
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 return helper.getHelpText();
202 }
203
204 /**
205 * Gets the icon file name for the analysis
206 *
207 * @return The analysis icon file name
208 */
209 public String getIconFile() {
210 IAnalysisModuleHelper helper = TmfAnalysisManager.getAnalysisModule(fAnalysisId);
211 if (helper == null) {
212 return null;
213 }
214 return helper.getIcon();
215 }
216
217 /**
218 * Gets the bundle this analysis is from
219 *
220 * @return The analysis bundle
221 */
222 public Bundle getBundle() {
223 IAnalysisModuleHelper helper = TmfAnalysisManager.getAnalysisModule(fAnalysisId);
224 if (helper == null) {
225 return null;
226 }
227 return helper.getBundle();
228 }
229
230 /** Delete all outputs under this analysis element */
231 private void deleteOutputs() {
232 for (TmfAnalysisOutputElement output : getAvailableOutputs()) {
233 removeChild(output);
234 }
235 }
236
237 /**
238 * Make sure the trace this analysis is associated to is the currently
239 * selected one
240 */
241 public void activateParent() {
242 ITmfProjectModelElement parent = getParent();
243
244 if (parent instanceof TmfTraceElement) {
245 TmfTraceElement traceElement = (TmfTraceElement) parent;
246 TmfOpenTraceHelper.openTraceFromElement(traceElement);
247 }
248 }
249
250 }
This page took 0.037365 seconds and 6 git commands to generate.