X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.modeling.ui%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2Fui%2Fscl%2FscriptEditor%2FRunSCLScriptActionFactory.java;fp=bundles%2Forg.simantics.modeling.ui%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2Fui%2Fscl%2FscriptEditor%2FRunSCLScriptActionFactory.java;h=6171e218577c1efa3a23733185d4d2e44be407ae;hb=e36d0cb0313ea18c76479e8dbfaf8499fa52db98;hp=0000000000000000000000000000000000000000;hpb=0cf894be3bd64a393d8883b7500822275c2a15fe;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/scl/scriptEditor/RunSCLScriptActionFactory.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/scl/scriptEditor/RunSCLScriptActionFactory.java new file mode 100644 index 000000000..6171e2185 --- /dev/null +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/scl/scriptEditor/RunSCLScriptActionFactory.java @@ -0,0 +1,69 @@ +/******************************************************************************* + * Copyright (c) 2017 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Semantum Oy - initial API and implementation + *******************************************************************************/ +package org.simantics.modeling.ui.scl.scriptEditor; + +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.jface.action.IStatusLineManager; +import org.eclipse.ui.IWorkbenchPart; +import org.simantics.Simantics; +import org.simantics.databoard.Bindings; +import org.simantics.db.Resource; +import org.simantics.db.Session; +import org.simantics.db.common.primitiverequest.RelatedValue; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.adapter.ActionFactory; +import org.simantics.layer0.Layer0; +import org.simantics.modeling.ui.Activator; +import org.simantics.modeling.ui.scl.SCLScripts; +import org.simantics.scl.compiler.commands.CommandSession; +import org.simantics.scl.runtime.reporting.SCLReportingHandler; +import org.simantics.utils.datastructures.Pair; +import org.simantics.utils.ui.workbench.WorkbenchUtils; + +/** + * @author Tuukka Lehtonen + * @since 1.31.0 + */ +public class RunSCLScriptActionFactory implements ActionFactory { + + @Override + public Runnable create(Object target) { + if(!(target instanceof Resource)) + return null; + return () -> runScript((Resource) target); + } + + public static void runScript(Resource resource) { + try { + Session s = Simantics.getSession(); + Layer0 L0 = Layer0.getInstance(s); + String error = SCLScripts.canRunScript(s, resource); + IWorkbenchPart part = WorkbenchUtils.getActiveWorkbenchPart(); + IStatusLineManager status = part != null ? WorkbenchUtils.getStatusLine(part) : null; + if (error == null) { + String scriptName = s.syncRequest(new RelatedValue(resource, L0.HasName, Bindings.STRING)); + String text = s.syncRequest(new RelatedValue(resource, L0.SCLScript_definition, Bindings.STRING)); + Pair p = SCLScripts.getOrCreateConsoleCommandSession(); + SCLScripts.runScriptWithProgress(scriptName, text, p.first, p.second); + status.setErrorMessage(null); + } else { + if (status != null) + status.setErrorMessage(error); + } + } catch (DatabaseException e) { + Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Failed to run SCL script.", e)); + return; + } + } + +}