]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/scl/scriptEditor/RunSCLScriptHandler.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 / RunSCLScriptHandler.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.commands.AbstractHandler;
15 import org.eclipse.core.commands.ExecutionEvent;
16 import org.eclipse.core.commands.ExecutionException;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.Status;
19 import org.eclipse.jface.action.IStatusLineManager;
20 import org.eclipse.jface.text.IDocument;
21 import org.eclipse.ui.IEditorInput;
22 import org.eclipse.ui.IEditorPart;
23 import org.eclipse.ui.handlers.HandlerUtil;
24 import org.eclipse.ui.texteditor.ITextEditor;
25 import org.simantics.Simantics;
26 import org.simantics.db.RequestProcessor;
27 import org.simantics.db.Resource;
28 import org.simantics.db.common.primitiverequest.PossibleResource;
29 import org.simantics.db.exception.DatabaseException;
30 import org.simantics.modeling.ui.Activator;
31 import org.simantics.modeling.ui.scl.SCLScripts;
32 import org.simantics.scl.compiler.commands.CommandSession;
33 import org.simantics.scl.runtime.reporting.SCLReportingHandler;
34 import org.simantics.utils.datastructures.Pair;
35 import org.simantics.utils.ui.workbench.WorkbenchUtils;
36
37 /**
38  * @author Tuukka Lehtonen
39  * @since 1.31.0
40  */
41 public class RunSCLScriptHandler extends AbstractHandler {
42
43         @Override
44         public Object execute(ExecutionEvent event) throws ExecutionException {
45                 IEditorPart editor = HandlerUtil.getActiveEditorChecked(event);
46                 IStatusLineManager status = WorkbenchUtils.getStatusLine(editor);
47                 try {
48                         final Resource script = getInputResource(Simantics.getSession(), editor);
49                         if (script == null)
50                                 return null;
51                         String error = SCLScripts.canRunScript(Simantics.getSession(), script);
52                         if (error == null) {
53                                 String textSnapshot = getDocumentText(editor);
54                                 if (textSnapshot != null) {
55                                         Pair<CommandSession, SCLReportingHandler> p = SCLScripts.getOrCreateConsoleCommandSession();
56                                         SCLScripts.runScriptWithProgress(editor.getTitle(), textSnapshot, p.first, p.second);
57                                         status.setErrorMessage(null);
58                                 }
59                         } else {
60                                 status.setErrorMessage(error);
61                         }
62                 } catch (DatabaseException ex) {
63                         Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Failed to run SCL script.", ex));
64                 }
65                 return null;
66         }
67
68         private static Resource getInputResource(RequestProcessor processor, IEditorPart editor) throws DatabaseException {
69                 IEditorInput input = editor.getEditorInput();
70                 return (input instanceof SCLScriptEditorInput)
71                                 ? processor.syncRequest(new PossibleResource(((SCLScriptEditorInput) input).getScriptURI()))
72                                 : null;
73         }
74
75         private static String getDocumentText(IEditorPart editor) {
76                 if (!(editor instanceof ITextEditor))
77                         return null;
78                 IDocument document = ((ITextEditor) editor).getDocumentProvider().getDocument(editor.getEditorInput());
79                 return document == null ? null : document.get();
80         }
81
82 }