KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > changes > RenameCompilationUnitChange


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
22
23 import org.eclipse.jdt.core.ICompilationUnit;
24 import org.eclipse.jdt.core.JavaModelException;
25
26 import org.eclipse.jdt.internal.corext.refactoring.AbstractJavaElementRenameChange;
27 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
28 import org.eclipse.jdt.internal.corext.util.Messages;
29
30 public final class RenameCompilationUnitChange extends AbstractJavaElementRenameChange {
31
32     public RenameCompilationUnitChange(ICompilationUnit unit, String JavaDoc newName) {
33         this(unit.getResource().getFullPath(), unit.getElementName(), newName, IResource.NULL_STAMP);
34         Assert.isTrue(!unit.isReadOnly(), "compilation unit must not be read-only"); //$NON-NLS-1$
35
}
36
37     private RenameCompilationUnitChange(IPath resourcePath, String JavaDoc oldName, String JavaDoc newName, long stampToRestore) {
38         super(resourcePath, oldName, newName, stampToRestore);
39     }
40
41     protected IPath createNewPath() {
42         final IPath path= getResourcePath();
43         if (path.getFileExtension() != null)
44             return path.removeFileExtension().removeLastSegments(1).append(getNewName());
45         else
46             return path.removeLastSegments(1).append(getNewName());
47     }
48
49     protected Change createUndoChange(long stampToRestore) throws JavaModelException {
50         return new RenameCompilationUnitChange(createNewPath(), getNewName(), getOldName(), stampToRestore);
51     }
52
53     protected void doRename(IProgressMonitor pm) throws CoreException {
54         ICompilationUnit cu= (ICompilationUnit) getModifiedElement();
55         if (cu != null)
56             cu.rename(getNewName(), false, pm);
57     }
58
59     public String JavaDoc getName() {
60         return Messages.format(RefactoringCoreMessages.RenameCompilationUnitChange_name, new String JavaDoc[] { getOldName(), getNewName()});
61     }
62
63     public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException {
64         return super.isValid(pm, READ_ONLY | SAVE_IF_DIRTY);
65     }
66 }
Popular Tags