PageRenderTime 45ms CodeModel.GetById 34ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre14/org/gjt/sp/jedit/search/HyperSearchRequest.java

#
Java | 279 lines | 199 code | 44 blank | 36 comment | 25 complexity | 1c5f8c2cef08a29c99d61edaefe81773 MD5 | raw file
  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
 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
 38class 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.searchString = SearchAndReplace.getSearchString();
 49		this.rootSearchNode = new DefaultMutableTreeNode(searchString);
 50
 51		this.selection = selection;
 52	} //}}}
 53
 54	//{{{ run() method
 55	public void run()
 56	{
 57		setStatus(jEdit.getProperty("hypersearch-status"));
 58
 59		SearchFileSet fileset = SearchAndReplace.getSearchFileSet();
 60		String[] files = fileset.getFiles(view);
 61		if(files == null || files.length == 0)
 62		{
 63			SwingUtilities.invokeLater(new Runnable()
 64			{
 65				public void run()
 66				{
 67					GUIUtilities.error(view,"empty-fileset",null);
 68					results.searchDone(rootSearchNode);
 69				}
 70			});
 71			return;
 72		}
 73
 74		setProgressMaximum(fileset.getFileCount(view));
 75
 76		// to minimise synchronization and stuff like that, we only
 77		// show a status message at most twice a second
 78
 79		// initially zero, so that we always show the first message
 80		long lastStatusTime = 0;
 81
 82		try
 83		{
 84			if(selection != null)
 85			{
 86				Buffer buffer = view.getBuffer();
 87
 88				searchInSelection(buffer);
 89			}
 90			else
 91			{
 92				int current = 0;
 93
 94loop:				for(int i = 0; i < files.length; i++)
 95				{
 96					String file = files[i];
 97					current++;
 98
 99					long currentTime = System.currentTimeMillis();
100					if(currentTime - lastStatusTime > 250)
101					{
102						setProgressValue(current);
103						lastStatusTime = currentTime;
104					}
105
106					Buffer buffer = jEdit.openTemporary(null,null,file,false);
107					if(buffer == null)
108						continue loop;
109
110					doHyperSearch(buffer);
111				};
112			}
113		}
114		catch(final Exception e)
115		{
116			Log.log(Log.ERROR,this,e);
117			SwingUtilities.invokeLater(new Runnable()
118			{
119				public void run()
120				{
121					SearchAndReplace.handleError(view,e);
122				}
123			});
124		}
125		catch(WorkThread.Abort a)
126		{
127		}
128		finally
129		{
130			VFSManager.runInAWTThread(new Runnable()
131			{
132				public void run()
133				{
134					results.searchDone(rootSearchNode);
135				}
136			});
137		}
138	} //}}}
139
140	//{{{ Private members
141
142	//{{{ Instance variables
143	private View view;
144	private SearchMatcher matcher;
145	private HyperSearchResults results;
146	private DefaultMutableTreeNode rootSearchNode;
147	private Selection[] selection;
148	private String searchString;
149	//}}}
150
151	//{{{ searchInSelection() method
152	private int searchInSelection(Buffer buffer) throws Exception
153	{
154		setAbortable(false);
155
156		int resultCount = 0;
157
158		try
159		{
160			buffer.readLock();
161
162			for(int i = 0; i < selection.length; i++)
163			{
164				Selection s = selection[i];
165				if(s instanceof Selection.Rect)
166				{
167					for(int j = s.getStartLine();
168						j <= s.getEndLine(); j++)
169					{
170						resultCount += doHyperSearch(buffer,
171							s.getStart(buffer,j),
172							s.getEnd(buffer,j));
173					}
174				}
175				else
176				{
177					resultCount += doHyperSearch(buffer,
178						s.getStart(),s.getEnd());
179				}
180			}
181		}
182		finally
183		{
184			buffer.readUnlock();
185		}
186
187		setAbortable(true);
188
189		return resultCount;
190	} //}}}
191
192	//{{{ doHyperSearch() method
193	private int doHyperSearch(Buffer buffer)
194		throws Exception
195	{
196		return doHyperSearch(buffer, 0, buffer.getLength());
197	} //}}}
198
199	//{{{ doHyperSearch() method
200	private int doHyperSearch(Buffer buffer, int start, int end)
201		throws Exception
202	{
203		setAbortable(false);
204
205		final DefaultMutableTreeNode bufferNode = new DefaultMutableTreeNode(
206			buffer.getPath());
207
208		int resultCount = doHyperSearch(buffer,start,end,bufferNode);
209
210		if(resultCount != 0)
211		{
212			rootSearchNode.insert(bufferNode,rootSearchNode.getChildCount());
213		}
214
215		setAbortable(true);
216
217		return resultCount;
218	} //}}}
219
220	//{{{ doHyperSearch() method
221	private int doHyperSearch(Buffer buffer, int start, int end,
222		DefaultMutableTreeNode bufferNode)
223	{
224		int resultCount = 0;
225
226		try
227		{
228			buffer.readLock();
229
230			boolean endOfLine = (buffer.getLineEndOffset(
231				buffer.getLineOfOffset(end)) - 1 == end);
232
233			Segment text = new Segment();
234			int offset = start;
235
236			HyperSearchResult lastResult = null;
237
238loop:			for(int counter = 0; ; counter++)
239			{
240				boolean startOfLine = (buffer.getLineStartOffset(
241					buffer.getLineOfOffset(offset)) == offset);
242
243				buffer.getText(offset,end - offset,text);
244				SearchMatcher.Match match = matcher.nextMatch(
245					new CharIndexedSegment(text,false),
246					startOfLine,endOfLine,counter == 0,
247					false);
248				if(match == null)
249					break loop;
250
251				int newLine = buffer.getLineOfOffset(
252					offset + match.start);
253				if(lastResult == null || lastResult.line != newLine)
254				{
255					lastResult = new HyperSearchResult(
256						buffer,newLine);
257					bufferNode.add(
258						new DefaultMutableTreeNode(
259						lastResult,false));
260				}
261
262				lastResult.addOccur(offset + match.start,
263					offset + match.end);
264
265				offset += match.end;
266
267				resultCount++;
268			}
269		}
270		finally
271		{
272			buffer.readUnlock();
273		}
274
275		return resultCount;
276	} //}}}
277
278	//}}}
279}