]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/adapter/TextCellVariable.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.spreadsheet.graph / src / org / simantics / spreadsheet / graph / adapter / TextCellVariable.java
1 package org.simantics.spreadsheet.graph.adapter;
2
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;
17
18 public class TextCellVariable extends StandardGraphChildVariable {
19
20         public TextCellVariable(Variable parent, Resource resource) throws DatabaseException {
21                 super(parent, null, resource);
22         }
23         
24         @Override
25         public String getLabel(ReadGraph graph) throws DatabaseException {
26                 return null;
27         }
28         
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() {
33                                 
34                                 @Override
35                                 public void edit(WriteGraph graph, Variable cell, String text) throws DatabaseException {
36                                         
37                                         SpreadsheetResource sr = SpreadsheetResource.getInstance(graph);
38                                         Resource configuration = cell.getPossibleRepresents(graph);
39                                         graph.claimLiteral(configuration, sr.Cell_content, Variant.ofInstance(text), Bindings.VARIANT);
40                                         
41                                 }
42                                 
43                                 @Override
44                                 public void copy(WriteGraph graph, Variable cell, String location) throws DatabaseException {
45                                         
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));
54                                         
55                                 }
56                                 
57                         };
58         }
59         return null;
60     }
61
62     @Override
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);
66     }
67
68     @Override
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);
72     }
73
74 }