]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
diff --git a/bundles/org.simantics.views.swt/src/org/simantics/views/swt/ModelledEditor.java b/bundles/org.simantics.views.swt/src/org/simantics/views/swt/ModelledEditor.java
new file mode 100644 (file)
index 0000000..2eaf658
--- /dev/null
@@ -0,0 +1,133 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.views.swt;\r
+\r
+import org.eclipse.jface.viewers.ISelection;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.layout.FillLayout;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.simantics.Simantics;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.VirtualGraph;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.request.WriteRequest;\r
+import org.simantics.db.common.request.WriteResultRequest;\r
+import org.simantics.db.common.utils.Logger;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.exception.ServiceNotFoundException;\r
+import org.simantics.db.layer0.variable.Variable;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.scenegraph.ontology.ScenegraphResources;\r
+import org.simantics.scl.runtime.function.Function3;\r
+import org.simantics.ui.workbench.ResourceEditorPart2;\r
+import org.simantics.utils.ui.jface.ActiveSelectionProvider;\r
+import org.simantics.views.swt.client.base.SWTRoot;\r
+\r
+/**\r
+ * To use this class, first model your view contents in .pgraph files according\r
+ * to the Browsing.pgraph ontology. After that there are two ways to put your\r
+ * configuration to use by defining a new view extension:\r
+ * <ol>\r
+ * <li>Set view extension class to\r
+ * <code>org.simantics.browsing.ui.swt.ModelledView:configurationURI=ConfigURI</code>\r
+ * , where ConfigURI is the URI of your view configuration.</li>\r
+ * <li>Extend this class and override at least {@link #configurationURI()} to\r
+ * define the URI from which the configuration for the view is found. Set view\r
+ * extension class to the created class.</li>\r
+ * </ol>\r
+ * \r
+ * @author Antti Villberg\r
+ */\r
+abstract public class ModelledEditor extends ResourceEditorPart2 {\r
+\r
+       public SWTRoot root;\r
+    private Composite base;\r
+       \r
+    abstract protected String configurationURI();\r
+       \r
+    @Override\r
+    public void createPartControl(Composite parent) {\r
+       \r
+        try {\r
+               \r
+               final Variable variable = getResourceInput2().getVariable();\r
+               \r
+            Resource runtime = Simantics.getSession().sync(new WriteResultRequest<Resource>(Simantics.getSession().getService(VirtualGraph.class)) {\r
+                @Override\r
+                public Resource perform(WriteGraph graph) throws DatabaseException {\r
+                       Layer0 L0 = Layer0.getInstance(graph);\r
+                       ScenegraphResources SG = ScenegraphResources.getInstance(graph);\r
+                       Resource resource = graph.newResource();\r
+                                       graph.claim(resource, L0.InstanceOf, null, SG.Runtime);\r
+                       graph.claimLiteral(resource, SG.Runtime_HasVariable, variable.getURI(graph), Bindings.STRING);\r
+                       return resource;\r
+                }\r
+            });\r
+            \r
+            base = new Composite(parent, SWT.NONE);\r
+            base.setLayout(new FillLayout());\r
+            \r
+                       SWTViewLoaderProcess loader = new SWTViewLoaderProcess(null, null);\r
+            final Variable editorVariable = loader.getVariable(getSession(), configurationURI(), runtime);\r
+\r
+               final Function3<WriteGraph, Variable, Variable, Boolean> onLoaded = getSession().syncRequest(new Read<Function3<WriteGraph, Variable, Variable, Boolean>>() {\r
+\r
+                       @Override\r
+                       public Function3<WriteGraph, Variable, Variable, Boolean> perform(ReadGraph graph) throws DatabaseException {\r
+                               return editorVariable.getPossiblePropertyValue(graph, "onLoaded");\r
+                       }\r
+\r
+               });\r
+\r
+               if(onLoaded != null) {\r
+\r
+                       Simantics.getSession().sync(new WriteRequest() {\r
+                    @Override\r
+                    public void perform(WriteGraph graph) throws DatabaseException {\r
+                       onLoaded.apply(graph, editorVariable, variable);\r
+                    }\r
+                });\r
+\r
+               }\r
+            \r
+                       root = loader.load(getSession(), editorVariable);\r
+                       root.createControls(base);\r
+            \r
+        } catch (ServiceNotFoundException e) {\r
+            Logger.defaultLogError(e);\r
+        } catch (DatabaseException e) {\r
+            Logger.defaultLogError(e);\r
+        }\r
+        \r
+        getSite().setSelectionProvider(new ActiveSelectionProvider() {\r
+               \r
+               @Override\r
+               public void setSelection(ISelection selection) {\r
+                       super.setSelection(selection);\r
+               }\r
+               \r
+        });\r
+        \r
+    }\r
+\r
+    @Override\r
+    public void setFocus() {\r
+       if (root != null && root.getControl() != null && !root.getControl().isDisposed())\r
+               root.getControl().setFocus();\r
+       else if (!base.isDisposed())\r
+           base.setFocus();\r
+    }\r
+    \r
+}\r