KickJava   Java API By Example, From Geeks To Geeks.

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


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.CoreException;
14 import org.eclipse.core.runtime.IProgressMonitor;
15 import org.eclipse.core.runtime.OperationCanceledException;
16
17 import org.eclipse.core.resources.IResource;
18
19 import org.eclipse.ltk.core.refactoring.Change;
20 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
21
22 import org.eclipse.jdt.core.ICompilationUnit;
23 import org.eclipse.jdt.core.IPackageFragment;
24
25 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
26 import org.eclipse.jdt.internal.corext.util.Messages;
27
28 public class MoveCompilationUnitChange extends CompilationUnitReorgChange {
29
30     private boolean fUndoable;
31     private long fStampToRestore;
32     
33     public MoveCompilationUnitChange(ICompilationUnit cu, IPackageFragment newPackage){
34         super(cu, newPackage);
35         fStampToRestore= IResource.NULL_STAMP;
36     }
37     
38     private MoveCompilationUnitChange(IPackageFragment oldPackage, String JavaDoc cuName, IPackageFragment newPackage, long stampToRestore) {
39         super(oldPackage.getHandleIdentifier(), newPackage.getHandleIdentifier(), oldPackage.getCompilationUnit(cuName).getHandleIdentifier());
40         fStampToRestore= stampToRestore;
41     }
42     
43     public String JavaDoc getName() {
44         return Messages.format(RefactoringCoreMessages.MoveCompilationUnitChange_name,
45         new String JavaDoc[]{getCu().getElementName(), getPackageName(getDestinationPackage())});
46     }
47
48     public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException {
49         return super.isValid(pm, READ_ONLY | SAVE_IF_DIRTY);
50     }
51     
52     Change doPerformReorg(IProgressMonitor pm) throws CoreException, OperationCanceledException {
53         String JavaDoc name;
54         String JavaDoc newName= getNewName();
55         if (newName == null)
56             name= getCu().getElementName();
57         else
58             name= newName;
59         
60         // get current modification stamp
61
long currentStamp= IResource.NULL_STAMP;
62         IResource resource= getCu().getResource();
63         if (resource != null) {
64             currentStamp= resource.getModificationStamp();
65         }
66         
67         fUndoable= ! getDestinationPackage().getCompilationUnit(name).exists();
68         
69         // perform the move and restore modification stamp
70
getCu().move(getDestinationPackage(), null, newName, true, pm);
71         if (fStampToRestore != IResource.NULL_STAMP) {
72             ICompilationUnit moved= getDestinationPackage().getCompilationUnit(name);
73             IResource movedResource= moved.getResource();
74             if (movedResource != null) {
75                 movedResource.revertModificationStamp(fStampToRestore);
76             }
77         }
78         
79         if (fUndoable) {
80             return new MoveCompilationUnitChange(getDestinationPackage(), getCu().getElementName(), getOldPackage(), currentStamp);
81         } else {
82             return null;
83         }
84     }
85 }
86
Popular Tags