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;
14 import java.io.StringReader;
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.Status;
19 import org.eclipse.core.runtime.jobs.Job;
20 import org.eclipse.ui.IWorkbenchPage;
21 import org.eclipse.ui.IWorkbenchPart;
22 import org.eclipse.ui.PartInitException;
23 import org.simantics.db.RequestProcessor;
24 import org.simantics.db.Resource;
25 import org.simantics.db.exception.DatabaseException;
26 import org.simantics.scl.compiler.commands.CommandSession;
27 import org.simantics.scl.osgi.SCLOsgi;
28 import org.simantics.scl.runtime.reporting.SCLReportingHandler;
29 import org.simantics.utils.datastructures.Pair;
30 import org.simantics.utils.ui.workbench.WorkbenchUtils;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * @author Tuukka Lehtonen
38 public class SCLScripts {
40 private static final Logger LOGGER = LoggerFactory.getLogger(SCLScripts.class);
42 private static final String SCL_SCRIPT_CONSOLE_ID = "org.simantics.scl.ui.scriptConsole";
45 * @param processor database handle
46 * @param script the script to validate
47 * @return <code>null</code> if ok to run script, otherwise a problem description
48 * @throws DatabaseException
50 public static String canRunScript(RequestProcessor processor, final Resource script) throws DatabaseException {
54 public static void runScriptWithProgress(String scriptName, String scriptText, CommandSession session, SCLReportingHandler handler) {
55 Job job = new Job("Run SCL Script") {
57 protected IStatus run(IProgressMonitor monitor) {
58 runScriptWithProgress(monitor, scriptName, scriptText, session, handler);
59 return Status.OK_STATUS;
66 public static void runScriptWithProgress(IProgressMonitor monitor, String scriptName, String scriptText, CommandSession session, SCLReportingHandler handler) {
67 monitor.beginTask(scriptName, IProgressMonitor.UNKNOWN);
69 session.execute(new StringReader(scriptText), handler);
75 public static Pair<CommandSession, SCLReportingHandler> getOrCreateConsoleCommandSession() {
76 return getSCLConsoleCommandSession(true);
79 public static Pair<CommandSession, SCLReportingHandler> getSCLConsoleCommandSession(boolean createIfNecessary) {
81 SCLReportingHandler handler = SCLReportingHandler.DEFAULT_WITHOUT_ECHO;
83 part = createIfNecessary
84 ? WorkbenchUtils.showView(SCL_SCRIPT_CONSOLE_ID, IWorkbenchPage.VIEW_VISIBLE)
85 : WorkbenchUtils.findView(SCL_SCRIPT_CONSOLE_ID);
87 return Pair.make(new CommandSession(SCLOsgi.MODULE_REPOSITORY, handler), part.getAdapter(SCLReportingHandler.class));
88 } catch (PartInitException e) {
89 LOGGER.error("Failed to open SCL Console view. Using new CommandSession, reporting to stdout via Logger.", e);
91 return Pair.make(new CommandSession(SCLOsgi.MODULE_REPOSITORY, handler), SCLReportingHandler.DEFAULT);