]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ProceduralComponentInstanceViewerEditorAdapter.java
Externalize strings
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / componentTypeEditor / ProceduralComponentInstanceViewerEditorAdapter.java
1 package org.simantics.modeling.ui.componentTypeEditor;
2
3 import org.eclipse.jface.resource.ImageDescriptor;
4 import org.eclipse.ui.PartInitException;
5 import org.eclipse.ui.PlatformUI;
6 import org.simantics.Simantics;
7 import org.simantics.db.ReadGraph;
8 import org.simantics.db.Resource;
9 import org.simantics.db.common.request.ReadRequest;
10 import org.simantics.db.exception.DatabaseException;
11 import org.simantics.modeling.ModelingResources;
12 import org.simantics.modeling.ui.Activator;
13 import org.simantics.structural.stubs.StructuralResource2;
14 import org.simantics.ui.workbench.ResourceEditorInput;
15 import org.simantics.ui.workbench.editor.AbstractResourceEditorAdapter;
16
17 public class ProceduralComponentInstanceViewerEditorAdapter extends
18         AbstractResourceEditorAdapter {
19     
20     public static final ImageDescriptor ICON = ImageDescriptor.createFromURL(Activator.getDefault().getBundle()
21             .getResource("icons/shape_3d_gray.png")); //$NON-NLS-1$
22     public static final String EDITOR_ID = "org.simantics.modeling.ui.proceduralComponentInstanceViewer"; //$NON-NLS-1$
23     
24     public ProceduralComponentInstanceViewerEditorAdapter() {
25         super(Messages.ProceduralComponentInstanceViewerEditorAdapter_ProceduralComponentInstanceViewer, ICON, -10);
26     }
27
28     private static Resource browseComponent(ReadGraph graph, Resource input) throws DatabaseException {
29         ModelingResources MOD = ModelingResources.getInstance(graph);
30         
31         Resource temp = graph.getPossibleObject(input, MOD.ElementToComponent);
32         if(temp != null)
33             return temp;
34         else
35             return input;
36     }
37     
38     @Override
39     protected boolean canHandle(ReadGraph graph, Resource input)
40             throws DatabaseException {
41         input = browseComponent(graph, input);
42         StructuralResource2 STR = StructuralResource2.getInstance(graph);
43         Resource type = graph.getPossibleType(input, STR.Component);
44         if(type == null)
45             return false;
46         return graph.isInstanceOf(type, STR.ProceduralComponentType);
47     }
48     
49     @Override
50     protected void openEditor(final Resource input) throws Exception {
51         Simantics.getSession().asyncRequest(new ReadRequest() {
52             @Override
53             public void run(ReadGraph graph) throws DatabaseException {
54                 final Resource actualInput = browseComponent(graph, input);
55                 PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
56                     @Override
57                     public void run() {
58                         try {
59                             openEditorWithId(EDITOR_ID, new ResourceEditorInput(EDITOR_ID, actualInput));
60                         } catch (PartInitException e) {
61                             e.printStackTrace();
62                         }
63                     }
64                     
65                 });
66             }
67             
68         });
69     }
70     
71 }