26
26
import javax .swing .border .EmptyBorder ;
27
27
import javax .swing .text .BadLocationException ;
28
28
import javax .swing .text .DefaultCaret ;
29
+ import javax .swing .event .UndoableEditListener ;
30
+ import javax .swing .text .AbstractDocument ;
29
31
import javax .swing .text .Document ;
30
32
31
33
import cc .arduino .packages .BoardPort ;
@@ -35,14 +37,20 @@ public abstract class AbstractTextMonitor extends AbstractMonitor {
35
37
36
38
protected JLabel noLineEndingAlert ;
37
39
protected TextAreaFIFO textArea ;
40
+ protected HTMLTextAreaFIFO htmlTextArea ;
38
41
protected JScrollPane scrollPane ;
42
+ protected JScrollPane htmlScrollPane ;
39
43
protected JTextField textField ;
40
44
protected JButton sendButton ;
41
45
protected JButton clearButton ;
42
46
protected JCheckBox autoscrollBox ;
43
47
protected JCheckBox addTimeStampBox ;
44
48
protected JComboBox lineEndings ;
45
49
protected JComboBox serialRates ;
50
+ protected Container mainPane ;
51
+ private long lastMessage ;
52
+ private javax .swing .Timer updateTimer ;
53
+ private boolean htmlView = true ;
46
54
47
55
private SimpleDateFormat logDateFormat ;
48
56
@@ -56,6 +64,7 @@ protected void onCreateWindow(Container mainPane) {
56
64
Font editorFont = PreferencesData .getFont ("editor.font" );
57
65
Font font = Theme .scale (new Font (consoleFont .getName (), consoleFont .getStyle (), editorFont .getSize ()));
58
66
67
+ this .mainPane = mainPane ;
59
68
mainPane .setLayout (new BorderLayout ());
60
69
61
70
textArea = new TextAreaFIFO (8000000 );
@@ -64,13 +73,89 @@ protected void onCreateWindow(Container mainPane) {
64
73
textArea .setEditable (false );
65
74
textArea .setFont (font );
66
75
76
+ htmlTextArea = new HTMLTextAreaFIFO (8000000 );
77
+ htmlTextArea .setEditable (false );
78
+ htmlTextArea .setFont (font );
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 );
156
+
157
+ htmlTextArea .setVisible (true );
158
+ htmlScrollPane .setVisible (true );
74
159
75
160
JPanel upperPane = new JPanel ();
76
161
upperPane .setLayout (new BoxLayout (upperPane , BoxLayout .X_AXIS ));
@@ -147,19 +232,26 @@ public void actionPerformed(ActionEvent e) {
147
232
pane .add (clearButton );
148
233
149
234
mainPane .add (pane , BorderLayout .SOUTH );
235
+
236
+ updateTimer .start ();
150
237
}
151
238
152
239
protected void onEnableWindow (boolean enable )
153
240
{
154
241
textArea .setEnabled (enable );
155
242
clearButton .setEnabled (enable );
243
+ htmlTextArea .setEnabled (enable );
156
244
scrollPane .setEnabled (enable );
245
+ htmlScrollPane .setEnabled (enable );
157
246
textField .setEnabled (enable );
158
247
sendButton .setEnabled (enable );
159
248
autoscrollBox .setEnabled (enable );
160
249
addTimeStampBox .setEnabled (enable );
161
250
lineEndings .setEnabled (enable );
162
251
serialRates .setEnabled (enable );
252
+ if (enable == false ) {
253
+ htmlTextArea .setText ("" );
254
+ }
163
255
}
164
256
165
257
public void onSendCommand (ActionListener listener ) {
@@ -174,8 +266,9 @@ public void onClearCommand(ActionListener listener) {
174
266
public void onSerialRateChange (ActionListener listener ) {
175
267
serialRates .addActionListener (listener );
176
268
}
177
-
269
+
178
270
public void message (final String s ) {
271
+ lastMessage = System .currentTimeMillis ();
179
272
SwingUtilities .invokeLater (new Runnable () {
180
273
public void run () {
181
274
if (addTimeStampBox .isSelected ()) {
0 commit comments