KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > search > HyperSearchRequest


1 /*
2  * HyperSearchRequest.java - HyperSearch request, run in I/O thread
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 1998, 1999, 2000, 2001, 2002 Slava Pestov
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */

22
23 package org.gjt.sp.jedit.search;
24
25 //{{{ Imports
26
import javax.swing.text.Segment JavaDoc;
27 import javax.swing.tree.*;
28 import javax.swing.SwingUtilities JavaDoc;
29 import org.gjt.sp.jedit.textarea.Selection;
30 import org.gjt.sp.jedit.io.VFSManager;
31 import org.gjt.sp.jedit.Buffer;
32 import org.gjt.sp.jedit.GUIUtilities;
33 import org.gjt.sp.jedit.jEdit;
34 import org.gjt.sp.jedit.View;
35 import org.gjt.sp.util.*;
36 //}}}
37

38 /**
39  * HyperSearch results window.
40  * @author Slava Pestov
41  * @version $Id: HyperSearchRequest.java 8219 2006-12-10 22:58:04Z kpouer $
42  */

43 class HyperSearchRequest extends WorkRequest
44 {
45     //{{{ HyperSearchRequest constructor
46
HyperSearchRequest(View view, SearchMatcher matcher,
47         HyperSearchResults results, Selection[] selection)
48     {
49         this.view = view;
50         this.matcher = matcher;
51
52         this.results = results;
53         searchString = SearchAndReplace.getSearchString();
54         rootSearchNode = new DefaultMutableTreeNode(new HyperSearchOperationNode(searchString));
55
56         this.selection = selection;
57     } //}}}
58

59     //{{{ run() method
60
public void run()
61     {
62         setStatus(jEdit.getProperty("hypersearch-status"));
63
64         SearchFileSet fileset = SearchAndReplace.getSearchFileSet();
65         String JavaDoc[] files = fileset.getFiles(view);
66         if(files == null || files.length == 0)
67         {
68             SwingUtilities.invokeLater(new Runnable JavaDoc()
69             {
70                 public void run()
71                 {
72                     GUIUtilities.error(view,"empty-fileset",null);
73                     results.searchDone(rootSearchNode);
74                 }
75             });
76             return;
77         }
78
79         setMaximum(fileset.getFileCount(view));
80
81         // to minimise synchronization and stuff like that, we only
82
// show a status message at most twice a second
83

84         // initially zero, so that we always show the first message
85
String JavaDoc searchingCaption = jEdit.getProperty("hypersearch-results.searching") + ' ';
86         try
87         {
88             if(selection != null)
89             {
90                 Buffer buffer = view.getBuffer();
91
92                 searchInSelection(buffer);
93             }
94             else
95             {
96                 int current = 0;
97
98                 long lastStatusTime = 0;
99 loop: for(int i = 0; i < files.length; i++)
100                 {
101                     String JavaDoc file = files[i];
102                     current++;
103
104                     long currentTime = System.currentTimeMillis();
105                     if(currentTime - lastStatusTime > 250)
106                     {
107                         setValue(current);
108                         lastStatusTime = currentTime;
109                         results.setSearchStatus(searchingCaption + file);
110                     }
111
112                     Buffer buffer = jEdit.openTemporary(null,null,file,false);
113                     if(buffer == null)
114                         continue loop;
115
116                     doHyperSearch(buffer);
117                 }
118             }
119         }
120         catch(final Exception JavaDoc e)
121         {
122             Log.log(Log.ERROR,this,e);
123             SwingUtilities.invokeLater(new Runnable JavaDoc()
124             {
125                 public void run()
126                 {
127                     SearchAndReplace.handleError(view,e);
128                 }
129             });
130         }
131         catch(WorkThread.Abort a)
132         {
133         }
134         finally
135         {
136             VFSManager.runInAWTThread(new Runnable JavaDoc()
137             {
138                 public void run()
139                 {
140                     results.searchDone(rootSearchNode);
141                 }
142             });
143         }
144     } //}}}
145

146     //{{{ Private members
147

148     //{{{ Instance variables
149
private View view;
150     private SearchMatcher matcher;
151     private HyperSearchResults results;
152     private DefaultMutableTreeNode rootSearchNode;
153     private Selection[] selection;
154     private String JavaDoc searchString;
155     //}}}
156

157     //{{{ searchInSelection() method
158
private int searchInSelection(Buffer buffer) throws Exception JavaDoc
159     {
160         setAbortable(false);
161
162         int resultCount = 0;
163
164         try
165         {
166             buffer.readLock();
167
168             for(int i = 0; i < selection.length; i++)
169             {
170                 Selection s = selection[i];
171                 if(s instanceof Selection.Rect)
172                 {
173                     for(int j = s.getStartLine();
174                         j <= s.getEndLine(); j++)
175                     {
176                         resultCount += doHyperSearch(buffer,
177                             s.getStart(buffer,j),
178                             s.getEnd(buffer,j));
179                     }
180                 }
181                 else
182                 {
183                     resultCount += doHyperSearch(buffer,
184                         s.getStart(),s.getEnd());
185                 }
186             }
187         }
188         finally
189         {
190             buffer.readUnlock();
191         }
192
193         setAbortable(true);
194
195         return resultCount;
196     } //}}}
197

198     //{{{ doHyperSearch() method
199
private int doHyperSearch(Buffer buffer)
200         throws Exception JavaDoc
201     {
202         return doHyperSearch(buffer, 0, buffer.getLength());
203     } //}}}
204

205     //{{{ doHyperSearch() method
206
private int doHyperSearch(Buffer buffer, int start, int end)
207         throws Exception JavaDoc
208     {
209         setAbortable(false);
210
211         HyperSearchFileNode hyperSearchFileNode = new HyperSearchFileNode(buffer.getPath());
212         DefaultMutableTreeNode bufferNode = new DefaultMutableTreeNode(hyperSearchFileNode);
213
214         int resultCount = doHyperSearch(buffer,start,end,bufferNode);
215         hyperSearchFileNode.setCount(resultCount);
216         if(resultCount != 0)
217             rootSearchNode.insert(bufferNode,rootSearchNode.getChildCount());
218
219         setAbortable(true);
220
221         return resultCount;
222     } //}}}
223

224     //{{{ doHyperSearch() method
225
private int doHyperSearch(Buffer buffer, int start, int end,
226         DefaultMutableTreeNode bufferNode)
227     {
228         int resultCount = 0;
229
230         try
231         {
232             buffer.readLock();
233
234             boolean endOfLine = buffer.getLineEndOffset(
235                 buffer.getLineOfOffset(end)) - 1 == end;
236
237             Segment JavaDoc text = new Segment JavaDoc();
238             int offset = start;
239
240             HyperSearchResult lastResult = null;
241
242 loop: for(int counter = 0; ; counter++)
243             {
244                 boolean startOfLine = buffer.getLineStartOffset(
245                     buffer.getLineOfOffset(offset)) == offset;
246
247                 buffer.getText(offset,end - offset,text);
248                 SearchMatcher.Match match = matcher.nextMatch(
249                     new SegmentCharSequence(text,false),
250                     startOfLine,endOfLine,counter == 0,
251                     false);
252                 if(match == null)
253                     break loop;
254
255                 int newLine = buffer.getLineOfOffset(
256                     offset + match.start);
257                 if(lastResult == null || lastResult.line != newLine)
258                 {
259                     lastResult = new HyperSearchResult(
260                         buffer,newLine);
261                     bufferNode.add(
262                         new DefaultMutableTreeNode(
263                         lastResult,false));
264                 }
265
266                 lastResult.addOccur(offset + match.start,
267                     offset + match.end);
268
269                 offset += match.end;
270                 if (matcher.isMatchingEOL())
271                 {
272                     if (offset < buffer.getLength())
273                         offset += 1;
274                     else
275                         break loop;
276                 }
277
278                 resultCount++;
279             }
280         }
281         finally
282         {
283             buffer.readUnlock();
284         }
285
286         return resultCount;
287     } //}}}
288

289     //}}}
290
}
291
Popular Tags