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