PageRenderTime 29ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/org/gjt/sp/jedit/buffer/ContentManager.java

#
Java | 193 lines | 123 code | 22 blank | 48 comment | 15 complexity | 8ca3cd5f5c302497fb227c1401ba0dd4 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. * 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 4227 2002-06-02 05:40:43Z spestov $
  33. * @since jEdit 4.0pre1
  34. */
  35. public class ContentManager
  36. {
  37. //{{{ ContentManager constructor
  38. public ContentManager()
  39. {
  40. text = new char[1024];
  41. } //}}}
  42. //{{{ getLength() method
  43. public final int getLength()
  44. {
  45. return length;
  46. } //}}}
  47. //{{{ getText() method
  48. public String getText(int start, int len)
  49. {
  50. if(start >= gapStart)
  51. return new String(text,start + gapEnd - gapStart,len);
  52. else if(start + len <= gapStart)
  53. return new String(text,start,len);
  54. else
  55. {
  56. return new String(text,start,gapStart - start)
  57. .concat(new String(text,gapEnd,start + len - gapStart));
  58. }
  59. } //}}}
  60. //{{{ getText() method
  61. public void getText(int start, int len, Segment seg)
  62. {
  63. if(start >= gapStart)
  64. {
  65. seg.array = text;
  66. seg.offset = start + gapEnd - gapStart;
  67. seg.count = len;
  68. }
  69. else if(start + len <= gapStart)
  70. {
  71. seg.array = text;
  72. seg.offset = start;
  73. seg.count = len;
  74. }
  75. else
  76. {
  77. seg.array = new char[len];
  78. // copy text before gap
  79. System.arraycopy(text,start,seg.array,0,gapStart - start);
  80. // copy text after gap
  81. System.arraycopy(text,gapEnd,seg.array,gapStart - start,
  82. len + start - gapStart);
  83. seg.offset = 0;
  84. seg.count = len;
  85. }
  86. } //}}}
  87. //{{{ insert() method
  88. public void insert(int start, String str)
  89. {
  90. int len = str.length();
  91. moveGapStart(start);
  92. if(gapEnd - gapStart < len)
  93. {
  94. ensureCapacity(length + len + 1024);
  95. moveGapEnd(start + len + 1024);
  96. }
  97. str.getChars(0,len,text,start);
  98. gapStart += len;
  99. length += len;
  100. } //}}}
  101. //{{{ insert() method
  102. public void insert(int start, Segment seg)
  103. {
  104. moveGapStart(start);
  105. if(gapEnd - gapStart < seg.count)
  106. {
  107. ensureCapacity(length + seg.count + 1024);
  108. moveGapEnd(start + seg.count + 1024);
  109. }
  110. System.arraycopy(seg.array,seg.offset,text,start,seg.count);
  111. gapStart += seg.count;
  112. length += seg.count;
  113. } //}}}
  114. //{{{ _setContent() method
  115. public void _setContent(char[] text, int length)
  116. {
  117. this.text = text;
  118. this.gapStart = this.gapEnd = 0;
  119. this.length = length;
  120. } //}}}
  121. //{{{ remove() method
  122. public void remove(int start, int len)
  123. {
  124. moveGapStart(start);
  125. gapEnd += len;
  126. length -= len;
  127. } //}}}
  128. //{{{ Private members
  129. private char[] text;
  130. private int gapStart;
  131. private int gapEnd;
  132. private int length;
  133. //{{{ moveGapStart() method
  134. private void moveGapStart(int newStart)
  135. {
  136. int newEnd = gapEnd + (newStart - gapStart);
  137. if(newStart == gapStart)
  138. {
  139. // nothing to do
  140. }
  141. else if(newStart > gapStart)
  142. {
  143. System.arraycopy(text,gapEnd,text,gapStart,
  144. newStart - gapStart);
  145. }
  146. else if(newStart < gapStart)
  147. {
  148. System.arraycopy(text,newStart,text,newEnd,
  149. gapStart - newStart);
  150. }
  151. gapStart = newStart;
  152. gapEnd = newEnd;
  153. } //}}}
  154. //{{{ moveGapEnd() method
  155. private void moveGapEnd(int newEnd)
  156. {
  157. System.arraycopy(text,gapEnd,text,newEnd,length - gapStart);
  158. gapEnd = newEnd;
  159. } //}}}
  160. //{{{ ensureCapacity() method
  161. private void ensureCapacity(int capacity)
  162. {
  163. if(capacity >= text.length)
  164. {
  165. char[] textN = new char[capacity * 2];
  166. System.arraycopy(text,0,textN,0,length + (gapEnd - gapStart));
  167. text = textN;
  168. }
  169. } //}}}
  170. //}}}
  171. }