1 19 20 package org.netbeans.api.java.source.query; 21 22 import org.netbeans.modules.java.source.engine.EngineEnvironment; 23 import org.netbeans.modules.java.source.engine.TreeFinder; 24 import com.sun.source.tree.ClassTree; 25 import org.netbeans.modules.java.source.engine.EngineEnvironment; 26 import org.netbeans.modules.java.source.engine.ASTModel; 27 import org.netbeans.modules.java.source.engine.TreeFinder; 28 import org.netbeans.modules.java.source.save.SourceBuffer; 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 java.util.*; 34 import javax.swing.ListSelectionModel ; 35 import org.netbeans.modules.java.source.engine.DefaultSourceSelection; 36 37 45 public class SearchResult extends QueryResultTableModel { 46 protected QueryEnvironment env; 47 protected ASTModel model; 48 private SearchEntry[] results; 49 protected List<SearchEntry> sorted = new ArrayList<SearchEntry>(); 50 private boolean changed; 51 private void changed() { results=null; changed = true; } 52 53 public SearchResult(Query query, String t) { 54 super(query, t); 55 } 56 57 public void attach(QueryEnvironment env) { 58 this.env = env; 59 this.model = ((EngineEnvironment)env).getModel(); 60 } 61 62 public void release() { 63 env = null; 64 model = null; 65 } 66 67 public SearchEntry[] getResults() { 68 if (results == null) { 69 Collections.sort(sorted); 70 results = (SearchEntry[]) sorted.toArray(new SearchEntry[sorted.size()]); 71 } 72 return results; 73 } 74 75 public final boolean add(SearchEntry e) { 76 sorted.add(e); 77 changed(); 78 return true; 79 } 80 81 public String asLogMessage(SearchEntry se) { 82 StringBuffer sb = new StringBuffer (); 83 sb.append(se.className); 84 if (se.methName != null) { 85 sb.append('.'); sb.append(se.methName); 87 } 88 if (se.note != null) { 89 sb.append(" \""); sb.append(se.note); 91 sb.append('\"'); } 93 return sb.toString(); 94 } 95 96 public final void clear() { 97 sorted.clear(); 98 changed(); 99 } 100 101 public Iterator<SearchEntry> iterator() { 102 return new Iterator<SearchEntry>() { 103 private int pos = 0; 104 private boolean deleted = false; 105 { getResults(); } 106 public boolean hasNext() { 107 if(pos<results.length) return true; 108 if(deleted) results = null; 109 return false; 110 } 111 public SearchEntry next() { return results[pos++]; } 112 public void remove() { deleted = true; sorted.remove(results[pos-1]); } 113 }; 114 } 115 116 public int getFlagsOr() { 117 int flags = 0; 118 SearchEntry[] r = getResults(); 119 for(int i = r.length; --i>=0; ) 120 flags |= r[i].flags; 121 return flags; 122 } 123 124 public Element getSymbol(int row) { 125 if(row<0) return null; 126 SearchEntry[] results = getResults(); 127 if(results==null || row>=results.length) return null; 128 return results[row].sym; 129 } 130 131 public int getFlagsMask() { 132 int mask = 0; 133 SearchEntry[] r = getResults(); 134 for(int i = r.length; --i>=0; ) 135 mask |= 1<<r[i].flags; 136 return mask; 137 } 138 139 public final void done() { 140 if (changed) { 141 changed = false; 142 results = null; 143 fireTableStructureChanged(); 144 } 145 } 146 147 public final int getRowCount() { 148 return getResults().length; 149 } 150 151 public final int getResultCount() { 152 return getRowCount(); 153 } 154 155 public QueryResult getResult(ListSelectionModel lsm) { 156 int selectedRow = lsm.getMinSelectionIndex(); 157 return selectedRow < getRowCount() ? getResult(selectedRow) : null; 158 } 159 160 public QueryResult getResult(int row) { 161 final SearchEntry se = getResults()[row]; 162 return new QueryResult() { 163 public String getQueryDescription() { 164 return se.query.getQueryDescription(); 165 } 166 public SourceSelection getSourceSelection() { 167 return makeSelection(se.getElement(), se.tree, env.getRootNode()); 168 } 169 public String getNote() { 170 return se.getNote(); 171 } 172 }; 173 } 174 175 protected SourceSelection makeSelection(Element sym, Tree tree, Tree root) { 176 if (tree == null) 177 tree = env.getTrees().getTree(sym); 178 final CompilationUnitTree topL = getTopLevel(sym, tree, root); 179 int start = model.getStartPos(tree); 180 int end = model.getEndPos(tree, topL); 181 String path = topL.getSourceFile().toUri().getPath(); 182 return new DefaultSourceSelection(sym, start, model.getPos(tree), end, path) { 183 public String getSource() { 184 return fromSource(topL); 185 } 186 }; 187 } 188 189 protected String fromSource(CompilationUnitTree tl) { 190 String srcFile = tl.getSourceFile().toUri().getPath(); 191 try { 192 SourceBuffer src = new SourceBuffer(srcFile); 193 return src.getString(0, src.length()); 194 } catch (IOException e) { 195 assert false : "source file not found: " + srcFile; 197 return ""; 198 } 199 } 200 201 protected CompilationUnitTree getTopLevel(Element sym, Tree tree, Tree root) { 202 final Element outerClass = env.getElementUtilities().outermostTypeElement(sym); 203 if (outerClass == null) return model.getTopLevel(tree); final Element pkg = outerClass.getEnclosingElement(); 206 assert pkg != null; 207 final CompilationUnitTree[] rtn = new CompilationUnitTree[1]; 208 root.accept(new TreeFinder(tree) { 209 @Override 210 public Boolean visitCompilationUnit(CompilationUnitTree tree, Object o) { 211 if (!found && scan(tree.getTypeDecls(), o)) 212 rtn[0] = tree; 213 return found; 214 } 215 @Override 216 public Boolean visitClass(ClassTree tree, Object o) { 217 if (!found && model.getElement(tree).equals(outerClass)) 218 found = true; 219 return found; 220 } 221 }, null); 222 assert rtn[0] != null; 223 return rtn[0]; 224 } 225 226 227 boolean hasmeth = false; 228 boolean hasnote = false; 229 boolean hassym = false; 230 private int ncols = -1; 231 private String [] columnNames; 232 233 public int getColumnCount() { 234 if (ncols == -1) { 235 SearchEntry[] r = getResults(); 236 for(int i = r.length; --i>=0; ) { 237 if(r[i].methName != null) 238 hasmeth = true; 239 if(r[i].note != null) 240 hasnote = true; 241 else if(r[i].symName != null) 242 hassym = true; 243 } 244 ncols = 1; 245 if(hasmeth) ncols++; 246 if(hasnote) ncols++; 247 if(hassym) ncols++; 248 } 249 return ncols; 250 } 251 252 public String getColumnName(int column) { 253 if (columnNames == null) { 254 columnNames = new String [ncols]; 255 columnNames[0] = "Class"; 256 int i = 1; 257 if (hasmeth) 258 columnNames[i++] = "Member"; 259 if (hassym) 260 columnNames[i++] = "Symbol"; 261 if (hasnote) 262 columnNames[i] = "Notes"; 263 } 264 assert column < columnNames.length : "column=" + Integer.toString(column); 265 return columnNames[column]; 266 } 267 268 public Object getValueAt(int row, int column) { 269 SearchEntry e = getResults()[row]; 270 switch (column) { 271 case 0: 272 return e.className; 273 case 1: 274 if(hasmeth) return e.methName; 275 else if(hassym) return e.getSymName(); 276 else if(hasnote) return e.getNote(); 277 break; 278 case 2: 279 if(hassym) return e.getSymName(); 280 else if(hasnote) return e.getNote(); 281 break; 282 case 3: 283 return e.getNote(); 284 } 285 return "Bug!!"; 286 } 287 288 public String toString() { 289 int cols = getColumnCount(); 290 int rows = getRowCount(); 291 StringBuffer sb = new StringBuffer (); 292 for (int i = 0; i < rows; i++) { 293 for (int j = 0; j < cols; j++) { 294 sb.append(getColumnName(j)); 295 sb.append('='); 296 sb.append(getValueAt(i, j)); 297 if (j+1 < cols) 298 sb.append(", "); 299 } 300 if (i+1 < rows) 301 sb.append('\n'); 302 } 303 return sb.toString(); 304 } 305 306 308 public int size() { return getResults().length; } 309 public boolean isEmpty() { return size()==0; } 310 public boolean contains(Object e) { 311 return sorted.contains(e); 312 } 313 public Object [] toArray() { return getResults(); } 314 323 public <E> boolean containsAll(Collection<E> c) { return sorted.containsAll(c); } 324 public boolean remove(Object e) { changed(); return sorted.remove(e); } 325 public <E> boolean removeAll(Collection<E> c) { changed(); return sorted.removeAll(c); } 326 public <E> boolean retainAll(Collection<E> c) { changed(); return sorted.retainAll(c); } 327 public <E extends SearchEntry> boolean addAll(Collection<E> c) { 328 for(Iterator<E> i = c.iterator(); i.hasNext(); ) 329 add(i.next()); 330 return true; 331 } 332 333 interface SourceMaker { 334 String getSource(); 335 } 336 337 static final long serialVersionUID = -4851462993177602438L; 338 } 339 | Popular Tags |