23af28ac52df780bd0e3ed99dd276530619d64e7
[deliverable/tracecompass.git] / tmf / 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.Collections;
18 import java.util.HashMap;
19 import java.util.List;
20 import java.util.Map;
21
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.TextStyle;
29 import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule;
30 import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModuleHelper;
31 import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisOutput;
32 import org.eclipse.tracecompass.tmf.core.project.model.ITmfPropertiesProvider;
33 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
34 import org.eclipse.tracecompass.tmf.ui.properties.ReadOnlyTextPropertyDescriptor;
35 import org.eclipse.ui.views.properties.IPropertyDescriptor;
36 import org.eclipse.ui.views.properties.IPropertySource2;
37 import org.osgi.framework.Bundle;
38
39 /**
40 * Class for project elements of type analysis modules
41 *
42 * @author Geneviève Bastien
43 * @since 2.0
44 */
45 public class TmfAnalysisElement extends TmfProjectModelElement implements ITmfStyledProjectModelElement, IPropertySource2 {
46
47 private static final Styler ANALYSIS_CANT_EXECUTE_STYLER = new Styler() {
48 @Override
49 public void applyStyles(TextStyle textStyle) {
50 textStyle.strikeout = true;
51 }
52 };
53
54 private final @NonNull IAnalysisModuleHelper fAnalysisHelper;
55 private boolean fCanExecute = true;
56
57 private static final String ANALYSIS_PROPERTIES_CATEGORY = Messages.TmfAnalysisElement_AnalysisProperties;
58 private static final String HELPER_PROPERTIES_CATEGORY = Messages.TmfAnalysisElement_HelperProperties;
59
60 /**
61 * Constructor
62 *
63 * @param name
64 * Name of the analysis
65 * @param resource
66 * The resource
67 * @param parent
68 * Parent of the analysis
69 * @param module
70 * The analysis module helper
71 * @since 1.0
72 */
73 protected TmfAnalysisElement(String name, IResource resource, ITmfProjectModelElement parent, @NonNull IAnalysisModuleHelper module) {
74 super(name, resource, parent);
75 fAnalysisHelper = module;
76 }
77
78 // ------------------------------------------------------------------------
79 // TmfProjectModelElement
80 // ------------------------------------------------------------------------
81
82 /**
83 * @since 2.0
84 */
85 @Override
86 protected void refreshChildren() {
87 fCanExecute = true;
88
89 /* Refresh the outputs of this analysis */
90 Map<String, TmfAnalysisOutputElement> childrenMap = new HashMap<>();
91 for (TmfAnalysisOutputElement output : getAvailableOutputs()) {
92 childrenMap.put(output.getName(), output);
93 }
94
95 /** Get base path for resource */
96 IPath path = getProject().getTracesFolder().getPath();
97 IResource resource = getResource();
98 if (resource instanceof IFolder) {
99 path = ((IFolder) resource).getFullPath();
100 }
101
102 /*
103 * We can get a list of available outputs once the analysis is
104 * instantiated when the trace is opened
105 */
106 ITmfProjectModelElement parent = getParent();
107 if (parent instanceof TmfCommonProjectElement) {
108 ITmfTrace trace = ((TmfCommonProjectElement) parent).getTrace();
109 if (trace == null) {
110 deleteOutputs();
111 return;
112 }
113
114 IAnalysisModule module = trace.getAnalysisModule(fAnalysisHelper.getId());
115 if (module == null) {
116 deleteOutputs();
117 /*
118 * Trace is opened, but the analysis is null, so it does not
119 * apply
120 */
121 fCanExecute = false;
122 return;
123 }
124
125 for (IAnalysisOutput output : module.getOutputs()) {
126 TmfAnalysisOutputElement outputElement = childrenMap.remove(output.getName());
127 if (outputElement == null) {
128 IFolder newresource = ResourcesPlugin.getWorkspace().getRoot().getFolder(path.append(output.getName()));
129 outputElement = new TmfAnalysisOutputElement(output.getName(), newresource, this, output);
130 addChild(outputElement);
131 }
132 outputElement.refreshChildren();
133 }
134
135 }
136 /* Remove outputs that are not children of this analysis anymore */
137 for (TmfAnalysisOutputElement output : childrenMap.values()) {
138 removeChild(output);
139 }
140 }
141
142 // ------------------------------------------------------------------------
143 // TmfProjectModelElement
144 // ------------------------------------------------------------------------
145
146 @Override
147 public Styler getStyler() {
148 if (!fCanExecute) {
149 return ANALYSIS_CANT_EXECUTE_STYLER;
150 }
151 return null;
152 }
153
154 // ------------------------------------------------------------------------
155 // Operations
156 // ------------------------------------------------------------------------
157
158 /**
159 * Get the list of analysis output model elements under this analysis
160 *
161 * @return Array of analysis output elements
162 */
163 public List<TmfAnalysisOutputElement> getAvailableOutputs() {
164 List<ITmfProjectModelElement> children = getChildren();
165 List<TmfAnalysisOutputElement> outputs = new ArrayList<>();
166 for (ITmfProjectModelElement child : children) {
167 if (child instanceof TmfAnalysisOutputElement) {
168 outputs.add((TmfAnalysisOutputElement) child);
169 }
170 }
171 return outputs;
172 }
173
174 /**
175 * Gets the analysis id of this module
176 *
177 * @return The analysis id
178 */
179 public String getAnalysisId() {
180 return fAnalysisHelper.getId();
181 }
182
183 /**
184 * Gets the help message for this analysis
185 *
186 * @return The help message
187 */
188 public String getHelpMessage() {
189 ITmfProjectModelElement parent = getParent();
190
191 ITmfTrace trace = null;
192 if (parent instanceof TmfTraceElement) {
193 TmfTraceElement traceElement = (TmfTraceElement) parent;
194 trace = traceElement.getTrace();
195 if (trace != null) {
196 IAnalysisModule module = trace.getAnalysisModule(fAnalysisHelper.getId());
197 if (module != null) {
198 return module.getHelpText(trace);
199 }
200 }
201 }
202
203 if (trace != null) {
204 return fAnalysisHelper.getHelpText(trace);
205 }
206
207 return fAnalysisHelper.getHelpText();
208 }
209
210 /**
211 * Gets the icon file name for the analysis
212 *
213 * @return The analysis icon file name
214 */
215 public String getIconFile() {
216 return fAnalysisHelper.getIcon();
217 }
218
219 /**
220 * Gets the bundle this analysis is from
221 *
222 * @return The analysis bundle
223 */
224 public Bundle getBundle() {
225 return fAnalysisHelper.getBundle();
226 }
227
228 /** Delete all outputs under this analysis element */
229 private void deleteOutputs() {
230 for (TmfAnalysisOutputElement output : getAvailableOutputs()) {
231 removeChild(output);
232 }
233 }
234
235 /**
236 * Make sure the trace this analysis is associated to is the currently
237 * selected one
238 */
239 public void activateParent() {
240 ITmfProjectModelElement parent = getParent();
241
242 if (parent instanceof TmfTraceElement) {
243 TmfTraceElement traceElement = (TmfTraceElement) parent;
244 TmfOpenTraceHelper.openTraceFromElement(traceElement);
245 }
246 }
247
248 // ------------------------------------------------------------------------
249 // IPropertySource2
250 // ------------------------------------------------------------------------
251
252 /**
253 * @since 2.0
254 */
255 @Override
256 public Object getEditableValue() {
257 return null;
258 }
259
260 /**
261 * Get the analysis properties of this analysisElement if the corresponding
262 * analysis exists for the current trace
263 *
264 * @return a map with the names and values of the trace properties
265 * respectively as keys and values
266 */
267 private Map<String, String> getAnalysisProperties() {
268 ITmfProjectModelElement parent = getParent();
269
270 if (parent instanceof TmfCommonProjectElement) {
271 ITmfTrace trace = ((TmfCommonProjectElement) parent).getTrace();
272 if (trace == null) {
273 return Collections.EMPTY_MAP;
274 }
275 IAnalysisModule module = trace.getAnalysisModule(fAnalysisHelper.getId());
276 if (module instanceof ITmfPropertiesProvider) {
277 return ((ITmfPropertiesProvider) module).getProperties();
278 }
279 }
280
281 return Collections.EMPTY_MAP;
282 }
283
284 private Map<String, String> getAnalysisHelperProperties() {
285 if (fAnalysisHelper instanceof ITmfPropertiesProvider) {
286 ITmfPropertiesProvider analysisProperties = (ITmfPropertiesProvider) fAnalysisHelper;
287 return analysisProperties.getProperties();
288 }
289 return Collections.EMPTY_MAP;
290 }
291
292 /**
293 * @since 2.0
294 */
295 @Override
296 public IPropertyDescriptor[] getPropertyDescriptors() {
297 Map<String, String> helperProperties = getAnalysisHelperProperties();
298 Map<String, String> analysisProperties = getAnalysisProperties();
299 if (!analysisProperties.isEmpty() || !helperProperties.isEmpty()) {
300 List<IPropertyDescriptor> propertyDescriptorArray = new ArrayList<>(analysisProperties.size() + helperProperties.size());
301 for (Map.Entry<String, String> varName : helperProperties.entrySet()) {
302 ReadOnlyTextPropertyDescriptor descriptor = new ReadOnlyTextPropertyDescriptor(this.getName() + '_' + varName.getKey(), varName.getKey());
303 descriptor.setCategory(HELPER_PROPERTIES_CATEGORY);
304 propertyDescriptorArray.add(descriptor);
305 }
306 for (Map.Entry<String, String> varName : analysisProperties.entrySet()) {
307 ReadOnlyTextPropertyDescriptor descriptor = new ReadOnlyTextPropertyDescriptor(this.getName() + '_' + varName.getKey(), varName.getKey());
308 descriptor.setCategory(ANALYSIS_PROPERTIES_CATEGORY);
309 propertyDescriptorArray.add(descriptor);
310 }
311 return propertyDescriptorArray.toArray(new IPropertyDescriptor[analysisProperties.size() + helperProperties.size()]);
312 }
313 return new IPropertyDescriptor[0];
314 }
315
316 /**
317 * @since 2.0
318 */
319 @Override
320 public Object getPropertyValue(Object id) {
321 if (id == null) {
322 return null;
323 }
324 Map<String, String> properties = getAnalysisHelperProperties();
325 String key = (String) id;
326 /* Remove name from key */
327 key = key.substring(this.getName().length() + 1);
328 if (properties.containsKey(key)) {
329 String value = properties.get(key);
330 return value;
331 }
332
333 properties = getAnalysisProperties();
334 if (properties.containsKey(key)) {
335 String value = properties.get(key);
336 return value;
337 }
338
339 return null;
340 }
341
342 /**
343 * @since 2.0
344 */
345 @Override
346 public final void resetPropertyValue(Object id) {
347 }
348
349 /**
350 * @since 2.0
351 */
352 @Override
353 public final void setPropertyValue(Object id, Object value) {
354 }
355
356 /**
357 * @since 2.0
358 */
359 @Override
360 public final boolean isPropertyResettable(Object id) {
361 return false;
362 }
363
364 /**
365 * @since 2.0
366 */
367 @Override
368 public final boolean isPropertySet(Object id) {
369 return false;
370 }
371
372 }
This page took 0.038716 seconds and 4 git commands to generate.