KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > participants > ResourceModifications


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.participants;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.runtime.Assert;
19 import org.eclipse.core.runtime.IPath;
20
21 import org.eclipse.core.resources.IFile;
22 import org.eclipse.core.resources.IResource;
23 import org.eclipse.core.resources.mapping.IResourceChangeDescriptionFactory;
24
25 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
26 import org.eclipse.ltk.core.refactoring.participants.CopyArguments;
27 import org.eclipse.ltk.core.refactoring.participants.CopyParticipant;
28 import org.eclipse.ltk.core.refactoring.participants.CreateArguments;
29 import org.eclipse.ltk.core.refactoring.participants.CreateParticipant;
30 import org.eclipse.ltk.core.refactoring.participants.DeleteArguments;
31 import org.eclipse.ltk.core.refactoring.participants.DeleteParticipant;
32 import org.eclipse.ltk.core.refactoring.participants.MoveArguments;
33 import org.eclipse.ltk.core.refactoring.participants.MoveParticipant;
34 import org.eclipse.ltk.core.refactoring.participants.ParticipantManager;
35 import org.eclipse.ltk.core.refactoring.participants.RefactoringParticipant;
36 import org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor;
37 import org.eclipse.ltk.core.refactoring.participants.RenameArguments;
38 import org.eclipse.ltk.core.refactoring.participants.RenameParticipant;
39 import org.eclipse.ltk.core.refactoring.participants.SharableParticipants;
40
41
42 /**
43  * A data structure to collect resource modifications.
44  *
45  * @since 3.0
46  */

47 public class ResourceModifications {
48     
49     private List JavaDoc/*<IResource>*/ fCreate;
50     private List JavaDoc/*<IResource>*/ fDelete;
51     
52     private List JavaDoc/*<IResource>*/ fMove;
53     private List JavaDoc/*<MoveArguments>*/ fMoveArguments;
54     
55     private List JavaDoc/*<IResource>*/ fRename;
56     private List JavaDoc/*<RenameArguments>*/ fRenameArguments;
57     
58     private List JavaDoc/*<IResource>*/ fCopy;
59     private List JavaDoc/*<CopyArguments>*/ fCopyArguments;
60     
61     private int fIgnoreCount;
62     private List JavaDoc/*<DeltaDescription>*/ fDeltaDescriptions;
63     
64     public static abstract class DeltaDescription {
65         protected IResource fResource;
66         public DeltaDescription(IResource resource) {
67             fResource= resource;
68         }
69         public abstract void buildDelta(IResourceChangeDescriptionFactory builder);
70         public abstract IPath getDestinationPath();
71         
72     }
73     public static class DeleteDescription extends DeltaDescription {
74         public DeleteDescription(IResource resource) {
75             super(resource);
76         }
77         public void buildDelta(IResourceChangeDescriptionFactory builder) {
78             builder.delete(fResource);
79         }
80         public IPath getDestinationPath() {
81             return null;
82         }
83     }
84     public static class ChangedDescription extends DeltaDescription {
85         public ChangedDescription(IFile resource) {
86             super(resource);
87         }
88         public void buildDelta(IResourceChangeDescriptionFactory builder) {
89             builder.change((IFile)fResource);
90         }
91         public IPath getDestinationPath() {
92             return null;
93         }
94     }
95     public static class CreateDescription extends DeltaDescription {
96         public CreateDescription(IResource resource) {
97             super(resource);
98         }
99         public void buildDelta(IResourceChangeDescriptionFactory builder) {
100             builder.create(fResource);
101         }
102         public IPath getDestinationPath() {
103             return fResource.getFullPath();
104         }
105     }
106     public static class MoveDescription extends DeltaDescription {
107         private IPath fDestination;
108         public MoveDescription(IResource resource, IPath destination) {
109             super(resource);
110             fDestination= destination;
111         }
112         public void buildDelta(IResourceChangeDescriptionFactory builder) {
113             builder.move(fResource, fDestination);
114         }
115         public IPath getDestinationPath() {
116             return fDestination;
117         }
118     }
119     public static class CopyDescription extends DeltaDescription {
120         private IPath fDestination;
121         public CopyDescription(IResource resource, IPath destination) {
122             super(resource);
123             fDestination= destination;
124         }
125         public void buildDelta(IResourceChangeDescriptionFactory builder) {
126             builder.copy(fResource, fDestination);
127         }
128         public IPath getDestinationPath() {
129             return fDestination;
130         }
131     }
132     
133     /**
134      * Adds the given file to the list of changed files.
135      *
136      * @param file the changed file
137      */

138     public void addChanged(IFile file) {
139         if (fIgnoreCount == 0) {
140             internalAdd(new ChangedDescription(file));
141         }
142     }
143     
144     /**
145      * Adds the given resource to the list of resources
146      * to be created.
147      *
148      * @param create the resource to be add to the list of
149      * resources to be created
150      */

151     public void addCreate(IResource create) {
152         if (fCreate == null)
153             fCreate= new ArrayList JavaDoc(2);
154         fCreate.add(create);
155         if (fIgnoreCount == 0) {
156             internalAdd(new CreateDescription(create));
157         }
158     }
159     
160     /**
161      * Adds the given resource to the list of resources
162      * to be deleted.
163      *
164      * @param delete the resource to be deleted
165      */

166     public void addDelete(IResource delete) {
167         if (fDelete == null)
168             fDelete= new ArrayList JavaDoc(2);
169         fDelete.add(delete);
170         if (fIgnoreCount == 0) {
171             internalAdd(new DeleteDescription(delete));
172         }
173     }
174     
175     /**
176      * Adds the given resource to the list of resources
177      * to be moved.
178      *
179      * @param move the resource to be moved
180      */

181     public void addMove(IResource move, MoveArguments arguments) {
182         if (fMove == null) {
183             fMove= new ArrayList JavaDoc(2);
184             fMoveArguments= new ArrayList JavaDoc(2);
185         }
186         fMove.add(move);
187         fMoveArguments.add(arguments);
188         if (fIgnoreCount == 0) {
189             IPath destination= ((IResource)arguments.getDestination()).getFullPath().append(move.getName());
190             internalAdd(new MoveDescription(move, destination));
191         }
192     }
193     
194     /**
195      * Adds the given resource to the list of resources
196      * to be copied.
197      *
198      * @param copy the resource to be copied
199      */

200     public void addCopy(IResource copy, CopyArguments arguments) {
201         if (fCopy == null) {
202             fCopy= new ArrayList JavaDoc(2);
203             fCopyArguments= new ArrayList JavaDoc(2);
204         }
205         fCopy.add(copy);
206         fCopyArguments.add(arguments);
207         addCopyDelta(copy, arguments);
208     }
209
210     /**
211      * Adds the given resource to the list of renamed
212      * resources.
213      *
214      * @param rename the resource to be renamed
215      * @param arguments the arguments of the rename
216      */

217     public void addRename(IResource rename, RenameArguments arguments) {
218         Assert.isNotNull(rename);
219         Assert.isNotNull(arguments);
220         if (fRename == null) {
221             fRename= new ArrayList JavaDoc(2);
222             fRenameArguments= new ArrayList JavaDoc(2);
223         }
224         fRename.add(rename);
225         fRenameArguments.add(arguments);
226         if (fIgnoreCount == 0) {
227             IPath newPath= rename.getFullPath().removeLastSegments(1).append(arguments.getNewName());
228             internalAdd(new MoveDescription(rename, newPath));
229         }
230     }
231     
232     public RefactoringParticipant[] getParticipants(RefactoringStatus status, RefactoringProcessor processor, String JavaDoc[] natures, SharableParticipants shared) {
233         List JavaDoc result= new ArrayList JavaDoc(5);
234         if (fDelete != null) {
235             DeleteArguments arguments= new DeleteArguments();
236             for (Iterator JavaDoc iter= fDelete.iterator(); iter.hasNext();) {
237                 DeleteParticipant[] deletes= ParticipantManager.loadDeleteParticipants(status,
238                     processor, iter.next(),
239                     arguments, natures, shared);
240                 result.addAll(Arrays.asList(deletes));
241             }
242         }
243         if (fCreate != null) {
244             CreateArguments arguments= new CreateArguments();
245             for (Iterator JavaDoc iter= fCreate.iterator(); iter.hasNext();) {
246                 CreateParticipant[] creates= ParticipantManager.loadCreateParticipants(status,
247                     processor, iter.next(),
248                     arguments, natures, shared);
249                 result.addAll(Arrays.asList(creates));
250             }
251         }
252         if (fMove != null) {
253             for (int i= 0; i < fMove.size(); i++) {
254                 Object JavaDoc element= fMove.get(i);
255                 MoveArguments arguments= (MoveArguments)fMoveArguments.get(i);
256                 MoveParticipant[] moves= ParticipantManager.loadMoveParticipants(status,
257                     processor, element,
258                     arguments, natures, shared);
259                 result.addAll(Arrays.asList(moves));
260                 
261             }
262         }
263         if (fCopy != null) {
264             for (int i= 0; i < fCopy.size(); i++) {
265                 Object JavaDoc element= fCopy.get(i);
266                 CopyArguments arguments= (CopyArguments)fCopyArguments.get(i);
267                 CopyParticipant[] copies= ParticipantManager.loadCopyParticipants(status,
268                     processor, element,
269                     arguments, natures, shared);
270                 result.addAll(Arrays.asList(copies));
271             }
272         }
273         if (fRename != null) {
274             for (int i= 0; i < fRename.size(); i++) {
275                 Object JavaDoc resource= fRename.get(i);
276                 RenameArguments arguments= (RenameArguments) fRenameArguments.get(i);
277                 RenameParticipant[] renames= ParticipantManager.loadRenameParticipants(status,
278                     processor, resource,
279                     arguments, natures, shared);
280                 result.addAll(Arrays.asList(renames));
281             }
282         }
283         return (RefactoringParticipant[])result.toArray(new RefactoringParticipant[result.size()]);
284     }
285     
286     public void ignoreForDelta() {
287         fIgnoreCount++;
288     }
289     
290     public void trackForDelta() {
291         fIgnoreCount--;
292     }
293     
294     public void addDelta(DeltaDescription description) {
295         if (fIgnoreCount > 0)
296             return;
297         internalAdd(description);
298     }
299     
300     public void addCopyDelta(IResource copy, CopyArguments arguments) {
301         if (fIgnoreCount == 0) {
302             IPath destination= ((IResource)arguments.getDestination()).getFullPath().append(copy.getName());
303             internalAdd(new CopyDescription(copy, destination));
304         }
305     }
306     
307     /**
308      * Checks if the resource will exist in the future based on
309      * the recorded resource modifications.
310      *
311      * @param resource the resource to check
312      * @return whether the resource will exist or not
313      */

314     public boolean willExist(IResource resource) {
315         if (fDeltaDescriptions == null)
316             return false;
317         IPath fullPath= resource.getFullPath();
318         for (Iterator JavaDoc iter= fDeltaDescriptions.iterator(); iter.hasNext();) {
319             DeltaDescription delta= (DeltaDescription) iter.next();
320             if (fullPath.equals(delta.getDestinationPath()))
321                 return true;
322         }
323         return false;
324     }
325     
326     public void buildDelta(IResourceChangeDescriptionFactory builder) {
327         if (fDeltaDescriptions == null)
328             return;
329         for (Iterator JavaDoc iter= fDeltaDescriptions.iterator(); iter.hasNext();) {
330             ((DeltaDescription) iter.next()).buildDelta(builder);
331         }
332     }
333     
334     public static void buildMoveDelta(IResourceChangeDescriptionFactory builder, IResource resource, RenameArguments args) {
335         IPath newPath= resource.getFullPath().removeLastSegments(1).append(args.getNewName());
336         builder.move(resource, newPath);
337     }
338     
339     public static void buildMoveDelta(IResourceChangeDescriptionFactory builder, IResource resource, MoveArguments args) {
340         IPath destination= ((IResource)args.getDestination()).getFullPath().append(resource.getName());
341         builder.move(resource, destination);
342     }
343     
344     public static void buildCopyDelta(IResourceChangeDescriptionFactory builder, IResource resource, CopyArguments args) {
345         IPath destination= ((IResource)args.getDestination()).getFullPath().append(resource.getName());
346         builder.copy(resource, destination);
347     }
348     
349     private void internalAdd(DeltaDescription description) {
350         if (fDeltaDescriptions == null)
351             fDeltaDescriptions= new ArrayList JavaDoc();
352         fDeltaDescriptions.add(description);
353     }
354 }
355
Popular Tags