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
.osgi
.util
.NLS
;
42 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.Activator
;
43 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.editors
.ITmfEventsEditorConstants
;
44 import org
.eclipse
.tracecompass
.tmf
.core
.TmfCommonConstants
;
45 import org
.eclipse
.tracecompass
.tmf
.core
.project
.model
.TmfTraceType
;
46 import org
.eclipse
.tracecompass
.tmf
.core
.project
.model
.TraceTypeHelper
;
47 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.experiment
.TmfExperiment
;
48 import org
.eclipse
.tracecompass
.tmf
.ui
.editors
.TmfEventsEditor
;
49 import org
.eclipse
.tracecompass
.tmf
.ui
.properties
.ReadOnlyTextPropertyDescriptor
;
50 import org
.eclipse
.ui
.views
.properties
.IPropertyDescriptor
;
51 import org
.eclipse
.ui
.views
.properties
.IPropertySource2
;
54 * Implementation of TMF Experiment Model Element.
57 * @author Francois Chouinard
60 public class TmfExperimentElement
extends TmfCommonProjectElement
implements IPropertySource2
{
62 // ------------------------------------------------------------------------
64 // ------------------------------------------------------------------------
66 // Property View stuff
67 private static final String sfInfoCategory
= "Info"; //$NON-NLS-1$
68 private static final String sfName
= "name"; //$NON-NLS-1$
69 private static final String sfPath
= "path"; //$NON-NLS-1$
70 private static final String sfLocation
= "location"; //$NON-NLS-1$
71 private static final String sfFolderSuffix
= "_exp"; //$NON-NLS-1$
72 private static final String sfExperimentType
= "type"; //$NON-NLS-1$
74 private static final ReadOnlyTextPropertyDescriptor sfNameDescriptor
= new ReadOnlyTextPropertyDescriptor(sfName
, sfName
);
75 private static final ReadOnlyTextPropertyDescriptor sfPathDescriptor
= new ReadOnlyTextPropertyDescriptor(sfPath
, sfPath
);
76 private static final ReadOnlyTextPropertyDescriptor sfLocationDescriptor
= new ReadOnlyTextPropertyDescriptor(sfLocation
,
78 private static final ReadOnlyTextPropertyDescriptor sfTypeDescriptor
= new ReadOnlyTextPropertyDescriptor(sfExperimentType
, sfExperimentType
);
80 private static final IPropertyDescriptor
[] sfDescriptors
= { sfNameDescriptor
, sfPathDescriptor
,
81 sfLocationDescriptor
, sfTypeDescriptor
};
84 sfNameDescriptor
.setCategory(sfInfoCategory
);
85 sfPathDescriptor
.setCategory(sfInfoCategory
);
86 sfLocationDescriptor
.setCategory(sfInfoCategory
);
87 sfTypeDescriptor
.setCategory(sfInfoCategory
);
90 // The mapping of available trace type IDs to their corresponding
91 // configuration element
92 private static final Map
<String
, IConfigurationElement
> sfTraceTypeAttributes
= new HashMap
<>();
93 private static final Map
<String
, IConfigurationElement
> sfTraceTypeUIAttributes
= new HashMap
<>();
94 private static final Map
<String
, IConfigurationElement
> sfTraceCategories
= new HashMap
<>();
96 // ------------------------------------------------------------------------
97 // Static initialization
98 // ------------------------------------------------------------------------
101 * Initialize statically at startup by getting extensions from the platform
102 * extension registry.
104 public static void init() {
105 IConfigurationElement
[] config
= Platform
.getExtensionRegistry().getConfigurationElementsFor(TmfTraceType
.TMF_TRACE_TYPE_ID
);
106 for (IConfigurationElement ce
: config
) {
107 String elementName
= ce
.getName();
108 if (elementName
.equals(TmfTraceType
.EXPERIMENT_ELEM
)) {
109 String traceTypeId
= ce
.getAttribute(TmfTraceType
.ID_ATTR
);
110 sfTraceTypeAttributes
.put(traceTypeId
, ce
);
111 } else if (elementName
.equals(TmfTraceType
.CATEGORY_ELEM
)) {
112 String categoryId
= ce
.getAttribute(TmfTraceType
.ID_ATTR
);
113 sfTraceCategories
.put(categoryId
, ce
);
118 * Read the corresponding tmf.ui "tracetypeui" extension point for this
119 * trace type, if it exists.
121 config
= Platform
.getExtensionRegistry().getConfigurationElementsFor(TmfTraceTypeUIUtils
.TMF_TRACE_TYPE_UI_ID
);
122 for (IConfigurationElement ce
: config
) {
123 String elemName
= ce
.getName();
124 if (TmfTraceTypeUIUtils
.EXPERIMENT_ELEM
.equals(elemName
)) {
125 String traceType
= ce
.getAttribute(TmfTraceTypeUIUtils
.TRACETYPE_ATTR
);
126 sfTraceTypeUIAttributes
.put(traceType
, ce
);
131 // ------------------------------------------------------------------------
133 // ------------------------------------------------------------------------
136 * @param name The name of the experiment
137 * @param folder The folder reference
138 * @param parent The experiment folder reference.
140 public TmfExperimentElement(String name
, IFolder folder
, TmfExperimentFolder parent
) {
141 super(name
, folder
, parent
);
144 // ------------------------------------------------------------------------
145 // TmfProjectModelElement
146 // ------------------------------------------------------------------------
149 public IFolder
getResource() {
150 return (IFolder
) fResource
;
154 void refreshChildren() {
155 IFolder folder
= getResource();
157 /* Update the trace children of this experiment */
158 // Get the children from the model
159 Map
<String
, ITmfProjectModelElement
> childrenMap
= new HashMap
<>();
160 for (TmfTraceElement trace
: getTraces()) {
161 childrenMap
.put(trace
.getElementPath(), trace
);
164 List
<IResource
> members
= getTraceResources();
165 for (IResource resource
: members
) {
166 String name
= resource
.getName();
167 String elementPath
= resource
.getFullPath().makeRelativeTo(folder
.getFullPath()).toString();
168 ITmfProjectModelElement element
= childrenMap
.get(elementPath
);
169 if (element
instanceof TmfTraceElement
) {
170 childrenMap
.remove(elementPath
);
172 element
= new TmfTraceElement(name
, resource
, this);
176 // Cleanup dangling children from the model
177 for (ITmfProjectModelElement danglingChild
: childrenMap
.values()) {
178 removeChild(danglingChild
);
181 /* Update the analysis under this experiment */
182 super.refreshChildren();
185 private List
<IResource
> getTraceResources() {
186 IFolder folder
= getResource();
187 final List
<IResource
> list
= new ArrayList
<>();
189 folder
.accept(new IResourceProxyVisitor() {
191 public boolean visit(IResourceProxy resource
) throws CoreException
{
192 if (resource
.isLinked()) {
193 list
.add(resource
.requestResource());
198 } catch (CoreException e
) {
200 Comparator
<IResource
> comparator
= new Comparator
<IResource
>() {
202 public int compare(IResource o1
, IResource o2
) {
203 return o1
.getFullPath().toString().compareTo(o2
.getFullPath().toString());
206 Collections
.sort(list
, comparator
);
210 // ------------------------------------------------------------------------
212 // ------------------------------------------------------------------------
215 * Refreshes the trace type filed by reading the trace type persistent
216 * property of the resource reference.
218 * If trace type is null after refresh, set it to the generic trace type
219 * (for seamless upgrade)
222 public void refreshTraceType() {
223 super.refreshTraceType();
224 if (getTraceType() == null) {
225 IConfigurationElement ce
= TmfTraceType
.getTraceAttributes(TmfTraceType
.DEFAULT_EXPERIMENT_TYPE
);
228 IFolder experimentFolder
= getResource();
229 experimentFolder
.setPersistentProperty(TmfCommonConstants
.TRACETYPE
, ce
.getAttribute(TmfTraceType
.ID_ATTR
));
230 super.refreshTraceType();
231 } catch (InvalidRegistryObjectException
| CoreException e
) {
238 * Returns a list of TmfTraceElements contained in this experiment.
239 * @return a list of TmfTraceElements
242 public List
<TmfTraceElement
> getTraces() {
243 List
<ITmfProjectModelElement
> children
= getChildren();
244 List
<TmfTraceElement
> traces
= new ArrayList
<>();
245 for (ITmfProjectModelElement child
: children
) {
246 if (child
instanceof TmfTraceElement
) {
247 traces
.add((TmfTraceElement
) child
);
254 * Adds a trace to the experiment
256 * @param trace The trace element to add
258 public void addTrace(TmfTraceElement trace
) {
259 addTrace(trace
, true);
263 * Adds a trace to the experiment
265 * @param trace The trace element to add
266 * @param refresh Flag for refreshing the project
268 public void addTrace(TmfTraceElement trace
, boolean refresh
) {
270 * Create a link to the actual trace and set the trace type
272 IFolder experiment
= getResource();
273 IResource resource
= trace
.getResource();
274 IPath location
= resource
.getLocation();
275 IWorkspace workspace
= ResourcesPlugin
.getWorkspace();
277 String traceTypeId
= TmfTraceType
.getTraceTypeId(trace
.getResource());
278 TraceTypeHelper traceType
= TmfTraceType
.getTraceType(traceTypeId
);
280 if (resource
instanceof IFolder
) {
281 IFolder folder
= experiment
.getFolder(trace
.getElementPath());
282 TraceUtils
.createFolder((IFolder
) folder
.getParent(), new NullProgressMonitor());
283 IStatus result
= workspace
.validateLinkLocation(folder
, location
);
284 if (result
.isOK() || result
.matches(IStatus
.INFO
| IStatus
.WARNING
)) {
285 folder
.createLink(location
, IResource
.REPLACE
, null);
286 if (traceType
!= null) {
287 TmfTraceTypeUIUtils
.setTraceType(folder
, traceType
, refresh
);
291 Activator
.getDefault().logError("Error creating link. Invalid trace location " + location
); //$NON-NLS-1$
294 IFile file
= experiment
.getFile(trace
.getElementPath());
295 TraceUtils
.createFolder((IFolder
) file
.getParent(), new NullProgressMonitor());
296 IStatus result
= workspace
.validateLinkLocation(file
, location
);
297 if (result
.isOK() || result
.matches(IStatus
.INFO
| IStatus
.WARNING
)) {
298 file
.createLink(location
, IResource
.REPLACE
, null);
299 if (traceType
!= null) {
300 TmfTraceTypeUIUtils
.setTraceType(file
, traceType
, refresh
);
303 Activator
.getDefault().logError("Error creating link. Invalid trace location " + location
); //$NON-NLS-1$
306 } catch (CoreException e
) {
307 Activator
.getDefault().logError("Error creating link to location " + location
, e
); //$NON-NLS-1$
313 * Removes a trace from an experiment
315 * @param trace The trace to remove
316 * @throws CoreException exception
318 public void removeTrace(TmfTraceElement trace
) throws CoreException
{
320 // Close the experiment if open
323 /* Finally, remove the trace from experiment*/
325 deleteTraceResource(trace
.getResource());
326 deleteSupplementaryResources();
329 private void deleteTraceResource(IResource resource
) throws CoreException
{
330 resource
.delete(true, null);
331 IContainer parent
= resource
.getParent();
332 // delete empty folders up to the parent experiment folder
333 if (!parent
.equals(getResource()) && parent
.members().length
== 0) {
334 deleteTraceResource(parent
);
339 public IFile
createBookmarksFile() throws CoreException
{
340 return createBookmarksFile(getProject().getExperimentsFolder().getResource(), ITmfEventsEditorConstants
.EXPERIMENT_EDITOR_INPUT_TYPE
);
344 public String
getEditorId() {
345 /* See if a default editor was set for this experiment type */
346 if (getTraceType() != null) {
347 IConfigurationElement ce
= sfTraceTypeUIAttributes
.get(getTraceType());
349 IConfigurationElement
[] defaultEditorCE
= ce
.getChildren(TmfTraceTypeUIUtils
.DEFAULT_EDITOR_ELEM
);
350 if (defaultEditorCE
.length
== 1) {
351 return defaultEditorCE
[0].getAttribute(TmfTraceType
.ID_ATTR
);
356 /* No default editor, try to find a common editor for all traces */
357 final List
<TmfTraceElement
> traceEntries
= getTraces();
358 String commonEditorId
= null;
360 for (TmfTraceElement element
: traceEntries
) {
361 // If all traces use the same editorId, use it, otherwise use the
363 final String editorId
= element
.getEditorId();
364 if (commonEditorId
== null) {
365 commonEditorId
= (editorId
!= null) ? editorId
: TmfEventsEditor
.ID
;
366 } else if (!commonEditorId
.equals(editorId
)) {
367 commonEditorId
= TmfEventsEditor
.ID
;
374 * Instantiate a {@link TmfExperiment} object based on the experiment type
375 * and the corresponding extension.
377 * @return the {@link TmfExperiment} or <code>null</code> for an error
380 public TmfExperiment
instantiateTrace() {
383 // make sure that supplementary folder exists
384 refreshSupplementaryFolder();
386 if (getTraceType() != null) {
388 IConfigurationElement ce
= sfTraceTypeAttributes
.get(getTraceType());
392 TmfExperiment experiment
= (TmfExperiment
) ce
.createExecutableExtension(TmfTraceType
.EXPERIMENT_TYPE_ATTR
);
395 } catch (CoreException e
) {
396 Activator
.getDefault().logError(NLS
.bind(Messages
.TmfExperimentElement_ErrorInstantiatingTrace
, getName()), e
);
402 public String
getTypeName() {
403 return Messages
.TmfExperimentElement_TypeName
;
406 // ------------------------------------------------------------------------
408 // ------------------------------------------------------------------------
411 public Object
getEditableValue() {
416 public IPropertyDescriptor
[] getPropertyDescriptors() {
417 return Arrays
.copyOf(sfDescriptors
, sfDescriptors
.length
);
421 public Object
getPropertyValue(Object id
) {
423 if (sfName
.equals(id
)) {
427 if (sfPath
.equals(id
)) {
428 return getPath().toString();
431 if (sfLocation
.equals(id
)) {
432 return getLocation().toString();
435 if (sfExperimentType
.equals(id
)) {
436 if (getTraceType() != null) {
437 IConfigurationElement ce
= sfTraceTypeAttributes
.get(getTraceType());
439 return ""; //$NON-NLS-1$
441 String categoryId
= ce
.getAttribute(TmfTraceType
.CATEGORY_ATTR
);
442 if (categoryId
!= null) {
443 IConfigurationElement category
= sfTraceCategories
.get(categoryId
);
444 if (category
!= null) {
445 return category
.getAttribute(TmfTraceType
.NAME_ATTR
) + ':' + ce
.getAttribute(TmfTraceType
.NAME_ATTR
);
448 return ce
.getAttribute(TmfTraceType
.NAME_ATTR
);
456 public void resetPropertyValue(Object id
) {
460 public void setPropertyValue(Object id
, Object value
) {
464 public boolean isPropertyResettable(Object id
) {
469 public boolean isPropertySet(Object id
) {
474 * Return the suffix for resource names
475 * @return The folder suffix
478 public String
getSuffix() {
479 return sfFolderSuffix
;