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.reorg;
011:
012: import java.util.ArrayList;
013: import java.util.Arrays;
014: import java.util.HashSet;
015: import java.util.Iterator;
016: import java.util.List;
017: import java.util.Set;
018:
019: import org.eclipse.core.runtime.Assert;
020: import org.eclipse.core.runtime.CoreException;
021:
022: import org.eclipse.core.resources.IContainer;
023: import org.eclipse.core.resources.IFile;
024: import org.eclipse.core.resources.IResource;
025: import org.eclipse.core.resources.mapping.IResourceChangeDescriptionFactory;
026:
027: import org.eclipse.ltk.core.refactoring.RefactoringStatus;
028: import org.eclipse.ltk.core.refactoring.participants.IParticipantDescriptorFilter;
029: import org.eclipse.ltk.core.refactoring.participants.MoveArguments;
030: import org.eclipse.ltk.core.refactoring.participants.ParticipantManager;
031: import org.eclipse.ltk.core.refactoring.participants.RefactoringArguments;
032: import org.eclipse.ltk.core.refactoring.participants.RefactoringParticipant;
033: import org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor;
034: import org.eclipse.ltk.core.refactoring.participants.SharableParticipants;
035: import org.eclipse.ltk.core.refactoring.participants.ValidateEditChecker;
036:
037: import org.eclipse.jdt.core.ICompilationUnit;
038: import org.eclipse.jdt.core.IJavaElement;
039: import org.eclipse.jdt.core.IPackageFragment;
040: import org.eclipse.jdt.core.IPackageFragmentRoot;
041: import org.eclipse.jdt.core.IType;
042:
043: import org.eclipse.jdt.internal.corext.refactoring.participants.ResourceModifications;
044:
045: public class MoveModifications extends RefactoringModifications {
046:
047: private List fMoves;
048: private List fMoveArguments;
049: private List fParticipantDescriptorFilter;
050:
051: public MoveModifications() {
052: fMoves = new ArrayList();
053: fMoveArguments = new ArrayList();
054: fParticipantDescriptorFilter = new ArrayList();
055: }
056:
057: public void move(IResource resource, MoveArguments args) {
058: add(resource, args, null);
059: }
060:
061: public void move(IPackageFragmentRoot sourceFolder,
062: MoveArguments arguments) {
063: add(sourceFolder, arguments, null);
064: IResource sourceResource = sourceFolder.getResource();
065: if (sourceResource != null) {
066: getResourceModifications().addMove(
067: sourceResource,
068: new MoveArguments(
069: getResourceDestination(arguments),
070: arguments.getUpdateReferences()));
071: IFile classpath = getClasspathFile(sourceResource);
072: if (classpath != null) {
073: getResourceModifications().addChanged(classpath);
074: }
075: classpath = getClasspathFile(getResourceDestination(arguments));
076: if (classpath != null) {
077: getResourceModifications().addChanged(classpath);
078: }
079: }
080: }
081:
082: public void move(IPackageFragment pack, MoveArguments args)
083: throws CoreException {
084: add(pack, args, null);
085: if (pack.getResource() == null)
086: return;
087: IPackageFragmentRoot javaDestination = (IPackageFragmentRoot) args
088: .getDestination();
089: if (javaDestination.getResource() == null)
090: return;
091: IPackageFragment newPack = javaDestination
092: .getPackageFragment(pack.getElementName());
093: if (!pack.hasSubpackages() && !newPack.exists()) {
094: // we can do a simple move
095: IContainer resourceDestination = newPack.getResource()
096: .getParent();
097: createIncludingParents(resourceDestination);
098: getResourceModifications().addMove(
099: pack.getResource(),
100: new MoveArguments(resourceDestination, args
101: .getUpdateReferences()));
102: } else {
103: IContainer resourceSource = (IContainer) pack.getResource();
104: IContainer resourceDestination = (IContainer) newPack
105: .getResource();
106: createIncludingParents(resourceDestination);
107: MoveArguments arguments = new MoveArguments(
108: resourceDestination, args.getUpdateReferences());
109: IResource[] resourcesToMove = collectResourcesOfInterest(pack);
110: Set allMembers = new HashSet(Arrays.asList(resourceSource
111: .members()));
112: for (int i = 0; i < resourcesToMove.length; i++) {
113: IResource toMove = resourcesToMove[i];
114: getResourceModifications().addMove(toMove, arguments);
115: allMembers.remove(toMove);
116: }
117: for (Iterator iter = allMembers.iterator(); iter.hasNext();) {
118: IResource element = (IResource) iter.next();
119: if (element instanceof IFile) {
120: getResourceModifications().addDelete(element);
121: iter.remove();
122: }
123: }
124: if (allMembers.isEmpty()) {
125: getResourceModifications().addDelete(resourceSource);
126: }
127: }
128: }
129:
130: public void move(ICompilationUnit unit, MoveArguments args)
131: throws CoreException {
132: add(unit, args, null);
133: IType[] types = unit.getTypes();
134: for (int tt = 0; tt < types.length; tt++) {
135: add(types[tt], args, null);
136: }
137: IResource resourceDestination = getResourceDestination(args);
138: if (resourceDestination != null && unit.getResource() != null) {
139: IResource parent = resourceDestination;
140: while (!parent.exists()) {
141: getResourceModifications().addCreate(parent);
142: parent = parent.getParent();
143: }
144:
145: getResourceModifications().addMove(
146: unit.getResource(),
147: new MoveArguments(resourceDestination, args
148: .getUpdateReferences()));
149: }
150: }
151:
152: public void buildDelta(IResourceChangeDescriptionFactory builder) {
153: for (int i = 0; i < fMoves.size(); i++) {
154: Object element = fMoves.get(i);
155: if (element instanceof IResource) {
156: ResourceModifications.buildMoveDelta(builder,
157: (IResource) element,
158: (MoveArguments) fMoveArguments.get(i));
159: }
160: }
161: getResourceModifications().buildDelta(builder);
162: }
163:
164: public void buildValidateEdits(ValidateEditChecker checker) {
165: for (Iterator iter = fMoves.iterator(); iter.hasNext();) {
166: Object element = iter.next();
167: if (element instanceof ICompilationUnit) {
168: ICompilationUnit unit = (ICompilationUnit) element;
169: IResource resource = unit.getResource();
170: if (resource != null
171: && resource.getType() == IResource.FILE) {
172: checker.addFile((IFile) resource);
173: }
174: }
175: }
176: }
177:
178: public RefactoringParticipant[] loadParticipants(
179: RefactoringStatus status, RefactoringProcessor owner,
180: String[] natures, SharableParticipants shared) {
181: List result = new ArrayList();
182: for (int i = 0; i < fMoves.size(); i++) {
183: result
184: .addAll(Arrays
185: .asList(ParticipantManager
186: .loadMoveParticipants(
187: status,
188: owner,
189: fMoves.get(i),
190: (MoveArguments) fMoveArguments
191: .get(i),
192: (IParticipantDescriptorFilter) fParticipantDescriptorFilter
193: .get(i), natures,
194: shared)));
195: }
196: result.addAll(Arrays.asList(getResourceModifications()
197: .getParticipants(status, owner, natures, shared)));
198: return (RefactoringParticipant[]) result
199: .toArray(new RefactoringParticipant[result.size()]);
200: }
201:
202: private void add(Object element, RefactoringArguments args,
203: IParticipantDescriptorFilter filter) {
204: Assert.isNotNull(element);
205: Assert.isNotNull(args);
206: fMoves.add(element);
207: fMoveArguments.add(args);
208: fParticipantDescriptorFilter.add(filter);
209: }
210:
211: private IResource getResourceDestination(MoveArguments args) {
212: Object genericDestination = args.getDestination();
213: IResource resourceDestination = null;
214: if (genericDestination instanceof IJavaElement) {
215: resourceDestination = ((IJavaElement) genericDestination)
216: .getResource();
217: } else if (genericDestination instanceof IResource) {
218: resourceDestination = (IResource) genericDestination;
219: }
220: return resourceDestination;
221: }
222: }
|