You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by rw...@apache.org on 2013/09/14 20:32:18 UTC

svn commit: r1523288 - in /pivot/trunk: ./ wtk/src/org/apache/pivot/wtk/TextPane.java

Author: rwhitcomb
Date: Sat Sep 14 18:32:18 2013
New Revision: 1523288

URL: http://svn.apache.org/r1523288
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.

This is a merge of revision 1523284 from branches/2.0.x to trunk.


Modified:
    pivot/trunk/   (props changed)
    pivot/trunk/wtk/src/org/apache/pivot/wtk/TextPane.java

Propchange: pivot/trunk/
------------------------------------------------------------------------------
  Merged /pivot/branches/2.0.x:r1523284

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=1523288&r1=1523287&r2=1523288&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/TextPane.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/TextPane.java Sat Sep 14 18:32:18 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));