1 package org.simantics.spreadsheet.graph.adapter;
3 import org.simantics.databoard.Bindings;
4 import org.simantics.databoard.binding.mutable.Variant;
5 import org.simantics.db.ReadGraph;
6 import org.simantics.db.Resource;
7 import org.simantics.db.WriteGraph;
8 import org.simantics.db.exception.DatabaseException;
9 import org.simantics.db.layer0.variable.StandardGraphChildVariable;
10 import org.simantics.db.layer0.variable.Variable;
11 import org.simantics.db.layer0.variable.VariableSpaceManipulator;
12 import org.simantics.db.layer0.variable.VariableSpaceManipulator.ChildCreationData;
13 import org.simantics.db.layer0.variable.VariableSpaceManipulator.Modification;
14 import org.simantics.db.layer0.variable.VariableSpaceManipulator.PropertyCreationData;
15 import org.simantics.spreadsheet.common.cell.VariableCellEditor;
16 import org.simantics.spreadsheet.resource.SpreadsheetResource;
18 public class TextCellVariable extends StandardGraphChildVariable {
20 public TextCellVariable(Variable parent, Resource resource) throws DatabaseException {
21 super(parent, null, resource);
25 public String getLabel(ReadGraph graph) throws DatabaseException {
29 @SuppressWarnings("unchecked")
30 protected <T> T tryAdapt(ReadGraph graph, Class<T> clazz) throws DatabaseException {
31 if(VariableCellEditor.class == clazz) {
32 return (T)new VariableCellEditor() {
35 public void edit(WriteGraph graph, Variable cell, String text) throws DatabaseException {
37 SpreadsheetResource sr = SpreadsheetResource.getInstance(graph);
38 Resource configuration = cell.getPossibleRepresents(graph);
39 graph.claimLiteral(configuration, sr.Cell_content, Variant.ofInstance(text), Bindings.VARIANT);
44 public void copy(WriteGraph graph, Variable cell, String location) throws DatabaseException {
46 SpreadsheetResource sr = SpreadsheetResource.getInstance(graph);
47 Resource configuration = cell.getPossibleRepresents(graph);
48 Variant content = graph.getPossibleRelatedValue(configuration, sr.Cell_content, Bindings.VARIANT);
49 PropertyCreationData[] pData = new PropertyCreationData[] { PropertyCreationData.build(SpreadsheetResource.URIs.Cell_content, content) };
50 ChildCreationData data = ChildCreationData.build(location, SpreadsheetResource.URIs.TextCell, pData);
51 Variable sheet = cell.getParent(graph);
52 VariableSpaceManipulator manipulator = sheet.adapt(graph, VariableSpaceManipulator.class);
53 manipulator.apply(graph, Modification.addChild(data));
63 public <T> T adapt(ReadGraph graph, Class<T> clazz) throws DatabaseException {
64 T t = tryAdapt(graph, clazz);
65 return t != null ? t : super.adapt(graph, clazz);
69 public <T> T adaptPossible(ReadGraph graph, Class<T> clazz) throws DatabaseException {
70 T t = tryAdapt(graph, clazz);
71 return t != null ? t : super.adaptPossible(graph, clazz);