1 7 package javax.swing.text; 8 9 import java.util.Vector ; 10 import java.io.Serializable ; 11 import javax.swing.undo.UndoableEdit ; 12 13 27 abstract class GapVector implements Serializable { 28 29 30 33 public GapVector() { 34 this(10); 35 } 36 37 43 public GapVector(int initialLength) { 44 array = allocateArray(initialLength); 45 g0 = 0; 46 g1 = initialLength; 47 } 48 49 53 protected abstract Object allocateArray(int len); 54 55 58 protected abstract int getArrayLength(); 59 60 64 protected final Object getArray() { 65 return array; 66 } 67 68 71 protected final int getGapStart() { 72 return g0; 73 } 74 75 78 protected final int getGapEnd() { 79 return g1; 80 } 81 82 84 87 private Object array; 88 89 92 private int g0; 93 94 97 private int g1; 98 99 100 102 114 protected void replace(int position, int rmSize, Object addItems, int addSize) { 115 int addOffset = 0; 116 if (addSize == 0) { 117 close(position, rmSize); 118 return; 119 } else if (rmSize > addSize) { 120 121 close(position+addSize, rmSize-addSize); 122 } else { 123 124 int endSize = addSize - rmSize; 125 int end = open(position + rmSize, endSize); 126 System.arraycopy(addItems, rmSize, array, end, endSize); 127 addSize = rmSize; 128 } 129 System.arraycopy(addItems, addOffset, array, position, addSize); 130 } 131 132 139 void close(int position, int nItems) { 140 if (nItems == 0) return; 141 142 int end = position + nItems; 143 int new_gs = (g1 - g0) + nItems; 144 if (end <= g0) { 145 if (g0 != end) { 147 shiftGap(end); 148 } 149 shiftGapStartDown(g0 - nItems); 151 } else if (position >= g0) { 152 if (g0 != position) { 154 shiftGap(position); 155 } 156 shiftGapEndUp(g0 + new_gs); 158 } else { 159 shiftGapStartDown(position); 162 shiftGapEndUp(g0 + new_gs); 163 } 164 } 165 166 172 int open(int position, int nItems) { 173 int gapSize = g1 - g0; 174 if (nItems == 0) { 175 if (position > g0) 176 position += gapSize; 177 return position; 178 } 179 180 shiftGap(position); 182 if (nItems >= gapSize) { 183 shiftEnd(getArrayLength() - gapSize + nItems); 185 gapSize = g1 - g0; 186 } 187 188 g0 = g0 + nItems; 189 return position; 190 } 191 192 196 void resize(int nsize) { 197 Object narray = allocateArray(nsize); 198 System.arraycopy(array, 0, narray, 0, Math.min(nsize, getArrayLength())); 199 array = narray; 200 } 201 202 206 protected void shiftEnd(int newSize) { 207 int oldSize = getArrayLength(); 208 int oldGapEnd = g1; 209 int upperSize = oldSize - oldGapEnd; 210 int arrayLength = getNewArraySize(newSize); 211 int newGapEnd = arrayLength - upperSize; 212 resize(arrayLength); 213 g1 = newGapEnd; 214 215 if (upperSize != 0) { 216 System.arraycopy(array, oldGapEnd, array, newGapEnd, upperSize); 218 } 219 } 220 221 227 int getNewArraySize(int reqSize) { 228 return (reqSize + 1) * 2; 229 } 230 231 237 protected void shiftGap(int newGapStart) { 238 if (newGapStart == g0) { 239 return; 240 } 241 int oldGapStart = g0; 242 int dg = newGapStart - oldGapStart; 243 int oldGapEnd = g1; 244 int newGapEnd = oldGapEnd + dg; 245 int gapSize = oldGapEnd - oldGapStart; 246 247 g0 = newGapStart; 248 g1 = newGapEnd; 249 if (dg > 0) { 250 System.arraycopy(array, oldGapEnd, array, oldGapStart, dg); 252 } else if (dg < 0) { 253 System.arraycopy(array, newGapStart, array, newGapEnd, -dg); 255 } 256 } 257 258 266 protected void shiftGapStartDown(int newGapStart) { 267 g0 = newGapStart; 268 } 269 270 278 protected void shiftGapEndUp(int newGapEnd) { 279 g1 = newGapEnd; 280 } 281 282 } 283 284 285 | Popular Tags |