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