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
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 1.0
73 */
74 protected TmfAnalysisElement(String name, IResource resource, ITmfProjectModelElement parent, @NonNull IAnalysisModuleHelper module) {
75 super(name, resource, parent);
76 fAnalysisHelper = module;
77 }
78
79 // ------------------------------------------------------------------------
80 // TmfProjectModelElement
81 // ------------------------------------------------------------------------
82
83 /**
84 * @since 2.0
85 */
86 @Override
87 protected void refreshChildren() {
88 fCanExecute = true;
89
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);
94 }
95
96 /** Get base path for resource */
97 IPath path = getProject().getTracesFolder().getPath();
98 IResource resource = getResource();
99 if (resource instanceof IFolder) {
100 path = ((IFolder) resource).getFullPath();
101 }
102
103 /*
104 * We can get a list of available outputs once the analysis is
105 * instantiated when the trace is opened
106 */
107 ITmfProjectModelElement parent = getParent();
108 if (parent instanceof TmfCommonProjectElement) {
109 ITmfTrace trace = ((TmfCommonProjectElement) parent).getTrace();
110 if (trace == null) {
111 deleteOutputs();
112 return;
113 }
114
115 IAnalysisModule module = trace.getAnalysisModule(fAnalysisHelper.getId());
116 if (module == null) {
117 deleteOutputs();
118 /*
119 * Trace is opened, but the analysis is null, so it does not
120 * apply
121 */
122 fCanExecute = false;
123 return;
124 }
125
126 for (IAnalysisOutput output : module.getOutputs()) {
127 TmfAnalysisOutputElement outputElement = childrenMap.remove(output.getName());
128 if (outputElement == null) {
129 IFolder newresource = ResourcesPlugin.getWorkspace().getRoot().getFolder(path.append(output.getName()));
130 outputElement = new TmfAnalysisOutputElement(output.getName(), newresource, this, output);
131 addChild(outputElement);
132 }
133 outputElement.refreshChildren();
134 }
135
136 }
137 /* Remove outputs that are not children of this analysis anymore */
138 for (TmfAnalysisOutputElement output : childrenMap.values()) {
139 removeChild(output);
140 }
141 }
142
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
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
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);
188 }
189 }
190 return outputs;
191 }
192
193 /**
194 * Gets the analysis id of this module
195 *
196 * @return The analysis id
197 */
198 public String getAnalysisId() {
199 return fAnalysisHelper.getId();
200 }
201
202 /**
203 * Gets the help message for this analysis
204 *
205 * @return The help message
206 */
207 public String getHelpMessage() {
208 ITmfProjectModelElement parent = getParent();
209
210 ITmfTrace trace = null;
211 if (parent instanceof TmfTraceElement) {
212 TmfTraceElement traceElement = (TmfTraceElement) parent;
213 trace = traceElement.getTrace();
214 if (trace != null) {
215 IAnalysisModule module = trace.getAnalysisModule(fAnalysisHelper.getId());
216 if (module != null) {
217 return module.getHelpText(trace);
218 }
219 }
220 }
221
222 if (trace != null) {
223 return fAnalysisHelper.getHelpText(trace);
224 }
225
226 return fAnalysisHelper.getHelpText();
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() {
235 return fAnalysisHelper.getIcon();
236 }
237
238 /**
239 * Gets the bundle this analysis is from
240 *
241 * @return The analysis bundle
242 */
243 public Bundle getBundle() {
244 return fAnalysisHelper.getBundle();
245 }
246
247 /** Delete all outputs under this analysis element */
248 private void deleteOutputs() {
249 for (TmfAnalysisOutputElement output : getAvailableOutputs()) {
250 removeChild(output);
251 }
252 }
253
254 /**
255 * Make sure the trace this analysis is associated to is the currently
256 * selected one
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 }
266
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
391 }
This page took 0.047096 seconds and 5 git commands to generate.