Editor for modelled STS variables for easier debugging
[simantics/platform.git] / bundles / org.simantics.tests.modelled.ui / src / org / simantics / tests / modelled / ui / STSEditorAdapter.java
1 package org.simantics.tests.modelled.ui;
2
3 import org.eclipse.core.runtime.IAdaptable;
4 import org.eclipse.jface.viewers.IStructuredSelection;
5 import org.eclipse.ui.IWorkbenchPage;
6 import org.eclipse.ui.PartInitException;
7 import org.eclipse.ui.PlatformUI;
8 import org.simantics.Simantics;
9 import org.simantics.db.ReadGraph;
10 import org.simantics.db.Resource;
11 import org.simantics.db.common.request.ReadRequest;
12 import org.simantics.db.exception.DatabaseException;
13 import org.simantics.tests.modelled.ontology.TestsResource;
14 import org.simantics.ui.workbench.editor.AbstractResourceEditorAdapter;
15 import org.simantics.ui.workbench.editor.EditorAdapter;
16 import org.simantics.utils.ui.workbench.WorkbenchUtils;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 public class STSEditorAdapter extends AbstractResourceEditorAdapter implements EditorAdapter {
21
22     private static final Logger LOGGER = LoggerFactory.getLogger(STSEditorAdapter.class);
23     
24     public STSEditorAdapter() {
25         super("STS Test Editor", null, 20);
26     }
27     
28     @Override
29     public boolean canHandle(ReadGraph g, Object input)
30             throws DatabaseException {
31         if(input instanceof IStructuredSelection)
32             input = ((IStructuredSelection)input).getFirstElement();
33         if(!(input instanceof Resource)) {
34             if(input instanceof IAdaptable) {
35                 input = ((IAdaptable)input).getAdapter(Resource.class);
36                 if(input == null)
37                     return false;
38             }
39             else
40                 return false;
41         }
42         Resource resource = (Resource)input;
43         return g.isInstanceOf(resource, TestsResource.getInstance(g).STSTest);
44     }
45     
46     protected void openEditor(Resource input) throws Exception {
47         IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
48         if(page == null)
49             return;
50         Simantics.getSession().asyncRequest(new ReadRequest() {
51             @Override
52             public void run(ReadGraph graph) throws DatabaseException {
53                 String uri = graph.getURI(input);
54                 PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
55                     @Override
56                     public void run() {
57                         try {
58                             WorkbenchUtils.openEditor("org.simantics.tests.ui.stsTestEditor", new STSTestEditorInput(uri));
59                         } catch (PartInitException e) {
60                             LOGGER.error("Could not initialize part", e);
61                         }
62                     }
63                 });
64             }
65         });
66     }
67
68 }