]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
(refs #7439) Added buffering to SCL Console output 68/868/1
authorHannu Niemistö <hannu.niemisto@semantum.fi>
Thu, 17 Aug 2017 14:49:41 +0000 (17:49 +0300)
committerHannu Niemistö <hannu.niemisto@semantum.fi>
Thu, 17 Aug 2017 14:49:41 +0000 (17:49 +0300)
Change-Id: I757e5b9c7d0e03838ac8eee033e78b91949c7985

bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/console/AbstractCommandConsole.java

index 388c87806ee7a5e8b1cd6b2d47794f51db2cb1c1..8039d4662a0d67c6c55a4e93b9fabb55ed8585fb 100644 (file)
@@ -400,24 +400,50 @@ public abstract class AbstractCommandConsole extends Composite {
         AbstractCommandConsole.this.layout(true);
     }
 
+    private StringBuilder outputBuffer = new StringBuilder();
+    private ArrayList<StyleRange> styleRanges = new ArrayList<StyleRange>();
+    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() {