PageRenderTime 165ms CodeModel.GetById 107ms RepoModel.GetById 8ms app.codeStats 0ms

/jEdit/tags/jedit-4-3-pre5/org/gjt/sp/jedit/search/HyperSearchResult.java

#
Java | 216 lines | 138 code | 27 blank | 51 comment | 20 complexity | c42187bca7c4104e2282f832b7049707 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. * HyperSearchResult.java - HyperSearch result
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 1998, 2003 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.Position;
  25. import org.gjt.sp.jedit.io.VFSManager;
  26. import org.gjt.sp.jedit.textarea.*;
  27. import org.gjt.sp.jedit.*;
  28. //}}}
  29. /**
  30. * A set of occurrences of the search string on a given line in a buffer.
  31. */
  32. public class HyperSearchResult implements HyperSearchNode
  33. {
  34. public String path;
  35. public Buffer buffer;
  36. public int line;
  37. public String str; // cached for speed
  38. public Occur occur;
  39. public int occurCount;
  40. //{{{ getBuffer() method
  41. public Buffer getBuffer()
  42. {
  43. if(buffer == null)
  44. buffer = jEdit.openFile(null,path);
  45. return buffer;
  46. } //}}}
  47. //{{{ getSelection() method
  48. /**
  49. * Returns an array of selection objects pointing to the occurrences
  50. * of the search term on the current line. The buffer must be opened
  51. * first.
  52. * @since jEdit 4.2pre5
  53. */
  54. public Selection[] getSelection()
  55. {
  56. if(buffer == null)
  57. return null;
  58. Selection[] returnValue = new Selection[occurCount];
  59. Occur o = occur;
  60. int i = 0;
  61. while(o != null)
  62. {
  63. Selection.Range s = new Selection.Range(
  64. o.startPos.getOffset(),
  65. o.endPos.getOffset()
  66. );
  67. returnValue[i++] = s;
  68. o = o.next;
  69. }
  70. return returnValue;
  71. } //}}}
  72. //{{{ goTo() method
  73. public void goTo(final EditPane editPane)
  74. {
  75. final Buffer buffer = getBuffer();
  76. if(buffer == null)
  77. return;
  78. editPane.setBuffer(buffer);
  79. VFSManager.runInAWTThread(new Runnable()
  80. {
  81. public void run()
  82. {
  83. Selection[] s = getSelection();
  84. if(s == null)
  85. return;
  86. JEditTextArea textArea = editPane.getTextArea();
  87. if(textArea.isMultipleSelectionEnabled())
  88. textArea.addToSelection(s);
  89. else
  90. textArea.setSelection(s);
  91. textArea.moveCaretPosition(occur.endPos.getOffset());
  92. }
  93. });
  94. } //}}}
  95. //{{{ toString() method
  96. public String toString()
  97. {
  98. return str;
  99. } //}}}
  100. //{{{ Package-private members
  101. //{{{ HyperSearchResult constructor
  102. HyperSearchResult(Buffer buffer, int line)
  103. {
  104. path = buffer.getPath();
  105. if(!buffer.isTemporary())
  106. bufferOpened(buffer);
  107. this.line = line;
  108. str = (line + 1) + ": " + buffer.getLineText(line)
  109. .replace('\t',' ').trim();
  110. } //}}}
  111. //{{{ bufferOpened() method
  112. void bufferOpened(Buffer buffer)
  113. {
  114. this.buffer = buffer;
  115. Occur o = occur;
  116. while(o != null)
  117. {
  118. o.bufferOpened();
  119. o = o.next;
  120. }
  121. } //}}}
  122. //{{{ bufferClosed() method
  123. void bufferClosed()
  124. {
  125. buffer = null;
  126. Occur o = occur;
  127. while(o != null)
  128. {
  129. o.bufferClosed();
  130. o = o.next;
  131. }
  132. } //}}}
  133. //{{{ addOccur() method
  134. void addOccur(int start, int end)
  135. {
  136. Occur o = new Occur(start,end);
  137. o.next = occur;
  138. occur = o;
  139. occurCount++;
  140. } //}}}
  141. //{{{ pathEquals() method
  142. /**
  143. * @param path A canonical path
  144. */
  145. boolean pathEquals(String path)
  146. {
  147. return path.equals(MiscUtilities.resolveSymlinks(this.path));
  148. } //}}}
  149. //{{{ equals() method
  150. public boolean equals(Object compareObj)
  151. {
  152. if (!(compareObj instanceof HyperSearchResult))
  153. return false;
  154. HyperSearchResult otherResult = (HyperSearchResult)compareObj;
  155. return pathEquals(otherResult.path) && line == otherResult.line
  156. && buffer.equals(otherResult.buffer);
  157. }//}}}
  158. //}}}
  159. //{{{ Occur class
  160. public class Occur
  161. {
  162. public int start, end;
  163. public Position startPos, endPos;
  164. public Occur next;
  165. //{{{ Occur constructor
  166. Occur(int start, int end)
  167. {
  168. this.start = start;
  169. this.end = end;
  170. if(buffer != null && !buffer.isTemporary())
  171. bufferOpened();
  172. } //}}}
  173. //{{{ bufferOpened() method
  174. void bufferOpened()
  175. {
  176. startPos = buffer.createPosition(Math.min(
  177. buffer.getLength(),start));
  178. endPos = buffer.createPosition(Math.min(
  179. buffer.getLength(),end));
  180. } //}}}
  181. //{{{ bufferClosed() method
  182. void bufferClosed()
  183. {
  184. start = startPos.getOffset();
  185. end = endPos.getOffset();
  186. startPos = endPos = null;
  187. } //}}}
  188. } //}}}
  189. }