]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Console printing enhancements for documents (Simupedia) 23/523/2
authorAntti Villberg <antti.villberg@semantum.fi>
Sat, 13 May 2017 18:36:10 +0000 (21:36 +0300)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Mon, 15 May 2017 12:41:41 +0000 (15:41 +0300)
#7226

Change-Id: Ib6f67ee6461dd3cec7d22b60c784631b065a4caa

bundles/org.simantics.document.server.io/src/org/simantics/document/server/io/CommandContextImpl.java
bundles/org.simantics.document.server/scl/Document/All.scl
bundles/org.simantics.document.server/src/org/simantics/document/server/Functions.java
bundles/org.simantics.modeling/scl/Simantics/All.scl

index 3277bbd1edfe02440a1b944f7f6c192c4808875c..5b49810fad9f8109c5a4a637ab24444ea3bf059c 100644 (file)
@@ -80,11 +80,24 @@ public class CommandContextImpl implements CommandContextMutable {
        @Override
        public String toString() {
                StringBuilder sb = new StringBuilder();
+               sb.append("CommandContext:\n");
                for (Map.Entry<String, List<List<Object>>> entry : data.entrySet()) {
-                        sb.append(entry.getKey());
-                        sb.append(":");
-                        sb.append(entry.getValue());
-                        sb.append("\n");
+                       String key = entry.getKey();
+                       if(key.startsWith("__")) continue;
+                       sb.append(key);
+                       sb.append(":");
+                       List<List<Object>> value = entry.getValue();
+                       if(value.size() == 1) {
+                               List<Object> t = (List<Object>)value.get(0);
+                               if(t.size() == 2) {
+                                       sb.append(t.get(1));
+                               } else {
+                                       sb.append(t);
+                               }
+                       } else {
+                               sb.append(value);
+                       }
+                       sb.append("\n");
                }
                return sb.toString();
        }
index fa83a224acfc2d7076af4417e2f2e181680e93e7..7781cfdb3ffaa9595eb322be7d01ffc286bf48c0 100644 (file)
@@ -85,6 +85,9 @@ instance Coercible CommandContextMutable CommandContext where
 cloneCommandContext :: CommandContext -> <Proc> CommandContextMutable
 cloneCommandContext context = merge (commandContext ()) context
 
+instance Show CommandContext where
+    show ctx = printContext ctx
+
 importJava "org.simantics.document.server.io.CommandResult" where
     data CommandResult
 
@@ -143,10 +146,10 @@ displayValue var str = propertyValue (property var str) "HasDisplayValue"
 displayValue0 :: Variable -> <ReadGraph> String
 displayValue0 var = propertyValue var "HasDisplayValue"
 
-consoleLog :: Variable -> String -> <ReadGraph> ()
-consoleLog state message = do
-    console = state#console
-    runProc $ addMessage console message
+consoleLog :: CommandContext -> String -> ()
+consoleLog context message = match possibleValue context "console" with
+  Nothing -> ()
+  Just console -> runProc $ addMessage console message
 
 contextDocument :: CommandContext -> <Proc> IDocument
 contextDocument ctx = justValue ctx "__document__"
index 25bec0c919248d5efc14c54d921f3b3f5ea5fbba..20fa2a71b7b272cec574e351c715a17c25479f04 100644 (file)
@@ -51,6 +51,7 @@ import org.simantics.document.server.io.CommandContext;
 import org.simantics.document.server.io.CommandContextImpl;
 import org.simantics.document.server.io.CommandContextMutable;
 import org.simantics.document.server.io.CommandResult;
+import org.simantics.document.server.io.IConsole;
 import org.simantics.document.server.request.ServerSCLHandlerValueRequest;
 import org.simantics.document.server.request.ServerSCLValueRequest;
 import org.simantics.document.server.serverResponse.ServerResponse;
@@ -674,7 +675,9 @@ public class Functions {
                        @Override
                        public CommandResult handle(final CommandContext parameters) {
                                
-                final SCLReportingHandler printer = (SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER);
+                               IConsole console = parameters.getValue("__console__");
+                               SCLReportingHandler printer = (console != null) ? new ConsoleSCLReportingHandler(console)
+                                               : (SCLReportingHandler) SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER);
                 
                 try {
                        
@@ -727,7 +730,15 @@ public class Functions {
                                });
 
                        } else {
-                               result = fn.apply(parameters);
+                               
+                                               SCLContext sclContext = SCLContext.getCurrent();
+                                               Object oldPrinter = sclContext.put(SCLReportingHandler.REPORTING_HANDLER, printer);
+                                               try {
+                                                       result = fn.apply(parameters);
+                                               } finally {
+                                                       sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
+                                               }
+
                        }
 
                        if (result instanceof org.simantics.document.server.serverResponse.Error) {
@@ -970,9 +981,7 @@ public class Functions {
     }
     
     public static String printContext(CommandContext context) {
-       String str = context.toString();
-       System.err.println(str);
-       return str;
+       return context.toString();
     }
     
     @SCLValue(type = "AbstractEventHandler")
@@ -1164,4 +1173,35 @@ public class Functions {
        return graph.syncRequest(new PathExistsRequest(context));
     }
     
+       static class ConsoleSCLReportingHandler implements SCLReportingHandler {
+
+               private final IConsole console;
+
+               ConsoleSCLReportingHandler(IConsole console) {
+                       this.console = console;
+               }
+
+               @Override
+               public void print(String text) {
+                       console.addMessage(text);
+               }
+
+               @Override
+               public void printError(String error) {
+                       console.addMessage(error);
+               }
+
+               @Override
+               public void printCommand(String command) {
+                       console.addMessage(command);
+               }
+
+               @Override
+               public void didWork(double amount) {
+                       console.addMessage("didWork " + amount);
+               }
+
+       }
+    
+    
 }
\ No newline at end of file
index 00d6980cfb07165a33e9694eb1dfb1e6d36ade53..85d42e103d1599d4a590ad26810bce6131a1f404 100644 (file)
@@ -32,4 +32,5 @@ include "Simantics/UI"
 include "Simantics/SelectionView"
 include "Simantics/Formatting"
 include "Simantics/Action"
-include "Simantics/Testing/BrowseContext"
\ No newline at end of file
+include "Simantics/Testing/BrowseContext"
+include "Debug" as Debug
\ No newline at end of file