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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.corext.refactoring.changes;
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;
18 import org.eclipse.core.resources.IResource;
20 import org.eclipse.ltk.core.refactoring.Change;
22 import org.eclipse.jdt.core.ICompilationUnit;
23 import org.eclipse.jdt.core.JavaModelException;
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;
29 import org.eclipse.jdt.internal.ui.viewsupport.BasicElementLabels;
31 public final class RenameCompilationUnitChange extends AbstractJavaElementRenameChange {
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$
38 private RenameCompilationUnitChange(IPath resourcePath, String oldName, String newName, long stampToRestore) {
39 super(resourcePath, oldName, newName, stampToRestore);
41 setValidationMethod(VALIDATE_NOT_READ_ONLY | SAVE_IF_DIRTY);
45 protected IPath createNewPath() {
46 final IPath path= getResourcePath();
47 if (path.getFileExtension() != null)
48 return path.removeFileExtension().removeLastSegments(1).append(getNewName());
50 return path.removeLastSegments(1).append(getNewName());
54 protected Change createUndoChange(long stampToRestore) throws JavaModelException {
55 return new RenameCompilationUnitChange(createNewPath(), getNewName(), getOldName(), stampToRestore);
59 protected void doRename(IProgressMonitor pm) throws CoreException {
60 ICompilationUnit cu= (ICompilationUnit) getModifiedElement();
62 cu.rename(getNewName(), false, pm);
66 public String getName() {
67 String[] keys= new String[] { BasicElementLabels.getJavaElementName(getOldName()), BasicElementLabels.getJavaElementName(getNewName())};
68 return Messages.format(RefactoringCoreMessages.RenameCompilationUnitChange_name, keys);