View | Details | Raw Unified | Return to bug 2855
Collapse All | Expand All

(-)src/org/eclipse/ltk/core/refactoring/resource/RenameProjectReferenceChange.java (+180 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2007, 2008 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.ltk.core.refactoring.resource;
12
13
import java.util.ArrayList;
14
import java.util.List;
15
16
import org.eclipse.core.runtime.CoreException;
17
import org.eclipse.core.runtime.IProgressMonitor;
18
import org.eclipse.core.runtime.SubProgressMonitor;
19
20
import org.eclipse.core.resources.IProject;
21
import org.eclipse.core.resources.IProjectDescription;
22
import org.eclipse.core.resources.IResource;
23
import org.eclipse.core.resources.ResourcesPlugin;
24
25
import org.eclipse.ltk.core.refactoring.Change;
26
import org.eclipse.ltk.core.refactoring.ChangeDescriptor;
27
import org.eclipse.ltk.internal.core.refactoring.Messages;
28
import org.eclipse.ltk.internal.core.refactoring.RefactoringCoreMessages;
29
30
/**
31
 * {@link Change} that updates project references.
32
 * 
33
 * @since 3.6
34
 */
35
public class RenameProjectReferenceChange extends ResourceChange {
36
37
	private final String fProjectName;
38
	private final String fOldReference;
39
	private final String fNewReference;
40
	private final long fStampToRestore;
41
42
	private ChangeDescriptor fDescriptor;
43
44
	/**
45
	 * Creates the change.
46
	 * 
47
	 * @param projectName the name of the project to update
48
	 * @param oldReference the name of the old reference
49
	 * @param newReference the name of the new reference
50
	 */
51
	public RenameProjectReferenceChange(String projectName,
52
			String oldReference, String newReference) {
53
		this(projectName, oldReference, newReference, IResource.NULL_STAMP);
54
	}
55
56
	/**
57
	 * Creates the change with a time stamp to restore.
58
	 * 
59
	 * @param projectName the name of the project to update
60
	 * @param oldReference the name of the old reference
61
	 * @param newReference the name of the new reference
62
	 * @param stampToRestore the time stamp to restore or {@link IResource#NULL_STAMP} to not restore the
63
	 * time stamp.
64
	 */
65
	protected RenameProjectReferenceChange(String projectName,
66
			String oldReference, String newReference, long stampToRestore) {
67
		if (isEmpty(projectName) || isEmpty(oldReference)
68
				|| isEmpty(newReference)) {
69
			throw new IllegalArgumentException();
70
		}
71
72
		fProjectName= projectName;
73
		fOldReference= oldReference;
74
		fNewReference= newReference;
75
		fStampToRestore= stampToRestore;
76
		fDescriptor= null;
77
		setValidationMethod(VALIDATE_NOT_DIRTY);
78
	}
79
80
	/**
81
	 * @param s string to check
82
	 * @return true if the specified string null or empty and false otherwise
83
	 */
84
	private static boolean isEmpty(String s) {
85
		return s == null || s.length() == 0;
86
	}
87
88
	/* (non-Javadoc)
89
	 * @see org.eclipse.ltk.core.refactoring.Change#getDescriptor()
90
	 */
91
	public ChangeDescriptor getDescriptor() {
92
		return fDescriptor;
93
	}
94
95
	/**
96
	 * Sets the change descriptor to be returned by {@link Change#getDescriptor()}.
97
	 *
98
	 * @param descriptor the change descriptor
99
	 */
100
	public void setDescriptor(ChangeDescriptor descriptor) {
101
		fDescriptor= descriptor;
102
	}
103
104
	/* (non-Javadoc)
105
	 * @see org.eclipse.ltk.core.refactoring.resource.ResourceChange#getModifiedResource()
106
	 */
107
	protected IResource getModifiedResource() {
108
		return getProject();
109
	}
110
111
	/* (non-Javadoc)
112
	 * @see org.eclipse.ltk.core.refactoring.Change#getName()
113
	 */
114
	public String getName() {
115
		return Messages.format(RefactoringCoreMessages.RenameProjectReferenceChange_name, fProjectName);
116
	}
117
118
	/**
119
	 * Return the new reference name
120
	 * 
121
	 * @return return the new reference name
122
	 */
123
	public String getNewReference() {
124
		return fNewReference;
125
	}
126
127
	/**
128
	 * Return the old reference name
129
	 * 
130
	 * @return return the old reference name
131
	 */
132
	public String getOldReference() {
133
		return fOldReference;
134
	}
135
136
	/* (non-Javadoc)
137
	 * @see org.eclipse.ltk.core.refactoring.Change#perform(org.eclipse.core.runtime.IProgressMonitor)
138
	 */
139
	public Change perform(IProgressMonitor pm) throws CoreException {
140
		try {
141
			pm.beginTask(Messages.format(RefactoringCoreMessages.RenameProjectReferenceChange_progress_description, fProjectName), 2);
142
143
			IProject project= getProject();
144
			long currentStamp= project.getModificationStamp();
145
			if (fStampToRestore != IResource.NULL_STAMP) {
146
				project.revertModificationStamp(fStampToRestore);
147
			}
148
			IProjectDescription description= project.getDescription();
149
			List newProjects= new ArrayList();
150
			IProject[] oldProjects= description.getReferencedProjects();
151
			for (int i= 0; i < oldProjects.length; i++) {
152
				IProject oldProject= oldProjects[i];
153
				if (oldProject.getName().equals(fOldReference)) {
154
					IProject newProject= ResourcesPlugin.getWorkspace()
155
							.getRoot().getProject(fNewReference);
156
					newProjects.add(newProject);
157
				} else {
158
					newProjects.add(oldProject);
159
				}
160
			}
161
162
			description.setReferencedProjects((IProject[])newProjects
163
					.toArray(new IProject[newProjects.size()]));
164
			pm.worked(1);
165
			project.setDescription(description, IResource.FORCE,
166
					new SubProgressMonitor(pm, 1));
167
168
			return new RenameProjectReferenceChange(fProjectName,
169
					fNewReference, fOldReference, currentStamp);
170
		} finally {
171
			pm.done();
172
		}
173
	}
174
175
	private IProject getProject() {
176
		return ResourcesPlugin.getWorkspace().getRoot()
177
				.getProject(fProjectName);
178
	}
179
180
}
(-)src/org/eclipse/ltk/internal/core/refactoring/RefactoringCoreMessages.java (+6 lines)
Lines 136-141 Link Here
136
136
137
	public static String Refactoring_execute_label;
137
	public static String Refactoring_execute_label;
138
138
139
	public static String RenameProjectReferenceChange_name;
140
141
	public static String RenameProjectReferenceChange_progress_description;
142
139
	public static String RenameResourceChange_name;
143
	public static String RenameResourceChange_name;
140
144
141
	public static String RenameResourceChange_progress_description;
145
	public static String RenameResourceChange_progress_description;
Lines 160-165 Link Here
160
164
161
	public static String RenameResourceProcessor_error_resource_already_exists;
165
	public static String RenameResourceProcessor_error_resource_already_exists;
162
166
167
	public static String RenameResourceProcessor_update_references_change_name;
168
163
	public static String Refactoring_redo_label;
169
	public static String Refactoring_redo_label;
164
170
165
	public static String Refactoring_undo_label;
171
	public static String Refactoring_undo_label;
(-)src/org/eclipse/ltk/internal/core/refactoring/RefactoringCoreMessages.properties (+3 lines)
Lines 67-72 Link Here
67
RefactoringUndoContext_label=Refactoring Undo Context
67
RefactoringUndoContext_label=Refactoring Undo Context
68
68
69
Refactoring_execute_label=Refactoring - Execute
69
Refactoring_execute_label=Refactoring - Execute
70
RenameProjectReferenceChange_name=Update ''{0}'' project references
71
RenameProjectReferenceChange_progress_description=Update ''{0}'' project references
70
RenameResourceChange_name=Rename ''{0}'' to ''{1}''
72
RenameResourceChange_name=Rename ''{0}'' to ''{1}''
71
RenameResourceChange_progress_description=Rename resource
73
RenameResourceChange_progress_description=Rename resource
72
RenameResourceDescriptor_error_name_not_defined=The rename resource refactoring can not be performed as the new name is invalid
74
RenameResourceDescriptor_error_name_not_defined=The rename resource refactoring can not be performed as the new name is invalid
Lines 79-84 Link Here
79
RenameResourceProcessor_error_no_parent=Internal Error
81
RenameResourceProcessor_error_no_parent=Internal Error
80
RenameResourceProcessor_error_resource_already_exists=A file or folder with this name already exists
82
RenameResourceProcessor_error_resource_already_exists=A file or folder with this name already exists
81
RenameResourceProcessor_processor_name=Rename Resource
83
RenameResourceProcessor_processor_name=Rename Resource
84
RenameResourceProcessor_update_references_change_name=Update referencing projects
82
Refactoring_undo_label=Refactoring - Undo
85
Refactoring_undo_label=Refactoring - Undo
83
Refactoring_redo_label=Refactoring - Redo
86
Refactoring_redo_label=Refactoring - Redo
84
87
(-)src/org/eclipse/ltk/internal/core/refactoring/resource/RenameResourceProcessor.java (-1 / +81 lines)
Lines 10-25 Link Here
10
 *******************************************************************************/
10
 *******************************************************************************/
11
package org.eclipse.ltk.internal.core.refactoring.resource;
11
package org.eclipse.ltk.internal.core.refactoring.resource;
12
12
13
import java.util.ArrayList;
14
import java.util.List;
15
13
import org.eclipse.core.runtime.Assert;
16
import org.eclipse.core.runtime.Assert;
14
import org.eclipse.core.runtime.CoreException;
17
import org.eclipse.core.runtime.CoreException;
15
import org.eclipse.core.runtime.IProgressMonitor;
18
import org.eclipse.core.runtime.IProgressMonitor;
16
19
17
import org.eclipse.core.resources.IContainer;
20
import org.eclipse.core.resources.IContainer;
21
import org.eclipse.core.resources.IFile;
18
import org.eclipse.core.resources.IProject;
22
import org.eclipse.core.resources.IProject;
23
import org.eclipse.core.resources.IProjectDescription;
19
import org.eclipse.core.resources.IResource;
24
import org.eclipse.core.resources.IResource;
25
import org.eclipse.core.resources.ResourcesPlugin;
20
import org.eclipse.core.resources.mapping.IResourceChangeDescriptionFactory;
26
import org.eclipse.core.resources.mapping.IResourceChangeDescriptionFactory;
21
27
22
import org.eclipse.ltk.core.refactoring.Change;
28
import org.eclipse.ltk.core.refactoring.Change;
29
import org.eclipse.ltk.core.refactoring.CompositeChange;
23
import org.eclipse.ltk.core.refactoring.RefactoringChangeDescriptor;
30
import org.eclipse.ltk.core.refactoring.RefactoringChangeDescriptor;
24
import org.eclipse.ltk.core.refactoring.RefactoringDescriptor;
31
import org.eclipse.ltk.core.refactoring.RefactoringDescriptor;
25
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
32
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
Lines 30-35 Link Here
30
import org.eclipse.ltk.core.refactoring.participants.RenameProcessor;
37
import org.eclipse.ltk.core.refactoring.participants.RenameProcessor;
31
import org.eclipse.ltk.core.refactoring.participants.ResourceChangeChecker;
38
import org.eclipse.ltk.core.refactoring.participants.ResourceChangeChecker;
32
import org.eclipse.ltk.core.refactoring.participants.SharableParticipants;
39
import org.eclipse.ltk.core.refactoring.participants.SharableParticipants;
40
import org.eclipse.ltk.core.refactoring.resource.RenameProjectReferenceChange;
33
import org.eclipse.ltk.core.refactoring.resource.RenameResourceChange;
41
import org.eclipse.ltk.core.refactoring.resource.RenameResourceChange;
34
import org.eclipse.ltk.core.refactoring.resource.RenameResourceDescriptor;
42
import org.eclipse.ltk.core.refactoring.resource.RenameResourceDescriptor;
35
import org.eclipse.ltk.internal.core.refactoring.BasicElementLabels;
43
import org.eclipse.ltk.internal.core.refactoring.BasicElementLabels;
Lines 123-129 Link Here
123
	 * @see org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor#checkFinalConditions(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext)
131
	 * @see org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor#checkFinalConditions(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext)
124
	 */
132
	 */
125
	public RefactoringStatus checkFinalConditions(IProgressMonitor pm, CheckConditionsContext context) throws CoreException {
133
	public RefactoringStatus checkFinalConditions(IProgressMonitor pm, CheckConditionsContext context) throws CoreException {
126
		pm.beginTask("", 1); //$NON-NLS-1$
134
		pm.beginTask("", 2); //$NON-NLS-1$
127
		try {
135
		try {
128
			fRenameArguments= new RenameArguments(getNewResourceName(), isUpdateReferences());
136
			fRenameArguments= new RenameArguments(getNewResourceName(), isUpdateReferences());
129
137
Lines 132-137 Link Here
132
140
133
			ResourceModifications.buildMoveDelta(deltaFactory, fResource, fRenameArguments);
141
			ResourceModifications.buildMoveDelta(deltaFactory, fResource, fRenameArguments);
134
142
143
			pm.worked(1);
144
145
			IProject[] projects= getReferencingProjects();
146
			if (projects.length > 0) {
147
				for (int i= 0; i < projects.length; i++) {
148
					IFile descriptor = projects[i].getFile(IProjectDescription.DESCRIPTION_FILE_NAME);
149
					deltaFactory.change(descriptor);
150
				}
151
			}
152
			pm.worked(1);
153
135
			return new RefactoringStatus();
154
			return new RefactoringStatus();
136
		} finally {
155
		} finally {
137
			pm.done();
156
			pm.done();
Lines 186-201 Link Here
186
		try {
205
		try {
187
			RenameResourceChange change= new RenameResourceChange(fResource.getFullPath(), getNewResourceName());
206
			RenameResourceChange change= new RenameResourceChange(fResource.getFullPath(), getNewResourceName());
188
			change.setDescriptor(new RefactoringChangeDescriptor(createDescriptor()));
207
			change.setDescriptor(new RefactoringChangeDescriptor(createDescriptor()));
208
209
			if (isUpdateReferences()) {
210
				IProject[] projects= getReferencingProjects();
211
				if (projects.length > 0) {
212
					CompositeChange root= new CompositeChange(change.getName());
213
					root.markAsSynthetic();
214
					root.add(change);
215
					root.add(createUpdateReferencesChange(projects));
216
					return root;
217
				}
218
			}
219
189
			return change;
220
			return change;
190
		} finally {
221
		} finally {
191
			pm.done();
222
			pm.done();
192
		}
223
		}
193
	}
224
	}
194
225
226
	private Change createUpdateReferencesChange(IProject[] projects) {
227
		String oldName= getResource().getName();
228
		String newName= getNewResourceName();
229
230
		CompositeChange updateReferencesChange= new CompositeChange(
231
				RefactoringCoreMessages.RenameResourceProcessor_update_references_change_name);
232
		for (int i= 0; i < projects.length; i++) {
233
			RenameProjectReferenceChange change= new RenameProjectReferenceChange(
234
					projects[i].getName(), oldName, newName);
235
			updateReferencesChange.add(change);
236
		}
237
238
		return updateReferencesChange;
239
	}
240
195
	private String createNewPath(String newName) {
241
	private String createNewPath(String newName) {
196
		return fResource.getFullPath().removeLastSegments(1).append(newName).toString();
242
		return fResource.getFullPath().removeLastSegments(1).append(newName).toString();
197
	}
243
	}
198
244
245
	/**
246
	 * If processed resource is a project this method return projects which refer to this one.
247
	 * Otherwise it return empty list
248
	 * 
249
	 * @return list of the referencing projects
250
	 */
251
	private IProject[] getReferencingProjects() {
252
		if (!(getResource() instanceof IProject)) {
253
			return new IProject[0];
254
		}
255
		String name= getResource().getName();
256
		IProject[] allProjects= ResourcesPlugin.getWorkspace().getRoot()
257
				.getProjects();
258
		List projects= new ArrayList(allProjects.length);
259
		for (int i= 0; i < allProjects.length; i++) {
260
			IProject project= allProjects[i];
261
			if (project.exists() && project.isOpen()) {
262
				try {
263
					IProjectDescription descriptor= project.getDescription();
264
					IProject[] referencedProjects= descriptor
265
							.getReferencedProjects();
266
					for (int j= 0; j < referencedProjects.length; j++) {
267
						if (name.equals(referencedProjects[j].getName())) {
268
							projects.add(project);
269
						}
270
					}
271
				} catch (Exception e) {
272
					// ignore problem projects
273
				}
274
			}
275
		}
276
		return (IProject[])projects.toArray(new IProject[projects.size()]);
277
	}
278
199
	/* (non-Javadoc)
279
	/* (non-Javadoc)
200
	 * @see org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor#getElements()
280
	 * @see org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor#getElements()
201
	 */
281
	 */
(-)src/org/eclipse/ltk/internal/ui/refactoring/RefactoringUIMessages.java (+2 lines)
Lines 332-337 Link Here
332
332
333
	public static String RenameResourceWizard_page_title;
333
	public static String RenameResourceWizard_page_title;
334
334
335
	public static String RenameResourceWizard_update_references_field_label;
336
335
	public static String RenameResourceWizard_window_title;
337
	public static String RenameResourceWizard_window_title;
336
338
337
	public static String SelectRefactoringHistoryControl_deselect_all_label;
339
	public static String SelectRefactoringHistoryControl_deselect_all_label;
(-)src/org/eclipse/ltk/internal/ui/refactoring/RefactoringUIMessages.properties (+1 lines)
Lines 190-195 Link Here
190
RenameResourceHandler_title=Rename Resource
190
RenameResourceHandler_title=Rename Resource
191
RenameResourceWizard_name_field_label=New na&me:
191
RenameResourceWizard_name_field_label=New na&me:
192
RenameResourceWizard_page_title=Rename Resource
192
RenameResourceWizard_page_title=Rename Resource
193
RenameResourceWizard_update_references_field_label=Update &references
193
RenameResourceWizard_window_title=Rename Resource
194
RenameResourceWizard_window_title=Rename Resource
194
195
195
AbstractRefactoringModelMerger_wizard_title=Pending Refactorings
196
AbstractRefactoringModelMerger_wizard_title=Pending Refactorings
(-)src/org/eclipse/ltk/ui/refactoring/resource/RenameResourceWizard.java (-1 / +22 lines)
Lines 15-24 Link Here
15
import org.eclipse.swt.events.ModifyListener;
15
import org.eclipse.swt.events.ModifyListener;
16
import org.eclipse.swt.layout.GridData;
16
import org.eclipse.swt.layout.GridData;
17
import org.eclipse.swt.layout.GridLayout;
17
import org.eclipse.swt.layout.GridLayout;
18
import org.eclipse.swt.widgets.Button;
18
import org.eclipse.swt.widgets.Composite;
19
import org.eclipse.swt.widgets.Composite;
19
import org.eclipse.swt.widgets.Label;
20
import org.eclipse.swt.widgets.Label;
20
import org.eclipse.swt.widgets.Text;
21
import org.eclipse.swt.widgets.Text;
21
22
23
import org.eclipse.core.resources.IProject;
22
import org.eclipse.core.resources.IResource;
24
import org.eclipse.core.resources.IResource;
23
25
24
import org.eclipse.jface.wizard.IWizardPage;
26
import org.eclipse.jface.wizard.IWizardPage;
Lines 61-66 Link Here
61
63
62
		private final RenameResourceProcessor fRefactoringProcessor;
64
		private final RenameResourceProcessor fRefactoringProcessor;
63
		private Text fNameField;
65
		private Text fNameField;
66
		private Button fUpdateReferencesField;
64
67
65
		public RenameResourceRefactoringConfigurationPage(RenameResourceProcessor processor) {
68
		public RenameResourceRefactoringConfigurationPage(RenameResourceProcessor processor) {
66
			super("RenameResourceRefactoringInputPage"); //$NON-NLS-1$
69
			super("RenameResourceRefactoringInputPage"); //$NON-NLS-1$
Lines 89-96 Link Here
89
					validatePage();
92
					validatePage();
90
				}
93
				}
91
			});
94
			});
92
93
			fNameField.selectAll();
95
			fNameField.selectAll();
96
97
			if (isNeedReferenceUpdate()) {
98
				fUpdateReferencesField= new Button(composite, SWT.CHECK);
99
				fUpdateReferencesField.setText(RefactoringUIMessages.RenameResourceWizard_update_references_field_label);
100
				fUpdateReferencesField.setSelection(fRefactoringProcessor
101
						.isUpdateReferences());
102
				fUpdateReferencesField.setLayoutData(new GridData(GridData.FILL,
103
						GridData.BEGINNING, true, false, 2, 1));
104
			}
105
94
			setPageComplete(false);
106
			setPageComplete(false);
95
			setControl(composite);
107
			setControl(composite);
96
		}
108
		}
Lines 108-113 Link Here
108
			setPageComplete(status);
120
			setPageComplete(status);
109
		}
121
		}
110
122
123
		protected boolean isNeedReferenceUpdate() {
124
			//only project have references
125
			return fRefactoringProcessor.getResource() instanceof IProject;
126
		}
127
111
		/* (non-Javadoc)
128
		/* (non-Javadoc)
112
		 * @see org.eclipse.ltk.ui.refactoring.UserInputWizardPage#performFinish()
129
		 * @see org.eclipse.ltk.ui.refactoring.UserInputWizardPage#performFinish()
113
		 */
130
		 */
Lines 131-136 Link Here
131
148
132
		private void initializeRefactoring() {
149
		private void initializeRefactoring() {
133
			fRefactoringProcessor.setNewResourceName(fNameField.getText());
150
			fRefactoringProcessor.setNewResourceName(fNameField.getText());
151
			if (fUpdateReferencesField != null) {
152
				fRefactoringProcessor.setUpdateReferences(fUpdateReferencesField
153
						.getSelection());
154
			}
134
		}
155
		}
135
	}
156
	}
136
}
157
}

Return to bug 2855