001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.corext.refactoring.participants;
011:
012: import java.util.ArrayList;
013: import java.util.Arrays;
014: import java.util.Iterator;
015: import java.util.List;
016:
017: import org.eclipse.core.runtime.Assert;
018: import org.eclipse.core.runtime.IPath;
019:
020: import org.eclipse.core.resources.IFile;
021: import org.eclipse.core.resources.IResource;
022: import org.eclipse.core.resources.mapping.IResourceChangeDescriptionFactory;
023:
024: import org.eclipse.ltk.core.refactoring.RefactoringStatus;
025: import org.eclipse.ltk.core.refactoring.participants.CopyArguments;
026: import org.eclipse.ltk.core.refactoring.participants.CopyParticipant;
027: import org.eclipse.ltk.core.refactoring.participants.CreateArguments;
028: import org.eclipse.ltk.core.refactoring.participants.CreateParticipant;
029: import org.eclipse.ltk.core.refactoring.participants.DeleteArguments;
030: import org.eclipse.ltk.core.refactoring.participants.DeleteParticipant;
031: import org.eclipse.ltk.core.refactoring.participants.MoveArguments;
032: import org.eclipse.ltk.core.refactoring.participants.MoveParticipant;
033: import org.eclipse.ltk.core.refactoring.participants.ParticipantManager;
034: import org.eclipse.ltk.core.refactoring.participants.RefactoringParticipant;
035: import org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor;
036: import org.eclipse.ltk.core.refactoring.participants.RenameArguments;
037: import org.eclipse.ltk.core.refactoring.participants.RenameParticipant;
038: import org.eclipse.ltk.core.refactoring.participants.SharableParticipants;
039:
040: /**
041: * A data structure to collect resource modifications.
042: *
043: * @since 3.0
044: */
045: public class ResourceModifications {
046:
047: private List/*<IResource>*/fCreate;
048: private List/*<IResource>*/fDelete;
049:
050: private List/*<IResource>*/fMove;
051: private List/*<MoveArguments>*/fMoveArguments;
052:
053: private List/*<IResource>*/fRename;
054: private List/*<RenameArguments>*/fRenameArguments;
055:
056: private List/*<IResource>*/fCopy;
057: private List/*<CopyArguments>*/fCopyArguments;
058:
059: private int fIgnoreCount;
060: private List/*<DeltaDescription>*/fDeltaDescriptions;
061:
062: public static abstract class DeltaDescription {
063: protected IResource fResource;
064:
065: public DeltaDescription(IResource resource) {
066: fResource = resource;
067: }
068:
069: public abstract void buildDelta(
070: IResourceChangeDescriptionFactory builder);
071:
072: public abstract IPath getDestinationPath();
073:
074: }
075:
076: public static class DeleteDescription extends DeltaDescription {
077: public DeleteDescription(IResource resource) {
078: super (resource);
079: }
080:
081: public void buildDelta(IResourceChangeDescriptionFactory builder) {
082: builder.delete(fResource);
083: }
084:
085: public IPath getDestinationPath() {
086: return null;
087: }
088: }
089:
090: public static class ChangedDescription extends DeltaDescription {
091: public ChangedDescription(IFile resource) {
092: super (resource);
093: }
094:
095: public void buildDelta(IResourceChangeDescriptionFactory builder) {
096: builder.change((IFile) fResource);
097: }
098:
099: public IPath getDestinationPath() {
100: return null;
101: }
102: }
103:
104: public static class CreateDescription extends DeltaDescription {
105: public CreateDescription(IResource resource) {
106: super (resource);
107: }
108:
109: public void buildDelta(IResourceChangeDescriptionFactory builder) {
110: builder.create(fResource);
111: }
112:
113: public IPath getDestinationPath() {
114: return fResource.getFullPath();
115: }
116: }
117:
118: public static class MoveDescription extends DeltaDescription {
119: private IPath fDestination;
120:
121: public MoveDescription(IResource resource, IPath destination) {
122: super (resource);
123: fDestination = destination;
124: }
125:
126: public void buildDelta(IResourceChangeDescriptionFactory builder) {
127: builder.move(fResource, fDestination);
128: }
129:
130: public IPath getDestinationPath() {
131: return fDestination;
132: }
133: }
134:
135: public static class CopyDescription extends DeltaDescription {
136: private IPath fDestination;
137:
138: public CopyDescription(IResource resource, IPath destination) {
139: super (resource);
140: fDestination = destination;
141: }
142:
143: public void buildDelta(IResourceChangeDescriptionFactory builder) {
144: builder.copy(fResource, fDestination);
145: }
146:
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(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(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: */
200: public void addMove(IResource move, MoveArguments arguments) {
201: if (fMove == null) {
202: fMove = new ArrayList(2);
203: fMoveArguments = new ArrayList(2);
204: }
205: fMove.add(move);
206: fMoveArguments.add(arguments);
207: if (fIgnoreCount == 0) {
208: IPath destination = ((IResource) arguments.getDestination())
209: .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: */
220: public void addCopy(IResource copy, CopyArguments arguments) {
221: if (fCopy == null) {
222: fCopy = new ArrayList(2);
223: fCopyArguments = new ArrayList(2);
224: }
225: fCopy.add(copy);
226: fCopyArguments.add(arguments);
227: addCopyDelta(copy, arguments);
228: }
229:
230: /**
231: * Adds the given resource to the list of renamed
232: * resources.
233: *
234: * @param rename the resource to be renamed
235: * @param arguments the arguments of the rename
236: */
237: public void addRename(IResource rename, RenameArguments arguments) {
238: Assert.isNotNull(rename);
239: Assert.isNotNull(arguments);
240: if (fRename == null) {
241: fRename = new ArrayList(2);
242: fRenameArguments = new ArrayList(2);
243: }
244: fRename.add(rename);
245: fRenameArguments.add(arguments);
246: if (fIgnoreCount == 0) {
247: IPath newPath = rename.getFullPath().removeLastSegments(1)
248: .append(arguments.getNewName());
249: internalAdd(new MoveDescription(rename, newPath));
250: }
251: }
252:
253: public RefactoringParticipant[] getParticipants(
254: RefactoringStatus status, RefactoringProcessor processor,
255: String[] natures, SharableParticipants shared) {
256: List result = new ArrayList(5);
257: if (fDelete != null) {
258: DeleteArguments arguments = new DeleteArguments();
259: for (Iterator iter = fDelete.iterator(); iter.hasNext();) {
260: DeleteParticipant[] deletes = ParticipantManager
261: .loadDeleteParticipants(status, processor, iter
262: .next(), arguments, natures, shared);
263: result.addAll(Arrays.asList(deletes));
264: }
265: }
266: if (fCreate != null) {
267: CreateArguments arguments = new CreateArguments();
268: for (Iterator iter = fCreate.iterator(); iter.hasNext();) {
269: CreateParticipant[] creates = ParticipantManager
270: .loadCreateParticipants(status, processor, iter
271: .next(), arguments, natures, shared);
272: result.addAll(Arrays.asList(creates));
273: }
274: }
275: if (fMove != null) {
276: for (int i = 0; i < fMove.size(); i++) {
277: Object element = fMove.get(i);
278: MoveArguments arguments = (MoveArguments) fMoveArguments
279: .get(i);
280: MoveParticipant[] moves = ParticipantManager
281: .loadMoveParticipants(status, processor,
282: element, arguments, natures, shared);
283: result.addAll(Arrays.asList(moves));
284:
285: }
286: }
287: if (fCopy != null) {
288: for (int i = 0; i < fCopy.size(); i++) {
289: Object element = fCopy.get(i);
290: CopyArguments arguments = (CopyArguments) fCopyArguments
291: .get(i);
292: CopyParticipant[] copies = ParticipantManager
293: .loadCopyParticipants(status, processor,
294: element, arguments, natures, shared);
295: result.addAll(Arrays.asList(copies));
296: }
297: }
298: if (fRename != null) {
299: for (int i = 0; i < fRename.size(); i++) {
300: Object resource = fRename.get(i);
301: RenameArguments arguments = (RenameArguments) fRenameArguments
302: .get(i);
303: RenameParticipant[] renames = ParticipantManager
304: .loadRenameParticipants(status, processor,
305: resource, arguments, natures, shared);
306: result.addAll(Arrays.asList(renames));
307: }
308: }
309: return (RefactoringParticipant[]) result
310: .toArray(new RefactoringParticipant[result.size()]);
311: }
312:
313: public void ignoreForDelta() {
314: fIgnoreCount++;
315: }
316:
317: public void trackForDelta() {
318: fIgnoreCount--;
319: }
320:
321: public void addDelta(DeltaDescription description) {
322: if (fIgnoreCount > 0)
323: return;
324: internalAdd(description);
325: }
326:
327: public void addCopyDelta(IResource copy, CopyArguments arguments) {
328: if (fIgnoreCount == 0) {
329: IPath destination = ((IResource) arguments.getDestination())
330: .getFullPath().append(copy.getName());
331: internalAdd(new CopyDescription(copy, destination));
332: }
333: }
334:
335: /**
336: * Checks if the resource will exist in the future based on
337: * the recorded resource modifications.
338: *
339: * @param resource the resource to check
340: * @return whether the resource will exist or not
341: */
342: public boolean willExist(IResource resource) {
343: if (fDeltaDescriptions == null)
344: return false;
345: IPath fullPath = resource.getFullPath();
346: for (Iterator iter = fDeltaDescriptions.iterator(); iter
347: .hasNext();) {
348: DeltaDescription delta = (DeltaDescription) iter.next();
349: if (fullPath.equals(delta.getDestinationPath()))
350: return true;
351: }
352: return false;
353: }
354:
355: public void buildDelta(IResourceChangeDescriptionFactory builder) {
356: if (fDeltaDescriptions == null)
357: return;
358: for (Iterator iter = fDeltaDescriptions.iterator(); iter
359: .hasNext();) {
360: ((DeltaDescription) iter.next()).buildDelta(builder);
361: }
362: }
363:
364: public static void buildMoveDelta(
365: IResourceChangeDescriptionFactory builder,
366: IResource resource, RenameArguments args) {
367: IPath newPath = resource.getFullPath().removeLastSegments(1)
368: .append(args.getNewName());
369: builder.move(resource, newPath);
370: }
371:
372: public static void buildMoveDelta(
373: IResourceChangeDescriptionFactory builder,
374: IResource resource, MoveArguments args) {
375: IPath destination = ((IResource) args.getDestination())
376: .getFullPath().append(resource.getName());
377: builder.move(resource, destination);
378: }
379:
380: public static void buildCopyDelta(
381: IResourceChangeDescriptionFactory builder,
382: IResource resource, CopyArguments args) {
383: IPath destination = ((IResource) args.getDestination())
384: .getFullPath().append(resource.getName());
385: builder.copy(resource, destination);
386: }
387:
388: private void internalAdd(DeltaDescription description) {
389: if (fDeltaDescriptions == null)
390: fDeltaDescriptions = new ArrayList();
391: fDeltaDescriptions.add(description);
392: }
393: }
|