]>
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 | import org.eclipse.ltk.core.refactoring.RefactoringStatus; | |
22 | ||
23 | import org.eclipse.jdt.core.IPackageFragmentRoot; | |
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.ui.JavaElementLabels; | |
30 | ||
31 | import org.eclipse.jdt.internal.ui.viewsupport.BasicElementLabels; | |
32 | ||
33 | public final class RenameSourceFolderChange extends AbstractJavaElementRenameChange { | |
34 | ||
35 | private static RefactoringStatus checkIfModifiable(IPackageFragmentRoot root) throws CoreException { | |
36 | RefactoringStatus result= new RefactoringStatus(); | |
37 | if (root == null) { | |
38 | result.addFatalError(RefactoringCoreMessages.DynamicValidationStateChange_workspace_changed); | |
39 | return result; | |
40 | } | |
41 | if (!root.exists()) { | |
42 | result.addFatalError(Messages.format(RefactoringCoreMessages.Change_does_not_exist, getRootLabel(root))); | |
43 | return result; | |
44 | } | |
45 | ||
46 | ||
47 | if (result.hasFatalError()) | |
48 | return result; | |
49 | ||
50 | if (root.isArchive()) { | |
51 | result.addFatalError(Messages.format(RefactoringCoreMessages.RenameSourceFolderChange_rename_archive, getRootLabel(root))); | |
52 | return result; | |
53 | } | |
54 | ||
55 | if (root.isExternal()) { | |
56 | result.addFatalError(Messages.format(RefactoringCoreMessages.RenameSourceFolderChange_rename_external, getRootLabel(root))); | |
57 | return result; | |
58 | } | |
59 | ||
60 | IResource correspondingResource= root.getCorrespondingResource(); | |
61 | if (correspondingResource == null || !correspondingResource.exists()) { | |
62 | result.addFatalError(Messages.format(RefactoringCoreMessages.RenameSourceFolderChange_error_underlying_resource_not_existing, getRootLabel(root))); | |
63 | return result; | |
64 | } | |
65 | ||
66 | if (correspondingResource.isLinked()) { | |
67 | result.addFatalError(Messages.format(RefactoringCoreMessages.RenameSourceFolderChange_rename_linked, getRootLabel(root))); | |
68 | return result; | |
69 | } | |
70 | ||
71 | return result; | |
72 | } | |
73 | ||
74 | private static String getRootLabel(IPackageFragmentRoot root) { | |
75 | return JavaElementLabels.getElementLabel(root, JavaElementLabels.ALL_DEFAULT); | |
76 | } | |
77 | ||
78 | public RenameSourceFolderChange(IPackageFragmentRoot sourceFolder, String newName) { | |
79 | this(sourceFolder.getPath(), sourceFolder.getElementName(), newName, IResource.NULL_STAMP); | |
80 | Assert.isTrue(!sourceFolder.isReadOnly(), "should not be read only"); //$NON-NLS-1$ | |
81 | Assert.isTrue(!sourceFolder.isArchive(), "should not be an archive"); //$NON-NLS-1$ | |
82 | Assert.isTrue(!sourceFolder.isExternal(), "should not be an external folder"); //$NON-NLS-1$ | |
83 | setValidationMethod(VALIDATE_NOT_DIRTY); | |
84 | } | |
85 | ||
86 | private RenameSourceFolderChange(IPath resourcePath, String oldName, String newName, long stampToRestore) { | |
87 | super(resourcePath, oldName, newName, stampToRestore); | |
88 | } | |
89 | ||
90 | @Override | |
91 | protected IPath createNewPath() { | |
92 | return getResourcePath().removeLastSegments(1).append(getNewName()); | |
93 | } | |
94 | ||
95 | @Override | |
96 | protected Change createUndoChange(long stampToRestore) { | |
97 | return new RenameSourceFolderChange(createNewPath(), getNewName(), getOldName(), stampToRestore); | |
98 | } | |
99 | ||
100 | @Override | |
101 | protected void doRename(IProgressMonitor pm) throws CoreException { | |
102 | IPackageFragmentRoot sourceFolder= getSourceFolder(); | |
103 | if (sourceFolder != null) | |
104 | sourceFolder.move(getNewPath(), getCoreMoveFlags(), getJavaModelUpdateFlags(), null, pm); | |
105 | } | |
106 | ||
107 | private int getCoreMoveFlags() { | |
108 | if (getResource().isLinked()) | |
109 | return IResource.SHALLOW; | |
110 | else | |
111 | return IResource.NONE; | |
112 | } | |
113 | ||
114 | private int getJavaModelUpdateFlags() { | |
115 | return IPackageFragmentRoot.DESTINATION_PROJECT_CLASSPATH | IPackageFragmentRoot.ORIGINATING_PROJECT_CLASSPATH | IPackageFragmentRoot.OTHER_REFERRING_PROJECTS_CLASSPATH | IPackageFragmentRoot.REPLACE; | |
116 | } | |
117 | ||
118 | @Override | |
119 | public String getName() { | |
120 | String[] keys= { BasicElementLabels.getJavaElementName(getOldName()), BasicElementLabels.getJavaElementName(getNewName())}; | |
121 | return Messages.format(RefactoringCoreMessages.RenameSourceFolderChange_rename, keys); | |
122 | } | |
123 | ||
124 | private IPath getNewPath() { | |
125 | return getResource().getFullPath().removeLastSegments(1).append(getNewName()); | |
126 | } | |
127 | ||
128 | private IPackageFragmentRoot getSourceFolder() { | |
129 | return (IPackageFragmentRoot) getModifiedElement(); | |
130 | } | |
131 | ||
132 | @Override | |
133 | public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException { | |
134 | RefactoringStatus result= super.isValid(pm); | |
135 | if (result.hasFatalError()) | |
136 | return result; | |
137 | ||
138 | IPackageFragmentRoot sourceFolder= getSourceFolder(); | |
139 | result.merge(checkIfModifiable(sourceFolder)); | |
140 | ||
141 | return result; | |
142 | } | |
143 | } |