]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagram/SheetFactory.java.keep
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / diagram / SheetFactory.java.keep
diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagram/SheetFactory.java.keep b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagram/SheetFactory.java.keep
new file mode 100644 (file)
index 0000000..e87e26f
--- /dev/null
@@ -0,0 +1,87 @@
+/*******************************************************************************\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.modeling.ui.diagram;\r
+\r
+import java.awt.geom.AffineTransform;\r
+\r
+import org.simantics.db.AsyncReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.procedure.AsyncProcedure;\r
+import org.simantics.diagram.adapter.ElementFactory;\r
+import org.simantics.diagram.synchronization.graph.BasicResources;\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.ElementUtils;\r
+import org.simantics.g2d.element.IElement;\r
+import org.simantics.g2d.element.handler.impl.StaticObjectAdapter;\r
+import org.simantics.spreadsheet.graph.GraphBackend;\r
+import org.simantics.spreadsheet.graph.SpreadsheetResource;\r
+import org.simantics.spreadsheet.ui.SimpleContainerTableModel;\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.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).SpreadsheetElement));\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).SpreadsheetElement));\r
+    }\r
+\r
+    @Override\r
+    public void load(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram,\r
+            Resource resource, final IElement element) {\r
+        \r
+        SpreadsheetResource sr = graph.getService(SpreadsheetResource.class);\r
+        \r
+        AffineTransform at = new AffineTransform();\r
+        ElementUtils.setTransform(element, at);\r
+        \r
+        graph.forPossibleObject(resource, sr.HasSheet, new AsyncProcedure<Resource>() {\r
+\r
+            @Override\r
+            public void exception(AsyncReadGraph graph, Throwable throwable) {\r
+                throwable.printStackTrace();\r
+            }\r
+\r
+            @Override\r
+            public void execute(AsyncReadGraph graph, Resource sheet) {\r
+\r
+                GraphBackend backend = new GraphBackend(graph.getSession());\r
+                \r
+                SimpleContainerTableModel tableModel = new SimpleContainerTableModel(backend);\r
+                \r
+                element.setHint(SheetClass.KEY_MODEL, tableModel);\r
+                element.setHint(SheetClass.KEY_TABLE_MODEL, tableModel);\r
+\r
+                backend.load(sheet, tableModel);\r
+                \r
+            }\r
+            \r
+        });\r
+        \r
+    }\r
+    \r
+\r
+}
\ No newline at end of file