1 /*******************************************************************************
2 * Copyright (c) 2010, 2013 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, added
14 *******************************************************************************/
16 package org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
;
18 import java
.io
.ByteArrayInputStream
;
19 import java
.io
.InputStream
;
20 import java
.util
.Arrays
;
21 import java
.util
.HashMap
;
24 import org
.eclipse
.core
.resources
.IFile
;
25 import org
.eclipse
.core
.resources
.IFolder
;
26 import org
.eclipse
.core
.resources
.IResource
;
27 import org
.eclipse
.core
.runtime
.CoreException
;
28 import org
.eclipse
.core
.runtime
.IConfigurationElement
;
29 import org
.eclipse
.core
.runtime
.Platform
;
30 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Activator
;
31 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.parsers
.custom
.CustomTxtEvent
;
32 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.parsers
.custom
.CustomTxtTrace
;
33 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.parsers
.custom
.CustomTxtTraceDefinition
;
34 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.parsers
.custom
.CustomXmlEvent
;
35 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.parsers
.custom
.CustomXmlTrace
;
36 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.parsers
.custom
.CustomXmlTraceDefinition
;
37 import org
.eclipse
.linuxtools
.tmf
.core
.TmfCommonConstants
;
38 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
39 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
40 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTrace
;
41 import org
.eclipse
.linuxtools
.tmf
.core
.util
.ReadOnlyTextPropertyDescriptor
;
42 import org
.eclipse
.linuxtools
.tmf
.ui
.editors
.TmfEventsEditor
;
43 import org
.eclipse
.ui
.IActionFilter
;
44 import org
.eclipse
.ui
.views
.properties
.IPropertyDescriptor
;
45 import org
.eclipse
.ui
.views
.properties
.IPropertySource2
;
48 * Implementation of trace model element representing a trace. It provides
49 * methods to instantiate <code>ITmfTrace</code> and <code>ITmfEvent</code> as
50 * well as editor ID from the trace type extension definition.
53 * @author Francois Chouinard
55 public class TmfTraceElement
extends TmfWithFolderElement
implements IActionFilter
, IPropertySource2
{
57 // ------------------------------------------------------------------------
59 // ------------------------------------------------------------------------
63 * Bundle attribute name
65 public static final String BUNDLE
= "bundle"; //$NON-NLS-1$
67 * IsLinked attribute name.
69 public static final String IS_LINKED
= "isLinked"; //$NON-NLS-1$
71 // Property View stuff
72 private static final String sfInfoCategory
= "Info"; //$NON-NLS-1$
73 private static final String sfName
= "name"; //$NON-NLS-1$
74 private static final String sfPath
= "path"; //$NON-NLS-1$
75 private static final String sfLocation
= "location"; //$NON-NLS-1$
76 private static final String sfEventType
= "type"; //$NON-NLS-1$
77 private static final String sfIsLinked
= "linked"; //$NON-NLS-1$
79 private static final ReadOnlyTextPropertyDescriptor sfNameDescriptor
= new ReadOnlyTextPropertyDescriptor(sfName
, sfName
);
80 private static final ReadOnlyTextPropertyDescriptor sfPathDescriptor
= new ReadOnlyTextPropertyDescriptor(sfPath
, sfPath
);
81 private static final ReadOnlyTextPropertyDescriptor sfLocationDescriptor
= new ReadOnlyTextPropertyDescriptor(sfLocation
, sfLocation
);
82 private static final ReadOnlyTextPropertyDescriptor sfTypeDescriptor
= new ReadOnlyTextPropertyDescriptor(sfEventType
, sfEventType
);
83 private static final ReadOnlyTextPropertyDescriptor sfIsLinkedDescriptor
= new ReadOnlyTextPropertyDescriptor(sfIsLinked
, sfIsLinked
);
85 private static final IPropertyDescriptor
[] sfDescriptors
= { sfNameDescriptor
, sfPathDescriptor
, sfLocationDescriptor
,
86 sfTypeDescriptor
, sfIsLinkedDescriptor
};
89 sfNameDescriptor
.setCategory(sfInfoCategory
);
90 sfPathDescriptor
.setCategory(sfInfoCategory
);
91 sfLocationDescriptor
.setCategory(sfInfoCategory
);
92 sfTypeDescriptor
.setCategory(sfInfoCategory
);
93 sfIsLinkedDescriptor
.setCategory(sfInfoCategory
);
96 private static final String BOOKMARKS_HIDDEN_FILE
= ".bookmarks"; //$NON-NLS-1$
98 // ------------------------------------------------------------------------
100 // ------------------------------------------------------------------------
102 // This trace type ID as defined in plugin.xml
103 private String fTraceTypeId
= null;
105 // ------------------------------------------------------------------------
106 // Static initialization
107 // ------------------------------------------------------------------------
109 // The mapping of available trace type IDs to their corresponding
110 // configuration element
111 private static final Map
<String
, IConfigurationElement
> sfTraceTypeAttributes
= new HashMap
<String
, IConfigurationElement
>();
112 private static final Map
<String
, IConfigurationElement
> sfTraceCategories
= new HashMap
<String
, IConfigurationElement
>();
115 * Initialize statically at startup by getting extensions from the platform
116 * extension registry.
118 public static void init() {
119 IConfigurationElement
[] config
= Platform
.getExtensionRegistry().getConfigurationElementsFor(TmfTraceType
.TMF_TRACE_TYPE_ID
);
120 for (IConfigurationElement ce
: config
) {
121 String elementName
= ce
.getName();
122 if (elementName
.equals(TmfTraceType
.TYPE_ELEM
)) {
123 String traceTypeId
= ce
.getAttribute(TmfTraceType
.ID_ATTR
);
124 sfTraceTypeAttributes
.put(traceTypeId
, ce
);
125 } else if (elementName
.equals(TmfTraceType
.CATEGORY_ELEM
)) {
126 String categoryId
= ce
.getAttribute(TmfTraceType
.ID_ATTR
);
127 sfTraceCategories
.put(categoryId
, ce
);
132 // ------------------------------------------------------------------------
134 // ------------------------------------------------------------------------
136 * Constructor. Creates trace model element under the trace folder.
141 * The trace resource.
143 * The parent element (trace folder)
145 public TmfTraceElement(String name
, IResource trace
, TmfTraceFolder parent
) {
146 this(name
, trace
, (TmfProjectModelElement
) parent
);
150 * Constructor. Creates trace model element under the experiment folder.
155 * The trace resource.
157 * The parent element (experiment folder)
159 public TmfTraceElement(String name
, IResource trace
, TmfExperimentElement parent
) {
160 this(name
, trace
, (TmfProjectModelElement
) parent
);
163 private TmfTraceElement(String name
, IResource trace
, TmfProjectModelElement parent
) {
164 super(name
, trace
, parent
);
165 parent
.addChild(this);
169 // ------------------------------------------------------------------------
171 // ------------------------------------------------------------------------
173 * Returns the trace type ID.
175 * @return trace type ID.
177 public String
getTraceType() {
182 * Refreshes the trace type filed by reading the trace type persistent
183 * property of the resource referenece.
185 public void refreshTraceType() {
187 fTraceTypeId
= getResource().getPersistentProperty(TmfCommonConstants
.TRACETYPE
);
188 } catch (CoreException e
) {
189 Activator
.getDefault().logError("Error refreshing trace type pesistent property for trace " + getName(), e
); //$NON-NLS-1$
194 * Instantiate a <code>ITmfTrace</code> object based on the trace type and
195 * the corresponding extension.
197 * @return the <code>ITmfTrace</code> or <code>null</code> for an error
199 public ITmfTrace
instantiateTrace() {
202 // make sure that supplementary folder exists
203 refreshSupplementaryFolder();
205 if (fTraceTypeId
!= null) {
206 if (fTraceTypeId
.startsWith(CustomTxtTrace
.class.getCanonicalName())) {
207 for (CustomTxtTraceDefinition def
: CustomTxtTraceDefinition
.loadAll()) {
208 if (fTraceTypeId
.equals(CustomTxtTrace
.class.getCanonicalName() + ":" + def
.definitionName
)) { //$NON-NLS-1$
209 return new CustomTxtTrace(def
);
213 if (fTraceTypeId
.startsWith(CustomXmlTrace
.class.getCanonicalName())) {
214 for (CustomXmlTraceDefinition def
: CustomXmlTraceDefinition
.loadAll()) {
215 if (fTraceTypeId
.equals(CustomXmlTrace
.class.getCanonicalName() + ":" + def
.definitionName
)) { //$NON-NLS-1$
216 return new CustomXmlTrace(def
);
220 IConfigurationElement ce
= sfTraceTypeAttributes
.get(fTraceTypeId
);
224 ITmfTrace trace
= (ITmfTrace
) ce
.createExecutableExtension(TmfTraceType
.TRACE_TYPE_ATTR
);
227 } catch (CoreException e
) {
228 Activator
.getDefault().logError("Error instantiating ITmfTrace object for trace " + getName(), e
); //$NON-NLS-1$
234 * Instantiate a <code>ITmfEvent</code> object based on the trace type and
235 * the corresponding extension.
237 * @return the <code>ITmfEvent</code> or <code>null</code> for an error
239 public ITmfEvent
instantiateEvent() {
241 if (fTraceTypeId
!= null) {
242 if (fTraceTypeId
.startsWith(CustomTxtTrace
.class.getCanonicalName())) {
243 for (CustomTxtTraceDefinition def
: CustomTxtTraceDefinition
.loadAll()) {
244 if (fTraceTypeId
.equals(CustomTxtTrace
.class.getCanonicalName() + ":" + def
.definitionName
)) { //$NON-NLS-1$
245 return new CustomTxtEvent(def
);
249 if (fTraceTypeId
.startsWith(CustomXmlTrace
.class.getCanonicalName())) {
250 for (CustomXmlTraceDefinition def
: CustomXmlTraceDefinition
.loadAll()) {
251 if (fTraceTypeId
.equals(CustomXmlTrace
.class.getCanonicalName() + ":" + def
.definitionName
)) { //$NON-NLS-1$
252 return new CustomXmlEvent(def
);
256 IConfigurationElement ce
= sfTraceTypeAttributes
.get(fTraceTypeId
);
260 ITmfEvent event
= (ITmfEvent
) ce
.createExecutableExtension(TmfTraceType
.EVENT_TYPE_ATTR
);
263 } catch (CoreException e
) {
264 Activator
.getDefault().logError("Error instantiating ITmfEvent object for trace " + getName(), e
); //$NON-NLS-1$
270 * Returns the optional editor ID from the trace type extension.
272 * @return the editor ID or <code>null</code> if not defined.
274 public String
getEditorId() {
275 if (fTraceTypeId
!= null) {
276 if (fTraceTypeId
.startsWith(CustomTxtTrace
.class.getCanonicalName())) {
277 return TmfEventsEditor
.ID
;
279 if (fTraceTypeId
.startsWith(CustomXmlTrace
.class.getCanonicalName())) {
280 return TmfEventsEditor
.ID
;
282 IConfigurationElement ce
= sfTraceTypeAttributes
.get(fTraceTypeId
);
283 IConfigurationElement
[] defaultEditorCE
= ce
.getChildren(TmfTraceType
.DEFAULT_EDITOR_ELEM
);
284 if (defaultEditorCE
.length
== 1) {
285 return defaultEditorCE
[0].getAttribute(TmfTraceType
.ID_ATTR
);
292 * Returns the file resource used to store bookmarks after creating it if
293 * necessary. If the trace resource is a file, it is returned directly. If
294 * the trace resource is a folder, a linked file is returned. The file will
295 * be created if it does not exist.
297 * @return the bookmarks file
298 * @throws CoreException
299 * if the bookmarks file cannot be created
302 public IFile
createBookmarksFile() throws CoreException
{
303 IFile file
= getBookmarksFile();
304 if (fResource
instanceof IFolder
) {
305 if (!file
.exists()) {
306 final IFile bookmarksFile
= getProject().getTracesFolder().getResource().getFile(BOOKMARKS_HIDDEN_FILE
);
307 if (!bookmarksFile
.exists()) {
308 final InputStream source
= new ByteArrayInputStream(new byte[0]);
309 bookmarksFile
.create(source
, true, null);
311 bookmarksFile
.setHidden(true);
312 file
.createLink(bookmarksFile
.getLocation(), IResource
.REPLACE
, null);
313 file
.setHidden(true);
314 file
.setPersistentProperty(TmfCommonConstants
.TRACETYPE
, TmfTrace
.class.getCanonicalName());
321 * Returns the file resource used to store bookmarks. The file may not
324 * @return the bookmarks file
327 public IFile
getBookmarksFile() {
329 if (fResource
instanceof IFile
) {
330 file
= (IFile
) fResource
;
331 } else if (fResource
instanceof IFolder
) {
332 final IFolder folder
= (IFolder
) fResource
;
333 file
= folder
.getFile(getName() + '_');
339 * Returns the <code>TmfTraceElement</code> located under the
340 * <code>TmfTracesFolder</code>.
342 * @return <code>this</code> if this element is under the
343 * <code>TmfTracesFolder</code> else the corresponding
344 * <code>TmfTraceElement</code> if this element is under
345 * <code>TmfExperimentElement</code>.
347 public TmfTraceElement
getElementUnderTraceFolder() {
349 // If trace is under an experiment, return original trace from the
351 if (getParent() instanceof TmfExperimentElement
) {
352 for (TmfTraceElement aTrace
: getProject().getTracesFolder().getTraces()) {
353 if (aTrace
.getName().equals(getName())) {
361 // ------------------------------------------------------------------------
363 // ------------------------------------------------------------------------
366 public boolean testAttribute(Object target
, String name
, String value
) {
367 if (name
.equals(IS_LINKED
)) {
368 boolean isLinked
= getResource().isLinked();
369 return Boolean
.toString(isLinked
).equals(value
);
374 // ------------------------------------------------------------------------
376 // ------------------------------------------------------------------------
379 public TmfProjectElement
getProject() {
380 if (getParent() instanceof TmfTraceFolder
) {
381 TmfTraceFolder folder
= (TmfTraceFolder
) getParent();
382 TmfProjectElement project
= (TmfProjectElement
) folder
.getParent();
385 if (getParent() instanceof TmfExperimentElement
) {
386 TmfExperimentElement experiment
= (TmfExperimentElement
) getParent();
387 TmfExperimentFolder folder
= (TmfExperimentFolder
) experiment
.getParent();
388 TmfProjectElement project
= (TmfProjectElement
) folder
.getParent();
394 // ------------------------------------------------------------------------
396 // ------------------------------------------------------------------------
399 public Object
getEditableValue() {
404 public IPropertyDescriptor
[] getPropertyDescriptors() {
405 return Arrays
.copyOf(sfDescriptors
, sfDescriptors
.length
);
409 public Object
getPropertyValue(Object id
) {
411 if (sfName
.equals(id
)) {
415 if (sfPath
.equals(id
)) {
416 return getPath().toString();
419 if (sfLocation
.equals(id
)) {
420 return getLocation().toString();
423 if (sfIsLinked
.equals(id
)) {
424 return Boolean
.valueOf(getResource().isLinked()).toString();
427 if (sfEventType
.equals(id
)) {
428 if (fTraceTypeId
!= null) {
429 IConfigurationElement ce
= sfTraceTypeAttributes
.get(fTraceTypeId
);
430 return (ce
!= null) ?
(getCategory(ce
) + " : " + ce
.getAttribute(TmfTraceType
.NAME_ATTR
)) : ""; //$NON-NLS-1$ //$NON-NLS-2$
437 private static String
getCategory(IConfigurationElement ce
) {
438 String categoryId
= ce
.getAttribute(TmfTraceType
.CATEGORY_ATTR
);
439 if (categoryId
!= null) {
440 IConfigurationElement category
= sfTraceCategories
.get(categoryId
);
441 if (category
!= null) {
442 return category
.getAttribute(TmfTraceType
.NAME_ATTR
);
445 return "[no category]"; //$NON-NLS-1$
449 public void resetPropertyValue(Object id
) {
453 public void setPropertyValue(Object id
, Object value
) {
457 public boolean isPropertyResettable(Object id
) {
462 public boolean isPropertySet(Object id
) {
467 * Copy this trace in the trace folder. No other parameters are mentioned so
468 * the trace is copied in this element's project trace folder
472 * @return the new Resource object
475 public TmfTraceElement
copy(String string
) {
476 TmfTraceFolder folder
= this.getProject().getTracesFolder();
477 IResource res
= super.copy(string
, false);
478 return new TmfTraceElement(string
, res
, folder
);