diff options
author | Camille Letavernier | 2015-03-25 08:43:08 +0000 |
---|---|---|
committer | Gerrit Code Review @ Eclipse.org | 2015-03-25 08:43:09 +0000 |
commit | 523558e63a3e9c3257c809e7efd6768db96beccc (patch) | |
tree | 1bbba36d8985181eb3de9c22dd63a1468bd96ab3 /plugins | |
parent | f56c059eebc4f1e006b78b7042f9f1c2fef033cf (diff) | |
parent | d0d2cdd5075b70f7cb96ef781c2881bf8480593d (diff) | |
download | org.eclipse.papyrus-523558e63a3e9c3257c809e7efd6768db96beccc.tar.gz org.eclipse.papyrus-523558e63a3e9c3257c809e7efd6768db96beccc.tar.xz org.eclipse.papyrus-523558e63a3e9c3257c809e7efd6768db96beccc.zip |
Merge "Bug 462999: [Copy/Paste] Future compilation error in class copied from Eclipse Platform https://bugs.eclipse.org/bugs/show_bug.cgi?id=462999"
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/infra/org.eclipse.papyrus.infra.onefile/src/org/eclipse/papyrus/infra/onefile/action/CopyFilesAndFoldersOperation.java | 42 |
1 files changed, 24 insertions, 18 deletions
diff --git a/plugins/infra/org.eclipse.papyrus.infra.onefile/src/org/eclipse/papyrus/infra/onefile/action/CopyFilesAndFoldersOperation.java b/plugins/infra/org.eclipse.papyrus.infra.onefile/src/org/eclipse/papyrus/infra/onefile/action/CopyFilesAndFoldersOperation.java index 46d315df90c..1e3e15d4f77 100644 --- a/plugins/infra/org.eclipse.papyrus.infra.onefile/src/org/eclipse/papyrus/infra/onefile/action/CopyFilesAndFoldersOperation.java +++ b/plugins/infra/org.eclipse.papyrus.infra.onefile/src/org/eclipse/papyrus/infra/onefile/action/CopyFilesAndFoldersOperation.java @@ -18,6 +18,8 @@ import java.net.URI; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.eclipse.core.commands.ExecutionException; import org.eclipse.core.filesystem.EFS; @@ -129,33 +131,37 @@ public class CopyFilesAndFoldersOperation { * @return the new full path for the copy */ static IPath getAutoNewNameFor(IPath originalName, IWorkspace workspace) { - int counter = 1; String resourceName = originalName.lastSegment(); IPath leadupSegment = originalName.removeLastSegments(1); + boolean isFile = !originalName.hasTrailingSeparator(); + String newName = computeNewName(resourceName, isFile); while (true) { - String nameSegment; - - if (counter > 1) { - nameSegment = NLS - .bind( - IDEWorkbenchMessages.CopyFilesAndFoldersOperation_copyNameTwoArgs, - new Integer(counter), resourceName); - } else { - nameSegment = NLS - .bind( - IDEWorkbenchMessages.CopyFilesAndFoldersOperation_copyNameOneArg, - resourceName); - } - - IPath pathToTry = leadupSegment.append(nameSegment); - + IPath pathToTry = leadupSegment.append(newName); if (!workspace.getRoot().exists(pathToTry)) { return pathToTry; } + newName = computeNewName(newName, isFile); + } + } - counter++; + private static String computeNewName(String str, boolean isFile) { + int lastIndexOfDot = str.lastIndexOf('.'); + String fileExtension = ""; //$NON-NLS-1$ + String fileNameNoExtension = str; + if (isFile && lastIndexOfDot > 0) { + fileExtension = str.substring(lastIndexOfDot); + fileNameNoExtension = str.substring(0, lastIndexOfDot); + } + Pattern p = Pattern.compile("[0-9]+$"); //$NON-NLS-1$ + Matcher m = p.matcher(fileNameNoExtension); + if (m.find()) { + // String ends with a number: increment it by 1 + int newNumber = Integer.parseInt(m.group()) + 1; + String numberStr = m.replaceFirst(Integer.toString(newNumber)); + return numberStr + fileExtension; } + return fileNameNoExtension + "2" + fileExtension; //$NON-NLS-1$ } /** |