PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre14/org/gjt/sp/jedit/search/HyperSearchResult.java

#
Java | 199 lines | 127 code | 26 blank | 46 comment | 18 complexity | 1d4e0ae34fe6d3036d8c92b567e8da30 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
  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 View view)
  74. {
  75. if(buffer == null)
  76. buffer = jEdit.openFile(null,path);
  77. if(buffer == null)
  78. return;
  79. VFSManager.runInAWTThread(new Runnable()
  80. {
  81. public void run()
  82. {
  83. Selection[] s = getSelection();
  84. if(s == null)
  85. return;
  86. EditPane pane = view.goToBuffer(buffer);
  87. JEditTextArea textArea = pane.getTextArea();
  88. if(textArea.isMultipleSelectionEnabled())
  89. textArea.addToSelection(s);
  90. else
  91. textArea.setSelection(s);
  92. textArea.moveCaretPosition(occur.endPos.getOffset());
  93. }
  94. });
  95. } //}}}
  96. //{{{ toString() method
  97. public String toString()
  98. {
  99. return str;
  100. } //}}}
  101. //{{{ Package-private members
  102. //{{{ HyperSearchResult constructor
  103. HyperSearchResult(Buffer buffer, int line)
  104. {
  105. path = buffer.getPath();
  106. if(!buffer.isTemporary())
  107. bufferOpened(buffer);
  108. this.line = line;
  109. str = (line + 1) + ": " + buffer.getLineText(line)
  110. .replace('\t',' ').trim();
  111. } //}}}
  112. //{{{ bufferOpened() method
  113. void bufferOpened(Buffer buffer)
  114. {
  115. this.buffer = buffer;
  116. Occur o = occur;
  117. while(o != null)
  118. {
  119. o.bufferOpened();
  120. o = o.next;
  121. }
  122. } //}}}
  123. //{{{ bufferClosed() method
  124. void bufferClosed()
  125. {
  126. buffer = null;
  127. Occur o = occur;
  128. while(o != null)
  129. {
  130. o.bufferClosed();
  131. o = o.next;
  132. }
  133. } //}}}
  134. //{{{ addOccur() method
  135. void addOccur(int start, int end)
  136. {
  137. Occur o = new Occur(start,end);
  138. o.next = occur;
  139. occur = o;
  140. occurCount++;
  141. } //}}}
  142. //}}}
  143. //{{{ Occur class
  144. public class Occur
  145. {
  146. public int start, end;
  147. public Position startPos, endPos;
  148. public Occur next;
  149. //{{{ Occur constructor
  150. Occur(int start, int end)
  151. {
  152. this.start = start;
  153. this.end = end;
  154. if(buffer != null && !buffer.isTemporary())
  155. bufferOpened();
  156. } //}}}
  157. //{{{ bufferOpened() method
  158. void bufferOpened()
  159. {
  160. startPos = buffer.createPosition(Math.min(
  161. buffer.getLength(),start));
  162. endPos = buffer.createPosition(Math.min(
  163. buffer.getLength(),end));
  164. } //}}}
  165. //{{{ bufferClosed() method
  166. void bufferClosed()
  167. {
  168. start = startPos.getOffset();
  169. end = endPos.getOffset();
  170. startPos = endPos = null;
  171. } //}}}
  172. } //}}}
  173. }