29
29
import javax .swing .border .EmptyBorder ;
30
30
import javax .swing .text .DefaultCaret ;
31
31
import javax .swing .text .DefaultEditorKit ;
32
+ import javax .swing .event .UndoableEditListener ;
33
+ import javax .swing .text .AbstractDocument ;
34
+ import javax .swing .text .Document ;
32
35
33
36
import cc .arduino .packages .BoardPort ;
34
37
@@ -37,14 +40,20 @@ public abstract class AbstractTextMonitor extends AbstractMonitor {
37
40
38
41
protected JLabel noLineEndingAlert ;
39
42
protected TextAreaFIFO textArea ;
43
+ protected HTMLTextAreaFIFO htmlTextArea ;
40
44
protected JScrollPane scrollPane ;
45
+ protected JScrollPane htmlScrollPane ;
41
46
protected JTextField textField ;
42
47
protected JButton sendButton ;
43
48
protected JButton clearButton ;
44
49
protected JCheckBox autoscrollBox ;
45
50
protected JCheckBox addTimeStampBox ;
46
51
protected JComboBox <String > lineEndings ;
47
52
protected JComboBox <String > serialRates ;
53
+ protected Container mainPane ;
54
+ private long lastMessage ;
55
+ private javax .swing .Timer updateTimer ;
56
+ private boolean htmlView = true ;
48
57
49
58
public AbstractTextMonitor (Base base , BoardPort boardPort ) {
50
59
super (boardPort );
@@ -57,21 +66,97 @@ public AbstractTextMonitor(Base base, BoardPort boardPort) {
57
66
@ Override
58
67
protected void onCreateWindow (Container mainPane ) {
59
68
69
+ this .mainPane = mainPane ;
60
70
mainPane .setLayout (new BorderLayout ());
61
71
62
72
textArea = new TextAreaFIFO (8_000_000 );
63
73
textArea .setRows (16 );
64
74
textArea .setColumns (40 );
65
75
textArea .setEditable (false );
66
76
77
+ htmlTextArea = new HTMLTextAreaFIFO (8000000 );
78
+ htmlTextArea .setEditable (false );
79
+ htmlTextArea .setOpaque (false );
80
+
67
81
// don't automatically update the caret. that way we can manually decide
68
82
// whether or not to do so based on the autoscroll checkbox.
69
83
((DefaultCaret ) textArea .getCaret ()).setUpdatePolicy (DefaultCaret .NEVER_UPDATE );
84
+ ((DefaultCaret ) htmlTextArea .getCaret ()).setUpdatePolicy (DefaultCaret .NEVER_UPDATE );
85
+
86
+ Document doc = textArea .getDocument ();
87
+ if (doc instanceof AbstractDocument )
88
+ {
89
+ UndoableEditListener [] undoListeners =
90
+ ( (AbstractDocument ) doc ).getUndoableEditListeners ();
91
+ if (undoListeners .length > 0 )
92
+ {
93
+ for (UndoableEditListener undoListener : undoListeners )
94
+ {
95
+ doc .removeUndoableEditListener (undoListener );
96
+ }
97
+ }
98
+ }
99
+
100
+ doc = htmlTextArea .getDocument ();
101
+ if (doc instanceof AbstractDocument )
102
+ {
103
+ UndoableEditListener [] undoListeners =
104
+ ( (AbstractDocument ) doc ).getUndoableEditListeners ();
105
+ if (undoListeners .length > 0 )
106
+ {
107
+ for (UndoableEditListener undoListener : undoListeners )
108
+ {
109
+ doc .removeUndoableEditListener (undoListener );
110
+ }
111
+ }
112
+ }
70
113
71
114
scrollPane = new JScrollPane (textArea );
115
+ scrollPane .setVerticalScrollBarPolicy (JScrollPane .VERTICAL_SCROLLBAR_AS_NEEDED );
116
+ htmlScrollPane = new JScrollPane (htmlTextArea );
117
+ htmlScrollPane .setVerticalScrollBarPolicy (JScrollPane .VERTICAL_SCROLLBAR_AS_NEEDED );
118
+
119
+ ActionListener checkIfSteady = new ActionListener () {
120
+ public void actionPerformed (ActionEvent evt ) {
121
+ if (System .currentTimeMillis () - lastMessage > 200 ) {
122
+ if (htmlView == false && textArea .getLength () < 1000 ) {
123
+
124
+ htmlTextArea .setText ("" );
125
+ boolean res = htmlTextArea .append (textArea .getText ());
126
+ if (res ) {
127
+ htmlView = true ;
128
+ mainPane .remove (scrollPane );
129
+ if (textArea .getCaretPosition () > htmlTextArea .getDocument ().getLength ()) {
130
+ htmlTextArea .setCaretPosition (htmlTextArea .getDocument ().getLength ());
131
+ } else {
132
+ htmlTextArea .setCaretPosition (textArea .getCaretPosition ());
133
+ }
134
+ mainPane .add (htmlScrollPane , BorderLayout .CENTER );
135
+ scrollPane .setVisible (false );
136
+ mainPane .validate ();
137
+ mainPane .repaint ();
138
+ }
139
+ }
140
+ } else {
141
+ if (htmlView == true ) {
142
+ htmlView = false ;
143
+ mainPane .remove (htmlScrollPane );
144
+ mainPane .add (scrollPane , BorderLayout .CENTER );
145
+ scrollPane .setVisible (true );
146
+ mainPane .validate ();
147
+ mainPane .repaint ();
148
+ }
149
+ }
150
+ }
151
+ };
152
+
153
+ updateTimer = new javax .swing .Timer (33 , checkIfSteady );
72
154
73
155
mainPane .add (scrollPane , BorderLayout .CENTER );
74
156
157
+ htmlTextArea .setVisible (true );
158
+ htmlScrollPane .setVisible (true );
159
+
75
160
JPanel upperPane = new JPanel ();
76
161
upperPane .setLayout (new BoxLayout (upperPane , BoxLayout .X_AXIS ));
77
162
upperPane .setBorder (new EmptyBorder (4 , 4 , 4 , 4 ));
@@ -156,20 +241,27 @@ public void windowGainedFocus(WindowEvent e) {
156
241
applyPreferences ();
157
242
158
243
mainPane .add (pane , BorderLayout .SOUTH );
244
+
245
+ updateTimer .start ();
159
246
}
160
247
161
248
@ Override
162
249
protected void onEnableWindow (boolean enable )
163
250
{
164
251
textArea .setEnabled (enable );
165
252
clearButton .setEnabled (enable );
253
+ htmlTextArea .setEnabled (enable );
166
254
scrollPane .setEnabled (enable );
255
+ htmlScrollPane .setEnabled (enable );
167
256
textField .setEnabled (enable );
168
257
sendButton .setEnabled (enable );
169
258
autoscrollBox .setEnabled (enable );
170
259
addTimeStampBox .setEnabled (enable );
171
260
lineEndings .setEnabled (enable );
172
261
serialRates .setEnabled (enable );
262
+ if (enable == false ) {
263
+ htmlTextArea .setText ("" );
264
+ }
173
265
}
174
266
175
267
public void onSendCommand (ActionListener listener ) {
@@ -187,6 +279,7 @@ public void onSerialRateChange(ActionListener listener) {
187
279
188
280
@ Override
189
281
public void message (String msg ) {
282
+ lastMessage = System .currentTimeMillis ();
190
283
SwingUtilities .invokeLater (() -> updateTextArea (msg ));
191
284
}
192
285
0 commit comments