ss: Move plugins to Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / internal / tmf / ui / project / wizards / importtrace / ImportTraceWizardPage.java
index e08932b72f63e7b534e89f92d2d97b5704e7ace7..4abb69d572d26143acfead0c37aea320da4cb80f 100644 (file)
@@ -113,17 +113,17 @@ import org.eclipse.ui.wizards.datatransfer.IImportStructureProvider;
 import org.eclipse.ui.wizards.datatransfer.ImportOperation;
 
 /**
- * A variant of the standard resource import wizard for importing traces
- * to given tracing project. If no project or tracing project was selected
- * the wizard imports it to the default tracing project which is created
- * if necessary.
+ * A variant of the standard resource import wizard for importing traces to
+ * given tracing project. If no project or tracing project was selected the
+ * wizard imports it to the default tracing project which is created if
+ * necessary.
  *
  * In our case traces could be files or a directory structure. This wizard
- * supports both cases. It imports traces for a selected trace type or, if
- * no trace type is selected, it tries to detect the trace type automatically.
- * However, the automatic detection is a best-effort and cannot guarantee
- * that the detection is successful. The reason for this is that there might
- * be multiple trace types that can be assigned to a single trace.
+ * supports both cases. It imports traces for a selected trace type or, if no
+ * trace type is selected, it tries to detect the trace type automatically.
+ * However, the automatic detection is a best-effort and cannot guarantee that
+ * the detection is successful. The reason for this is that there might be
+ * multiple trace types that can be assigned to a single trace.
  *
  *
  * @author Francois Chouinard
@@ -258,8 +258,8 @@ public class ImportTraceWizardPage extends WizardResourceImportPage {
             }
         }
 
-         //  If no tracing project was selected or trace folder doesn't exist use
-         //  default tracing project
+        // If no tracing project was selected or trace folder doesn't exist use
+        // default tracing project
         if (traceFolder == null) {
             IProject project = TmfProjectRegistry.createProject(
                     TmfCommonConstants.DEFAULT_TRACE_PROJECT_NAME, null, new NullProgressMonitor());
@@ -360,7 +360,7 @@ public class ImportTraceWizardPage extends WizardResourceImportPage {
                     if (element.isPopulated()) {
                         return getChildren(element).length > 0;
                     }
-                    //If we have not populated then wait until asked
+                    // If we have not populated then wait until asked
                     return true;
                 }
                 return false;
@@ -805,12 +805,14 @@ public class ImportTraceWizardPage extends WizardResourceImportPage {
             String archivePath = getSourceArchiveFile() != null ? getSourceArchiveFile().getAbsolutePath() : ""; //$NON-NLS-1$
             if (ArchiveFileManipulations.isTarFile(archivePath)) {
                 if (ensureTarSourceIsValid(archivePath)) {
-                    // We close the file when we dispose the import provider, see disposeSelectionGroupRoot
+                    // We close the file when we dispose the import provider,
+                    // see disposeSelectionGroupRoot
                     TarFile tarFile = getSpecifiedTarSourceFile(archivePath);
                     leveledImportStructureProvider = new FileSystemObjectLeveledImportStructureProvider(new TarLeveledStructureProvider(tarFile), archivePath);
                 }
             } else if (ensureZipSourceIsValid(archivePath)) {
-                // We close the file when we dispose the import provider, see disposeSelectionGroupRoot
+                // We close the file when we dispose the import provider, see
+                // disposeSelectionGroupRoot
                 @SuppressWarnings("resource")
                 ZipFile zipFile = getSpecifiedZipSourceFile(archivePath);
                 leveledImportStructureProvider = new FileSystemObjectLeveledImportStructureProvider(new ZipLeveledStructureProvider(zipFile), archivePath);
@@ -837,6 +839,7 @@ public class ImportTraceWizardPage extends WizardResourceImportPage {
 
         private IImportStructureProvider fImportProvider;
         private String fArchivePath;
+
         private FileSystemObjectImportStructureProvider(IImportStructureProvider importStructureProvider, String archivePath) {
             fImportProvider = importStructureProvider;
             fArchivePath = archivePath;
@@ -845,7 +848,7 @@ public class ImportTraceWizardPage extends WizardResourceImportPage {
         @Override
         public List<IFileSystemObject> getChildren(Object element) {
             @SuppressWarnings("rawtypes")
-            List children = fImportProvider.getChildren(((IFileSystemObject)element).getRawFileSystemObject());
+            List children = fImportProvider.getChildren(((IFileSystemObject) element).getRawFileSystemObject());
             List<IFileSystemObject> adapted = new ArrayList<>(children.size());
             for (Object o : children) {
                 adapted.add(getIFileSystemObject(o));
@@ -871,22 +874,22 @@ public class ImportTraceWizardPage extends WizardResourceImportPage {
 
         @Override
         public InputStream getContents(Object element) {
-            return fImportProvider.getContents(((IFileSystemObject)element).getRawFileSystemObject());
+            return fImportProvider.getContents(((IFileSystemObject) element).getRawFileSystemObject());
         }
 
         @Override
         public String getFullPath(Object element) {
-            return fImportProvider.getFullPath(((IFileSystemObject)element).getRawFileSystemObject());
+            return fImportProvider.getFullPath(((IFileSystemObject) element).getRawFileSystemObject());
         }
 
         @Override
         public String getLabel(Object element) {
-            return fImportProvider.getLabel(((IFileSystemObject)element).getRawFileSystemObject());
+            return fImportProvider.getLabel(((IFileSystemObject) element).getRawFileSystemObject());
         }
 
         @Override
         public boolean isFolder(Object element) {
-            return fImportProvider.isFolder(((IFileSystemObject)element).getRawFileSystemObject());
+            return fImportProvider.isFolder(((IFileSystemObject) element).getRawFileSystemObject());
         }
 
         /**
@@ -1012,7 +1015,7 @@ public class ImportTraceWizardPage extends WizardResourceImportPage {
                 elementLabel, dummyParent, isContainer, provider);
         result.setFileSystemObject(element);
 
-        //Get the files for the element so as to build the first level
+        // Get the files for the element so as to build the first level
         result.getFiles();
 
         return dummyParent;
@@ -1297,20 +1300,19 @@ public class ImportTraceWizardPage extends WizardResourceImportPage {
             status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.ImportTraceWizard_ImportProblem, e);
         } catch (InterruptedException e) {
             status = Status.CANCEL_STATUS;
-        } finally {
-            if (!status.isOK()) {
-                if (status.getSeverity() == IStatus.CANCEL) {
-                    setMessage(Messages.ImportTraceWizard_ImportOperationCancelled);
-                    setErrorMessage(null);
-                } else {
-                    if (status.getException() != null) {
-                        displayErrorDialog(status.getMessage() + ": " + status.getException()); //$NON-NLS-1$
-                    }
-                    setMessage(null);
-                    setErrorMessage(Messages.ImportTraceWizard_ImportProblem);
+        }
+        if (!status.isOK()) {
+            if (status.getSeverity() == IStatus.CANCEL) {
+                setMessage(Messages.ImportTraceWizard_ImportOperationCancelled);
+                setErrorMessage(null);
+            } else {
+                if (status.getException() != null) {
+                    displayErrorDialog(status.getMessage() + ": " + status.getException()); //$NON-NLS-1$
                 }
-                return false;
+                setMessage(null);
+                setErrorMessage(Messages.ImportTraceWizard_ImportProblem);
             }
+            return false;
         }
         setErrorMessage(null);
         return true;
@@ -1398,15 +1400,17 @@ public class ImportTraceWizardPage extends WizardResourceImportPage {
                     public void filterElements(Collection elements, IProgressMonitor monitor) {
                         fileSystemElements.addAll(elements);
                     }
+
                     @Override
                     public void filterElements(Object[] elements, IProgressMonitor monitor) {
                         for (int i = 0; i < elements.length; i++) {
-                            fileSystemElements.add((TraceFileSystemElement)elements[i]);
+                            fileSystemElements.add((TraceFileSystemElement) elements[i]);
                         }
                     }
                 };
 
-                // List fileSystemElements will be filled using the passThroughFilter
+                // List fileSystemElements will be filled using the
+                // passThroughFilter
                 SubMonitor subMonitor = SubMonitor.convert(progressMonitor, 1);
                 fSelectionGroup.getAllCheckedListItems(passThroughFilter, subMonitor);
 
@@ -1457,7 +1461,7 @@ public class ImportTraceWizardPage extends WizardResourceImportPage {
                             }
                         }
                     } else {
-                        TraceFileSystemElement parentElement = (TraceFileSystemElement)element.getParent();
+                        TraceFileSystemElement parentElement = (TraceFileSystemElement) element.getParent();
                         String parentPath = parentElement.getFileSystemObject().getAbsolutePath(fBaseSourceContainerPath.toOSString());
                         parentElement.setDestinationContainerPath(computeDestinationContainerPath(new Path(parentPath)));
                         currentPath = parentPath;
@@ -1485,21 +1489,25 @@ public class ImportTraceWizardPage extends WizardResourceImportPage {
                 String errorMessage = Messages.ImportTraceWizard_ImportProblem + ": " + //$NON-NLS-1$
                         (currentPath != null ? currentPath : ""); //$NON-NLS-1$
                 Activator.getDefault().logError(errorMessage, e);
-                setStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID, errorMessage , e));
+                setStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID, errorMessage, e));
             }
         }
 
         private Iterator<TraceFileSystemElement> extractSelectedFiles(Iterator<TraceFileSystemElement> fileSystemElementsIter, IFolder tempFolder, IProgressMonitor progressMonitor) throws InterruptedException,
                 InvocationTargetException {
             List<TraceFileSystemElement> subList = new ArrayList<>();
+            Map<IPath, String> sourceLocationMap = new HashMap<>();
             // Collect all the elements
             while (fileSystemElementsIter.hasNext()) {
                 ModalContext.checkCanceled(progressMonitor);
                 TraceFileSystemElement element = fileSystemElementsIter.next();
+                sourceLocationMap.put(new Path(element.getFileSystemObject().getName()).removeTrailingSeparator(), element.getSourceLocation());
+                TraceFileSystemElement parentElement = (TraceFileSystemElement) element.getParent();
+                sourceLocationMap.put(new Path(parentElement.getFileSystemObject().getName()).removeTrailingSeparator(), parentElement.getSourceLocation());
                 if (element.isDirectory()) {
                     Object[] array = element.getFiles().getChildren();
                     for (int i = 0; i < array.length; i++) {
-                        subList.add((TraceFileSystemElement)array[i]);
+                        subList.add((TraceFileSystemElement) array[i]);
                     }
                 }
                 subList.add(element);
@@ -1531,13 +1539,22 @@ public class ImportTraceWizardPage extends WizardResourceImportPage {
 
             operation.run(new SubProgressMonitor(progressMonitor, subList.size(), SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK));
 
-            // Create the new import provider and root element based on the extracted temp folder
+            // Create the new import provider and root element based on the
+            // extracted temp folder
             FileSystemObjectImportStructureProvider importStructureProvider = new FileSystemObjectImportStructureProvider(FileSystemStructureProvider.INSTANCE, null);
             IFileSystemObject rootElement = importStructureProvider.getIFileSystemObject(new File(tempFolder.getLocation().toOSString()));
             TraceFileSystemElement createRootElement = createRootElement(rootElement, importStructureProvider);
             List<TraceFileSystemElement> list = new ArrayList<>();
             getAllChildren(list, createRootElement);
             Iterator<TraceFileSystemElement> extractedElementsIter = list.iterator();
+            IPath tempPath = new Path(tempFolder.getLocation().toOSString());
+            for (TraceFileSystemElement element : list) {
+                IPath path = new Path(((File) element.getFileSystemObject().getRawFileSystemObject()).getAbsolutePath()).makeRelativeTo(tempPath);
+                element.setSourceLocation(sourceLocationMap.get(path));
+                TraceFileSystemElement parentElement = (TraceFileSystemElement) element.getParent();
+                IPath parentPath = new Path(((File) parentElement.getFileSystemObject().getRawFileSystemObject()).getAbsolutePath()).makeRelativeTo(tempPath);
+                parentElement.setSourceLocation(sourceLocationMap.get(parentPath));
+            }
             return extractedElementsIter;
         }
 
@@ -1557,20 +1574,22 @@ public class ImportTraceWizardPage extends WizardResourceImportPage {
 
             AdaptableList folders = rootElement.getFolders();
             for (Object folder : folders.getChildren()) {
-                getAllChildren(result, (TraceFileSystemElement)folder);
+                getAllChildren(result, (TraceFileSystemElement) folder);
             }
         }
 
         private IPath computeDestinationContainerPath(Path resourcePath) {
             IPath destinationContainerPath = fDestinationContainerPath;
 
-            // We need to figure out the new destination path relative to the selected "base" source directory.
+            // We need to figure out the new destination path relative to the
+            // selected "base" source directory.
             // Here for example, the selected source directory is /home/user
             if ((fImportOptionFlags & OPTION_PRESERVE_FOLDER_STRUCTURE) != 0) {
                 // /home/user/bar/foo/trace -> /home/user/bar/foo
                 IPath sourceContainerPath = resourcePath.removeLastSegments(1);
                 if (fBaseSourceContainerPath.equals(resourcePath)) {
-                    // Use resourcePath directory if fBaseSourceContainerPath points to a directory trace
+                    // Use resourcePath directory if fBaseSourceContainerPath
+                    // points to a directory trace
                     sourceContainerPath = resourcePath;
                 }
                 // /home/user/bar/foo, /home/user -> bar/foo
@@ -1677,7 +1696,7 @@ public class ImportTraceWizardPage extends WizardResourceImportPage {
 
                 Object[] array = fileSystemElement.getFiles().getChildren();
                 for (int i = 0; i < array.length; i++) {
-                    subList.add((TraceFileSystemElement)array[i]);
+                    subList.add((TraceFileSystemElement) array[i]);
                 }
                 parentFolder = fileSystemElement;
 
@@ -1685,7 +1704,6 @@ public class ImportTraceWizardPage extends WizardResourceImportPage {
                 subList.add(fileSystemElement);
             }
 
-
             ImportProvider fileSystemStructureProvider = new ImportProvider();
 
             IOverwriteQuery myQueryImpl = new IOverwriteQuery() {
@@ -1705,7 +1723,7 @@ public class ImportTraceWizardPage extends WizardResourceImportPage {
             operation.setVirtualFolders(false);
 
             operation.run(new SubProgressMonitor(monitor, 1, SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK));
-            String sourceLocation = fileSystemElement.getFileSystemObject().getSourceLocation();
+            String sourceLocation = fileSystemElement.getSourceLocation();
             IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(tracePath);
             if (sourceLocation != null) {
                 resource.setPersistentProperty(TmfCommonConstants.SOURCE_LOCATION, sourceLocation);
@@ -1728,8 +1746,8 @@ public class ImportTraceWizardPage extends WizardResourceImportPage {
             // handle rename
             if (getExistingTrace(tracePath) != null) {
                 if ((fConfirmationMode == ImportConfirmation.RENAME_ALL) ||
-                    (fConfirmationMode == ImportConfirmation.OVERWRITE_ALL) ||
-                    (fConfirmationMode == ImportConfirmation.SKIP_ALL)) {
+                        (fConfirmationMode == ImportConfirmation.OVERWRITE_ALL) ||
+                        (fConfirmationMode == ImportConfirmation.SKIP_ALL)) {
                     return fConfirmationMode;
                 }
 
@@ -1748,12 +1766,12 @@ public class ImportTraceWizardPage extends WizardResourceImportPage {
             final MessageDialog dialog = new MessageDialog(getContainer()
                     .getShell(), null, null, NLS.bind(Messages.ImportTraceWizard_TraceAlreadyExists, tracePath.makeRelativeTo(fTraceFolderElement.getProject().getPath())),
                     MessageDialog.QUESTION, new String[] {
-                        ImportConfirmation.RENAME.getInName(),
-                        ImportConfirmation.RENAME_ALL.getInName(),
-                        ImportConfirmation.OVERWRITE.getInName(),
-                        ImportConfirmation.OVERWRITE_ALL.getInName(),
-                        ImportConfirmation.SKIP.getInName(),
-                        ImportConfirmation.SKIP_ALL.getInName(),
+                            ImportConfirmation.RENAME.getInName(),
+                            ImportConfirmation.RENAME_ALL.getInName(),
+                            ImportConfirmation.OVERWRITE.getInName(),
+                            ImportConfirmation.OVERWRITE_ALL.getInName(),
+                            ImportConfirmation.SKIP.getInName(),
+                            ImportConfirmation.SKIP_ALL.getInName(),
                     }, 4) {
                 @Override
                 protected int getShellStyle() {
@@ -1787,7 +1805,8 @@ public class ImportTraceWizardPage extends WizardResourceImportPage {
                 return;
             }
 
-            // Not using IFolder on purpose to leave the door open to import directly into an IProject
+            // Not using IFolder on purpose to leave the door open to import
+            // directly into an IProject
             IContainer folder = (IContainer) trace.getParent().getResource();
             int i = 2;
             while (true) {
@@ -1836,8 +1855,9 @@ public class ImportTraceWizardPage extends WizardResourceImportPage {
     }
 
     /**
-     * The <code>TraceFileSystemElement</code> is a <code>FileSystemElement</code> that knows
-     * if it has been populated or not.
+     * The <code>TraceFileSystemElement</code> is a
+     * <code>FileSystemElement</code> that knows if it has been populated or
+     * not.
      */
     private static class TraceFileSystemElement extends FileSystemElement {
 
@@ -1845,6 +1865,7 @@ public class ImportTraceWizardPage extends WizardResourceImportPage {
         private String fLabel = null;
         private IPath fDestinationContainerPath;
         private FileSystemObjectImportStructureProvider fProvider;
+        private String fSourceLocation;
 
         public TraceFileSystemElement(String name, FileSystemElement parent, boolean isDirectory, FileSystemObjectImportStructureProvider provider) {
             super(name, parent, isDirectory);
@@ -1865,7 +1886,7 @@ public class ImportTraceWizardPage extends WizardResourceImportPage {
 
         @Override
         public AdaptableList getFiles() {
-            if(!fIsPopulated) {
+            if (!fIsPopulated) {
                 populateElementChildren();
             }
             return super.getFiles();
@@ -1873,7 +1894,7 @@ public class ImportTraceWizardPage extends WizardResourceImportPage {
 
         @Override
         public AdaptableList getFolders() {
-            if(!fIsPopulated) {
+            if (!fIsPopulated) {
                 populateElementChildren();
             }
             return super.getFolders();
@@ -1881,6 +1902,7 @@ public class ImportTraceWizardPage extends WizardResourceImportPage {
 
         /**
          * Sets the label for the trace to be used when importing at trace.
+         *
          * @param name
          *            the label for the trace
          */
@@ -1910,14 +1932,15 @@ public class ImportTraceWizardPage extends WizardResourceImportPage {
         }
 
         /**
-         * Populates the children of the specified parent <code>FileSystemElement</code>
+         * Populates the children of the specified parent
+         * <code>FileSystemElement</code>
          */
         private void populateElementChildren() {
             List<IFileSystemObject> allchildren = fProvider.getChildren(this.getFileSystemObject());
             Object child = null;
             TraceFileSystemElement newelement = null;
             Iterator<IFileSystemObject> iter = allchildren.iterator();
-            while(iter.hasNext()) {
+            while (iter.hasNext()) {
                 child = iter.next();
                 newelement = new TraceFileSystemElement(fProvider.getLabel(child), this, fProvider.isFolder(child), fProvider);
                 newelement.setFileSystemObject(child);
@@ -1934,6 +1957,17 @@ public class ImportTraceWizardPage extends WizardResourceImportPage {
             Object fileSystemObject = super.getFileSystemObject();
             return (IFileSystemObject) fileSystemObject;
         }
+
+        public String getSourceLocation() {
+            if (fSourceLocation == null) {
+                fSourceLocation = getFileSystemObject().getSourceLocation();
+            }
+            return fSourceLocation;
+        }
+
+        public void setSourceLocation(String sourceLocation) {
+            fSourceLocation = sourceLocation;
+        }
     }
 
     /**
@@ -1944,9 +1978,15 @@ public class ImportTraceWizardPage extends WizardResourceImportPage {
      */
     private interface IFileSystemObject {
         String getLabel();
+
+        String getName();
+
         String getAbsolutePath(String parentContainerPath);
+
         String getSourceLocation();
+
         Object getRawFileSystemObject();
+
         boolean exists();
     }
 
@@ -1970,6 +2010,11 @@ public class ImportTraceWizardPage extends WizardResourceImportPage {
             return name;
         }
 
+        @Override
+        public String getName() {
+            return fFileSystemObject.getName();
+        }
+
         @Override
         public String getAbsolutePath(String parentContainerPath) {
             return fFileSystemObject.getAbsolutePath();
@@ -2027,6 +2072,11 @@ public class ImportTraceWizardPage extends WizardResourceImportPage {
             return new Path(fFileSystemObject.getName()).lastSegment();
         }
 
+        @Override
+        public String getName() {
+            return fFileSystemObject.getName();
+        }
+
         @Override
         public String getAbsolutePath(String parentContainerPath) {
             return new Path(parentContainerPath).append(fFileSystemObject.getName()).toOSString();
@@ -2068,6 +2118,11 @@ public class ImportTraceWizardPage extends WizardResourceImportPage {
             return new Path(fFileSystemObject.getName()).lastSegment();
         }
 
+        @Override
+        public String getName() {
+            return fFileSystemObject.getName();
+        }
+
         @Override
         public String getAbsolutePath(String parentContainerPath) {
             return new Path(parentContainerPath).append(fFileSystemObject.getName()).toOSString();
@@ -2098,36 +2153,36 @@ public class ImportTraceWizardPage extends WizardResourceImportPage {
 
         @Override
         public String getLabel(Object element) {
-            TraceFileSystemElement resource = (TraceFileSystemElement)element;
+            TraceFileSystemElement resource = (TraceFileSystemElement) element;
             return resource.getLabel();
         }
 
         @Override
         public List getChildren(Object element) {
-            TraceFileSystemElement resource = (TraceFileSystemElement)element;
+            TraceFileSystemElement resource = (TraceFileSystemElement) element;
             Object[] array = resource.getFiles().getChildren();
-                    List<Object> list = new ArrayList<>();
-                    for (int i = 0; i < array.length; i++) {
-                        list.add(array[i]);
-                    }
+            List<Object> list = new ArrayList<>();
+            for (int i = 0; i < array.length; i++) {
+                list.add(array[i]);
+            }
             return list;
         }
 
         @Override
         public InputStream getContents(Object element) {
-            TraceFileSystemElement resource = (TraceFileSystemElement)element;
+            TraceFileSystemElement resource = (TraceFileSystemElement) element;
             return resource.getProvider().getContents(resource.getFileSystemObject());
         }
 
         @Override
         public String getFullPath(Object element) {
-            TraceFileSystemElement resource = (TraceFileSystemElement)element;
+            TraceFileSystemElement resource = (TraceFileSystemElement) element;
             return resource.getProvider().getFullPath(resource.getFileSystemObject());
         }
 
         @Override
         public boolean isFolder(Object element) {
-            TraceFileSystemElement resource = (TraceFileSystemElement)element;
+            TraceFileSystemElement resource = (TraceFileSystemElement) element;
             return resource.isDirectory();
         }
     }
@@ -2158,7 +2213,9 @@ public class ImportTraceWizardPage extends WizardResourceImportPage {
 
         /**
          * Private constructor
-         * @param name the name of state
+         *
+         * @param name
+         *            the name of state
          */
         private ImportConfirmation(String name) {
             fInName = name;
This page took 0.033574 seconds and 5 git commands to generate.