PageRenderTime 35ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre4/org/gjt/sp/jedit/buffer/ContentManager.java

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