]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/scl/SCLScripts.java
Improved Copy Visible Data usability in time series chart editor
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / scl / SCLScripts.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;
13
14 import java.io.StringReader;
15
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;
33
34 /**
35  * @author Tuukka Lehtonen
36  * @since 1.31.0
37  */
38 public class SCLScripts {
39
40         private static final Logger LOGGER = LoggerFactory.getLogger(SCLScripts.class);
41
42         private static final String SCL_SCRIPT_CONSOLE_ID = "org.simantics.scl.ui.scriptConsole";
43
44         /**
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
49          */
50         public static String canRunScript(RequestProcessor processor, final Resource script) throws DatabaseException {
51                 return null;
52         }
53
54         public static void runScriptWithProgress(String scriptName, String scriptText, CommandSession session, SCLReportingHandler handler) {
55                 Job job = new Job("Run SCL Script") {
56                         @Override
57                         protected IStatus run(IProgressMonitor monitor) {
58                                 runScriptWithProgress(monitor, scriptName, scriptText, session, handler);
59                                 return Status.OK_STATUS;
60                         }
61                 };
62                 job.setUser(true);
63                 job.schedule();
64         }
65
66         public static void runScriptWithProgress(IProgressMonitor monitor, String scriptName, String scriptText, CommandSession session, SCLReportingHandler handler) {
67                 monitor.beginTask(scriptName, IProgressMonitor.UNKNOWN);
68                 try {
69                         session.execute(new StringReader(scriptText), handler);
70                 } finally {
71                         monitor.done();
72                 }
73         }
74
75         public static Pair<CommandSession, SCLReportingHandler> getOrCreateConsoleCommandSession() {
76                 return getSCLConsoleCommandSession(true);
77         }
78
79         public static Pair<CommandSession, SCLReportingHandler> getSCLConsoleCommandSession(boolean createIfNecessary) {
80                 IWorkbenchPart part;
81                 SCLReportingHandler handler = SCLReportingHandler.DEFAULT_WITHOUT_ECHO;
82                 try {
83                         part = createIfNecessary
84                                         ? WorkbenchUtils.showView(SCL_SCRIPT_CONSOLE_ID, IWorkbenchPage.VIEW_VISIBLE)
85                                         : WorkbenchUtils.findView(SCL_SCRIPT_CONSOLE_ID);
86                         if (part != null)
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);
90                 }
91                 return Pair.make(new CommandSession(SCLOsgi.MODULE_REPOSITORY, handler), SCLReportingHandler.DEFAULT);
92         }
93
94 }