]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/scl/scriptEditor/RunSCLScriptActionFactory.java
Support for SCL script database storage, editing and execution
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / scl / scriptEditor / RunSCLScriptActionFactory.java
1 /*******************************************************************************
2  * Copyright (c) 2017 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.ui.scl.scriptEditor;
13
14 import org.eclipse.core.runtime.IStatus;
15 import org.eclipse.core.runtime.Status;
16 import org.eclipse.jface.action.IStatusLineManager;
17 import org.eclipse.ui.IWorkbenchPart;
18 import org.simantics.Simantics;
19 import org.simantics.databoard.Bindings;
20 import org.simantics.db.Resource;
21 import org.simantics.db.Session;
22 import org.simantics.db.common.primitiverequest.RelatedValue;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.db.layer0.adapter.ActionFactory;
25 import org.simantics.layer0.Layer0;
26 import org.simantics.modeling.ui.Activator;
27 import org.simantics.modeling.ui.scl.SCLScripts;
28 import org.simantics.scl.compiler.commands.CommandSession;
29 import org.simantics.scl.runtime.reporting.SCLReportingHandler;
30 import org.simantics.utils.datastructures.Pair;
31 import org.simantics.utils.ui.workbench.WorkbenchUtils;
32
33 /**
34  * @author Tuukka Lehtonen
35  * @since 1.31.0
36  */
37 public class RunSCLScriptActionFactory implements ActionFactory {
38
39         @Override
40         public Runnable create(Object target) {
41                 if(!(target instanceof Resource))
42                         return null;
43                 return () -> runScript((Resource) target);
44         }
45
46         public static void runScript(Resource resource) {
47                 try {
48                         Session s = Simantics.getSession();
49                         Layer0 L0 = Layer0.getInstance(s);
50                         String error = SCLScripts.canRunScript(s, resource);
51                         IWorkbenchPart part = WorkbenchUtils.getActiveWorkbenchPart();
52                         IStatusLineManager status = part != null ? WorkbenchUtils.getStatusLine(part) : null;
53                         if (error == null) {
54                                 String scriptName = s.syncRequest(new RelatedValue<String>(resource, L0.HasName, Bindings.STRING));
55                                 String text = s.syncRequest(new RelatedValue<String>(resource, L0.SCLScript_definition, Bindings.STRING));
56                                 Pair<CommandSession, SCLReportingHandler> p = SCLScripts.getOrCreateConsoleCommandSession();
57                                 SCLScripts.runScriptWithProgress(scriptName, text, p.first, p.second);
58                                 status.setErrorMessage(null);
59                         } else {
60                                 if (status != null)
61                                         status.setErrorMessage(error);
62                         }
63                 } catch (DatabaseException e) {
64                         Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Failed to run SCL script.", e));
65                         return;
66                 }
67         }
68
69 }