]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
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.List; | |
15 | ||
16 | import org.eclipse.core.runtime.Assert; | |
17 | ||
18 | import org.eclipse.core.resources.IResource; | |
19 | ||
20 | import org.eclipse.jdt.core.IJarEntryResource; | |
21 | import org.eclipse.jdt.core.IJavaElement; | |
22 | import org.eclipse.jdt.core.JavaCore; | |
23 | ||
24 | ||
25 | public class ParentChecker { | |
26 | private IResource[] fResources; | |
27 | private IJavaElement[] fJavaElements; | |
28 | private IJarEntryResource[] fJarResources; | |
29 | ||
30 | public ParentChecker(IResource[] resources, IJavaElement[] javaElements) { | |
31 | this(resources, javaElements, new IJarEntryResource[0]); | |
32 | } | |
33 | ||
34 | public ParentChecker(IResource[] resources, IJavaElement[] javaElements, IJarEntryResource[] jarResources) { | |
35 | Assert.isNotNull(resources); | |
36 | Assert.isNotNull(javaElements); | |
37 | Assert.isNotNull(jarResources); | |
38 | fResources= resources; | |
39 | fJavaElements= javaElements; | |
40 | fJarResources= jarResources; | |
41 | } | |
42 | ||
43 | public boolean haveCommonParent() { | |
44 | return getCommonParent() != null; | |
45 | } | |
46 | ||
47 | public Object getCommonParent(){ | |
48 | if (fJavaElements.length == 0 && fResources.length == 0 && fJarResources.length == 0) | |
49 | return null; | |
50 | if (! resourcesHaveCommonParent() || ! javaElementsHaveCommonParent() || ! jarResourcesHaveCommonParent()) | |
51 | return null; | |
52 | if (fJavaElements.length == 0 && fResources.length == 0) | |
53 | return getCommonJarResourceParent(); | |
54 | if (fJavaElements.length == 0 && fJarResources.length == 0) { | |
55 | IResource commonResourceParent= getCommonResourceParent(); | |
56 | Assert.isNotNull(commonResourceParent); | |
57 | IJavaElement convertedToJava= JavaCore.create(commonResourceParent); | |
58 | if (convertedToJava != null && convertedToJava.exists()) | |
59 | return convertedToJava; | |
60 | else | |
61 | return commonResourceParent; | |
62 | } | |
63 | if (fResources.length == 0 && fJarResources.length == 0) | |
64 | return getCommonJavaElementParent(); | |
65 | ||
66 | IJavaElement convertedToJava= null; | |
67 | IJavaElement commonJavaElementParent= null; | |
68 | Object commonJarResourcesParent= null; | |
69 | Object commonParent= null; | |
70 | if (fResources.length != 0) { | |
71 | IResource commonResourceParent= getCommonResourceParent(); | |
72 | Assert.isNotNull(commonResourceParent); | |
73 | convertedToJava= JavaCore.create(commonResourceParent); | |
74 | if (convertedToJava == null || !convertedToJava.exists()) | |
75 | return null; | |
76 | } | |
77 | if (fJavaElements.length != 0) { | |
78 | commonJavaElementParent= getCommonJavaElementParent(); | |
79 | Assert.isNotNull(commonJavaElementParent); | |
80 | if (convertedToJava != null && !commonJavaElementParent.equals(convertedToJava)) | |
81 | return null; | |
82 | } | |
83 | commonParent= convertedToJava == null ? commonJavaElementParent : convertedToJava; | |
84 | if (fJarResources.length != 0) { | |
85 | commonJarResourcesParent= getCommonJarResourceParent(); | |
86 | Assert.isNotNull(commonJarResourcesParent); | |
87 | if (!commonJarResourcesParent.equals(commonParent)) | |
88 | return null; | |
89 | } | |
90 | return commonParent; | |
91 | } | |
92 | ||
93 | /** | |
94 | * Return the common parent for the jar resources. | |
95 | * | |
96 | * @return the common parent for the jar resources | |
97 | * @since 3.6 | |
98 | */ | |
99 | private Object getCommonJarResourceParent() { | |
100 | Assert.isNotNull(fJarResources); | |
101 | Assert.isTrue(fJarResources.length > 0);//safe - checked before | |
102 | return fJarResources[0].getParent(); | |
103 | } | |
104 | ||
105 | private IJavaElement getCommonJavaElementParent() { | |
106 | Assert.isNotNull(fJavaElements); | |
107 | Assert.isTrue(fJavaElements.length > 0);//safe - checked before | |
108 | return fJavaElements[0].getParent(); | |
109 | } | |
110 | ||
111 | private IResource getCommonResourceParent() { | |
112 | Assert.isNotNull(fResources); | |
113 | Assert.isTrue(fResources.length > 0);//safe - checked before | |
114 | return fResources[0].getParent(); | |
115 | } | |
116 | ||
117 | private boolean javaElementsHaveCommonParent() { | |
118 | if (fJavaElements.length == 0) | |
119 | return true; | |
120 | IJavaElement firstParent= fJavaElements[0].getParent(); | |
121 | Assert.isNotNull(firstParent); //this should never happen | |
122 | for (int i= 1; i < fJavaElements.length; i++) { | |
123 | if (! firstParent.equals(fJavaElements[i].getParent())) | |
124 | return false; | |
125 | } | |
126 | return true; | |
127 | } | |
128 | ||
129 | private boolean resourcesHaveCommonParent() { | |
130 | if (fResources.length == 0) | |
131 | return true; | |
132 | IResource firstParent= fResources[0].getParent(); | |
133 | Assert.isNotNull(firstParent); | |
134 | for (int i= 1; i < fResources.length; i++) { | |
135 | if (! firstParent.equals(fResources[i].getParent())) | |
136 | return false; | |
137 | } | |
138 | return true; | |
139 | } | |
140 | ||
141 | /** | |
142 | * Checks whether the jar resources have the same parent. | |
143 | * | |
144 | * @return <code>true</code> if the jar resources have the same parent, <code>false</code> | |
145 | * otherwise | |
146 | * @since 3.6 | |
147 | */ | |
148 | private boolean jarResourcesHaveCommonParent() { | |
149 | if (fJarResources.length == 0) | |
150 | return true; | |
151 | Object firstParent= fJarResources[0].getParent(); | |
152 | Assert.isNotNull(firstParent); | |
153 | for (int i= 1; i < fJarResources.length; i++) { | |
154 | if (! firstParent.equals(fJarResources[i].getParent())) | |
155 | return false; | |
156 | } | |
157 | return true; | |
158 | } | |
159 | ||
160 | public IResource[] getResources(){ | |
161 | return fResources; | |
162 | } | |
163 | ||
164 | public IJavaElement[] getJavaElements(){ | |
165 | return fJavaElements; | |
166 | } | |
167 | ||
168 | public void removeElementsWithAncestorsOnList(boolean removeOnlyJavaElements) { | |
169 | if (! removeOnlyJavaElements){ | |
170 | removeResourcesDescendantsOfResources(); | |
171 | removeResourcesDescendantsOfJavaElements(); | |
172 | } | |
173 | removeJavaElementsDescendantsOfJavaElements(); | |
174 | // removeJavaElementsChildrenOfResources(); //this case is covered by removeUnconfirmedArchives | |
175 | } | |
176 | ||
177 | private void removeResourcesDescendantsOfJavaElements() { | |
178 | List<IResource> subResources= new ArrayList<IResource>(3); | |
179 | for (int i= 0; i < fResources.length; i++) { | |
180 | IResource subResource= fResources[i]; | |
181 | for (int j= 0; j < fJavaElements.length; j++) { | |
182 | IJavaElement superElements= fJavaElements[j]; | |
183 | if (isDescendantOf(subResource, superElements)) | |
184 | subResources.add(subResource); | |
185 | } | |
186 | } | |
187 | removeFromSetToDelete(subResources.toArray(new IResource[subResources.size()])); | |
188 | } | |
189 | ||
190 | private void removeJavaElementsDescendantsOfJavaElements() { | |
191 | List<IJavaElement> subElements= new ArrayList<IJavaElement>(3); | |
192 | for (int i= 0; i < fJavaElements.length; i++) { | |
193 | IJavaElement subElement= fJavaElements[i]; | |
194 | for (int j= 0; j < fJavaElements.length; j++) { | |
195 | IJavaElement superElement= fJavaElements[j]; | |
196 | if (isDescendantOf(subElement, superElement)) | |
197 | subElements.add(subElement); | |
198 | } | |
199 | } | |
200 | removeFromSetToDelete(subElements.toArray(new IJavaElement[subElements.size()])); | |
201 | } | |
202 | ||
203 | private void removeResourcesDescendantsOfResources() { | |
204 | List<IResource> subResources= new ArrayList<IResource>(3); | |
205 | for (int i= 0; i < fResources.length; i++) { | |
206 | IResource subResource= fResources[i]; | |
207 | for (int j= 0; j < fResources.length; j++) { | |
208 | IResource superResource= fResources[j]; | |
209 | if (isDescendantOf(subResource, superResource)) | |
210 | subResources.add(subResource); | |
211 | } | |
212 | } | |
213 | removeFromSetToDelete(subResources.toArray(new IResource[subResources.size()])); | |
214 | } | |
215 | ||
216 | public static boolean isDescendantOf(IResource subResource, IJavaElement superElement) { | |
217 | IResource parent= subResource.getParent(); | |
218 | while(parent != null){ | |
219 | IJavaElement el= JavaCore.create(parent); | |
220 | if (el != null && el.exists() && el.equals(superElement)) | |
221 | return true; | |
222 | parent= parent.getParent(); | |
223 | } | |
224 | return false; | |
225 | } | |
226 | ||
227 | public static boolean isDescendantOf(IJavaElement subElement, IJavaElement superElement) { | |
228 | if (subElement.equals(superElement)) | |
229 | return false; | |
230 | IJavaElement parent= subElement.getParent(); | |
231 | while(parent != null){ | |
232 | if (parent.equals(superElement)) | |
233 | return true; | |
234 | parent= parent.getParent(); | |
235 | } | |
236 | return false; | |
237 | } | |
238 | ||
239 | public static boolean isDescendantOf(IResource subResource, IResource superResource) { | |
240 | return ! subResource.equals(superResource) && superResource.getFullPath().isPrefixOf(subResource.getFullPath()); | |
241 | } | |
242 | ||
243 | private void removeFromSetToDelete(IResource[] resourcesToNotDelete) { | |
244 | fResources= ReorgUtils.setMinus(fResources, resourcesToNotDelete); | |
245 | } | |
246 | ||
247 | private void removeFromSetToDelete(IJavaElement[] elementsToNotDelete) { | |
248 | fJavaElements= ReorgUtils.setMinus(fJavaElements, elementsToNotDelete); | |
249 | } | |
250 | } |