1 /*******************************************************************************
2 * Copyright (c) 2010, 2014 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 * Bernd Hufmann - Added supplementary files handling
12 * Geneviève Bastien - Moved supplementary files handling to parent class,
13 * added code to copy trace
14 * Patrick Tasse - Close editors to release resources
15 * Jean-Christian Kouame - added trace properties to be shown into
17 * Geneviève Bastien - Moved trace type related methods to parent class
18 *******************************************************************************/
20 package org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
;
22 import java
.util
.Arrays
;
23 import java
.util
.HashMap
;
24 import java
.util
.LinkedList
;
25 import java
.util
.List
;
28 import org
.eclipse
.core
.resources
.IFile
;
29 import org
.eclipse
.core
.resources
.IFolder
;
30 import org
.eclipse
.core
.resources
.IResource
;
31 import org
.eclipse
.core
.runtime
.CoreException
;
32 import org
.eclipse
.core
.runtime
.IConfigurationElement
;
33 import org
.eclipse
.core
.runtime
.IPath
;
34 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
35 import org
.eclipse
.core
.runtime
.Platform
;
36 import org
.eclipse
.core
.runtime
.URIUtil
;
37 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Activator
;
38 import org
.eclipse
.linuxtools
.tmf
.core
.TmfCommonConstants
;
39 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
40 import org
.eclipse
.linuxtools
.tmf
.core
.parsers
.custom
.CustomTxtEvent
;
41 import org
.eclipse
.linuxtools
.tmf
.core
.parsers
.custom
.CustomTxtTrace
;
42 import org
.eclipse
.linuxtools
.tmf
.core
.parsers
.custom
.CustomTxtTraceDefinition
;
43 import org
.eclipse
.linuxtools
.tmf
.core
.parsers
.custom
.CustomXmlEvent
;
44 import org
.eclipse
.linuxtools
.tmf
.core
.parsers
.custom
.CustomXmlTrace
;
45 import org
.eclipse
.linuxtools
.tmf
.core
.parsers
.custom
.CustomXmlTraceDefinition
;
46 import org
.eclipse
.linuxtools
.tmf
.core
.project
.model
.TmfTraceType
;
47 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
48 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTraceProperties
;
49 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTrace
;
50 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTraceManager
;
51 import org
.eclipse
.linuxtools
.tmf
.ui
.editors
.TmfEventsEditor
;
52 import org
.eclipse
.linuxtools
.tmf
.ui
.properties
.ReadOnlyTextPropertyDescriptor
;
53 import org
.eclipse
.ui
.IActionFilter
;
54 import org
.eclipse
.ui
.views
.properties
.IPropertyDescriptor
;
55 import org
.eclipse
.ui
.views
.properties
.IPropertySource2
;
58 * Implementation of trace model element representing a trace. It provides
59 * methods to instantiate <code>ITmfTrace</code> and <code>ITmfEvent</code> as
60 * well as editor ID from the trace type extension definition.
63 * @author Francois Chouinard
65 public class TmfTraceElement
extends TmfCommonProjectElement
implements IActionFilter
, IPropertySource2
{
67 // ------------------------------------------------------------------------
69 // ------------------------------------------------------------------------
73 * Bundle attribute name
75 public static final String BUNDLE
= "bundle"; //$NON-NLS-1$
77 * IsLinked attribute name.
79 public static final String IS_LINKED
= "isLinked"; //$NON-NLS-1$
81 // Property View stuff
82 private static final String sfResourcePropertiesCategory
= Messages
.TmfTraceElement_ResourceProperties
;
83 private static final String sfName
= Messages
.TmfTraceElement_Name
;
84 private static final String sfPath
= Messages
.TmfTraceElement_Path
;
85 private static final String sfLocation
= Messages
.TmfTraceElement_Location
;
86 private static final String sfEventType
= Messages
.TmfTraceElement_EventType
;
87 private static final String sfIsLinked
= Messages
.TmfTraceElement_IsLinked
;
88 private static final String sfSourceLocation
= Messages
.TmfTraceElement_SourceLocation
;
89 private static final String sfTracePropertiesCategory
= Messages
.TmfTraceElement_TraceProperties
;
91 private static final ReadOnlyTextPropertyDescriptor sfNameDescriptor
= new ReadOnlyTextPropertyDescriptor(sfName
, sfName
);
92 private static final ReadOnlyTextPropertyDescriptor sfPathDescriptor
= new ReadOnlyTextPropertyDescriptor(sfPath
, sfPath
);
93 private static final ReadOnlyTextPropertyDescriptor sfLocationDescriptor
= new ReadOnlyTextPropertyDescriptor(sfLocation
, sfLocation
);
94 private static final ReadOnlyTextPropertyDescriptor sfTypeDescriptor
= new ReadOnlyTextPropertyDescriptor(sfEventType
, sfEventType
);
95 private static final ReadOnlyTextPropertyDescriptor sfIsLinkedDescriptor
= new ReadOnlyTextPropertyDescriptor(sfIsLinked
, sfIsLinked
);
96 private static final ReadOnlyTextPropertyDescriptor sfSourceLocationDescriptor
= new ReadOnlyTextPropertyDescriptor(sfSourceLocation
, sfSourceLocation
);
98 private static final IPropertyDescriptor
[] sfDescriptors
= { sfNameDescriptor
, sfPathDescriptor
, sfLocationDescriptor
,
99 sfTypeDescriptor
, sfIsLinkedDescriptor
, sfSourceLocationDescriptor
};
102 sfNameDescriptor
.setCategory(sfResourcePropertiesCategory
);
103 sfPathDescriptor
.setCategory(sfResourcePropertiesCategory
);
104 sfLocationDescriptor
.setCategory(sfResourcePropertiesCategory
);
105 sfTypeDescriptor
.setCategory(sfResourcePropertiesCategory
);
106 sfIsLinkedDescriptor
.setCategory(sfResourcePropertiesCategory
);
107 sfSourceLocationDescriptor
.setCategory(sfResourcePropertiesCategory
);
110 // ------------------------------------------------------------------------
111 // Static initialization
112 // ------------------------------------------------------------------------
114 // The mapping of available trace type IDs to their corresponding
115 // configuration element
116 private static final Map
<String
, IConfigurationElement
> sfTraceTypeAttributes
= new HashMap
<>();
117 private static final Map
<String
, IConfigurationElement
> sfTraceTypeUIAttributes
= new HashMap
<>();
118 private static final Map
<String
, IConfigurationElement
> sfTraceCategories
= new HashMap
<>();
121 * Initialize statically at startup by getting extensions from the platform
122 * extension registry.
124 public static void init() {
125 /* Read the tmf.core "tracetype" extension point */
126 IConfigurationElement
[] config
= Platform
.getExtensionRegistry().getConfigurationElementsFor(TmfTraceType
.TMF_TRACE_TYPE_ID
);
127 for (IConfigurationElement ce
: config
) {
128 switch (ce
.getName()) {
129 case TmfTraceType
.TYPE_ELEM
:
130 String traceTypeId
= ce
.getAttribute(TmfTraceType
.ID_ATTR
);
131 sfTraceTypeAttributes
.put(traceTypeId
, ce
);
133 case TmfTraceType
.CATEGORY_ELEM
:
134 String categoryId
= ce
.getAttribute(TmfTraceType
.ID_ATTR
);
135 sfTraceCategories
.put(categoryId
, ce
);
142 * Read the corresponding tmf.ui "tracetypeui" extension point for this
143 * trace type, if it exists.
145 config
= Platform
.getExtensionRegistry().getConfigurationElementsFor(TmfTraceTypeUIUtils
.TMF_TRACE_TYPE_UI_ID
);
146 for (IConfigurationElement ce
: config
) {
147 String elemName
= ce
.getName();
148 if (TmfTraceTypeUIUtils
.TYPE_ELEM
.equals(elemName
)) {
149 String traceType
= ce
.getAttribute(TmfTraceTypeUIUtils
.TRACETYPE_ATTR
);
150 sfTraceTypeUIAttributes
.put(traceType
, ce
);
155 // ------------------------------------------------------------------------
157 // ------------------------------------------------------------------------
159 * Constructor. Creates trace model element under the trace folder.
164 * The trace resource.
166 * The parent element (trace folder)
168 public TmfTraceElement(String name
, IResource trace
, TmfTraceFolder parent
) {
169 this(name
, trace
, (TmfProjectModelElement
) parent
);
173 * Constructor. Creates trace model element under the experiment folder.
178 * The trace resource.
180 * The parent element (experiment folder)
182 public TmfTraceElement(String name
, IResource trace
, TmfExperimentElement parent
) {
183 this(name
, trace
, (TmfProjectModelElement
) parent
);
186 private TmfTraceElement(String name
, IResource trace
, TmfProjectModelElement parent
) {
187 super(name
, trace
, parent
);
190 // ------------------------------------------------------------------------
192 // ------------------------------------------------------------------------
195 * Instantiate a <code>ITmfTrace</code> object based on the trace type and
196 * the corresponding extension.
198 * @return the <code>ITmfTrace</code> or <code>null</code> for an error
201 public ITmfTrace
instantiateTrace() {
204 // make sure that supplementary folder exists
205 refreshSupplementaryFolder();
207 if (getTraceType() != null) {
208 if (getTraceType().startsWith(CustomTxtTrace
.class.getCanonicalName())) {
209 for (CustomTxtTraceDefinition def
: CustomTxtTraceDefinition
.loadAll()) {
210 if (getTraceType().equals(CustomTxtTrace
.class.getCanonicalName() + ":" + def
.definitionName
)) { //$NON-NLS-1$
211 return new CustomTxtTrace(def
);
215 if (getTraceType().startsWith(CustomXmlTrace
.class.getCanonicalName())) {
216 for (CustomXmlTraceDefinition def
: CustomXmlTraceDefinition
.loadAll()) {
217 if (getTraceType().equals(CustomXmlTrace
.class.getCanonicalName() + ":" + def
.definitionName
)) { //$NON-NLS-1$
218 return new CustomXmlTrace(def
);
222 IConfigurationElement ce
= sfTraceTypeAttributes
.get(getTraceType());
226 ITmfTrace trace
= (ITmfTrace
) ce
.createExecutableExtension(TmfTraceType
.TRACE_TYPE_ATTR
);
229 } catch (CoreException e
) {
230 Activator
.getDefault().logError("Error instantiating ITmfTrace object for trace " + getName(), e
); //$NON-NLS-1$
236 * Instantiate a <code>ITmfEvent</code> object based on the trace type and
237 * the corresponding extension.
239 * @return the <code>ITmfEvent</code> or <code>null</code> for an error
241 public ITmfEvent
instantiateEvent() {
243 if (getTraceType() != null) {
244 if (getTraceType().startsWith(CustomTxtTrace
.class.getCanonicalName())) {
245 for (CustomTxtTraceDefinition def
: CustomTxtTraceDefinition
.loadAll()) {
246 if (getTraceType().equals(CustomTxtTrace
.class.getCanonicalName() + ":" + def
.definitionName
)) { //$NON-NLS-1$
247 return new CustomTxtEvent(def
);
251 if (getTraceType().startsWith(CustomXmlTrace
.class.getCanonicalName())) {
252 for (CustomXmlTraceDefinition def
: CustomXmlTraceDefinition
.loadAll()) {
253 if (getTraceType().equals(CustomXmlTrace
.class.getCanonicalName() + ":" + def
.definitionName
)) { //$NON-NLS-1$
254 return new CustomXmlEvent(def
);
258 IConfigurationElement ce
= sfTraceTypeAttributes
.get(getTraceType());
262 ITmfEvent event
= (ITmfEvent
) ce
.createExecutableExtension(TmfTraceType
.EVENT_TYPE_ATTR
);
265 } catch (CoreException e
) {
266 Activator
.getDefault().logError("Error instantiating ITmfEvent object for trace " + getName(), e
); //$NON-NLS-1$
272 public String
getEditorId() {
273 if (getTraceType() != null) {
274 if (getTraceType().startsWith(CustomTxtTrace
.class.getCanonicalName())) {
275 return TmfEventsEditor
.ID
;
277 if (getTraceType().startsWith(CustomXmlTrace
.class.getCanonicalName())) {
278 return TmfEventsEditor
.ID
;
280 IConfigurationElement ce
= sfTraceTypeUIAttributes
.get(getTraceType());
282 /* This trace type does not define UI attributes */
285 IConfigurationElement
[] defaultEditorCE
= ce
.getChildren(TmfTraceTypeUIUtils
.DEFAULT_EDITOR_ELEM
);
286 if (defaultEditorCE
.length
== 1) {
287 return defaultEditorCE
[0].getAttribute(TmfTraceType
.ID_ATTR
);
294 * Returns the file resource used to store bookmarks after creating it if
295 * necessary. If the trace resource is a file, it is returned directly. If
296 * the trace resource is a folder, a linked file is returned. The file will
297 * be created if it does not exist.
299 * @return the bookmarks file
300 * @throws CoreException
301 * if the bookmarks file cannot be created
305 public IFile
createBookmarksFile() throws CoreException
{
306 IFile file
= getBookmarksFile();
307 if (fResource
instanceof IFolder
) {
308 return createBookmarksFile(getProject().getTracesFolder().getResource(), TmfTrace
.class.getCanonicalName());
314 * Returns the file resource used to store bookmarks. The file may not
317 * @return the bookmarks file
321 public IFile
getBookmarksFile() {
323 if (fResource
instanceof IFile
) {
324 file
= (IFile
) fResource
;
325 } else if (fResource
instanceof IFolder
) {
326 final IFolder folder
= (IFolder
) fResource
;
327 file
= folder
.getFile(getName() + '_');
333 * Returns the <code>TmfTraceElement</code> located under the
334 * <code>TmfTracesFolder</code>.
336 * @return <code>this</code> if this element is under the
337 * <code>TmfTracesFolder</code> else the corresponding
338 * <code>TmfTraceElement</code> if this element is under
339 * <code>TmfExperimentElement</code>.
341 public TmfTraceElement
getElementUnderTraceFolder() {
343 // If trace is under an experiment, return original trace from the
345 if (getParent() instanceof TmfExperimentElement
) {
346 for (TmfTraceElement aTrace
: getProject().getTracesFolder().getTraces()) {
347 if (aTrace
.getName().equals(getName())) {
356 public String
getTypeName() {
357 return Messages
.TmfTraceElement_TypeName
;
360 // ------------------------------------------------------------------------
362 // ------------------------------------------------------------------------
365 public boolean testAttribute(Object target
, String name
, String value
) {
366 if (name
.equals(IS_LINKED
)) {
367 boolean isLinked
= getResource().isLinked();
368 return Boolean
.toString(isLinked
).equals(value
);
373 // ------------------------------------------------------------------------
375 // ------------------------------------------------------------------------
378 public TmfProjectElement
getProject() {
379 if (getParent() instanceof TmfTraceFolder
) {
380 TmfTraceFolder folder
= (TmfTraceFolder
) getParent();
381 TmfProjectElement project
= (TmfProjectElement
) folder
.getParent();
384 if (getParent() instanceof TmfExperimentElement
) {
385 TmfExperimentElement experiment
= (TmfExperimentElement
) getParent();
386 TmfExperimentFolder folder
= (TmfExperimentFolder
) experiment
.getParent();
387 TmfProjectElement project
= (TmfProjectElement
) folder
.getParent();
393 // ------------------------------------------------------------------------
395 // ------------------------------------------------------------------------
398 public Object
getEditableValue() {
403 * Get the trace properties of this traceElement if the corresponding trace
404 * is opened in an editor
406 * @return a map with the names and values of the trace properties
407 * respectively as keys and values
409 private Map
<String
, String
> getTraceProperties() {
410 for (ITmfTrace openedTrace
: TmfTraceManager
.getInstance().getOpenedTraces()) {
411 for (ITmfTrace singleTrace
: TmfTraceManager
.getTraceSet(openedTrace
)) {
412 if (this.getLocation().getPath().endsWith(singleTrace
.getPath())) {
413 if (singleTrace
instanceof ITmfTraceProperties
) {
414 ITmfTraceProperties traceProperties
= (ITmfTraceProperties
) singleTrace
;
415 return traceProperties
.getTraceProperties();
420 return new HashMap
<>();
424 public IPropertyDescriptor
[] getPropertyDescriptors() {
425 Map
<String
, String
> traceProperties
= getTraceProperties();
426 if (!traceProperties
.isEmpty()) {
427 IPropertyDescriptor
[] propertyDescriptorArray
= new IPropertyDescriptor
[traceProperties
.size() + sfDescriptors
.length
];
429 for (Map
.Entry
<String
, String
> varName
: traceProperties
.entrySet()) {
430 ReadOnlyTextPropertyDescriptor descriptor
= new ReadOnlyTextPropertyDescriptor(this.getName() + "_" + varName
.getKey(), varName
.getKey()); //$NON-NLS-1$
431 descriptor
.setCategory(sfTracePropertiesCategory
);
432 propertyDescriptorArray
[index
] = descriptor
;
435 for (int i
= 0; i
< sfDescriptors
.length
; i
++) {
436 propertyDescriptorArray
[index
] = sfDescriptors
[i
];
439 return propertyDescriptorArray
;
441 return Arrays
.copyOf(sfDescriptors
, sfDescriptors
.length
);
445 public Object
getPropertyValue(Object id
) {
447 if (sfName
.equals(id
)) {
451 if (sfPath
.equals(id
)) {
452 return getPath().toString();
455 if (sfLocation
.equals(id
)) {
456 return URIUtil
.toUnencodedString(getLocation());
459 if (sfIsLinked
.equals(id
)) {
460 return Boolean
.valueOf(getResource().isLinked()).toString();
463 if (sfSourceLocation
.equals(id
)) {
465 String sourceLocation
= getElementUnderTraceFolder().getResource().getPersistentProperty(TmfCommonConstants
.SOURCE_LOCATION
);
466 if (sourceLocation
!= null) {
467 return sourceLocation
;
469 } catch (CoreException e
) {
471 return ""; //$NON-NLS-1$
474 if (sfEventType
.equals(id
)) {
475 if (getTraceType() != null) {
476 IConfigurationElement ce
= sfTraceTypeAttributes
.get(getTraceType());
477 return (ce
!= null) ?
(getCategory(ce
) + " : " + ce
.getAttribute(TmfTraceType
.NAME_ATTR
)) : ""; //$NON-NLS-1$ //$NON-NLS-2$
481 Map
<String
, String
> traceProperties
= getTraceProperties();
482 if (id
!= null && !traceProperties
.isEmpty()) {
483 String key
= (String
) id
;
484 key
= key
.substring(this.getName().length() + 1); // remove name_
485 String value
= traceProperties
.get(key
);
492 private static String
getCategory(IConfigurationElement ce
) {
493 String categoryId
= ce
.getAttribute(TmfTraceType
.CATEGORY_ATTR
);
494 if (categoryId
!= null) {
495 IConfigurationElement category
= sfTraceCategories
.get(categoryId
);
496 if (category
!= null) {
497 return category
.getAttribute(TmfTraceType
.NAME_ATTR
);
500 return "[no category]"; //$NON-NLS-1$
504 public void resetPropertyValue(Object id
) {
508 public void setPropertyValue(Object id
, Object value
) {
512 public boolean isPropertyResettable(Object id
) {
517 public boolean isPropertySet(Object id
) {
522 * Copy this trace in the trace folder. No other parameters are mentioned so
523 * the trace is copied in this element's project trace folder
527 * @return the new Resource object
530 public TmfTraceElement
copy(String string
) {
531 TmfTraceFolder folder
= this.getProject().getTracesFolder();
532 IResource res
= super.copy(string
, false);
533 return new TmfTraceElement(string
, res
, folder
);
537 * Close opened editors associated with this trace.
542 public void closeEditors() {
543 super.closeEditors();
545 // Close experiments that contain the trace if open
546 if (getParent() instanceof TmfTraceFolder
) {
547 TmfExperimentFolder experimentFolder
= getProject().getExperimentsFolder();
548 for (ITmfProjectModelElement experiment
: experimentFolder
.getChildren()) {
549 for (ITmfProjectModelElement child
: experiment
.getChildren()) {
550 if (child
.getName().equals(getName())) {
551 ((TmfExperimentElement
) experiment
).closeEditors();
556 } else if (getParent() instanceof TmfExperimentElement
) {
557 TmfExperimentElement experiment
= (TmfExperimentElement
) getParent();
558 experiment
.closeEditors();
562 * We will be closing a trace shortly. Invoke GC to release
563 * MappedByteBuffer objects, which some trace types, like CTF, use.
564 * (see Java bug JDK-4724038)
570 * Delete the trace resource, remove it from experiments and delete its
571 * supplementary files
573 * @param progressMonitor
574 * a progress monitor, or null if progress reporting is not
577 * @throws CoreException
578 * thrown when IResource.delete fails
581 public void delete(IProgressMonitor progressMonitor
) throws CoreException
{
584 IPath path
= fResource
.getLocation();
586 if (getParent() instanceof TmfTraceFolder
) {
587 TmfExperimentFolder experimentFolder
= getProject().getExperimentsFolder();
589 // Propagate the removal to traces
590 for (ITmfProjectModelElement experiment
: experimentFolder
.getChildren()) {
591 List
<ITmfProjectModelElement
> toRemove
= new LinkedList
<>();
592 for (ITmfProjectModelElement child
: experiment
.getChildren()) {
593 if (child
.getName().equals(getName())) {
597 for (ITmfProjectModelElement child
: toRemove
) {
598 ((TmfExperimentElement
) experiment
).removeTrace((TmfTraceElement
) child
);
602 // Delete supplementary files
603 deleteSupplementaryFolder();
605 } else if (getParent() instanceof TmfExperimentElement
) {
606 TmfExperimentElement experimentElement
= (TmfExperimentElement
) getParent();
607 experimentElement
.removeTrace(this);
611 // Finally, delete the trace
612 fResource
.delete(true, progressMonitor
);