From d49167ad01099b4c35705d702b3ddc89160b42a1 Mon Sep 17 00:00:00 2001 From: Antti Villberg Date: Sat, 13 May 2017 21:36:10 +0300 Subject: [PATCH] Console printing enhancements for documents (Simupedia) #7226 Change-Id: Ib6f67ee6461dd3cec7d22b60c784631b065a4caa --- .../server/io/CommandContextImpl.java | 21 ++++++-- .../scl/Document/All.scl | 11 ++-- .../simantics/document/server/Functions.java | 50 +++++++++++++++++-- .../scl/Simantics/All.scl | 3 +- 4 files changed, 71 insertions(+), 14 deletions(-) diff --git a/bundles/org.simantics.document.server.io/src/org/simantics/document/server/io/CommandContextImpl.java b/bundles/org.simantics.document.server.io/src/org/simantics/document/server/io/CommandContextImpl.java index 3277bbd1e..5b49810fa 100644 --- a/bundles/org.simantics.document.server.io/src/org/simantics/document/server/io/CommandContextImpl.java +++ b/bundles/org.simantics.document.server.io/src/org/simantics/document/server/io/CommandContextImpl.java @@ -80,11 +80,24 @@ public class CommandContextImpl implements CommandContextMutable { @Override public String toString() { StringBuilder sb = new StringBuilder(); + sb.append("CommandContext:\n"); for (Map.Entry>> 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> value = entry.getValue(); + if(value.size() == 1) { + List t = (List)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(); } diff --git a/bundles/org.simantics.document.server/scl/Document/All.scl b/bundles/org.simantics.document.server/scl/Document/All.scl index fa83a224a..7781cfdb3 100644 --- a/bundles/org.simantics.document.server/scl/Document/All.scl +++ b/bundles/org.simantics.document.server/scl/Document/All.scl @@ -85,6 +85,9 @@ instance Coercible CommandContextMutable CommandContext where cloneCommandContext :: CommandContext -> 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 -> String displayValue0 var = propertyValue var "HasDisplayValue" -consoleLog :: Variable -> String -> () -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 -> IDocument contextDocument ctx = justValue ctx "__document__" diff --git a/bundles/org.simantics.document.server/src/org/simantics/document/server/Functions.java b/bundles/org.simantics.document.server/src/org/simantics/document/server/Functions.java index 25bec0c91..20fa2a71b 100644 --- a/bundles/org.simantics.document.server/src/org/simantics/document/server/Functions.java +++ b/bundles/org.simantics.document.server/src/org/simantics/document/server/Functions.java @@ -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 diff --git a/bundles/org.simantics.modeling/scl/Simantics/All.scl b/bundles/org.simantics.modeling/scl/Simantics/All.scl index 00d6980cf..85d42e103 100644 --- a/bundles/org.simantics.modeling/scl/Simantics/All.scl +++ b/bundles/org.simantics.modeling/scl/Simantics/All.scl @@ -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 -- 2.43.2