632 * Fetches the mark at the given index
633 */
634 public MarkData elementAt(int index) {
635 int g0 = getGapStart();
636 int g1 = getGapEnd();
637 MarkData[] array = (MarkData[]) getArray();
638 if (index < g0) {
639 // below gap
640 return array[index];
641 } else {
642 // above gap
643 index += g1 - g0;
644 return array[index];
645 }
646 }
647
648 /**
649 * Replaces the elements in the specified range with the passed
650 * in objects. This will NOT adjust the gap. The passed in indices
651 * do not account for the gap, they are the same as would be used
652 * int <code>elementAt</code>.
653 */
654 protected void replaceRange(int start, int end, Object[] marks) {
655 int g0 = getGapStart();
656 int g1 = getGapEnd();
657 int index = start;
658 int newIndex = 0;
659 Object[] array = (Object[]) getArray();
660 if (start >= g0) {
661 // Completely passed gap
662 index += (g1 - g0);
663 end += (g1 - g0);
664 }
665 else if (end >= g0) {
666 // straddles gap
667 end += (g1 - g0);
668 while (index < g0) {
669 array[index++] = marks[newIndex++];
670 }
671 index = g1;
672 }
684 MarkData[] oneMark = new MarkData[1];
685
686 }
687
688 // --- serialization -------------------------------------
689
690 private void readObject(ObjectInputStream s)
691 throws ClassNotFoundException, IOException {
692 s.defaultReadObject();
693 marks = new MarkVector();
694 search = new MarkData(0);
695 queue = new ReferenceQueue<StickyPosition>();
696 }
697
698
699 // --- undo support --------------------------------------
700
701 /**
702 * Returns a Vector containing instances of UndoPosRef for the
703 * Positions in the range
704 * <code>offset</code> to <code>offset</code> + <code>length</code>.
705 * If <code>v</code> is not null the matching Positions are placed in
706 * there. The vector with the resulting Positions are returned.
707 *
708 * @param v the Vector to use, with a new one created on null
709 * @param offset the starting offset >= 0
710 * @param length the length >= 0
711 * @return the set of instances
712 */
713 @SuppressWarnings({"rawtypes", "unchecked"}) // UndoPosRef type cannot be exposed
714 protected Vector getPositionsInRange(Vector v,
715 int offset, int length) {
716 int endOffset = offset + length;
717 int startIndex;
718 int endIndex;
719 int g0 = getGapStart();
720 int g1 = getGapEnd();
721
722 // Find the index of the marks.
723 if (offset < g0) {
724 if (offset == 0) {
725 // findMarkAdjustIndex start at 1!
735 endIndex = findMarkAdjustIndex(endOffset + 1);
736 }
737 }
738 else {
739 startIndex = findMarkAdjustIndex(offset + (g1 - g0));
740 endIndex = findMarkAdjustIndex(endOffset + (g1 - g0) + 1);
741 }
742
743 Vector<UndoPosRef> placeIn = (v == null) ?
744 new Vector<>(Math.max(1, endIndex - startIndex)) :
745 v;
746
747 for (int counter = startIndex; counter < endIndex; counter++) {
748 placeIn.addElement(new UndoPosRef(marks.elementAt(counter)));
749 }
750 return placeIn;
751 }
752
753 /**
754 * Resets the location for all the UndoPosRef instances
755 * in <code>positions</code>.
756 * <p>
757 * This is meant for internal usage, and is generally not of interest
758 * to subclasses.
759 *
760 * @param positions the UndoPosRef instances to reset
761 * @param offset where the string was inserted
762 * @param length length of inserted string
763 */
764 @SuppressWarnings("rawtypes") // UndoPosRef type cannot be exposed
765 protected void updateUndoPositions(Vector positions, int offset,
766 int length) {
767 // Find the indexs of the end points.
768 int endOffset = offset + length;
769 int g1 = getGapEnd();
770 int startIndex;
771 int endIndex = findMarkAdjustIndex(g1 + 1);
772
773 if (offset != 0) {
774 startIndex = findMarkAdjustIndex(g1);
775 }
|
632 * Fetches the mark at the given index
633 */
634 public MarkData elementAt(int index) {
635 int g0 = getGapStart();
636 int g1 = getGapEnd();
637 MarkData[] array = (MarkData[]) getArray();
638 if (index < g0) {
639 // below gap
640 return array[index];
641 } else {
642 // above gap
643 index += g1 - g0;
644 return array[index];
645 }
646 }
647
648 /**
649 * Replaces the elements in the specified range with the passed
650 * in objects. This will NOT adjust the gap. The passed in indices
651 * do not account for the gap, they are the same as would be used
652 * int {@code elementAt}.
653 */
654 protected void replaceRange(int start, int end, Object[] marks) {
655 int g0 = getGapStart();
656 int g1 = getGapEnd();
657 int index = start;
658 int newIndex = 0;
659 Object[] array = (Object[]) getArray();
660 if (start >= g0) {
661 // Completely passed gap
662 index += (g1 - g0);
663 end += (g1 - g0);
664 }
665 else if (end >= g0) {
666 // straddles gap
667 end += (g1 - g0);
668 while (index < g0) {
669 array[index++] = marks[newIndex++];
670 }
671 index = g1;
672 }
684 MarkData[] oneMark = new MarkData[1];
685
686 }
687
688 // --- serialization -------------------------------------
689
690 private void readObject(ObjectInputStream s)
691 throws ClassNotFoundException, IOException {
692 s.defaultReadObject();
693 marks = new MarkVector();
694 search = new MarkData(0);
695 queue = new ReferenceQueue<StickyPosition>();
696 }
697
698
699 // --- undo support --------------------------------------
700
701 /**
702 * Returns a Vector containing instances of UndoPosRef for the
703 * Positions in the range
704 * {@code offset} to {@code offset} + {@code length}.
705 * If {@code v} is not null the matching Positions are placed in
706 * there. The vector with the resulting Positions are returned.
707 *
708 * @param v the Vector to use, with a new one created on null
709 * @param offset the starting offset >= 0
710 * @param length the length >= 0
711 * @return the set of instances
712 */
713 @SuppressWarnings({"rawtypes", "unchecked"}) // UndoPosRef type cannot be exposed
714 protected Vector getPositionsInRange(Vector v,
715 int offset, int length) {
716 int endOffset = offset + length;
717 int startIndex;
718 int endIndex;
719 int g0 = getGapStart();
720 int g1 = getGapEnd();
721
722 // Find the index of the marks.
723 if (offset < g0) {
724 if (offset == 0) {
725 // findMarkAdjustIndex start at 1!
735 endIndex = findMarkAdjustIndex(endOffset + 1);
736 }
737 }
738 else {
739 startIndex = findMarkAdjustIndex(offset + (g1 - g0));
740 endIndex = findMarkAdjustIndex(endOffset + (g1 - g0) + 1);
741 }
742
743 Vector<UndoPosRef> placeIn = (v == null) ?
744 new Vector<>(Math.max(1, endIndex - startIndex)) :
745 v;
746
747 for (int counter = startIndex; counter < endIndex; counter++) {
748 placeIn.addElement(new UndoPosRef(marks.elementAt(counter)));
749 }
750 return placeIn;
751 }
752
753 /**
754 * Resets the location for all the UndoPosRef instances
755 * in {@code positions}.
756 * <p>
757 * This is meant for internal usage, and is generally not of interest
758 * to subclasses.
759 *
760 * @param positions the UndoPosRef instances to reset
761 * @param offset where the string was inserted
762 * @param length length of inserted string
763 */
764 @SuppressWarnings("rawtypes") // UndoPosRef type cannot be exposed
765 protected void updateUndoPositions(Vector positions, int offset,
766 int length) {
767 // Find the indexs of the end points.
768 int endOffset = offset + length;
769 int g1 = getGapEnd();
770 int startIndex;
771 int endIndex = findMarkAdjustIndex(g1 + 1);
772
773 if (offset != 0) {
774 startIndex = findMarkAdjustIndex(g1);
775 }
|