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;
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;
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;
List<MMenuElement> children = subMenu.getChildren();
subMenu.setLabel("Component Actions");
items.add(subMenu);
-
+
for (Tuple2 action : actions) {
String label = (String) action.c0;
@SuppressWarnings("rawtypes")
@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
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);
+ });
}
};