Skip to content

Commit 3ae7aa7

Browse files
author
Federico Fissore
committed
First unsuccessfull attempt to solve #2233
1 parent 3a1b7ea commit 3ae7aa7

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

Diff for: app/src/processing/app/AbstractMonitor.java

+21-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import javax.swing.*;
77
import javax.swing.border.EmptyBorder;
8-
import javax.swing.text.DefaultCaret;
8+
import javax.swing.text.*;
99
import java.awt.*;
1010
import java.awt.event.ActionEvent;
1111
import java.awt.event.ActionListener;
@@ -16,7 +16,10 @@
1616

1717
public abstract class AbstractMonitor extends JFrame implements MessageConsumer {
1818

19+
private static final int DEFAULT_MAX_CONTENT_LENGTH = 10 * 1024;
20+
1921
protected final JLabel noLineEndingAlert;
22+
protected final int maxContentLength;
2023
protected JTextArea textArea;
2124
protected JScrollPane scrollPane;
2225
protected JTextField textField;
@@ -28,6 +31,8 @@ public abstract class AbstractMonitor extends JFrame implements MessageConsumer
2831
public AbstractMonitor(String title) {
2932
super(title);
3033

34+
maxContentLength = Preferences.getInteger("serial_monitor_max_content_length", DEFAULT_MAX_CONTENT_LENGTH);
35+
3136
addWindowListener(new WindowAdapter() {
3237
public void windowClosing(WindowEvent event) {
3338
try {
@@ -58,7 +63,21 @@ public void actionPerformed(ActionEvent event) {
5863
Font editorFont = Preferences.getFont("editor.font");
5964
Font font = new Font(consoleFont.getName(), consoleFont.getStyle(), editorFont.getSize());
6065

61-
textArea = new JTextArea(16, 40);
66+
PlainDocument textAreaModel = new PlainDocument();
67+
textAreaModel.setDocumentFilter(new DocumentFilter() {
68+
@Override
69+
public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
70+
int documentLength = fb.getDocument().getLength();
71+
int sumOfLengths = documentLength + string.length();
72+
if (sumOfLengths > maxContentLength) {
73+
int lengthToRemove = sumOfLengths - maxContentLength;
74+
fb.remove(0, lengthToRemove);
75+
offset -= lengthToRemove;
76+
}
77+
super.insertString(fb, offset, string, attr);
78+
}
79+
});
80+
textArea = new JTextArea(textAreaModel, null, 16, 40);
6281
textArea.setEditable(false);
6382
textArea.setFont(font);
6483

Diff for: app/src/processing/app/Preferences.java

+3
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,9 @@ static public int getInteger(String attribute /*, int defaultValue*/) {
892892
*/
893893
}
894894

895+
static public int getInteger(String attribute, int defaultValue) {
896+
return Integer.parseInt(get(attribute, String.valueOf(defaultValue)));
897+
}
895898

896899
static public void setInteger(String key, int value) {
897900
set(key, String.valueOf(value));

0 commit comments

Comments
 (0)