1. /*
  2. * @(#)GapContent.java 1.21 01/12/03
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing.text;
  8. import java.util.Vector;
  9. import java.io.IOException;
  10. import java.io.ObjectInputStream;
  11. import java.io.Serializable;
  12. import javax.swing.undo.AbstractUndoableEdit;
  13. import javax.swing.undo.CannotRedoException;
  14. import javax.swing.undo.CannotUndoException;
  15. import javax.swing.undo.UndoableEdit;
  16. import javax.swing.SwingUtilities;
  17. import java.lang.ref.WeakReference;
  18. import java.lang.ref.ReferenceQueue;
  19. /**
  20. * An implementation of the AbstractDocument.Content interface
  21. * implemented using a gapped buffer similar to that used by emacs.
  22. * The underlying storage is a array of unicode characters with
  23. * a gap somewhere. The gap is moved to the location of changes
  24. * to take advantage of common behavior where most changes are
  25. * in the same location. Changes that occur at a gap boundary are
  26. * generally cheap and moving the gap is generally cheaper than
  27. * moving the array contents directly to accomodate the change.
  28. * <p>
  29. * The positions tracking change are also generally cheap to
  30. * maintain. The Position implementations (marks) store the array
  31. * index and can easily calculate the sequential position from
  32. * the current gap location. Changes only require update to the
  33. * the marks between the old and new gap boundaries when the gap
  34. * is moved, so generally updating the marks is pretty cheap.
  35. * The marks are stored sorted so they can be located quickly
  36. * with a binary search. This increases the cost of adding a
  37. * mark, and decreases the cost of keeping the mark updated.
  38. *
  39. * @author Timothy Prinzing
  40. * @version 1.21 12/03/01
  41. */
  42. public class GapContent extends GapVector implements AbstractDocument.Content, Serializable {
  43. /**
  44. * Creates a new GapContent object. Initial size defaults to 10.
  45. */
  46. public GapContent() {
  47. this(10);
  48. }
  49. /**
  50. * Creates a new GapContent object, with the initial
  51. * size specified. The initial size will not be allowed
  52. * to go below 2, to give room for the implied break and
  53. * the gap.
  54. *
  55. * @param initialLength the initial size
  56. */
  57. public GapContent(int initialLength) {
  58. super(Math.max(initialLength,2));
  59. char[] implied = new char[1];
  60. implied[0] = '\n';
  61. replace(0, 0, implied, implied.length);
  62. marks = new MarkVector();
  63. search = new MarkData(0);
  64. queue = new ReferenceQueue();
  65. }
  66. /**
  67. * Allocate an array to store items of the type
  68. * appropriate (which is determined by the subclass).
  69. */
  70. protected Object allocateArray(int len) {
  71. return new char[len];
  72. }
  73. /**
  74. * Get the length of the allocated array.
  75. */
  76. protected int getArrayLength() {
  77. char[] carray = (char[]) getArray();
  78. return carray.length;
  79. }
  80. // --- AbstractDocument.Content methods -------------------------
  81. /**
  82. * Returns the length of the content.
  83. *
  84. * @return the length >= 1
  85. * @see AbstractDocument.Content#length
  86. */
  87. public int length() {
  88. int len = getArrayLength() - (getGapEnd() - getGapStart());
  89. return len;
  90. }
  91. /**
  92. * Inserts a string into the content.
  93. *
  94. * @param where the starting position >= 0, < length()
  95. * @param str the non-null string to insert
  96. * @return an UndoableEdit object for undoing
  97. * @exception BadLocationException if the specified position is invalid
  98. * @see AbstractDocument.Content#insertString
  99. */
  100. public UndoableEdit insertString(int where, String str) throws BadLocationException {
  101. if (where >= length() || where < 0) {
  102. throw new BadLocationException("Invalid insert", length());
  103. }
  104. char[] chars = str.toCharArray();
  105. replace(where, 0, chars, chars.length);
  106. return new InsertUndo(where, str.length());
  107. }
  108. /**
  109. * Removes part of the content.
  110. *
  111. * @param where the starting position >= 0, where + nitems < length()
  112. * @param nitems the number of characters to remove >= 0
  113. * @return an UndoableEdit object for undoing
  114. * @exception BadLocationException if the specified position is invalid
  115. * @see AbstractDocument.Content#remove
  116. */
  117. public UndoableEdit remove(int where, int nitems) throws BadLocationException {
  118. if (where + nitems >= length()) {
  119. throw new BadLocationException("Invalid remove", length() + 1);
  120. }
  121. String removedString = getString(where, nitems);
  122. UndoableEdit edit = new RemoveUndo(where, removedString);
  123. replace(where, nitems, empty, 0);
  124. return edit;
  125. }
  126. /**
  127. * Retrieves a portion of the content.
  128. *
  129. * @param where the starting position >= 0
  130. * @param len the length to retrieve >= 0
  131. * @return a string representing the content
  132. * @exception BadLocationException if the specified position is invalid
  133. * @see AbstractDocument.Content#getString
  134. */
  135. public String getString(int where, int len) throws BadLocationException {
  136. Segment s = new Segment();
  137. getChars(where, len, s);
  138. return new String(s.array, s.offset, s.count);
  139. }
  140. /**
  141. * Retrieves a portion of the content. If the desired content spans
  142. * the gap, we copy the content. If the desired content does not
  143. * span the gap, the actual store is returned to avoid the copy since
  144. * it is contiguous.
  145. *
  146. * @param where the starting position >= 0, where + len <= length()
  147. * @param len the number of characters to retrieve >= 0
  148. * @param chars the Segment object to return the characters in
  149. * @exception BadLocationException if the specified position is invalid
  150. * @see AbstractDocument.Content#getChars
  151. */
  152. public void getChars(int where, int len, Segment chars) throws BadLocationException {
  153. int end = where + len;
  154. if (where < 0 || end < 0) {
  155. throw new BadLocationException("Invalid location", -1);
  156. }
  157. if (end > length() || where > length()) {
  158. throw new BadLocationException("Invalid location", length() + 1);
  159. }
  160. int g0 = getGapStart();
  161. int g1 = getGapEnd();
  162. char[] array = (char[]) getArray();
  163. if ((where + len) <= g0) {
  164. // below gap
  165. chars.array = array;
  166. chars.offset = where;
  167. } else if (where >= g0) {
  168. // above gap
  169. chars.array = array;
  170. chars.offset = g1 + where - g0;
  171. } else {
  172. // spans the gap
  173. int before = g0 - where;
  174. if (chars.isPartialReturn()) {
  175. // partial return allowed, return amount before the gap
  176. chars.array = array;
  177. chars.offset = where;
  178. chars.count = before;
  179. return;
  180. }
  181. // partial return not allowed, must copy
  182. chars.array = new char[len];
  183. chars.offset = 0;
  184. System.arraycopy(array, where, chars.array, 0, before);
  185. System.arraycopy(array, g1, chars.array, before, len - before);
  186. }
  187. chars.count = len;
  188. }
  189. /**
  190. * Creates a position within the content that will
  191. * track change as the content is mutated.
  192. *
  193. * @param offset the offset to track >= 0
  194. * @return the position
  195. * @exception BadLocationException if the specified position is invalid
  196. */
  197. public Position createPosition(int offset) throws BadLocationException {
  198. while ( queue.poll() != null ) {
  199. unusedMarks++;
  200. }
  201. if (unusedMarks > Math.max(5, (marks.size() / 10))) {
  202. removeUnusedMarks();
  203. }
  204. int g0 = getGapStart();
  205. int g1 = getGapEnd();
  206. int index = (offset < g0) ? offset : offset + (g1 - g0);
  207. search.index = index;
  208. int sortIndex = findSortIndex(search);
  209. MarkData m;
  210. StickyPosition position;
  211. if (sortIndex < marks.size()
  212. && (m = marks.elementAt(sortIndex)).index == index
  213. && (position = m.getPosition()) != null) {
  214. //position references the correct StickyPostition
  215. } else {
  216. position = new StickyPosition();
  217. m = new MarkData(index,position,queue);
  218. position.setMark(m);
  219. marks.insertElementAt(m, sortIndex);
  220. }
  221. return position;
  222. }
  223. /**
  224. * Holds the data for a mark... separately from
  225. * the real mark so that the real mark (Position
  226. * that the caller of createPosition holds) can be
  227. * collected if there are no more references to
  228. * it. The update table holds only a reference
  229. * to this data.
  230. */
  231. final class MarkData extends WeakReference {
  232. MarkData(int index) {
  233. super(null);
  234. this.index = index;
  235. }
  236. MarkData(int index, StickyPosition position, ReferenceQueue queue) {
  237. super(position, queue);
  238. this.index = index;
  239. }
  240. /**
  241. * Fetch the location in the contiguous sequence
  242. * being modeled. The index in the gap array
  243. * is held by the mark, so it is adjusted according
  244. * to it's relationship to the gap.
  245. */
  246. public final int getOffset() {
  247. int g0 = getGapStart();
  248. int g1 = getGapEnd();
  249. int offs = (index < g0) ? index : index - (g1 - g0);
  250. return Math.max(offs, 0);
  251. }
  252. StickyPosition getPosition() {
  253. return (StickyPosition)get();
  254. }
  255. int index;
  256. }
  257. final class StickyPosition implements Position {
  258. StickyPosition() {
  259. }
  260. void setMark(MarkData mark) {
  261. this.mark = mark;
  262. }
  263. public final int getOffset() {
  264. return mark.getOffset();
  265. }
  266. public String toString() {
  267. return Integer.toString(getOffset());
  268. }
  269. MarkData mark;
  270. }
  271. // --- variables --------------------------------------
  272. private static final char[] empty = new char[0];
  273. private transient MarkVector marks;
  274. /**
  275. * Record used for searching for the place to
  276. * start updating mark indexs when the gap
  277. * boundaries are moved.
  278. */
  279. private transient MarkData search;
  280. /**
  281. * The number of unused mark entries
  282. */
  283. private transient int unusedMarks = 0;
  284. private transient ReferenceQueue queue;
  285. // --- gap management -------------------------------
  286. /**
  287. * Make the gap bigger, moving any necessary data and updating
  288. * the appropriate marks
  289. */
  290. protected void shiftEnd(int newSize) {
  291. int oldGapEnd = getGapEnd();
  292. super.shiftEnd(newSize);
  293. // Adjust marks.
  294. int dg = getGapEnd() - oldGapEnd;
  295. int adjustIndex = findMarkAdjustIndex(oldGapEnd);
  296. int n = marks.size();
  297. for (int i = adjustIndex; i < n; i++) {
  298. MarkData mark = marks.elementAt(i);
  299. mark.index += dg;
  300. }
  301. }
  302. /**
  303. * Move the start of the gap to a new location,
  304. * without changing the size of the gap. This
  305. * moves the data in the array and updates the
  306. * marks accordingly.
  307. */
  308. protected void shiftGap(int newGapStart) {
  309. int oldGapStart = getGapStart();
  310. int dg = newGapStart - oldGapStart;
  311. int oldGapEnd = getGapEnd();
  312. int newGapEnd = oldGapEnd + dg;
  313. int gapSize = oldGapEnd - oldGapStart;
  314. // shift gap in the character array
  315. super.shiftGap(newGapStart);
  316. // update the marks
  317. if (dg > 0) {
  318. // Move gap up, move data and marks down.
  319. int adjustIndex = findMarkAdjustIndex(oldGapStart);
  320. int n = marks.size();
  321. for (int i = adjustIndex; i < n; i++) {
  322. MarkData mark = marks.elementAt(i);
  323. if (mark.index >= newGapEnd) {
  324. break;
  325. }
  326. mark.index -= gapSize;
  327. }
  328. } else if (dg < 0) {
  329. // Move gap down, move data and marks up.
  330. int adjustIndex = findMarkAdjustIndex(newGapStart);
  331. int n = marks.size();
  332. for (int i = adjustIndex; i < n; i++) {
  333. MarkData mark = marks.elementAt(i);
  334. if (mark.index >= oldGapEnd) {
  335. break;
  336. }
  337. mark.index += gapSize;
  338. }
  339. }
  340. resetMarksAtZero();
  341. }
  342. /**
  343. * Resets all the marks that have an offset of 0 to have an index of
  344. * zero as well.
  345. */
  346. protected void resetMarksAtZero() {
  347. if (marks != null && getGapStart() == 0) {
  348. int g1 = getGapEnd();
  349. for (int counter = 0, maxCounter = marks.size();
  350. counter < maxCounter; counter++) {
  351. MarkData mark = marks.elementAt(counter);
  352. if (mark.index <= g1) {
  353. mark.index = 0;
  354. }
  355. else {
  356. break;
  357. }
  358. }
  359. }
  360. }
  361. /**
  362. * Adjust the gap end downward. This doesn't move
  363. * any data, but it does update any marks affected
  364. * by the boundary change. All marks from the old
  365. * gap start down to the new gap start are squeezed
  366. * to the end of the gap (their location has been
  367. * removed).
  368. */
  369. protected void shiftGapStartDown(int newGapStart) {
  370. // Push aside all marks from oldGapStart down to newGapStart.
  371. int adjustIndex = findMarkAdjustIndex(newGapStart);
  372. int n = marks.size();
  373. int g0 = getGapStart();
  374. int g1 = getGapEnd();
  375. for (int i = adjustIndex; i < n; i++) {
  376. MarkData mark = marks.elementAt(i);
  377. if (mark.index > g0) {
  378. // no more marks to adjust
  379. break;
  380. }
  381. mark.index = g1;
  382. }
  383. // shift the gap in the character array
  384. super.shiftGapStartDown(newGapStart);
  385. resetMarksAtZero();
  386. }
  387. /**
  388. * Adjust the gap end upward. This doesn't move
  389. * any data, but it does update any marks affected
  390. * by the boundary change. All marks from the old
  391. * gap end up to the new gap end are squeezed
  392. * to the end of the gap (their location has been
  393. * removed).
  394. */
  395. protected void shiftGapEndUp(int newGapEnd) {
  396. int adjustIndex = findMarkAdjustIndex(getGapEnd());
  397. int n = marks.size();
  398. for (int i = adjustIndex; i < n; i++) {
  399. MarkData mark = marks.elementAt(i);
  400. if (mark.index >= newGapEnd) {
  401. break;
  402. }
  403. mark.index = newGapEnd;
  404. }
  405. // shift the gap in the character array
  406. super.shiftGapEndUp(newGapEnd);
  407. resetMarksAtZero();
  408. }
  409. /**
  410. * Compares two marks.
  411. *
  412. * @param o1 the first object
  413. * @param o2 the second object
  414. * @return < 0 if o1 < o2, 0 if the same, > 0 if o1 > o2
  415. */
  416. final int compare(MarkData o1, MarkData o2) {
  417. if (o1.index < o2.index) {
  418. return -1;
  419. } else if (o1.index > o2.index) {
  420. return 1;
  421. } else {
  422. return 0;
  423. }
  424. }
  425. /**
  426. * Finds the index to start mark adjustments given
  427. * some search index.
  428. */
  429. final int findMarkAdjustIndex(int searchIndex) {
  430. search.index = Math.max(searchIndex, 1);
  431. int index = findSortIndex(search);
  432. // return the first in the series
  433. // (ie. there may be duplicates).
  434. for (int i = index - 1; i >= 0; i--) {
  435. MarkData d = marks.elementAt(i);
  436. if (d.index != search.index) {
  437. break;
  438. }
  439. index -= 1;
  440. }
  441. return index;
  442. }
  443. /**
  444. * Finds the index of where to insert a new mark.
  445. *
  446. * @param o the mark to insert
  447. * @return the index
  448. */
  449. final int findSortIndex(MarkData o) {
  450. int lower = 0;
  451. int upper = marks.size() - 1;
  452. int mid = 0;
  453. if (upper == -1) {
  454. return 0;
  455. }
  456. int cmp = 0;
  457. MarkData last = marks.elementAt(upper);
  458. cmp = compare(o, last);
  459. if (cmp > 0)
  460. return upper + 1;
  461. while (lower <= upper) {
  462. mid = lower + ((upper - lower) / 2);
  463. MarkData entry = marks.elementAt(mid);
  464. cmp = compare(o, entry);
  465. if (cmp == 0) {
  466. // found a match
  467. return mid;
  468. } else if (cmp < 0) {
  469. upper = mid - 1;
  470. } else {
  471. lower = mid + 1;
  472. }
  473. }
  474. // didn't find it, but we indicate the index of where it would belong.
  475. return (cmp < 0) ? mid : mid + 1;
  476. }
  477. /**
  478. * Remove all unused marks out of the sorted collection
  479. * of marks.
  480. */
  481. final void removeUnusedMarks() {
  482. int n = marks.size();
  483. MarkVector cleaned = new MarkVector(n);
  484. for (int i = 0; i < n; i++) {
  485. MarkData mark = marks.elementAt(i);
  486. if (mark.get() != null) {
  487. cleaned.addElement(mark);
  488. }
  489. }
  490. marks = cleaned;
  491. unusedMarks = 0;
  492. }
  493. static class MarkVector extends GapVector {
  494. MarkVector() {
  495. super();
  496. }
  497. MarkVector(int size) {
  498. super(size);
  499. }
  500. /**
  501. * Allocate an array to store items of the type
  502. * appropriate (which is determined by the subclass).
  503. */
  504. protected Object allocateArray(int len) {
  505. return new MarkData[len];
  506. }
  507. /**
  508. * Get the length of the allocated array
  509. */
  510. protected int getArrayLength() {
  511. MarkData[] marks = (MarkData[]) getArray();
  512. return marks.length;
  513. }
  514. /**
  515. * Returns the number of marks currently held
  516. */
  517. public int size() {
  518. int len = getArrayLength() - (getGapEnd() - getGapStart());
  519. return len;
  520. }
  521. /**
  522. * Inserts a mark into the vector
  523. */
  524. public void insertElementAt(MarkData m, int index) {
  525. oneMark[0] = m;
  526. replace(index, 0, oneMark, 1);
  527. }
  528. /**
  529. * Add a mark to the end
  530. */
  531. public void addElement(MarkData m) {
  532. insertElementAt(m, size());
  533. }
  534. /**
  535. * Fetches the mark at the given index
  536. */
  537. public MarkData elementAt(int index) {
  538. int g0 = getGapStart();
  539. int g1 = getGapEnd();
  540. MarkData[] array = (MarkData[]) getArray();
  541. if (index < g0) {
  542. // below gap
  543. return array[index];
  544. } else {
  545. // above gap
  546. index += g1 - g0;
  547. return array[index];
  548. }
  549. }
  550. /**
  551. * Replaces the elements in the specified range with the passed
  552. * in objects. This will NOT adjust the gap. The passed in indices
  553. * do not account for the gap, they are the same as would be used
  554. * int <code>elementAt</code>.
  555. */
  556. protected void replaceRange(int start, int end, Object[] marks) {
  557. int g0 = getGapStart();
  558. int g1 = getGapEnd();
  559. int index = start;
  560. int newIndex = 0;
  561. Object[] array = (Object[]) getArray();
  562. if (start >= g0) {
  563. // Completely passed gap
  564. index += (g1 - g0);
  565. end += (g1 - g0);
  566. }
  567. else if (end >= g0) {
  568. // straddles gap
  569. end += (g1 - g0);
  570. while (index < g0) {
  571. array[index++] = marks[newIndex++];
  572. }
  573. index = g1;
  574. }
  575. else {
  576. // below gap
  577. while (index < end) {
  578. array[index++] = marks[newIndex++];
  579. }
  580. }
  581. while (index < end) {
  582. array[index++] = marks[newIndex++];
  583. }
  584. }
  585. MarkData[] oneMark = new MarkData[1];
  586. }
  587. // --- serialization -------------------------------------
  588. private void readObject(ObjectInputStream s)
  589. throws ClassNotFoundException, IOException {
  590. s.defaultReadObject();
  591. marks = new MarkVector();
  592. search = new MarkData(0);
  593. queue = new ReferenceQueue();
  594. }
  595. // --- undo support --------------------------------------
  596. /**
  597. * Returns a Vector containing instances of UndoPosRef for the
  598. * Positions in the range
  599. * <code>offset</code> to <code>offset</code> + <code>length</code>.
  600. * If <code>v</code> is not null the matching Positions are placed in
  601. * there. The vector with the resulting Positions are returned.
  602. *
  603. * @param v the Vector to use, with a new one created on null
  604. * @param offset the starting offset >= 0
  605. * @param length the length >= 0
  606. * @return the set of instances
  607. */
  608. protected Vector getPositionsInRange(Vector v, int offset, int length) {
  609. int endOffset = offset + length;
  610. int startIndex;
  611. int endIndex;
  612. int g0 = getGapStart();
  613. int g1 = getGapEnd();
  614. // Find the index of the marks.
  615. if (offset < g0) {
  616. if (offset == 0) {
  617. // findMarkAdjustIndex start at 1!
  618. startIndex = 0;
  619. }
  620. else {
  621. startIndex = findMarkAdjustIndex(offset);
  622. }
  623. if (endOffset >= g0) {
  624. endIndex = findMarkAdjustIndex(endOffset + (g1 - g0) + 1);
  625. }
  626. else {
  627. endIndex = findMarkAdjustIndex(endOffset + 1);
  628. }
  629. }
  630. else {
  631. startIndex = findMarkAdjustIndex(offset + (g1 - g0));
  632. endIndex = findMarkAdjustIndex(endOffset + (g1 - g0) + 1);
  633. }
  634. Vector placeIn = (v == null) ? new Vector(Math.max(1, endIndex -
  635. startIndex)) : v;
  636. for (int counter = startIndex; counter < endIndex; counter++) {
  637. placeIn.addElement(new UndoPosRef(marks.elementAt(counter)));
  638. }
  639. return placeIn;
  640. }
  641. /**
  642. * Resets the location for all the UndoPosRef instances
  643. * in <code>positions</code>.
  644. * <p>
  645. * This is meant for internal usage, and is generally not of interest
  646. * to subclasses.
  647. *
  648. * @param positions the UndoPosRef instances to reset
  649. */
  650. protected void updateUndoPositions(Vector positions, int offset,
  651. int length) {
  652. // Find the indexs of the end points.
  653. int endOffset = offset + length;
  654. int g1 = getGapEnd();
  655. int startIndex;
  656. int endIndex = findMarkAdjustIndex(g1 + 1);
  657. if (offset != 0) {
  658. startIndex = findMarkAdjustIndex(g1);
  659. }
  660. else {
  661. startIndex = 0;
  662. }
  663. // Reset the location of the refenences.
  664. for(int counter = positions.size() - 1; counter >= 0; counter--) {
  665. UndoPosRef ref = (UndoPosRef)positions.elementAt(counter);
  666. ref.resetLocation(endOffset, g1);
  667. }
  668. // We have to resort the marks in the range startIndex to endIndex.
  669. // We can take advantage of the fact that it will be in
  670. // increasing order, accept there will be a bunch of MarkData's with
  671. // the index g1 (or 0 if offset == 0) interspersed throughout.
  672. if (startIndex < endIndex) {
  673. Object[] sorted = new Object[endIndex - startIndex];
  674. int addIndex = 0;
  675. int counter;
  676. if (offset == 0) {
  677. // If the offset is 0, the positions won't have incremented,
  678. // have to do the reverse thing.
  679. // Find the elements in startIndex whose index is 0
  680. for (counter = startIndex; counter < endIndex; counter++) {
  681. MarkData mark = marks.elementAt(counter);
  682. if (mark.index == 0) {
  683. sorted[addIndex++] = mark;
  684. }
  685. }
  686. for (counter = startIndex; counter < endIndex; counter++) {
  687. MarkData mark = marks.elementAt(counter);
  688. if (mark.index != 0) {
  689. sorted[addIndex++] = mark;
  690. }
  691. }
  692. }
  693. else {
  694. for (counter = startIndex; counter < endIndex; counter++) {
  695. MarkData mark = marks.elementAt(counter);
  696. if (mark.index != g1) {
  697. sorted[addIndex++] = mark;
  698. }
  699. }
  700. for (counter = startIndex; counter < endIndex; counter++) {
  701. MarkData mark = marks.elementAt(counter);
  702. if (mark.index == g1) {
  703. sorted[addIndex++] = mark;
  704. }
  705. }
  706. }
  707. // And replace
  708. marks.replaceRange(startIndex, endIndex, sorted);
  709. }
  710. }
  711. /**
  712. * Used to hold a reference to a Mark that is being reset as the
  713. * result of removing from the content.
  714. */
  715. final class UndoPosRef {
  716. UndoPosRef(MarkData rec) {
  717. this.rec = rec;
  718. this.undoLocation = rec.getOffset();
  719. }
  720. /**
  721. * Resets the location of the Position to the offset when the
  722. * receiver was instantiated.
  723. *
  724. * @param endOffset end location of inserted string.
  725. * @param g1 resulting end of gap.
  726. */
  727. protected void resetLocation(int endOffset, int g1) {
  728. if (undoLocation != endOffset) {
  729. this.rec.index = undoLocation;
  730. }
  731. else {
  732. this.rec.index = g1;
  733. }
  734. }
  735. /** Previous Offset of rec. */
  736. protected int undoLocation;
  737. /** Mark to reset offset. */
  738. protected MarkData rec;
  739. } // End of GapContent.UndoPosRef
  740. /**
  741. * UnoableEdit created for inserts.
  742. */
  743. class InsertUndo extends AbstractUndoableEdit {
  744. protected InsertUndo(int offset, int length) {
  745. super();
  746. this.offset = offset;
  747. this.length = length;
  748. }
  749. public void undo() throws CannotUndoException {
  750. super.undo();
  751. try {
  752. // Get the Positions in the range being removed.
  753. posRefs = getPositionsInRange(null, offset, length);
  754. string = getString(offset, length);
  755. remove(offset, length);
  756. } catch (BadLocationException bl) {
  757. throw new CannotUndoException();
  758. }
  759. }
  760. public void redo() throws CannotRedoException {
  761. super.redo();
  762. try {
  763. insertString(offset, string);
  764. string = null;
  765. // Update the Positions that were in the range removed.
  766. if(posRefs != null) {
  767. updateUndoPositions(posRefs, offset, length);
  768. posRefs = null;
  769. }
  770. } catch (BadLocationException bl) {
  771. throw new CannotRedoException();
  772. }
  773. }
  774. /** Where string was inserted. */
  775. protected int offset;
  776. /** Length of string inserted. */
  777. protected int length;
  778. /** The string that was inserted. This will only be valid after an
  779. * undo. */
  780. protected String string;
  781. /** An array of instances of UndoPosRef for the Positions in the
  782. * range that was removed, valid after undo. */
  783. protected Vector posRefs;
  784. } // GapContent.InsertUndo
  785. /**
  786. * UndoableEdit created for removes.
  787. */
  788. class RemoveUndo extends AbstractUndoableEdit {
  789. protected RemoveUndo(int offset, String string) {
  790. super();
  791. this.offset = offset;
  792. this.string = string;
  793. this.length = string.length();
  794. posRefs = getPositionsInRange(null, offset, length);
  795. }
  796. public void undo() throws CannotUndoException {
  797. super.undo();
  798. try {
  799. insertString(offset, string);
  800. // Update the Positions that were in the range removed.
  801. if(posRefs != null) {
  802. updateUndoPositions(posRefs, offset, length);
  803. posRefs = null;
  804. }
  805. string = null;
  806. } catch (BadLocationException bl) {
  807. throw new CannotUndoException();
  808. }
  809. }
  810. public void redo() throws CannotRedoException {
  811. super.redo();
  812. try {
  813. string = getString(offset, length);
  814. // Get the Positions in the range being removed.
  815. posRefs = getPositionsInRange(null, offset, length);
  816. remove(offset, length);
  817. } catch (BadLocationException bl) {
  818. throw new CannotRedoException();
  819. }
  820. }
  821. /** Where the string was removed from. */
  822. protected int offset;
  823. /** Length of string removed. */
  824. protected int length;
  825. /** The string that was removed. This is valid when redo is valid. */
  826. protected String string;
  827. /** An array of instances of UndoPosRef for the Positions in the
  828. * range that was removed, valid before undo. */
  829. protected Vector posRefs;
  830. } // GapContent.RemoveUndo
  831. }