1 /*******************************************************************************
2 * Copyright (c) 2010, 2011 Ericsson
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 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
;
15 import java
.util
.Arrays
;
16 import java
.util
.HashMap
;
19 import org
.eclipse
.core
.resources
.IFolder
;
20 import org
.eclipse
.core
.resources
.IResource
;
21 import org
.eclipse
.core
.runtime
.CoreException
;
22 import org
.eclipse
.core
.runtime
.IConfigurationElement
;
23 import org
.eclipse
.core
.runtime
.IStatus
;
24 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
25 import org
.eclipse
.core
.runtime
.Platform
;
26 import org
.eclipse
.core
.runtime
.Status
;
27 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.TmfUiPlugin
;
28 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.parsers
.custom
.CustomTxtEvent
;
29 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.parsers
.custom
.CustomTxtTrace
;
30 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.parsers
.custom
.CustomTxtTraceDefinition
;
31 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.parsers
.custom
.CustomXmlEvent
;
32 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.parsers
.custom
.CustomXmlTrace
;
33 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.parsers
.custom
.CustomXmlTraceDefinition
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.TmfCommonConstants
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
37 import org
.eclipse
.linuxtools
.tmf
.ui
.editors
.TmfEventsEditor
;
38 import org
.eclipse
.ui
.IActionFilter
;
39 import org
.eclipse
.ui
.views
.properties
.IPropertyDescriptor
;
40 import org
.eclipse
.ui
.views
.properties
.IPropertySource2
;
41 import org
.eclipse
.ui
.views
.properties
.TextPropertyDescriptor
;
44 * <b><u>TmfTraceElement</u></b>
47 public class TmfTraceElement
extends TmfProjectModelElement
implements IActionFilter
, IPropertySource2
{
49 // ------------------------------------------------------------------------
51 // ------------------------------------------------------------------------
54 public static final String BUNDLE
= "bundle"; //$NON-NLS-1$
55 public static final String IS_LINKED
= "isLinked"; //$NON-NLS-1$
57 // Property View stuff
58 private static final String sfInfoCategory
= "Info"; //$NON-NLS-1$
59 private static final String sfName
= "name"; //$NON-NLS-1$
60 private static final String sfPath
= "path"; //$NON-NLS-1$
61 private static final String sfLocation
= "location"; //$NON-NLS-1$
62 private static final String sfEventType
= "type"; //$NON-NLS-1$
63 private static final String sfIsLinked
= "linked"; //$NON-NLS-1$
65 private static final TextPropertyDescriptor sfNameDescriptor
= new TextPropertyDescriptor(sfName
, sfName
);
66 private static final TextPropertyDescriptor sfPathDescriptor
= new TextPropertyDescriptor(sfPath
, sfPath
);
67 private static final TextPropertyDescriptor sfLocationDescriptor
= new TextPropertyDescriptor(sfLocation
, sfLocation
);
68 private static final TextPropertyDescriptor sfTypeDescriptor
= new TextPropertyDescriptor(sfEventType
, sfEventType
);
69 private static final TextPropertyDescriptor sfIsLinkedDescriptor
= new TextPropertyDescriptor(sfIsLinked
, sfIsLinked
);
71 private static final IPropertyDescriptor
[] sfDescriptors
= { sfNameDescriptor
, sfPathDescriptor
, sfLocationDescriptor
,
72 sfTypeDescriptor
, sfIsLinkedDescriptor
};
75 sfNameDescriptor
.setCategory(sfInfoCategory
);
76 sfPathDescriptor
.setCategory(sfInfoCategory
);
77 sfLocationDescriptor
.setCategory(sfInfoCategory
);
78 sfTypeDescriptor
.setCategory(sfInfoCategory
);
79 sfIsLinkedDescriptor
.setCategory(sfInfoCategory
);
82 // ------------------------------------------------------------------------
84 // ------------------------------------------------------------------------
86 // This trace type ID as defined in plugin.xml
87 private String fTraceTypeId
= null;
89 // ------------------------------------------------------------------------
90 // Static initialization
91 // ------------------------------------------------------------------------
93 // The mapping of available trace type IDs to their corresponding configuration element
94 private static final Map
<String
, IConfigurationElement
> sfTraceTypeAttributes
= new HashMap
<String
, IConfigurationElement
>();
95 private static final Map
<String
, IConfigurationElement
> sfTraceCategories
= new HashMap
<String
, IConfigurationElement
>();
97 // Initialize statically at startup
98 public static void init() {
99 IConfigurationElement
[] config
= Platform
.getExtensionRegistry().getConfigurationElementsFor(TmfTraceType
.TMF_TRACE_TYPE_ID
);
100 for (IConfigurationElement ce
: config
) {
101 String elementName
= ce
.getName();
102 if (elementName
.equals(TmfTraceType
.TYPE_ELEM
)) {
103 String traceTypeId
= ce
.getAttribute(TmfTraceType
.ID_ATTR
);
104 sfTraceTypeAttributes
.put(traceTypeId
, ce
);
105 } else if (elementName
.equals(TmfTraceType
.CATEGORY_ELEM
)) {
106 String categoryId
= ce
.getAttribute(TmfTraceType
.ID_ATTR
);
107 sfTraceCategories
.put(categoryId
, ce
);
112 // ------------------------------------------------------------------------
114 // ------------------------------------------------------------------------
116 public TmfTraceElement(String name
, IResource trace
, TmfTraceFolder parent
) {
117 this(name
, trace
, (TmfProjectModelElement
) parent
);
120 public TmfTraceElement(String name
, IResource trace
, TmfExperimentElement parent
) {
121 this(name
, trace
, (TmfProjectModelElement
) parent
);
124 private TmfTraceElement(String name
, IResource trace
, TmfProjectModelElement parent
) {
125 super(name
, trace
, parent
);
126 parent
.addChild(this);
130 // ------------------------------------------------------------------------
132 // ------------------------------------------------------------------------
134 public String
getTraceType() {
138 public void refreshTraceType() {
140 fTraceTypeId
= getResource().getPersistentProperty(TmfCommonConstants
.TRACETYPE
);
141 } catch (CoreException e
) {
146 public ITmfTrace
<?
> instantiateTrace() {
149 // make sure that supplementary folder exists
150 refreshSupplementaryFolder();
152 if (fTraceTypeId
!= null) {
153 if (fTraceTypeId
.startsWith(CustomTxtTrace
.class.getCanonicalName())) {
154 for (CustomTxtTraceDefinition def
: CustomTxtTraceDefinition
.loadAll()) {
155 if (fTraceTypeId
.equals(CustomTxtTrace
.class.getCanonicalName() + ":" + def
.definitionName
)) { //$NON-NLS-1$
156 return new CustomTxtTrace(def
);
160 if (fTraceTypeId
.startsWith(CustomXmlTrace
.class.getCanonicalName())) {
161 for (CustomXmlTraceDefinition def
: CustomXmlTraceDefinition
.loadAll()) {
162 if (fTraceTypeId
.equals(CustomXmlTrace
.class.getCanonicalName() + ":" + def
.definitionName
)) { //$NON-NLS-1$
163 return new CustomXmlTrace(def
);
167 IConfigurationElement ce
= sfTraceTypeAttributes
.get(fTraceTypeId
);
168 ITmfTrace
<?
> trace
= (ITmfTrace
<?
>) ce
.createExecutableExtension(TmfTraceType
.TRACE_TYPE_ATTR
);
171 } catch (CoreException e
) {
177 public ITmfEvent
instantiateEvent() {
179 if (fTraceTypeId
!= null) {
180 if (fTraceTypeId
.startsWith(CustomTxtTrace
.class.getCanonicalName())) {
181 for (CustomTxtTraceDefinition def
: CustomTxtTraceDefinition
.loadAll()) {
182 if (fTraceTypeId
.equals(CustomTxtTrace
.class.getCanonicalName() + ":" + def
.definitionName
)) { //$NON-NLS-1$
183 return new CustomTxtEvent(def
);
187 if (fTraceTypeId
.startsWith(CustomXmlTrace
.class.getCanonicalName())) {
188 for (CustomXmlTraceDefinition def
: CustomXmlTraceDefinition
.loadAll()) {
189 if (fTraceTypeId
.equals(CustomXmlTrace
.class.getCanonicalName() + ":" + def
.definitionName
)) { //$NON-NLS-1$
190 return new CustomXmlEvent(def
);
194 IConfigurationElement ce
= sfTraceTypeAttributes
.get(fTraceTypeId
);
195 ITmfEvent event
= (ITmfEvent
) ce
.createExecutableExtension(TmfTraceType
.EVENT_TYPE_ATTR
);
198 } catch (CoreException e
) {
204 public String
getEditorId() {
205 if (fTraceTypeId
!= null) {
206 if (fTraceTypeId
.startsWith(CustomTxtTrace
.class.getCanonicalName())) {
207 return TmfEventsEditor
.ID
;
209 if (fTraceTypeId
.startsWith(CustomXmlTrace
.class.getCanonicalName())) {
210 return TmfEventsEditor
.ID
;
212 IConfigurationElement ce
= sfTraceTypeAttributes
.get(fTraceTypeId
);
213 IConfigurationElement
[] defaultEditorCE
= ce
.getChildren(TmfTraceType
.DEFAULT_EDITOR_ELEM
);
214 if (defaultEditorCE
.length
== 1) {
215 return defaultEditorCE
[0].getAttribute(TmfTraceType
.ID_ATTR
);
222 * Returns the <code>TmfTraceElement</code> located under the <code>TmfTracesFolder</code>.
224 * @return <code>this</code> if this element is under the <code>TmfTracesFolder</code>
225 * else the corresponding <code>TmfTraceElement</code> if this element is under
226 * <code>TmfExperimentElement</code>.
228 public TmfTraceElement
getElementUnderTraceFolder() {
230 // If trace is under an experiment, return original trace from the traces folder
231 if (getParent() instanceof TmfExperimentElement
) {
232 for (TmfTraceElement aTrace
: getProject().getTracesFolder().getTraces()) {
233 if (aTrace
.getName().equals(getName())) {
242 * Deletes the trace specific supplementary folder.
244 public void deleteSupplementaryFolder() {
245 IFolder supplFolder
= getTraceSupplementaryFolder(fResource
.getName());
246 if (supplFolder
.exists()) {
248 supplFolder
.delete(true, new NullProgressMonitor());
249 } catch (CoreException e
) {
250 TmfUiPlugin
.getDefault().getLog().log(new Status(IStatus
.ERROR
, TmfUiPlugin
.PLUGIN_ID
, "Error deleting supplementary folder " + supplFolder
, e
)); //$NON-NLS-1$
256 * Renames the trace specific supplementary folder according to the new trace name.
258 * @param newTraceName The new trace name
260 public void renameSupplementaryFolder(String newTraceName
) {
261 IFolder oldSupplFolder
= getTraceSupplementaryFolder(fResource
.getName());
262 IFolder newSupplFolder
= getTraceSupplementaryFolder(newTraceName
);
264 // Rename supplementary folder
265 if (oldSupplFolder
.exists()) {
267 oldSupplFolder
.move(newSupplFolder
.getFullPath(), true, new NullProgressMonitor());
268 } catch (CoreException e
) {
269 TmfUiPlugin
.getDefault().getLog().log(new Status(IStatus
.ERROR
, TmfUiPlugin
.PLUGIN_ID
, "Error renaming supplementary folder " + oldSupplFolder
, e
)); //$NON-NLS-1$
275 * Copies the trace specific supplementary folder to the new trace name.
277 * @param newTraceName The new trace name
279 public void copySupplementaryFolder(String newTraceName
) {
280 IFolder oldSupplFolder
= getTraceSupplementaryFolder(fResource
.getName());
281 IFolder newSupplFolder
= getTraceSupplementaryFolder(newTraceName
);
283 // copy supplementary folder
284 if (oldSupplFolder
.exists()) {
286 oldSupplFolder
.copy(newSupplFolder
.getFullPath(), true, new NullProgressMonitor());
287 } catch (CoreException e
) {
288 TmfUiPlugin
.getDefault().getLog().log(new Status(IStatus
.ERROR
, TmfUiPlugin
.PLUGIN_ID
, "Error renaming supplementary folder " + oldSupplFolder
, e
)); //$NON-NLS-1$
294 * Copies the trace specific supplementary folder a new folder.
296 * @param destination The destination folder to copy to.
298 public void copySupplementaryFolder(IFolder destination
) {
299 IFolder oldSupplFolder
= getTraceSupplementaryFolder(fResource
.getName());
301 // copy supplementary folder
302 if (oldSupplFolder
.exists()) {
304 oldSupplFolder
.copy(destination
.getFullPath(), true, new NullProgressMonitor());
305 } catch (CoreException e
) {
306 TmfUiPlugin
.getDefault().getLog().log(new Status(IStatus
.ERROR
, TmfUiPlugin
.PLUGIN_ID
, "Error renaming supplementary folder " + oldSupplFolder
, e
)); //$NON-NLS-1$
313 * Refreshes the trace specific supplementary folder information. It creates the folder if not exists.
314 * It sets the persistence property of the trace resource
316 public void refreshSupplementaryFolder() {
317 createSupplementaryDirectory();
321 * Checks if supplementary resource exist or not.
323 * @return <code>true</code> if one or more files are under the trace supplementary folder
325 public boolean hasSupplementaryResources() {
326 IResource
[] resources
= getSupplementaryResources();
327 return (resources
.length
> 0);
331 * Returns the supplementary resources under the trace supplementary folder.
333 * @return array of resources under the trace supplementary folder.
335 public IResource
[] getSupplementaryResources() {
336 IFolder supplFolder
= getTraceSupplementaryFolder(fResource
.getName());
337 if (supplFolder
.exists()) {
339 return supplFolder
.members();
340 } catch (CoreException e
) {
341 TmfUiPlugin
.getDefault().getLog().log(new Status(IStatus
.ERROR
, TmfUiPlugin
.PLUGIN_ID
, "Error deleting supplementary folder " + supplFolder
, e
)); //$NON-NLS-1$
344 return new IResource
[0];
348 * Deletes the given resources.
350 * @param resources array of resources to delete.
352 public void deleteSupplementaryResources(IResource
[] resources
) {
354 for (int i
= 0; i
< resources
.length
; i
++) {
356 resources
[i
].delete(true, new NullProgressMonitor());
357 } catch (CoreException e
) {
358 TmfUiPlugin
.getDefault().getLog().log(new Status(IStatus
.ERROR
, TmfUiPlugin
.PLUGIN_ID
, "Error deleting supplementary resource " + resources
[i
], e
)); //$NON-NLS-1$
363 private void createSupplementaryDirectory() {
364 IFolder supplFolder
= getTraceSupplementaryFolder(fResource
.getName());
365 if (!supplFolder
.exists()) {
367 supplFolder
.create(true, true, new NullProgressMonitor());
368 } catch (CoreException e
) {
369 TmfUiPlugin
.getDefault().getLog().log(new Status(IStatus
.ERROR
, TmfUiPlugin
.PLUGIN_ID
, "Error creating resource supplementary file " + supplFolder
, e
)); //$NON-NLS-1$
374 fResource
.setPersistentProperty(TmfCommonConstants
.TRACE_SUPPLEMENTARY_FOLDER
, supplFolder
.getLocationURI().getPath());
375 } catch (CoreException e
) {
376 TmfUiPlugin
.getDefault().getLog().log(new Status(IStatus
.ERROR
, TmfUiPlugin
.PLUGIN_ID
, "Error setting persistant property " + TmfCommonConstants
.TRACE_SUPPLEMENTARY_FOLDER
, e
)); //$NON-NLS-1$
381 // ------------------------------------------------------------------------
383 // ------------------------------------------------------------------------
386 public boolean testAttribute(Object target
, String name
, String value
) {
387 if (name
.equals(IS_LINKED
)) {
388 boolean isLinked
= getResource().isLinked();
389 return Boolean
.toString(isLinked
).equals(value
);
394 // ------------------------------------------------------------------------
396 // ------------------------------------------------------------------------
399 public TmfProjectElement
getProject() {
400 if (getParent() instanceof TmfTraceFolder
) {
401 TmfTraceFolder folder
= (TmfTraceFolder
) getParent();
402 TmfProjectElement project
= (TmfProjectElement
) folder
.getParent();
405 if (getParent() instanceof TmfExperimentElement
) {
406 TmfExperimentElement experiment
= (TmfExperimentElement
) getParent();
407 TmfExperimentFolder folder
= (TmfExperimentFolder
) experiment
.getParent();
408 TmfProjectElement project
= (TmfProjectElement
) folder
.getParent();
414 // ------------------------------------------------------------------------
416 // ------------------------------------------------------------------------
419 public Object
getEditableValue() {
424 public IPropertyDescriptor
[] getPropertyDescriptors() {
425 return (sfDescriptors
!= null) ? Arrays
.copyOf(sfDescriptors
, sfDescriptors
.length
) : null;
429 public Object
getPropertyValue(Object id
) {
431 if (sfName
.equals(id
)) {
435 if (sfPath
.equals(id
)) {
436 return getPath().toString();
439 if (sfLocation
.equals(id
)) {
440 return getLocation().toString();
443 if (sfIsLinked
.equals(id
)) {
444 return Boolean
.valueOf(getResource().isLinked()).toString();
447 if (sfEventType
.equals(id
)) {
448 if (fTraceTypeId
!= null) {
449 IConfigurationElement ce
= sfTraceTypeAttributes
.get(fTraceTypeId
);
450 return (ce
!= null) ?
(getCategory(ce
) + " : " + ce
.getAttribute(TmfTraceType
.NAME_ATTR
)) : ""; //$NON-NLS-1$ //$NON-NLS-2$
457 private String
getCategory(IConfigurationElement ce
) {
458 String categoryId
= ce
.getAttribute(TmfTraceType
.CATEGORY_ATTR
);
459 if (categoryId
!= null) {
460 IConfigurationElement category
= sfTraceCategories
.get(categoryId
);
461 if (category
!= null) {
462 return category
.getAttribute(TmfTraceType
.NAME_ATTR
);
465 return "[no category]"; //$NON-NLS-1$
469 public void resetPropertyValue(Object id
) {
473 public void setPropertyValue(Object id
, Object value
) {
477 public boolean isPropertyResettable(Object id
) {
482 public boolean isPropertySet(Object id
) {