tmf: Support folders in package import
authorMarc-Andre Laperle <marc-andre.laperle@ericsson.com>
Mon, 5 May 2014 23:06:45 +0000 (19:06 -0400)
committerMarc-Andre Laperle <marc-andre.laperle@ericsson.com>
Fri, 9 May 2014 23:51:16 +0000 (19:51 -0400)
Change-Id: I2338b8e250c7afda9d8c576db56b76b00429b9a6
Signed-off-by: Marc-Andre Laperle <marc-andre.laperle@ericsson.com>
Reviewed-on: https://git.eclipse.org/r/26232
Tested-by: Hudson CI
org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/internal/tmf/ui/project/wizards/tracepkg/TracePackageTraceElement.java
org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/internal/tmf/ui/project/wizards/tracepkg/importexport/ImportTracePackageWizardPage.java
org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/internal/tmf/ui/project/wizards/tracepkg/importexport/TracePackageImportOperation.java

index 84f862b28752d7b5428abdf3714b7f74239581dc..56bea290aa3876d0da5c824a1135ebfb02b4cd1f 100644 (file)
@@ -12,6 +12,9 @@
 
 package org.eclipse.linuxtools.internal.tmf.ui.project.wizards.tracepkg;
 
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.linuxtools.tmf.ui.project.model.TmfCommonProjectElement;
 import org.eclipse.linuxtools.tmf.ui.project.model.TmfNavigatorLabelProvider;
 import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement;
 import org.eclipse.swt.graphics.Image;
@@ -62,7 +65,26 @@ public class TracePackageTraceElement extends TracePackageElement {
 
     @Override
     public String getText() {
-        return fTraceElement != null ? fTraceElement.getElementPath() : fImportName;
+        return fTraceElement != null ? fTraceElement.getElementPath() : getDestinationElementPath();
+    }
+
+    /**
+     * Return the target TmfCommonProjectElement element path for a given trace
+     * package element. {@link TmfCommonProjectElement#getElementPath()}
+     *
+     * @return the element path
+     */
+    public String getDestinationElementPath() {
+        String traceName = getImportName();
+        for (TracePackageElement element : getChildren()) {
+            if (element instanceof TracePackageFilesElement) {
+                TracePackageFilesElement tracePackageFilesElement = (TracePackageFilesElement) element;
+                IPath filePath = new Path(tracePackageFilesElement.getFileName());
+                return filePath.removeLastSegments(1).append(traceName).toString();
+            }
+        }
+
+        return traceName;
     }
 
     /**
@@ -72,6 +94,13 @@ public class TracePackageTraceElement extends TracePackageElement {
         return fTraceElement;
     }
 
+    /**
+     * @return the import name
+     */
+    public String getImportName() {
+        return fImportName;
+    }
+
     /**
      * @return the trace type of this trace
      */
index a10a0ee02f0a357c68988558793526d951c1b035..6787584d4e777d50b989c26c8210ca329ac3138d 100644 (file)
@@ -19,6 +19,7 @@ import java.util.ArrayList;
 import java.util.List;
 
 import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IResource;
 import org.eclipse.core.resources.ResourcesPlugin;
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.IStatus;
@@ -36,7 +37,6 @@ import org.eclipse.linuxtools.internal.tmf.ui.project.wizards.tracepkg.TracePack
 import org.eclipse.linuxtools.internal.tmf.ui.project.wizards.tracepkg.TracePackageTraceElement;
 import org.eclipse.linuxtools.tmf.core.TmfCommonConstants;
 import org.eclipse.linuxtools.tmf.ui.project.model.TmfProjectRegistry;
-import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement;
 import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder;
 import org.eclipse.linuxtools.tmf.ui.project.model.TraceUtils;
 import org.eclipse.swt.SWT;
@@ -359,9 +359,8 @@ public class ImportTracePackageWizardPage extends AbstractTracePackageWizardPage
                 continue;
             }
 
-            String traceName = traceElement.getText();
-            if (traceExists(traceName)) {
-                int returnCode = promptForOverwrite(traceName);
+            if (traceExists(traceElement)) {
+                int returnCode = promptForOverwrite(traceElement.getDestinationElementPath());
                 // The return code is an index to a button in the dialog but the
                 // 'X' button in the window corner is not considered a button
                 // therefore it returns -1 and unfortunately, there is no
@@ -394,15 +393,9 @@ public class ImportTracePackageWizardPage extends AbstractTracePackageWizardPage
         return true;
     }
 
-    private boolean traceExists(String traceName) {
-        List<TmfTraceElement> traces = fTmfTraceFolder.getTraces();
-        for (TmfTraceElement t : traces) {
-            if (t.getName().equals(traceName)) {
-                return true;
-            }
-        }
-
-        return false;
+    private boolean traceExists(TracePackageTraceElement traceElement) {
+        IResource traceRes = fTmfTraceFolder.getResource().findMember(traceElement.getDestinationElementPath());
+        return traceRes != null;
     }
 
     private int promptForOverwrite(String traceName) {
index f6519e3fb15420093417fbfb61ec08a9996e958b..c7c87ab89c013dab9203550fd7873dfa2cd4dfa6 100644 (file)
@@ -198,7 +198,7 @@ public class TracePackageImportOperation extends AbstractTracePackageOperation i
 
                     if (element instanceof TracePackageFilesElement) {
                         traceFilesElement = (TracePackageFilesElement) element;
-                        setStatus(importTraceFiles(traceFilesElement, progressMonitor));
+                        setStatus(importTraceFiles(traceFilesElement, traceElement, progressMonitor));
 
                     } else if (element instanceof TracePackageSupplFilesElement) {
                         TracePackageSupplFilesElement suppFilesElement = (TracePackageSupplFilesElement) element;
@@ -209,53 +209,6 @@ public class TracePackageImportOperation extends AbstractTracePackageOperation i
                         return;
                     }
                 }
-
-                String traceName = traceElement.getText();
-                IResource traceRes = fTmfTraceFolder.getResource().findMember(traceName);
-                if (traceRes == null || !traceRes.exists()) {
-                    setStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID, MessageFormat.format(Messages.ImportTracePackageWizardPage_ErrorFindingImportedTrace, traceName)));
-                    return;
-                }
-
-                TraceTypeHelper traceType = null;
-                String traceTypeStr = traceElement.getTraceType();
-                if (traceTypeStr != null) {
-                    traceType = TmfTraceType.getInstance().getTraceType(traceTypeStr);
-                    if (traceType == null) {
-                        setStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID, MessageFormat.format(Messages.ImportTracePackageWizardPage_ErrorSettingTraceType, traceElement.getTraceType(), traceName)));
-                        return;
-                    }
-                } else {
-                    try {
-                        progressMonitor.subTask(MessageFormat.format(Messages.TracePackageImportOperation_DetectingTraceType, traceName));
-                        traceType = TmfTraceTypeUIUtils.selectTraceType(traceRes.getLocation().toOSString(), null, null);
-                    } catch (TmfTraceImportException e) {
-                        // Could not figure out the type
-                    }
-                }
-
-                if (traceType != null) {
-                    try {
-                        TmfTraceTypeUIUtils.setTraceType(traceRes, traceType);
-                    } catch (CoreException e) {
-                        setStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID, MessageFormat.format(Messages.ImportTracePackageWizardPage_ErrorSettingTraceType, traceElement.getTraceType(), traceName), e));
-                    }
-                }
-
-                importBookmarks(traceRes, traceElement, progressMonitor);
-
-                try {
-                    if (traceFilesElement != null) {
-                        URI uri = new File(getFileName()).toURI();
-                        IPath entryPath = new Path(traceFilesElement.getFileName());
-                        if (traceRes instanceof IFolder) {
-                            entryPath = entryPath.addTrailingSeparator();
-                        }
-                        String sourceLocation = URIUtil.toUnencodedString(URIUtil.toJarURI(uri, entryPath));
-                        traceRes.setPersistentProperty(TmfCommonConstants.SOURCE_LOCATION, sourceLocation);
-                    }
-                } catch (CoreException e) {
-                }
             }
 
         } catch (InterruptedException e) {
@@ -263,23 +216,48 @@ public class TracePackageImportOperation extends AbstractTracePackageOperation i
         }
     }
 
-    private IStatus deleteExistingTraces(IProgressMonitor progressMonitor) {
+    /**
+     * Returns whether or not the Files element is checked under the given trace
+     * package element
+     *
+     * @param tracePackageElement
+     *            the trace package element
+     * @return whether or not the Files element is checked under the given trace
+     *         package element
+     */
+    public static boolean isFilesChecked(TracePackageElement tracePackageElement) {
+        for (TracePackageElement element : tracePackageElement.getChildren()) {
+            if (element instanceof TracePackageFilesElement) {
+                return element.isChecked();
+            }
+        }
+
+        return false;
+    }
+
+    /**
+     * Return the matching TmfTraceElement for a given trace element.
+     */
+    private TmfTraceElement getMatchingTraceElement(TracePackageTraceElement tracePackageElement) {
+        IPath tracePath = fTmfTraceFolder.getPath().append(tracePackageElement.getDestinationElementPath());
         List<TmfTraceElement> traces = fTmfTraceFolder.getTraces();
+        for (TmfTraceElement t : traces) {
+            if (t.getPath().equals(tracePath)) {
+                return t;
+            }
+        }
+
+        return null;
+    }
 
+    private IStatus deleteExistingTraces(IProgressMonitor progressMonitor) {
         for (TracePackageElement packageElement : fImportTraceElements) {
             TracePackageTraceElement traceElement = (TracePackageTraceElement) packageElement;
             if (!isFilesChecked(traceElement)) {
                 continue;
             }
 
-            TmfTraceElement existingTrace = null;
-            for (TmfTraceElement t : traces) {
-                if (t.getName().equals(traceElement.getText())) {
-                    existingTrace = t;
-                    break;
-                }
-            }
-
+            TmfTraceElement existingTrace = getMatchingTraceElement(traceElement);
             if (existingTrace != null) {
                 try {
                     existingTrace.delete(new SubProgressMonitor(progressMonitor, 1, SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK));
@@ -298,24 +276,21 @@ public class TracePackageImportOperation extends AbstractTracePackageOperation i
 
                 // Get element
                 IFile bookmarksFile = null;
-                List<TmfTraceElement> traces = fTmfTraceFolder.getTraces();
-                for (TmfTraceElement t : traces) {
-                    if (t.getName().equals(traceRes.getName())) {
-                        try {
-                            bookmarksFile = t.createBookmarksFile();
+                TmfTraceElement tmfTraceElement = getMatchingTraceElement(traceElement);
+                if (tmfTraceElement != null) {
+                    try {
+                        bookmarksFile = tmfTraceElement.createBookmarksFile();
 
-                            // Make sure that if a bookmark is double-clicked first
-                            // before opening the trace, it opens the right editor
+                        // Make sure that if a bookmark is double-clicked first
+                        // before opening the trace, it opens the right editor
 
-                            // Get the editor id from the extension point
-                            String traceEditorId = t.getEditorId();
-                            final String editorId = (traceEditorId != null) ? traceEditorId : TmfEventsEditor.ID;
-                            IDE.setDefaultEditor(bookmarksFile, editorId);
+                        // Get the editor id from the extension point
+                        String traceEditorId = tmfTraceElement.getEditorId();
+                        final String editorId = (traceEditorId != null) ? traceEditorId : TmfEventsEditor.ID;
+                        IDE.setDefaultEditor(bookmarksFile, editorId);
 
-                        } catch (CoreException e) {
-                            Activator.getDefault().logError(MessageFormat.format(Messages.TracePackageImportOperation_ErrorCreatingBookmarkFile, traceRes.getName()), e);
-                        }
-                        break;
+                    } catch (CoreException e) {
+                        Activator.getDefault().logError(MessageFormat.format(Messages.TracePackageImportOperation_ErrorCreatingBookmarkFile, traceRes.getName()), e);
                     }
                 }
 
@@ -354,16 +329,63 @@ public class TracePackageImportOperation extends AbstractTracePackageOperation i
         monitor.worked(1);
     }
 
-    private IStatus importTraceFiles(TracePackageFilesElement traceFilesElement, IProgressMonitor monitor) {
+    private IStatus importTraceFiles(TracePackageFilesElement traceFilesElement, TracePackageTraceElement traceElement, IProgressMonitor monitor) {
         List<Pair<String, String>> fileNameAndLabelPairs = new ArrayList<>();
 
         String sourceName = traceFilesElement.getFileName();
-        String destinationName = traceFilesElement.getParent().getText();
+        String destinationName = traceElement.getImportName();
 
         fileNameAndLabelPairs.add(new Pair<>(sourceName, destinationName));
 
         IPath containerPath = fTmfTraceFolder.getPath();
-        IStatus status = importFiles(getSpecifiedArchiveFile(), fileNameAndLabelPairs, containerPath, monitor);
+        IStatus status = importFiles(getSpecifiedArchiveFile(), fileNameAndLabelPairs, containerPath, Path.EMPTY, monitor);
+        if (getStatus().getSeverity() != IStatus.OK) {
+            return status;
+        }
+
+        // We need to set the trace type before importing the supplementary files so we do it here
+        IResource traceRes = fTmfTraceFolder.getResource().findMember(traceElement.getDestinationElementPath());
+        if (traceRes == null || !traceRes.exists()) {
+            return new Status(IStatus.ERROR, Activator.PLUGIN_ID, MessageFormat.format(Messages.ImportTracePackageWizardPage_ErrorFindingImportedTrace, destinationName));
+        }
+
+        TraceTypeHelper traceType = null;
+        String traceTypeStr = traceElement.getTraceType();
+        if (traceTypeStr != null) {
+            traceType = TmfTraceType.getInstance().getTraceType(traceTypeStr);
+            if (traceType == null) {
+                return new Status(IStatus.ERROR, Activator.PLUGIN_ID, MessageFormat.format(Messages.ImportTracePackageWizardPage_ErrorSettingTraceType, traceElement.getTraceType(), destinationName));
+            }
+        } else {
+            try {
+                monitor.subTask(MessageFormat.format(Messages.TracePackageImportOperation_DetectingTraceType, destinationName));
+                traceType = TmfTraceTypeUIUtils.selectTraceType(traceRes.getLocation().toOSString(), null, null);
+            } catch (TmfTraceImportException e) {
+                // Could not figure out the type
+            }
+        }
+
+        if (traceType != null) {
+            try {
+                TmfTraceTypeUIUtils.setTraceType(traceRes, traceType);
+            } catch (CoreException e) {
+                return new Status(IStatus.ERROR, Activator.PLUGIN_ID, MessageFormat.format(Messages.ImportTracePackageWizardPage_ErrorSettingTraceType, traceElement.getTraceType(), destinationName), e);
+            }
+        }
+
+        importBookmarks(traceRes, traceElement, monitor);
+
+        try {
+            URI uri = new File(getFileName()).toURI();
+            IPath entryPath = new Path(traceFilesElement.getFileName());
+            if (traceRes instanceof IFolder) {
+                entryPath = entryPath.addTrailingSeparator();
+            }
+            String sourceLocation = URIUtil.toUnencodedString(URIUtil.toJarURI(uri, entryPath));
+            traceRes.setPersistentProperty(TmfCommonConstants.SOURCE_LOCATION, sourceLocation);
+        } catch (CoreException e) {
+        }
+
         return status;
     }
 
@@ -375,29 +397,25 @@ public class TracePackageImportOperation extends AbstractTracePackageOperation i
         }
 
         if (!fileNameAndLabelPairs.isEmpty()) {
-            List<TmfTraceElement> traces = fTmfTraceFolder.getTraces();
-            TmfTraceElement tmfTraceElement = null;
-            for (TmfTraceElement t : traces) {
-                if (t.getName().equals(traceElement.getText())) {
-                    tmfTraceElement = t;
-                    break;
-                }
-            }
-
-            if (tmfTraceElement != null) {
+            TmfTraceElement existingTrace = getMatchingTraceElement(traceElement);
+            if (existingTrace != null) {
                 ArchiveFile archiveFile = getSpecifiedArchiveFile();
-                tmfTraceElement.refreshSupplementaryFolder();
-                String traceName = tmfTraceElement.getResource().getName();
-                // Project/.tracing/tracename
-                IPath destinationContainerPath = tmfTraceElement.prepareTraceSupplementaryFolder(traceName, false).getFullPath();
-                return importFiles(archiveFile, fileNameAndLabelPairs, destinationContainerPath, monitor);
+                existingTrace.refreshSupplementaryFolder();
+                // Project/Traces/A/B -> A/B
+                IPath traceFolderRelativePath = fTmfTraceFolder.getPath().makeRelativeTo(fTmfTraceFolder.getProject().getTracesFolder().getPath());
+                // Project/.tracing/A/B/
+                IFolder traceSupplementaryFolder = fTmfTraceFolder.getTraceSupplementaryFolder(traceFolderRelativePath.toString());
+                IPath destinationContainerPath = traceSupplementaryFolder.getFullPath();
+                // Remove the .tracing segment at the beginnin so that a file in folder .tracing/A/B/ imports destinationContainerPath/A/B/
+                Path baseSourcePath = new Path(TmfCommonConstants.TRACE_SUPPLEMENTARY_FOLDER_NAME);
+                return importFiles(archiveFile, fileNameAndLabelPairs, destinationContainerPath, baseSourcePath, monitor);
             }
         }
 
         return Status.OK_STATUS;
     }
 
-    private IStatus importFiles(ArchiveFile archiveFile, List<Pair<String, String>> fileNameAndLabelPairs, IPath destinationContainerPath, IProgressMonitor monitor) {
+    private IStatus importFiles(ArchiveFile archiveFile, List<Pair<String, String>> fileNameAndLabelPairs, IPath destinationContainerPath, IPath baseSourcePath, IProgressMonitor monitor) {
         List<ArchiveProviderElement> objects = new ArrayList<>();
         Enumeration<?> entries = archiveFile.entries();
         while (entries.hasMoreElements()) {
@@ -411,21 +429,17 @@ public class TracePackageImportOperation extends AbstractTracePackageOperation i
 
             for (Pair<String, String> fileNameAndLabel : fileNameAndLabelPairs) {
 
-                // Examples: Traces/kernel/     .tracing/testtexttrace.txt/statistics.ht
+                // Examples: Traces/aaa/kernel/     .tracing/aaa/testtexttrace.txt/statistics.ht
                 IPath searchedArchivePath = new Path(fileNameAndLabel.getFirst());
 
                 // Check if this archive entry matches the searched file name at this archive location
                 boolean fileMatch = entryName.equalsIgnoreCase(searchedArchivePath.toString());
-                // For example Traces/kernel/metadata matches Traces/kernel/
+                // For example Traces/aaa/kernel/metadata matches Traces/aaa/kernel/
                 boolean folderMatch = entryName.startsWith(searchedArchivePath + "/"); //$NON-NLS-1$
 
                 if (fileMatch || folderMatch) {
-                    // Traces/     .tracing/testtexttrace.txt/
-                    IPath searchedArchivePathContainer = searchedArchivePath.removeLastSegments(1);
-
-                    // Traces/kernel/metadata -> kernel/metadata   .tracing/testtexttrace.txt/statistics.ht -> statistics.ht
-                    // Note: The ImportOperation will take care of creating the kernel folder
-                    IPath destinationPath = fullArchivePath.makeRelativeTo(searchedArchivePathContainer);
+                    // .tracing/aaa/testtexttrace.txt/statistics.ht -> aaa/testtexttrace.txt/statistics.ht
+                    IPath destinationPath = fullArchivePath.makeRelativeTo(baseSourcePath);
 
                     // metadata    statistics.ht
                     // We don't use the label when the entry is a folder match because the labels for individual files
This page took 0.034996 seconds and 5 git commands to generate.