1 22 23 package org.gjt.sp.jedit.buffer; 24 25 import javax.swing.text.Segment ; 26 27 38 public class ContentManager 39 { 40 public final int getLength() 42 { 43 return length; 44 } 46 public String getText(int start, int len) 48 { 49 if(start >= gapStart) 50 return new String (text,start + gapEnd - gapStart,len); 51 else if(start + len <= gapStart) 52 return new String (text,start,len); 53 else 54 { 55 return new String (text,start,gapStart - start) 56 .concat(new String (text,gapEnd,start + len - gapStart)); 57 } 58 } 60 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 79 System.arraycopy(text,start,seg.array,0,gapStart - start); 81 82 System.arraycopy(text,gapEnd,seg.array,gapStart - start, 84 len + start - gapStart); 85 86 seg.offset = 0; 87 seg.count = len; 88 } 89 } 91 public void insert(int start, String str) 93 { 94 int len = str.length(); 95 moveGapStart(start); 96 if(gapEnd - gapStart < len) 97 { 98 ensureCapacity(length + len + 1024); 99 moveGapEnd(start + len + 1024); 100 } 101 102 str.getChars(0,len,text,start); 103 gapStart += len; 104 length += len; 105 } 107 public void insert(int start, Segment seg) 109 { 110 moveGapStart(start); 111 if(gapEnd - gapStart < seg.count) 112 { 113 ensureCapacity(length + seg.count + 1024); 114 moveGapEnd(start + seg.count + 1024); 115 } 116 117 System.arraycopy(seg.array,seg.offset,text,start,seg.count); 118 gapStart += seg.count; 119 length += seg.count; 120 } 122 public void _setContent(char[] text, int length) 124 { 125 this.text = text; 126 this.gapStart = this.gapEnd = 0; 127 this.length = length; 128 } 130 public void remove(int start, int len) 132 { 133 moveGapStart(start); 134 gapEnd += len; 135 length -= len; 136 } 138 private char[] text; 140 private int gapStart; 141 private int gapEnd; 142 private int length; 143 144 private void moveGapStart(int newStart) 146 { 147 int newEnd = gapEnd + (newStart - gapStart); 148 149 if(newStart == gapStart) 150 { 151 } 153 else if(newStart > gapStart) 154 { 155 System.arraycopy(text,gapEnd,text,gapStart, 156 newStart - gapStart); 157 } 158 else if(newStart < gapStart) 159 { 160 System.arraycopy(text,newStart,text,newEnd, 161 gapStart - newStart); 162 } 163 164 gapStart = newStart; 165 gapEnd = newEnd; 166 } 168 private void moveGapEnd(int newEnd) 170 { 171 System.arraycopy(text,gapEnd,text,newEnd,length - gapStart); 172 gapEnd = newEnd; 173 } 175 private void ensureCapacity(int capacity) 177 { 178 if(capacity >= text.length) 179 { 180 char[] textN = new char[capacity * 2]; 181 System.arraycopy(text,0,textN,0,length + (gapEnd - gapStart)); 182 text = textN; 183 } 184 } 186 } 188 | Popular Tags |