1 /*******************************************************************************
2 * Copyright (c) 2014 Ericsson.
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
10 * Bernd Hufmann - Initial API and implementation
11 *******************************************************************************/
12 package org
.eclipse
.tracecompass
.internal
.tmf
.ui
.project
.wizards
.importtrace
;
14 import java
.util
.List
;
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
;
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.
34 * See {@link ImportConfirmation} for users selection choices.
36 * @author Bernd Hufmann
38 public class ImportConflictHandler
{
40 // ------------------------------------------------------------------------
42 // ------------------------------------------------------------------------
44 private TmfTraceFolder fTraceFolderElement
;
45 private ImportConfirmation fConfirmationMode
;
47 // ------------------------------------------------------------------------
49 // ------------------------------------------------------------------------
52 * shell to display confirmation dialog
54 * Target folder for the traces
56 * Initial confirmation mode
58 public ImportConflictHandler(Shell shell
, TmfTraceFolder folder
, ImportConfirmation initialMode
) {
60 fTraceFolderElement
= folder
;
61 fConfirmationMode
= initialMode
;
64 // ------------------------------------------------------------------------
66 // ------------------------------------------------------------------------
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
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).
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.
83 * In case the dialog {@link ImportConfirmation#SKIP} or
84 * {@link ImportConfirmation#SKIP_ALL} it will return null to indicate
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
97 public String
checkAndHandleNameClash(IPath tracePath
, IProgressMonitor monitor
) throws InterruptedException
, CoreException
{
98 ImportConfirmation mode
= checkForNameClash(tracePath
);
102 return rename(tracePath
);
105 delete(tracePath
, monitor
);
108 return tracePath
.lastSegment();
116 // ------------------------------------------------------------------------
118 // ------------------------------------------------------------------------
119 private ImportConfirmation
checkForNameClash(IPath tracePath
) throws InterruptedException
{
121 if (getExistingTrace(tracePath
) != null) {
122 if ((fConfirmationMode
== ImportConfirmation
.RENAME_ALL
) ||
123 (fConfirmationMode
== ImportConfirmation
.OVERWRITE_ALL
) ||
124 (fConfirmationMode
== ImportConfirmation
.SKIP_ALL
)) {
125 return fConfirmationMode
;
128 int returnCode
= promptForOverwrite(tracePath
);
129 if (returnCode
< 0) {
131 throw new InterruptedException();
133 fConfirmationMode
= ImportConfirmation
.values()[returnCode
];
134 return fConfirmationMode
;
136 return ImportConfirmation
.CONTINUE
;
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(),
151 protected int getShellStyle() {
152 return super.getShellStyle() | SWT
.SHEET
;
156 final int[] returnValue
= new int[1];
157 fShell
.getDisplay().syncExec(new Runnable() {
161 returnValue
[0] = dialog
.open();
164 return returnValue
[0];
167 private String
rename(IPath tracePath
) {
168 TmfTraceElement trace
= getExistingTrace(tracePath
);
170 return tracePath
.lastSegment();
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();
178 String name
= trace
.getName() + '(' + Integer
.toString(i
++) + ')';
179 IResource resource
= folder
.findMember(name
);
180 if (resource
== null) {
186 private void delete(IPath tracePath
, IProgressMonitor monitor
) throws CoreException
{
187 TmfTraceElement trace
= getExistingTrace(tracePath
);
192 trace
.delete(monitor
);
195 private TmfTraceElement
getExistingTrace(IPath tracePath
) {
196 List
<TmfTraceElement
> traces
= fTraceFolderElement
.getTraces();
197 for (TmfTraceElement t
: traces
) {
198 if (t
.getPath().equals(tracePath
)) {