KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javadoc > search > IndexSearchThread


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.modules.javadoc.search;
21
22 import java.util.StringTokenizer JavaDoc;
23
24 import org.openide.util.RequestProcessor;
25 import org.openide.filesystems.FileObject;
26
27 /** Abstract class for thread which searches for documentation
28  *
29  * @author Petr Hrebejk, Petr Suchomel
30  */

31
32 public abstract class IndexSearchThread implements Runnable JavaDoc {
33
34     // PENDING: Add some abstract methods
35

36     //protected String toFind;
37

38     // documentation index file (or foldee for splitted index)
39
protected FileObject indexRoot;
40     private DocIndexItemConsumer ddiConsumer;
41     RequestProcessor.Task rpTask = null;
42     protected boolean caseSensitive;
43     
44     protected String JavaDoc lastField=""; //NOI18N
45
protected String JavaDoc middleField=""; //NOI18N
46
protected String JavaDoc reminder=""; //NOI18N
47
private int tokens=0;
48
49     private String JavaDoc lastAdd =""; //NOI18N
50
private String JavaDoc lastDeclaring=""; //NOI18N
51
/** This method must terminate the process of searching */
52     abstract void stopSearch();
53
54     public IndexSearchThread( String JavaDoc toFind, FileObject fo, DocIndexItemConsumer ddiConsumer, boolean caseSensitive ) {
55         this.ddiConsumer = ddiConsumer;
56         this.indexRoot = fo;
57         this.caseSensitive = caseSensitive;
58         
59         //this.toFind = toFind;
60
//rpTask = RequestProcessor.createRequest( this );
61

62         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(toFind, "."); //NOI18N
63
tokens = st.countTokens();
64         //System.out.println(tokens);
65

66         if( tokens > 1 ){
67             if( tokens == 2 ){
68                 middleField = st.nextToken();
69                 lastField = st.nextToken();
70             }
71             else{
72                 for( int i = 0; i < tokens-2; i++){
73                     reminder += st.nextToken();
74                     if( i+1 < tokens-2 )
75                         reminder += '.';
76                 }
77                 middleField = st.nextToken();
78                 lastField = st.nextToken();
79             }
80         }
81         else{
82             lastField = toFind;
83         }
84         if( !caseSensitive ){
85             reminder = reminder.toUpperCase();
86             middleField = middleField.toUpperCase();
87             lastField = lastField.toUpperCase();
88         }
89         //System.out.println("lastField" + lastField);
90
}
91
92     protected synchronized void insertDocIndexItem( DocIndexItem dii ) {
93         //no '.', can add directly
94
//System.out.println("Inserting");
95
/*
96         try{
97             PrintWriter pw = new PrintWriter( new FileWriter( "c:/javadoc.dump", true ));
98             pw.println("\"" + dii.getField() +"\""+ " " + "\""+dii.getDeclaringClass()+ "\"" + " " + "\""+ dii.getPackage()+ "\"");
99             pw.println("\"" + lastField + "\"" + " " + "\"" + middleField + "\"" + " " + "\"" + reminder + "\"");
100             pw.flush();
101             pw.close();
102         }
103         catch(IOException ioEx){ioEx.printStackTrace();}
104         */

105         String JavaDoc diiField = dii.getField();
106         String JavaDoc diiDeclaringClass = dii.getDeclaringClass();
107         String JavaDoc diiPackage = dii.getPackage();
108         if( !caseSensitive ){
109             diiField = diiField.toUpperCase();
110             diiDeclaringClass = diiDeclaringClass.toUpperCase();
111             diiPackage = diiPackage.toUpperCase();
112         }
113         
114         if( tokens < 2 ){
115             if( diiField.startsWith( lastField ) ){
116                 //System.out.println("------");
117
//System.out.println("Field: " + diiField + " last field: " + lastAdd + " declaring " + diiDeclaringClass + " package " + diiPackage);
118
if( !lastAdd.equals( diiField ) || !lastDeclaring.equals( diiDeclaringClass )){
119                     //System.out.println("ADDED");
120
ddiConsumer.addDocIndexItem ( dii );
121                     lastAdd = diiField;
122                     lastDeclaring = diiDeclaringClass;
123                 }
124                 //System.out.println("------");
125
}
126             else if( diiDeclaringClass.startsWith( lastField ) && dii.getIconIndex() == DocSearchIcons.ICON_CLASS ) {
127                 if( !lastAdd.equals( diiDeclaringClass ) ){
128                     ddiConsumer.addDocIndexItem ( dii );//System.out.println("Declaring class " + diiDeclaringClass + " icon " + dii.getIconIndex() + " remark " + dii.getRemark());
129
lastAdd = diiDeclaringClass;
130                 }
131             }
132             else if( diiPackage.startsWith( lastField + '.' ) && dii.getIconIndex() == DocSearchIcons.ICON_PACKAGE ) {
133                 if( !lastAdd.equals( diiPackage ) ){
134                     ddiConsumer.addDocIndexItem ( dii );//System.out.println("Package " + diiPackage + " icon " + dii.getIconIndex() + " remark " + dii.getRemark());
135
lastAdd = diiPackage;
136                 }
137             }
138         }
139         else{
140             if( tokens == 2 ){
141                 //class and field (method etc. are equals)
142
//System.out.println(dii.getField() + " " + lastField + " " + dii.getDeclaringClass() + " " + middleField);
143
if( diiField.startsWith(lastField) && diiDeclaringClass.equals(middleField) ){
144                     ddiConsumer.addDocIndexItem ( dii );
145                 }
146                 else if( diiPackage.startsWith( middleField ) && diiDeclaringClass.equals( lastField ) ){
147                     ddiConsumer.addDocIndexItem ( dii );
148                 }
149                 else if( diiPackage.startsWith( (middleField + '.' + lastField) ) && dii.getIconIndex() == DocSearchIcons.ICON_PACKAGE ){
150                     ddiConsumer.addDocIndexItem ( dii );
151                 }
152             }
153             else{
154                 //class and field (method etc. are equals)
155
if( diiField.startsWith(lastField) && diiDeclaringClass.equals(middleField) && diiPackage.startsWith( reminder ) ){
156                     ddiConsumer.addDocIndexItem ( dii );
157                 }
158                 //else if( diiDeclaringClass.equals(lastField) && diiPackage.startsWith( (reminder + '.' + middleField).toUpperCase()) ){
159
else if( diiDeclaringClass.startsWith(lastField) && diiPackage.equals( (reminder + '.' + middleField + '.')) ){
160                     ddiConsumer.addDocIndexItem ( dii );
161                 }
162                 else if( diiPackage.startsWith( (reminder + '.' + middleField + '.' + lastField) ) && dii.getIconIndex() == DocSearchIcons.ICON_PACKAGE ){
163                     ddiConsumer.addDocIndexItem ( dii );
164                 }
165             }
166         }
167     }
168
169     public void go() {
170         rpTask = RequestProcessor.getDefault().post( this, 0, Thread.NORM_PRIORITY );
171     }
172
173     public void finish() {
174         if ( !rpTask.isFinished() && !rpTask.cancel() )
175             stopSearch();
176         taskFinished();
177     }
178
179     public void taskFinished() {
180         ddiConsumer.indexSearchThreadFinished( this );
181     }
182
183     /** Class for callback. Used to feed some container with found
184      * index items;
185      */

186
187     public static interface DocIndexItemConsumer {
188
189         /** Called when an item is found */
190         public void addDocIndexItem ( DocIndexItem dii );
191
192         /** Called when a task finished. May be called more than once */
193         public void indexSearchThreadFinished( IndexSearchThread ist );
194
195
196     }
197
198 }
199
Popular Tags