tmf: Put analyses under their own node in the Project View
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / project / model / TmfExperimentElement.java
1 /*******************************************************************************
2 * Copyright (c) 2010, 2015 Ericsson, É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 * Francois Chouinard - Initial API and implementation
11 * Geneviève Bastien - Copied code to add/remove traces in this class
12 * Patrick Tasse - Close editors to release resources
13 * Geneviève Bastien - Experiment instantiated with trace type
14 *******************************************************************************/
15
16 package org.eclipse.tracecompass.tmf.ui.project.model;
17
18 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
19
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.Collections;
23 import java.util.Comparator;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27
28 import org.eclipse.core.resources.IContainer;
29 import org.eclipse.core.resources.IFile;
30 import org.eclipse.core.resources.IFolder;
31 import org.eclipse.core.resources.IResource;
32 import org.eclipse.core.resources.IResourceProxy;
33 import org.eclipse.core.resources.IResourceProxyVisitor;
34 import org.eclipse.core.resources.IWorkspace;
35 import org.eclipse.core.resources.ResourcesPlugin;
36 import org.eclipse.core.runtime.CoreException;
37 import org.eclipse.core.runtime.IConfigurationElement;
38 import org.eclipse.core.runtime.IPath;
39 import org.eclipse.core.runtime.IStatus;
40 import org.eclipse.core.runtime.InvalidRegistryObjectException;
41 import org.eclipse.core.runtime.NullProgressMonitor;
42 import org.eclipse.core.runtime.Platform;
43 import org.eclipse.jdt.annotation.NonNull;
44 import org.eclipse.osgi.util.NLS;
45 import org.eclipse.swt.graphics.Image;
46 import org.eclipse.swt.widgets.Display;
47 import org.eclipse.tracecompass.internal.tmf.ui.Activator;
48 import org.eclipse.tracecompass.internal.tmf.ui.editors.ITmfEventsEditorConstants;
49 import org.eclipse.tracecompass.tmf.core.TmfCommonConstants;
50 import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModuleHelper;
51 import org.eclipse.tracecompass.tmf.core.analysis.TmfAnalysisManager;
52 import org.eclipse.tracecompass.tmf.core.project.model.TmfTraceType;
53 import org.eclipse.tracecompass.tmf.core.project.model.TraceTypeHelper;
54 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
55 import org.eclipse.tracecompass.tmf.core.trace.experiment.TmfExperiment;
56 import org.eclipse.tracecompass.tmf.ui.editors.TmfEventsEditor;
57 import org.eclipse.tracecompass.tmf.ui.properties.ReadOnlyTextPropertyDescriptor;
58 import org.eclipse.ui.views.properties.IPropertyDescriptor;
59 import org.eclipse.ui.views.properties.IPropertySource2;
60
61 /**
62 * Implementation of TMF Experiment Model Element.
63 * <p>
64 *
65 * @version 1.0
66 * @author Francois Chouinard
67 *
68 */
69 public class TmfExperimentElement extends TmfCommonProjectElement implements IPropertySource2 {
70
71 // ------------------------------------------------------------------------
72 // Constants
73 // ------------------------------------------------------------------------
74
75 // Property View stuff
76 private static final String INFO_CATEGORY = "Info"; //$NON-NLS-1$
77 private static final String NAME = "name"; //$NON-NLS-1$
78 private static final String PATH = "path"; //$NON-NLS-1$
79 private static final String LOCATION = "location"; //$NON-NLS-1$
80 private static final String FOLDER_SUFFIX = "_exp"; //$NON-NLS-1$
81 private static final String EXPERIMENT_TYPE = "type"; //$NON-NLS-1$
82
83 private static final ReadOnlyTextPropertyDescriptor NAME_DESCRIPTOR = new ReadOnlyTextPropertyDescriptor(NAME, NAME);
84 private static final ReadOnlyTextPropertyDescriptor PATH_DESCRIPTOR = new ReadOnlyTextPropertyDescriptor(PATH, PATH);
85 private static final ReadOnlyTextPropertyDescriptor LOCATION_DESCRIPTOR = new ReadOnlyTextPropertyDescriptor(LOCATION,
86 LOCATION);
87 private static final ReadOnlyTextPropertyDescriptor TYPE_DESCRIPTOR = new ReadOnlyTextPropertyDescriptor(EXPERIMENT_TYPE, EXPERIMENT_TYPE);
88
89 private static final IPropertyDescriptor[] DESCRIPTORS = { NAME_DESCRIPTOR, PATH_DESCRIPTOR,
90 LOCATION_DESCRIPTOR, TYPE_DESCRIPTOR };
91
92 static {
93 NAME_DESCRIPTOR.setCategory(INFO_CATEGORY);
94 PATH_DESCRIPTOR.setCategory(INFO_CATEGORY);
95 LOCATION_DESCRIPTOR.setCategory(INFO_CATEGORY);
96 TYPE_DESCRIPTOR.setCategory(INFO_CATEGORY);
97 }
98
99 // The mapping of available trace type IDs to their corresponding
100 // configuration element
101 private static final Map<String, IConfigurationElement> TRACE_TYPE_ATTRIBUTES = new HashMap<>();
102 private static final Map<String, IConfigurationElement> TRACE_TYPE_UI_ATTRIBUTES = new HashMap<>();
103 private static final Map<String, IConfigurationElement> TRACE_CATEGORIES = new HashMap<>();
104
105 // ------------------------------------------------------------------------
106 // Static initialization
107 // ------------------------------------------------------------------------
108
109 /**
110 * Initialize statically at startup by getting extensions from the platform
111 * extension registry.
112 */
113 public static void init() {
114 IConfigurationElement[] config = Platform.getExtensionRegistry().getConfigurationElementsFor(TmfTraceType.TMF_TRACE_TYPE_ID);
115 for (IConfigurationElement ce : config) {
116 String elementName = ce.getName();
117 if (elementName.equals(TmfTraceType.EXPERIMENT_ELEM)) {
118 String traceTypeId = ce.getAttribute(TmfTraceType.ID_ATTR);
119 TRACE_TYPE_ATTRIBUTES.put(traceTypeId, ce);
120 } else if (elementName.equals(TmfTraceType.CATEGORY_ELEM)) {
121 String categoryId = ce.getAttribute(TmfTraceType.ID_ATTR);
122 TRACE_CATEGORIES.put(categoryId, ce);
123 }
124 }
125
126 /*
127 * Read the corresponding tmf.ui "tracetypeui" extension point for this
128 * trace type, if it exists.
129 */
130 config = Platform.getExtensionRegistry().getConfigurationElementsFor(TmfTraceTypeUIUtils.TMF_TRACE_TYPE_UI_ID);
131 for (IConfigurationElement ce : config) {
132 String elemName = ce.getName();
133 if (TmfTraceTypeUIUtils.EXPERIMENT_ELEM.equals(elemName)) {
134 String traceType = ce.getAttribute(TmfTraceTypeUIUtils.TRACETYPE_ATTR);
135 TRACE_TYPE_UI_ATTRIBUTES.put(traceType, ce);
136 }
137 }
138 }
139
140 // ------------------------------------------------------------------------
141 // Constructors
142 // ------------------------------------------------------------------------
143 /**
144 * Constructor
145 *
146 * @param name
147 * The name of the experiment
148 * @param folder
149 * The folder reference
150 * @param parent
151 * The experiment folder reference.
152 */
153 public TmfExperimentElement(String name, IFolder folder, TmfExperimentFolder parent) {
154 super(name, folder, parent);
155 }
156
157 // ------------------------------------------------------------------------
158 // TmfProjectModelElement
159 // ------------------------------------------------------------------------
160
161 @Override
162 public IFolder getResource() {
163 return (IFolder) super.getResource();
164 }
165
166 /**
167 * @since 2.0
168 */
169 @Override
170 protected void refreshChildren() {
171 IFolder folder = getResource();
172
173 /* Update the trace children of this experiment */
174 // Get the children from the model
175 Map<String, ITmfProjectModelElement> childrenMap = new HashMap<>();
176 for (TmfTraceElement trace : getTraces()) {
177 childrenMap.put(trace.getElementPath(), trace);
178 }
179
180 List<IResource> members = getTraceResources();
181 for (IResource resource : members) {
182 String name = resource.getName();
183 String elementPath = resource.getFullPath().makeRelativeTo(folder.getFullPath()).toString();
184 ITmfProjectModelElement element = childrenMap.get(elementPath);
185 if (element instanceof TmfTraceElement) {
186 childrenMap.remove(elementPath);
187 } else {
188 element = new TmfTraceElement(name, resource, this);
189 addChild(element);
190 }
191 }
192
193 // Cleanup dangling children from the model
194 for (ITmfProjectModelElement danglingChild : childrenMap.values()) {
195 removeChild(danglingChild);
196 }
197
198 /* Update the analysis under this experiment */
199 super.refreshChildren();
200
201 /*
202 * If the experiment is opened, add any analysis that was not added by
203 * the parent if it is available with the experiment
204 */
205 ITmfTrace experiment = getTrace();
206 if (experiment == null) {
207 return;
208 }
209
210 /* super.refreshChildren() above should have set this */
211 TmfViewsElement viewsElement = checkNotNull(getChildElementViews());
212
213 Map<String, TmfAnalysisElement> analysisMap = new HashMap<>();
214 for (TmfAnalysisElement analysis : getAvailableAnalysis()) {
215 analysisMap.put(analysis.getAnalysisId(), analysis);
216 }
217 for (IAnalysisModuleHelper module : TmfAnalysisManager.getAnalysisModules().values()) {
218 if (!analysisMap.containsKey(module.getId()) && module.appliesToExperiment() && (experiment.getAnalysisModule(module.getId()) != null)) {
219 IFolder newresource = ResourcesPlugin.getWorkspace().getRoot().getFolder(getResource().getFullPath().append(module.getId()));
220 TmfAnalysisElement analysis = new TmfAnalysisElement(module.getName(), newresource, viewsElement, module);
221 viewsElement.addChild(analysis);
222 analysis.refreshChildren();
223 analysisMap.put(module.getId(), analysis);
224 }
225 }
226 }
227
228 private List<IResource> getTraceResources() {
229 IFolder folder = getResource();
230 final List<IResource> list = new ArrayList<>();
231 try {
232 folder.accept(new IResourceProxyVisitor() {
233 @Override
234 public boolean visit(IResourceProxy resource) throws CoreException {
235 if (resource.isLinked()) {
236 list.add(resource.requestResource());
237 }
238 return true;
239 }
240 }, IResource.NONE);
241 } catch (CoreException e) {
242 }
243 Comparator<IResource> comparator = new Comparator<IResource>() {
244 @Override
245 public int compare(IResource o1, IResource o2) {
246 return o1.getFullPath().toString().compareTo(o2.getFullPath().toString());
247 }
248 };
249 Collections.sort(list, comparator);
250 return list;
251 }
252
253 /**
254 * @since 2.0
255 */
256 @Override
257 public @NonNull Image getIcon() {
258 Image icon = super.getIcon();
259 return (icon == null ? TmfProjectModelIcons.DEFAULT_EXPERIMENT_ICON : icon);
260 }
261
262 /**
263 * @since 2.0
264 */
265 @Override
266 public String getLabelText() {
267 return getName() + " [" + getTraces().size() + "]"; //$NON-NLS-1$ //$NON-NLS-2$
268 }
269
270 // ------------------------------------------------------------------------
271 // Operations
272 // ------------------------------------------------------------------------
273
274 /**
275 * Refreshes the trace type filed by reading the trace type persistent
276 * property of the resource reference.
277 *
278 * If trace type is null after refresh, set it to the generic trace type
279 * (for seamless upgrade)
280 */
281 @Override
282 public void refreshTraceType() {
283 super.refreshTraceType();
284 if (getTraceType() == null) {
285 IConfigurationElement ce = TmfTraceType.getTraceAttributes(TmfTraceType.DEFAULT_EXPERIMENT_TYPE);
286 if (ce != null) {
287 try {
288 IFolder experimentFolder = getResource();
289 experimentFolder.setPersistentProperty(TmfCommonConstants.TRACETYPE, ce.getAttribute(TmfTraceType.ID_ATTR));
290 super.refreshTraceType();
291 } catch (InvalidRegistryObjectException | CoreException e) {
292 }
293 }
294 }
295 }
296
297 /**
298 * Returns a list of TmfTraceElements contained in this experiment.
299 *
300 * @return a list of TmfTraceElements
301 */
302 @Override
303 public List<TmfTraceElement> getTraces() {
304 List<ITmfProjectModelElement> children = getChildren();
305 List<TmfTraceElement> traces = new ArrayList<>();
306 for (ITmfProjectModelElement child : children) {
307 if (child instanceof TmfTraceElement) {
308 traces.add((TmfTraceElement) child);
309 }
310 }
311 return traces;
312 }
313
314 /**
315 * Adds a trace to the experiment
316 *
317 * @param trace
318 * The trace element to add
319 */
320 public void addTrace(TmfTraceElement trace) {
321 addTrace(trace, true);
322 }
323
324 /**
325 * Adds a trace to the experiment
326 *
327 * @param trace
328 * The trace element to add
329 * @param refresh
330 * Flag for refreshing the project
331 */
332 public void addTrace(TmfTraceElement trace, boolean refresh) {
333 /**
334 * Create a link to the actual trace and set the trace type
335 */
336 IFolder experiment = getResource();
337 IResource resource = trace.getResource();
338 IPath location = resource.getLocation();
339 IWorkspace workspace = ResourcesPlugin.getWorkspace();
340 try {
341 String traceTypeId = TmfTraceType.getTraceTypeId(trace.getResource());
342 TraceTypeHelper traceType = TmfTraceType.getTraceType(traceTypeId);
343
344 if (resource instanceof IFolder) {
345 IFolder folder = experiment.getFolder(trace.getElementPath());
346 TraceUtils.createFolder((IFolder) folder.getParent(), new NullProgressMonitor());
347 IStatus result = workspace.validateLinkLocation(folder, location);
348 if (result.isOK() || result.matches(IStatus.INFO | IStatus.WARNING)) {
349 folder.createLink(location, IResource.REPLACE, null);
350 if (traceType != null) {
351 TmfTraceTypeUIUtils.setTraceType(folder, traceType, refresh);
352 }
353
354 } else {
355 Activator.getDefault().logError("Error creating link. Invalid trace location " + location); //$NON-NLS-1$
356 }
357 } else {
358 IFile file = experiment.getFile(trace.getElementPath());
359 TraceUtils.createFolder((IFolder) file.getParent(), new NullProgressMonitor());
360 IStatus result = workspace.validateLinkLocation(file, location);
361 if (result.isOK() || result.matches(IStatus.INFO | IStatus.WARNING)) {
362 file.createLink(location, IResource.REPLACE, null);
363 if (traceType != null) {
364 TmfTraceTypeUIUtils.setTraceType(file, traceType, refresh);
365 }
366 } else {
367 Activator.getDefault().logError("Error creating link. Invalid trace location " + location); //$NON-NLS-1$
368 }
369 }
370 } catch (CoreException e) {
371 Activator.getDefault().logError("Error creating link to location " + location, e); //$NON-NLS-1$
372 }
373
374 }
375
376 /**
377 * Removes a trace from an experiment
378 *
379 * @param trace
380 * The trace to remove
381 * @throws CoreException
382 * exception
383 */
384 public void removeTrace(TmfTraceElement trace) throws CoreException {
385
386 // Close editors in UI Thread
387 Display.getDefault().syncExec(new Runnable() {
388 @Override
389 public void run() {
390 closeEditors();
391 }
392 });
393
394 /* Finally, remove the trace from experiment */
395 removeChild(trace);
396 deleteTraceResource(trace.getResource());
397 deleteSupplementaryResources();
398 }
399
400 private void deleteTraceResource(IResource resource) throws CoreException {
401 resource.delete(true, null);
402 IContainer parent = resource.getParent();
403 // delete empty folders up to the parent experiment folder
404 if (!parent.equals(getResource()) && parent.members().length == 0) {
405 deleteTraceResource(parent);
406 }
407 }
408
409 @Override
410 public IFile createBookmarksFile() throws CoreException {
411 return createBookmarksFile(getProject().getExperimentsFolder().getResource(), ITmfEventsEditorConstants.EXPERIMENT_EDITOR_INPUT_TYPE);
412 }
413
414 @Override
415 public String getEditorId() {
416 /* See if a default editor was set for this experiment type */
417 if (getTraceType() != null) {
418 IConfigurationElement ce = TRACE_TYPE_UI_ATTRIBUTES.get(getTraceType());
419 if (ce != null) {
420 IConfigurationElement[] defaultEditorCE = ce.getChildren(TmfTraceTypeUIUtils.DEFAULT_EDITOR_ELEM);
421 if (defaultEditorCE.length == 1) {
422 return defaultEditorCE[0].getAttribute(TmfTraceType.ID_ATTR);
423 }
424 }
425 }
426
427 /* No default editor, try to find a common editor for all traces */
428 final List<TmfTraceElement> traceEntries = getTraces();
429 String commonEditorId = null;
430
431 for (TmfTraceElement element : traceEntries) {
432 // If all traces use the same editorId, use it, otherwise use the
433 // default
434 final String editorId = element.getEditorId();
435 if (commonEditorId == null) {
436 commonEditorId = (editorId != null) ? editorId : TmfEventsEditor.ID;
437 } else if (!commonEditorId.equals(editorId)) {
438 commonEditorId = TmfEventsEditor.ID;
439 }
440 }
441 return null;
442 }
443
444 /**
445 * Instantiate a {@link TmfExperiment} object based on the experiment type
446 * and the corresponding extension.
447 *
448 * @return the {@link TmfExperiment} or <code>null</code> for an error
449 */
450 @Override
451 public TmfExperiment instantiateTrace() {
452 try {
453
454 // make sure that supplementary folder exists
455 refreshSupplementaryFolder();
456
457 if (getTraceType() != null) {
458
459 IConfigurationElement ce = TRACE_TYPE_ATTRIBUTES.get(getTraceType());
460 if (ce == null) {
461 return null;
462 }
463 TmfExperiment experiment = (TmfExperiment) ce.createExecutableExtension(TmfTraceType.EXPERIMENT_TYPE_ATTR);
464 return experiment;
465 }
466 } catch (CoreException e) {
467 Activator.getDefault().logError(NLS.bind(Messages.TmfExperimentElement_ErrorInstantiatingTrace, getName()), e);
468 }
469 return null;
470 }
471
472 @Override
473 public String getTypeName() {
474 return Messages.TmfExperimentElement_TypeName;
475 }
476
477 // ------------------------------------------------------------------------
478 // IPropertySource2
479 // ------------------------------------------------------------------------
480
481 @Override
482 public Object getEditableValue() {
483 return null;
484 }
485
486 @Override
487 public IPropertyDescriptor[] getPropertyDescriptors() {
488 return Arrays.copyOf(DESCRIPTORS, DESCRIPTORS.length);
489 }
490
491 @Override
492 public Object getPropertyValue(Object id) {
493
494 if (NAME.equals(id)) {
495 return getName();
496 }
497
498 if (PATH.equals(id)) {
499 return getPath().toString();
500 }
501
502 if (LOCATION.equals(id)) {
503 return getLocation().toString();
504 }
505
506 if (EXPERIMENT_TYPE.equals(id)) {
507 if (getTraceType() != null) {
508 IConfigurationElement ce = TRACE_TYPE_ATTRIBUTES.get(getTraceType());
509 if (ce == null) {
510 return ""; //$NON-NLS-1$
511 }
512 String categoryId = ce.getAttribute(TmfTraceType.CATEGORY_ATTR);
513 if (categoryId != null) {
514 IConfigurationElement category = TRACE_CATEGORIES.get(categoryId);
515 if (category != null) {
516 return category.getAttribute(TmfTraceType.NAME_ATTR) + ':' + ce.getAttribute(TmfTraceType.NAME_ATTR);
517 }
518 }
519 return ce.getAttribute(TmfTraceType.NAME_ATTR);
520 }
521 }
522
523 return null;
524 }
525
526 @Override
527 public void resetPropertyValue(Object id) {
528 }
529
530 @Override
531 public void setPropertyValue(Object id, Object value) {
532 }
533
534 @Override
535 public boolean isPropertyResettable(Object id) {
536 return false;
537 }
538
539 @Override
540 public boolean isPropertySet(Object id) {
541 return false;
542 }
543
544 /**
545 * Return the suffix for resource names
546 *
547 * @return The folder suffix
548 */
549 @Override
550 public String getSuffix() {
551 return FOLDER_SUFFIX;
552 }
553
554 }
This page took 0.063345 seconds and 6 git commands to generate.