]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.views.swt/src/org/simantics/views/swt/ModelledEditor.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.views.swt / src / org / simantics / views / swt / ModelledEditor.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.views.swt;\r
13 \r
14 import org.eclipse.jface.viewers.ISelection;\r
15 import org.eclipse.swt.SWT;\r
16 import org.eclipse.swt.layout.FillLayout;\r
17 import org.eclipse.swt.widgets.Composite;\r
18 import org.simantics.Simantics;\r
19 import org.simantics.databoard.Bindings;\r
20 import org.simantics.db.ReadGraph;\r
21 import org.simantics.db.Resource;\r
22 import org.simantics.db.VirtualGraph;\r
23 import org.simantics.db.WriteGraph;\r
24 import org.simantics.db.common.request.WriteRequest;\r
25 import org.simantics.db.common.request.WriteResultRequest;\r
26 import org.simantics.db.common.utils.Logger;\r
27 import org.simantics.db.exception.DatabaseException;\r
28 import org.simantics.db.exception.ServiceNotFoundException;\r
29 import org.simantics.db.layer0.variable.Variable;\r
30 import org.simantics.db.request.Read;\r
31 import org.simantics.layer0.Layer0;\r
32 import org.simantics.scenegraph.ontology.ScenegraphResources;\r
33 import org.simantics.scl.runtime.function.Function3;\r
34 import org.simantics.ui.workbench.ResourceEditorPart2;\r
35 import org.simantics.utils.ui.jface.ActiveSelectionProvider;\r
36 import org.simantics.views.swt.client.base.SWTRoot;\r
37 \r
38 /**\r
39  * To use this class, first model your view contents in .pgraph files according\r
40  * to the Browsing.pgraph ontology. After that there are two ways to put your\r
41  * configuration to use by defining a new view extension:\r
42  * <ol>\r
43  * <li>Set view extension class to\r
44  * <code>org.simantics.browsing.ui.swt.ModelledView:configurationURI=ConfigURI</code>\r
45  * , where ConfigURI is the URI of your view configuration.</li>\r
46  * <li>Extend this class and override at least {@link #configurationURI()} to\r
47  * define the URI from which the configuration for the view is found. Set view\r
48  * extension class to the created class.</li>\r
49  * </ol>\r
50  * \r
51  * @author Antti Villberg\r
52  */\r
53 abstract public class ModelledEditor extends ResourceEditorPart2 {\r
54 \r
55         public SWTRoot root;\r
56     private Composite base;\r
57         \r
58     abstract protected String configurationURI();\r
59         \r
60     @Override\r
61     public void createPartControl(Composite parent) {\r
62         \r
63         try {\r
64                 \r
65                 final Variable variable = getResourceInput2().getVariable();\r
66                 \r
67             Resource runtime = Simantics.getSession().sync(new WriteResultRequest<Resource>(Simantics.getSession().getService(VirtualGraph.class)) {\r
68                 @Override\r
69                 public Resource perform(WriteGraph graph) throws DatabaseException {\r
70                         Layer0 L0 = Layer0.getInstance(graph);\r
71                         ScenegraphResources SG = ScenegraphResources.getInstance(graph);\r
72                         Resource resource = graph.newResource();\r
73                                         graph.claim(resource, L0.InstanceOf, null, SG.Runtime);\r
74                         graph.claimLiteral(resource, SG.Runtime_HasVariable, variable.getURI(graph), Bindings.STRING);\r
75                         return resource;\r
76                 }\r
77             });\r
78             \r
79             base = new Composite(parent, SWT.NONE);\r
80             base.setLayout(new FillLayout());\r
81             \r
82                         SWTViewLoaderProcess loader = new SWTViewLoaderProcess(null, null);\r
83             final Variable editorVariable = loader.getVariable(getSession(), configurationURI(), runtime);\r
84 \r
85                 final Function3<WriteGraph, Variable, Variable, Boolean> onLoaded = getSession().syncRequest(new Read<Function3<WriteGraph, Variable, Variable, Boolean>>() {\r
86 \r
87                         @Override\r
88                         public Function3<WriteGraph, Variable, Variable, Boolean> perform(ReadGraph graph) throws DatabaseException {\r
89                                 return editorVariable.getPossiblePropertyValue(graph, "onLoaded");\r
90                         }\r
91 \r
92                 });\r
93 \r
94                 if(onLoaded != null) {\r
95 \r
96                         Simantics.getSession().sync(new WriteRequest() {\r
97                     @Override\r
98                     public void perform(WriteGraph graph) throws DatabaseException {\r
99                         onLoaded.apply(graph, editorVariable, variable);\r
100                     }\r
101                 });\r
102 \r
103                 }\r
104             \r
105                         root = loader.load(getSession(), editorVariable);\r
106                         root.createControls(base);\r
107             \r
108         } catch (ServiceNotFoundException e) {\r
109             Logger.defaultLogError(e);\r
110         } catch (DatabaseException e) {\r
111             Logger.defaultLogError(e);\r
112         }\r
113         \r
114         getSite().setSelectionProvider(new ActiveSelectionProvider() {\r
115                 \r
116                 @Override\r
117                 public void setSelection(ISelection selection) {\r
118                         super.setSelection(selection);\r
119                 }\r
120                 \r
121         });\r
122         \r
123     }\r
124 \r
125     @Override\r
126     public void setFocus() {\r
127         if (root != null && root.getControl() != null && !root.getControl().isDisposed())\r
128                 root.getControl().setFocus();\r
129         else if (!base.isDisposed())\r
130             base.setFocus();\r
131     }\r
132     \r
133 }\r