PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms 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
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. }
  62. });
  63. return;
  64. }
  65. setProgressMaximum(fileset.getFileCount(view));
  66. // to minimise synchronization and stuff like that, we only
  67. // show a status message at most twice a second
  68. // initially zero, so that we always show the first message
  69. long lastStatusTime = 0;
  70. try
  71. {
  72. if(selection != null)
  73. {
  74. Buffer buffer = view.getBuffer();
  75. searchInSelection(buffer);
  76. }
  77. else
  78. {
  79. int current = 0;
  80. loop: for(int i = 0; i < files.length; i++)
  81. {
  82. String file = files[i];
  83. current++;
  84. long currentTime = System.currentTimeMillis();
  85. if(currentTime - lastStatusTime > 250)
  86. {
  87. setProgressValue(current);
  88. lastStatusTime = currentTime;
  89. }
  90. Buffer buffer = jEdit.openTemporary(null,null,file,false);
  91. if(buffer == null)
  92. continue loop;
  93. doHyperSearch(buffer);
  94. };
  95. }
  96. }
  97. catch(final Exception e)
  98. {
  99. Log.log(Log.ERROR,this,e);
  100. SwingUtilities.invokeLater(new Runnable()
  101. {
  102. public void run()
  103. {
  104. GUIUtilities.error(view,"searcherror",
  105. new String[] { e.toString() });
  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. int line = -1;
  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 matchStart = offset + match.start;
  213. int matchEnd = offset + match.end;
  214. offset += match.end;
  215. int newLine = buffer.getLineOfOffset(offset);
  216. if(line == newLine)
  217. {
  218. // already had a result on this
  219. // line, skip
  220. continue loop;
  221. }
  222. line = newLine;
  223. resultCount++;
  224. bufferNode.add(new DefaultMutableTreeNode(
  225. new HyperSearchResult(buffer,line,
  226. matchStart,matchEnd),false));
  227. }
  228. }
  229. finally
  230. {
  231. buffer.readUnlock();
  232. }
  233. return resultCount;
  234. } //}}}
  235. //}}}
  236. }