--- /dev/null
+package org.simantics.spreadsheet.graph.adapter;\r
+\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.binding.mutable.Variant;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.variable.StandardGraphChildVariable;\r
+import org.simantics.db.layer0.variable.Variable;\r
+import org.simantics.db.layer0.variable.VariableSpaceManipulator;\r
+import org.simantics.db.layer0.variable.VariableSpaceManipulator.ChildCreationData;\r
+import org.simantics.db.layer0.variable.VariableSpaceManipulator.Modification;\r
+import org.simantics.db.layer0.variable.VariableSpaceManipulator.PropertyCreationData;\r
+import org.simantics.spreadsheet.common.cell.VariableCellEditor;\r
+import org.simantics.spreadsheet.resource.SpreadsheetResource;\r
+\r
+public class TextCellVariable extends StandardGraphChildVariable {\r
+\r
+ public TextCellVariable(Variable parent, Resource resource) throws DatabaseException {\r
+ super(parent, null, resource);\r
+ }\r
+ \r
+ @Override\r
+ public String getLabel(ReadGraph graph) throws DatabaseException {\r
+ return null;\r
+ }\r
+ \r
+ @SuppressWarnings("unchecked")\r
+ protected <T> T tryAdapt(ReadGraph graph, Class<T> clazz) throws DatabaseException {\r
+ if(VariableCellEditor.class == clazz) {\r
+ return (T)new VariableCellEditor() {\r
+ \r
+ @Override\r
+ public void edit(WriteGraph graph, Variable cell, String text) throws DatabaseException {\r
+ \r
+ SpreadsheetResource sr = SpreadsheetResource.getInstance(graph);\r
+ Resource configuration = cell.getPossibleRepresents(graph);\r
+ graph.claimLiteral(configuration, sr.Cell_content, Variant.ofInstance(text), Bindings.VARIANT);\r
+ \r
+ }\r
+ \r
+ @Override\r
+ public void copy(WriteGraph graph, Variable cell, String location) throws DatabaseException {\r
+ \r
+ SpreadsheetResource sr = SpreadsheetResource.getInstance(graph);\r
+ Resource configuration = cell.getPossibleRepresents(graph);\r
+ Variant content = graph.getPossibleRelatedValue(configuration, sr.Cell_content, Bindings.VARIANT);\r
+ PropertyCreationData[] pData = new PropertyCreationData[] { PropertyCreationData.build(SpreadsheetResource.URIs.Cell_content, content) };\r
+ ChildCreationData data = ChildCreationData.build(location, SpreadsheetResource.URIs.TextCell, pData);\r
+ Variable sheet = cell.getParent(graph);\r
+ VariableSpaceManipulator manipulator = sheet.adapt(graph, VariableSpaceManipulator.class);\r
+ manipulator.apply(graph, Modification.addChild(data));\r
+ \r
+ }\r
+ \r
+ };\r
+ }\r
+ return null;\r
+ }\r
+\r
+ @Override\r
+ public <T> T adapt(ReadGraph graph, Class<T> clazz) throws DatabaseException {\r
+ T t = tryAdapt(graph, clazz);\r
+ return t != null ? t : super.adapt(graph, clazz);\r
+ }\r
+\r
+ @Override\r
+ public <T> T adaptPossible(ReadGraph graph, Class<T> clazz) throws DatabaseException {\r
+ T t = tryAdapt(graph, clazz);\r
+ return t != null ? t : super.adaptPossible(graph, clazz);\r
+ }\r
+\r
+}\r