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