21
21
import javax .swing .SwingUtilities ;
22
22
import javax .swing .border .EmptyBorder ;
23
23
import javax .swing .text .DefaultCaret ;
24
+ import javax .swing .event .UndoableEditListener ;
25
+ import javax .swing .text .AbstractDocument ;
26
+ import javax .swing .text .Document ;
24
27
25
28
import cc .arduino .packages .BoardPort ;
26
29
@@ -29,12 +32,18 @@ public abstract class AbstractTextMonitor extends AbstractMonitor {
29
32
30
33
protected JLabel noLineEndingAlert ;
31
34
protected TextAreaFIFO textArea ;
35
+ protected HTMLTextAreaFIFO htmlTextArea ;
32
36
protected JScrollPane scrollPane ;
37
+ protected JScrollPane htmlScrollPane ;
33
38
protected JTextField textField ;
34
39
protected JButton sendButton ;
35
40
protected JCheckBox autoscrollBox ;
36
41
protected JComboBox lineEndings ;
37
42
protected JComboBox serialRates ;
43
+ protected Container mainPane ;
44
+ private long lastMessage ;
45
+ private javax .swing .Timer updateTimer ;
46
+ private boolean htmlView = true ;
38
47
39
48
public AbstractTextMonitor (BoardPort boardPort ) {
40
49
super (boardPort );
@@ -45,6 +54,7 @@ protected void onCreateWindow(Container mainPane) {
45
54
Font editorFont = PreferencesData .getFont ("editor.font" );
46
55
Font font = Theme .scale (new Font (consoleFont .getName (), consoleFont .getStyle (), editorFont .getSize ()));
47
56
57
+ this .mainPane = mainPane ;
48
58
mainPane .setLayout (new BorderLayout ());
49
59
50
60
textArea = new TextAreaFIFO (8000000 );
@@ -53,13 +63,89 @@ protected void onCreateWindow(Container mainPane) {
53
63
textArea .setEditable (false );
54
64
textArea .setFont (font );
55
65
66
+ htmlTextArea = new HTMLTextAreaFIFO (8000000 );
67
+ htmlTextArea .setEditable (false );
68
+ htmlTextArea .setFont (font );
69
+ htmlTextArea .setOpaque (false );
70
+
56
71
// don't automatically update the caret. that way we can manually decide
57
72
// whether or not to do so based on the autoscroll checkbox.
58
73
((DefaultCaret ) textArea .getCaret ()).setUpdatePolicy (DefaultCaret .NEVER_UPDATE );
74
+ ((DefaultCaret ) htmlTextArea .getCaret ()).setUpdatePolicy (DefaultCaret .NEVER_UPDATE );
75
+
76
+ Document doc = textArea .getDocument ();
77
+ if (doc instanceof AbstractDocument )
78
+ {
79
+ UndoableEditListener [] undoListeners =
80
+ ( (AbstractDocument ) doc ).getUndoableEditListeners ();
81
+ if (undoListeners .length > 0 )
82
+ {
83
+ for (UndoableEditListener undoListener : undoListeners )
84
+ {
85
+ doc .removeUndoableEditListener (undoListener );
86
+ }
87
+ }
88
+ }
89
+
90
+ doc = htmlTextArea .getDocument ();
91
+ if (doc instanceof AbstractDocument )
92
+ {
93
+ UndoableEditListener [] undoListeners =
94
+ ( (AbstractDocument ) doc ).getUndoableEditListeners ();
95
+ if (undoListeners .length > 0 )
96
+ {
97
+ for (UndoableEditListener undoListener : undoListeners )
98
+ {
99
+ doc .removeUndoableEditListener (undoListener );
100
+ }
101
+ }
102
+ }
59
103
60
104
scrollPane = new JScrollPane (textArea );
105
+ scrollPane .setVerticalScrollBarPolicy (JScrollPane .VERTICAL_SCROLLBAR_AS_NEEDED );
106
+ htmlScrollPane = new JScrollPane (htmlTextArea );
107
+ htmlScrollPane .setVerticalScrollBarPolicy (JScrollPane .VERTICAL_SCROLLBAR_AS_NEEDED );
108
+
109
+ ActionListener checkIfSteady = new ActionListener () {
110
+ public void actionPerformed (ActionEvent evt ) {
111
+ if (System .currentTimeMillis () - lastMessage > 200 ) {
112
+ if (htmlView == false && textArea .getLength () < 1000 ) {
113
+
114
+ htmlTextArea .setText ("" );
115
+ boolean res = htmlTextArea .append (textArea .getText ());
116
+ if (res ) {
117
+ htmlView = true ;
118
+ mainPane .remove (scrollPane );
119
+ if (textArea .getCaretPosition () > htmlTextArea .getDocument ().getLength ()) {
120
+ htmlTextArea .setCaretPosition (htmlTextArea .getDocument ().getLength ());
121
+ } else {
122
+ htmlTextArea .setCaretPosition (textArea .getCaretPosition ());
123
+ }
124
+ mainPane .add (htmlScrollPane , BorderLayout .CENTER );
125
+ scrollPane .setVisible (false );
126
+ mainPane .validate ();
127
+ mainPane .repaint ();
128
+ }
129
+ }
130
+ } else {
131
+ if (htmlView == true ) {
132
+ htmlView = false ;
133
+ mainPane .remove (htmlScrollPane );
134
+ mainPane .add (scrollPane , BorderLayout .CENTER );
135
+ scrollPane .setVisible (true );
136
+ mainPane .validate ();
137
+ mainPane .repaint ();
138
+ }
139
+ }
140
+ }
141
+ };
142
+
143
+ updateTimer = new javax .swing .Timer (33 , checkIfSteady );
61
144
62
145
mainPane .add (scrollPane , BorderLayout .CENTER );
146
+
147
+ htmlTextArea .setVisible (true );
148
+ htmlScrollPane .setVisible (true );
63
149
64
150
JPanel upperPane = new JPanel ();
65
151
upperPane .setLayout (new BoxLayout (upperPane , BoxLayout .X_AXIS ));
@@ -115,17 +201,24 @@ public void actionPerformed(ActionEvent event) {
115
201
pane .add (serialRates );
116
202
117
203
mainPane .add (pane , BorderLayout .SOUTH );
204
+
205
+ updateTimer .start ();
118
206
}
119
207
120
208
protected void onEnableWindow (boolean enable )
121
209
{
122
210
textArea .setEnabled (enable );
211
+ htmlTextArea .setEnabled (enable );
123
212
scrollPane .setEnabled (enable );
213
+ htmlScrollPane .setEnabled (enable );
124
214
textField .setEnabled (enable );
125
215
sendButton .setEnabled (enable );
126
216
autoscrollBox .setEnabled (enable );
127
217
lineEndings .setEnabled (enable );
128
218
serialRates .setEnabled (enable );
219
+ if (enable == false ) {
220
+ htmlTextArea .setText ("" );
221
+ }
129
222
}
130
223
131
224
public void onSendCommand (ActionListener listener ) {
@@ -136,8 +229,9 @@ public void onSendCommand(ActionListener listener) {
136
229
public void onSerialRateChange (ActionListener listener ) {
137
230
serialRates .addActionListener (listener );
138
231
}
139
-
232
+
140
233
public void message (final String s ) {
234
+ lastMessage = System .currentTimeMillis ();
141
235
SwingUtilities .invokeLater (new Runnable () {
142
236
public void run () {
143
237
textArea .append (s );
0 commit comments