KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.sun.source.tree.Tree;
23 import javax.lang.model.element.Element;
24 import javax.lang.model.element.PackageElement;
25 import javax.lang.model.element.TypeElement;
26
27 /**
28  * An individual entry (line) in a SearchResult.
29  */

30 public class SearchEntry implements Comparable JavaDoc<SearchEntry> {
31     public Query query;
32     public Tree tree;
33     private int pos; // used for sorting
34
public Element sym;
35     public String JavaDoc className;
36     public String JavaDoc methName;
37     public String JavaDoc symName;
38     public String JavaDoc note;
39     public int flags;
40     
41     public SearchEntry (Query q, Element e, Tree t, int pos) {
42     this(q, e, t, pos, null);
43     }
44     
45     public SearchEntry (Query q, Element e, Tree t, int pos, String JavaDoc n) {
46     this(q, e, t, pos, n, 0);
47     }
48     
49     public SearchEntry (Query q, Element e, Tree t, int pos, String JavaDoc n, int f) {
50         query = q;
51     flags = f;
52         sym = e;
53     tree = t;
54         this.pos = pos;
55     if(n != null && n.length()>0) note = n;
56     if(e==null) { className = "Unknown"; methName = "Unknown"; }
57     else if (e instanceof TypeElement)
58         className = classSymbolName(sym);
59     else {
60         while (e.getEnclosingElement() != null &&
61                    !(e.getEnclosingElement() instanceof TypeElement))
62                 e = e.getEnclosingElement();
63         className = e.getEnclosingElement() == null ? "NULL PARENT" : classSymbolName(e);
64         methName = e.getSimpleName().toString();
65         if(e!=sym) symName = sym.getSimpleName().toString();
66     }
67     }
68     
69     private String JavaDoc classSymbolName(Element e) {
70         if (e == null || e instanceof PackageElement)
71             return "";
72         if (!(e instanceof TypeElement))
73             return classSymbolName(e.getEnclosingElement());
74         String JavaDoc owner = classSymbolName(e.getEnclosingElement());
75         String JavaDoc name = e.getSimpleName().toString();
76         if (name.length() == 0)
77             name = e.asType().toString();
78         return owner.length() > 0 ? owner + '.' + name : name;
79     }
80     
81     public int compareTo(SearchEntry oe) {
82     int ret = className.compareTo(oe.className);
83     if (ret == 0)
84         if ((methName == null || oe.methName == null ||
85         (ret = methName.compareTo(oe.methName)) == 0) &&
86         tree != null)
87         ret = pos - oe.pos;
88     return ret;
89     }
90     
91     public String JavaDoc getSymName() {
92     return symName==null ? "" : symName;
93     }
94     
95     public String JavaDoc getNote() {
96     return note==null ? "" : note;
97     }
98     
99     public Element getElement() {
100             return this.sym;
101     }
102 }
103
Popular Tags