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 e868b74580f607a1d11928ffdb34481e58cd76d3..20d985203638ecb6f4a3b251cd14cbc88944b32b 100644 (file)
@@ -9,19 +9,22 @@
  * 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;
 
+import org.eclipse.core.resources.IFile;
 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.NullProgressMonitor;
 import org.eclipse.core.runtime.Platform;
 import org.eclipse.linuxtools.internal.tmf.ui.Activator;
 import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomTxtEvent;
@@ -33,6 +36,7 @@ import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomXmlTraceDefin
 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;
@@ -43,11 +47,11 @@ import org.eclipse.ui.views.properties.TextPropertyDescriptor;
  * 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
@@ -87,7 +91,9 @@ public class TmfTraceElement extends TmfProjectModelElement implements IActionFi
         sfTypeDescriptor.setCategory(sfInfoCategory);
         sfIsLinkedDescriptor.setCategory(sfInfoCategory);
     }
-    
+
+    private static final String BOOKMARKS_HIDDEN_FILE = ".bookmarks"; //$NON-NLS-1$
+
     // ------------------------------------------------------------------------
     // Attributes
     // ------------------------------------------------------------------------
@@ -124,7 +130,7 @@ public class TmfTraceElement extends TmfProjectModelElement implements IActionFi
     // Constructors
     // ------------------------------------------------------------------------
     /**
-     * Constructor. 
+     * Constructor.
      * Creates trace model element under the trace folder.
      * @param name The name of trace
      * @param trace The trace resource.
@@ -134,7 +140,7 @@ public class TmfTraceElement extends TmfProjectModelElement implements IActionFi
         this(name, trace, (TmfProjectModelElement) parent);
     }
     /**
-     * Constructor. 
+     * Constructor.
      * Creates trace model element under the experiment folder.
      * @param name The name of trace
      * @param trace The trace resource.
@@ -162,7 +168,7 @@ public class TmfTraceElement extends TmfProjectModelElement implements IActionFi
     }
 
     /**
-     * Refreshes the trace type filed by reading the trace type persistent property of the resource 
+     * Refreshes the trace type filed by reading the trace type persistent property of the resource
      * referenece.
      */
     public void refreshTraceType() {
@@ -174,11 +180,11 @@ public class TmfTraceElement extends TmfProjectModelElement implements IActionFi
     }
 
     /**
-     * Instantiate a <code>ITmfTrace</code> object based on the trace type and the corresponding extension. 
-     * 
+     * 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() {
+    public ITmfTrace instantiateTrace() {
         try {
 
             // make sure that supplementary folder exists
@@ -200,7 +206,7 @@ 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) {
@@ -210,8 +216,8 @@ public class TmfTraceElement extends TmfProjectModelElement implements IActionFi
     }
 
     /**
-     * Instantiate a <code>ITmfEvent</code> object based on the trace type and the corresponding extension. 
-     * 
+     * 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() {
@@ -263,163 +269,67 @@ public class TmfTraceElement extends TmfProjectModelElement implements IActionFi
     }
 
     /**
-     * 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>.
+     * 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 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;
+    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 this;
-    }
-    
-    /**
-     * Deletes the trace specific supplementary folder.
-     */
-    public void deleteSupplementaryFolder() {
-        IFolder supplFolder = getTraceSupplementaryFolder(fResource.getName());
-        if (supplFolder.exists()) {
-            try {
-                supplFolder.delete(true, new NullProgressMonitor());
-            } catch (CoreException e) {
-                Activator.getDefault().logError("Error deleting supplementary folder " + supplFolder, e); //$NON-NLS-1$
-            }
-        }
-    }
-
-    /**
-     * Renames the trace specific supplementary folder according to the new trace name.
-     * 
-     * @param newTraceName The new trace name 
-     */
-    public void renameSupplementaryFolder(String newTraceName) {
-        IFolder oldSupplFolder = getTraceSupplementaryFolder(fResource.getName());
-        IFolder newSupplFolder =  getTraceSupplementaryFolder(newTraceName);
-
-        // Rename supplementary folder
-        if (oldSupplFolder.exists()) {
-            try {
-                oldSupplFolder.move(newSupplFolder.getFullPath(), true, new NullProgressMonitor());
-            } catch (CoreException e) {
-                Activator.getDefault().logError("Error renaming supplementary folder " + oldSupplFolder, e); //$NON-NLS-1$
-            }
-        }
+        return file;
     }
 
     /**
-     * Copies the trace specific supplementary folder to the new trace name.
-     * 
-     * @param newTraceName The new trace name 
+     * Returns the file resource used to store bookmarks.
+     * The file may not exist.
+     * @return the bookmarks file
+     * @since 2.0
      */
-    public void copySupplementaryFolder(String newTraceName) {
-        IFolder oldSupplFolder = getTraceSupplementaryFolder(fResource.getName());
-        IFolder newSupplFolder = getTraceSupplementaryFolder(newTraceName);
-
-        // copy supplementary folder
-        if (oldSupplFolder.exists()) {
-            try {
-                oldSupplFolder.copy(newSupplFolder.getFullPath(), true, new NullProgressMonitor());
-            } catch (CoreException e) {
-                Activator.getDefault().logError("Error renaming supplementary folder " + oldSupplFolder, e); //$NON-NLS-1$
-            }
-        }
-    }
-
-    /**
-     * Copies the trace specific supplementary folder a new folder.
-     * 
-     * @param destination The destination folder to copy to. 
-     */
-    public void copySupplementaryFolder(IFolder destination) {
-        IFolder oldSupplFolder = getTraceSupplementaryFolder(fResource.getName());
-
-        // copy supplementary folder
-        if (oldSupplFolder.exists()) {
-            try {
-                oldSupplFolder.copy(destination.getFullPath(), true, new NullProgressMonitor());
-            } catch (CoreException e) {
-                Activator.getDefault().logError("Error copying supplementary folder " + oldSupplFolder, e); //$NON-NLS-1$
-            }
+    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;
     }
 
-    
     /**
-     * Refreshes the trace specific supplementary folder information. It creates the folder if not exists.
-     * It sets the persistence property of the trace resource  
-     */
-    public void refreshSupplementaryFolder() {
-        createSupplementaryDirectory();
-    }
-
-    /**
-     * Checks if supplementary resource exist or not.
-     *  
-     * @return <code>true</code> if one or more files are under the trace supplementary folder
-     */
-    public boolean hasSupplementaryResources() {
-        IResource[] resources = getSupplementaryResources();
-        return (resources.length > 0);
-    }
-    
-    /**
-     * Returns the supplementary resources under the trace supplementary folder.
-     *  
-     * @return array of resources under the trace supplementary folder.
-     */
-    public IResource[] getSupplementaryResources() {
-        IFolder supplFolder = getTraceSupplementaryFolder(fResource.getName());
-        if (supplFolder.exists()) {
-            try {
-                return supplFolder.members();
-            } catch (CoreException e) {
-                Activator.getDefault().logError("Error deleting supplementary folder " + supplFolder, e); //$NON-NLS-1$
-            }
-        }
-        return new IResource[0];
-    }
-
-    /**
-     * Deletes the given resources.  
-     * 
-     * @param resources array of resources to delete.
+     * 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 void deleteSupplementaryResources(IResource[] resources) {
-        
-        for (int i = 0; i < resources.length; i++) {
-            try {
-                resources[i].delete(true, new NullProgressMonitor());
-            } catch (CoreException e) {
-                Activator.getDefault().logError("Error deleting supplementary resource " + resources[i], e); //$NON-NLS-1$
-            }
-        }
-    }
+    public TmfTraceElement getElementUnderTraceFolder() {
 
-    private void createSupplementaryDirectory() {
-        IFolder supplFolder = getTraceSupplementaryFolder(fResource.getName());
-        if (!supplFolder.exists()) {
-            try {
-                supplFolder.create(true, true, new NullProgressMonitor());
-            } catch (CoreException e) {
-                Activator.getDefault().logError("Error creating resource supplementary file " + supplFolder, e); //$NON-NLS-1$
+        // 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;
+                }
             }
         }
-
-        try {
-            fResource.setPersistentProperty(TmfCommonConstants.TRACE_SUPPLEMENTARY_FOLDER, supplFolder.getLocationURI().getPath());
-        } catch (CoreException e) {
-            Activator.getDefault().logError("Error setting persistant property " + TmfCommonConstants.TRACE_SUPPLEMENTARY_FOLDER, e); //$NON-NLS-1$
-        }
-        
+        return this;
     }
 
     // ------------------------------------------------------------------------
@@ -477,7 +387,7 @@ public class TmfTraceElement extends TmfProjectModelElement implements IActionFi
      */
     @Override
     public IPropertyDescriptor[] getPropertyDescriptors() {
-        return (sfDescriptors != null) ? Arrays.copyOf(sfDescriptors, sfDescriptors.length) : null;
+        return Arrays.copyOf(sfDescriptors, sfDescriptors.length);
     }
 
     /*
@@ -513,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);
This page took 0.02856 seconds and 5 git commands to generate.