PageRenderTime 37ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/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. 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. public 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.resultTreeModel = results.getTreeModel();
  45. this.resultTreeRoot = (DefaultMutableTreeNode)resultTreeModel
  46. .getRoot();
  47. this.selection = selection;
  48. } //}}}
  49. //{{{ run() method
  50. public void run()
  51. {
  52. SearchFileSet fileset = SearchAndReplace.getSearchFileSet();
  53. setProgressMaximum(fileset.getFileCount());
  54. setStatus(jEdit.getProperty("hypersearch.status"));
  55. int resultCount = 0;
  56. int bufferCount = 0;
  57. String[] files = fileset.getFiles(view);
  58. try
  59. {
  60. if(selection != null)
  61. {
  62. Buffer buffer = jEdit.openTemporary(null,null,files[0],false);
  63. if(buffer == null)
  64. return;
  65. searchInSelection(buffer);
  66. }
  67. else
  68. {
  69. int current = 0;
  70. loop: for(int i = 0; i < files.length; i++)
  71. {
  72. setProgressValue(++current);
  73. Buffer buffer = jEdit.openTemporary(null,null,files[i],false);
  74. if(buffer == null)
  75. continue loop;
  76. int thisResultCount = doHyperSearch(buffer,
  77. 0,buffer.getLength());
  78. if(thisResultCount != 0)
  79. {
  80. bufferCount++;
  81. resultCount += thisResultCount;
  82. }
  83. };
  84. }
  85. }
  86. catch(final Exception e)
  87. {
  88. Log.log(Log.ERROR,this,e);
  89. SwingUtilities.invokeLater(new Runnable()
  90. {
  91. public void run()
  92. {
  93. GUIUtilities.error(view,"searcherror",
  94. new String[] { e.toString() });
  95. }
  96. });
  97. }
  98. catch(WorkThread.Abort a)
  99. {
  100. }
  101. finally
  102. {
  103. final int _resultCount = resultCount;
  104. final int _bufferCount = bufferCount;
  105. VFSManager.runInAWTThread(new Runnable()
  106. {
  107. public void run()
  108. {
  109. results.searchDone(_resultCount,_bufferCount);
  110. }
  111. });
  112. }
  113. } //}}}
  114. //{{{ Private members
  115. //{{{ Instance variables
  116. private View view;
  117. private SearchMatcher matcher;
  118. private HyperSearchResults results;
  119. private DefaultTreeModel resultTreeModel;
  120. private DefaultMutableTreeNode resultTreeRoot;
  121. private Selection[] selection;
  122. //}}}
  123. //{{{ searchInSelection() method
  124. private int searchInSelection(Buffer buffer) throws Exception
  125. {
  126. setAbortable(false);
  127. final DefaultMutableTreeNode bufferNode = new DefaultMutableTreeNode(
  128. buffer.getPath());
  129. int resultCount = 0;
  130. for(int i = 0; i < selection.length; i++)
  131. {
  132. Selection s = selection[i];
  133. resultCount += doHyperSearch(buffer,s.getStart(),s.getEnd());
  134. }
  135. setAbortable(true);
  136. return resultCount;
  137. } //}}}
  138. //{{{ doHyperSearch() method
  139. private int doHyperSearch(Buffer buffer, int start, int end)
  140. throws Exception
  141. {
  142. setAbortable(false);
  143. final DefaultMutableTreeNode bufferNode = new DefaultMutableTreeNode(
  144. buffer.getPath());
  145. int resultCount = doHyperSearch(buffer,start,end,bufferNode);
  146. if(resultCount != 0)
  147. {
  148. resultTreeRoot.insert(bufferNode,resultTreeRoot.getChildCount());
  149. SwingUtilities.invokeLater(new Runnable()
  150. {
  151. public void run()
  152. {
  153. resultTreeModel.reload(resultTreeRoot);
  154. }
  155. });
  156. }
  157. setAbortable(true);
  158. return resultCount;
  159. } //}}}
  160. //{{{ doHyperSearch() method
  161. private int doHyperSearch(Buffer buffer, int start, int end,
  162. DefaultMutableTreeNode bufferNode)
  163. {
  164. int resultCount = 0;
  165. try
  166. {
  167. buffer.readLock();
  168. Segment text = new Segment();
  169. int offset = start;
  170. int length = end;
  171. int line = -1;
  172. loop: for(;;)
  173. {
  174. buffer.getText(offset,length - offset,text);
  175. int[] match = matcher.nextMatch(
  176. new CharIndexedSegment(text,false),
  177. offset == 0,length == buffer.getLength());
  178. if(match == null)
  179. break loop;
  180. int matchStart = offset + match[0];
  181. int matchEnd = offset + match[1];
  182. offset += match[1];
  183. if(match[0] - match[1] == 0)
  184. offset++;
  185. int newLine = buffer.getLineOfOffset(offset);
  186. if(line == newLine)
  187. {
  188. // already had a result on this
  189. // line, skip
  190. continue loop;
  191. }
  192. line = newLine;
  193. resultCount++;
  194. bufferNode.add(new DefaultMutableTreeNode(
  195. new HyperSearchResult(buffer,line,
  196. matchStart,matchEnd),false));
  197. }
  198. }
  199. finally
  200. {
  201. buffer.readUnlock();
  202. }
  203. return resultCount;
  204. } //}}}
  205. //}}}
  206. }