You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by no...@apache.org on 2011/03/16 09:41:34 UTC

svn commit: r1082094 - in /pivot/trunk/wtk/src/org/apache/pivot/wtk: TextPane.java text/Element.java text/Span.java

Author: noelgrandin
Date: Wed Mar 16 08:41:34 2011
New Revision: 1082094

URL: http://svn.apache.org/viewvc?rev=1082094&view=rev
Log:
PIVOT-716 Clearing a Document by removing all elements crashes

Modified:
    pivot/trunk/wtk/src/org/apache/pivot/wtk/TextPane.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/text/Element.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/text/Span.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=1082094&r1=1082093&r2=1082094&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/TextPane.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/TextPane.java Wed Mar 16 08:41:34 2011
@@ -114,16 +114,20 @@ public class TextPane extends Container 
     private class RangeRemovedEdit implements Edit {
         private final Node node;
         private final int offset;
-        private final Node range;
+        private final Sequence<Node> removed;
 
-        public RangeRemovedEdit(Node node, int offset, int characterCount) {
+        public RangeRemovedEdit(Node node, Sequence<Node> removed, int offset) {
             this.node = node;
             this.offset = offset;
-            this.range = node.getRange(offset, characterCount);
+            this.removed = removed;
         }
 
         public void undo() {
-            node.insertRange(range, offset);
+            Document tmp = new Document();
+            for (int i=0; i<removed.getLength(); i++) {
+                tmp.add(removed.get(i));
+            }
+            node.insertRange(tmp, offset);
         }
     }
 
@@ -234,6 +238,10 @@ public class TextPane extends Container 
                     TextPane.super.remove(componentNode.getComponent());
                 }
             }
+
+            if (!undoingHistory) {
+                addHistoryItem(new RangeRemovedEdit(node, removed, offset));
+            }
         }
 
         @Override
@@ -264,10 +272,6 @@ public class TextPane extends Container 
                 }
             }
 
-            if (!undoingHistory) {
-                addHistoryItem(new RangeRemovedEdit(node, offset, characterCount));
-            }
-
             textPaneCharacterListeners.charactersRemoved(TextPane.this, offset, characterCount);
         }
     };
@@ -367,8 +371,7 @@ public class TextPane extends Container 
             throw new IllegalArgumentException("text is null.");
         }
 
-        if (document == null
-            || document.getCharacterCount() == 0) {
+        if (document == null) {
             throw new IllegalStateException();
         }
 
@@ -376,38 +379,45 @@ public class TextPane extends Container 
             delete(false);
         }
 
-        Node descendant = document.getDescendantAt(selectionStart);
-        int offset = selectionStart - descendant.getDocumentOffset();
+        if (document.getCharacterCount() == 0) {
+            // the document is currently empty
+            Paragraph paragraph = new Paragraph();
+            paragraph.add(text);
+            document.insert(paragraph, 0);
+        } else {
+            Node descendant = document.getDescendantAt(selectionStart);
+            int offset = selectionStart - descendant.getDocumentOffset();
 
-        if (descendant instanceof TextNode) {
-            // The caret is positioned within an existing text node
-            TextNode textNode = (TextNode)descendant;
-            textNode.insertText(text, offset);
-        } else if (descendant instanceof Paragraph) {
-            // The caret is positioned on the paragraph terminator
-            Paragraph paragraph = (Paragraph)descendant;
-
-            int n = paragraph.getLength();
-            if (n > 0) {
-                Node node = paragraph.get(n - 1);
-                if (node instanceof TextNode) {
-                    // Insert the text into the existing node
-                    TextNode textNode = (TextNode)node;
-                    textNode.insertText(text, offset - textNode.getOffset());
+            if (descendant instanceof TextNode) {
+                // The caret is positioned within an existing text node
+                TextNode textNode = (TextNode)descendant;
+                textNode.insertText(text, offset);
+            } else if (descendant instanceof Paragraph) {
+                // The caret is positioned on the paragraph terminator
+                Paragraph paragraph = (Paragraph)descendant;
+
+                int n = paragraph.getLength();
+                if (n > 0) {
+                    Node node = paragraph.get(n - 1);
+                    if (node instanceof TextNode) {
+                        // Insert the text into the existing node
+                        TextNode textNode = (TextNode)node;
+                        textNode.insertText(text, offset - textNode.getOffset());
+                    } else {
+                        // Append a new text node
+                        paragraph.add(new TextNode(text));
+                    }
                 } else {
-                    // Append a new text node
+                    // The paragraph is currently empty
                     paragraph.add(new TextNode(text));
                 }
             } else {
-                // The paragraph is currently empty
-                paragraph.add(new TextNode(text));
+                // The caret is positioned on a non-text character node; insert
+                // the text into the descendant's parent
+                Element parent = descendant.getParent();
+                int index = parent.indexOf(descendant);
+                parent.insert(new TextNode(text), index);
             }
-        } else {
-            // The caret is positioned on a non-text character node; insert
-            // the text into the descendant's parent
-            Element parent = descendant.getParent();
-            int index = parent.indexOf(descendant);
-            parent.insert(new TextNode(text), index);
         }
 
         // Set the selection start to the character following the insertion

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=1082094&r1=1082093&r2=1082094&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 Wed Mar 16 08:41:34 2011
@@ -496,7 +496,7 @@ public abstract class Element extends No
     public int getNodeAt(int offset) {
         if (offset < 0
             || offset >= characterCount) {
-            throw new IndexOutOfBoundsException();
+            throw new IndexOutOfBoundsException("offset " + offset + " out of range [0," + characterCount + "]");
         }
 
         int i = nodes.getLength() - 1;

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/text/Span.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/text/Span.java?rev=1082094&r1=1082093&r2=1082094&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/text/Span.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/text/Span.java Wed Mar 16 08:41:34 2011
@@ -37,7 +37,7 @@ public class Span extends Element {
     public void insert(Node node, int index) {
         if (node instanceof Block) {
             throw new IllegalArgumentException("Child node must not be an instance of "
-                + Block.class.getName());
+                + Block.class.getName() + ", " + node.getClass());
         }
 
         super.insert(node, index);