API Overview API Index Package Overview Direct link to this page
JDK 1.6
  javax.swing.text. GapContent View Javadoc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937

/*
 * @(#)GapContent.java	1.21 01/12/03
 *
 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package javax.swing.text;

import java.util.Vector;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import javax.swing.undo.AbstractUndoableEdit;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.CannotUndoException;
import javax.swing.undo.UndoableEdit;
import javax.swing.SwingUtilities;
import java.lang.ref.WeakReference;
import java.lang.ref.ReferenceQueue;

/**
 * An implementation of the AbstractDocument.Content interface 
 * implemented using a gapped buffer similar to that used by emacs.
 * The underlying storage is a array of unicode characters with
 * a gap somewhere.  The gap is moved to the location of changes
 * to take advantage of common behavior where most changes are
 * in the same location.  Changes that occur at a gap boundary are
 * generally cheap and moving the gap is generally cheaper than
 * moving the array contents directly to accomodate the change.
 * <p>
 * The positions tracking change are also generally cheap to
 * maintain.  The Position implementations (marks) store the array
 * index and can easily calculate the sequential position from
 * the current gap location.  Changes only require update to the
 * the marks between the old and new gap boundaries when the gap
 * is moved, so generally updating the marks is pretty cheap.
 * The marks are stored sorted so they can be located quickly
 * with a binary search.  This increases the cost of adding a
 * mark, and decreases the cost of keeping the mark updated.
 *
 * @author  Timothy Prinzing
 * @version 1.21 12/03/01
 */
public class GapContent extends GapVector implements AbstractDocument.Content, Serializable {

    /**
     * Creates a new GapContent object.  Initial size defaults to 10.
     */
    public GapContent() {
	this(10);
    }

    /**
     * Creates a new GapContent object, with the initial
     * size specified.  The initial size will not be allowed
     * to go below 2, to give room for the implied break and
     * the gap.
     *
     * @param initialLength the initial size
     */
    public GapContent(int initialLength) {
	super(Math.max(initialLength,2));
	char[] implied = new char[1];
	implied[0] = '\n';
	replace(0, 0, implied, implied.length);

	marks = new MarkVector();
	search = new MarkData(0);
	queue = new ReferenceQueue();
    }
    
    /**
     * Allocate an array to store items of the type
     * appropriate (which is determined by the subclass).
     */
    protected Object allocateArray(int len) {
	return new char[len];
    }
	
    /**
     * Get the length of the allocated array.
     */
    protected int getArrayLength() {
	char[] carray = (char[]) getArray();
	return carray.length;
    }

    // --- AbstractDocument.Content methods -------------------------

    /**
     * Returns the length of the content.
     *
     * @return the length >= 1
     * @see AbstractDocument.Content#length
     */
    public int length() {
	int len = getArrayLength() - (getGapEnd() - getGapStart());
	return len;
    }

    /**
     * Inserts a string into the content.
     *
     * @param where the starting position >= 0, < length()
     * @param str the non-null string to insert
     * @return an UndoableEdit object for undoing
     * @exception BadLocationException if the specified position is invalid
     * @see AbstractDocument.Content#insertString
     */
    public UndoableEdit insertString(int where, String str) throws BadLocationException {
	if (where > length() || where < 0) {
	    throw new BadLocationException("Invalid insert", length());
	}
	char[] chars = str.toCharArray();
	replace(where, 0, chars, chars.length);
	return new InsertUndo(where, str.length());
    }

    /**
     * Removes part of the content.
     *
     * @param where the starting position >= 0, where + nitems < length()
     * @param nitems the number of characters to remove >= 0
     * @return an UndoableEdit object for undoing
     * @exception BadLocationException if the specified position is invalid
     * @see AbstractDocument.Content#remove
     */
    public UndoableEdit remove(int where, int nitems) throws BadLocationException {
	if (where + nitems >= length()) {
	    throw new BadLocationException("Invalid remove", length() + 1);
	}
	String removedString = getString(where, nitems);
	UndoableEdit edit = new RemoveUndo(where, removedString);
	replace(where, nitems, empty, 0);
	return edit;
	
    }

    /**
     * Retrieves a portion of the content.
     *
     * @param where the starting position >= 0
     * @param len the length to retrieve >= 0
     * @return a string representing the content
     * @exception BadLocationException if the specified position is invalid
     * @see AbstractDocument.Content#getString
     */
    public String getString(int where, int len) throws BadLocationException {
	Segment s = new Segment();
	getChars(where, len, s);
	return new String(s.array, s.offset, s.count);
    }

    /**
     * Retrieves a portion of the content.  If the desired content spans
     * the gap, we copy the content.  If the desired content does not
     * span the gap, the actual store is returned to avoid the copy since
     * it is contiguous.
     *
     * @param where the starting position >= 0, where + len <= length()
     * @param len the number of characters to retrieve >= 0
     * @param chars the Segment object to return the characters in
     * @exception BadLocationException if the specified position is invalid
     * @see AbstractDocument.Content#getChars
     */
    public void getChars(int where, int len, Segment chars) throws BadLocationException {
	int end = where + len;
	if (where < 0 || end < 0) {
	    throw new BadLocationException("Invalid location", -1);
	}
	if (end > length() || where > length()) {
	    throw new BadLocationException("Invalid location", length() + 1);
	}
	int g0 = getGapStart();
	int g1 = getGapEnd();
	char[] array = (char[]) getArray();
	if ((where + len) <= g0) {
	    // below gap
	    chars.array = array;
	    chars.offset = where;
	} else if (where >= g0) {
	    // above gap
	    chars.array = array;
	    chars.offset = g1 + where - g0;
	} else {
	    // spans the gap
	    int before = g0 - where;
	    if (chars.isPartialReturn()) {
		// partial return allowed, return amount before the gap
		chars.array = array;
		chars.offset = where;
		chars.count = before;
		return;
	    }
	    // partial return not allowed, must copy
	    chars.array = new char[len];
	    chars.offset = 0;
	    System.arraycopy(array, where, chars.array, 0, before);
	    System.arraycopy(array, g1, chars.array, before, len - before);
	}
	chars.count = len;
    }

    /**
     * Creates a position within the content that will
     * track change as the content is mutated.
     *
     * @param offset the offset to track >= 0
     * @return the position
     * @exception BadLocationException if the specified position is invalid
     */
    public Position createPosition(int offset) throws BadLocationException {
	while ( queue.poll() != null ) {
	    unusedMarks++;
	}
	if (unusedMarks > Math.max(5, (marks.size() / 10))) {
	    removeUnusedMarks();
	}
	int g0 = getGapStart();
	int g1 = getGapEnd();
	int index = (offset < g0) ? offset : offset + (g1 - g0);
	search.index = index;
	int sortIndex = findSortIndex(search);
	MarkData m;
	StickyPosition position;
	if (sortIndex < marks.size()
	    && (m = marks.elementAt(sortIndex)).index == index
	    && (position = m.getPosition()) != null) {
	    //position references the correct StickyPostition
	} else {
	    position = new StickyPosition();
	    m = new MarkData(index,position,queue);
	    position.setMark(m);
	    marks.insertElementAt(m, sortIndex);
	}

	return position;
    }

    /**
     * Holds the data for a mark... separately from
     * the real mark so that the real mark (Position
     * that the caller of createPosition holds) can be 
     * collected if there are no more references to
     * it.  The update table holds only a reference
     * to this data.
     */
    final class MarkData extends WeakReference {

	MarkData(int index) {
	    super(null);
	    this.index = index;
	}
	MarkData(int index, StickyPosition position, ReferenceQueue queue) {
	    super(position, queue);
	    this.index = index;
	}

	/**
	 * Fetch the location in the contiguous sequence
	 * being modeled.  The index in the gap array
	 * is held by the mark, so it is adjusted according
	 * to it's relationship to the gap.
	 */
        public final int getOffset() {
	    int g0 = getGapStart();
	    int g1 = getGapEnd();
	    int offs = (index < g0) ? index : index - (g1 - g0);
	    return Math.max(offs, 0);
	}
	
	StickyPosition getPosition() {
	    return (StickyPosition)get();
	}
	int index;
    }

    final class StickyPosition implements Position {

	StickyPosition() {
	}

	void setMark(MarkData mark) {
	    this.mark = mark;
	}

        public final int getOffset() {
	    return mark.getOffset();
	}

        public String toString() {
	    return Integer.toString(getOffset());
	}

	MarkData mark;
    }

    // --- variables --------------------------------------

    private static final char[] empty = new char[0];
    private transient MarkVector marks;

    /**
     * Record used for searching for the place to
     * start updating mark indexs when the gap 
     * boundaries are moved.
     */
    private transient MarkData search;

    /**
     * The number of unused mark entries
     */
    private transient int unusedMarks = 0;

    private transient ReferenceQueue queue;

    final static int GROWTH_SIZE = 1024 * 512;

    // --- gap management -------------------------------

    /**
     * Make the gap bigger, moving any necessary data and updating 
     * the appropriate marks
     */
    protected void shiftEnd(int newSize) {
	int oldGapEnd = getGapEnd();

	super.shiftEnd(newSize);

	// Adjust marks.
	int dg = getGapEnd() - oldGapEnd;
	int adjustIndex = findMarkAdjustIndex(oldGapEnd);
	int n = marks.size();
	for (int i = adjustIndex; i < n; i++) {
	    MarkData mark = marks.elementAt(i);
	    mark.index += dg;
	}
    }

    /**
     * Overridden to make growth policy less agressive for large
     * text amount.
     */
    int getNewArraySize(int reqSize) {
        if (reqSize < GROWTH_SIZE) {
            return super.getNewArraySize(reqSize);
        } else {
            return reqSize + GROWTH_SIZE;
        }
    }

    /**
     * Move the start of the gap to a new location,
     * without changing the size of the gap.  This 
     * moves the data in the array and updates the
     * marks accordingly.
     */
    protected void shiftGap(int newGapStart) {
	int oldGapStart = getGapStart();
	int dg = newGapStart - oldGapStart;
	int oldGapEnd = getGapEnd();
	int newGapEnd = oldGapEnd + dg;
	int gapSize = oldGapEnd - oldGapStart;

	// shift gap in the character array
	super.shiftGap(newGapStart);

	// update the marks
	if (dg > 0) {
	    // Move gap up, move data and marks down.
	    int adjustIndex = findMarkAdjustIndex(oldGapStart);
	    int n = marks.size();
	    for (int i = adjustIndex; i < n; i++) {
		MarkData mark = marks.elementAt(i);
		if (mark.index >= newGapEnd) {
		    break;
		}
		mark.index -= gapSize;
	    }
	} else if (dg < 0) {
	    // Move gap down, move data and marks up.
	    int adjustIndex = findMarkAdjustIndex(newGapStart);
	    int n = marks.size();
	    for (int i = adjustIndex; i < n; i++) {
		MarkData mark = marks.elementAt(i);
		if (mark.index >= oldGapEnd) {
		    break;
		}
		mark.index += gapSize;
	    }
	}
	resetMarksAtZero();
    }

    /**
     * Resets all the marks that have an offset of 0 to have an index of
     * zero as well.
     */
    protected void resetMarksAtZero() {
	if (marks != null && getGapStart() == 0) {
	    int g1 = getGapEnd();
	    for (int counter = 0, maxCounter = marks.size();
		 counter < maxCounter; counter++) {
		MarkData mark = marks.elementAt(counter);
		if (mark.index <= g1) {
		    mark.index = 0;
		}
		else {
		    break;
		}
	    }
	}
    }

    /**
     * Adjust the gap end downward.  This doesn't move
     * any data, but it does update any marks affected 
     * by the boundary change.  All marks from the old
     * gap start down to the new gap start are squeezed
     * to the end of the gap (their location has been
     * removed).
     */
    protected void shiftGapStartDown(int newGapStart) {
	// Push aside all marks from oldGapStart down to newGapStart.
	int adjustIndex = findMarkAdjustIndex(newGapStart);
	int n = marks.size();
	int g0 = getGapStart();
	int g1 = getGapEnd();
	for (int i = adjustIndex; i < n; i++) {
	    MarkData mark = marks.elementAt(i);
	    if (mark.index > g0) {
		// no more marks to adjust
		break;
	    }
	    mark.index = g1;
	}

	// shift the gap in the character array
	super.shiftGapStartDown(newGapStart);

	resetMarksAtZero();
    }

    /**
     * Adjust the gap end upward.  This doesn't move
     * any data, but it does update any marks affected 
     * by the boundary change. All marks from the old
     * gap end up to the new gap end are squeezed
     * to the end of the gap (their location has been
     * removed).
     */
    protected void shiftGapEndUp(int newGapEnd) {
	int adjustIndex = findMarkAdjustIndex(getGapEnd());
	int n = marks.size();
	for (int i = adjustIndex; i < n; i++) {
	    MarkData mark = marks.elementAt(i);
	    if (mark.index >= newGapEnd) {
		break;
	    }
	    mark.index = newGapEnd;
	}

	// shift the gap in the character array
	super.shiftGapEndUp(newGapEnd);

	resetMarksAtZero();
    }

    /**
     * Compares two marks.
     *
     * @param o1 the first object
     * @param o2 the second object
     * @return < 0 if o1 < o2, 0 if the same, > 0 if o1 > o2
     */
    final int compare(MarkData o1, MarkData o2) {
	if (o1.index < o2.index) {
	    return -1;
	} else if (o1.index > o2.index) {
	    return 1;
	} else {
	    return 0;
	}
    }

    /**
     * Finds the index to start mark adjustments given
     * some search index.
     */
    final int findMarkAdjustIndex(int searchIndex) {
	search.index = Math.max(searchIndex, 1);
	int index = findSortIndex(search);

	// return the first in the series
	// (ie. there may be duplicates).
	for (int i = index - 1; i >= 0; i--) {
	    MarkData d = marks.elementAt(i);
	    if (d.index != search.index) {
		break;
	    }
	    index -= 1;
	}
	return index;
    }

    /**
     * Finds the index of where to insert a new mark.
     *
     * @param o the mark to insert
     * @return the index
     */
    final int findSortIndex(MarkData o) {
	int lower = 0; 
	int upper = marks.size() - 1;
	int mid = 0;
	
	if (upper == -1) {
	    return 0;
	}

	int cmp = 0;
	MarkData last = marks.elementAt(upper);
	cmp = compare(o, last);
	if (cmp > 0)
	    return upper + 1;
	
	while (lower <= upper) {
	    mid = lower + ((upper - lower) / 2);
	    MarkData entry = marks.elementAt(mid);
	    cmp = compare(o, entry);

	    if (cmp == 0) {
		// found a match
		return mid;
	    } else if (cmp < 0) {        
		upper = mid - 1;
	    } else {
		lower = mid + 1;
	    }
	}

	// didn't find it, but we indicate the index of where it would belong.
	return (cmp < 0) ? mid : mid + 1;
    }

    /**
     * Remove all unused marks out of the sorted collection
     * of marks.  
     */
    final void removeUnusedMarks() {
	int n = marks.size();
	MarkVector cleaned = new MarkVector(n);
	for (int i = 0; i < n; i++) {
	    MarkData mark = marks.elementAt(i);
	    if (mark.get() != null) {
		cleaned.addElement(mark);
	    }
	}
	marks = cleaned;
	unusedMarks = 0;
    }


    static class MarkVector extends GapVector {

	MarkVector() {
	    super();
	}

	MarkVector(int size) {
	    super(size);
	}

	/**
	 * Allocate an array to store items of the type
	 * appropriate (which is determined by the subclass).
	 */
        protected Object allocateArray(int len) {
	    return new MarkData[len];
	}
	
	/**
	 * Get the length of the allocated array
	 */
        protected int getArrayLength() {
	    MarkData[] marks = (MarkData[]) getArray();
	    return marks.length;
	}

	/**
	 * Returns the number of marks currently held
	 */
        public int size() {
	    int len = getArrayLength() - (getGapEnd() - getGapStart());
	    return len;
	}

	/**
	 * Inserts a mark into the vector
	 */
	public void insertElementAt(MarkData m, int index) {
	    oneMark[0] = m;
	    replace(index, 0, oneMark, 1);
	}

	/**
	 * Add a mark to the end
	 */
	public void addElement(MarkData m) {
	    insertElementAt(m, size());
	}

	/**
	 * Fetches the mark at the given index
	 */
	public MarkData elementAt(int index) {
	    int g0 = getGapStart();
	    int g1 = getGapEnd();
	    MarkData[] array = (MarkData[]) getArray();
	    if (index < g0) {
		// below gap
		return array[index];
	    } else {
		// above gap
		index += g1 - g0;
		return array[index];
	    }
	}

	/**
	 * Replaces the elements in the specified range with the passed
	 * in objects. This will NOT adjust the gap. The passed in indices
	 * do not account for the gap, they are the same as would be used
	 * int <code>elementAt</code>.
	 */
	protected void replaceRange(int start, int end, Object[] marks) {
	    int g0 = getGapStart();
	    int g1 = getGapEnd();
	    int index = start;
	    int newIndex = 0;
	    Object[] array = (Object[]) getArray();
	    if (start >= g0) {
		// Completely passed gap
		index += (g1 - g0);
		end += (g1 - g0);
	    }
	    else if (end >= g0) {
		// straddles gap
		end += (g1 - g0);
		while (index < g0) {
		    array[index++] = marks[newIndex++];
		}
		index = g1;
	    }
	    else {
		// below gap
		while (index < end) {
		    array[index++] = marks[newIndex++];
		}
	    }
	    while (index < end) {
		array[index++] = marks[newIndex++];
	    }
	}

	MarkData[] oneMark = new MarkData[1];

    }

    // --- serialization -------------------------------------

    private void readObject(ObjectInputStream s)
      throws ClassNotFoundException, IOException {
        s.defaultReadObject();
	marks = new MarkVector();
	search = new MarkData(0);
        queue = new ReferenceQueue();
    }


    // --- undo support --------------------------------------

    /**
     * Returns a Vector containing instances of UndoPosRef for the
     * Positions in the range
     * <code>offset</code> to <code>offset</code> + <code>length</code>.
     * If <code>v</code> is not null the matching Positions are placed in
     * there. The vector with the resulting Positions are returned.
     *
     * @param v the Vector to use, with a new one created on null
     * @param offset the starting offset >= 0
     * @param length the length >= 0
     * @return the set of instances
     */
    protected Vector getPositionsInRange(Vector v, int offset, int length) {
	int endOffset = offset + length;
	int startIndex;
	int endIndex;
	int g0 = getGapStart();
	int g1 = getGapEnd();

	// Find the index of the marks.
	if (offset < g0) {
	    if (offset == 0) {
		// findMarkAdjustIndex start at 1!
		startIndex = 0;
	    }
	    else {
		startIndex = findMarkAdjustIndex(offset);
	    }
	    if (endOffset >= g0) {
		endIndex = findMarkAdjustIndex(endOffset + (g1 - g0) + 1);
	    }
	    else {
		endIndex = findMarkAdjustIndex(endOffset + 1);
	    }
	}
	else {
	    startIndex = findMarkAdjustIndex(offset + (g1 - g0));
	    endIndex = findMarkAdjustIndex(endOffset + (g1 - g0) + 1);
	}

	Vector placeIn = (v == null) ? new Vector(Math.max(1, endIndex -
							   startIndex)) : v;

	for (int counter = startIndex; counter < endIndex; counter++) {
	    placeIn.addElement(new UndoPosRef(marks.elementAt(counter)));
	}
	return placeIn;
    }

    /**
     * Resets the location for all the UndoPosRef instances
     * in <code>positions</code>.
     * <p>
     * This is meant for internal usage, and is generally not of interest
     * to subclasses.
     *
     * @param positions the UndoPosRef instances to reset
     */
    protected void updateUndoPositions(Vector positions, int offset,
				       int length) {
	// Find the indexs of the end points.
	int endOffset = offset + length;
	int g1 = getGapEnd();
	int startIndex;
	int endIndex = findMarkAdjustIndex(g1 + 1);

	if (offset != 0) {
	    startIndex = findMarkAdjustIndex(g1);
	}
	else {
	    startIndex = 0;
	}

	// Reset the location of the refenences.
	for(int counter = positions.size() - 1; counter >= 0; counter--) {
	    UndoPosRef ref = (UndoPosRef)positions.elementAt(counter);
	    ref.resetLocation(endOffset, g1);
	}
	// We have to resort the marks in the range startIndex to endIndex.
	// We can take advantage of the fact that it will be in
	// increasing order, accept there will be a bunch of MarkData's with
	// the index g1 (or 0 if offset == 0) interspersed throughout.
	if (startIndex < endIndex) {
	    Object[] sorted = new Object[endIndex - startIndex];
	    int addIndex = 0;
	    int counter;
	    if (offset == 0) {
		// If the offset is 0, the positions won't have incremented,
		// have to do the reverse thing.
		// Find the elements in startIndex whose index is 0
		for (counter = startIndex; counter < endIndex; counter++) {
		    MarkData mark = marks.elementAt(counter);
		    if (mark.index == 0) {
			sorted[addIndex++] = mark;
		    }
		}
		for (counter = startIndex; counter < endIndex; counter++) {
		    MarkData mark = marks.elementAt(counter);
		    if (mark.index != 0) {
			sorted[addIndex++] = mark;
		    }
		}
	    }
	    else {
		for (counter = startIndex; counter < endIndex; counter++) {
		    MarkData mark = marks.elementAt(counter);
		    if (mark.index != g1) {
			sorted[addIndex++] = mark;
		    }
		}
		for (counter = startIndex; counter < endIndex; counter++) {
		    MarkData mark = marks.elementAt(counter);
		    if (mark.index == g1) {
			sorted[addIndex++] = mark;
		    }
		}
	    }
	    // And replace
	    marks.replaceRange(startIndex, endIndex, sorted);
	}
    }

    /**
     * Used to hold a reference to a Mark that is being reset as the
     * result of removing from the content.
     */
    final class UndoPosRef {
	UndoPosRef(MarkData rec) {
	    this.rec = rec;
	    this.undoLocation = rec.getOffset();
	}

	/**
	 * Resets the location of the Position to the offset when the
	 * receiver was instantiated.
	 *
	 * @param endOffset end location of inserted string.
	 * @param g1 resulting end of gap.
	 */
	protected void resetLocation(int endOffset, int g1) {
	    if (undoLocation != endOffset) {
		this.rec.index = undoLocation;
	    }
	    else {
		this.rec.index = g1;
	    }
	}

	/** Previous Offset of rec. */
	protected int undoLocation;
	/** Mark to reset offset. */
	protected MarkData rec;
    } // End of GapContent.UndoPosRef


    /**
     * UnoableEdit created for inserts.
     */
    class InsertUndo extends AbstractUndoableEdit {
	protected InsertUndo(int offset, int length) {
	    super();
	    this.offset = offset;
	    this.length = length;
	}

	public void undo() throws CannotUndoException {
	    super.undo();
	    try {
		// Get the Positions in the range being removed.
		posRefs = getPositionsInRange(null, offset, length);
		string = getString(offset, length);
		remove(offset, length);
	    } catch (BadLocationException bl) {
	      throw new CannotUndoException();
	    }
	}

	public void redo() throws CannotRedoException {
	    super.redo();
	    try {
		insertString(offset, string);
		string = null;
		// Update the Positions that were in the range removed.
		if(posRefs != null) {
		    updateUndoPositions(posRefs, offset, length);
		    posRefs = null;
		}
	    } catch (BadLocationException bl) {
		throw new CannotRedoException();
	    }
	}

	/** Where string was inserted. */
	protected int offset;
	/** Length of string inserted. */
	protected int length;
	/** The string that was inserted. This will only be valid after an
	 * undo. */
	protected String string;
	/** An array of instances of UndoPosRef for the Positions in the
	 * range that was removed, valid after undo. */
	protected Vector posRefs;
    } // GapContent.InsertUndo


    /**
     * UndoableEdit created for removes.
     */
    class RemoveUndo extends AbstractUndoableEdit {
	protected RemoveUndo(int offset, String string) {
	    super();
	    this.offset = offset;
	    this.string = string;
	    this.length = string.length();
	    posRefs = getPositionsInRange(null, offset, length);
	}

	public void undo() throws CannotUndoException {
	    super.undo();
	    try {
		insertString(offset, string);
		// Update the Positions that were in the range removed.
		if(posRefs != null) {
		    updateUndoPositions(posRefs, offset, length);
		    posRefs = null;
		}
		string = null;
	    } catch (BadLocationException bl) {
	      throw new CannotUndoException();
	    }
	}

	public void redo() throws CannotRedoException {
	    super.redo();
	    try {
		string = getString(offset, length);
		// Get the Positions in the range being removed.
		posRefs = getPositionsInRange(null, offset, length);
		remove(offset, length);
	    } catch (BadLocationException bl) {
	      throw new CannotRedoException();
	    }
	}

	/** Where the string was removed from. */
	protected int offset;
	/** Length of string removed. */
	protected int length;
	/** The string that was removed. This is valid when redo is valid. */
	protected String string;
	/** An array of instances of UndoPosRef for the Positions in the
	 * range that was removed, valid before undo. */
	protected Vector posRefs;
    } // GapContent.RemoveUndo
}

Generated By: JavaOnTracks Doclet 0.1.4     ©Thibaut Colar