From 00780eaab266d26ec16d7bbb9e882a23ef1807d0 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 b3cc0891..cca37172 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; @@ -64,7 +67,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") @@ -88,6 +91,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 @@ -102,9 +147,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