/jEdit/tags/jedit-4-0-pre5/org/gjt/sp/jedit/search/HyperSearchRequest.java
Java | 246 lines | 172 code | 40 blank | 34 comment | 21 complexity | 5c00388ac3875a31994a7084545b8b7a MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
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 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
23package org.gjt.sp.jedit.search;
24
25//{{{ Imports
26import javax.swing.text.Segment;
27import javax.swing.tree.*;
28import javax.swing.SwingUtilities;
29import org.gjt.sp.jedit.textarea.Selection;
30import org.gjt.sp.jedit.io.VFSManager;
31import org.gjt.sp.jedit.Buffer;
32import org.gjt.sp.jedit.GUIUtilities;
33import org.gjt.sp.jedit.jEdit;
34import org.gjt.sp.jedit.View;
35import org.gjt.sp.util.*;
36//}}}
37
38public class HyperSearchRequest extends WorkRequest
39{
40 //{{{ HyperSearchRequest constructor
41 public HyperSearchRequest(View view, SearchMatcher matcher,
42 HyperSearchResults results, Selection[] selection)
43 {
44 this.view = view;
45 this.matcher = matcher;
46
47 this.results = results;
48 this.resultTreeModel = results.getTreeModel();
49 this.resultTreeRoot = (DefaultMutableTreeNode)resultTreeModel
50 .getRoot();
51
52 this.selection = selection;
53 } //}}}
54
55 //{{{ run() method
56 public void run()
57 {
58 SearchFileSet fileset = SearchAndReplace.getSearchFileSet();
59 setProgressMaximum(fileset.getFileCount());
60 setStatus(jEdit.getProperty("hypersearch.status"));
61
62 int resultCount = 0;
63 int bufferCount = 0;
64
65 String[] files = fileset.getFiles(view);
66
67 try
68 {
69 if(selection != null)
70 {
71 Buffer buffer = jEdit.openTemporary(null,null,files[0],false);
72 if(buffer == null)
73 return;
74
75 searchInSelection(buffer);
76 }
77 else
78 {
79 int current = 0;
80
81loop: for(int i = 0; i < files.length; i++)
82 {
83 setProgressValue(++current);
84
85 Buffer buffer = jEdit.openTemporary(null,null,files[i],false);
86 if(buffer == null)
87 continue loop;
88
89 int thisResultCount = doHyperSearch(buffer,
90 0,buffer.getLength());
91 if(thisResultCount != 0)
92 {
93 bufferCount++;
94 resultCount += thisResultCount;
95 }
96 };
97 }
98 }
99 catch(final Exception e)
100 {
101 Log.log(Log.ERROR,this,e);
102 SwingUtilities.invokeLater(new Runnable()
103 {
104 public void run()
105 {
106 GUIUtilities.error(view,"searcherror",
107 new String[] { e.toString() });
108 }
109 });
110 }
111 catch(WorkThread.Abort a)
112 {
113 }
114 finally
115 {
116 final int _resultCount = resultCount;
117 final int _bufferCount = bufferCount;
118 VFSManager.runInAWTThread(new Runnable()
119 {
120 public void run()
121 {
122 results.searchDone(_resultCount,_bufferCount);
123 }
124 });
125 }
126 } //}}}
127
128 //{{{ Private members
129
130 //{{{ Instance variables
131 private View view;
132 private SearchMatcher matcher;
133 private HyperSearchResults results;
134 private DefaultTreeModel resultTreeModel;
135 private DefaultMutableTreeNode resultTreeRoot;
136 private Selection[] selection;
137 //}}}
138
139 //{{{ searchInSelection() method
140 private int searchInSelection(Buffer buffer) throws Exception
141 {
142 setAbortable(false);
143
144 final DefaultMutableTreeNode bufferNode = new DefaultMutableTreeNode(
145 buffer.getPath());
146
147 int resultCount = 0;
148
149 for(int i = 0; i < selection.length; i++)
150 {
151 Selection s = selection[i];
152 resultCount += doHyperSearch(buffer,s.getStart(),s.getEnd());
153 }
154
155 setAbortable(true);
156
157 return resultCount;
158 } //}}}
159
160 //{{{ doHyperSearch() method
161 private int doHyperSearch(Buffer buffer, int start, int end)
162 throws Exception
163 {
164 setAbortable(false);
165
166 final DefaultMutableTreeNode bufferNode = new DefaultMutableTreeNode(
167 buffer.getPath());
168
169 int resultCount = doHyperSearch(buffer,start,end,bufferNode);
170
171 if(resultCount != 0)
172 {
173 resultTreeRoot.insert(bufferNode,resultTreeRoot.getChildCount());
174
175 SwingUtilities.invokeLater(new Runnable()
176 {
177 public void run()
178 {
179 resultTreeModel.reload(resultTreeRoot);
180 }
181 });
182 }
183
184 setAbortable(true);
185
186 return resultCount;
187 } //}}}
188
189 //{{{ doHyperSearch() method
190 private int doHyperSearch(Buffer buffer, int start, int end,
191 DefaultMutableTreeNode bufferNode)
192 {
193 int resultCount = 0;
194
195 try
196 {
197 buffer.readLock();
198
199 Segment text = new Segment();
200 int offset = start;
201 int length = end;
202 int line = -1;
203
204loop: for(;;)
205 {
206 buffer.getText(offset,length - offset,text);
207 int[] match = matcher.nextMatch(
208 new CharIndexedSegment(text,false),
209 offset == 0,length == buffer.getLength());
210 if(match == null)
211 break loop;
212
213 int matchStart = offset + match[0];
214 int matchEnd = offset + match[1];
215
216 offset += match[1];
217 if(match[0] - match[1] == 0)
218 offset++;
219
220 int newLine = buffer.getLineOfOffset(offset);
221 if(line == newLine)
222 {
223 // already had a result on this
224 // line, skip
225 continue loop;
226 }
227
228 line = newLine;
229
230 resultCount++;
231
232 bufferNode.add(new DefaultMutableTreeNode(
233 new HyperSearchResult(buffer,line,
234 matchStart,matchEnd),false));
235 }
236 }
237 finally
238 {
239 buffer.readUnlock();
240 }
241
242 return resultCount;
243 } //}}}
244
245 //}}}
246}