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.runtime.IAdaptable;
15 import org.eclipse.jface.viewers.IStructuredSelection;
16 import org.eclipse.ui.IWorkbenchPage;
17 import org.eclipse.ui.PartInitException;
18 import org.eclipse.ui.PlatformUI;
19 import org.simantics.Simantics;
20 import org.simantics.db.ReadGraph;
21 import org.simantics.db.Resource;
22 import org.simantics.db.common.request.ReadRequest;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.layer0.Layer0;
25 import org.simantics.ui.workbench.editor.AbstractResourceEditorAdapter;
26 import org.simantics.ui.workbench.editor.EditorAdapter;
27 import org.simantics.utils.ui.workbench.WorkbenchUtils;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * @author Tuukka Lehtonen
35 public class SCLScriptEditorAdapter extends AbstractResourceEditorAdapter implements EditorAdapter {
37 private static final Logger LOGGER = LoggerFactory.getLogger(SCLScriptEditorAdapter.class);
39 public SCLScriptEditorAdapter() {
40 super("SCL Script Editor", null, 20);
43 private Resource toResource(Object input) {
44 if (input instanceof IStructuredSelection)
45 input = ((IStructuredSelection)input).getFirstElement();
46 if (!(input instanceof Resource)) {
47 if(input instanceof IAdaptable) {
48 input = ((IAdaptable)input).getAdapter(Resource.class);
55 return (Resource) input;
59 public boolean canHandle(ReadGraph g, Object input) throws DatabaseException {
60 Resource resource = toResource(input);
61 return resource != null && g.isInstanceOf(resource, Layer0.getInstance(g).SCLScript);
64 protected void openEditor(Resource input) throws Exception {
65 IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
68 Simantics.getSession().asyncRequest(new ReadRequest() {
70 public void run(ReadGraph graph) throws DatabaseException {
71 String uri = graph.getURI(input);
72 PlatformUI.getWorkbench().getDisplay().asyncExec(openEditor(uri));
77 private static Runnable openEditor(String uri) {
80 WorkbenchUtils.openEditor("org.simantics.modeling.ui.scl.scriptEditor", new SCLScriptEditorInput(uri));
81 } catch (PartInitException e) {
82 LOGGER.error("Could not initialize part", e);