5
5
6
6
import javax .swing .*;
7
7
import javax .swing .border .EmptyBorder ;
8
- import javax .swing .text .DefaultCaret ;
8
+ import javax .swing .text .* ;
9
9
import java .awt .*;
10
10
import java .awt .event .ActionEvent ;
11
11
import java .awt .event .ActionListener ;
16
16
17
17
public abstract class AbstractMonitor extends JFrame implements MessageConsumer {
18
18
19
+ private static final int DEFAULT_MAX_CONTENT_LENGTH = 10 * 1024 ;
20
+
19
21
protected final JLabel noLineEndingAlert ;
22
+ protected final int maxContentLength ;
20
23
protected JTextArea textArea ;
21
24
protected JScrollPane scrollPane ;
22
25
protected JTextField textField ;
@@ -28,6 +31,8 @@ public abstract class AbstractMonitor extends JFrame implements MessageConsumer
28
31
public AbstractMonitor (String title ) {
29
32
super (title );
30
33
34
+ maxContentLength = Preferences .getInteger ("serial_monitor_max_content_length" , DEFAULT_MAX_CONTENT_LENGTH );
35
+
31
36
addWindowListener (new WindowAdapter () {
32
37
public void windowClosing (WindowEvent event ) {
33
38
try {
@@ -58,7 +63,21 @@ public void actionPerformed(ActionEvent event) {
58
63
Font editorFont = Preferences .getFont ("editor.font" );
59
64
Font font = new Font (consoleFont .getName (), consoleFont .getStyle (), editorFont .getSize ());
60
65
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 );
62
81
textArea .setEditable (false );
63
82
textArea .setFont (font );
64
83
0 commit comments