1 3 package gnu.jemacs.swt; 4 5 import gnu.lists.FString; 6 import gnu.lists.GapVector; 7 8 13 public class SwtCharBuffer extends GapVector implements CharSequence 14 { 15 protected FString chars; 16 protected LineOffsets lineOffsets; 17 18 public SwtCharBuffer(int initialSize) 19 { 20 super(new FString(new char[initialSize])); 21 chars = (FString) base; 22 this.lineOffsets = new LineOffsets(initialSize/50); 23 } 24 25 26 public CharSequence subSequence(int start, int end) 27 { 28 return new SubSequence(start, end); 29 } 30 31 class SubSequence implements CharSequence 32 { 33 private int start; 34 private int end; 35 36 public SubSequence(int start, int end) 37 { 38 this.start = start; 39 this.end = end; 40 } 41 42 45 public int length() 46 { 47 return end - start; 48 } 49 50 53 public char charAt(int index) 54 { 55 return SwtCharBuffer.this.charAt(index + start); 56 } 57 58 61 public CharSequence subSequence(int start, int end) 62 { 63 return SwtCharBuffer.this.subSequence(this.start + start, this.start + end); 64 } 65 } 66 67 68 public int pos2offset(int pos) 69 { 70 return pos < gapStart ? pos : pos + gapEnd - gapStart; 71 } 72 73 public int offset2pos(int offset) 74 { 75 return offset <= gapStart ? offset : offset - gapEnd + gapStart; 76 } 77 78 81 protected void gapReserve(int size) 82 { 83 int oldGapSize = gapEnd - gapStart; 84 super.gapReserve(size); 85 int newGapSize = gapEnd - gapStart; 86 if (newGapSize > oldGapSize) { 88 lineOffsets.textRegionMoved(gapStart + oldGapSize, size(), newGapSize - oldGapSize); 89 } 90 } 91 94 protected void shiftGap(int newGapStart) 95 { 96 int oldGapStart = gapStart; 97 super.shiftGap(newGapStart); 98 if (oldGapStart != gapStart) 99 { 100 int regionStart = oldGapStart < newGapStart ? oldGapStart + gapEnd - gapStart : gapStart; 101 int regionEnd = oldGapStart < newGapStart ? gapEnd : oldGapStart; 102 int displacement = oldGapStart < newGapStart ? gapStart - gapEnd : gapEnd - gapStart; 103 lineOffsets.textRegionMoved(regionStart, regionEnd, displacement); 104 105 } 106 } 107 110 public int length() 111 { 112 return size(); 113 } 114 115 118 public char charAt(int index) 119 { 120 char c = chars.charAt(index < gapStart ? index : index + gapEnd - gapStart); 121 return c; 122 } 123 127 public void insert(int where, String str) 128 { 129 gapReserve(where, str.length()); 130 str.getChars(0, str.length(), chars.data, where); 131 gapStart += str.length(); 132 lineOffsets.textInserted(where, str); 133 } 134 135 139 public void delete(int where, int count) 140 { 141 shiftGap(where + count); 142 gapStart -= count; 143 lineOffsets.textDeleted(where, where + count); 144 } 145 146 public void getChars(int start, int end, char[] dest, int destStart) 147 { 148 int startOffset = pos2offset(start); 149 int endOffset = pos2offset(end); 150 int startOffset1 = Math.min(startOffset, gapStart); 152 int len1 = Math.min(endOffset, gapStart) - startOffset1; 153 System.arraycopy(chars.data, startOffset1, dest, destStart, len1); 154 155 int startOffset2 = Math.max(startOffset, gapEnd); 157 int len2 = Math.max(endOffset, gapEnd) - startOffset2; 158 System.arraycopy(chars.data, startOffset2, dest, destStart + len1, len2); 159 } 160 161 public String toString() 162 { 163 StringBuffer sbuf = new StringBuffer (); 164 sbuf.append("Text: {" + size() + ", " + gapStart + ", " + gapEnd); 165 sbuf.append(" ["); 166 for (int i = 0; i < size(); i++) 167 { 168 if (i == gapStart) 169 { 170 sbuf.append("|"); 171 } 172 char c = chars.charAt(i < gapStart ? i : i + gapEnd - gapStart); 173 if (c == '\r') c = 'R'; 174 if (c == '\n') c = 'N'; 175 sbuf.append(c); 176 } 177 sbuf.append("]}\n"); 178 sbuf.append(lineOffsets.toString()); 179 180 return sbuf.toString(); 181 182 183 } 184 185 186 189 protected void show() 190 { 191 System.out.println(this); 192 } 193 194 protected static String printable(String s) 195 { 196 return ">>" + s.replaceAll("\r", "R").replaceAll("\n", "N") + "<<"; 197 } 198 199 200 } 201 | Popular Tags |