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