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 static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
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
;
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
;
62 * Implementation of TMF Experiment Model Element.
66 * @author Francois Chouinard
69 public class TmfExperimentElement
extends TmfCommonProjectElement
implements IPropertySource2
{
71 // ------------------------------------------------------------------------
73 // ------------------------------------------------------------------------
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$
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
,
87 private static final ReadOnlyTextPropertyDescriptor TYPE_DESCRIPTOR
= new ReadOnlyTextPropertyDescriptor(EXPERIMENT_TYPE
, EXPERIMENT_TYPE
);
89 private static final IPropertyDescriptor
[] DESCRIPTORS
= { NAME_DESCRIPTOR
, PATH_DESCRIPTOR
,
90 LOCATION_DESCRIPTOR
, TYPE_DESCRIPTOR
};
93 NAME_DESCRIPTOR
.setCategory(INFO_CATEGORY
);
94 PATH_DESCRIPTOR
.setCategory(INFO_CATEGORY
);
95 LOCATION_DESCRIPTOR
.setCategory(INFO_CATEGORY
);
96 TYPE_DESCRIPTOR
.setCategory(INFO_CATEGORY
);
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
<>();
105 // ------------------------------------------------------------------------
106 // Static initialization
107 // ------------------------------------------------------------------------
110 * Initialize statically at startup by getting extensions from the platform
111 * extension registry.
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
);
127 * Read the corresponding tmf.ui "tracetypeui" extension point for this
128 * trace type, if it exists.
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
);
140 // ------------------------------------------------------------------------
142 // ------------------------------------------------------------------------
147 * The name of the experiment
149 * The folder reference
151 * The experiment folder reference.
153 public TmfExperimentElement(String name
, IFolder folder
, TmfExperimentFolder parent
) {
154 super(name
, folder
, parent
);
157 // ------------------------------------------------------------------------
158 // TmfProjectModelElement
159 // ------------------------------------------------------------------------
162 public IFolder
getResource() {
163 return (IFolder
) super.getResource();
170 protected void refreshChildren() {
171 IFolder folder
= getResource();
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
);
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
);
188 element
= new TmfTraceElement(name
, resource
, this);
193 // Cleanup dangling children from the model
194 for (ITmfProjectModelElement danglingChild
: childrenMap
.values()) {
195 removeChild(danglingChild
);
198 /* Update the analysis under this experiment */
199 super.refreshChildren();
202 * If the experiment is opened, add any analysis that was not added by
203 * the parent if it is available with the experiment
205 ITmfTrace experiment
= getTrace();
206 if (experiment
== null) {
210 /* super.refreshChildren() above should have set this */
211 TmfViewsElement viewsElement
= checkNotNull(getChildElementViews());
213 Map
<String
, TmfAnalysisElement
> analysisMap
= new HashMap
<>();
214 for (TmfAnalysisElement analysis
: getAvailableAnalysis()) {
215 analysisMap
.put(analysis
.getAnalysisId(), analysis
);
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
);
228 private List
<IResource
> getTraceResources() {
229 IFolder folder
= getResource();
230 final List
<IResource
> list
= new ArrayList
<>();
232 folder
.accept(new IResourceProxyVisitor() {
234 public boolean visit(IResourceProxy resource
) throws CoreException
{
235 if (resource
.isLinked()) {
236 list
.add(resource
.requestResource());
241 } catch (CoreException e
) {
243 Comparator
<IResource
> comparator
= new Comparator
<IResource
>() {
245 public int compare(IResource o1
, IResource o2
) {
246 return o1
.getFullPath().toString().compareTo(o2
.getFullPath().toString());
249 Collections
.sort(list
, comparator
);
257 public @NonNull Image
getIcon() {
258 Image icon
= super.getIcon();
259 return (icon
== null ? TmfProjectModelIcons
.DEFAULT_EXPERIMENT_ICON
: icon
);
266 public String
getLabelText() {
267 return getName() + " [" + getTraces().size() + "]"; //$NON-NLS-1$ //$NON-NLS-2$
270 // ------------------------------------------------------------------------
272 // ------------------------------------------------------------------------
275 * Refreshes the trace type filed by reading the trace type persistent
276 * property of the resource reference.
278 * If trace type is null after refresh, set it to the generic trace type
279 * (for seamless upgrade)
282 public void refreshTraceType() {
283 super.refreshTraceType();
284 if (getTraceType() == null) {
285 IConfigurationElement ce
= TmfTraceType
.getTraceAttributes(TmfTraceType
.DEFAULT_EXPERIMENT_TYPE
);
288 IFolder experimentFolder
= getResource();
289 experimentFolder
.setPersistentProperty(TmfCommonConstants
.TRACETYPE
, ce
.getAttribute(TmfTraceType
.ID_ATTR
));
290 super.refreshTraceType();
291 } catch (InvalidRegistryObjectException
| CoreException e
) {
298 * Returns a list of TmfTraceElements contained in this experiment.
300 * @return a list of TmfTraceElements
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
);
315 * Adds a trace to the experiment
318 * The trace element to add
320 public void addTrace(TmfTraceElement trace
) {
321 addTrace(trace
, true);
325 * Adds a trace to the experiment
328 * The trace element to add
330 * Flag for refreshing the project
332 public void addTrace(TmfTraceElement trace
, boolean refresh
) {
334 * Create a link to the actual trace and set the trace type
336 IFolder experiment
= getResource();
337 IResource resource
= trace
.getResource();
338 IPath location
= resource
.getLocation();
339 IWorkspace workspace
= ResourcesPlugin
.getWorkspace();
341 String traceTypeId
= TmfTraceType
.getTraceTypeId(trace
.getResource());
342 TraceTypeHelper traceType
= TmfTraceType
.getTraceType(traceTypeId
);
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
);
355 Activator
.getDefault().logError("Error creating link. Invalid trace location " + location
); //$NON-NLS-1$
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
);
367 Activator
.getDefault().logError("Error creating link. Invalid trace location " + location
); //$NON-NLS-1$
370 } catch (CoreException e
) {
371 Activator
.getDefault().logError("Error creating link to location " + location
, e
); //$NON-NLS-1$
377 * Removes a trace from an experiment
380 * The trace to remove
381 * @throws CoreException
384 public void removeTrace(TmfTraceElement trace
) throws CoreException
{
386 // Close editors in UI Thread
387 Display
.getDefault().syncExec(new Runnable() {
394 /* Finally, remove the trace from experiment */
396 deleteTraceResource(trace
.getResource());
397 deleteSupplementaryResources();
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
);
410 public IFile
createBookmarksFile() throws CoreException
{
411 return createBookmarksFile(getProject().getExperimentsFolder().getResource(), ITmfEventsEditorConstants
.EXPERIMENT_EDITOR_INPUT_TYPE
);
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());
420 IConfigurationElement
[] defaultEditorCE
= ce
.getChildren(TmfTraceTypeUIUtils
.DEFAULT_EDITOR_ELEM
);
421 if (defaultEditorCE
.length
== 1) {
422 return defaultEditorCE
[0].getAttribute(TmfTraceType
.ID_ATTR
);
427 /* No default editor, try to find a common editor for all traces */
428 final List
<TmfTraceElement
> traceEntries
= getTraces();
429 String commonEditorId
= null;
431 for (TmfTraceElement element
: traceEntries
) {
432 // If all traces use the same editorId, use it, otherwise use the
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
;
445 * Instantiate a {@link TmfExperiment} object based on the experiment type
446 * and the corresponding extension.
448 * @return the {@link TmfExperiment} or <code>null</code> for an error
451 public TmfExperiment
instantiateTrace() {
454 // make sure that supplementary folder exists
455 refreshSupplementaryFolder();
457 if (getTraceType() != null) {
459 IConfigurationElement ce
= TRACE_TYPE_ATTRIBUTES
.get(getTraceType());
463 TmfExperiment experiment
= (TmfExperiment
) ce
.createExecutableExtension(TmfTraceType
.EXPERIMENT_TYPE_ATTR
);
466 } catch (CoreException e
) {
467 Activator
.getDefault().logError(NLS
.bind(Messages
.TmfExperimentElement_ErrorInstantiatingTrace
, getName()), e
);
473 public String
getTypeName() {
474 return Messages
.TmfExperimentElement_TypeName
;
477 // ------------------------------------------------------------------------
479 // ------------------------------------------------------------------------
482 public Object
getEditableValue() {
487 public IPropertyDescriptor
[] getPropertyDescriptors() {
488 return Arrays
.copyOf(DESCRIPTORS
, DESCRIPTORS
.length
);
492 public Object
getPropertyValue(Object id
) {
494 if (NAME
.equals(id
)) {
498 if (PATH
.equals(id
)) {
499 return getPath().toString();
502 if (LOCATION
.equals(id
)) {
503 return getLocation().toString();
506 if (EXPERIMENT_TYPE
.equals(id
)) {
507 if (getTraceType() != null) {
508 IConfigurationElement ce
= TRACE_TYPE_ATTRIBUTES
.get(getTraceType());
510 return ""; //$NON-NLS-1$
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
);
519 return ce
.getAttribute(TmfTraceType
.NAME_ATTR
);
527 public void resetPropertyValue(Object id
) {
531 public void setPropertyValue(Object id
, Object value
) {
535 public boolean isPropertyResettable(Object id
) {
540 public boolean isPropertySet(Object id
) {
545 * Return the suffix for resource names
547 * @return The folder suffix
550 public String
getSuffix() {
551 return FOLDER_SUFFIX
;