]> gerrit.simantics Code Review - simantics/platform.git/blob
849c5be7c2ef9025de399a4b7c2dec96caeae71f
[simantics/platform.git] /
1 package org.simantics.modeling.ui.componentTypeEditor;
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.layer0.Layer0;
14 import org.simantics.scl.ui.editor2.StandardSCLModuleEditorInput;
15 import org.simantics.ui.workbench.editor.AbstractResourceEditorAdapter;
16 import org.simantics.ui.workbench.editor.EditorAdapter;
17 import org.simantics.utils.ui.workbench.WorkbenchUtils;
18
19 public class SCLModuleEditorAdapter extends AbstractResourceEditorAdapter
20         implements EditorAdapter {
21
22     public SCLModuleEditorAdapter() {
23         super("SCL Module Editor", null, 20);
24     }
25     
26     @Override
27     public boolean canHandle(ReadGraph g, Object input)
28             throws DatabaseException {
29         if(input instanceof IStructuredSelection)
30             input = ((IStructuredSelection)input).getFirstElement();
31         if(!(input instanceof Resource)) {
32             if(input instanceof IAdaptable) {
33                 input = ((IAdaptable)input).getAdapter(Resource.class);
34                 if(input == null)
35                     return false;
36             }
37             else
38                 return false;
39         }
40         Resource resource = (Resource)input;
41         Layer0 L0 = Layer0.getInstance(g);
42         return g.isInstanceOf(resource, L0.SCLModule);
43     }
44     
45     protected void openEditor(Resource input) throws Exception {
46         IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
47         if(page == null)
48             return;
49         Simantics.getSession().asyncRequest(new ReadRequest() {
50             @Override
51             public void run(ReadGraph graph) throws DatabaseException {
52                 String uri = graph.getURI(input);
53                 PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
54                     @Override
55                     public void run() {
56                         try {
57                             WorkbenchUtils.openEditor("org.simantics.scl.ui.editor2",
58                                     new StandardSCLModuleEditorInput(uri));
59                         } catch (PartInitException e) {
60                             e.printStackTrace();
61                         }
62                     }
63                 });
64             }
65         });
66     }
67
68 }