TMF: Add supplementary folder to experiments
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / project / model / TmfTraceElement.java
index c465ea79e3e4e69321305cbaad7bf4a8825349e9..20d985203638ecb6f4a3b251cd14cbc88944b32b 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2010, 2011 Ericsson
+ * Copyright (c) 2010, 2011, 2012 Ericsson
  *
  * All rights reserved. This program and the accompanying materials are
  * made available under the terms of the Eclipse Public License v1.0 which
@@ -8,10 +8,14 @@
  *
  * Contributors:
  *   Francois Chouinard - Initial API and implementation
+ *   Bernd Hufmann - Added supplementary files handling
+ *   Geneviève Bastien - Moved supplementary files handling to parent class
  *******************************************************************************/
 
 package org.eclipse.linuxtools.tmf.ui.project.model;
 
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
 import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Map;
@@ -21,20 +25,18 @@ import org.eclipse.core.resources.IFolder;
 import org.eclipse.core.resources.IResource;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IConfigurationElement;
-import org.eclipse.core.runtime.IStatus;
-import org.eclipse.core.runtime.NullProgressMonitor;
 import org.eclipse.core.runtime.Platform;
-import org.eclipse.core.runtime.QualifiedName;
-import org.eclipse.core.runtime.Status;
-import org.eclipse.linuxtools.internal.tmf.ui.TmfUiPlugin;
+import org.eclipse.linuxtools.internal.tmf.ui.Activator;
 import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomTxtEvent;
 import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomTxtTrace;
 import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomTxtTraceDefinition;
 import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomXmlEvent;
 import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomXmlTrace;
 import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomXmlTraceDefinition;
+import org.eclipse.linuxtools.tmf.core.TmfCommonConstants;
 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
+import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;
 import org.eclipse.linuxtools.tmf.ui.editors.TmfEventsEditor;
 import org.eclipse.ui.IActionFilter;
 import org.eclipse.ui.views.properties.IPropertyDescriptor;
@@ -42,22 +44,27 @@ import org.eclipse.ui.views.properties.IPropertySource2;
 import org.eclipse.ui.views.properties.TextPropertyDescriptor;
 
 /**
- * <b><u>TmfTraceElement</u></b>
- * <p>
+ * Implementation of trace model element representing a trace. It provides methods to instantiate
+ * <code>ITmfTrace</code> and <code>ITmfEvent</code> as well as editor ID from the trace type
+ * extension definition.
+ *
+ * @version 1.0
+ * @author Francois Chouinard
  */
-public class TmfTraceElement extends TmfProjectModelElement implements IActionFilter, IPropertySource2 {
+public class TmfTraceElement extends TmfWithFolderElement implements IActionFilter, IPropertySource2 {
 
     // ------------------------------------------------------------------------
     // Constants
     // ------------------------------------------------------------------------
 
-    // Property keys
-    public static final QualifiedName TRACEBUNDLE = new QualifiedName("org.eclipse.linuxtools.tmf", "tracetype.bundle"); //$NON-NLS-1$//$NON-NLS-2$
-    public static final QualifiedName TRACETYPE = new QualifiedName("org.eclipse.linuxtools.tmf", "tracetype.id"); //$NON-NLS-1$//$NON-NLS-2$
-    public static final QualifiedName TRACEICON = new QualifiedName("org.eclipse.linuxtools.tmf", "tracetype.icon"); //$NON-NLS-1$//$NON-NLS-2$
-
     // Other attributes
+    /**
+     * Bundle attribute name
+     */
     public static final String BUNDLE = "bundle"; //$NON-NLS-1$
+    /**
+     * IsLinked attribute name.
+     */
     public static final String IS_LINKED = "isLinked"; //$NON-NLS-1$
 
     // Property View stuff
@@ -85,9 +92,8 @@ public class TmfTraceElement extends TmfProjectModelElement implements IActionFi
         sfIsLinkedDescriptor.setCategory(sfInfoCategory);
     }
 
-    private static final String[] SUPPLEMENATARY_FILES_EXTENSIONS = { ".ht" }; //$NON-NLS-1$
-    public static final String SUPPLEMENATARY_FILES_PREFIX = "."; //$NON-NLS-1$
-    
+    private static final String BOOKMARKS_HIDDEN_FILE = ".bookmarks"; //$NON-NLS-1$
+
     // ------------------------------------------------------------------------
     // Attributes
     // ------------------------------------------------------------------------
@@ -103,7 +109,9 @@ public class TmfTraceElement extends TmfProjectModelElement implements IActionFi
     private static final Map<String, IConfigurationElement> sfTraceTypeAttributes = new HashMap<String, IConfigurationElement>();
     private static final Map<String, IConfigurationElement> sfTraceCategories = new HashMap<String, IConfigurationElement>();
 
-    // Initialize statically at startup
+    /**
+     *  Initialize statically at startup by getting extensions from the platform extension registry.
+     */
     public static void init() {
         IConfigurationElement[] config = Platform.getExtensionRegistry().getConfigurationElementsFor(TmfTraceType.TMF_TRACE_TYPE_ID);
         for (IConfigurationElement ce : config) {
@@ -121,11 +129,23 @@ public class TmfTraceElement extends TmfProjectModelElement implements IActionFi
     // ------------------------------------------------------------------------
     // Constructors
     // ------------------------------------------------------------------------
-
+    /**
+     * Constructor.
+     * Creates trace model element under the trace folder.
+     * @param name The name of trace
+     * @param trace The trace resource.
+     * @param parent The parent element (trace folder)
+     */
     public TmfTraceElement(String name, IResource trace, TmfTraceFolder parent) {
         this(name, trace, (TmfProjectModelElement) parent);
     }
-
+    /**
+     * Constructor.
+     * Creates trace model element under the experiment folder.
+     * @param name The name of trace
+     * @param trace The trace resource.
+     * @param parent The parent element (experiment folder)
+     */
     public TmfTraceElement(String name, IResource trace, TmfExperimentElement parent) {
         this(name, trace, (TmfProjectModelElement) parent);
     }
@@ -139,21 +159,37 @@ public class TmfTraceElement extends TmfProjectModelElement implements IActionFi
     // ------------------------------------------------------------------------
     // Operations
     // ------------------------------------------------------------------------
-
+    /**
+     * Returns the trace type ID.
+     * @return trace type ID.
+     */
     public String getTraceType() {
         return fTraceTypeId;
     }
 
+    /**
+     * Refreshes the trace type filed by reading the trace type persistent property of the resource
+     * referenece.
+     */
     public void refreshTraceType() {
         try {
-            fTraceTypeId = getResource().getPersistentProperty(TRACETYPE);
+            fTraceTypeId = getResource().getPersistentProperty(TmfCommonConstants.TRACETYPE);
         } catch (CoreException e) {
-            e.printStackTrace();
+            Activator.getDefault().logError("Error refreshing trace type pesistent property for trace " + getName(), e); //$NON-NLS-1$
         }
     }
 
-    public ITmfTrace<?> instantiateTrace() {
+    /**
+     * Instantiate a <code>ITmfTrace</code> object based on the trace type and the corresponding extension.
+     *
+     * @return the <code>ITmfTrace</code> or <code>null</code> for an error
+     */
+    public ITmfTrace instantiateTrace() {
         try {
+
+            // make sure that supplementary folder exists
+            refreshSupplementaryFolder();
+
             if (fTraceTypeId != null) {
                 if (fTraceTypeId.startsWith(CustomTxtTrace.class.getCanonicalName())) {
                     for (CustomTxtTraceDefinition def : CustomTxtTraceDefinition.loadAll()) {
@@ -170,15 +206,20 @@ public class TmfTraceElement extends TmfProjectModelElement implements IActionFi
                     }
                 }
                 IConfigurationElement ce = sfTraceTypeAttributes.get(fTraceTypeId);
-                ITmfTrace<?> trace = (ITmfTrace<?>) ce.createExecutableExtension(TmfTraceType.TRACE_TYPE_ATTR);
+                ITmfTrace trace = (ITmfTrace) ce.createExecutableExtension(TmfTraceType.TRACE_TYPE_ATTR);
                 return trace;
             }
         } catch (CoreException e) {
-            e.printStackTrace();
+            Activator.getDefault().logError("Error instantiating ITmfTrace object for trace " + getName(), e); //$NON-NLS-1$
         }
         return null;
     }
 
+    /**
+     * Instantiate a <code>ITmfEvent</code> object based on the trace type and the corresponding extension.
+     *
+     * @return the <code>ITmfEvent</code> or <code>null</code> for an error
+     */
     public ITmfEvent instantiateEvent() {
         try {
             if (fTraceTypeId != null) {
@@ -201,11 +242,15 @@ public class TmfTraceElement extends TmfProjectModelElement implements IActionFi
                 return event;
             }
         } catch (CoreException e) {
-            e.printStackTrace();
+            Activator.getDefault().logError("Error instantiating ITmfEvent object for trace " + getName(), e); //$NON-NLS-1$
         }
         return null;
     }
 
+    /**
+     * Returns the optional editor ID from the trace type extension.
+     * @return the editor ID or <code>null</code> if not defined.
+     */
     public String getEditorId() {
         if (fTraceTypeId != null) {
             if (fTraceTypeId.startsWith(CustomTxtTrace.class.getCanonicalName())) {
@@ -222,84 +267,71 @@ public class TmfTraceElement extends TmfProjectModelElement implements IActionFi
         }
         return null;
     }
-    
-    public TmfTraceElement getElementUnderTraceFolder() {
-        
-        // If trace is under an experiment, return original trace from the traces folder
-        if (getParent() instanceof TmfExperimentElement) {
-            for (TmfTraceElement aTrace : getProject().getTracesFolder().getTraces()) {
-                if (aTrace.getName().equals(getName())) {
-                    return aTrace;
-                }
-            }
-        }
-        return this;
-    }
 
-    public boolean hasSupplementaryFiles() {
-        // Check for all supplementary files
-        for (int i = 0; i < SUPPLEMENATARY_FILES_EXTENSIONS.length; i++) {
-            IFile supplFile = createSupplemenatryFile(fResource.getName(), SUPPLEMENATARY_FILES_EXTENSIONS[i]);
-
-            if (supplFile.exists()) {
-                return true;
-            }
-        }
-        return false;
-    }
-    
-    public void deleteSupplementaryFiles() {
-        // Delete all supplementary files
-        for (int i = 0; i < SUPPLEMENATARY_FILES_EXTENSIONS.length; i++) {
-            IFile supplFile = createSupplemenatryFile(fResource.getName(), SUPPLEMENATARY_FILES_EXTENSIONS[i]);
-            if (supplFile.exists()) {
-                try {
-                    supplFile.delete(true, new NullProgressMonitor());
-                } catch (CoreException e) {
-                    TmfUiPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, TmfUiPlugin.PLUGIN_ID, "Error deleting resource supplementary file " + supplFile, e)); //$NON-NLS-1$
+    /**
+     * Returns the file resource used to store bookmarks after creating it if necessary.
+     * If the trace resource is a file, it is returned directly.
+     * If the trace resource is a folder, a linked file is returned.
+     * The file will be created if it does not exist.
+     * @return the bookmarks file
+     * @throws CoreException if the bookmarks file cannot be created
+     * @since 2.0
+     */
+    public IFile createBookmarksFile() throws CoreException {
+        IFile file = getBookmarksFile();
+        if (fResource instanceof IFolder) {
+            if (!file.exists()) {
+                final IFile bookmarksFile = getProject().getTracesFolder().getResource().getFile(BOOKMARKS_HIDDEN_FILE);
+                if (!bookmarksFile.exists()) {
+                    final InputStream source = new ByteArrayInputStream(new byte[0]);
+                    bookmarksFile.create(source, true, null);
                 }
+                bookmarksFile.setHidden(true);
+                file.createLink(bookmarksFile.getLocation(), IResource.REPLACE, null);
+                file.setHidden(true);
+                file.setPersistentProperty(TmfCommonConstants.TRACETYPE, TmfTrace.class.getCanonicalName());
             }
         }
+        return file;
     }
 
-    public void renameSupplementaryFiles(String newTraceName) {
-        // Rename all supplementary files
-        for (int i = 0; i < SUPPLEMENATARY_FILES_EXTENSIONS.length; i++) {
-            IFile oldSupplFile = createSupplemenatryFile(fResource.getName(), SUPPLEMENATARY_FILES_EXTENSIONS[i]);
-            IFile newSupplFile = createSupplemenatryFile(newTraceName, SUPPLEMENATARY_FILES_EXTENSIONS[i]);
-
-            if (oldSupplFile.exists()) {
-                try {
-                    oldSupplFile.move(newSupplFile.getFullPath(), true, new NullProgressMonitor());
-                } catch (CoreException e) {
-                    TmfUiPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, TmfUiPlugin.PLUGIN_ID, "Error renaming resource supplementary file " + oldSupplFile, e)); //$NON-NLS-1$
-                }
-            }
+    /**
+     * Returns the file resource used to store bookmarks.
+     * The file may not exist.
+     * @return the bookmarks file
+     * @since 2.0
+     */
+    public IFile getBookmarksFile() {
+        IFile file = null;
+        if (fResource instanceof IFile) {
+            file = (IFile) fResource;
+        } else if (fResource instanceof IFolder) {
+            final IFolder folder = (IFolder) fResource;
+            file = folder.getFile(getName() + '_');
         }
+        return file;
     }
-    
-    public void copySupplementaryFiles(String newTraceName) {
-        // Copy all supplementary files
-        for (int i = 0; i < SUPPLEMENATARY_FILES_EXTENSIONS.length; i++) {
-            IFile oldSupplFile = createSupplemenatryFile(fResource.getName(), SUPPLEMENATARY_FILES_EXTENSIONS[i]);
-            IFile newSupplFile = createSupplemenatryFile(newTraceName, SUPPLEMENATARY_FILES_EXTENSIONS[i]);
-
-            if (oldSupplFile.exists()) {
-                try {
-                    oldSupplFile.copy(newSupplFile.getFullPath(), true, new NullProgressMonitor());
-                } catch (CoreException e) {
-                    TmfUiPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, TmfUiPlugin.PLUGIN_ID, "Error copying resource supplementary file " + oldSupplFile, e)); //$NON-NLS-1$
+
+    /**
+     * Returns the <code>TmfTraceElement</code> located under the <code>TmfTracesFolder</code>.
+     *
+     * @return <code>this</code> if this element is under the <code>TmfTracesFolder</code>
+     *         else the corresponding <code>TmfTraceElement</code> if this element is under
+     *         <code>TmfExperimentElement</code>.
+     */
+    public TmfTraceElement getElementUnderTraceFolder() {
+
+        // If trace is under an experiment, return original trace from the traces folder
+        if (getParent() instanceof TmfExperimentElement) {
+            for (TmfTraceElement aTrace : getProject().getTracesFolder().getTraces()) {
+                if (aTrace.getName().equals(getName())) {
+                    return aTrace;
                 }
             }
         }
+        return this;
     }
-    
-    private IFile createSupplemenatryFile(String fileName, String extension) {
-        String name = SUPPLEMENATARY_FILES_PREFIX + fileName + extension;
-        IFolder folder = (IFolder)fResource.getParent();
-        return folder.getFile(name);
-    }
-    
+
     // ------------------------------------------------------------------------
     // IActionFilter
     // ------------------------------------------------------------------------
@@ -316,7 +348,10 @@ public class TmfTraceElement extends TmfProjectModelElement implements IActionFi
     // ------------------------------------------------------------------------
     // TmfTraceElement
     // ------------------------------------------------------------------------
-
+    /*
+     * (non-Javadoc)
+     * @see org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement#getProject()
+     */
     @Override
     public TmfProjectElement getProject() {
         if (getParent() instanceof TmfTraceFolder) {
@@ -337,16 +372,28 @@ public class TmfTraceElement extends TmfProjectModelElement implements IActionFi
     // IPropertySource2
     // ------------------------------------------------------------------------
 
+    /*
+     * (non-Javadoc)
+     * @see org.eclipse.ui.views.properties.IPropertySource#getEditableValue()
+     */
     @Override
     public Object getEditableValue() {
         return null;
     }
 
+    /*
+     * (non-Javadoc)
+     * @see org.eclipse.ui.views.properties.IPropertySource#getPropertyDescriptors()
+     */
     @Override
     public IPropertyDescriptor[] getPropertyDescriptors() {
-        return (sfDescriptors != null) ? Arrays.copyOf(sfDescriptors, sfDescriptors.length) : null;
+        return Arrays.copyOf(sfDescriptors, sfDescriptors.length);
     }
 
+    /*
+     * (non-Javadoc)
+     * @see org.eclipse.ui.views.properties.IPropertySource#getPropertyValue(java.lang.Object)
+     */
     @Override
     public Object getPropertyValue(Object id) {
 
@@ -376,7 +423,7 @@ public class TmfTraceElement extends TmfProjectModelElement implements IActionFi
         return null;
     }
 
-    private String getCategory(IConfigurationElement ce) {
+    private static String getCategory(IConfigurationElement ce) {
         String categoryId = ce.getAttribute(TmfTraceType.CATEGORY_ATTR);
         if (categoryId != null) {
             IConfigurationElement category = sfTraceCategories.get(categoryId);
@@ -387,19 +434,35 @@ public class TmfTraceElement extends TmfProjectModelElement implements IActionFi
         return "[no category]"; //$NON-NLS-1$
     }
 
+    /*
+     * (non-Javadoc)
+     * @see org.eclipse.ui.views.properties.IPropertySource#resetPropertyValue(java.lang.Object)
+     */
     @Override
     public void resetPropertyValue(Object id) {
     }
 
+    /*
+     * (non-Javadoc)
+     * @see org.eclipse.ui.views.properties.IPropertySource#setPropertyValue(java.lang.Object, java.lang.Object)
+     */
     @Override
     public void setPropertyValue(Object id, Object value) {
     }
 
+    /*
+     * (non-Javadoc)
+     * @see org.eclipse.ui.views.properties.IPropertySource2#isPropertyResettable(java.lang.Object)
+     */
     @Override
     public boolean isPropertyResettable(Object id) {
         return false;
     }
 
+    /*
+     * (non-Javadoc)
+     * @see org.eclipse.ui.views.properties.IPropertySource2#isPropertySet(java.lang.Object)
+     */
     @Override
     public boolean isPropertySet(Object id) {
         return false;
This page took 0.030163 seconds and 5 git commands to generate.