KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > refactoring > java > ui > tree > ElementGripFactory


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 JavaDoc;
25 import java.util.Set JavaDoc;
26 import java.util.WeakHashMap JavaDoc;
27 import javax.lang.model.element.Element;
28 import org.netbeans.api.java.source.CompilationInfo;
29 import org.openide.filesystems.FileObject;
30
31 /**
32  *
33  * @author Jan Becicka
34  */

35 public class ElementGripFactory {
36
37     private static ElementGripFactory instance;
38     private WeakHashMap JavaDoc <FileObject, Interval> map = new WeakHashMap JavaDoc();
39     
40     /**
41      * Creates a new instance of ElementGripFactory
42      */

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 JavaDoc 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 JavaDoc<Interval> subintervals= new HashSet JavaDoc();
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                         //xxx: rather workaround. should be fixed better.
121
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