]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/scl/scriptEditor/SCLScriptEditorAdapter.java
(refs #7571) Fixed incorrect handling of effects in edo
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / scl / scriptEditor / SCLScriptEditorAdapter.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.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;
30
31 /**
32  * @author Tuukka Lehtonen
33  * @since 1.31.0
34  */
35 public class SCLScriptEditorAdapter extends AbstractResourceEditorAdapter implements EditorAdapter {
36
37     private static final Logger LOGGER = LoggerFactory.getLogger(SCLScriptEditorAdapter.class);
38     
39     public SCLScriptEditorAdapter() {
40         super("SCL Script Editor", null, 20);
41     }
42
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);
49                 if (input == null)
50                     return null;
51             }
52             else
53                 return null;
54         }
55         return (Resource) input;
56     }
57
58     @Override
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);
62     }
63
64     protected void openEditor(Resource input) throws Exception {
65         IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
66         if (page == null)
67             return;
68         Simantics.getSession().asyncRequest(new ReadRequest() {
69             @Override
70             public void run(ReadGraph graph) throws DatabaseException {
71                 String uri = graph.getURI(input);
72                 PlatformUI.getWorkbench().getDisplay().asyncExec(openEditor(uri));
73             }
74         });
75     }
76
77     private static Runnable openEditor(String uri) {
78         return () -> {
79             try {
80                 WorkbenchUtils.openEditor("org.simantics.modeling.ui.scl.scriptEditor", new SCLScriptEditorInput(uri));
81             } catch (PartInitException e) {
82                 LOGGER.error("Could not initialize part", e);
83             }
84         };
85     }
86
87 }