]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.spreadsheet.ui/src/org/simantics/spreadsheet/ui/SheetFactory.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.spreadsheet.ui / src / org / simantics / spreadsheet / ui / SheetFactory.java
diff --git a/bundles/org.simantics.spreadsheet.ui/src/org/simantics/spreadsheet/ui/SheetFactory.java b/bundles/org.simantics.spreadsheet.ui/src/org/simantics/spreadsheet/ui/SheetFactory.java
new file mode 100644 (file)
index 0000000..7f43496
--- /dev/null
@@ -0,0 +1,156 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007- VTT Technical Research Centre of Finland.\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.spreadsheet.ui;\r
+\r
+import java.awt.geom.AffineTransform;\r
+\r
+import org.simantics.db.AsyncReadGraph;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.Session;\r
+import org.simantics.db.common.request.ReadRequest;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.variable.Variable;\r
+import org.simantics.db.procedure.AsyncProcedure;\r
+import org.simantics.diagram.adapter.ElementFactory;\r
+import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;\r
+import org.simantics.diagram.ui.DiagramModelHints;\r
+import org.simantics.g2d.canvas.ICanvasContext;\r
+import org.simantics.g2d.diagram.IDiagram;\r
+import org.simantics.g2d.element.ElementClass;\r
+import org.simantics.g2d.element.ElementHints;\r
+import org.simantics.g2d.element.ElementUtils;\r
+import org.simantics.g2d.element.IElement;\r
+import org.simantics.g2d.element.handler.impl.StaticObjectAdapter;\r
+import org.simantics.operation.Layer0X;\r
+import org.simantics.scenegraph.INode;\r
+import org.simantics.spreadsheet.graph.GraphUI;\r
+import org.simantics.spreadsheet.resource.SpreadsheetResource;\r
+import org.simantics.utils.datastructures.Callback;\r
+\r
+\r
+/**\r
+ * TODO: recognize experiment disposal and reset monitor contents at that point\r
+ */\r
+public class SheetFactory implements ElementFactory {\r
+\r
+    public static ElementClass createSheetClass(Resource elementType) {\r
+        return SheetClass.INSTANCE.newClassWith(new StaticObjectAdapter(elementType));//create(null, null, null, 1.0, 1.0, new StaticObjectAdapter(elementType));\r
+    }\r
+\r
+    @Override\r
+    public void getClass(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram,\r
+            Resource resource, AsyncProcedure<ElementClass> procedure) {\r
+        procedure.execute(graph, createSheetClass(graph.getSession().getService(SpreadsheetResource.class).Spreadsheet));\r
+    }\r
+\r
+    @Override\r
+    public void create(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram,\r
+            Resource elementType, AsyncProcedure<ElementClass> procedure) {\r
+        procedure.execute(graph, createSheetClass(graph.getSession().getService(SpreadsheetResource.class).Spreadsheet));\r
+    }\r
+\r
+    @Override\r
+    public void load(AsyncReadGraph graph, ICanvasContext canvas, final IDiagram diagram, final Resource resource, final IElement element, final AsyncProcedure<IElement> procedure) {\r
+\r
+        final SpreadsheetResource sr = graph.getService(SpreadsheetResource.class);\r
+\r
+        AffineTransform at = new AffineTransform();\r
+        ElementUtils.setTransform(element, at);\r
+\r
+        System.out.println("SheetFactory loads sheet at " + resource);\r
+\r
+        graph.forPossibleObject(resource, sr.HasSheet, new AsyncProcedure<Resource>() {\r
+\r
+            @Override\r
+            public void exception(AsyncReadGraph graph, Throwable throwable) {\r
+                procedure.exception(graph, throwable);\r
+            }\r
+\r
+            @Override\r
+            public void execute(AsyncReadGraph graph, final Resource sheet) {\r
+\r
+                if (sheet != null) {\r
+\r
+                    graph.asyncRequest(new ReadRequest() {\r
+\r
+                        @Override\r
+                        public void run(ReadGraph graph) throws DatabaseException {\r
+\r
+                            Layer0X L0X = Layer0X.getInstance(graph);\r
+                            \r
+                            final Session session = graph.getSession();\r
+\r
+                            AffineTransform at = DiagramGraphUtil.getAffineTransform(graph, resource);\r
+                            ElementUtils.setTransform(element, at);\r
+\r
+//                            GraphBackend backend = new GraphBackend(session, session.getService(VirtualGraph.class));\r
+                            final GraphUI ui = new GraphUI(session);\r
+\r
+                            final String rvi = graph.getPossibleRelatedValue(resource, sr.HasRVI);\r
+                            System.out.println("SheetFactory loads rvi = " + rvi);\r
+\r
+                            final Resource model = graph.getResource((String)diagram.getHint(DiagramModelHints.KEY_DIAGRAM_MODEL_URI));\r
+\r
+                            Resource realization = graph.getPossibleObject(model, L0X.HasBaseRealization);\r
+\r
+                            Variable base = graph.adapt(realization, Variable.class);\r
+                            System.out.println("SheetFactory loads base = " + base.getURI(graph));\r
+\r
+                            final Variable sheetVariable = base.browse(graph, rvi);\r
+\r
+//                            try {\r
+////                                backend.load(session, model, sheetVariable);\r
+//\r
+//                            } catch (DatabaseException e) {\r
+//                                e.printStackTrace();\r
+//                            }\r
+\r
+                            element.setHint(SheetClass.KEY_SHEET, sheet);\r
+                            element.setHint(SheetClass.KEY_RVI, rvi);\r
+\r
+                            element.setHint(ElementHints.KEY_SG_CALLBACK, new Callback<INode>() {\r
+\r
+                                @Override\r
+                                public void run(INode _node) {\r
+\r
+                                    final SheetNode node = (SheetNode)_node;\r
+\r
+                                    try {\r
+                                        ui.load(sheetVariable, node.getModifier());\r
+                                    } catch (DatabaseException e) {\r
+                                        e.printStackTrace();\r
+                                    }\r
+\r
+                                }\r
+\r
+                            });\r
+\r
+\r
+                            // This is called too early as backend.load is\r
+                            // definitely not complete at this time, but right now that is\r
+                            // acceptable from the implementation point-of-view.\r
+                            procedure.execute(graph, element);\r
+\r
+                        }\r
+\r
+                    });\r
+\r
+                }\r
+\r
+            }\r
+\r
+        });\r
+\r
+    }\r
+\r
+\r
+}
\ No newline at end of file