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