1 19 20 package org.netbeans.modules.refactoring.java.ui.tree; 21 22 import com.sun.source.tree.Tree; 23 import com.sun.source.util.TreePath; 24 import java.util.HashSet ; 25 import java.util.Set ; 26 import java.util.WeakHashMap ; 27 import javax.lang.model.element.Element; 28 import org.netbeans.api.java.source.CompilationInfo; 29 import org.openide.filesystems.FileObject; 30 31 35 public class ElementGripFactory { 36 37 private static ElementGripFactory instance; 38 private WeakHashMap <FileObject, Interval> map = new WeakHashMap (); 39 40 43 private ElementGripFactory() { 44 } 45 46 public static ElementGripFactory getDefault() { 47 if (instance == null) { 48 instance = new ElementGripFactory(); 49 } 50 return instance; 51 } 52 53 public void cleanUp() { 54 map.clear(); 55 } 56 57 public ElementGrip get(FileObject fileObject, int position) { 58 Interval start = map.get(fileObject); 59 if (start==null) 60 return null; 61 try { 62 return (ElementGrip) start.get(position).item; 63 } catch (RuntimeException e) { 64 return start.item; 65 } 66 } 67 68 public ElementGrip getParent(ElementGrip el) { 69 Interval start = map.get(el.getFileObject()); 70 return (ElementGrip) start.getParent(el); 71 } 72 73 public void put(FileObject parentFile, TreePath tp, CompilationInfo info) { 74 Interval root = map.get(parentFile); 75 Interval i = Interval.createInterval(tp,info, root,null, parentFile); 76 if (i!=null) { 77 map.put(parentFile,i); 78 } 79 } 80 81 private static class Interval { 82 long from=-1,to=-1; 83 Set <Interval> subintervals= new HashSet (); 84 ElementGrip item = null; 85 86 Interval get(long position) { 87 if (from<=position && to >=position) { 88 for (Interval o:subintervals) { 89 Interval ob = o.get(position); 90 if (ob!=null) 91 return ob; 92 } 93 return this; 94 } 95 return null; 96 } 97 98 ElementGrip getParent(ElementGrip eh) { 99 for (Interval i:subintervals) { 100 if (i.item.equals(eh)) { 101 return this.item; 102 } else { 103 ElementGrip e = i.getParent(eh); 104 if (e!=null) { 105 return e; 106 } 107 } 108 } 109 return null; 110 } 111 112 public static Interval createInterval(TreePath tp, CompilationInfo info, Interval root, Interval p, FileObject parentFile) { 113 Tree t = tp.getLeaf(); 114 long start = info.getTrees().getSourcePositions().getStartPosition(info.getCompilationUnit(), t); 115 long end = info.getTrees().getSourcePositions().getEndPosition(info.getCompilationUnit(), t); 116 Element current = info.getTrees().getElement(tp); 117 Tree.Kind kind = tp.getLeaf().getKind(); 118 if (kind != Tree.Kind.CLASS && kind != Tree.Kind.METHOD) { 119 if (tp.getParentPath().getLeaf().getKind() == Tree.Kind.COMPILATION_UNIT) { 120 return null; 122 } else { 123 return createInterval(tp.getParentPath(), info, root, p, parentFile); 124 } 125 } 126 Interval i = null; 127 if (root != null) { 128 Interval o = root.get(start); 129 if (o!= null && o.item.resolveElement(info).equals(current)) { 130 if (p!=null) 131 o.subintervals.add(p); 132 return null; 133 } 134 } 135 if (i==null) 136 i = new Interval(); 137 if (i.from != start) { 138 i.from = start; 139 i.to = end; 140 ElementGrip currentHandle2 = new ElementGrip(tp, info); 141 i.item = currentHandle2; 142 } 143 if (p!=null) { 144 i.subintervals.add(p); 145 } 146 if (tp.getParentPath().getLeaf().getKind() == Tree.Kind.COMPILATION_UNIT) { 147 return i; 148 } 149 return createInterval(tp.getParentPath(), info, root, i, parentFile); 150 } 151 } 152 } 153 154 | Popular Tags |