]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2000, 2011 IBM Corporation and others. | |
3 | * All rights reserved. This program and the accompanying materials | |
4 | * are made available under the terms of the Eclipse Public License v1.0 | |
5 | * which accompanies this distribution, and is available at | |
6 | * http://www.eclipse.org/legal/epl-v10.html | |
7 | * | |
8 | * Contributors: | |
9 | * IBM Corporation - initial API and implementation | |
10 | *******************************************************************************/ | |
11 | package org.eclipse.jdt.internal.corext.refactoring.changes; | |
12 | ||
13 | import org.eclipse.core.runtime.Assert; | |
14 | import org.eclipse.core.runtime.CoreException; | |
15 | import org.eclipse.core.runtime.IPath; | |
16 | import org.eclipse.core.runtime.IProgressMonitor; | |
17 | ||
18 | import org.eclipse.core.resources.IResource; | |
19 | ||
20 | import org.eclipse.ltk.core.refactoring.Change; | |
21 | ||
22 | import org.eclipse.jdt.core.ICompilationUnit; | |
23 | import org.eclipse.jdt.core.JavaModelException; | |
24 | ||
25 | import org.eclipse.jdt.internal.corext.refactoring.AbstractJavaElementRenameChange; | |
26 | import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages; | |
27 | import org.eclipse.jdt.internal.corext.util.Messages; | |
28 | ||
29 | import org.eclipse.jdt.internal.ui.viewsupport.BasicElementLabels; | |
30 | ||
31 | public final class RenameCompilationUnitChange extends AbstractJavaElementRenameChange { | |
32 | ||
33 | public RenameCompilationUnitChange(ICompilationUnit unit, String newName) { | |
34 | this(unit.getResource().getFullPath(), unit.getElementName(), newName, IResource.NULL_STAMP); | |
35 | Assert.isTrue(!unit.isReadOnly(), "compilation unit must not be read-only"); //$NON-NLS-1$ | |
36 | } | |
37 | ||
38 | private RenameCompilationUnitChange(IPath resourcePath, String oldName, String newName, long stampToRestore) { | |
39 | super(resourcePath, oldName, newName, stampToRestore); | |
40 | ||
41 | setValidationMethod(VALIDATE_NOT_READ_ONLY | SAVE_IF_DIRTY); | |
42 | } | |
43 | ||
44 | @Override | |
45 | protected IPath createNewPath() { | |
46 | final IPath path= getResourcePath(); | |
47 | if (path.getFileExtension() != null) | |
48 | return path.removeFileExtension().removeLastSegments(1).append(getNewName()); | |
49 | else | |
50 | return path.removeLastSegments(1).append(getNewName()); | |
51 | } | |
52 | ||
53 | @Override | |
54 | protected Change createUndoChange(long stampToRestore) throws JavaModelException { | |
55 | return new RenameCompilationUnitChange(createNewPath(), getNewName(), getOldName(), stampToRestore); | |
56 | } | |
57 | ||
58 | @Override | |
59 | protected void doRename(IProgressMonitor pm) throws CoreException { | |
60 | ICompilationUnit cu= (ICompilationUnit) getModifiedElement(); | |
61 | if (cu != null) | |
62 | cu.rename(getNewName(), false, pm); | |
63 | } | |
64 | ||
65 | @Override | |
66 | public String getName() { | |
67 | String[] keys= new String[] { BasicElementLabels.getJavaElementName(getOldName()), BasicElementLabels.getJavaElementName(getNewName())}; | |
68 | return Messages.format(RefactoringCoreMessages.RenameCompilationUnitChange_name, keys); | |
69 | } | |
70 | } |