1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-01-19 08:52:15 +01:00

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:

https://github.com/arduino/Arduino/pull/8088#issuecomment-433727423
This commit is contained in:
Cristian Maglie 2018-11-14 16:59:54 +01:00 committed by Cristian Maglie
parent c2f324508f
commit 4b7c7e886c

View File

@ -171,37 +171,20 @@ public abstract class AbstractTextMonitor extends AbstractMonitor {
serialRates.addActionListener(listener);
}
public void message(final String msg) {
SwingUtilities.invokeLater(new UpdateTextAreaAction(textArea,
addTimeStampBox.isSelected(),
autoscrollBox.isSelected(),
msg));
public void message(String msg) {
SwingUtilities.invokeLater(() -> updateTextArea(msg));
}
static class UpdateTextAreaAction implements Runnable {
private static final String LINE_SEPARATOR = "\n";
private boolean isStartingLine = true;
private String msg;
private boolean addTimeStamp;
private boolean doAutoscroll;
private TextAreaFIFO textArea;
UpdateTextAreaAction(TextAreaFIFO textArea, boolean addTimeStamp,
boolean doAutoscroll, String msg) {
this.msg = msg;
this.textArea = textArea;
this.addTimeStamp = addTimeStamp;
this.doAutoscroll = doAutoscroll;
}
public void run() {
if (addTimeStamp) {
protected void updateTextArea(String msg) {
if (addTimeStampBox.isSelected()) {
textArea.append(addTimestamps(msg));
} else {
textArea.append(msg);
}
if (doAutoscroll) {
if (autoscrollBox.isSelected()) {
textArea.setCaretPosition(textArea.getDocument().getLength());
}
}
@ -209,7 +192,6 @@ public abstract class AbstractTextMonitor extends AbstractMonitor {
private String addTimestamps(String text) {
String now = new SimpleDateFormat("HH:mm:ss.SSS -> ").format(new Date());
final StringBuilder sb = new StringBuilder(text.length() + now.length());
boolean isStartingLine = true;
StringTokenizer tokenizer = new StringTokenizer(text, LINE_SEPARATOR, true);
while (tokenizer.hasMoreTokens()) {
if (isStartingLine) {
@ -222,5 +204,4 @@ public abstract class AbstractTextMonitor extends AbstractMonitor {
}
return sb.toString();
}
}
}