]> gerrit.simantics Code Review - simantics/district.git/commitdiff
Open SCL script output console on demand for context menu commands. 62/2862/1
authorReino Ruusu <reino.ruusu@semantum.fi>
Tue, 23 Apr 2019 12:20:24 +0000 (15:20 +0300)
committerReino Ruusu <reino.ruusu@semantum.fi>
Tue, 23 Apr 2019 12:20:24 +0000 (15:20 +0300)
gitlab #34

Change-Id: I024b4cc70f0d2644cb527606366347e1d862ade0

org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/NetworkElementActionMenuContribution.java

index b3cc0891daf05e03d54d519a1cbe64f883368fa5..cca371728d8dcb13a95feb81ca26589f0f5bd009 100644 (file)
@@ -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<MMenuElement> items) {
+    public void aboutToShow(@Named(IServiceConstants.ACTIVE_SELECTION) Object selection, Display display, List<MMenuElement> items) {
         final List<Resource> vertices = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class);
         if (vertices.size() != 1)
             return;
@@ -64,7 +67,7 @@ public class NetworkElementActionMenuContribution {
         List<MMenuElement> 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);
+                    });
                 }
             };