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
.NullProgressMonitor
;
24 import org
.eclipse
.core
.runtime
.Platform
;
25 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.TmfUiPlugin
;
26 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.parsers
.custom
.CustomTxtEvent
;
27 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.parsers
.custom
.CustomTxtTrace
;
28 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.parsers
.custom
.CustomTxtTraceDefinition
;
29 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.parsers
.custom
.CustomXmlEvent
;
30 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.parsers
.custom
.CustomXmlTrace
;
31 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.parsers
.custom
.CustomXmlTraceDefinition
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.TmfCommonConstants
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
35 import org
.eclipse
.linuxtools
.tmf
.ui
.editors
.TmfEventsEditor
;
36 import org
.eclipse
.ui
.IActionFilter
;
37 import org
.eclipse
.ui
.views
.properties
.IPropertyDescriptor
;
38 import org
.eclipse
.ui
.views
.properties
.IPropertySource2
;
39 import org
.eclipse
.ui
.views
.properties
.TextPropertyDescriptor
;
42 * <b><u>TmfTraceElement</u></b>
45 public class TmfTraceElement
extends TmfProjectModelElement
implements IActionFilter
, IPropertySource2
{
47 // ------------------------------------------------------------------------
49 // ------------------------------------------------------------------------
52 public static final String BUNDLE
= "bundle"; //$NON-NLS-1$
53 public static final String IS_LINKED
= "isLinked"; //$NON-NLS-1$
55 // Property View stuff
56 private static final String sfInfoCategory
= "Info"; //$NON-NLS-1$
57 private static final String sfName
= "name"; //$NON-NLS-1$
58 private static final String sfPath
= "path"; //$NON-NLS-1$
59 private static final String sfLocation
= "location"; //$NON-NLS-1$
60 private static final String sfEventType
= "type"; //$NON-NLS-1$
61 private static final String sfIsLinked
= "linked"; //$NON-NLS-1$
63 private static final TextPropertyDescriptor sfNameDescriptor
= new TextPropertyDescriptor(sfName
, sfName
);
64 private static final TextPropertyDescriptor sfPathDescriptor
= new TextPropertyDescriptor(sfPath
, sfPath
);
65 private static final TextPropertyDescriptor sfLocationDescriptor
= new TextPropertyDescriptor(sfLocation
, sfLocation
);
66 private static final TextPropertyDescriptor sfTypeDescriptor
= new TextPropertyDescriptor(sfEventType
, sfEventType
);
67 private static final TextPropertyDescriptor sfIsLinkedDescriptor
= new TextPropertyDescriptor(sfIsLinked
, sfIsLinked
);
69 private static final IPropertyDescriptor
[] sfDescriptors
= { sfNameDescriptor
, sfPathDescriptor
, sfLocationDescriptor
,
70 sfTypeDescriptor
, sfIsLinkedDescriptor
};
73 sfNameDescriptor
.setCategory(sfInfoCategory
);
74 sfPathDescriptor
.setCategory(sfInfoCategory
);
75 sfLocationDescriptor
.setCategory(sfInfoCategory
);
76 sfTypeDescriptor
.setCategory(sfInfoCategory
);
77 sfIsLinkedDescriptor
.setCategory(sfInfoCategory
);
80 // ------------------------------------------------------------------------
82 // ------------------------------------------------------------------------
84 // This trace type ID as defined in plugin.xml
85 private String fTraceTypeId
= null;
87 // ------------------------------------------------------------------------
88 // Static initialization
89 // ------------------------------------------------------------------------
91 // The mapping of available trace type IDs to their corresponding configuration element
92 private static final Map
<String
, IConfigurationElement
> sfTraceTypeAttributes
= new HashMap
<String
, IConfigurationElement
>();
93 private static final Map
<String
, IConfigurationElement
> sfTraceCategories
= new HashMap
<String
, IConfigurationElement
>();
95 // Initialize statically at startup
96 public static void init() {
97 IConfigurationElement
[] config
= Platform
.getExtensionRegistry().getConfigurationElementsFor(TmfTraceType
.TMF_TRACE_TYPE_ID
);
98 for (IConfigurationElement ce
: config
) {
99 String elementName
= ce
.getName();
100 if (elementName
.equals(TmfTraceType
.TYPE_ELEM
)) {
101 String traceTypeId
= ce
.getAttribute(TmfTraceType
.ID_ATTR
);
102 sfTraceTypeAttributes
.put(traceTypeId
, ce
);
103 } else if (elementName
.equals(TmfTraceType
.CATEGORY_ELEM
)) {
104 String categoryId
= ce
.getAttribute(TmfTraceType
.ID_ATTR
);
105 sfTraceCategories
.put(categoryId
, ce
);
110 // ------------------------------------------------------------------------
112 // ------------------------------------------------------------------------
114 public TmfTraceElement(String name
, IResource trace
, TmfTraceFolder parent
) {
115 this(name
, trace
, (TmfProjectModelElement
) parent
);
118 public TmfTraceElement(String name
, IResource trace
, TmfExperimentElement parent
) {
119 this(name
, trace
, (TmfProjectModelElement
) parent
);
122 private TmfTraceElement(String name
, IResource trace
, TmfProjectModelElement parent
) {
123 super(name
, trace
, parent
);
124 parent
.addChild(this);
128 // ------------------------------------------------------------------------
130 // ------------------------------------------------------------------------
132 public String
getTraceType() {
136 public void refreshTraceType() {
138 fTraceTypeId
= getResource().getPersistentProperty(TmfCommonConstants
.TRACETYPE
);
139 } catch (CoreException e
) {
140 TmfUiPlugin
.getDefault().logError("Error refreshing trace type pesistent property for trace " + getName(), e
); //$NON-NLS-1$
144 public ITmfTrace
<?
> instantiateTrace() {
147 // make sure that supplementary folder exists
148 refreshSupplementaryFolder();
150 if (fTraceTypeId
!= null) {
151 if (fTraceTypeId
.startsWith(CustomTxtTrace
.class.getCanonicalName())) {
152 for (CustomTxtTraceDefinition def
: CustomTxtTraceDefinition
.loadAll()) {
153 if (fTraceTypeId
.equals(CustomTxtTrace
.class.getCanonicalName() + ":" + def
.definitionName
)) { //$NON-NLS-1$
154 return new CustomTxtTrace(def
);
158 if (fTraceTypeId
.startsWith(CustomXmlTrace
.class.getCanonicalName())) {
159 for (CustomXmlTraceDefinition def
: CustomXmlTraceDefinition
.loadAll()) {
160 if (fTraceTypeId
.equals(CustomXmlTrace
.class.getCanonicalName() + ":" + def
.definitionName
)) { //$NON-NLS-1$
161 return new CustomXmlTrace(def
);
165 IConfigurationElement ce
= sfTraceTypeAttributes
.get(fTraceTypeId
);
166 ITmfTrace
<?
> trace
= (ITmfTrace
<?
>) ce
.createExecutableExtension(TmfTraceType
.TRACE_TYPE_ATTR
);
169 } catch (CoreException e
) {
170 TmfUiPlugin
.getDefault().logError("Error instantiating ITmfTrace object for trace " + getName(), e
); //$NON-NLS-1$
175 public ITmfEvent
instantiateEvent() {
177 if (fTraceTypeId
!= null) {
178 if (fTraceTypeId
.startsWith(CustomTxtTrace
.class.getCanonicalName())) {
179 for (CustomTxtTraceDefinition def
: CustomTxtTraceDefinition
.loadAll()) {
180 if (fTraceTypeId
.equals(CustomTxtTrace
.class.getCanonicalName() + ":" + def
.definitionName
)) { //$NON-NLS-1$
181 return new CustomTxtEvent(def
);
185 if (fTraceTypeId
.startsWith(CustomXmlTrace
.class.getCanonicalName())) {
186 for (CustomXmlTraceDefinition def
: CustomXmlTraceDefinition
.loadAll()) {
187 if (fTraceTypeId
.equals(CustomXmlTrace
.class.getCanonicalName() + ":" + def
.definitionName
)) { //$NON-NLS-1$
188 return new CustomXmlEvent(def
);
192 IConfigurationElement ce
= sfTraceTypeAttributes
.get(fTraceTypeId
);
193 ITmfEvent event
= (ITmfEvent
) ce
.createExecutableExtension(TmfTraceType
.EVENT_TYPE_ATTR
);
196 } catch (CoreException e
) {
197 TmfUiPlugin
.getDefault().logError("Error instantiating ITmfEvent object for trace " + getName(), e
); //$NON-NLS-1$
202 public String
getEditorId() {
203 if (fTraceTypeId
!= null) {
204 if (fTraceTypeId
.startsWith(CustomTxtTrace
.class.getCanonicalName())) {
205 return TmfEventsEditor
.ID
;
207 if (fTraceTypeId
.startsWith(CustomXmlTrace
.class.getCanonicalName())) {
208 return TmfEventsEditor
.ID
;
210 IConfigurationElement ce
= sfTraceTypeAttributes
.get(fTraceTypeId
);
211 IConfigurationElement
[] defaultEditorCE
= ce
.getChildren(TmfTraceType
.DEFAULT_EDITOR_ELEM
);
212 if (defaultEditorCE
.length
== 1) {
213 return defaultEditorCE
[0].getAttribute(TmfTraceType
.ID_ATTR
);
220 * Returns the <code>TmfTraceElement</code> located under the <code>TmfTracesFolder</code>.
222 * @return <code>this</code> if this element is under the <code>TmfTracesFolder</code>
223 * else the corresponding <code>TmfTraceElement</code> if this element is under
224 * <code>TmfExperimentElement</code>.
226 public TmfTraceElement
getElementUnderTraceFolder() {
228 // If trace is under an experiment, return original trace from the traces folder
229 if (getParent() instanceof TmfExperimentElement
) {
230 for (TmfTraceElement aTrace
: getProject().getTracesFolder().getTraces()) {
231 if (aTrace
.getName().equals(getName())) {
240 * Deletes the trace specific supplementary folder.
242 public void deleteSupplementaryFolder() {
243 IFolder supplFolder
= getTraceSupplementaryFolder(fResource
.getName());
244 if (supplFolder
.exists()) {
246 supplFolder
.delete(true, new NullProgressMonitor());
247 } catch (CoreException e
) {
248 TmfUiPlugin
.getDefault().logError("Error deleting supplementary folder " + supplFolder
, e
); //$NON-NLS-1$
254 * Renames the trace specific supplementary folder according to the new trace name.
256 * @param newTraceName The new trace name
258 public void renameSupplementaryFolder(String newTraceName
) {
259 IFolder oldSupplFolder
= getTraceSupplementaryFolder(fResource
.getName());
260 IFolder newSupplFolder
= getTraceSupplementaryFolder(newTraceName
);
262 // Rename supplementary folder
263 if (oldSupplFolder
.exists()) {
265 oldSupplFolder
.move(newSupplFolder
.getFullPath(), true, new NullProgressMonitor());
266 } catch (CoreException e
) {
267 TmfUiPlugin
.getDefault().logError("Error renaming supplementary folder " + oldSupplFolder
, e
); //$NON-NLS-1$
273 * Copies the trace specific supplementary folder to the new trace name.
275 * @param newTraceName The new trace name
277 public void copySupplementaryFolder(String newTraceName
) {
278 IFolder oldSupplFolder
= getTraceSupplementaryFolder(fResource
.getName());
279 IFolder newSupplFolder
= getTraceSupplementaryFolder(newTraceName
);
281 // copy supplementary folder
282 if (oldSupplFolder
.exists()) {
284 oldSupplFolder
.copy(newSupplFolder
.getFullPath(), true, new NullProgressMonitor());
285 } catch (CoreException e
) {
286 TmfUiPlugin
.getDefault().logError("Error renaming supplementary folder " + oldSupplFolder
, e
); //$NON-NLS-1$
292 * Copies the trace specific supplementary folder a new folder.
294 * @param destination The destination folder to copy to.
296 public void copySupplementaryFolder(IFolder destination
) {
297 IFolder oldSupplFolder
= getTraceSupplementaryFolder(fResource
.getName());
299 // copy supplementary folder
300 if (oldSupplFolder
.exists()) {
302 oldSupplFolder
.copy(destination
.getFullPath(), true, new NullProgressMonitor());
303 } catch (CoreException e
) {
304 TmfUiPlugin
.getDefault().logError("Error renaming supplementary folder " + oldSupplFolder
, e
); //$NON-NLS-1$
311 * Refreshes the trace specific supplementary folder information. It creates the folder if not exists.
312 * It sets the persistence property of the trace resource
314 public void refreshSupplementaryFolder() {
315 createSupplementaryDirectory();
319 * Checks if supplementary resource exist or not.
321 * @return <code>true</code> if one or more files are under the trace supplementary folder
323 public boolean hasSupplementaryResources() {
324 IResource
[] resources
= getSupplementaryResources();
325 return (resources
.length
> 0);
329 * Returns the supplementary resources under the trace supplementary folder.
331 * @return array of resources under the trace supplementary folder.
333 public IResource
[] getSupplementaryResources() {
334 IFolder supplFolder
= getTraceSupplementaryFolder(fResource
.getName());
335 if (supplFolder
.exists()) {
337 return supplFolder
.members();
338 } catch (CoreException e
) {
339 TmfUiPlugin
.getDefault().logError("Error deleting supplementary folder " + supplFolder
, e
); //$NON-NLS-1$
342 return new IResource
[0];
346 * Deletes the given resources.
348 * @param resources array of resources to delete.
350 public void deleteSupplementaryResources(IResource
[] resources
) {
352 for (int i
= 0; i
< resources
.length
; i
++) {
354 resources
[i
].delete(true, new NullProgressMonitor());
355 } catch (CoreException e
) {
356 TmfUiPlugin
.getDefault().logError("Error deleting supplementary resource " + resources
[i
], e
); //$NON-NLS-1$
361 private void createSupplementaryDirectory() {
362 IFolder supplFolder
= getTraceSupplementaryFolder(fResource
.getName());
363 if (!supplFolder
.exists()) {
365 supplFolder
.create(true, true, new NullProgressMonitor());
366 } catch (CoreException e
) {
367 TmfUiPlugin
.getDefault().logError("Error creating resource supplementary file " + supplFolder
, e
); //$NON-NLS-1$
372 fResource
.setPersistentProperty(TmfCommonConstants
.TRACE_SUPPLEMENTARY_FOLDER
, supplFolder
.getLocationURI().getPath());
373 } catch (CoreException e
) {
374 TmfUiPlugin
.getDefault().logError("Error setting persistant property " + TmfCommonConstants
.TRACE_SUPPLEMENTARY_FOLDER
, e
); //$NON-NLS-1$
379 // ------------------------------------------------------------------------
381 // ------------------------------------------------------------------------
384 public boolean testAttribute(Object target
, String name
, String value
) {
385 if (name
.equals(IS_LINKED
)) {
386 boolean isLinked
= getResource().isLinked();
387 return Boolean
.toString(isLinked
).equals(value
);
392 // ------------------------------------------------------------------------
394 // ------------------------------------------------------------------------
397 public TmfProjectElement
getProject() {
398 if (getParent() instanceof TmfTraceFolder
) {
399 TmfTraceFolder folder
= (TmfTraceFolder
) getParent();
400 TmfProjectElement project
= (TmfProjectElement
) folder
.getParent();
403 if (getParent() instanceof TmfExperimentElement
) {
404 TmfExperimentElement experiment
= (TmfExperimentElement
) getParent();
405 TmfExperimentFolder folder
= (TmfExperimentFolder
) experiment
.getParent();
406 TmfProjectElement project
= (TmfProjectElement
) folder
.getParent();
412 // ------------------------------------------------------------------------
414 // ------------------------------------------------------------------------
417 public Object
getEditableValue() {
422 public IPropertyDescriptor
[] getPropertyDescriptors() {
423 return (sfDescriptors
!= null) ? Arrays
.copyOf(sfDescriptors
, sfDescriptors
.length
) : null;
427 public Object
getPropertyValue(Object id
) {
429 if (sfName
.equals(id
)) {
433 if (sfPath
.equals(id
)) {
434 return getPath().toString();
437 if (sfLocation
.equals(id
)) {
438 return getLocation().toString();
441 if (sfIsLinked
.equals(id
)) {
442 return Boolean
.valueOf(getResource().isLinked()).toString();
445 if (sfEventType
.equals(id
)) {
446 if (fTraceTypeId
!= null) {
447 IConfigurationElement ce
= sfTraceTypeAttributes
.get(fTraceTypeId
);
448 return (ce
!= null) ?
(getCategory(ce
) + " : " + ce
.getAttribute(TmfTraceType
.NAME_ATTR
)) : ""; //$NON-NLS-1$ //$NON-NLS-2$
455 private String
getCategory(IConfigurationElement ce
) {
456 String categoryId
= ce
.getAttribute(TmfTraceType
.CATEGORY_ATTR
);
457 if (categoryId
!= null) {
458 IConfigurationElement category
= sfTraceCategories
.get(categoryId
);
459 if (category
!= null) {
460 return category
.getAttribute(TmfTraceType
.NAME_ATTR
);
463 return "[no category]"; //$NON-NLS-1$
467 public void resetPropertyValue(Object id
) {
471 public void setPropertyValue(Object id
, Object value
) {
475 public boolean isPropertyResettable(Object id
) {
480 public boolean isPropertySet(Object id
) {