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.List;
015:
016: import org.eclipse.core.runtime.Assert;
017: import org.eclipse.core.runtime.CoreException;
018:
019: import org.eclipse.core.resources.IContainer;
020: import org.eclipse.core.resources.IFile;
021: import org.eclipse.core.resources.IResource;
022: import org.eclipse.core.resources.mapping.IResourceChangeDescriptionFactory;
023: import org.eclipse.core.resources.mapping.ResourceMapping;
024:
025: import org.eclipse.ltk.core.refactoring.RefactoringStatus;
026: import org.eclipse.ltk.core.refactoring.participants.CopyArguments;
027: import org.eclipse.ltk.core.refactoring.participants.IParticipantDescriptorFilter;
028: import org.eclipse.ltk.core.refactoring.participants.ParticipantManager;
029: import org.eclipse.ltk.core.refactoring.participants.RefactoringArguments;
030: import org.eclipse.ltk.core.refactoring.participants.RefactoringParticipant;
031: import org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor;
032: import org.eclipse.ltk.core.refactoring.participants.SharableParticipants;
033:
034: import org.eclipse.jdt.core.ICompilationUnit;
035: import org.eclipse.jdt.core.IJavaElement;
036: import org.eclipse.jdt.core.IPackageFragment;
037: import org.eclipse.jdt.core.IPackageFragmentRoot;
038:
039: import org.eclipse.jdt.internal.corext.refactoring.participants.ResourceModifications;
040: import org.eclipse.jdt.internal.corext.util.JavaElementResourceMapping;
041:
042: public class CopyModifications extends RefactoringModifications {
043:
044: private List fCopies;
045: private List fCopyArguments;
046: private List fParticipantDescriptorFilter;
047:
048: public CopyModifications() {
049: fCopies = new ArrayList();
050: fCopyArguments = new ArrayList();
051: fParticipantDescriptorFilter = new ArrayList();
052: }
053:
054: public void copy(IResource resource, CopyArguments args) {
055: add(resource, args, null);
056: }
057:
058: public void copy(IJavaElement element, CopyArguments javaArgs,
059: CopyArguments resourceArgs) throws CoreException {
060: switch (element.getElementType()) {
061: case IJavaElement.PACKAGE_FRAGMENT_ROOT:
062: copy((IPackageFragmentRoot) element, javaArgs, resourceArgs);
063: break;
064: case IJavaElement.PACKAGE_FRAGMENT:
065: copy((IPackageFragment) element, javaArgs, resourceArgs);
066: break;
067: case IJavaElement.COMPILATION_UNIT:
068: copy((ICompilationUnit) element, javaArgs, resourceArgs);
069: break;
070: default:
071: add(element, javaArgs, null);
072: }
073: }
074:
075: public void copy(IPackageFragmentRoot sourceFolder,
076: CopyArguments javaArgs, CopyArguments resourceArgs) {
077: add(sourceFolder, javaArgs, null);
078: ResourceMapping mapping = JavaElementResourceMapping
079: .create(sourceFolder);
080: if (mapping != null) {
081: add(mapping, resourceArgs, null);
082: }
083: IResource sourceResource = sourceFolder.getResource();
084: if (sourceResource != null) {
085: getResourceModifications().addCopyDelta(sourceResource,
086: resourceArgs);
087: IFile classpath = getClasspathFile((IResource) resourceArgs
088: .getDestination());
089: if (classpath != null) {
090: getResourceModifications().addChanged(classpath);
091: }
092: }
093: }
094:
095: public void copy(IPackageFragment pack, CopyArguments javaArgs,
096: CopyArguments resourceArgs) throws CoreException {
097: add(pack, javaArgs, null);
098: ResourceMapping mapping = JavaElementResourceMapping
099: .create(pack);
100: if (mapping != null) {
101: add(mapping, resourceArgs, null);
102: }
103: IPackageFragmentRoot javaDestination = (IPackageFragmentRoot) javaArgs
104: .getDestination();
105: if (javaDestination.getResource() == null)
106: return;
107: IPackageFragment newPack = javaDestination
108: .getPackageFragment(pack.getElementName());
109: // Here we have a special case. When we copy a package into the same source
110: // folder than the user will choose an "unused" name at the end which will
111: // lead to the fact that we can copy the pack. Unfortunately we don't know
112: // the new name yet, so we use the current package name.
113: if (!pack.hasSubpackages()
114: && (!newPack.exists() || pack.equals(newPack))) {
115: // we can do a simple move
116: IContainer resourceDestination = newPack.getResource()
117: .getParent();
118: createIncludingParents(resourceDestination);
119: getResourceModifications().addCopyDelta(pack.getResource(),
120: resourceArgs);
121: } else {
122: IContainer resourceDestination = (IContainer) newPack
123: .getResource();
124: createIncludingParents(resourceDestination);
125: CopyArguments arguments = new CopyArguments(
126: resourceDestination, resourceArgs.getExecutionLog());
127: IResource[] resourcesToCopy = collectResourcesOfInterest(pack);
128: for (int i = 0; i < resourcesToCopy.length; i++) {
129: IResource toCopy = resourcesToCopy[i];
130: getResourceModifications().addCopyDelta(toCopy,
131: arguments);
132: }
133: }
134: }
135:
136: public void copy(ICompilationUnit unit, CopyArguments javaArgs,
137: CopyArguments resourceArgs) throws CoreException {
138: add(unit, javaArgs, null);
139: ResourceMapping mapping = JavaElementResourceMapping
140: .create(unit);
141: if (mapping != null) {
142: add(mapping, resourceArgs, null);
143: }
144: if (unit.getResource() != null) {
145: getResourceModifications().addCopyDelta(unit.getResource(),
146: resourceArgs);
147: }
148: }
149:
150: public void buildDelta(IResourceChangeDescriptionFactory builder) {
151: for (int i = 0; i < fCopies.size(); i++) {
152: Object element = fCopies.get(i);
153: if (element instanceof IResource) {
154: ResourceModifications.buildCopyDelta(builder,
155: (IResource) element,
156: (CopyArguments) fCopyArguments.get(i));
157: }
158: }
159: getResourceModifications().buildDelta(builder);
160: }
161:
162: public RefactoringParticipant[] loadParticipants(
163: RefactoringStatus status, RefactoringProcessor owner,
164: String[] natures, SharableParticipants shared) {
165: List result = new ArrayList();
166: for (int i = 0; i < fCopies.size(); i++) {
167: result
168: .addAll(Arrays
169: .asList(ParticipantManager
170: .loadCopyParticipants(
171: status,
172: owner,
173: fCopies.get(i),
174: (CopyArguments) fCopyArguments
175: .get(i),
176: (IParticipantDescriptorFilter) fParticipantDescriptorFilter
177: .get(i), natures,
178: shared)));
179: }
180: result.addAll(Arrays.asList(getResourceModifications()
181: .getParticipants(status, owner, natures, shared)));
182: return (RefactoringParticipant[]) result
183: .toArray(new RefactoringParticipant[result.size()]);
184: }
185:
186: private void add(Object element, RefactoringArguments args,
187: IParticipantDescriptorFilter filter) {
188: Assert.isNotNull(element);
189: Assert.isNotNull(args);
190: fCopies.add(element);
191: fCopyArguments.add(args);
192: fParticipantDescriptorFilter.add(filter);
193: }
194: }
|