]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/core refactoring/org/eclipse/jdt/internal/corext/refactoring/reorg/CopyModifications.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / core refactoring / org / eclipse / jdt / internal / corext / refactoring / reorg / CopyModifications.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2011 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;
14 import java.util.Arrays;
15 import java.util.List;
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<Object> fCopies;
46         private List<RefactoringArguments> fCopyArguments;
47         private List<IParticipantDescriptorFilter> fParticipantDescriptorFilter;
48
49         public CopyModifications() {
50                 fCopies= new ArrayList<Object>();
51                 fCopyArguments= new ArrayList<RefactoringArguments>();
52                 fParticipantDescriptorFilter= new ArrayList<IParticipantDescriptorFilter>();
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) {
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         @Override
134         public void buildDelta(IResourceChangeDescriptionFactory builder) {
135                 for (int i= 0; i < fCopies.size(); i++) {
136                         Object element= fCopies.get(i);
137                         if (element instanceof IResource) {
138                                 ResourceModifications.buildCopyDelta(builder, (IResource) element, (CopyArguments) fCopyArguments.get(i));
139                         }
140                 }
141                 getResourceModifications().buildDelta(builder);
142         }
143
144         @Override
145         public RefactoringParticipant[] loadParticipants(RefactoringStatus status, RefactoringProcessor owner, String[] natures, SharableParticipants shared) {
146                 List<RefactoringParticipant> result= new ArrayList<RefactoringParticipant>();
147                 for (int i= 0; i < fCopies.size(); i++) {
148                         result.addAll(Arrays.asList(ParticipantManager.loadCopyParticipants(status,
149                                 owner, fCopies.get(i),
150                                 (CopyArguments) fCopyArguments.get(i),
151                                 fParticipantDescriptorFilter.get(i),
152                                 natures, shared)));
153                 }
154                 result.addAll(Arrays.asList(getResourceModifications().getParticipants(status, owner, natures, shared)));
155                 return result.toArray(new RefactoringParticipant[result.size()]);
156         }
157
158         private void add(Object element, RefactoringArguments args, IParticipantDescriptorFilter filter) {
159                 Assert.isNotNull(element);
160                 Assert.isNotNull(args);
161                 fCopies.add(element);
162                 fCopyArguments.add(args);
163                 fParticipantDescriptorFilter.add(filter);
164         }
165 }