KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > java > source > query > SearchResult


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.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 JavaDoc;
33 import java.util.*;
34 import javax.swing.ListSelectionModel JavaDoc;
35 import org.netbeans.modules.java.source.engine.DefaultSourceSelection;
36
37 /**
38  * A SearchResult is a list of results returned from a Jackpot query.
39  * It extends an AbstractTableModel so as to be self-describing and
40  * customizable for different types of results.
41  *
42  * TODO: this class needs to be made abstract, and the operator-specific
43  * code in it extracted to different subclasses.
44  */

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 JavaDoc 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 JavaDoc asLogMessage(SearchEntry se) {
82         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
83         sb.append(se.className);
84         if (se.methName != null) {
85             sb.append('.'); // NOI18N
86
sb.append(se.methName);
87         }
88         if (se.note != null) {
89             sb.append(" \""); // NOI18N
90
sb.append(se.note);
91             sb.append('\"'); // NOI18N
92
}
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 JavaDoc 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 JavaDoc getQueryDescription() {
164                 return se.query.getQueryDescription();
165             }
166             public SourceSelection getSourceSelection() {
167                 return makeSelection(se.getElement(), se.tree, env.getRootNode());
168             }
169             public String JavaDoc 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 JavaDoc path = topL.getSourceFile().toUri().getPath();
182         return new DefaultSourceSelection(sym, start, model.getPos(tree), end, path) {
183             public String JavaDoc getSource() {
184                 return fromSource(topL);
185             }
186         };
187     }
188     
189     protected String JavaDoc fromSource(CompilationUnitTree tl) {
190         String JavaDoc srcFile = tl.getSourceFile().toUri().getPath();
191         try {
192             SourceBuffer src = new SourceBuffer(srcFile);
193             return src.getString(0, src.length());
194         } catch (IOException JavaDoc e) {
195             // source file disappeared since Jackpot read it.
196
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) // true for package and import statements
204
return model.getTopLevel(tree); // slower lookup
205
final Element pkg = outerClass.getEnclosingElement();
206         assert pkg != null;
207         final CompilationUnitTree[] rtn = new CompilationUnitTree[1];
208         root.accept(new TreeFinder(tree) {
209             @Override JavaDoc
210             public Boolean JavaDoc visitCompilationUnit(CompilationUnitTree tree, Object JavaDoc o) {
211                 if (!found && scan(tree.getTypeDecls(), o))
212                     rtn[0] = tree;
213                 return found;
214             }
215             @Override JavaDoc
216             public Boolean JavaDoc visitClass(ClassTree tree, Object JavaDoc 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 JavaDoc[] 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 JavaDoc getColumnName(int column) {
253         if (columnNames == null) {
254             columnNames = new String JavaDoc[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 JavaDoc 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 JavaDoc toString() {
289         int cols = getColumnCount();
290         int rows = getRowCount();
291         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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     // Collections API
307

308     public int size() { return getResults().length; }
309     public boolean isEmpty() { return size()==0; }
310     public boolean contains(Object JavaDoc e) {
311         return sorted.contains(e);
312     }
313     public Object JavaDoc[] toArray() { return getResults(); }
314 /* public <E> E[] toArray(E[] a) {
315         SearchEntry[] r = getResults();
316         if(a==null || a.length<r.length)
317             a = (E[])java.lang.reflect.Array
318                     .newInstance(a.getClass().getComponentType(), r.length);
319         System.arraycopy(r, 0, a, 0, r.length);
320         return a;
321     }
322  */

323     public <E> boolean containsAll(Collection<E> c) { return sorted.containsAll(c); }
324     public boolean remove(Object JavaDoc 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 JavaDoc getSource();
335     }
336     
337     static final long serialVersionUID = -4851462993177602438L;
338 }
339
Popular Tags