]>
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 java.util.ArrayList; | |
14 | ||
15 | import org.eclipse.core.runtime.CoreException; | |
16 | import org.eclipse.core.runtime.IProgressMonitor; | |
17 | import org.eclipse.core.runtime.OperationCanceledException; | |
18 | ||
19 | import org.eclipse.core.resources.IResource; | |
20 | ||
21 | import org.eclipse.ltk.core.refactoring.Change; | |
22 | ||
23 | import org.eclipse.jdt.core.ICompilationUnit; | |
24 | import org.eclipse.jdt.core.IPackageFragment; | |
25 | import org.eclipse.jdt.core.IPackageFragmentRoot; | |
26 | import org.eclipse.jdt.core.JavaModelException; | |
27 | ||
28 | import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages; | |
29 | import org.eclipse.jdt.internal.corext.util.Messages; | |
30 | ||
31 | import org.eclipse.jdt.internal.ui.viewsupport.BasicElementLabels; | |
32 | ||
33 | public class MoveCompilationUnitChange extends CompilationUnitReorgChange { | |
34 | ||
35 | private boolean fUndoable; | |
36 | private long fStampToRestore; | |
37 | private final IPackageFragment[] fDeletePackages; | |
38 | ||
39 | public MoveCompilationUnitChange(ICompilationUnit cu, IPackageFragment newPackage){ | |
40 | super(cu, newPackage); | |
41 | fStampToRestore= IResource.NULL_STAMP; | |
42 | fDeletePackages= null; | |
43 | ||
44 | setValidationMethod(SAVE_IF_DIRTY | VALIDATE_NOT_READ_ONLY); | |
45 | } | |
46 | ||
47 | private MoveCompilationUnitChange(IPackageFragment oldPackage, String cuName, IPackageFragment newPackage, long stampToRestore, IPackageFragment[] deletePackages) { | |
48 | super(oldPackage.getHandleIdentifier(), newPackage.getHandleIdentifier(), oldPackage.getCompilationUnit(cuName).getHandleIdentifier()); | |
49 | fStampToRestore= stampToRestore; | |
50 | fDeletePackages= deletePackages; | |
51 | ||
52 | setValidationMethod(SAVE_IF_DIRTY | VALIDATE_NOT_READ_ONLY); | |
53 | } | |
54 | ||
55 | @Override | |
56 | public String getName() { | |
57 | return Messages.format(RefactoringCoreMessages.MoveCompilationUnitChange_name, | |
58 | new String[]{BasicElementLabels.getFileName(getCu()), getPackageName(getDestinationPackage())}); | |
59 | } | |
60 | ||
61 | @Override | |
62 | Change doPerformReorg(IProgressMonitor pm) throws CoreException, OperationCanceledException { | |
63 | String name; | |
64 | String newName= getNewName(); | |
65 | if (newName == null) | |
66 | name= getCu().getElementName(); | |
67 | else | |
68 | name= newName; | |
69 | ||
70 | // get current modification stamp | |
71 | long currentStamp= IResource.NULL_STAMP; | |
72 | IResource resource= getCu().getResource(); | |
73 | if (resource != null) { | |
74 | currentStamp= resource.getModificationStamp(); | |
75 | } | |
76 | ||
77 | IPackageFragment destination= getDestinationPackage(); | |
78 | fUndoable= !destination.exists() || !destination.getCompilationUnit(name).exists(); | |
79 | ||
80 | IPackageFragment[] createdPackages= null; | |
81 | if (!destination.exists()) { | |
82 | IPackageFragmentRoot packageFragmentRoot= (IPackageFragmentRoot) destination.getParent(); | |
83 | createdPackages= createDestination(packageFragmentRoot, destination, pm); | |
84 | } | |
85 | ||
86 | // perform the move and restore modification stamp | |
87 | getCu().move(destination, null, newName, true, pm); | |
88 | if (fStampToRestore != IResource.NULL_STAMP) { | |
89 | ICompilationUnit moved= destination.getCompilationUnit(name); | |
90 | IResource movedResource= moved.getResource(); | |
91 | if (movedResource != null) { | |
92 | movedResource.revertModificationStamp(fStampToRestore); | |
93 | } | |
94 | } | |
95 | ||
96 | if (fDeletePackages != null) { | |
97 | for (int i= fDeletePackages.length - 1; i >= 0; i--) { | |
98 | fDeletePackages[i].delete(true, pm); | |
99 | } | |
100 | } | |
101 | ||
102 | if (fUndoable) { | |
103 | return new MoveCompilationUnitChange(destination, getCu().getElementName(), getOldPackage(), currentStamp, createdPackages); | |
104 | } else { | |
105 | return null; | |
106 | } | |
107 | } | |
108 | ||
109 | private IPackageFragment[] createDestination(IPackageFragmentRoot root, IPackageFragment destination, IProgressMonitor pm) throws JavaModelException { | |
110 | String packageName= destination.getElementName(); | |
111 | String[] split= packageName.split("\\."); //$NON-NLS-1$ | |
112 | ||
113 | ArrayList<IPackageFragment> created= new ArrayList<IPackageFragment>(); | |
114 | ||
115 | StringBuffer name= new StringBuffer(); | |
116 | name.append(split[0]); | |
117 | for (int i= 0; i < split.length; i++) { | |
118 | IPackageFragment fragment= root.getPackageFragment(name.toString()); | |
119 | if (!fragment.exists()) { | |
120 | created.add(fragment); | |
121 | } | |
122 | ||
123 | if (fragment.equals(destination)) { | |
124 | root.createPackageFragment(name.toString(), true, pm); | |
125 | return created.toArray(new IPackageFragment[created.size()]); | |
126 | } | |
127 | ||
128 | name.append("."); //$NON-NLS-1$ | |
129 | name.append(split[i + 1]); | |
130 | } | |
131 | ||
132 | return null; | |
133 | } | |
134 | } |