/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/buffer/ContentManager.java

# · Java · 245 lines · 147 code · 24 blank · 74 comment · 20 complexity · 49f360fb91c6dd10ead4c2c86b5b10c0 MD5 · raw file

  1. /*
  2. * ContentManager.java - Manages text content
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 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.buffer;
  23. import javax.swing.text.Segment;
  24. /**
  25. * A class internal to jEdit's document model. You should not use it
  26. * directly. To improve performance, none of the methods in this class
  27. * check for out of bounds access, nor are they thread-safe. The
  28. * <code>Buffer</code> class, through which these methods must be
  29. * called through, implements such protection.
  30. *
  31. * @author Slava Pestov
  32. * @version $Id: ContentManager.java 17813 2010-05-12 14:20:37Z k_satoda $
  33. * @since jEdit 4.0pre1
  34. */
  35. class ContentManager
  36. {
  37. //{{{ getLength() method
  38. public final int getLength()
  39. {
  40. return length;
  41. } //}}}
  42. //{{{ getText() methods
  43. public String getText(int start, int len)
  44. {
  45. if(start >= gapStart)
  46. return new String(text,start + gapEnd - gapStart,len);
  47. else if(start + len <= gapStart)
  48. return new String(text,start,len);
  49. else
  50. {
  51. return new String(text,start,gapStart - start)
  52. .concat(new String(text,gapEnd,start + len - gapStart));
  53. }
  54. }
  55. /**
  56. * Returns the specified text range in a <code>Segment</code>.<p>
  57. *
  58. * Using a <classname>Segment</classname> is generally more
  59. * efficient than using a <classname>String</classname> because it
  60. * results in less memory allocation and array copying.<p>
  61. *
  62. *
  63. * @param start The start offset
  64. * @param len The number of characters to get
  65. * @param seg The segment to copy the text to
  66. * @see JEditBuffer#getText(int, int, Segment)
  67. */
  68. public void getText(int start, int len, Segment seg)
  69. {
  70. if(start >= gapStart)
  71. {
  72. seg.array = text;
  73. seg.offset = start + gapEnd - gapStart;
  74. seg.count = len;
  75. }
  76. else if(start + len <= gapStart)
  77. {
  78. seg.array = text;
  79. seg.offset = start;
  80. seg.count = len;
  81. }
  82. else
  83. {
  84. seg.array = new char[len];
  85. // copy text before gap
  86. System.arraycopy(text,start,seg.array,0,gapStart - start);
  87. // copy text after gap
  88. System.arraycopy(text,gapEnd,seg.array,gapStart - start,
  89. len + start - gapStart);
  90. seg.offset = 0;
  91. seg.count = len;
  92. }
  93. } //}}}
  94. //{{{ getSegment() method
  95. /**
  96. * Returns a read-only segment of the buffer.
  97. * It doesn't copy the text
  98. *
  99. * @param start The start offset
  100. * @param len The number of characters to get
  101. *
  102. * @return a CharSequence that contains the text wanted text
  103. * @since jEdit 4.3pre15
  104. */
  105. public CharSequence getSegment(int start, int len)
  106. {
  107. if(start >= gapStart)
  108. return new BufferSegment(text,start + gapEnd - gapStart,len);
  109. else if(start + len <= gapStart)
  110. return new BufferSegment(text,start,len);
  111. else
  112. {
  113. return new BufferSegment(text,start,gapStart - start,
  114. new BufferSegment(text,gapEnd,start + len - gapStart));
  115. }
  116. } //}}}
  117. //{{{ insert() methods
  118. public void insert(int start, String str)
  119. {
  120. int len = str.length();
  121. moveGapStart(start);
  122. if(gapEnd - gapStart < len)
  123. {
  124. ensureCapacity(length + len + 1024);
  125. moveGapEnd(start + len + 1024);
  126. }
  127. str.getChars(0,len,text,start);
  128. gapStart += len;
  129. length += len;
  130. }
  131. /**
  132. * Inserts the given data into the buffer.
  133. *
  134. * @since jEdit 4.3pre15
  135. */
  136. public void insert(int start, CharSequence str)
  137. {
  138. int len = str.length();
  139. moveGapStart(start);
  140. if(gapEnd - gapStart < len)
  141. {
  142. ensureCapacity(length + len + 1024);
  143. moveGapEnd(start + len + 1024);
  144. }
  145. for (int i = 0; i < len; i++)
  146. {
  147. text[start+i] = str.charAt(i);
  148. }
  149. gapStart += len;
  150. length += len;
  151. }
  152. public void insert(int start, Segment seg)
  153. {
  154. moveGapStart(start);
  155. if(gapEnd - gapStart < seg.count)
  156. {
  157. ensureCapacity(length + seg.count + 1024);
  158. moveGapEnd(start + seg.count + 1024);
  159. }
  160. System.arraycopy(seg.array,seg.offset,text,start,seg.count);
  161. gapStart += seg.count;
  162. length += seg.count;
  163. } //}}}
  164. //{{{ _setContent() method
  165. public void _setContent(char[] text, int length)
  166. {
  167. this.text = text;
  168. this.gapStart = this.gapEnd = 0;
  169. this.length = length;
  170. } //}}}
  171. //{{{ remove() method
  172. public void remove(int start, int len)
  173. {
  174. moveGapStart(start);
  175. gapEnd += len;
  176. length -= len;
  177. } //}}}
  178. //{{{ Private members
  179. private char[] text;
  180. private int gapStart;
  181. private int gapEnd;
  182. private int length;
  183. //{{{ moveGapStart() method
  184. private void moveGapStart(int newStart)
  185. {
  186. int newEnd = gapEnd + (newStart - gapStart);
  187. if(newStart == gapStart)
  188. {
  189. // nothing to do
  190. }
  191. else if(newStart > gapStart)
  192. {
  193. System.arraycopy(text,gapEnd,text,gapStart,
  194. newStart - gapStart);
  195. }
  196. else if(newStart < gapStart)
  197. {
  198. System.arraycopy(text,newStart,text,newEnd,
  199. gapStart - newStart);
  200. }
  201. gapStart = newStart;
  202. gapEnd = newEnd;
  203. } //}}}
  204. //{{{ moveGapEnd() method
  205. private void moveGapEnd(int newEnd)
  206. {
  207. System.arraycopy(text,gapEnd,text,newEnd,length - gapStart);
  208. gapEnd = newEnd;
  209. } //}}}
  210. //{{{ ensureCapacity() method
  211. private void ensureCapacity(int capacity)
  212. {
  213. if(capacity >= text.length)
  214. {
  215. char[] textN = new char[capacity * 2];
  216. System.arraycopy(text,0,textN,0,length + (gapEnd - gapStart));
  217. text = textN;
  218. }
  219. } //}}}
  220. //}}}
  221. }