--- /dev/null
+package org.simantics.sysdyn.ui.sg;\r
+\r
+import java.awt.AWTEvent;\r
+import java.awt.event.AWTEventListener;\r
+import java.awt.geom.Rectangle2D;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.Session;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.request.ReadRequest;\r
+import org.simantics.db.common.request.WriteRequest;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.h2d.diagram.IDiagram;\r
+import org.simantics.h2d.diagram.IDiagramListener;\r
+import org.simantics.h2d.element.IElement;\r
+import org.simantics.objmap.IMapping;\r
+import org.simantics.objmap.IMappingListener;\r
+import org.simantics.objmap.MappingException;\r
+import org.simantics.objmap.Mappings;\r
+import org.simantics.scenegraph.adapters.ISceneGraphProvider;\r
+import org.simantics.scenegraph.g2d.G2DParentNode;\r
+import org.simantics.scenegraph.g2d.G2DSceneGraph;\r
+import org.simantics.scenegraph.g2d.nodes.NavigationNode;\r
+import org.simantics.scenegraph.g2d.nodes.PageBorderNode;\r
+import org.simantics.scenegraph.g2d.nodes.TransformNode;\r
+import org.simantics.sysdyn.ui.editor.SysdynDiagramSchema;\r
+\r
+public class SysdynSceneGraphProvider implements ISceneGraphProvider, AWTEventListener {\r
+\r
+ protected Resource resource;\r
+\r
+ IDiagram diagram;\r
+ IMapping mapping;\r
+\r
+ public SysdynSceneGraphProvider(ReadGraph g, final Resource resource) {\r
+ this.resource = resource;\r
+ \r
+ SysdynDiagramSchema schema;\r
+ try {\r
+ schema = new SysdynDiagramSchema(g);\r
+ } catch (DatabaseException e1) {\r
+ // TODO Auto-generated catch block\r
+ e1.printStackTrace();\r
+ return;\r
+ }\r
+ \r
+ mapping = Mappings.createWithListening(schema);\r
+ \r
+ try {\r
+ diagram = (IDiagram)mapping.map(g, resource);\r
+ } catch (MappingException e) {\r
+ // TODO Auto-generated catch block\r
+ e.printStackTrace();\r
+ }\r
+ \r
+ final Session session = g.getSession();\r
+ mapping.addMappingListener(new IMappingListener() {\r
+ \r
+ @Override\r
+ public void rangeModified() { \r
+ session.asyncRequest(new WriteRequest() {\r
+ \r
+ @Override\r
+ public void perform(WriteGraph graph)\r
+ throws DatabaseException {\r
+ try {\r
+ mapping.updateDomain(graph);\r
+ } catch(Exception e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+ \r
+ });\r
+ }\r
+ \r
+ @Override\r
+ public void domainModified() {\r
+ session.asyncRequest(new ReadRequest() {\r
+ \r
+ @Override\r
+ public void run(ReadGraph graph) throws DatabaseException {\r
+ try {\r
+ mapping.updateRange(graph);\r
+ } catch(Exception e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+ });\r
+ }\r
+ });\r
+ }\r
+\r
+ @Override\r
+ public void dispose() {\r
+ \r
+ }\r
+ \r
+ @Override\r
+ public AWTEventListener getEventListener() {\r
+ return this;\r
+ }\r
+\r
+ @Override\r
+ public G2DSceneGraph initializeSceneGraph(G2DSceneGraph sg) {\r
+ return initializeSceneGraph(sg, null);\r
+ }\r
+\r
+ G2DSceneGraph sceneGraph;\r
+ TransformNode diagramCoordinatesNode;\r
+ G2DParentNode elementsNode;\r
+ G2DParentNode selectionNode;\r
+ G2DParentNode actionNode;\r
+\r
+ @Override\r
+ public G2DSceneGraph initializeSceneGraph(G2DSceneGraph sg, String view) {\r
+ sceneGraph = sg;\r
+ diagramCoordinatesNode = sceneGraph.addNode(NavigationNode.class);\r
+\r
+ PageBorderNode border = diagramCoordinatesNode.addNode(PageBorderNode.class);\r
+ border.init(new Rectangle2D.Double(0.0, 0.0, 297.0, 210.0), new Rectangle2D.Double(10.0, 10.0, 277.0, 190.0), Boolean.TRUE);\r
+\r
+ elementsNode = diagramCoordinatesNode.addNode(G2DParentNode.class);\r
+ elementsNode.setZIndex(0);\r
+\r
+ selectionNode = diagramCoordinatesNode.addNode(G2DParentNode.class);\r
+ selectionNode.setZIndex(1);\r
+\r
+ actionNode = diagramCoordinatesNode.addNode(G2DParentNode.class);\r
+ actionNode.setZIndex(2);\r
+ \r
+ for(IElement element : diagram.getElements())\r
+ element.init(elementsNode);\r
+ diagram.addDiagramListener(new IDiagramListener() {\r
+ \r
+ @Override\r
+ public void elementAdded(IElement element) {\r
+ element.init(elementsNode);\r
+ }\r
+\r
+ @Override\r
+ public void elementRemoved(IElement element) {\r
+ element.remove();\r
+ }\r
+ });\r
+ \r
+ return sg;\r
+ }\r
+\r
+ @Override\r
+ public void setHint(Object key, Object value) {\r
+ // TODO Auto-generated method stub\r
+\r
+ }\r
+\r
+ @Override\r
+ public void eventDispatched(AWTEvent event) {\r
+ \r
+ }\r
+\r
+}\r