avoid awt thread floods from mspsim cli output

This commit is contained in:
Fredrik Osterlind 2012-03-01 17:02:40 +01:00
parent 28bdc2fb50
commit a84dc139ea

View file

@ -37,6 +37,7 @@ import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter; import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import java.io.PrintStream; import java.io.PrintStream;
import java.util.List;
import javax.swing.JMenuItem; import javax.swing.JMenuItem;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
@ -44,7 +45,6 @@ import javax.swing.JPopupMenu;
import javax.swing.JScrollPane; import javax.swing.JScrollPane;
import javax.swing.JTextArea; import javax.swing.JTextArea;
import javax.swing.JTextField; import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import se.sics.cooja.ClassDescription; import se.sics.cooja.ClassDescription;
import se.sics.cooja.GUI; import se.sics.cooja.GUI;
@ -53,6 +53,7 @@ import se.sics.cooja.MotePlugin;
import se.sics.cooja.PluginType; import se.sics.cooja.PluginType;
import se.sics.cooja.Simulation; import se.sics.cooja.Simulation;
import se.sics.cooja.VisPlugin; import se.sics.cooja.VisPlugin;
import se.sics.cooja.dialogs.UpdateAggregator;
import se.sics.cooja.mspmote.MspMote; import se.sics.cooja.mspmote.MspMote;
import se.sics.mspsim.cli.CommandContext; import se.sics.mspsim.cli.CommandContext;
import se.sics.mspsim.cli.LineListener; import se.sics.mspsim.cli.LineListener;
@ -77,8 +78,8 @@ public class MspCLI extends VisPlugin implements MotePlugin {
final Container panel = getContentPane(); final Container panel = getContentPane();
logArea = new JTextArea(4, 30); logArea = new JTextArea(4, 20);
logArea.setTabSize(4); logArea.setTabSize(8);
logArea.setEditable(false); logArea.setEditable(false);
panel.add(new JScrollPane(logArea), BorderLayout.CENTER); panel.add(new JScrollPane(logArea), BorderLayout.CENTER);
@ -176,26 +177,40 @@ public class MspCLI extends VisPlugin implements MotePlugin {
} }
}); });
cliResponseAggregator.start();
panel.add(commandField, BorderLayout.SOUTH); panel.add(commandField, BorderLayout.SOUTH);
setSize(500,500);
} }
public void closePlugin() { public void closePlugin() {
cliResponseAggregator.stop();
} }
public void addCLIData(final String text) { public void addCLIData(final String text) {
SwingUtilities.invokeLater(new Runnable() { cliResponseAggregator.add(text);
public void run() { }
private static final int UPDATE_INTERVAL = 250;
private UpdateAggregator<String> cliResponseAggregator = new UpdateAggregator<String>(UPDATE_INTERVAL) {
protected void handle(List<String> ls) {
String current = logArea.getText(); String current = logArea.getText();
int len = current.length(); int len = current.length();
if (len > 4096) { if (len > 4096) {
current = current.substring(len - 4096); current = current.substring(len - 4096);
} }
current = len > 0 ? (current + '\n' + text) : text;
logArea.setText(current); /* Add */
logArea.setCaretPosition(current.length()); StringBuilder sb = new StringBuilder(current);
for (String l: ls) {
sb.append(l);
sb.append('\n');
} }
}); logArea.setText(sb.toString());
logArea.setCaretPosition(sb.length());
} }
};
private String trim(String text) { private String trim(String text) {
return (text != null) && ((text = text.trim()).length() > 0) ? text : null; return (text != null) && ((text = text.trim()).length() > 0) ? text : null;