Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 66d4ef18487c3d548a04e14b853c648981840962 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*******************************************************************************
 * Copyright (c) 2014, 2017 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.resources.ResourcesPlugin;
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 (getExistingResource(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,
                Messages.ImportTraceWizard_MessageTitle, 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(() -> returnValue[0] = dialog.open());
        return returnValue[0];
    }

    private static String rename(IPath tracePath) {
        IResource existingResource = getExistingResource(tracePath);
        if (existingResource == null) {
            return tracePath.lastSegment();
        }

        // Not using IFolder on purpose to leave the door open to import
        // directly into an IProject
        IContainer folder = existingResource.getParent();

        int i = 2;
        while (true) {
            String name = existingResource.getName() + '(' + Integer.toString(i++) + ')';
            IResource resource = folder.findMember(name);
            if (resource == null) {
                return name;
            }
        }
    }

    private void delete(IPath tracePath, IProgressMonitor monitor) throws CoreException {
        IResource existingResource = getExistingResource(tracePath);
        if (existingResource == null) {
            return;
        }
        TmfTraceElement existingTraceElement = getExistingTrace(tracePath);
        if (existingTraceElement != null) {
            // Delete existing TmfTraceElement
            existingTraceElement.delete(monitor, true);
            return;
        }

        // Delete resource existing in workspace
        existingResource.delete(true, monitor);
    }

    private TmfTraceElement getExistingTrace(IPath tracePath) {
        List<TmfTraceElement> traces = fTraceFolderElement.getTraces();
        for (TmfTraceElement t : traces) {
            if (t.getPath().equals(tracePath)) {
                return t;
            }
        }
        return null;
    }

    private static IResource getExistingResource(IPath tracePath) {
        // Look for existing resource
        return ResourcesPlugin.getWorkspace().getRoot().findMember(tracePath);
    }
}

Back to the top