1 /*******************************************************************************
2 * Copyright (c) 2010, 2015 Ericsson, École Polytechnique de Montréal
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
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 *******************************************************************************/
16 package org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
;
18 import java
.util
.ArrayList
;
19 import java
.util
.Arrays
;
20 import java
.util
.Collections
;
21 import java
.util
.Comparator
;
22 import java
.util
.HashMap
;
23 import java
.util
.List
;
26 import org
.eclipse
.core
.resources
.IContainer
;
27 import org
.eclipse
.core
.resources
.IFile
;
28 import org
.eclipse
.core
.resources
.IFolder
;
29 import org
.eclipse
.core
.resources
.IResource
;
30 import org
.eclipse
.core
.resources
.IResourceProxy
;
31 import org
.eclipse
.core
.resources
.IResourceProxyVisitor
;
32 import org
.eclipse
.core
.resources
.IWorkspace
;
33 import org
.eclipse
.core
.resources
.ResourcesPlugin
;
34 import org
.eclipse
.core
.runtime
.CoreException
;
35 import org
.eclipse
.core
.runtime
.IConfigurationElement
;
36 import org
.eclipse
.core
.runtime
.IPath
;
37 import org
.eclipse
.core
.runtime
.IStatus
;
38 import org
.eclipse
.core
.runtime
.InvalidRegistryObjectException
;
39 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
40 import org
.eclipse
.core
.runtime
.Platform
;
41 import org
.eclipse
.jdt
.annotation
.NonNull
;
42 import org
.eclipse
.osgi
.util
.NLS
;
43 import org
.eclipse
.swt
.graphics
.Image
;
44 import org
.eclipse
.swt
.widgets
.Display
;
45 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.Activator
;
46 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.editors
.ITmfEventsEditorConstants
;
47 import org
.eclipse
.tracecompass
.tmf
.core
.TmfCommonConstants
;
48 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.IAnalysisModuleHelper
;
49 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.TmfAnalysisManager
;
50 import org
.eclipse
.tracecompass
.tmf
.core
.project
.model
.TmfTraceType
;
51 import org
.eclipse
.tracecompass
.tmf
.core
.project
.model
.TraceTypeHelper
;
52 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
53 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.experiment
.TmfExperiment
;
54 import org
.eclipse
.tracecompass
.tmf
.ui
.editors
.TmfEventsEditor
;
55 import org
.eclipse
.tracecompass
.tmf
.ui
.properties
.ReadOnlyTextPropertyDescriptor
;
56 import org
.eclipse
.ui
.views
.properties
.IPropertyDescriptor
;
57 import org
.eclipse
.ui
.views
.properties
.IPropertySource2
;
60 * Implementation of TMF Experiment Model Element.
64 * @author Francois Chouinard
67 public class TmfExperimentElement
extends TmfCommonProjectElement
implements IPropertySource2
{
69 // ------------------------------------------------------------------------
71 // ------------------------------------------------------------------------
73 // Property View stuff
74 private static final String INFO_CATEGORY
= "Info"; //$NON-NLS-1$
75 private static final String NAME
= "name"; //$NON-NLS-1$
76 private static final String PATH
= "path"; //$NON-NLS-1$
77 private static final String LOCATION
= "location"; //$NON-NLS-1$
78 private static final String FOLDER_SUFFIX
= "_exp"; //$NON-NLS-1$
79 private static final String EXPERIMENT_TYPE
= "type"; //$NON-NLS-1$
81 private static final ReadOnlyTextPropertyDescriptor NAME_DESCRIPTOR
= new ReadOnlyTextPropertyDescriptor(NAME
, NAME
);
82 private static final ReadOnlyTextPropertyDescriptor PATH_DESCRIPTOR
= new ReadOnlyTextPropertyDescriptor(PATH
, PATH
);
83 private static final ReadOnlyTextPropertyDescriptor LOCATION_DESCRIPTOR
= new ReadOnlyTextPropertyDescriptor(LOCATION
,
85 private static final ReadOnlyTextPropertyDescriptor TYPE_DESCRIPTOR
= new ReadOnlyTextPropertyDescriptor(EXPERIMENT_TYPE
, EXPERIMENT_TYPE
);
87 private static final IPropertyDescriptor
[] DESCRIPTORS
= { NAME_DESCRIPTOR
, PATH_DESCRIPTOR
,
88 LOCATION_DESCRIPTOR
, TYPE_DESCRIPTOR
};
91 NAME_DESCRIPTOR
.setCategory(INFO_CATEGORY
);
92 PATH_DESCRIPTOR
.setCategory(INFO_CATEGORY
);
93 LOCATION_DESCRIPTOR
.setCategory(INFO_CATEGORY
);
94 TYPE_DESCRIPTOR
.setCategory(INFO_CATEGORY
);
97 // The mapping of available trace type IDs to their corresponding
98 // configuration element
99 private static final Map
<String
, IConfigurationElement
> TRACE_TYPE_ATTRIBUTES
= new HashMap
<>();
100 private static final Map
<String
, IConfigurationElement
> TRACE_TYPE_UI_ATTRIBUTES
= new HashMap
<>();
101 private static final Map
<String
, IConfigurationElement
> TRACE_CATEGORIES
= new HashMap
<>();
103 // ------------------------------------------------------------------------
104 // Static initialization
105 // ------------------------------------------------------------------------
108 * Initialize statically at startup by getting extensions from the platform
109 * extension registry.
111 public static void init() {
112 IConfigurationElement
[] config
= Platform
.getExtensionRegistry().getConfigurationElementsFor(TmfTraceType
.TMF_TRACE_TYPE_ID
);
113 for (IConfigurationElement ce
: config
) {
114 String elementName
= ce
.getName();
115 if (elementName
.equals(TmfTraceType
.EXPERIMENT_ELEM
)) {
116 String traceTypeId
= ce
.getAttribute(TmfTraceType
.ID_ATTR
);
117 TRACE_TYPE_ATTRIBUTES
.put(traceTypeId
, ce
);
118 } else if (elementName
.equals(TmfTraceType
.CATEGORY_ELEM
)) {
119 String categoryId
= ce
.getAttribute(TmfTraceType
.ID_ATTR
);
120 TRACE_CATEGORIES
.put(categoryId
, ce
);
125 * Read the corresponding tmf.ui "tracetypeui" extension point for this
126 * trace type, if it exists.
128 config
= Platform
.getExtensionRegistry().getConfigurationElementsFor(TmfTraceTypeUIUtils
.TMF_TRACE_TYPE_UI_ID
);
129 for (IConfigurationElement ce
: config
) {
130 String elemName
= ce
.getName();
131 if (TmfTraceTypeUIUtils
.EXPERIMENT_ELEM
.equals(elemName
)) {
132 String traceType
= ce
.getAttribute(TmfTraceTypeUIUtils
.TRACETYPE_ATTR
);
133 TRACE_TYPE_UI_ATTRIBUTES
.put(traceType
, ce
);
138 // ------------------------------------------------------------------------
140 // ------------------------------------------------------------------------
145 * The name of the experiment
147 * The folder reference
149 * The experiment folder reference.
151 public TmfExperimentElement(String name
, IFolder folder
, TmfExperimentFolder parent
) {
152 super(name
, folder
, parent
);
155 // ------------------------------------------------------------------------
156 // TmfProjectModelElement
157 // ------------------------------------------------------------------------
160 public IFolder
getResource() {
161 return (IFolder
) super.getResource();
168 protected void refreshChildren() {
169 IFolder folder
= getResource();
171 /* Update the trace children of this experiment */
172 // Get the children from the model
173 Map
<String
, ITmfProjectModelElement
> childrenMap
= new HashMap
<>();
174 for (TmfTraceElement trace
: getTraces()) {
175 childrenMap
.put(trace
.getElementPath(), trace
);
178 List
<IResource
> members
= getTraceResources();
179 for (IResource resource
: members
) {
180 String name
= resource
.getName();
181 String elementPath
= resource
.getFullPath().makeRelativeTo(folder
.getFullPath()).toString();
182 ITmfProjectModelElement element
= childrenMap
.get(elementPath
);
183 if (element
instanceof TmfTraceElement
) {
184 childrenMap
.remove(elementPath
);
186 element
= new TmfTraceElement(name
, resource
, this);
191 // Cleanup dangling children from the model
192 for (ITmfProjectModelElement danglingChild
: childrenMap
.values()) {
193 removeChild(danglingChild
);
196 /* Update the analysis under this experiment */
197 super.refreshChildren();
200 * If the experiment is opened, add any analysis that was not added by
201 * the parent if it is available with the experiment
203 ITmfTrace experiment
= getTrace();
204 if (experiment
== null) {
207 Map
<String
, TmfAnalysisElement
> analysisMap
= new HashMap
<>();
208 for (TmfAnalysisElement analysis
: getAvailableAnalysis()) {
209 analysisMap
.put(analysis
.getAnalysisId(), analysis
);
211 for (IAnalysisModuleHelper module
: TmfAnalysisManager
.getAnalysisModules().values()) {
212 if (!analysisMap
.containsKey(module
.getId()) && module
.appliesToExperiment() && (experiment
.getAnalysisModule(module
.getId()) != null)) {
213 IFolder newresource
= ResourcesPlugin
.getWorkspace().getRoot().getFolder(getResource().getFullPath().append(module
.getId()));
214 TmfAnalysisElement analysis
= new TmfAnalysisElement(module
.getName(), newresource
, this, module
);
216 analysis
.refreshChildren();
217 analysisMap
.put(module
.getId(), analysis
);
222 private List
<IResource
> getTraceResources() {
223 IFolder folder
= getResource();
224 final List
<IResource
> list
= new ArrayList
<>();
226 folder
.accept(new IResourceProxyVisitor() {
228 public boolean visit(IResourceProxy resource
) throws CoreException
{
229 if (resource
.isLinked()) {
230 list
.add(resource
.requestResource());
235 } catch (CoreException e
) {
237 Comparator
<IResource
> comparator
= new Comparator
<IResource
>() {
239 public int compare(IResource o1
, IResource o2
) {
240 return o1
.getFullPath().toString().compareTo(o2
.getFullPath().toString());
243 Collections
.sort(list
, comparator
);
251 public @NonNull Image
getIcon() {
252 Image icon
= super.getIcon();
253 return (icon
== null ? TmfProjectModelIcons
.DEFAULT_EXPERIMENT_ICON
: icon
);
260 public String
getLabelText() {
261 return getName() + " [" + getTraces().size() + "]"; //$NON-NLS-1$ //$NON-NLS-2$
264 // ------------------------------------------------------------------------
266 // ------------------------------------------------------------------------
269 * Refreshes the trace type filed by reading the trace type persistent
270 * property of the resource reference.
272 * If trace type is null after refresh, set it to the generic trace type
273 * (for seamless upgrade)
276 public void refreshTraceType() {
277 super.refreshTraceType();
278 if (getTraceType() == null) {
279 IConfigurationElement ce
= TmfTraceType
.getTraceAttributes(TmfTraceType
.DEFAULT_EXPERIMENT_TYPE
);
282 IFolder experimentFolder
= getResource();
283 experimentFolder
.setPersistentProperty(TmfCommonConstants
.TRACETYPE
, ce
.getAttribute(TmfTraceType
.ID_ATTR
));
284 super.refreshTraceType();
285 } catch (InvalidRegistryObjectException
| CoreException e
) {
292 * Returns a list of TmfTraceElements contained in this experiment.
294 * @return a list of TmfTraceElements
297 public List
<TmfTraceElement
> getTraces() {
298 List
<ITmfProjectModelElement
> children
= getChildren();
299 List
<TmfTraceElement
> traces
= new ArrayList
<>();
300 for (ITmfProjectModelElement child
: children
) {
301 if (child
instanceof TmfTraceElement
) {
302 traces
.add((TmfTraceElement
) child
);
309 * Adds a trace to the experiment
312 * The trace element to add
314 public void addTrace(TmfTraceElement trace
) {
315 addTrace(trace
, true);
319 * Adds a trace to the experiment
322 * The trace element to add
324 * Flag for refreshing the project
326 public void addTrace(TmfTraceElement trace
, boolean refresh
) {
328 * Create a link to the actual trace and set the trace type
330 IFolder experiment
= getResource();
331 IResource resource
= trace
.getResource();
332 IPath location
= resource
.getLocation();
333 IWorkspace workspace
= ResourcesPlugin
.getWorkspace();
335 String traceTypeId
= TmfTraceType
.getTraceTypeId(trace
.getResource());
336 TraceTypeHelper traceType
= TmfTraceType
.getTraceType(traceTypeId
);
338 if (resource
instanceof IFolder
) {
339 IFolder folder
= experiment
.getFolder(trace
.getElementPath());
340 TraceUtils
.createFolder((IFolder
) folder
.getParent(), new NullProgressMonitor());
341 IStatus result
= workspace
.validateLinkLocation(folder
, location
);
342 if (result
.isOK() || result
.matches(IStatus
.INFO
| IStatus
.WARNING
)) {
343 folder
.createLink(location
, IResource
.REPLACE
, null);
344 if (traceType
!= null) {
345 TmfTraceTypeUIUtils
.setTraceType(folder
, traceType
, refresh
);
349 Activator
.getDefault().logError("Error creating link. Invalid trace location " + location
); //$NON-NLS-1$
352 IFile file
= experiment
.getFile(trace
.getElementPath());
353 TraceUtils
.createFolder((IFolder
) file
.getParent(), new NullProgressMonitor());
354 IStatus result
= workspace
.validateLinkLocation(file
, location
);
355 if (result
.isOK() || result
.matches(IStatus
.INFO
| IStatus
.WARNING
)) {
356 file
.createLink(location
, IResource
.REPLACE
, null);
357 if (traceType
!= null) {
358 TmfTraceTypeUIUtils
.setTraceType(file
, traceType
, refresh
);
361 Activator
.getDefault().logError("Error creating link. Invalid trace location " + location
); //$NON-NLS-1$
364 } catch (CoreException e
) {
365 Activator
.getDefault().logError("Error creating link to location " + location
, e
); //$NON-NLS-1$
371 * Removes a trace from an experiment
374 * The trace to remove
375 * @throws CoreException
378 public void removeTrace(TmfTraceElement trace
) throws CoreException
{
380 // Close editors in UI Thread
381 Display
.getDefault().syncExec(new Runnable() {
388 /* Finally, remove the trace from experiment */
390 deleteTraceResource(trace
.getResource());
391 deleteSupplementaryResources();
394 private void deleteTraceResource(IResource resource
) throws CoreException
{
395 resource
.delete(true, null);
396 IContainer parent
= resource
.getParent();
397 // delete empty folders up to the parent experiment folder
398 if (!parent
.equals(getResource()) && parent
.members().length
== 0) {
399 deleteTraceResource(parent
);
404 public IFile
createBookmarksFile() throws CoreException
{
405 return createBookmarksFile(getProject().getExperimentsFolder().getResource(), ITmfEventsEditorConstants
.EXPERIMENT_EDITOR_INPUT_TYPE
);
409 public String
getEditorId() {
410 /* See if a default editor was set for this experiment type */
411 if (getTraceType() != null) {
412 IConfigurationElement ce
= TRACE_TYPE_UI_ATTRIBUTES
.get(getTraceType());
414 IConfigurationElement
[] defaultEditorCE
= ce
.getChildren(TmfTraceTypeUIUtils
.DEFAULT_EDITOR_ELEM
);
415 if (defaultEditorCE
.length
== 1) {
416 return defaultEditorCE
[0].getAttribute(TmfTraceType
.ID_ATTR
);
421 /* No default editor, try to find a common editor for all traces */
422 final List
<TmfTraceElement
> traceEntries
= getTraces();
423 String commonEditorId
= null;
425 for (TmfTraceElement element
: traceEntries
) {
426 // If all traces use the same editorId, use it, otherwise use the
428 final String editorId
= element
.getEditorId();
429 if (commonEditorId
== null) {
430 commonEditorId
= (editorId
!= null) ? editorId
: TmfEventsEditor
.ID
;
431 } else if (!commonEditorId
.equals(editorId
)) {
432 commonEditorId
= TmfEventsEditor
.ID
;
439 * Instantiate a {@link TmfExperiment} object based on the experiment type
440 * and the corresponding extension.
442 * @return the {@link TmfExperiment} or <code>null</code> for an error
445 public TmfExperiment
instantiateTrace() {
448 // make sure that supplementary folder exists
449 refreshSupplementaryFolder();
451 if (getTraceType() != null) {
453 IConfigurationElement ce
= TRACE_TYPE_ATTRIBUTES
.get(getTraceType());
457 TmfExperiment experiment
= (TmfExperiment
) ce
.createExecutableExtension(TmfTraceType
.EXPERIMENT_TYPE_ATTR
);
460 } catch (CoreException e
) {
461 Activator
.getDefault().logError(NLS
.bind(Messages
.TmfExperimentElement_ErrorInstantiatingTrace
, getName()), e
);
467 public String
getTypeName() {
468 return Messages
.TmfExperimentElement_TypeName
;
471 // ------------------------------------------------------------------------
473 // ------------------------------------------------------------------------
476 public Object
getEditableValue() {
481 public IPropertyDescriptor
[] getPropertyDescriptors() {
482 return Arrays
.copyOf(DESCRIPTORS
, DESCRIPTORS
.length
);
486 public Object
getPropertyValue(Object id
) {
488 if (NAME
.equals(id
)) {
492 if (PATH
.equals(id
)) {
493 return getPath().toString();
496 if (LOCATION
.equals(id
)) {
497 return getLocation().toString();
500 if (EXPERIMENT_TYPE
.equals(id
)) {
501 if (getTraceType() != null) {
502 IConfigurationElement ce
= TRACE_TYPE_ATTRIBUTES
.get(getTraceType());
504 return ""; //$NON-NLS-1$
506 String categoryId
= ce
.getAttribute(TmfTraceType
.CATEGORY_ATTR
);
507 if (categoryId
!= null) {
508 IConfigurationElement category
= TRACE_CATEGORIES
.get(categoryId
);
509 if (category
!= null) {
510 return category
.getAttribute(TmfTraceType
.NAME_ATTR
) + ':' + ce
.getAttribute(TmfTraceType
.NAME_ATTR
);
513 return ce
.getAttribute(TmfTraceType
.NAME_ATTR
);
521 public void resetPropertyValue(Object id
) {
525 public void setPropertyValue(Object id
, Object value
) {
529 public boolean isPropertyResettable(Object id
) {
534 public boolean isPropertySet(Object id
) {
539 * Return the suffix for resource names
541 * @return The folder suffix
544 public String
getSuffix() {
545 return FOLDER_SUFFIX
;