1c2f783ff6f88e1b8646b589b98c62c2f7e40d42
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / internal / tmf / ui / project / wizards / importtrace / ImportConflictHandler.java
1 /*******************************************************************************
2 * Copyright (c) 2014 Ericsson.
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Bernd Hufmann - Initial API and implementation
11 *******************************************************************************/
12 package org.eclipse.tracecompass.internal.tmf.ui.project.wizards.importtrace;
13
14 import java.util.List;
15
16 import org.eclipse.core.resources.IContainer;
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IPath;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.jface.dialogs.MessageDialog;
22 import org.eclipse.osgi.util.NLS;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.widgets.Shell;
25 import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceElement;
26 import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceFolder;
27
28 /**
29 * Handler to check for name clashes during import operations. It will allow
30 * users to select renaming, overwriting or skipping of a given trace as well
31 * as upcoming traces by keeping track of the user selection. In case of
32 * overwriting the original trace will be deleted.
33 *
34 * See {@link ImportConfirmation} for users selection choices.
35 *
36 * @author Bernd Hufmann
37 */
38 public class ImportConflictHandler {
39
40 // ------------------------------------------------------------------------
41 // Attributes
42 // ------------------------------------------------------------------------
43 private Shell fShell;
44 private TmfTraceFolder fTraceFolderElement;
45 private ImportConfirmation fConfirmationMode;
46
47 // ------------------------------------------------------------------------
48 // Constructor(s)
49 // ------------------------------------------------------------------------
50 /**
51 * @param shell
52 * shell to display confirmation dialog
53 * @param folder
54 * Target folder for the traces
55 * @param initialMode
56 * Initial confirmation mode
57 */
58 public ImportConflictHandler(Shell shell, TmfTraceFolder folder, ImportConfirmation initialMode) {
59 fShell = shell;
60 fTraceFolderElement = folder;
61 fConfirmationMode = initialMode;
62 }
63
64 // ------------------------------------------------------------------------
65 // Operation(s)
66 // ------------------------------------------------------------------------
67 /**
68 * It checks for name clashes. In case of a name clash it will open a
69 * confirmation dialog where the use can rename, overwrite or skip
70 * the trace. The user has also the choice to rename, overwrite or
71 * skip all traces of subsequent calls to this method. This class will
72 * keep track about the {@link ImportConfirmation} mode selected by the
73 * user.
74 *
75 * In case of {@link ImportConfirmation#RENAME} or
76 * {@link ImportConfirmation#RENAME_ALL} a new name will be return by
77 * adding sequence number surrounded by (), e.g. (1) or (2).
78 *
79 * In case of {@link ImportConfirmation#OVERWRITE} or
80 * {@link ImportConfirmation#OVERWRITE_ALL} the original trace will be
81 * deleted and the original name will be returned.
82 *
83 * In case the dialog {@link ImportConfirmation#SKIP} or
84 * {@link ImportConfirmation#SKIP_ALL} it will return null to indicate
85 * the skipping.
86 *
87 * @param tracePath
88 * The trace to check
89 * @param monitor
90 * The progress monitor
91 * @return the trace name to use or null
92 * @throws InterruptedException
93 * If the dialog box was cancelled
94 * @throws CoreException
95 * If an error during deletion occurred
96 */
97 public String checkAndHandleNameClash(IPath tracePath, IProgressMonitor monitor) throws InterruptedException, CoreException {
98 ImportConfirmation mode = checkForNameClash(tracePath);
99 switch (mode) {
100 case RENAME:
101 case RENAME_ALL:
102 return rename(tracePath);
103 case OVERWRITE:
104 case OVERWRITE_ALL:
105 delete(tracePath, monitor);
106 //$FALL-THROUGH$
107 case CONTINUE:
108 return tracePath.lastSegment();
109 case SKIP:
110 case SKIP_ALL:
111 default:
112 return null;
113 }
114 }
115
116 // ------------------------------------------------------------------------
117 // Helper methods
118 // ------------------------------------------------------------------------
119 private ImportConfirmation checkForNameClash(IPath tracePath) throws InterruptedException {
120 // handle rename
121 if (getExistingTrace(tracePath) != null) {
122 if ((fConfirmationMode == ImportConfirmation.RENAME_ALL) ||
123 (fConfirmationMode == ImportConfirmation.OVERWRITE_ALL) ||
124 (fConfirmationMode == ImportConfirmation.SKIP_ALL)) {
125 return fConfirmationMode;
126 }
127
128 int returnCode = promptForOverwrite(tracePath);
129 if (returnCode < 0) {
130 // Cancel
131 throw new InterruptedException();
132 }
133 fConfirmationMode = ImportConfirmation.values()[returnCode];
134 return fConfirmationMode;
135 }
136 return ImportConfirmation.CONTINUE;
137 }
138
139 private int promptForOverwrite(IPath tracePath) {
140 final MessageDialog dialog = new MessageDialog(fShell,
141 null, null, NLS.bind(Messages.ImportTraceWizard_TraceAlreadyExists, tracePath.makeRelativeTo(fTraceFolderElement.getProject().getPath())),
142 MessageDialog.QUESTION, new String[] {
143 ImportConfirmation.RENAME.getInName(),
144 ImportConfirmation.RENAME_ALL.getInName(),
145 ImportConfirmation.OVERWRITE.getInName(),
146 ImportConfirmation.OVERWRITE_ALL.getInName(),
147 ImportConfirmation.SKIP.getInName(),
148 ImportConfirmation.SKIP_ALL.getInName(),
149 }, 4) {
150 @Override
151 protected int getShellStyle() {
152 return super.getShellStyle() | SWT.SHEET;
153 }
154 };
155
156 final int[] returnValue = new int[1];
157 fShell.getDisplay().syncExec(new Runnable() {
158
159 @Override
160 public void run() {
161 returnValue[0] = dialog.open();
162 }
163 });
164 return returnValue[0];
165 }
166
167 private String rename(IPath tracePath) {
168 TmfTraceElement trace = getExistingTrace(tracePath);
169 if (trace == null) {
170 return tracePath.lastSegment();
171 }
172
173 // Not using IFolder on purpose to leave the door open to import
174 // directly into an IProject
175 IContainer folder = (IContainer) trace.getParent().getResource();
176 int i = 2;
177 while (true) {
178 String name = trace.getName() + '(' + Integer.toString(i++) + ')';
179 IResource resource = folder.findMember(name);
180 if (resource == null) {
181 return name;
182 }
183 }
184 }
185
186 private void delete(IPath tracePath, IProgressMonitor monitor) throws CoreException {
187 TmfTraceElement trace = getExistingTrace(tracePath);
188 if (trace == null) {
189 return;
190 }
191
192 trace.delete(monitor);
193 }
194
195 private TmfTraceElement getExistingTrace(IPath tracePath) {
196 List<TmfTraceElement> traces = fTraceFolderElement.getTraces();
197 for (TmfTraceElement t : traces) {
198 if (t.getPath().equals(tracePath)) {
199 return t;
200 }
201 }
202 return null;
203 }
204 }
205
This page took 0.034139 seconds and 4 git commands to generate.