1 package org.simantics.selectionview;
3 import java.io.IOException;
5 import org.simantics.common.format.Formatter;
6 import org.simantics.databoard.Bindings;
7 import org.simantics.databoard.Datatypes;
8 import org.simantics.databoard.adapter.AdaptException;
9 import org.simantics.databoard.binding.Binding;
10 import org.simantics.databoard.binding.NumberBinding;
11 import org.simantics.databoard.binding.StringBinding;
12 import org.simantics.databoard.binding.error.BindingConstructionException;
13 import org.simantics.databoard.binding.error.BindingException;
14 import org.simantics.databoard.binding.mutable.MutableStringBinding;
15 import org.simantics.databoard.parser.DataValuePrinter;
16 import org.simantics.databoard.parser.repository.DataTypeSyntaxError;
17 import org.simantics.databoard.parser.repository.DataValueRepository;
18 import org.simantics.databoard.primitives.MutableString;
19 import org.simantics.databoard.type.Datatype;
20 import org.simantics.databoard.util.ObjectUtils;
21 import org.simantics.db.ReadGraph;
22 import org.simantics.db.Resource;
23 import org.simantics.db.WriteGraph;
24 import org.simantics.db.common.CommentMetadata;
25 import org.simantics.db.common.adaption.SimpleContextualAdapter;
26 import org.simantics.db.exception.DatabaseException;
27 import org.simantics.db.layer0.util.Layer0Utils;
28 import org.simantics.db.layer0.variable.ModelledVariablePropertyDescriptor;
29 import org.simantics.db.layer0.variable.StandardGraphPropertyVariable;
30 import org.simantics.db.layer0.variable.Variable;
31 import org.simantics.db.layer0.variable.Variables;
32 import org.simantics.layer0.Layer0;
33 import org.simantics.modeling.ModelingResources;
34 import org.simantics.scl.runtime.function.Function1;
36 public class DisplayValueVariableAdapter extends SimpleContextualAdapter<Variable, ModelledVariablePropertyDescriptor> {
40 public DisplayValueVariableAdapter() {
41 key = SelectionViewResources.URIs.HasDisplayValue;
44 public DisplayValueVariableAdapter(String key) {
48 class Impl extends StandardGraphPropertyVariable {
50 private Datatype datatype;
52 public Impl(ReadGraph graph, Variable parent, Resource parentResource, Datatype datatype) throws DatabaseException {
53 super(graph, parent, null, parentResource, graph.getResource(key));
54 this.datatype = datatype;
57 @SuppressWarnings("unchecked")
59 public <T> T getValue(ReadGraph graph) throws DatabaseException {
60 return (T)standardGetDisplayValue1(graph, this);
63 @SuppressWarnings("unchecked")
65 public <T> T getValue(ReadGraph graph, Binding binding) throws DatabaseException {
66 return (T)standardGetDisplayValue2(graph, this, binding);
70 public Datatype getDatatype(ReadGraph graph) throws DatabaseException {
75 public void setValue(WriteGraph graph, Object value) throws DatabaseException {
76 standardSetDisplayValue2(graph, this, value);
81 public void setValue(WriteGraph graph, Object _value, Binding _binding) throws DatabaseException {
82 standardSetDisplayValue3(graph, this, _value, _binding);
87 public static boolean isPrimitive(Datatype dt) {
88 if(Datatypes.STRING.equals(dt)) return true;
92 private static String possibleExpression(ReadGraph graph, Variable variable) throws DatabaseException {
94 Layer0 L0 = Layer0.getInstance(graph);
95 ModelingResources MOD = ModelingResources.getInstance(graph);
96 Resource object = variable.getPossibleRepresents(graph);
97 if(object != null && graph.isInstanceOf(object, MOD.SCLValue)) {
98 String expression = graph.getPossibleRelatedValue(object, L0.SCLValue_expression);
99 if (expression != null)
100 return "=" + expression;
107 public Variable adapt(ReadGraph graph, Resource source, ModelledVariablePropertyDescriptor context) throws DatabaseException {
109 // String value = getDisplayValue(graph, context.getVariable());
110 return new Impl(graph, context.getVariable(), context.getSubject(), Datatypes.STRING);
114 public static Object standardGetDisplayValue1(ReadGraph graph, Variable property_) throws DatabaseException {
116 Variable property = property_.getParent(graph);
118 String expression = possibleExpression(graph, property);
119 if(expression != null) return expression;
123 Resource formatter = property.getPossiblePropertyValue(graph, Variables.FORMATTER);
124 if(formatter != null) {
125 Formatter fmt = graph.adaptContextual(formatter, property, Variable.class, Formatter.class);
126 value = fmt.format(property.getValue(graph));
129 SelectionViewResources SEL = SelectionViewResources.getInstance(graph);
130 Function1<Object,String> formatterFunction = property.getPossiblePropertyValue(graph, SEL.formatter);
131 if(formatterFunction != null) {
132 value = formatterFunction.apply(property.getValue(graph));
137 Datatype dt = property.getPossibleDatatype(graph);
139 if(!isPrimitive(dt)) {
140 Binding binding = Bindings.getBinding(dt);
142 value = DataValuePrinter.writeValueSingleLine(binding, property.getValue(graph));
143 } catch (IOException e) {
145 } catch (BindingException e) {
153 return value != null ? value.toString() : "null";
157 public static Object standardGetDisplayValue2(ReadGraph graph, Variable property, Binding binding) throws DatabaseException {
160 return Bindings.adapt(standardGetDisplayValue1(graph, property), Bindings.STRING, binding);
161 } catch (AdaptException e) {
162 throw new DatabaseException(e);
167 public static void standardSetDisplayValue2(WriteGraph graph, Variable property_, Object _value) throws DatabaseException {
170 Binding binding = Bindings.getBinding(_value.getClass());
171 standardSetDisplayValue3(graph, property_, _value, binding);
172 } catch (BindingConstructionException e) {
173 throw new DatabaseException(e);
178 public static void standardSetDisplayValue3(WriteGraph graph, Variable property_, Object _value, Binding binding_) throws DatabaseException {
182 Variable parent = property_.getParent(graph);
184 if(!(_value instanceof String)) throw new DatabaseException("setValue for HasDisplayValue only accepts String (got " + _value.getClass().getSimpleName() + ")");
186 String text = (String)_value;
187 if(text.startsWith("=")) {
188 Layer0Utils.setExpression(graph, parent, text, ModelingResources.getInstance(graph).SCLValue);
192 String parsedLabel = (String)_value;
193 Object value = parsedLabel;
195 Datatype type = parent.getPossibleDatatype(graph);
198 Binding binding = Bindings.getBinding(type);
200 if (binding instanceof StringBinding) {
202 if (binding instanceof MutableStringBinding)
203 value = new MutableString(parsedLabel);
209 if (binding instanceof NumberBinding) {
210 parsedLabel = parsedLabel.replace(",", ".");
213 value = binding.parseValue(parsedLabel, new DataValueRepository());
216 //System.out.println("VariableWrite " + ObjectUtils.toString(value));
217 parent.setValue(graph, value, binding);
221 parent.setValue(graph, value);
226 // Add a comment to metadata.
227 CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
228 graph.addMetadata(cm.add("Set value " + ObjectUtils.toString(value)));
230 } catch (DataTypeSyntaxError e) {
231 throw new DatabaseException(e);
232 } catch (BindingException e) {
233 throw new DatabaseException(e);
238 public static Datatype standardGetDisplayValueDatatype(ReadGraph graph, Variable property_) throws DatabaseException {
239 return Datatypes.STRING;