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 protected Vector getPositionsInRange(Vector v, int offset, int length) {
714 int endOffset = offset + length;
715 int startIndex;
716 int endIndex;
717 int g0 = getGapStart();
718 int g1 = getGapEnd();
719
720 // Find the index of the marks.
721 if (offset < g0) {
722 if (offset == 0) {
723 // findMarkAdjustIndex start at 1!
724 startIndex = 0;
725 }
726 else {
727 startIndex = findMarkAdjustIndex(offset);
728 }
729 if (endOffset >= g0) {
730 endIndex = findMarkAdjustIndex(endOffset + (g1 - g0) + 1);
731 }
732 else {
733 endIndex = findMarkAdjustIndex(endOffset + 1);
734 }
735 }
736 else {
737 startIndex = findMarkAdjustIndex(offset + (g1 - g0));
738 endIndex = findMarkAdjustIndex(endOffset + (g1 - g0) + 1);
739 }
740
741 Vector placeIn = (v == null) ? new Vector(Math.max(1, endIndex -
742 startIndex)) : v;
743
744 for (int counter = startIndex; counter < endIndex; counter++) {
745 placeIn.addElement(new UndoPosRef(marks.elementAt(counter)));
746 }
747 return placeIn;
748 }
749
750 /**
751 * Resets the location for all the UndoPosRef instances
752 * in <code>positions</code>.
753 * <p>
754 * This is meant for internal usage, and is generally not of interest
755 * to subclasses.
756 *
757 * @param positions the UndoPosRef instances to reset
758 */
759 protected void updateUndoPositions(Vector positions, int offset,
760 int length) {
761 // Find the indexs of the end points.
762 int endOffset = offset + length;
763 int g1 = getGapEnd();
764 int startIndex;
765 int endIndex = findMarkAdjustIndex(g1 + 1);
766
767 if (offset != 0) {
768 startIndex = findMarkAdjustIndex(g1);
769 }
770 else {
771 startIndex = 0;
772 }
773
774 // Reset the location of the refenences.
775 for(int counter = positions.size() - 1; counter >= 0; counter--) {
776 UndoPosRef ref = (UndoPosRef)positions.elementAt(counter);
777 ref.resetLocation(endOffset, g1);
778 }
779 // We have to resort the marks in the range startIndex to endIndex.
780 // We can take advantage of the fact that it will be in
781 // increasing order, accept there will be a bunch of MarkData's with
782 // the index g1 (or 0 if offset == 0) interspersed throughout.
783 if (startIndex < endIndex) {
784 Object[] sorted = new Object[endIndex - startIndex];
785 int addIndex = 0;
786 int counter;
787 if (offset == 0) {
788 // If the offset is 0, the positions won't have incremented,
789 // have to do the reverse thing.
790 // Find the elements in startIndex whose index is 0
791 for (counter = startIndex; counter < endIndex; counter++) {
792 MarkData mark = marks.elementAt(counter);
793 if (mark.index == 0) {
794 sorted[addIndex++] = mark;
795 }
796 }
883 string = null;
884 // Update the Positions that were in the range removed.
885 if(posRefs != null) {
886 updateUndoPositions(posRefs, offset, length);
887 posRefs = null;
888 }
889 } catch (BadLocationException bl) {
890 throw new CannotRedoException();
891 }
892 }
893
894 /** Where string was inserted. */
895 protected int offset;
896 /** Length of string inserted. */
897 protected int length;
898 /** The string that was inserted. This will only be valid after an
899 * undo. */
900 protected String string;
901 /** An array of instances of UndoPosRef for the Positions in the
902 * range that was removed, valid after undo. */
903 protected Vector posRefs;
904 } // GapContent.InsertUndo
905
906
907 /**
908 * UndoableEdit created for removes.
909 */
910 @SuppressWarnings("serial") // JDK-implementation class
911 class RemoveUndo extends AbstractUndoableEdit {
912 protected RemoveUndo(int offset, String string) {
913 super();
914 this.offset = offset;
915 this.string = string;
916 this.length = string.length();
917 posRefs = getPositionsInRange(null, offset, length);
918 }
919
920 public void undo() throws CannotUndoException {
921 super.undo();
922 try {
923 insertString(offset, string);
935 public void redo() throws CannotRedoException {
936 super.redo();
937 try {
938 string = getString(offset, length);
939 // Get the Positions in the range being removed.
940 posRefs = getPositionsInRange(null, offset, length);
941 remove(offset, length);
942 } catch (BadLocationException bl) {
943 throw new CannotRedoException();
944 }
945 }
946
947 /** Where the string was removed from. */
948 protected int offset;
949 /** Length of string removed. */
950 protected int length;
951 /** The string that was removed. This is valid when redo is valid. */
952 protected String string;
953 /** An array of instances of UndoPosRef for the Positions in the
954 * range that was removed, valid before undo. */
955 protected Vector posRefs;
956 } // GapContent.RemoveUndo
957 }
|
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 protected Vector<UndoPosRef> getPositionsInRange(Vector<UndoPosRef> v,
714 int offset, int length) {
715 int endOffset = offset + length;
716 int startIndex;
717 int endIndex;
718 int g0 = getGapStart();
719 int g1 = getGapEnd();
720
721 // Find the index of the marks.
722 if (offset < g0) {
723 if (offset == 0) {
724 // findMarkAdjustIndex start at 1!
725 startIndex = 0;
726 }
727 else {
728 startIndex = findMarkAdjustIndex(offset);
729 }
730 if (endOffset >= g0) {
731 endIndex = findMarkAdjustIndex(endOffset + (g1 - g0) + 1);
732 }
733 else {
734 endIndex = findMarkAdjustIndex(endOffset + 1);
735 }
736 }
737 else {
738 startIndex = findMarkAdjustIndex(offset + (g1 - g0));
739 endIndex = findMarkAdjustIndex(endOffset + (g1 - g0) + 1);
740 }
741
742 Vector<UndoPosRef> placeIn = (v == null) ?
743 new Vector<>(Math.max(1, endIndex - startIndex)) :
744 v;
745
746 for (int counter = startIndex; counter < endIndex; counter++) {
747 placeIn.addElement(new UndoPosRef(marks.elementAt(counter)));
748 }
749 return placeIn;
750 }
751
752 /**
753 * Resets the location for all the UndoPosRef instances
754 * in <code>positions</code>.
755 * <p>
756 * This is meant for internal usage, and is generally not of interest
757 * to subclasses.
758 *
759 * @param positions the UndoPosRef instances to reset
760 */
761 protected void updateUndoPositions(Vector<UndoPosRef> positions, int offset,
762 int length) {
763 // Find the indexs of the end points.
764 int endOffset = offset + length;
765 int g1 = getGapEnd();
766 int startIndex;
767 int endIndex = findMarkAdjustIndex(g1 + 1);
768
769 if (offset != 0) {
770 startIndex = findMarkAdjustIndex(g1);
771 }
772 else {
773 startIndex = 0;
774 }
775
776 // Reset the location of the refenences.
777 for(int counter = positions.size() - 1; counter >= 0; counter--) {
778 UndoPosRef ref = positions.elementAt(counter);
779 ref.resetLocation(endOffset, g1);
780 }
781 // We have to resort the marks in the range startIndex to endIndex.
782 // We can take advantage of the fact that it will be in
783 // increasing order, accept there will be a bunch of MarkData's with
784 // the index g1 (or 0 if offset == 0) interspersed throughout.
785 if (startIndex < endIndex) {
786 Object[] sorted = new Object[endIndex - startIndex];
787 int addIndex = 0;
788 int counter;
789 if (offset == 0) {
790 // If the offset is 0, the positions won't have incremented,
791 // have to do the reverse thing.
792 // Find the elements in startIndex whose index is 0
793 for (counter = startIndex; counter < endIndex; counter++) {
794 MarkData mark = marks.elementAt(counter);
795 if (mark.index == 0) {
796 sorted[addIndex++] = mark;
797 }
798 }
885 string = null;
886 // Update the Positions that were in the range removed.
887 if(posRefs != null) {
888 updateUndoPositions(posRefs, offset, length);
889 posRefs = null;
890 }
891 } catch (BadLocationException bl) {
892 throw new CannotRedoException();
893 }
894 }
895
896 /** Where string was inserted. */
897 protected int offset;
898 /** Length of string inserted. */
899 protected int length;
900 /** The string that was inserted. This will only be valid after an
901 * undo. */
902 protected String string;
903 /** An array of instances of UndoPosRef for the Positions in the
904 * range that was removed, valid after undo. */
905 protected Vector<UndoPosRef> posRefs;
906 } // GapContent.InsertUndo
907
908
909 /**
910 * UndoableEdit created for removes.
911 */
912 @SuppressWarnings("serial") // JDK-implementation class
913 class RemoveUndo extends AbstractUndoableEdit {
914 protected RemoveUndo(int offset, String string) {
915 super();
916 this.offset = offset;
917 this.string = string;
918 this.length = string.length();
919 posRefs = getPositionsInRange(null, offset, length);
920 }
921
922 public void undo() throws CannotUndoException {
923 super.undo();
924 try {
925 insertString(offset, string);
937 public void redo() throws CannotRedoException {
938 super.redo();
939 try {
940 string = getString(offset, length);
941 // Get the Positions in the range being removed.
942 posRefs = getPositionsInRange(null, offset, length);
943 remove(offset, length);
944 } catch (BadLocationException bl) {
945 throw new CannotRedoException();
946 }
947 }
948
949 /** Where the string was removed from. */
950 protected int offset;
951 /** Length of string removed. */
952 protected int length;
953 /** The string that was removed. This is valid when redo is valid. */
954 protected String string;
955 /** An array of instances of UndoPosRef for the Positions in the
956 * range that was removed, valid before undo. */
957 protected Vector<UndoPosRef> posRefs;
958 } // GapContent.RemoveUndo
959 }
|