PageRenderTime 15ms CodeModel.GetById 1ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 281 lines | 198 code | 45 blank | 38 comment | 23 complexity | 963d59e41b51ce594c7e41230041370c 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				}
 69			});
 70			return;
 71		}
 72
 73		setProgressMaximum(fileset.getFileCount(view));
 74
 75		// to minimise synchronization and stuff like that, we only
 76		// show a status message at most twice a second
 77
 78		// initially zero, so that we always show the first message
 79		long lastStatusTime = 0;
 80
 81		try
 82		{
 83			if(selection != null)
 84			{
 85				Buffer buffer = view.getBuffer();
 86
 87				searchInSelection(buffer);
 88			}
 89			else
 90			{
 91				int current = 0;
 92
 93loop:				for(int i = 0; i < files.length; i++)
 94				{
 95					String file = files[i];
 96					current++;
 97
 98					long currentTime = System.currentTimeMillis();
 99					if(currentTime - lastStatusTime > 250)
100					{
101						setProgressValue(current);
102						lastStatusTime = currentTime;
103					}
104
105					Buffer buffer = jEdit.openTemporary(null,null,file,false);
106					if(buffer == null)
107						continue loop;
108
109					doHyperSearch(buffer);
110				};
111			}
112		}
113		catch(final Exception e)
114		{
115			Log.log(Log.ERROR,this,e);
116			SwingUtilities.invokeLater(new Runnable()
117			{
118				public void run()
119				{
120					GUIUtilities.error(view,"searcherror",
121						new String[] { e.toString() });
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			int line = -1;
236
237loop:			for(int counter = 0; ; counter++)
238			{
239				boolean startOfLine = (buffer.getLineStartOffset(
240					buffer.getLineOfOffset(offset)) == offset);
241
242				buffer.getText(offset,end - offset,text);
243				SearchMatcher.Match match = matcher.nextMatch(
244					new CharIndexedSegment(text,false),
245					startOfLine,endOfLine,counter == 0,
246					false);
247				if(match == null)
248					break loop;
249
250				int matchStart = offset + match.start;
251				int matchEnd = offset + match.end;
252
253				offset += match.end;
254
255				int newLine = buffer.getLineOfOffset(offset);
256				if(line == newLine)
257				{
258					// already had a result on this
259					// line, skip
260					continue loop;
261				}
262
263				line = newLine;
264
265				resultCount++;
266
267				bufferNode.add(new DefaultMutableTreeNode(
268					new HyperSearchResult(buffer,line,
269					matchStart,matchEnd),false));
270			}
271		}
272		finally
273		{
274			buffer.readUnlock();
275		}
276
277		return resultCount;
278	} //}}}
279
280	//}}}
281}