tmf: add (internal) API for handling name clashes during import
authorBernd Hufmann <Bernd.Hufmann@ericsson.com>
Thu, 6 Nov 2014 15:18:13 +0000 (10:18 -0500)
committerBernd Hufmann <bernd.hufmann@ericsson.com>
Thu, 13 Nov 2014 03:05:12 +0000 (22:05 -0500)
Change-Id: Ice0d488b313f7c3e303e953b80f018f4c906ebf5
Signed-off-by: Bernd Hufmann <Bernd.Hufmann@ericsson.com>
Reviewed-on: https://git.eclipse.org/r/36227
Tested-by: Hudson CI
Reviewed-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Reviewed-by: Bernd Hufmann <bernd.hufmann@ericsson.com>
org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/internal/tmf/ui/project/wizards/importtrace/ImportConfirmation.java [new file with mode: 0644]
org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/internal/tmf/ui/project/wizards/importtrace/ImportConflictHandler.java [new file with mode: 0644]
org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/internal/tmf/ui/project/wizards/importtrace/ImportTraceWizardPage.java

diff --git a/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/internal/tmf/ui/project/wizards/importtrace/ImportConfirmation.java b/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/internal/tmf/ui/project/wizards/importtrace/ImportConfirmation.java
new file mode 100644 (file)
index 0000000..49f4ff8
--- /dev/null
@@ -0,0 +1,69 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Ericsson.
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *   Bernd Hufmann  - Initial API and implementation
+ *******************************************************************************/
+package org.eclipse.tracecompass.internal.tmf.ui.project.wizards.importtrace;
+
+/**
+ * Enumeration for import conflict dialog
+ *
+ * @author Bernd Hufmann
+ */
+public enum ImportConfirmation {
+
+    // ------------------------------------------------------------------------
+    // Enum definition
+    // ------------------------------------------------------------------------
+    /** Single rename */
+    RENAME(Messages.ImportTraceWizard_ImportConfigurationRename),
+    /** Rename all */
+    RENAME_ALL(Messages.ImportTraceWizard_ImportConfigurationRenameAll),
+    /** Single overwrite */
+    OVERWRITE(Messages.ImportTraceWizard_ImportConfigurationOverwrite),
+    /** Overwrite all */
+    OVERWRITE_ALL(Messages.ImportTraceWizard_ImportConfigurationOverwriteAll),
+    /** Single skip */
+    SKIP(Messages.ImportTraceWizard_ImportConfigurationSkip),
+    /** Skip all*/
+    SKIP_ALL(Messages.ImportTraceWizard_ImportConfigurationSkipAll),
+    /** Default value*/
+    CONTINUE("CONTINUE"); //$NON-NLS-1$
+
+    // ------------------------------------------------------------------------
+    // Attributes
+    // ------------------------------------------------------------------------
+    /**
+     * Name of enum
+     */
+    private final String fInName;
+
+    // ------------------------------------------------------------------------
+    // Constuctor
+    // ------------------------------------------------------------------------
+    /**
+     * Private constructor
+     *
+     * @param name
+     *            the name of state
+     */
+    private ImportConfirmation(String name) {
+        fInName = name;
+    }
+
+    // ------------------------------------------------------------------------
+    // Accessors
+    // ------------------------------------------------------------------------
+    /**
+     * @return state name
+     */
+    public String getInName() {
+        return fInName;
+    }
+}
diff --git a/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/internal/tmf/ui/project/wizards/importtrace/ImportConflictHandler.java b/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/internal/tmf/ui/project/wizards/importtrace/ImportConflictHandler.java
new file mode 100644 (file)
index 0000000..1c2f783
--- /dev/null
@@ -0,0 +1,205 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Ericsson.
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *   Bernd Hufmann  - Initial API and implementation
+ *******************************************************************************/
+package org.eclipse.tracecompass.internal.tmf.ui.project.wizards.importtrace;
+
+import java.util.List;
+
+import org.eclipse.core.resources.IContainer;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.osgi.util.NLS;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceElement;
+import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceFolder;
+
+/**
+ * Handler to check for name clashes during import operations. It will allow
+ * users to select renaming, overwriting or skipping of a given trace as well
+ * as upcoming traces by keeping track of the user selection. In case of
+ * overwriting the original trace will be deleted.
+ *
+ * See {@link ImportConfirmation} for users selection choices.
+ *
+ * @author Bernd Hufmann
+ */
+public class ImportConflictHandler {
+
+    // ------------------------------------------------------------------------
+    // Attributes
+    // ------------------------------------------------------------------------
+    private Shell fShell;
+    private TmfTraceFolder fTraceFolderElement;
+    private ImportConfirmation fConfirmationMode;
+
+    // ------------------------------------------------------------------------
+    // Constructor(s)
+    // ------------------------------------------------------------------------
+    /**
+     * @param shell
+     *              shell to display confirmation dialog
+     * @param folder
+     *              Target folder for the traces
+     * @param initialMode
+     *              Initial confirmation mode
+     */
+    public ImportConflictHandler(Shell shell, TmfTraceFolder folder, ImportConfirmation initialMode) {
+        fShell = shell;
+        fTraceFolderElement = folder;
+        fConfirmationMode = initialMode;
+    }
+
+    // ------------------------------------------------------------------------
+    // Operation(s)
+    // ------------------------------------------------------------------------
+    /**
+     * It checks for name clashes. In case of a name clash it will open a
+     * confirmation dialog where the use can rename, overwrite or skip
+     * the trace. The user has also the choice to rename, overwrite or
+     * skip all traces of subsequent calls to this method. This class will
+     * keep track about the {@link ImportConfirmation} mode selected by the
+     * user.
+     *
+     * In case of {@link ImportConfirmation#RENAME} or
+     * {@link ImportConfirmation#RENAME_ALL} a new name will be return by
+     * adding sequence number surrounded by (), e.g. (1) or (2).
+     *
+     * In case of {@link ImportConfirmation#OVERWRITE} or
+     * {@link ImportConfirmation#OVERWRITE_ALL} the original trace will be
+     * deleted and the original name will be returned.
+     *
+     * In case the dialog {@link ImportConfirmation#SKIP} or
+     * {@link ImportConfirmation#SKIP_ALL} it will return null to indicate
+     * the skipping.
+     *
+     * @param tracePath
+     *                The trace to check
+     * @param monitor
+     *                The progress monitor
+     * @return the trace name to use or null
+     * @throws InterruptedException
+     *                If the dialog box was cancelled
+     * @throws CoreException
+     *                If an error during deletion occurred
+     */
+    public String checkAndHandleNameClash(IPath tracePath, IProgressMonitor monitor) throws InterruptedException, CoreException {
+        ImportConfirmation mode = checkForNameClash(tracePath);
+        switch (mode) {
+        case RENAME:
+        case RENAME_ALL:
+            return rename(tracePath);
+        case OVERWRITE:
+        case OVERWRITE_ALL:
+            delete(tracePath, monitor);
+            //$FALL-THROUGH$
+        case CONTINUE:
+            return tracePath.lastSegment();
+        case SKIP:
+        case SKIP_ALL:
+        default:
+            return null;
+        }
+    }
+
+    // ------------------------------------------------------------------------
+    // Helper methods
+    // ------------------------------------------------------------------------
+    private ImportConfirmation checkForNameClash(IPath tracePath) throws InterruptedException {
+        // handle rename
+        if (getExistingTrace(tracePath) != null) {
+            if ((fConfirmationMode == ImportConfirmation.RENAME_ALL) ||
+                    (fConfirmationMode == ImportConfirmation.OVERWRITE_ALL) ||
+                    (fConfirmationMode == ImportConfirmation.SKIP_ALL)) {
+                return fConfirmationMode;
+            }
+
+            int returnCode = promptForOverwrite(tracePath);
+            if (returnCode < 0) {
+                // Cancel
+                throw new InterruptedException();
+            }
+            fConfirmationMode = ImportConfirmation.values()[returnCode];
+            return fConfirmationMode;
+        }
+        return ImportConfirmation.CONTINUE;
+    }
+
+    private int promptForOverwrite(IPath tracePath) {
+        final MessageDialog dialog = new MessageDialog(fShell,
+                 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(),
+                }, 4) {
+            @Override
+            protected int getShellStyle() {
+                return super.getShellStyle() | SWT.SHEET;
+            }
+        };
+
+        final int[] returnValue = new int[1];
+        fShell.getDisplay().syncExec(new Runnable() {
+
+            @Override
+            public void run() {
+                returnValue[0] = dialog.open();
+            }
+        });
+        return returnValue[0];
+    }
+
+    private String rename(IPath tracePath) {
+        TmfTraceElement trace = getExistingTrace(tracePath);
+        if (trace == null) {
+            return tracePath.lastSegment();
+        }
+
+        // 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) {
+            String name = trace.getName() + '(' + Integer.toString(i++) + ')';
+            IResource resource = folder.findMember(name);
+            if (resource == null) {
+                return name;
+            }
+        }
+    }
+
+    private void delete(IPath tracePath, IProgressMonitor monitor) throws CoreException {
+        TmfTraceElement trace = getExistingTrace(tracePath);
+        if (trace == null) {
+            return;
+        }
+
+        trace.delete(monitor);
+    }
+
+    private TmfTraceElement getExistingTrace(IPath tracePath) {
+        List<TmfTraceElement> traces = fTraceFolderElement.getTraces();
+        for (TmfTraceElement t : traces) {
+            if (t.getPath().equals(tracePath)) {
+                return t;
+            }
+        }
+        return null;
+    }
+}
+
index d05b511722815f7a66a016170de43e8ce25e8b39..b09886d71a741f6dc16ca053cfbe0be42fc45d1d 100644 (file)
@@ -51,7 +51,6 @@ import org.eclipse.core.runtime.SubMonitor;
 import org.eclipse.core.runtime.SubProgressMonitor;
 import org.eclipse.core.runtime.URIUtil;
 import org.eclipse.jface.dialogs.IDialogSettings;
-import org.eclipse.jface.dialogs.MessageDialog;
 import org.eclipse.jface.layout.PixelConverter;
 import org.eclipse.jface.operation.IRunnableWithProgress;
 import org.eclipse.jface.operation.ModalContext;
@@ -59,7 +58,6 @@ import org.eclipse.jface.viewers.CheckStateChangedEvent;
 import org.eclipse.jface.viewers.ICheckStateListener;
 import org.eclipse.jface.viewers.IStructuredSelection;
 import org.eclipse.jface.viewers.ITreeContentProvider;
-import org.eclipse.osgi.util.NLS;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.custom.BusyIndicator;
 import org.eclipse.swt.events.FocusAdapter;
@@ -88,7 +86,6 @@ import org.eclipse.tracecompass.tmf.core.project.model.TmfTraceType;
 import org.eclipse.tracecompass.tmf.core.project.model.TraceTypeHelper;
 import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectElement;
 import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectRegistry;
-import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceElement;
 import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceFolder;
 import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceTypeUIUtils;
 import org.eclipse.tracecompass.tmf.ui.project.model.TmfTracesFolder;
@@ -1373,7 +1370,7 @@ public class ImportTraceWizardPage extends WizardResourceImportPage {
         private IPath fBaseSourceContainerPath;
         private boolean fImportFromArchive;
         private int fImportOptionFlags;
-        private ImportConfirmation fConfirmationMode = ImportConfirmation.SKIP;
+        private ImportConflictHandler fConflictHandler;
 
         private TraceValidateAndImportOperation(String traceId, IPath baseSourceContainerPath, IPath destinationContainerPath, boolean importFromArchive, int importOptionFlags) {
             fTraceType = traceId;
@@ -1384,7 +1381,9 @@ public class ImportTraceWizardPage extends WizardResourceImportPage {
 
             boolean overwriteExistingResources = (importOptionFlags & OPTION_OVERWRITE_EXISTING_RESOURCES) != 0;
             if (overwriteExistingResources) {
-                fConfirmationMode = ImportConfirmation.OVERWRITE_ALL;
+                fConflictHandler = new ImportConflictHandler(getContainer().getShell(), fTraceFolderElement, ImportConfirmation.OVERWRITE_ALL);
+            } else {
+                fConflictHandler = new ImportConflictHandler(getContainer().getShell(), fTraceFolderElement, ImportConfirmation.SKIP);
             }
         }
 
@@ -1666,30 +1665,19 @@ public class ImportTraceWizardPage extends WizardResourceImportPage {
         private IResource importResource(TraceFileSystemElement fileSystemElement, IProgressMonitor monitor)
                 throws InvocationTargetException, InterruptedException, CoreException {
 
-            ImportConfirmation mode = checkForNameClashes(fileSystemElement);
-            switch (mode) {
-            case RENAME:
-            case RENAME_ALL:
-                rename(fileSystemElement);
-                break;
-            case OVERWRITE:
-            case OVERWRITE_ALL:
-                delete(fileSystemElement, monitor);
-                break;
-            case CONTINUE:
-                break;
-            case SKIP:
-            case SKIP_ALL:
-            default:
+            IPath tracePath = getInitialDestinationPath(fileSystemElement);
+            String newName = fConflictHandler.checkAndHandleNameClash(tracePath, monitor);
+            if (newName == null) {
                 return null;
             }
+            fileSystemElement.setLabel(newName);
 
             List<TraceFileSystemElement> subList = new ArrayList<>();
 
             FileSystemElement parentFolder = fileSystemElement.getParent();
 
             IPath containerPath = fileSystemElement.getDestinationContainerPath();
-            IPath tracePath = containerPath.addTrailingSeparator().append(fileSystemElement.getLabel());
+            tracePath = containerPath.addTrailingSeparator().append(fileSystemElement.getLabel());
             boolean createLinksInWorkspace = (fImportOptionFlags & OPTION_CREATE_LINKS_IN_WORKSPACE) != 0;
             if (fileSystemElement.isDirectory() && !createLinksInWorkspace) {
                 containerPath = tracePath;
@@ -1740,56 +1728,6 @@ public class ImportTraceWizardPage extends WizardResourceImportPage {
             return false;
         }
 
-        private ImportConfirmation checkForNameClashes(TraceFileSystemElement fileSystemElement) throws InterruptedException {
-            IPath tracePath = getInitialDestinationPath(fileSystemElement);
-
-            // handle rename
-            if (getExistingTrace(tracePath) != null) {
-                if ((fConfirmationMode == ImportConfirmation.RENAME_ALL) ||
-                        (fConfirmationMode == ImportConfirmation.OVERWRITE_ALL) ||
-                        (fConfirmationMode == ImportConfirmation.SKIP_ALL)) {
-                    return fConfirmationMode;
-                }
-
-                int returnCode = promptForOverwrite(tracePath);
-                if (returnCode < 0) {
-                    // Cancel
-                    throw new InterruptedException();
-                }
-                fConfirmationMode = ImportConfirmation.values()[returnCode];
-                return fConfirmationMode;
-            }
-            return ImportConfirmation.CONTINUE;
-        }
-
-        private int promptForOverwrite(IPath tracePath) {
-            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(),
-                    }, 4) {
-                @Override
-                protected int getShellStyle() {
-                    return super.getShellStyle() | SWT.SHEET;
-                }
-            };
-
-            final int[] returnValue = new int[1];
-            getShell().getDisplay().syncExec(new Runnable() {
-
-                @Override
-                public void run() {
-                    returnValue[0] = dialog.open();
-                }
-            });
-            return returnValue[0];
-        }
-
         /**
          * @return the initial destination path, before rename, if any
          */
@@ -1798,47 +1736,6 @@ public class ImportTraceWizardPage extends WizardResourceImportPage {
             return traceFolderPath.append(fileSystemElement.getFileSystemObject().getLabel());
         }
 
-        private void rename(TraceFileSystemElement fileSystemElement) {
-            IPath tracePath = getInitialDestinationPath(fileSystemElement);
-            TmfTraceElement trace = getExistingTrace(tracePath);
-            if (trace == null) {
-                return;
-            }
-
-            // 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) {
-                String name = trace.getName() + '(' + Integer.toString(i++) + ')';
-                IResource resource = folder.findMember(name);
-                if (resource == null) {
-                    fileSystemElement.setLabel(name);
-                    return;
-                }
-            }
-        }
-
-        private void delete(TraceFileSystemElement fileSystemElement, IProgressMonitor monitor) throws CoreException {
-            IPath tracePath = getInitialDestinationPath(fileSystemElement);
-            TmfTraceElement trace = getExistingTrace(tracePath);
-            if (trace == null) {
-                return;
-            }
-
-            trace.delete(monitor);
-        }
-
-        private TmfTraceElement getExistingTrace(IPath tracePath) {
-            List<TmfTraceElement> traces = fTraceFolderElement.getTraces();
-            for (TmfTraceElement t : traces) {
-                if (t.getPath().equals(tracePath)) {
-                    return t;
-                }
-            }
-            return null;
-        }
-
         /**
          * Set the status for this operation
          *
@@ -2186,49 +2083,4 @@ public class ImportTraceWizardPage extends WizardResourceImportPage {
             return resource.isDirectory();
         }
     }
-
-    private enum ImportConfirmation {
-        // ------------------------------------------------------------------------
-        // Enum definition
-        // ------------------------------------------------------------------------
-        RENAME(Messages.ImportTraceWizard_ImportConfigurationRename),
-        RENAME_ALL(Messages.ImportTraceWizard_ImportConfigurationRenameAll),
-        OVERWRITE(Messages.ImportTraceWizard_ImportConfigurationOverwrite),
-        OVERWRITE_ALL(Messages.ImportTraceWizard_ImportConfigurationOverwriteAll),
-        SKIP(Messages.ImportTraceWizard_ImportConfigurationSkip),
-        SKIP_ALL(Messages.ImportTraceWizard_ImportConfigurationSkipAll),
-        CONTINUE("CONTINUE"); //$NON-NLS-1$
-
-        // ------------------------------------------------------------------------
-        // Attributes
-        // ------------------------------------------------------------------------
-        /**
-         * Name of enum
-         */
-        private final String fInName;
-
-        // ------------------------------------------------------------------------
-        // Constuctors
-        // ------------------------------------------------------------------------
-
-        /**
-         * Private constructor
-         *
-         * @param name
-         *            the name of state
-         */
-        private ImportConfirmation(String name) {
-            fInName = name;
-        }
-
-        // ------------------------------------------------------------------------
-        // Accessors
-        // ------------------------------------------------------------------------
-        /**
-         * @return state name
-         */
-        public String getInName() {
-            return fInName;
-        }
-    }
 }
This page took 0.062572 seconds and 5 git commands to generate.