]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/adapter/TextCellVariable.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.spreadsheet.graph / src / org / simantics / spreadsheet / graph / adapter / TextCellVariable.java
1 package org.simantics.spreadsheet.graph.adapter;\r
2 \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
17 \r
18 public class TextCellVariable extends StandardGraphChildVariable {\r
19 \r
20         public TextCellVariable(Variable parent, Resource resource) throws DatabaseException {\r
21                 super(parent, null, resource);\r
22         }\r
23         \r
24         @Override\r
25         public String getLabel(ReadGraph graph) throws DatabaseException {\r
26                 return null;\r
27         }\r
28         \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
33                                 \r
34                                 @Override\r
35                                 public void edit(WriteGraph graph, Variable cell, String text) throws DatabaseException {\r
36                                         \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
40                                         \r
41                                 }\r
42                                 \r
43                                 @Override\r
44                                 public void copy(WriteGraph graph, Variable cell, String location) throws DatabaseException {\r
45                                         \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
54                                         \r
55                                 }\r
56                                 \r
57                         };\r
58         }\r
59         return null;\r
60     }\r
61 \r
62     @Override\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
66     }\r
67 \r
68     @Override\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
72     }\r
73 \r
74 }\r