1 /*******************************************************************************
2 * Copyright (c) 2017 Association for Decentralized Information Management
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
10 * Semantum Oy - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.ui.scl.scriptEditor;
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;
38 * @author Tuukka Lehtonen
41 public class RunSCLScriptHandler extends AbstractHandler {
44 public Object execute(ExecutionEvent event) throws ExecutionException {
45 IEditorPart editor = HandlerUtil.getActiveEditorChecked(event);
46 IStatusLineManager status = WorkbenchUtils.getStatusLine(editor);
48 final Resource script = getInputResource(Simantics.getSession(), editor);
51 String error = SCLScripts.canRunScript(Simantics.getSession(), script);
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);
60 status.setErrorMessage(error);
62 } catch (DatabaseException ex) {
63 Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Failed to run SCL script.", ex));
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()))
75 private static String getDocumentText(IEditorPart editor) {
76 if (!(editor instanceof ITextEditor))
78 IDocument document = ((ITextEditor) editor).getDocumentProvider().getDocument(editor.getEditorInput());
79 return document == null ? null : document.get();