/jEdit/tags/jedit-4-0-pre1/org/gjt/sp/jedit/search/HyperSearchRequest.java

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