]> git.uio.no Git - ifi-stolz-refaktor.git/blame - case-study/jdt-before/core refactoring/org/eclipse/jdt/internal/corext/refactoring/participants/ResourceModifications.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / core refactoring / org / eclipse / jdt / internal / corext / refactoring / participants / ResourceModifications.java
CommitLineData
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 *******************************************************************************/
11package org.eclipse.jdt.internal.corext.refactoring.participants;
12
13import java.util.ArrayList;
14import java.util.Arrays;
15import java.util.Iterator;
16import java.util.List;
17
18import org.eclipse.core.runtime.Assert;
19import org.eclipse.core.runtime.IPath;
20
21import org.eclipse.core.resources.IFile;
22import org.eclipse.core.resources.IResource;
23import org.eclipse.core.resources.ResourcesPlugin;
24import org.eclipse.core.resources.mapping.IResourceChangeDescriptionFactory;
25
26import org.eclipse.ltk.core.refactoring.RefactoringStatus;
27import org.eclipse.ltk.core.refactoring.participants.CopyArguments;
28import org.eclipse.ltk.core.refactoring.participants.CopyParticipant;
29import org.eclipse.ltk.core.refactoring.participants.CreateArguments;
30import org.eclipse.ltk.core.refactoring.participants.CreateParticipant;
31import org.eclipse.ltk.core.refactoring.participants.DeleteArguments;
32import org.eclipse.ltk.core.refactoring.participants.DeleteParticipant;
33import org.eclipse.ltk.core.refactoring.participants.MoveArguments;
34import org.eclipse.ltk.core.refactoring.participants.MoveParticipant;
35import org.eclipse.ltk.core.refactoring.participants.ParticipantManager;
36import org.eclipse.ltk.core.refactoring.participants.RefactoringParticipant;
37import org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor;
38import org.eclipse.ltk.core.refactoring.participants.RenameArguments;
39import org.eclipse.ltk.core.refactoring.participants.RenameParticipant;
40import org.eclipse.ltk.core.refactoring.participants.SharableParticipants;
41
42
43/**
44 * A data structure to collect resource modifications.
45 *
46 * @since 3.0
47 */
48public class ResourceModifications {
49
50 private List<IResource> fCreate;
51 private List<IResource> fDelete;
52
53 private List<IResource> fMove;
54 private List<MoveArguments> fMoveArguments;
55
56 private List<IResource> fRename;
57 private List<RenameArguments> fRenameArguments;
58
59 private List<IResource> fCopy;
60 private List<CopyArguments> fCopyArguments;
61
62 private int fIgnoreCount;
63 private List<DeltaDescription> fDeltaDescriptions;
64
65 public static abstract class DeltaDescription {
66 protected IResource fResource;
67 public DeltaDescription(IResource resource) {
68 fResource= resource;
69 }
70 public abstract void buildDelta(IResourceChangeDescriptionFactory builder);
71 public abstract IPath getDestinationPath();
72
73 }
74 public static class DeleteDescription extends DeltaDescription {
75 public DeleteDescription(IResource resource) {
76 super(resource);
77 }
78 @Override
79 public void buildDelta(IResourceChangeDescriptionFactory builder) {
80 builder.delete(fResource);
81 }
82 @Override
83 public IPath getDestinationPath() {
84 return null;
85 }
86 }
87 public static class ChangedDescription extends DeltaDescription {
88 public ChangedDescription(IFile resource) {
89 super(resource);
90 }
91 @Override
92 public void buildDelta(IResourceChangeDescriptionFactory builder) {
93 builder.change((IFile)fResource);
94 }
95 @Override
96 public IPath getDestinationPath() {
97 return null;
98 }
99 }
100 public static class CreateDescription extends DeltaDescription {
101 public CreateDescription(IResource resource) {
102 super(resource);
103 }
104 @Override
105 public void buildDelta(IResourceChangeDescriptionFactory builder) {
106 builder.create(fResource);
107 }
108 @Override
109 public IPath getDestinationPath() {
110 return fResource.getFullPath();
111 }
112 }
113 public static class MoveDescription extends DeltaDescription {
114 private IPath fDestination;
115 public MoveDescription(IResource resource, IPath destination) {
116 super(resource);
117 fDestination= destination;
118 }
119 @Override
120 public void buildDelta(IResourceChangeDescriptionFactory builder) {
121 IResource existing= ResourcesPlugin.getWorkspace().getRoot().findMember(fDestination);
122 if (existing != null && !existing.equals(fResource)) {
123 builder.delete(existing);
124 }
125 builder.move(fResource, fDestination);
126 }
127 @Override
128 public IPath getDestinationPath() {
129 return fDestination;
130 }
131 }
132 public static class CopyDescription extends DeltaDescription {
133 private IPath fDestination;
134 public CopyDescription(IResource resource, IPath destination) {
135 super(resource);
136 fDestination= destination;
137 }
138 @Override
139 public void buildDelta(IResourceChangeDescriptionFactory builder) {
140 IResource existing= ResourcesPlugin.getWorkspace().getRoot().findMember(fDestination);
141 if (existing != null && !existing.equals(fResource)) {
142 builder.delete(existing);
143 }
144 builder.copy(fResource, fDestination);
145 }
146 @Override
147 public IPath getDestinationPath() {
148 return fDestination;
149 }
150 }
151
152 /**
153 * Adds the given file to the list of changed files.
154 *
155 * @param file the changed file
156 */
157 public void addChanged(IFile file) {
158 if (fIgnoreCount == 0) {
159 internalAdd(new ChangedDescription(file));
160 }
161 }
162
163 /**
164 * Adds the given resource to the list of resources
165 * to be created.
166 *
167 * @param create the resource to be add to the list of
168 * resources to be created
169 */
170 public void addCreate(IResource create) {
171 if (fCreate == null)
172 fCreate= new ArrayList<IResource>(2);
173 fCreate.add(create);
174 if (fIgnoreCount == 0) {
175 internalAdd(new CreateDescription(create));
176 }
177 }
178
179 /**
180 * Adds the given resource to the list of resources
181 * to be deleted.
182 *
183 * @param delete the resource to be deleted
184 */
185 public void addDelete(IResource delete) {
186 if (fDelete == null)
187 fDelete= new ArrayList<IResource>(2);
188 fDelete.add(delete);
189 if (fIgnoreCount == 0) {
190 internalAdd(new DeleteDescription(delete));
191 }
192 }
193
194 /**
195 * Adds the given resource to the list of resources
196 * to be moved.
197 *
198 * @param move the resource to be moved
199 * @param arguments the move arguments
200 */
201 public void addMove(IResource move, MoveArguments arguments) {
202 if (fMove == null) {
203 fMove= new ArrayList<IResource>(2);
204 fMoveArguments= new ArrayList<MoveArguments>(2);
205 }
206 fMove.add(move);
207 fMoveArguments.add(arguments);
208 if (fIgnoreCount == 0) {
209 IPath destination= ((IResource)arguments.getDestination()).getFullPath().append(move.getName());
210 internalAdd(new MoveDescription(move, destination));
211 }
212 }
213
214 /**
215 * Adds the given resource to the list of resources
216 * to be copied.
217 *
218 * @param copy the resource to be copied
219 * @param arguments the copy arguments
220 */
221 public void addCopy(IResource copy, CopyArguments arguments) {
222 if (fCopy == null) {
223 fCopy= new ArrayList<IResource>(2);
224 fCopyArguments= new ArrayList<CopyArguments>(2);
225 }
226 fCopy.add(copy);
227 fCopyArguments.add(arguments);
228 addCopyDelta(copy, arguments);
229 }
230
231 /**
232 * Adds the given resource to the list of renamed
233 * resources.
234 *
235 * @param rename the resource to be renamed
236 * @param arguments the arguments of the rename
237 */
238 public void addRename(IResource rename, RenameArguments arguments) {
239 Assert.isNotNull(rename);
240 Assert.isNotNull(arguments);
241 if (fRename == null) {
242 fRename= new ArrayList<IResource>(2);
243 fRenameArguments= new ArrayList<RenameArguments>(2);
244 }
245 fRename.add(rename);
246 fRenameArguments.add(arguments);
247 if (fIgnoreCount == 0) {
248 IPath newPath= rename.getFullPath().removeLastSegments(1).append(arguments.getNewName());
249 internalAdd(new MoveDescription(rename, newPath));
250 }
251 }
252
253 public RefactoringParticipant[] getParticipants(RefactoringStatus status, RefactoringProcessor processor, String[] natures, SharableParticipants shared) {
254 List<RefactoringParticipant> result= new ArrayList<RefactoringParticipant>(5);
255 if (fDelete != null) {
256 DeleteArguments arguments= new DeleteArguments();
257 for (Iterator<IResource> iter= fDelete.iterator(); iter.hasNext();) {
258 DeleteParticipant[] deletes= ParticipantManager.loadDeleteParticipants(status,
259 processor, iter.next(),
260 arguments, natures, shared);
261 result.addAll(Arrays.asList(deletes));
262 }
263 }
264 if (fCreate != null) {
265 CreateArguments arguments= new CreateArguments();
266 for (Iterator<IResource> iter= fCreate.iterator(); iter.hasNext();) {
267 CreateParticipant[] creates= ParticipantManager.loadCreateParticipants(status,
268 processor, iter.next(),
269 arguments, natures, shared);
270 result.addAll(Arrays.asList(creates));
271 }
272 }
273 if (fMove != null) {
274 for (int i= 0; i < fMove.size(); i++) {
275 Object element= fMove.get(i);
276 MoveArguments arguments= fMoveArguments.get(i);
277 MoveParticipant[] moves= ParticipantManager.loadMoveParticipants(status,
278 processor, element,
279 arguments, natures, shared);
280 result.addAll(Arrays.asList(moves));
281
282 }
283 }
284 if (fCopy != null) {
285 for (int i= 0; i < fCopy.size(); i++) {
286 Object element= fCopy.get(i);
287 CopyArguments arguments= fCopyArguments.get(i);
288 CopyParticipant[] copies= ParticipantManager.loadCopyParticipants(status,
289 processor, element,
290 arguments, natures, shared);
291 result.addAll(Arrays.asList(copies));
292 }
293 }
294 if (fRename != null) {
295 for (int i= 0; i < fRename.size(); i++) {
296 Object resource= fRename.get(i);
297 RenameArguments arguments= fRenameArguments.get(i);
298 RenameParticipant[] renames= ParticipantManager.loadRenameParticipants(status,
299 processor, resource,
300 arguments, natures, shared);
301 result.addAll(Arrays.asList(renames));
302 }
303 }
304 return result.toArray(new RefactoringParticipant[result.size()]);
305 }
306
307 public void ignoreForDelta() {
308 fIgnoreCount++;
309 }
310
311 public void trackForDelta() {
312 fIgnoreCount--;
313 }
314
315 public void addDelta(DeltaDescription description) {
316 if (fIgnoreCount > 0)
317 return;
318 internalAdd(description);
319 }
320
321 public void addCopyDelta(IResource copy, CopyArguments arguments) {
322 if (fIgnoreCount == 0) {
323 IPath destination= ((IResource)arguments.getDestination()).getFullPath().append(copy.getName());
324 internalAdd(new CopyDescription(copy, destination));
325 }
326 }
327
328 /**
329 * Checks if the resource will exist in the future based on
330 * the recorded resource modifications.
331 *
332 * @param resource the resource to check
333 * @return whether the resource will exist or not
334 */
335 public boolean willExist(IResource resource) {
336 if (fDeltaDescriptions == null)
337 return false;
338 IPath fullPath= resource.getFullPath();
339 for (Iterator<DeltaDescription> iter= fDeltaDescriptions.iterator(); iter.hasNext();) {
340 DeltaDescription delta= iter.next();
341 if (fullPath.equals(delta.getDestinationPath()))
342 return true;
343 }
344 return false;
345 }
346
347 public void buildDelta(IResourceChangeDescriptionFactory builder) {
348 if (fDeltaDescriptions == null)
349 return;
350 for (Iterator<DeltaDescription> iter= fDeltaDescriptions.iterator(); iter.hasNext();) {
351 iter.next().buildDelta(builder);
352 }
353 }
354
355 public static void buildMoveDelta(IResourceChangeDescriptionFactory builder, IResource resource, RenameArguments args) {
356 IPath newPath= resource.getFullPath().removeLastSegments(1).append(args.getNewName());
357 new MoveDescription(resource, newPath).buildDelta(builder);
358 }
359
360 public static void buildMoveDelta(IResourceChangeDescriptionFactory builder, IResource resource, MoveArguments args) {
361 IPath destination= ((IResource)args.getDestination()).getFullPath().append(resource.getName());
362 new MoveDescription(resource, destination).buildDelta(builder);
363 }
364
365 public static void buildCopyDelta(IResourceChangeDescriptionFactory builder, IResource resource, CopyArguments args) {
366 IPath destination= ((IResource)args.getDestination()).getFullPath().append(resource.getName());
367 new CopyDescription(resource, destination).buildDelta(builder);
368 }
369
370 private void internalAdd(DeltaDescription description) {
371 if (fDeltaDescriptions == null)
372 fDeltaDescriptions= new ArrayList<DeltaDescription>();
373 fDeltaDescriptions.add(description);
374 }
375}