Author: rwhitcomb Date: Thu Jan 11 18:53:49 2018 New Revision: 1820918 URL: http://svn.apache.org/viewvc?rev=1820918&view=rev Log: PIVOT-1021: Fix "undo" of a delete of part of a text node in TextPane. * Save the characters to delete from the StringBuilder before the delete. * Add an optional "removedChars" parameter to the "rangeRemoved" methods which is set in this one case where TextNode just removes some chars from its internal text buffer. * Update all the callers / users of "rangeRemoved" to pass this new param. * Add the param to the RangeRemovedEdit in TextPane and implement the "undo" method there using the saved characters that were removed. * Also pass in the current node through this chain (similar to the last change so that the "undo" code can get the real node to act on).
Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/TextPane.java pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/TextPaneSkinNodeView.java pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/TextPaneSkinTextNodeView.java pivot/trunk/wtk/src/org/apache/pivot/wtk/text/Element.java pivot/trunk/wtk/src/org/apache/pivot/wtk/text/Node.java pivot/trunk/wtk/src/org/apache/pivot/wtk/text/NodeListener.java pivot/trunk/wtk/src/org/apache/pivot/wtk/text/TextNode.java Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/TextPane.java URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/TextPane.java?rev=1820918&r1=1820917&r2=1820918&view=diff ============================================================================== --- pivot/trunk/wtk/src/org/apache/pivot/wtk/TextPane.java (original) +++ pivot/trunk/wtk/src/org/apache/pivot/wtk/TextPane.java Thu Jan 11 18:53:49 2018 @@ -142,16 +142,23 @@ public class TextPane extends Container private final Node node; private final int offset; private final int characterCount; + private final CharSequence removedChars; - public RangeRemovedEdit(Node node, int offset, int characterCount) { + public RangeRemovedEdit(Node node, int offset, int characterCount, CharSequence removedChars) { this.node = node; this.offset = offset; this.characterCount = characterCount; + this.removedChars = removedChars; } @Override public void undo() { - // TODO: implement + if (!(node instanceof TextNode)) { + // TODO: can we / should we handle this? + throw new IllegalArgumentException("Undo of removed characters must be to a TextNode."); + } + TextNode textNode = (TextNode)node; + textNode.insertText(removedChars, offset - textNode.getDocumentOffset()); } } @@ -302,7 +309,7 @@ public class TextPane extends Container * @param offset Offset into the document. */ @Override - public void rangeRemoved(Node node, int offset, int characterCount) { + public void rangeRemoved(Node node, int offset, int characterCount, CharSequence removedChars) { // if the end of the selection is in or after the range removed if (selectionStart + selectionLength > offset) { // if the start of the selection is in the range removed @@ -319,9 +326,9 @@ public class TextPane extends Container } } -/* if (!undoingHistory) { - addHistoryItem(new RangeRemovedEdit(node, offset, characterCount)); - } */ + if (!undoingHistory && removedChars != null) { + addHistoryItem(new RangeRemovedEdit(node, offset, characterCount, removedChars)); + } if (!bulkOperation) { textPaneCharacterListeners.charactersRemoved(TextPane.this, offset, characterCount); @@ -335,7 +342,7 @@ public class TextPane extends Container private TextPaneCharacterListenerList textPaneCharacterListeners = new TextPaneCharacterListenerList(); private TextPaneSelectionListenerList textPaneSelectionListeners = new TextPaneSelectionListenerList(); - private static final int MAXIMUM_EDIT_HISTORY_LENGTH = 30; + private static final int MAXIMUM_EDIT_HISTORY_LENGTH = 100; public TextPane() { installSkin(TextPane.class); Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/TextPaneSkinNodeView.java URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/TextPaneSkinNodeView.java?rev=1820918&r1=1820917&r2=1820918&view=diff ============================================================================== --- pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/TextPaneSkinNodeView.java (original) +++ pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/TextPaneSkinNodeView.java Thu Jan 11 18:53:49 2018 @@ -238,7 +238,8 @@ abstract class TextPaneSkinNodeView impl } @Override - public void rangeRemoved(Node nodeArgument, int offset, int characterCount) { + public void rangeRemoved(Node nodeArgument, int offset, int characterCount, + CharSequence removedChars) { // No-op } Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/TextPaneSkinTextNodeView.java URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/TextPaneSkinTextNodeView.java?rev=1820918&r1=1820917&r2=1820918&view=diff ============================================================================== --- pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/TextPaneSkinTextNodeView.java (original) +++ pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/TextPaneSkinTextNodeView.java Thu Jan 11 18:53:49 2018 @@ -104,8 +104,7 @@ class TextPaneSkinTextNodeView extends T return new Dimensions((int)Math.ceil(textLayout.getAdvance()), lineHeight); } // TODO: should this be 0 height? Maybe use average character height - Dimensions zero = new Dimensions(0, 0); - return zero; + return Dimensions.ZERO; } private AttributedStringCharacterIterator getCharIterator(TextNode textNode, int start, int end, Font font) { Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/text/Element.java URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/text/Element.java?rev=1820918&r1=1820917&r2=1820918&view=diff ============================================================================== --- pivot/trunk/wtk/src/org/apache/pivot/wtk/text/Element.java (original) +++ pivot/trunk/wtk/src/org/apache/pivot/wtk/text/Element.java Thu Jan 11 18:53:49 2018 @@ -437,7 +437,7 @@ public abstract class Element extends No } // Notify parent - super.rangeRemoved(offset, removedCharacterCount); + super.rangeRemoved(this, offset, removedCharacterCount, null); super.nodesRemoved(this, removed, offset); // Fire event @@ -542,7 +542,7 @@ public abstract class Element extends No } @Override - protected void rangeRemoved(int offset, int charCount) { + protected void rangeRemoved(Node originalNode, int offset, int charCount, CharSequence removedChars) { this.characterCount -= charCount; // Update the offsets of consecutive nodes, if any @@ -555,7 +555,7 @@ public abstract class Element extends No } } - super.rangeRemoved(offset, charCount); + super.rangeRemoved(originalNode, offset, charCount, removedChars); } @Override Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/text/Node.java URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/text/Node.java?rev=1820918&r1=1820917&r2=1820918&view=diff ============================================================================== --- pivot/trunk/wtk/src/org/apache/pivot/wtk/text/Node.java (original) +++ pivot/trunk/wtk/src/org/apache/pivot/wtk/text/Node.java Thu Jan 11 18:53:49 2018 @@ -64,8 +64,8 @@ public abstract class Node { * @param offset Offset relative to this node. */ @Override - public void rangeRemoved(Node node, int offset, int characterCount) { - forEach(listener -> listener.rangeRemoved(node, offset, characterCount)); + public void rangeRemoved(Node node, int offset, int characterCount, CharSequence removedChars) { + forEach(listener -> listener.rangeRemoved(node, offset, characterCount, removedChars)); } } @@ -229,15 +229,20 @@ public abstract class Node { * Therefore the topmost node will be given the offset into the whole document. * Listeners for this node will just be given the offset relative to this node. * - * @param offsetArgument Offset relative to this node. + * @param node The <em>original</em> node (that is, NOT the parent) where the + * range was removed. + * @param offsetArgument Offset relative to the current node. * @param characterCount Count of characters removed. + * @param removedChars The optional actual characters removed (only in the case + * of direct removal from a text node). */ - protected void rangeRemoved(int offsetArgument, int characterCount) { + protected void rangeRemoved(Node node, int offsetArgument, int characterCount, + CharSequence removedChars) { if (parent != null) { - parent.rangeRemoved(offsetArgument + this.offset, characterCount); + parent.rangeRemoved(node, offsetArgument + this.offset, characterCount, removedChars); } - nodeListeners.rangeRemoved(this, offsetArgument, characterCount); + nodeListeners.rangeRemoved(node, offsetArgument, characterCount, removedChars); } /** @@ -249,7 +254,7 @@ public abstract class Node { * Listeners for this node will just be given the offset relative to this node. * * @param node The <em>original</em> node (that is, NOT the parent) where the - * nodes were removed from. + * nodes were removed. * @param removed The actual sequence of nodes removed from that node. * @param offsetArgument Offset relative to this node. */ Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/text/NodeListener.java URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/text/NodeListener.java?rev=1820918&r1=1820917&r2=1820918&view=diff ============================================================================== --- pivot/trunk/wtk/src/org/apache/pivot/wtk/text/NodeListener.java (original) +++ pivot/trunk/wtk/src/org/apache/pivot/wtk/text/NodeListener.java Thu Jan 11 18:53:49 2018 @@ -49,7 +49,7 @@ public interface NodeListener { } @Override - public void rangeRemoved(Node node, int offset, int characterCount) { + public void rangeRemoved(Node node, int offset, int characterCount, CharSequence removedChars) { // empty block } } @@ -103,6 +103,9 @@ public interface NodeListener { * @param node The node where text was removed. * @param offset Starting offset of the text removal. * @param characterCount Count of characters removed. + * @param removedChars (optional) Actual characters that were removed + * if the removal was directly from a text node, + * otherwise this will be null. */ - public void rangeRemoved(Node node, int offset, int characterCount); + public void rangeRemoved(Node node, int offset, int characterCount, CharSequence removedChars); } Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/text/TextNode.java URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/text/TextNode.java?rev=1820918&r1=1820917&r2=1820918&view=diff ============================================================================== --- pivot/trunk/wtk/src/org/apache/pivot/wtk/text/TextNode.java (original) +++ pivot/trunk/wtk/src/org/apache/pivot/wtk/text/TextNode.java Thu Jan 11 18:53:49 2018 @@ -107,10 +107,12 @@ public final class TextNode extends Node Utils.checkIndexBounds(index, count, 0, characters.length()); if (count > 0) { + // Save the deleted characters for possible undo later + CharSequence removedChars = getCharacters(index, index + count); characters.delete(index, index + count); textNodeListeners.charactersRemoved(this, index, count); - rangeRemoved(index, count); + rangeRemoved(this, index, count, removedChars); } }