From: Hannu Niemistö Date: Thu, 17 Aug 2017 14:49:41 +0000 (+0300) Subject: (refs #7439) Added buffering to SCL Console output X-Git-Tag: v1.31.0~233 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=afa02bc122a8a9954fd357aa284ec3eae8ba27c6;p=simantics%2Fplatform.git (refs #7439) Added buffering to SCL Console output Change-Id: I757e5b9c7d0e03838ac8eee033e78b91949c7985 --- diff --git a/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/console/AbstractCommandConsole.java b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/console/AbstractCommandConsole.java index 388c87806..8039d4662 100644 --- a/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/console/AbstractCommandConsole.java +++ b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/console/AbstractCommandConsole.java @@ -400,24 +400,50 @@ public abstract class AbstractCommandConsole extends Composite { AbstractCommandConsole.this.layout(true); } + private StringBuilder outputBuffer = new StringBuilder(); + private ArrayList styleRanges = new ArrayList(); + private volatile boolean outputScheduled = false; + public void appendOutput(final String text, final Color foreground, final Color background) { - final Display display = Display.getDefault(); - if(display.isDisposed()) return; - display.asyncExec(new Runnable() { - @Override - public void run() { - if(output.isDisposed()) return; - int pos = output.getCharCount(); - outputModiLock = true; - output.replaceTextRange(pos, 0, text); - outputModiLock = false; - output.setStyleRange(new StyleRange(pos, text.length(), - foreground, background)); - output.setCaretOffset(output.getCharCount()); - output.showSelection(); - } + synchronized (outputBuffer) { + styleRanges.add(new StyleRange(outputBuffer.length(), text.length(), foreground, background)); + outputBuffer.append(text); + } + if(!outputScheduled) { + outputScheduled = true; + final Display display = Display.getDefault(); + if(display.isDisposed()) return; + display.asyncExec(new Runnable() { + @Override + public void run() { + if(output.isDisposed()) return; + String outputText; + StyleRange[] styleRangeArray; + synchronized(outputBuffer) { + outputScheduled = false; + + outputText = outputBuffer.toString(); + outputBuffer = new StringBuilder(); + + styleRangeArray = styleRanges.toArray(new StyleRange[styleRanges.size()]); + styleRanges.clear(); + } + int pos = output.getCharCount(); - }); + outputModiLock = true; + output.replaceTextRange(pos, 0, outputText); + outputModiLock = false; + + for(StyleRange styleRange : styleRangeArray) { + styleRange.start += pos; + output.setStyleRange(styleRange); + } + + output.setCaretOffset(output.getCharCount()); + output.showSelection(); + } + }); + } } private void execute() {