Author: rwhitcomb
Date: Sat Sep 14 18:26:10 2013
New Revision: 1523284

URL: http://svn.apache.org/r1523284
Log:
Small tweak to TextPane to allow insert at the end of a paragraph to add to
a TextNode that is positioned there, instead of always creating a new TextNode.
This allows a bit more efficiency when doing things like syntax coloring of
a document.


Modified:
    pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/TextPane.java

Modified: pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/TextPane.java
URL: 
http://svn.apache.org/viewvc/pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/TextPane.java?rev=1523284&r1=1523283&r2=1523284&view=diff
==============================================================================
--- pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/TextPane.java (original)
+++ pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/TextPane.java Sat Sep 14 
18:26:10 2013
@@ -371,6 +371,18 @@ public class TextPane extends Container 
         }
     }
 
+    private Node getRightmostDescendant(Element element) {
+        int n = element.getLength();
+        if (n > 0) {
+            Node node = element.get(n - 1);
+            if (node instanceof Element) {
+                return getRightmostDescendant((Element)node);
+            }
+            return node;
+        }
+        return element;
+    }
+
     public void insert(char character) {
         // TODO Don't make every character undoable; break at word boundaries?
 
@@ -405,19 +417,18 @@ public class TextPane extends Container 
                 textNode.insertText(text, offset);
             } else if (descendant instanceof Paragraph) {
                 // The caret is positioned on the paragraph terminator
+                // so get to the bottom rightmost descendant and add there
                 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));
-                    }
+                Node node = getRightmostDescendant(paragraph);
+                if (node instanceof TextNode) {
+                    // Insert the text into the existing node
+                    TextNode textNode = (TextNode)node;
+                    textNode.insertText(text, selectionStart - 
textNode.getDocumentOffset());
+                } else if (node instanceof Element) {
+                    // Append a new text node
+                    Element element = (Element)node;
+                    element.add(new TextNode(text));
                 } else {
                     // The paragraph is currently empty
                     paragraph.add(new TextNode(text));


Reply via email to