]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/creator/CellCreatorBase.java.keep
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.spreadsheet.graph / src / org / simantics / spreadsheet / graph / creator / CellCreatorBase.java.keep
diff --git a/bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/creator/CellCreatorBase.java.keep b/bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/creator/CellCreatorBase.java.keep
new file mode 100644 (file)
index 0000000..03874bd
--- /dev/null
@@ -0,0 +1,77 @@
+/*******************************************************************************\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.spreadsheet.graph.creator;\r
+\r
+import java.util.Collection;\r
+import java.util.Collections;\r
+\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.adapter.AdaptException;\r
+import org.simantics.databoard.binding.Binding;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.spreadsheet.ModelCellManager.CellCreationData;\r
+import org.simantics.spreadsheet.common.cell.Parsers;\r
+import org.simantics.spreadsheet.graph.request.Columns;\r
+import org.simantics.spreadsheet.graph.request.Rows;\r
+import org.simantics.spreadsheet.graph.request.RowsColumnsIndex;\r
+\r
+abstract public class CellCreatorBase implements GraphCellCreator {\r
+\r
+    final protected Resource realization;\r
+\r
+    public CellCreatorBase(Resource realization) {\r
+        this.realization = realization;\r
+    }\r
+\r
+    protected <T> T getProperty(CellCreationData datum, String property, Binding binding) throws DatabaseException {\r
+    \r
+       try {\r
+                       return (T)datum.properties.get(Parsers.EXPRESSION_PROPERTY).getValue(Bindings.STRING);\r
+               } catch (AdaptException e) {\r
+                       throw new DatabaseException(e);\r
+               }\r
+       \r
+    }\r
+    \r
+    public void create(WriteGraph graph, CellCreationData cell) throws DatabaseException {\r
+\r
+        create(graph, Collections.singleton(cell));\r
+\r
+    }\r
+\r
+    public void create(WriteGraph graph, Collection<CellCreationData> data) throws DatabaseException {\r
+        Layer0 l0 = Layer0.getInstance(graph);\r
+\r
+        int maxRow = 0;\r
+        int maxColumn = 0;\r
+        for(CellCreationData datum : data) {\r
+            maxRow = Math.max(maxRow, datum.row);\r
+            maxColumn = Math.max(maxColumn, datum.column);\r
+        }\r
+\r
+        Resource spreadsheet = graph.getPossibleObject(realization, l0.Represents);\r
+\r
+        RowsColumnsIndex rowIndex = graph.syncRequest(new Rows(spreadsheet));\r
+        RowsColumnsIndex columnIndex = graph.syncRequest(new Columns(spreadsheet));\r
+        rowIndex = rowIndex.copy();\r
+        columnIndex = columnIndex.copy();\r
+        rowIndex.ensureIndex(graph, maxRow);\r
+        columnIndex.ensureIndex(graph, maxColumn);\r
+\r
+        create(graph, rowIndex, columnIndex, data);\r
+\r
+    }\r
+\r
+}\r