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 2021/01/06 23:29:52 UTC

svn commit: r1885217 - /pivot/trunk/wtk/src/org/apache/pivot/wtk/util/TextAreaOutputStream.java

Author: rwhitcomb
Date: Wed Jan  6 23:29:52 2021
New Revision: 1885217

URL: http://svn.apache.org/viewvc?rev=1885217&view=rev
Log:
Fix the EDT-ness of TextAreaOutputStream.

Modified:
    pivot/trunk/wtk/src/org/apache/pivot/wtk/util/TextAreaOutputStream.java

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/util/TextAreaOutputStream.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/util/TextAreaOutputStream.java?rev=1885217&r1=1885216&r2=1885217&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/util/TextAreaOutputStream.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/util/TextAreaOutputStream.java Wed Jan  6 23:29:52 2021
@@ -111,23 +111,34 @@ public final class TextAreaOutputStream
      * @param addNewLine Add a newline ('\n') character after any buffered text.
      */
     private void flushLineBuffer(final boolean addNewLine) {
-        int length    = textArea.getCharacterCount();
-        int newLength = length;
+	final String text;
 
         if (lineBuffer.size() > 0) {
             byte[] bytes = lineBuffer.toByteArray();
-            String text  = new String(bytes, incomingCharset);
-            textArea.insertText(text, length);
-            newLength += text.length();
+            text = new String(bytes, incomingCharset);
             lineBuffer.reset();
+        } else {
+            text = "";
         }
-        if (addNewLine) {
-            textArea.insertText("\n", newLength++);
-        }
-        // If there was anything added to the text, scroll to make it visible
-        if (newLength > length) {
-            Bounds beginningOfLineBounds = textArea.getCharacterBounds(newLength);
-            ApplicationContext.queueCallback(() -> textArea.scrollAreaToVisible(beginningOfLineBounds));
+
+        // Do the actual text manipulation (including scrolling) on the event thread
+        if (!text.isEmpty() || addNewLine) {
+            ApplicationContext.queueCallback(() -> {
+                int length    = textArea.getCharacterCount();
+                int newLength = length;
+
+                if (!text.isEmpty()) {
+                    textArea.insertText(text, length);
+                    newLength += text.length();
+                }
+
+                if (addNewLine) {
+                    textArea.insertText("\n", newLength++);
+                }
+
+                Bounds lastCharBounds = textArea.getCharacterBounds(newLength);
+                textArea.scrollAreaToVisible(lastCharBounds);
+            });
         }
     }