1 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 ; 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 displayText; 42 private FileObject parentFile; 43 public WhereUsedElement(PositionBounds bounds, String 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 getDisplayText() { 51 return displayText; 52 } 53 54 public Object getComposite() { 55 Object 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 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 content = null; 79 try { 80 content = unit.getSourceFile().getCharContent(true); 81 } catch (IOException 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 ("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 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 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 sb = new StringBuffer (); 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 |