PageRenderTime 49ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/jedit-4.3.2/jEdit/org/gjt/sp/jedit/search/HyperSearchRequest.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 303 lines | 224 code | 39 blank | 40 comment | 35 complexity | 7a1e9573ae662bd6488b8daed7b6fb9a 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. package org.gjt.sp.jedit.search;
  23. //{{{ Imports
  24. import javax.swing.tree.*;
  25. import javax.swing.*;
  26. import org.gjt.sp.jedit.textarea.Selection;
  27. import org.gjt.sp.jedit.textarea.JEditTextArea;
  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. /**
  36. * HyperSearch results window.
  37. * @author Slava Pestov
  38. * @version $Id: HyperSearchRequest.java 16331 2009-10-13 13:35:10Z kpouer $
  39. */
  40. class HyperSearchRequest extends WorkRequest
  41. {
  42. //{{{ HyperSearchRequest constructor
  43. HyperSearchRequest(View view, SearchMatcher matcher,
  44. HyperSearchResults results, Selection[] selection)
  45. {
  46. this.view = view;
  47. this.matcher = matcher;
  48. this.results = results;
  49. searchString = SearchAndReplace.getSearchString();
  50. rootSearchNode = new DefaultMutableTreeNode(new HyperSearchOperationNode(searchString, matcher));
  51. this.selection = selection;
  52. } //}}}
  53. //{{{ run() method
  54. public void run()
  55. {
  56. setStatus(jEdit.getProperty("hypersearch-status"));
  57. SearchFileSet fileset = SearchAndReplace.getSearchFileSet();
  58. String[] files = fileset.getFiles(view);
  59. if(files == null || files.length == 0)
  60. {
  61. SwingUtilities.invokeLater(new Runnable()
  62. {
  63. public void run()
  64. {
  65. GUIUtilities.error(view,"empty-fileset",null);
  66. results.searchDone(rootSearchNode);
  67. }
  68. });
  69. return;
  70. }
  71. setMaximum(fileset.getFileCount(view));
  72. // to minimize synchronization and stuff like that, we only
  73. // show a status message at most twice a second
  74. // initially zero, so that we always show the first message
  75. String searchingCaption = jEdit.getProperty("hypersearch-results.searching",
  76. new String[] { SearchAndReplace.getSearchString() }) + ' ';
  77. try
  78. {
  79. if(selection != null)
  80. {
  81. Buffer buffer = view.getBuffer();
  82. searchInSelection(buffer);
  83. }
  84. else
  85. {
  86. int current = 0;
  87. long lastStatusTime = 0;
  88. int resultCount = 0;
  89. boolean asked = false;
  90. int maxResults = jEdit.getIntegerProperty("hypersearch.maxWarningResults");
  91. loop: for(int i = 0; i < files.length; i++)
  92. {
  93. if(jEdit.getBooleanProperty("hyperSearch-stopButton"))
  94. {
  95. jEdit.setTemporaryProperty("hyperSearch-stopButton", "false");
  96. Log.log(Log.MESSAGE, this, "Search stopped by user action (stop button)");
  97. break;
  98. }
  99. if (!asked && resultCount > maxResults && maxResults != 0)
  100. {
  101. Log.log(Log.DEBUG, this, "Search in progress, " + resultCount +
  102. " occurrences found, asking the user to stop");
  103. asked = true;
  104. int ret = GUIUtilities.confirm(view, "hypersearch.tooManyResults",
  105. new Object[]{resultCount},
  106. JOptionPane.YES_NO_OPTION,
  107. JOptionPane.QUESTION_MESSAGE);
  108. if (ret == JOptionPane.YES_OPTION)
  109. {
  110. Log.log(Log.MESSAGE, this, "Search stopped by user action");
  111. break;
  112. }
  113. }
  114. String file = files[i];
  115. current++;
  116. long currentTime = System.currentTimeMillis();
  117. if(currentTime - lastStatusTime > 250)
  118. {
  119. setValue(current);
  120. lastStatusTime = currentTime;
  121. results.setSearchStatus(searchingCaption + file);
  122. }
  123. Buffer buffer = jEdit.openTemporary(null,null,file,false);
  124. if(buffer == null)
  125. continue loop;
  126. resultCount += doHyperSearch(buffer, 0, buffer.getLength());
  127. }
  128. Log.log(Log.MESSAGE, this, resultCount +" OCCURENCES");
  129. }
  130. }
  131. catch(final Exception e)
  132. {
  133. Log.log(Log.ERROR,this,e);
  134. SwingUtilities.invokeLater(new Runnable()
  135. {
  136. public void run()
  137. {
  138. SearchAndReplace.handleError(view,e);
  139. }
  140. });
  141. }
  142. catch(WorkThread.Abort a)
  143. {
  144. }
  145. finally
  146. {
  147. VFSManager.runInAWTThread(new Runnable()
  148. {
  149. public void run()
  150. {
  151. results.searchDone(rootSearchNode, selectNode);
  152. }
  153. });
  154. }
  155. } //}}}
  156. //{{{ Private members
  157. //{{{ Instance variables
  158. private View view;
  159. private SearchMatcher matcher;
  160. private HyperSearchResults results;
  161. private DefaultMutableTreeNode rootSearchNode;
  162. private Selection[] selection;
  163. private String searchString;
  164. private DefaultMutableTreeNode selectNode;
  165. //}}}
  166. //{{{ searchInSelection() method
  167. private int searchInSelection(Buffer buffer) throws Exception
  168. {
  169. setAbortable(false);
  170. int resultCount = 0;
  171. try
  172. {
  173. buffer.readLock();
  174. for(int i = 0; i < selection.length; i++)
  175. {
  176. Selection s = selection[i];
  177. if(s instanceof Selection.Rect)
  178. {
  179. for(int j = s.getStartLine();
  180. j <= s.getEndLine(); j++)
  181. {
  182. resultCount += doHyperSearch(buffer,
  183. s.getStart(buffer,j),
  184. s.getEnd(buffer,j));
  185. }
  186. }
  187. else
  188. {
  189. resultCount += doHyperSearch(buffer,
  190. s.getStart(),s.getEnd());
  191. }
  192. }
  193. }
  194. finally
  195. {
  196. buffer.readUnlock();
  197. }
  198. setAbortable(true);
  199. return resultCount;
  200. } //}}}
  201. //{{{ doHyperSearch() method
  202. private int doHyperSearch(Buffer buffer, int start, int end)
  203. throws Exception
  204. {
  205. setAbortable(false);
  206. HyperSearchFileNode hyperSearchFileNode = new HyperSearchFileNode(buffer.getPath());
  207. DefaultMutableTreeNode bufferNode = new DefaultMutableTreeNode(hyperSearchFileNode);
  208. int resultCount = doHyperSearch(buffer,start,end,bufferNode);
  209. hyperSearchFileNode.setCount(resultCount);
  210. if(resultCount != 0)
  211. rootSearchNode.insert(bufferNode,rootSearchNode.getChildCount());
  212. setAbortable(true);
  213. return resultCount;
  214. } //}}}
  215. //{{{ doHyperSearch() method
  216. private int doHyperSearch(Buffer buffer, int start, int end,
  217. DefaultMutableTreeNode bufferNode)
  218. {
  219. int resultCount = 0;
  220. JEditTextArea textArea = jEdit.getActiveView().getTextArea();
  221. int caretLine = textArea.getBuffer() == buffer ? textArea.getCaretLine() : -1;
  222. try
  223. {
  224. buffer.readLock();
  225. boolean endOfLine = buffer.getLineEndOffset(
  226. buffer.getLineOfOffset(end)) - 1 == end;
  227. int offset = start;
  228. HyperSearchResult lastResult = null;
  229. loop: for(int counter = 0; ; counter++)
  230. {
  231. boolean startOfLine = buffer.getLineStartOffset(
  232. buffer.getLineOfOffset(offset)) == offset;
  233. SearchMatcher.Match match = matcher.nextMatch(
  234. buffer.getSegment(offset, end - offset),
  235. startOfLine,endOfLine,counter == 0,
  236. false);
  237. if(match == null)
  238. break loop;
  239. int newLine = buffer.getLineOfOffset(
  240. offset + match.start);
  241. if(lastResult == null || lastResult.line != newLine)
  242. {
  243. lastResult = new HyperSearchResult(
  244. buffer,newLine);
  245. DefaultMutableTreeNode child = new DefaultMutableTreeNode(
  246. lastResult, false);
  247. if (lastResult.line == caretLine)
  248. selectNode = child;
  249. bufferNode.add(child);
  250. }
  251. lastResult.addOccur(offset + match.start,
  252. offset + match.end);
  253. offset += match.end;
  254. resultCount++;
  255. }
  256. }
  257. finally
  258. {
  259. buffer.readUnlock();
  260. }
  261. return resultCount;
  262. } //}}}
  263. //}}}
  264. }