-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRenameResourceChange.java
More file actions
142 lines (119 loc) · 4.14 KB
/
RenameResourceChange.java
File metadata and controls
142 lines (119 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*******************************************************************************
* Copyright (c) 2007, 2008 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.ltk.core.refactoring.resource;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.ChangeDescriptor;
import org.eclipse.ltk.internal.core.refactoring.BasicElementLabels;
import org.eclipse.ltk.internal.core.refactoring.Messages;
import org.eclipse.ltk.internal.core.refactoring.RefactoringCoreMessages;
/**
* {@link Change} that renames a resource.
*
* @since 3.4
*/
public class RenameResourceChange extends ResourceChange {
private final String fNewName;
private final IPath fResourcePath;
private final long fStampToRestore;
private ChangeDescriptor fDescriptor;
/**
* Creates the change.
*
* @param resourcePath the path of the resource to rename
* @param newName the new name. Must not be empty.
*/
public RenameResourceChange(IPath resourcePath, String newName) {
this(resourcePath, newName, IResource.NULL_STAMP);
}
/**
* Creates the change with a time stamp to restore.
*
* @param resourcePath the path of the resource to rename
* @param newName the new name. Must not be empty.
* @param stampToRestore the time stamp to restore or
* {@link IResource#NULL_STAMP} to not restore the time
* stamp.
*/
protected RenameResourceChange(IPath resourcePath, String newName, long stampToRestore) {
if (resourcePath == null || newName == null || newName.length() == 0) {
throw new IllegalArgumentException();
}
fResourcePath = resourcePath;
fNewName = newName;
fStampToRestore = stampToRestore;
fDescriptor = null;
setValidationMethod(VALIDATE_NOT_DIRTY);
}
@Override
public ChangeDescriptor getDescriptor() {
return fDescriptor;
}
/**
* Sets the change descriptor to be returned by {@link Change#getDescriptor()}.
*
* @param descriptor the change descriptor
*/
public void setDescriptor(ChangeDescriptor descriptor) {
fDescriptor = descriptor;
}
@Override
protected IResource getModifiedResource() {
return getResource();
}
@Override
public String getName() {
return Messages.format(RefactoringCoreMessages.RenameResourceChange_name, new String[] {
BasicElementLabels.getPathLabel(fResourcePath, false), BasicElementLabels.getResourceName(fNewName) });
}
/**
* Returns the new name.
*
* @return return the new name
*/
public String getNewName() {
return fNewName;
}
public IPath getResourcePath() {
return fResourcePath;
}
public IResource getResource() {
return ResourcesPlugin.getWorkspace().getRoot().findMember(fResourcePath);
}
@Override
public Change perform(IProgressMonitor pm) throws CoreException {
try {
pm.beginTask(RefactoringCoreMessages.RenameResourceChange_progress_description, 1);
IResource resource = getResource();
long currentStamp = resource.getModificationStamp();
IPath newPath = renamedResourcePath(fResourcePath, fNewName);
resource.move(newPath, IResource.SHALLOW, pm);
if (fStampToRestore != IResource.NULL_STAMP) {
IResource newResource = ResourcesPlugin.getWorkspace().getRoot().findMember(newPath);
newResource.revertModificationStamp(fStampToRestore);
}
String oldName = fResourcePath.lastSegment();
return new RenameResourceChange(newPath, oldName, currentStamp);
} finally {
pm.done();
}
}
static IPath renamedResourcePath(IPath path, String newName) {
return path.removeLastSegments(1).append(newName);
}
}