From 9e2f4897402a40266b5f6d3897d6548e89b676f3 Mon Sep 17 00:00:00 2001 From: Reino Ruusu Date: Tue, 23 Apr 2019 15:20:24 +0300 Subject: [PATCH] Open SCL script output console on demand for context menu commands. gitlab #34 Change-Id: I024b4cc70f0d2644cb527606366347e1d862ade0 --- .../NetworkElementActionMenuContribution.java | 62 +++++++++++++++++-- 1 file changed, 58 insertions(+), 4 deletions(-) diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/NetworkElementActionMenuContribution.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/NetworkElementActionMenuContribution.java index ec03ab87..e29d11a2 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/NetworkElementActionMenuContribution.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/NetworkElementActionMenuContribution.java @@ -14,6 +14,7 @@ import org.eclipse.e4.ui.model.application.ui.menu.MMenu; import org.eclipse.e4.ui.model.application.ui.menu.MMenuElement; import org.eclipse.e4.ui.model.application.ui.menu.MMenuFactory; import org.eclipse.e4.ui.services.IServiceConstants; +import org.eclipse.swt.widgets.Display; import org.simantics.Simantics; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; @@ -30,10 +31,12 @@ import org.simantics.db.layer0.variable.Variables; import org.simantics.district.network.ontology.DistrictNetworkResource; import org.simantics.layer0.Layer0; import org.simantics.modeling.ModelingResources; +import org.simantics.modeling.ui.scl.SCLScripts; import org.simantics.scl.compiler.top.ValueNotFound; import org.simantics.scl.osgi.SCLOsgi; import org.simantics.scl.runtime.SCLContext; import org.simantics.scl.runtime.function.Function; +import org.simantics.scl.runtime.reporting.SCLReportingHandler; import org.simantics.scl.runtime.tuple.Tuple2; import org.simantics.utils.ui.ISelectionUtils; import org.slf4j.Logger; @@ -44,7 +47,7 @@ public class NetworkElementActionMenuContribution { static private Logger LOGGER = LoggerFactory.getLogger(NetworkElementActionMenuContribution.class); @AboutToShow - public void aboutToShow(@Named(IServiceConstants.ACTIVE_SELECTION) Object selection, List items) { + public void aboutToShow(@Named(IServiceConstants.ACTIVE_SELECTION) Object selection, Display display, List items) { final List vertices = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class); if (vertices.size() != 1) return; @@ -67,7 +70,7 @@ public class NetworkElementActionMenuContribution { List children = subMenu.getChildren(); subMenu.setLabel("Component Actions"); items.add(subMenu); - + for (Tuple2 action : actions) { String label = (String) action.c0; @SuppressWarnings("rawtypes") @@ -91,6 +94,48 @@ public class NetworkElementActionMenuContribution { @Execute public void execute() { + // Handler that only opens the SCL script console on demand + SCLReportingHandler handler = new SCLReportingHandler() { + + private SCLReportingHandler handler; + + // Get a handler for the SCL script console view + private SCLReportingHandler getHandler() { + if (handler == null) { + handler = SCLScripts.getOrCreateConsoleCommandSession().second; + } + return handler; + } + + @Override + public void printError(String error) { + display.asyncExec(() -> { + getHandler().printError(error); + }); + } + + @Override + public void printCommand(String command) { + display.asyncExec(() -> { + getHandler().printCommand(command); + }); + } + + @Override + public void print(String text) { + display.asyncExec(() -> { + getHandler().print(text); + }); + } + + @Override + public void didWork(double amount) { + display.asyncExec(() -> { + getHandler().didWork(amount); + }); + } + }; + Simantics.getSession().asyncRequest(new WriteRequest() { @SuppressWarnings("unchecked") @Override @@ -105,9 +150,18 @@ public class NetworkElementActionMenuContribution { graph.markUndoPoint(); Layer0Utils.addCommentMetadata(graph, label + " for " + v.getName(graph)); - Simantics.applySCLWrite(graph, function, v); + SCLContext context = SCLContext.getCurrent(); + Object oldHandler = context.put(SCLReportingHandler.REPORTING_HANDLER, handler); + try { + Simantics.applySCLWrite(graph, function, v); + } + finally { + context.put(SCLReportingHandler.REPORTING_HANDLER, oldHandler); + } } - }, (DatabaseException e) -> LOGGER.error("Running command " + label + " for " + vertex + " failed", e)); + }, (DatabaseException e) -> { + if (e != null) LOGGER.error("Running command " + label + " for " + vertex + " failed", e); + }); } }; -- 2.47.1