Skip to content

Commit 4b7c7e8

Browse files
committed
Replaced UpdateTextAreaAction with a lambda that performs a method call
The overhead is negligible and this design simplifies a lot the class structure. More discussion here: #8088 (comment)
1 parent c2f3245 commit 4b7c7e8

File tree

1 file changed

+24
-43
lines changed

1 file changed

+24
-43
lines changed

app/src/processing/app/AbstractTextMonitor.java

+24-43
Original file line numberDiff line numberDiff line change
@@ -171,56 +171,37 @@ public void onSerialRateChange(ActionListener listener) {
171171
serialRates.addActionListener(listener);
172172
}
173173

174-
public void message(final String msg) {
175-
SwingUtilities.invokeLater(new UpdateTextAreaAction(textArea,
176-
addTimeStampBox.isSelected(),
177-
autoscrollBox.isSelected(),
178-
msg));
174+
public void message(String msg) {
175+
SwingUtilities.invokeLater(() -> updateTextArea(msg));
179176
}
180177

181-
static class UpdateTextAreaAction implements Runnable {
178+
private static final String LINE_SEPARATOR = "\n";
179+
private boolean isStartingLine = true;
182180

183-
private static final String LINE_SEPARATOR = "\n";
184-
185-
private String msg;
186-
private boolean addTimeStamp;
187-
private boolean doAutoscroll;
188-
private TextAreaFIFO textArea;
189-
190-
UpdateTextAreaAction(TextAreaFIFO textArea, boolean addTimeStamp,
191-
boolean doAutoscroll, String msg) {
192-
this.msg = msg;
193-
this.textArea = textArea;
194-
this.addTimeStamp = addTimeStamp;
195-
this.doAutoscroll = doAutoscroll;
181+
protected void updateTextArea(String msg) {
182+
if (addTimeStampBox.isSelected()) {
183+
textArea.append(addTimestamps(msg));
184+
} else {
185+
textArea.append(msg);
196186
}
197-
198-
public void run() {
199-
if (addTimeStamp) {
200-
textArea.append(addTimestamps(msg));
201-
} else {
202-
textArea.append(msg);
203-
}
204-
if (doAutoscroll) {
205-
textArea.setCaretPosition(textArea.getDocument().getLength());
206-
}
187+
if (autoscrollBox.isSelected()) {
188+
textArea.setCaretPosition(textArea.getDocument().getLength());
207189
}
190+
}
208191

209-
private String addTimestamps(String text) {
210-
String now = new SimpleDateFormat("HH:mm:ss.SSS -> ").format(new Date());
211-
final StringBuilder sb = new StringBuilder(text.length() + now.length());
212-
boolean isStartingLine = true;
213-
StringTokenizer tokenizer = new StringTokenizer(text, LINE_SEPARATOR, true);
214-
while (tokenizer.hasMoreTokens()) {
215-
if (isStartingLine) {
216-
sb.append(now);
217-
}
218-
String token = tokenizer.nextToken();
219-
sb.append(token);
220-
// tokenizer returns "\n" as a single token
221-
isStartingLine = token.equals(LINE_SEPARATOR);
192+
private String addTimestamps(String text) {
193+
String now = new SimpleDateFormat("HH:mm:ss.SSS -> ").format(new Date());
194+
final StringBuilder sb = new StringBuilder(text.length() + now.length());
195+
StringTokenizer tokenizer = new StringTokenizer(text, LINE_SEPARATOR, true);
196+
while (tokenizer.hasMoreTokens()) {
197+
if (isStartingLine) {
198+
sb.append(now);
222199
}
223-
return sb.toString();
200+
String token = tokenizer.nextToken();
201+
sb.append(token);
202+
// tokenizer returns "\n" as a single token
203+
isStartingLine = token.equals(LINE_SEPARATOR);
224204
}
205+
return sb.toString();
225206
}
226207
}

0 commit comments

Comments
 (0)