KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > refactoring > java > WhereUsedElement


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 package org.netbeans.modules.refactoring.java;
20
21 import com.sun.source.tree.*;
22 import com.sun.source.util.SourcePositions;
23 import com.sun.source.util.TreePath;
24 import java.io.IOException JavaDoc;
25 import javax.swing.text.Position.Bias;
26 import org.netbeans.api.java.source.CompilationInfo;
27 import org.netbeans.modules.refactoring.java.plugins.JavaWhereUsedQueryPlugin;
28 import org.netbeans.modules.refactoring.spi.SimpleRefactoringElementImpl;
29 import org.netbeans.modules.refactoring.java.ui.tree.ElementGripFactory;
30 import org.openide.ErrorManager;
31 import org.openide.filesystems.FileObject;
32 import org.openide.text.PositionBounds;
33 import static org.netbeans.modules.refactoring.java.RetoucheUtils.*;
34 import org.openide.loaders.DataObject;
35 import org.openide.loaders.DataObjectNotFoundException;
36 import org.openide.text.CloneableEditorSupport;
37 import org.openide.text.PositionRef;
38
39 public class WhereUsedElement extends SimpleRefactoringElementImpl {
40     private PositionBounds bounds;
41     private String JavaDoc displayText;
42     private FileObject parentFile;
43     public WhereUsedElement(PositionBounds bounds, String JavaDoc displayText, FileObject parentFile, TreePath tp, CompilationInfo info) {
44         this.bounds = bounds;
45         this.displayText = displayText;
46         this.parentFile = parentFile;
47         ElementGripFactory.getDefault().put(parentFile, tp, info);
48     }
49
50     public String JavaDoc getDisplayText() {
51         return displayText;
52     }
53
54     public Object JavaDoc getComposite() {
55         Object JavaDoc composite = ElementGripFactory.getDefault().get(parentFile, bounds.getBegin().getOffset());
56         if (composite==null)
57             composite = parentFile;
58         return composite;
59     }
60
61     public PositionBounds getPosition() {
62         return bounds;
63     }
64
65     public String JavaDoc getText() {
66         return displayText;
67     }
68
69     public void performChange() {
70     }
71
72     public FileObject getParentFile() {
73         return parentFile;
74     }
75     
76     public static WhereUsedElement create(CompilationInfo compiler, TreePath tree) {
77         CompilationUnitTree unit = tree.getCompilationUnit();
78         CharSequence JavaDoc content = null;
79         try {
80             content = unit.getSourceFile().getCharContent(true);
81         } catch (IOException JavaDoc ex) {
82             ex.printStackTrace();
83         }
84         SourcePositions sp = compiler.getTrees().getSourcePositions();
85         Tree t= tree.getLeaf();
86         int start = (int)sp.getStartPosition(unit, t);
87         int end = (int)sp.getEndPosition(unit, t);
88         if (end == -1) {
89             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, new RuntimeException JavaDoc("Cannot get end position for " + t.getClass().getName()+ " " + t + " file:" + compiler.getFileObject().getPath()));
90             end=start;
91         }
92         if (t.getKind() == Tree.Kind.CLASS) {
93             //this is strange - how to get position of start of name?
94
start = start + t.toString().trim().indexOf(((ClassTree)t).getSimpleName().toString());
95             end = start + ((ClassTree)t).getSimpleName().toString().length();
96         }
97         if (t.getKind() == Tree.Kind.METHOD) {
98             start = start + t.toString().indexOf(((MethodTree)t).getName().toString())+1;
99             end = start + ((MethodTree)t).getName().toString().length();
100         }
101         if (t.getKind() == Tree.Kind.MEMBER_SELECT) {
102             //XXX: must be improved
103
start = (int)sp.getEndPosition(unit, ((MemberSelectTree) t).getExpression());
104         }
105                 
106         LineMap lm = tree.getCompilationUnit().getLineMap();
107         long line = lm.getLineNumber(start);
108         long endLine = lm.getLineNumber(end);
109         long sta = lm.getStartPosition(line);
110         long en = lm.getStartPosition(endLine+1)-1;
111         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
112         sb.append(RetoucheUtils.getHtml(content.subSequence((int)sta,(int)start).toString().trim()));
113         sb.append(" <b>");
114         sb.append(content.subSequence((int)start,(int)end));
115         sb.append("</b> ");
116         sb.append(RetoucheUtils.getHtml(content.subSequence((int)end,(int)en).toString().trim()));
117         
118         DataObject dob = null;
119         try {
120             dob = DataObject.find(compiler.getFileObject());
121         } catch (DataObjectNotFoundException ex) {
122             ex.printStackTrace();
123         }
124         CloneableEditorSupport ces = JavaWhereUsedQueryPlugin.findCloneableEditorSupport(dob);
125         PositionRef ref1 = ces.createPositionRef(start, Bias.Forward);
126         PositionRef ref2 = ces.createPositionRef(end, Bias.Forward);
127         PositionBounds bounds = new PositionBounds(ref1, ref2);
128         TreePath tr = getEnclosingTree(tree);
129         return new WhereUsedElement(bounds, sb.toString().trim(), compiler.getFileObject(), tr, compiler);
130     }
131     
132     private static TreePath getEnclosingTree(TreePath tp) {
133         while(tp != null) {
134             Tree tree = tp.getLeaf();
135             if (tree.getKind() == Tree.Kind.CLASS || tree.getKind() == Tree.Kind.METHOD || tree.getKind() == Tree.Kind.IMPORT) {
136                 return tp;
137             }
138             tp = tp.getParentPath();
139         }
140         return null;
141     }
142
143 }
144
Popular Tags