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

/jEdit/tags/jedit-4-0-pre3/org/gjt/sp/jedit/textarea/Selection.java

#
Java | 229 lines | 113 code | 27 blank | 89 comment | 8 complexity | c3802f969c0600eed0d127bceb8a5941 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. * Selection.java - Selected text
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 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.textarea;
  23. import org.gjt.sp.jedit.Buffer;
  24. /**
  25. * An interface representing a portion of the current selection.
  26. * @author Slava Pestov
  27. * @version $Id: Selection.java 3853 2001-10-25 07:35:25Z spestov $
  28. * @since jEdit 3.2pre1
  29. */
  30. public abstract class Selection
  31. {
  32. //{{{ getStart() method
  33. /**
  34. * Returns the start offset of this selection.
  35. */
  36. public int getStart()
  37. {
  38. return start;
  39. } //}}}
  40. //{{{ getEnd() method
  41. /**
  42. * Returns the end offset of this selection.
  43. */
  44. public int getEnd()
  45. {
  46. return end;
  47. } //}}}
  48. //{{{ getStartLine() method
  49. /**
  50. * Returns the starting line number of this selection.
  51. */
  52. public int getStartLine()
  53. {
  54. return startLine;
  55. } //}}}
  56. //{{{ getEndLine() method
  57. /**
  58. * Returns the ending line number of this selection.
  59. */
  60. public int getEndLine()
  61. {
  62. return endLine;
  63. } //}}}
  64. //{{{ getStart() method
  65. /**
  66. * Returns the start offset of this selection on the specified
  67. * line.
  68. * @param buffer The buffer
  69. * @param line The line number
  70. */
  71. public abstract int getStart(Buffer buffer, int line);
  72. //}}}
  73. //{{{ getEnd() method
  74. /**
  75. * Returns the end offset of this selection on the specified
  76. * line.
  77. * @param buffer The buffer
  78. * @param line The line number
  79. */
  80. public abstract int getEnd(Buffer buffer, int line);
  81. //}}}
  82. //{{{ toString() method
  83. public String toString()
  84. {
  85. return getClass().getName() + "[start=" + start
  86. + ",end=" + end + ",startLine=" + startLine
  87. + ",endLine=" + endLine + "]";
  88. } //}}}
  89. //{{{ Package-private members
  90. int start, end, startLine, endLine;
  91. //}}}
  92. //{{{ Protected members
  93. //{{{ Selection constructor
  94. protected Selection()
  95. {
  96. } //}}}
  97. //{{{ Selection constructor
  98. protected Selection(Selection copy)
  99. {
  100. start = copy.start;
  101. end = copy.end;
  102. } //}}}
  103. //{{{ Selection constructor
  104. protected Selection(int start, int end)
  105. {
  106. this.start = start;
  107. this.end = end;
  108. // setting these is handled by textArea._addToSelection();
  109. //this.startLine = startLine;
  110. //this.endLine = endLine;
  111. } //}}}
  112. //{{{ Range class
  113. /**
  114. * An ordinary range selection.
  115. * @since jEdit 3.2pre1
  116. */
  117. public static class Range extends Selection
  118. {
  119. //{{{ Range constructor
  120. public Range()
  121. {
  122. super();
  123. } //}}}
  124. //{{{ Range constructor
  125. public Range(Selection sel)
  126. {
  127. super(sel);
  128. } //}}}
  129. //{{{ Range constructor
  130. public Range(int start, int end)
  131. {
  132. super(start,end);
  133. } //}}}
  134. //{{{ getStart() method
  135. public int getStart(Buffer buffer, int line)
  136. {
  137. if(line == startLine)
  138. return start;
  139. else
  140. return buffer.getLineStartOffset(line);
  141. } //}}}
  142. //{{{ getEnd() method
  143. public int getEnd(Buffer buffer, int line)
  144. {
  145. if(line == endLine)
  146. return end;
  147. else
  148. return buffer.getLineEndOffset(line) - 1;
  149. } //}}}
  150. } //}}}
  151. //{{{ Rect class
  152. /**
  153. * A rectangular selection.
  154. * @since jEdit 3.2pre1
  155. */
  156. public static class Rect extends Selection
  157. {
  158. //{{{ Rect constructor
  159. public Rect()
  160. {
  161. super();
  162. } //}}}
  163. //{{{ Rect constructor
  164. public Rect(Selection sel)
  165. {
  166. super(sel);
  167. } //}}}
  168. //{{{ Rect constructor
  169. public Rect(int start, int end)
  170. {
  171. super(start,end);
  172. } //}}}
  173. //{{{ getStart() method
  174. public int getStart(Buffer buffer, int line)
  175. {
  176. if(line == startLine)
  177. return start;
  178. else
  179. {
  180. int _start = start - buffer.getLineStartOffset(startLine);
  181. int _end = end - buffer.getLineStartOffset(endLine);
  182. return Math.min(buffer.getLineEndOffset(line) - 1,
  183. buffer.getLineStartOffset(line)
  184. + Math.min(_start,_end));
  185. }
  186. } //}}}
  187. //{{{ getEnd() method
  188. public int getEnd(Buffer buffer, int line)
  189. {
  190. if(line == endLine)
  191. return end;
  192. else
  193. {
  194. int _start = start - buffer.getLineStartOffset(startLine);
  195. int _end = end - buffer.getLineEndOffset(endLine);
  196. return Math.min(buffer.getLineEndOffset(line) - 1,
  197. buffer.getLineStartOffset(line)
  198. + Math.max(_start,_end));
  199. }
  200. } //}}}
  201. } //}}}
  202. }