PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms 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
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, 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. package org.gjt.sp.jedit.search;
  23. //{{{ Imports
  24. import javax.swing.text.Segment;
  25. import javax.swing.tree.*;
  26. import javax.swing.SwingUtilities;
  27. import org.gjt.sp.jedit.textarea.Selection;
  28. import org.gjt.sp.jedit.io.VFSManager;
  29. import org.gjt.sp.jedit.Buffer;
  30. import org.gjt.sp.jedit.GUIUtilities;
  31. import org.gjt.sp.jedit.jEdit;
  32. import org.gjt.sp.jedit.View;
  33. import org.gjt.sp.util.*;
  34. //}}}
  35. class HyperSearchRequest extends WorkRequest
  36. {
  37. //{{{ HyperSearchRequest constructor
  38. public HyperSearchRequest(View view, SearchMatcher matcher,
  39. HyperSearchResults results, Selection[] selection)
  40. {
  41. this.view = view;
  42. this.matcher = matcher;
  43. this.results = results;
  44. this.searchString = SearchAndReplace.getSearchString();
  45. this.rootSearchNode = new DefaultMutableTreeNode(searchString);
  46. this.selection = selection;
  47. } //}}}
  48. //{{{ run() method
  49. public void run()
  50. {
  51. setStatus(jEdit.getProperty("hypersearch-status"));
  52. SearchFileSet fileset = SearchAndReplace.getSearchFileSet();
  53. String[] files = fileset.getFiles(view);
  54. if(files == null || files.length == 0)
  55. {
  56. SwingUtilities.invokeLater(new Runnable()
  57. {
  58. public void run()
  59. {
  60. GUIUtilities.error(view,"empty-fileset",null);
  61. results.searchDone(rootSearchNode);
  62. }
  63. });
  64. return;
  65. }
  66. setProgressMaximum(fileset.getFileCount(view));
  67. // to minimise synchronization and stuff like that, we only
  68. // show a status message at most twice a second
  69. // initially zero, so that we always show the first message
  70. long lastStatusTime = 0;
  71. try
  72. {
  73. if(selection != null)
  74. {
  75. Buffer buffer = view.getBuffer();
  76. searchInSelection(buffer);
  77. }
  78. else
  79. {
  80. int current = 0;
  81. loop: for(int i = 0; i < files.length; i++)
  82. {
  83. String file = files[i];
  84. current++;
  85. long currentTime = System.currentTimeMillis();
  86. if(currentTime - lastStatusTime > 250)
  87. {
  88. setProgressValue(current);
  89. lastStatusTime = currentTime;
  90. }
  91. Buffer buffer = jEdit.openTemporary(null,null,file,false);
  92. if(buffer == null)
  93. continue loop;
  94. doHyperSearch(buffer);
  95. };
  96. }
  97. }
  98. catch(final Exception e)
  99. {
  100. Log.log(Log.ERROR,this,e);
  101. SwingUtilities.invokeLater(new Runnable()
  102. {
  103. public void run()
  104. {
  105. SearchAndReplace.handleError(view,e);
  106. }
  107. });
  108. }
  109. catch(WorkThread.Abort a)
  110. {
  111. }
  112. finally
  113. {
  114. VFSManager.runInAWTThread(new Runnable()
  115. {
  116. public void run()
  117. {
  118. results.searchDone(rootSearchNode);
  119. }
  120. });
  121. }
  122. } //}}}
  123. //{{{ Private members
  124. //{{{ Instance variables
  125. private View view;
  126. private SearchMatcher matcher;
  127. private HyperSearchResults results;
  128. private DefaultMutableTreeNode rootSearchNode;
  129. private Selection[] selection;
  130. private String searchString;
  131. //}}}
  132. //{{{ searchInSelection() method
  133. private int searchInSelection(Buffer buffer) throws Exception
  134. {
  135. setAbortable(false);
  136. int resultCount = 0;
  137. try
  138. {
  139. buffer.readLock();
  140. for(int i = 0; i < selection.length; i++)
  141. {
  142. Selection s = selection[i];
  143. if(s instanceof Selection.Rect)
  144. {
  145. for(int j = s.getStartLine();
  146. j <= s.getEndLine(); j++)
  147. {
  148. resultCount += doHyperSearch(buffer,
  149. s.getStart(buffer,j),
  150. s.getEnd(buffer,j));
  151. }
  152. }
  153. else
  154. {
  155. resultCount += doHyperSearch(buffer,
  156. s.getStart(),s.getEnd());
  157. }
  158. }
  159. }
  160. finally
  161. {
  162. buffer.readUnlock();
  163. }
  164. setAbortable(true);
  165. return resultCount;
  166. } //}}}
  167. //{{{ doHyperSearch() method
  168. private int doHyperSearch(Buffer buffer)
  169. throws Exception
  170. {
  171. return doHyperSearch(buffer, 0, buffer.getLength());
  172. } //}}}
  173. //{{{ doHyperSearch() method
  174. private int doHyperSearch(Buffer buffer, int start, int end)
  175. throws Exception
  176. {
  177. setAbortable(false);
  178. final DefaultMutableTreeNode bufferNode = new DefaultMutableTreeNode(
  179. buffer.getPath());
  180. int resultCount = doHyperSearch(buffer,start,end,bufferNode);
  181. if(resultCount != 0)
  182. {
  183. rootSearchNode.insert(bufferNode,rootSearchNode.getChildCount());
  184. }
  185. setAbortable(true);
  186. return resultCount;
  187. } //}}}
  188. //{{{ doHyperSearch() method
  189. private int doHyperSearch(Buffer buffer, int start, int end,
  190. DefaultMutableTreeNode bufferNode)
  191. {
  192. int resultCount = 0;
  193. try
  194. {
  195. buffer.readLock();
  196. boolean endOfLine = (buffer.getLineEndOffset(
  197. buffer.getLineOfOffset(end)) - 1 == end);
  198. Segment text = new Segment();
  199. int offset = start;
  200. HyperSearchResult lastResult = null;
  201. loop: for(int counter = 0; ; counter++)
  202. {
  203. boolean startOfLine = (buffer.getLineStartOffset(
  204. buffer.getLineOfOffset(offset)) == offset);
  205. buffer.getText(offset,end - offset,text);
  206. SearchMatcher.Match match = matcher.nextMatch(
  207. new CharIndexedSegment(text,false),
  208. startOfLine,endOfLine,counter == 0,
  209. false);
  210. if(match == null)
  211. break loop;
  212. int newLine = buffer.getLineOfOffset(
  213. offset + match.start);
  214. if(lastResult == null || lastResult.line != newLine)
  215. {
  216. lastResult = new HyperSearchResult(
  217. buffer,newLine);
  218. bufferNode.add(
  219. new DefaultMutableTreeNode(
  220. lastResult,false));
  221. }
  222. lastResult.addOccur(offset + match.start,
  223. offset + match.end);
  224. offset += match.end;
  225. resultCount++;
  226. }
  227. }
  228. finally
  229. {
  230. buffer.readUnlock();
  231. }
  232. return resultCount;
  233. } //}}}
  234. //}}}
  235. }