KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > reorg > CopyModifications


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.reorg;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.Assert;
18 import org.eclipse.core.runtime.CoreException;
19
20 import org.eclipse.core.resources.IContainer;
21 import org.eclipse.core.resources.IFile;
22 import org.eclipse.core.resources.IResource;
23 import org.eclipse.core.resources.mapping.IResourceChangeDescriptionFactory;
24 import org.eclipse.core.resources.mapping.ResourceMapping;
25
26 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
27 import org.eclipse.ltk.core.refactoring.participants.CopyArguments;
28 import org.eclipse.ltk.core.refactoring.participants.IParticipantDescriptorFilter;
29 import org.eclipse.ltk.core.refactoring.participants.ParticipantManager;
30 import org.eclipse.ltk.core.refactoring.participants.RefactoringArguments;
31 import org.eclipse.ltk.core.refactoring.participants.RefactoringParticipant;
32 import org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor;
33 import org.eclipse.ltk.core.refactoring.participants.SharableParticipants;
34
35 import org.eclipse.jdt.core.ICompilationUnit;
36 import org.eclipse.jdt.core.IJavaElement;
37 import org.eclipse.jdt.core.IPackageFragment;
38 import org.eclipse.jdt.core.IPackageFragmentRoot;
39
40 import org.eclipse.jdt.internal.corext.refactoring.participants.ResourceModifications;
41 import org.eclipse.jdt.internal.corext.util.JavaElementResourceMapping;
42
43 public class CopyModifications extends RefactoringModifications {
44     
45     private List JavaDoc fCopies;
46     private List JavaDoc fCopyArguments;
47     private List JavaDoc fParticipantDescriptorFilter;
48     
49     public CopyModifications() {
50         fCopies= new ArrayList JavaDoc();
51         fCopyArguments= new ArrayList JavaDoc();
52         fParticipantDescriptorFilter= new ArrayList JavaDoc();
53     }
54     
55     public void copy(IResource resource, CopyArguments args) {
56         add(resource, args, null);
57     }
58     
59     public void copy(IJavaElement element, CopyArguments javaArgs, CopyArguments resourceArgs) throws CoreException {
60         switch(element.getElementType()) {
61             case IJavaElement.PACKAGE_FRAGMENT_ROOT:
62                 copy((IPackageFragmentRoot)element, javaArgs, resourceArgs);
63                 break;
64             case IJavaElement.PACKAGE_FRAGMENT:
65                 copy((IPackageFragment)element, javaArgs, resourceArgs);
66                 break;
67             case IJavaElement.COMPILATION_UNIT:
68                 copy((ICompilationUnit)element, javaArgs, resourceArgs);
69                 break;
70             default:
71                 add(element, javaArgs, null);
72         }
73     }
74
75     public void copy(IPackageFragmentRoot sourceFolder, CopyArguments javaArgs, CopyArguments resourceArgs) {
76         add(sourceFolder, javaArgs, null);
77         ResourceMapping mapping= JavaElementResourceMapping.create(sourceFolder);
78         if (mapping != null) {
79             add(mapping, resourceArgs, null);
80         }
81         IResource sourceResource= sourceFolder.getResource();
82         if (sourceResource != null) {
83             getResourceModifications().addCopyDelta(sourceResource, resourceArgs);
84             IFile classpath= getClasspathFile((IResource) resourceArgs.getDestination());
85             if (classpath != null) {
86                 getResourceModifications().addChanged(classpath);
87             }
88         }
89     }
90     
91     public void copy(IPackageFragment pack, CopyArguments javaArgs, CopyArguments resourceArgs) throws CoreException {
92         add(pack, javaArgs, null);
93         ResourceMapping mapping= JavaElementResourceMapping.create(pack);
94         if (mapping != null) {
95             add(mapping, resourceArgs, null);
96         }
97         IPackageFragmentRoot javaDestination= (IPackageFragmentRoot) javaArgs.getDestination();
98         if (javaDestination.getResource() == null)
99             return;
100         IPackageFragment newPack= javaDestination.getPackageFragment(pack.getElementName());
101         // Here we have a special case. When we copy a package into the same source
102
// folder than the user will choose an "unused" name at the end which will
103
// lead to the fact that we can copy the pack. Unfortunately we don't know
104
// the new name yet, so we use the current package name.
105
if (!pack.hasSubpackages() && (!newPack.exists() || pack.equals(newPack))) {
106             // we can do a simple move
107
IContainer resourceDestination= newPack.getResource().getParent();
108             createIncludingParents(resourceDestination);
109             getResourceModifications().addCopyDelta(pack.getResource(), resourceArgs);
110         } else {
111             IContainer resourceDestination= (IContainer) newPack.getResource();
112             createIncludingParents(resourceDestination);
113             CopyArguments arguments= new CopyArguments(resourceDestination, resourceArgs.getExecutionLog());
114             IResource[] resourcesToCopy= collectResourcesOfInterest(pack);
115             for (int i= 0; i < resourcesToCopy.length; i++) {
116                 IResource toCopy= resourcesToCopy[i];
117                 getResourceModifications().addCopyDelta(toCopy, arguments);
118             }
119         }
120     }
121
122     public void copy(ICompilationUnit unit, CopyArguments javaArgs, CopyArguments resourceArgs) throws CoreException {
123         add(unit, javaArgs, null);
124         ResourceMapping mapping= JavaElementResourceMapping.create(unit);
125         if (mapping != null) {
126             add(mapping, resourceArgs, null);
127         }
128         if (unit.getResource() != null) {
129             getResourceModifications().addCopyDelta(unit.getResource(), resourceArgs);
130         }
131     }
132
133     public void buildDelta(IResourceChangeDescriptionFactory builder) {
134         for (int i= 0; i < fCopies.size(); i++) {
135             Object JavaDoc element= fCopies.get(i);
136             if (element instanceof IResource) {
137                 ResourceModifications.buildCopyDelta(builder, (IResource) element, (CopyArguments) fCopyArguments.get(i));
138             }
139         }
140         getResourceModifications().buildDelta(builder);
141     }
142     
143     public RefactoringParticipant[] loadParticipants(RefactoringStatus status, RefactoringProcessor owner, String JavaDoc[] natures, SharableParticipants shared) {
144         List JavaDoc result= new ArrayList JavaDoc();
145         for (int i= 0; i < fCopies.size(); i++) {
146             result.addAll(Arrays.asList(ParticipantManager.loadCopyParticipants(status,
147                 owner, fCopies.get(i),
148                 (CopyArguments) fCopyArguments.get(i),
149                 (IParticipantDescriptorFilter) fParticipantDescriptorFilter.get(i),
150                 natures, shared)));
151         }
152         result.addAll(Arrays.asList(getResourceModifications().getParticipants(status, owner, natures, shared)));
153         return (RefactoringParticipant[]) result.toArray(new RefactoringParticipant[result.size()]);
154     }
155     
156     private void add(Object JavaDoc element, RefactoringArguments args, IParticipantDescriptorFilter filter) {
157         Assert.isNotNull(element);
158         Assert.isNotNull(args);
159         fCopies.add(element);
160         fCopyArguments.add(args);
161         fParticipantDescriptorFilter.add(filter);
162     }
163 }
164
Popular Tags