Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 151668 | Differences between
and this patch

Collapse All | Expand All

(-)a/org.eclipse.jdt.ui.tests.refactoring/test cases/org/eclipse/jdt/ui/tests/refactoring/ccp/CopyTest.java (+52 lines)
Lines 173-184 public class CopyTest extends RefactoringTest { Link Here
173
		private static final String NEW_FILE_NAME= "UnusedName.gif";
173
		private static final String NEW_FILE_NAME= "UnusedName.gif";
174
		private static final String NEW_FOLDER_NAME= "UnusedName";
174
		private static final String NEW_FOLDER_NAME= "UnusedName";
175
		private static final String NEW_CU_NAME= "UnusedName";
175
		private static final String NEW_CU_NAME= "UnusedName";
176
		private String fCuInitialSuggestedName= "unset";
177
		private String fResourceInitialSuggestedName= "unset";
176
178
177
		public INewNameQuery createNewCompilationUnitNameQuery(ICompilationUnit cu, String s) {
179
		public INewNameQuery createNewCompilationUnitNameQuery(ICompilationUnit cu, String s) {
180
			setCuInitialSuggestedName(s);
178
			return createStaticQuery(NEW_CU_NAME);
181
			return createStaticQuery(NEW_CU_NAME);
179
		}
182
		}
180
183
181
		public INewNameQuery createNewResourceNameQuery(IResource res, String s) {
184
		public INewNameQuery createNewResourceNameQuery(IResource res, String s) {
185
			setResourceInitialSuggestedName(s);
182
			if (res instanceof IFile)
186
			if (res instanceof IFile)
183
				return createStaticQuery(NEW_FILE_NAME);
187
				return createStaticQuery(NEW_FILE_NAME);
184
			else
188
			else
Lines 204-209 public class CopyTest extends RefactoringTest { Link Here
204
		public INewNameQuery createNewPackageFragmentRootNameQuery(IPackageFragmentRoot root, String initialSuggestedName) {
208
		public INewNameQuery createNewPackageFragmentRootNameQuery(IPackageFragmentRoot root, String initialSuggestedName) {
205
			return createStaticQuery(NEW_PACKAGE_FRAGMENT_ROOT_NAME);
209
			return createStaticQuery(NEW_PACKAGE_FRAGMENT_ROOT_NAME);
206
		}
210
		}
211
212
		public String getCuInitialSuggestedName() {
213
			return fCuInitialSuggestedName;
214
		}
215
216
		private void setCuInitialSuggestedName(String cuInitialSuggestedName) {
217
			this.fCuInitialSuggestedName= cuInitialSuggestedName;
218
		}
219
220
		public String getResourceInitialSuggestedName() {
221
			return fResourceInitialSuggestedName;
222
		}
223
224
		public void setResourceInitialSuggestedName(String resourceInitialSuggestedName) {
225
			fResourceInitialSuggestedName= resourceInitialSuggestedName;
226
		}
207
	}
227
	}
208
228
209
	private static class MockCancelNameQueries implements INewNameQueries{
229
	private static class MockCancelNameQueries implements INewNameQueries{
Lines 1481-1486 public class CopyTest extends RefactoringTest { Link Here
1481
		ParticipantTesting.testCopy(handles, new CopyArguments[] {
1501
		ParticipantTesting.testCopy(handles, new CopyArguments[] {
1482
				new CopyArguments(destination, log)
1502
				new CopyArguments(destination, log)
1483
		});
1503
		});
1504
		assertEquals("a2.txt", ((MockNewNameQueries)queries).getResourceInitialSuggestedName());
1484
	}
1505
	}
1485
1506
1486
	public void testCopy_File_to_Same_Folder_Cancel() throws Exception {
1507
	public void testCopy_File_to_Same_Folder_Cancel() throws Exception {
Lines 1541-1546 public class CopyTest extends RefactoringTest { Link Here
1541
		assertTrue("new file does not exist after copying", newFile.exists());
1562
		assertTrue("new file does not exist after copying", newFile.exists());
1542
	}
1563
	}
1543
1564
1565
	public void testCopy_File_to_Itself_Conflict() throws Exception {
1566
		IFolder parentFolder= (IFolder) getPackageP().getResource();
1567
		String fileName= "A.java";
1568
		IFile file= parentFolder.getFile(fileName);
1569
		file.create(getStream("123"), true, null);
1570
		String conflictingFileName= "A2.java";
1571
		IFile conflictingFile= parentFolder.getFile(conflictingFileName);
1572
		conflictingFile.create(getStream("456"), true, null);
1573
1574
		INewNameQueries queries= new MockNewNameQueries();
1575
1576
		IJavaElement[] javaElements= {};
1577
		IResource[] resources= { file };
1578
		JavaCopyProcessor ref= verifyEnabled(resources, javaElements, queries, createReorgQueries());
1579
1580
		Object destination= file;
1581
		verifyValidDestination(ref, destination);
1582
1583
		assertTrue("source file does not exist before copying", file.exists());
1584
1585
		RefactoringStatus status= performRefactoring(ref, false);
1586
		assertEquals(null, status);
1587
1588
		assertTrue("source file does not exist after copying", file.exists());
1589
1590
		IFile newFile= parentFolder.getFile(MockNewNameQueries.NEW_FILE_NAME);
1591
		assertTrue("new file does not exist after copying", newFile.exists());
1592
		assertEquals("A3.java", ((MockNewNameQueries)queries).getResourceInitialSuggestedName());
1593
	}
1594
1544
	public void testCopy_File_to_AnotherFile() throws Exception {
1595
	public void testCopy_File_to_AnotherFile() throws Exception {
1545
		IFolder parentFolder= (IFolder) getPackageP().getResource();
1596
		IFolder parentFolder= (IFolder) getPackageP().getResource();
1546
		String fileName= "a.txt";
1597
		String fileName= "a.txt";
Lines 1919-1924 public class CopyTest extends RefactoringTest { Link Here
1919
1970
1920
		ICompilationUnit newCu= getPackageP().getCompilationUnit(MockNewNameQueries.NEW_CU_NAME + ".java");
1971
		ICompilationUnit newCu= getPackageP().getCompilationUnit(MockNewNameQueries.NEW_CU_NAME + ".java");
1921
		assertTrue("new file does not exist after copying", newCu.exists());
1972
		assertTrue("new file does not exist after copying", newCu.exists());
1973
		assertEquals("A2", ((MockNewNameQueries)queries).getCuInitialSuggestedName());
1922
	}
1974
	}
1923
1975
1924
	public void testCopy_Cu_to_OtherPackage() throws Exception {
1976
	public void testCopy_Cu_to_OtherPackage() throws Exception {
(-)a/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/RefactoringCoreMessages.java (-8 lines)
Lines 276-293 public final class RefactoringCoreMessages extends NLS { Link Here
276
276
277
	public static String CopyPackageFragmentRootChange_copy;
277
	public static String CopyPackageFragmentRootChange_copy;
278
278
279
	public static String CopyRefactoring_cu_copyOf1;
280
281
	public static String CopyRefactoring_cu_copyOfMore;
282
283
	public static String CopyRefactoring_package_copyOf1;
279
	public static String CopyRefactoring_package_copyOf1;
284
280
285
	public static String CopyRefactoring_package_copyOfMore;
281
	public static String CopyRefactoring_package_copyOfMore;
286
282
287
	public static String CopyRefactoring_resource_copyOf1;
288
289
	public static String CopyRefactoring_resource_copyOfMore;
290
291
	public static String CopyRefactoring_update_ref;
283
	public static String CopyRefactoring_update_ref;
292
284
293
	public static String CopyResourceString_copy;
285
	public static String CopyResourceString_copy;
(-)a/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/refactoring.properties (-4 lines)
Lines 579-588 MoveCuUpdateCreator_update_imports=Update imports Link Here
579
MoveCuUpdateCreator_searching=Searching for references to types in ''{0}''
579
MoveCuUpdateCreator_searching=Searching for references to types in ''{0}''
580
MoveCuUpdateCreator_update_references=Update references
580
MoveCuUpdateCreator_update_references=Update references
581
581
582
CopyRefactoring_cu_copyOf1=CopyOf{0}
583
CopyRefactoring_cu_copyOfMore=Copy_{0}_of_{1}
584
CopyRefactoring_resource_copyOf1=Copy of {0}
585
CopyRefactoring_resource_copyOfMore=Copy ({0}) of {1}
586
CopyRefactoring_package_copyOf1={0}.copy
582
CopyRefactoring_package_copyOf1={0}.copy
587
CopyRefactoring_package_copyOfMore={1}.copy{0}
583
CopyRefactoring_package_copyOfMore={1}.copy{0}
588
584
(-)a/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/reorg/ReorgPolicyFactory.java (-15 / +32 lines)
Lines 22-27 import java.util.List; Link Here
22
import java.util.Map;
22
import java.util.Map;
23
import java.util.Set;
23
import java.util.Set;
24
import java.util.StringTokenizer;
24
import java.util.StringTokenizer;
25
import java.util.regex.Matcher;
26
import java.util.regex.Pattern;
25
27
26
import org.eclipse.core.runtime.Assert;
28
import org.eclipse.core.runtime.Assert;
27
import org.eclipse.core.runtime.CoreException;
29
import org.eclipse.core.runtime.CoreException;
Lines 2557-2579 public final class ReorgPolicyFactory { Link Here
2557
2559
2558
		private final Set<String> fAutoGeneratedNewNames= new HashSet<String>(2);
2560
		private final Set<String> fAutoGeneratedNewNames= new HashSet<String>(2);
2559
2561
2562
        private String computeNewName(String str) {
2563
        	final int lastIndexOfDot= str.lastIndexOf('.');
2564
            String fileExtension= ""; //$NON-NLS-1$
2565
            String fileNameNoExtension= str;
2566
            if (lastIndexOfDot != -1) {
2567
            	fileExtension= str.substring(lastIndexOfDot);
2568
            	fileNameNoExtension= str.substring(0, lastIndexOfDot);
2569
            }
2570
            final Pattern p= Pattern.compile("[0-9]+$"); //$NON-NLS-1$
2571
            final Matcher m= p.matcher(fileNameNoExtension);
2572
            if (m.find()) {
2573
                // String ends with a number: increment it by 1
2574
                final Integer newNumber= new Integer(Integer.parseInt(m.group()) + 1);
2575
                final String numberStr= m.replaceFirst(newNumber.toString());
2576
                return numberStr + fileExtension;
2577
            } else {
2578
                return fileNameNoExtension + "2" + fileExtension; //$NON-NLS-1$
2579
            }
2580
        }
2581
2560
		public String createNewName(ICompilationUnit cu, IPackageFragment destination) {
2582
		public String createNewName(ICompilationUnit cu, IPackageFragment destination) {
2561
			if (isNewNameOk(destination, cu.getElementName()))
2583
			if (isNewNameOk(destination, cu.getElementName()))
2562
				return null;
2584
				return null;
2563
			if (!ReorgUtils.isParentInWorkspaceOrOnDisk(cu, destination))
2585
			if (!ReorgUtils.isParentInWorkspaceOrOnDisk(cu, destination))
2564
				return null;
2586
				return null;
2565
			int i= 1;
2587
			
2588
			String newName= computeNewName(cu.getElementName());
2589
			
2566
			while (true) {
2590
			while (true) {
2567
				String newName;
2568
				if (i == 1)
2569
					newName= Messages.format(RefactoringCoreMessages.CopyRefactoring_cu_copyOf1, cu.getElementName()); // Don't use BasicElementLabels! No RTL!
2570
				else
2571
					newName= Messages.format(RefactoringCoreMessages.CopyRefactoring_cu_copyOfMore, new String[] { String.valueOf(i), cu.getElementName()}); // Don't use BasicElementLabels! No RTL!
2572
				if (isNewNameOk(destination, newName) && !fAutoGeneratedNewNames.contains(newName)) {
2591
				if (isNewNameOk(destination, newName) && !fAutoGeneratedNewNames.contains(newName)) {
2573
					fAutoGeneratedNewNames.add(newName);
2592
					fAutoGeneratedNewNames.add(newName);
2574
					return JavaCore.removeJavaLikeExtension(newName);
2593
					return JavaCore.removeJavaLikeExtension(newName);
2594
				} else {
2595
					newName= computeNewName(newName);
2575
				}
2596
				}
2576
				i++;
2577
			}
2597
			}
2578
		}
2598
		}
2579
2599
Lines 2602-2619 public final class ReorgPolicyFactory { Link Here
2602
				return null;
2622
				return null;
2603
			if (!ReorgUtils.isParentInWorkspaceOrOnDisk(res, destination))
2623
			if (!ReorgUtils.isParentInWorkspaceOrOnDisk(res, destination))
2604
				return null;
2624
				return null;
2605
			int i= 1;
2625
			
2626
			String newName= computeNewName(res.getName());
2627
			
2606
			while (true) {
2628
			while (true) {
2607
				String newName;
2608
				if (i == 1)
2609
					newName= Messages.format(RefactoringCoreMessages.CopyRefactoring_resource_copyOf1, res.getName()); // Don't use BasicElementLabels! No RTL!
2610
				else
2611
					newName= Messages.format(RefactoringCoreMessages.CopyRefactoring_resource_copyOfMore, new String[] { String.valueOf(i), res.getName()}); // Don't use BasicElementLabels! No RTL!
2612
				if (isNewNameOk(destination, newName) && !fAutoGeneratedNewNames.contains(newName)) {
2629
				if (isNewNameOk(destination, newName) && !fAutoGeneratedNewNames.contains(newName)) {
2613
					fAutoGeneratedNewNames.add(newName);
2630
					fAutoGeneratedNewNames.add(newName);
2614
					return newName;
2631
					return newName;
2632
				} else {
2633
					newName= computeNewName(newName);
2615
				}
2634
				}
2616
				i++;
2617
			}
2635
			}
2618
		}
2636
		}
2619
	}
2637
	}
2620
- 

Return to bug 151668