From 8e0a311e871a3feb505f18771a6fd58abf5048cd Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Tue, 23 Dec 2014 14:06:23 +0100 Subject: [PATCH] SerialMonitor: limit buffering without autoscroll When the "autoscroll" checkbox is deselected the buffer may continue to grow up to twice of the maximum size. This is a compromise to ensure a better user experience and, at the same time, reduce the chance to lose data and get "holes" in the serial stream. See #2491 --- app/src/processing/app/SerialMonitor.java | 10 +++---- .../processing/app/debug/TextAreaFIFO.java | 26 ++++++++++++++----- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/app/src/processing/app/SerialMonitor.java b/app/src/processing/app/SerialMonitor.java index 64c8bb399..7ce00474b 100644 --- a/app/src/processing/app/SerialMonitor.java +++ b/app/src/processing/app/SerialMonitor.java @@ -69,7 +69,7 @@ public class SerialMonitor extends JFrame implements ActionListener { Font editorFont = Preferences.getFont("editor.font"); Font font = new Font(consoleFont.getName(), consoleFont.getStyle(), editorFont.getSize()); - textArea = new TextAreaFIFO(4000000); + textArea = new TextAreaFIFO(8000000); textArea.setRows(16); textArea.setColumns(40); textArea.setEditable(false); @@ -244,11 +244,11 @@ public class SerialMonitor extends JFrame implements ActionListener { final String s = consumeUpdateBuffer(); if (s.length() > 0) { //System.out.println("gui append " + s.length()); - boolean scroll = autoscrollBox.isSelected(); - textArea.allowTrim(scroll); - textArea.append(s); - if (scroll) { + if (autoscrollBox.isSelected()) { + textArea.appendTrim(s); textArea.setCaretPosition(textArea.getDocument().getLength()); + } else { + textArea.appendNoTrim(s); } } } diff --git a/app/src/processing/app/debug/TextAreaFIFO.java b/app/src/processing/app/debug/TextAreaFIFO.java index 9a6d575e1..9783cd42c 100644 --- a/app/src/processing/app/debug/TextAreaFIFO.java +++ b/app/src/processing/app/debug/TextAreaFIFO.java @@ -30,6 +30,7 @@ import javax.swing.text.BadLocationException; public class TextAreaFIFO extends JTextArea implements DocumentListener { private int maxChars; + private int trimMaxChars; private int updateCount; // limit how often we trim the document @@ -37,15 +38,12 @@ public class TextAreaFIFO extends JTextArea implements DocumentListener { public TextAreaFIFO(int max) { maxChars = max; + trimMaxChars = max / 2; updateCount = 0; doTrim = true; getDocument().addDocumentListener(this); } - public void allowTrim(boolean trim) { - doTrim = trim; - } - public void insertUpdate(DocumentEvent e) { if (++updateCount > 150 && doTrim) { updateCount = 0; @@ -66,8 +64,8 @@ public class TextAreaFIFO extends JTextArea implements DocumentListener { public void trimDocument() { int len = 0; len = getDocument().getLength(); - if (len > maxChars) { - int n = len - maxChars; + if (len > trimMaxChars) { + int n = len - trimMaxChars; //System.out.println("trimDocument: remove " + n + " chars"); try { getDocument().remove(0, n); @@ -75,4 +73,20 @@ public class TextAreaFIFO extends JTextArea implements DocumentListener { } } } + + public void appendNoTrim(String s) { + int free = maxChars - getDocument().getLength(); + if (free <= 0) + return; + if (s.length() > free) + append(s.substring(0, free)); + else + append(s); + doTrim = false; + } + + public void appendTrim(String str) { + append(str); + doTrim = true; + } }