1 19 20 package org.netbeans.api.java.source.transform; 21 22 import org.netbeans.api.java.source.transform.UndoList; 23 import org.netbeans.api.java.source.transform.UndoList; 24 import org.netbeans.api.java.source.query.QueryException; 25 import org.netbeans.api.java.source.query.SearchEntry; 26 import org.netbeans.api.java.source.query.SearchResult; 27 import org.netbeans.api.java.source.query.QueryResult; 28 import org.netbeans.api.java.source.query.SourceSelection; 29 import com.sun.source.tree.CompilationUnitTree; 30 import com.sun.source.tree.Tree; 31 import javax.lang.model.element.Element; 32 import java.io.IOException ; 33 import org.netbeans.modules.java.source.engine.DefaultSourceSelection; 34 35 39 public class TransformResult extends SearchResult { 40 ChangeSet changeSet; 41 42 43 public TransformResult(Transformer transformer, String title, ChangeSet changeSet) { 44 super(transformer, title); 45 this.changeSet = changeSet; 46 } 47 48 @Override 49 public QueryResult getResult(int row) { 50 SearchEntry se = getResults()[row]; 51 if (changeSet.hasOriginal(se.tree)) 52 return new TransformerResultImpl(se); 53 else 54 return super.getResult(row); 55 } 56 57 public ChangeSet getChangeSet() { 58 return changeSet; 59 } 60 61 protected SourceSelection makeSelection(Element sym, Tree tree, Tree root, final boolean original) { 62 final CompilationUnitTree topL = getTopLevel(sym, tree, root); 63 int start = model.getStartPos(tree); 64 int end = model.getEndPos(tree, topL); 65 String path = topL.getSourceFile().toUri().getPath(); 66 return new DefaultSourceSelection(sym, start, model.getPos(tree), end, path) { 67 public String getSource() { 68 return original ? fromSource(topL) : toSource(topL); 69 } 70 }; 71 } 72 73 @Override 74 protected CompilationUnitTree getTopLevel(Element sym, Tree tree, Tree root) { 75 CompilationUnitTree tl = super.getTopLevel(sym, tree, root); 76 if (tl == null) { 77 tree = env.getTrees().getTree(env.getElementUtilities().outermostTypeElement(sym)); 79 tl = super.getTopLevel(sym, tree, root); 80 } 81 return tl; 82 } 83 84 protected String toSource(CompilationUnitTree tl) { 85 try { 86 return env.toSource(tl); 87 } catch (IOException e) { 88 throw new QueryException(e); 90 } 91 } 92 93 private class TransformerResultImpl implements TransformerResult { 94 private SearchEntry se; 95 TransformerResultImpl(SearchEntry se) { 96 this.se = se; 97 } 98 public SourceSelection getSourceSelection() { 99 Tree root = env.getRootNode(); 100 return makeSelection(se.sym, se.tree, root, false); 101 } 102 public String getNote() { 103 return se.getNote(); 104 } 105 public SourceSelection getOriginalSourceSelection() { 106 UndoList undo = env.getUndoList(); 107 Tree root = env.getRootNode(); 108 Tree oldRoot = (Tree)undo.getOld(root); 109 assert oldRoot != null; 110 Tree searchTree = se.tree; 111 Tree oldTree = changeSet.hasOriginal(searchTree) ? 112 changeSet.getOriginal(searchTree) : searchTree; 113 return makeSelection(se.sym, oldTree, oldRoot, true); 114 } 115 public boolean equals(Object o) { 116 if (this == o) 117 return true; 118 return (o instanceof TransformerResultImpl) ? 119 (se.equals(((TransformerResultImpl)o).se)) : false; 120 } 121 public int hashCode() { 122 return se.hashCode(); 123 } 124 125 public Change getChanges() { 126 assert se.tree != null; 127 return changeSet.getResultChanges(se); 128 } 129 130 public String getRefactoringDescription() { 131 return ((Transformer)se.query).getRefactoringDescription(); 132 } 133 134 public String getQueryDescription() { 135 return se.query.getQueryDescription(); 136 } 137 } 138 } 139 | Popular Tags |