]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.tests.modelled.ui/src/org/simantics/tests/modelled/ui/STSVariableViewerAdapter.java
Merge "(refs #7307) Added features field to SCL module header"
[simantics/platform.git] / bundles / org.simantics.tests.modelled.ui / src / org / simantics / tests / modelled / ui / STSVariableViewerAdapter.java
1 package org.simantics.tests.modelled.ui;
2
3 import org.eclipse.core.runtime.IAdaptable;
4 import org.eclipse.jface.resource.ImageDescriptor;
5 import org.eclipse.jface.viewers.IStructuredSelection;
6 import org.eclipse.ui.IEditorInput;
7 import org.eclipse.ui.IPersistableElement;
8 import org.eclipse.ui.IWorkbenchPage;
9 import org.eclipse.ui.PartInitException;
10 import org.eclipse.ui.PlatformUI;
11 import org.simantics.Simantics;
12 import org.simantics.databoard.Bindings;
13 import org.simantics.db.ReadGraph;
14 import org.simantics.db.Resource;
15 import org.simantics.db.common.request.ReadRequest;
16 import org.simantics.db.exception.DatabaseException;
17 import org.simantics.layer0.Layer0;
18 import org.simantics.tests.modelled.ontology.TestsResource;
19 import org.simantics.ui.workbench.editor.AbstractResourceEditorAdapter;
20 import org.simantics.ui.workbench.editor.EditorAdapter;
21 import org.simantics.utils.ui.workbench.WorkbenchUtils;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 public class STSVariableViewerAdapter extends AbstractResourceEditorAdapter implements EditorAdapter {
26
27     private static final Logger LOGGER = LoggerFactory.getLogger(STSVariableViewerAdapter.class);
28     
29     public STSVariableViewerAdapter() {
30         super("STS Variable Viewer", null, 20);
31     }
32     
33     @Override
34     public boolean canHandle(ReadGraph g, Object input)
35             throws DatabaseException {
36         if(input instanceof IStructuredSelection)
37             input = ((IStructuredSelection)input).getFirstElement();
38         if(!(input instanceof Resource)) {
39             if(input instanceof IAdaptable) {
40                 input = ((IAdaptable)input).getAdapter(Resource.class);
41                 if(input == null)
42                     return false;
43             }
44             else
45                 return false;
46         }
47         Resource resource = (Resource)input;
48         return g.isInstanceOf(resource, TestsResource.getInstance(g).STSVariable);
49     }
50     
51     protected void openEditor(Resource input) throws Exception {
52         IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
53         if(page == null)
54             return;
55         Simantics.getSession().asyncRequest(new ReadRequest() {
56             @Override
57             public void run(ReadGraph graph) throws DatabaseException {
58                 String variableName = graph.getRelatedValue2(input, Layer0.getInstance(graph).HasName, Bindings.STRING);
59                 String contents = graph.getRelatedValue2(input, TestsResource.getInstance(graph).STSVariable_definition, Bindings.STRING);
60                 PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
61                     @Override
62                     public void run() {
63                         try {
64                             WorkbenchUtils.openEditor("org.simantics.tests.ui.stsVariableViewer", new STSVariableViewerEditorInput(variableName, contents));
65 //                            WorkbenchUtils.openEditor("org.simantics.tests.ui.stsVariableViewer", new STSTestEditorInput(uri));
66                         } catch (PartInitException e) {
67                             LOGGER.error("Could not initialize part", e);
68                         }
69                     }
70                 });
71             }
72         });
73     }
74     
75     public static class STSVariableViewerEditorInput implements IEditorInput {
76
77         private String name;
78         private String contents;
79
80         public STSVariableViewerEditorInput(String name, String contents) {
81             this.name = name;
82             this.contents = contents;
83         }
84         
85         @Override
86         public <T> T getAdapter(Class<T> adapter) {
87             return null;
88         }
89
90         @Override
91         public boolean exists() {
92             return true;
93         }
94
95         @Override
96         public ImageDescriptor getImageDescriptor() {
97             return null;
98         }
99
100         @Override
101         public String getName() {
102             return name;
103         }
104
105         @Override
106         public IPersistableElement getPersistable() {
107             return null;
108         }
109
110         @Override
111         public String getToolTipText() {
112             return null;
113         }
114
115         public String getContents() {
116             return contents;
117         }
118     }
119
120 }