KickJava   Java API By Example, From Geeks To Geeks.

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


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.Comparator JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.HashSet JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.Set JavaDoc;
21
22 import org.eclipse.core.runtime.Assert;
23
24 import org.eclipse.core.resources.IFile;
25
26 import org.eclipse.jdt.core.IClassFile;
27 import org.eclipse.jdt.core.ICompilationUnit;
28 import org.eclipse.jdt.core.IJavaElement;
29 import org.eclipse.jdt.core.ISourceReference;
30 import org.eclipse.jdt.core.JavaModelException;
31
32
33 public class SourceReferenceUtil {
34     
35     //no instances
36
private SourceReferenceUtil(){}
37     
38     public static IFile getFile(ISourceReference ref) {
39         ICompilationUnit unit= getCompilationUnit(ref);
40         return (IFile) unit.getPrimary().getResource();
41     }
42     
43     public static ICompilationUnit getCompilationUnit(ISourceReference o){
44         Assert.isTrue(! (o instanceof IClassFile));
45         
46         if (o instanceof ICompilationUnit)
47             return (ICompilationUnit)o;
48         if (o instanceof IJavaElement)
49             return (ICompilationUnit) ((IJavaElement)o).getAncestor(IJavaElement.COMPILATION_UNIT);
50         return null;
51     }
52     
53     private static boolean hasParentInSet(IJavaElement elem, Set JavaDoc set){
54         IJavaElement parent= elem.getParent();
55         while (parent != null) {
56             if (set.contains(parent))
57                 return true;
58             parent= parent.getParent();
59         }
60         return false;
61     }
62     
63     public static ISourceReference[] removeAllWithParentsSelected(ISourceReference[] elems){
64         Set JavaDoc set= new HashSet JavaDoc(Arrays.asList(elems));
65         List JavaDoc result= new ArrayList JavaDoc(elems.length);
66         for (int i= 0; i < elems.length; i++) {
67             ISourceReference elem= elems[i];
68             if (! (elem instanceof IJavaElement))
69                 result.add(elem);
70             else{
71                 if (! hasParentInSet(((IJavaElement)elem), set))
72                     result.add(elem);
73             }
74         }
75         return (ISourceReference[]) result.toArray(new ISourceReference[result.size()]);
76     }
77     
78     /**
79      * @return IFile -> List of ISourceReference (elements from that file)
80      */

81     public static Map JavaDoc groupByFile(ISourceReference[] elems) {
82         Map JavaDoc map= new HashMap JavaDoc();
83         for (int i= 0; i < elems.length; i++) {
84             ISourceReference elem= elems[i];
85             IFile file= SourceReferenceUtil.getFile(elem);
86             if (! map.containsKey(file))
87                 map.put(file, new ArrayList JavaDoc());
88             ((List JavaDoc)map.get(file)).add(elem);
89         }
90         return map;
91     }
92     
93     public static ISourceReference[] sortByOffset(ISourceReference[] methods){
94         Arrays.sort(methods, new Comparator JavaDoc(){
95             public int compare(Object JavaDoc o1, Object JavaDoc o2){
96                 try{
97                     return ((ISourceReference)o2).getSourceRange().getOffset() - ((ISourceReference)o1).getSourceRange().getOffset();
98                 } catch (JavaModelException e){
99                     return o2.hashCode() - o1.hashCode();
100                 }
101             }
102         });
103         return methods;
104     }
105 }
106
107
Popular Tags