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;
35 public class DisplayValueVariableAdapter extends SimpleContextualAdapter<Variable, ModelledVariablePropertyDescriptor> {
39 public DisplayValueVariableAdapter() {
40 key = SelectionViewResources.URIs.HasDisplayValue;
43 public DisplayValueVariableAdapter(String key) {
47 class Impl extends StandardGraphPropertyVariable {
49 private Datatype datatype;
51 public Impl(ReadGraph graph, Variable parent, Resource parentResource, Datatype datatype) throws DatabaseException {
52 super(graph, parent, null, parentResource, graph.getResource(key));
53 this.datatype = datatype;
56 @SuppressWarnings("unchecked")
58 public <T> T getValue(ReadGraph graph) throws DatabaseException {
59 return (T)standardGetDisplayValue1(graph, this);
62 @SuppressWarnings("unchecked")
64 public <T> T getValue(ReadGraph graph, Binding binding) throws DatabaseException {
65 return (T)standardGetDisplayValue2(graph, this, binding);
69 public Datatype getDatatype(ReadGraph graph) throws DatabaseException {
74 public void setValue(WriteGraph graph, Object value) throws DatabaseException {
75 standardSetDisplayValue2(graph, this, value);
80 public void setValue(WriteGraph graph, Object _value, Binding _binding) throws DatabaseException {
81 standardSetDisplayValue3(graph, this, _value, _binding);
86 public static boolean isPrimitive(Datatype dt) {
87 if(Datatypes.STRING.equals(dt)) return true;
91 private static String possibleExpression(ReadGraph graph, Variable variable) throws DatabaseException {
93 Layer0 L0 = Layer0.getInstance(graph);
94 ModelingResources MOD = ModelingResources.getInstance(graph);
95 Resource object = variable.getPossibleRepresents(graph);
96 if(object != null && graph.isInstanceOf(object, MOD.SCLValue)) {
97 String expression = graph.getPossibleRelatedValue(object, L0.SCLValue_expression);
98 if (expression != null)
99 return "=" + expression;
106 public Variable adapt(ReadGraph graph, Resource source, ModelledVariablePropertyDescriptor context) throws DatabaseException {
108 // String value = getDisplayValue(graph, context.getVariable());
109 return new Impl(graph, context.getVariable(), context.getSubject(), Datatypes.STRING);
113 public static Object standardGetDisplayValue1(ReadGraph graph, Variable property_) throws DatabaseException {
115 Variable property = property_.getParent(graph);
117 String expression = possibleExpression(graph, property);
118 if(expression != null) return expression;
120 Object value = property.getValue(graph);
122 Resource formatter = property.getPossiblePropertyValue(graph, Variables.FORMATTER);
123 if(formatter != null) {
124 Formatter fmt = graph.adaptContextual(formatter, property, Variable.class, Formatter.class);
125 value = fmt.format(value);
128 Datatype dt = property.getPossibleDatatype(graph);
130 if(!isPrimitive(dt)) {
131 Binding binding = Bindings.getBinding(dt);
133 value = DataValuePrinter.writeValueSingleLine(binding, value);
134 } catch (IOException e) {
136 } catch (BindingException e) {
144 return value != null ? value.toString() : "null";
148 public static Object standardGetDisplayValue2(ReadGraph graph, Variable property, Binding binding) throws DatabaseException {
151 return Bindings.adapt(standardGetDisplayValue1(graph, property), Bindings.STRING, binding);
152 } catch (AdaptException e) {
153 throw new DatabaseException(e);
158 public static void standardSetDisplayValue2(WriteGraph graph, Variable property_, Object _value) throws DatabaseException {
161 Binding binding = Bindings.getBinding(_value.getClass());
162 standardSetDisplayValue3(graph, property_, _value, binding);
163 } catch (BindingConstructionException e) {
164 throw new DatabaseException(e);
169 public static void standardSetDisplayValue3(WriteGraph graph, Variable property_, Object _value, Binding binding_) throws DatabaseException {
173 Variable parent = property_.getParent(graph);
175 if(!(_value instanceof String)) throw new DatabaseException("setValue for HasDisplayValue only accepts String (got " + _value.getClass().getSimpleName() + ")");
177 String text = (String)_value;
178 if(text.startsWith("=")) {
179 Layer0Utils.setExpression(graph, parent, text, ModelingResources.getInstance(graph).SCLValue);
183 String parsedLabel = (String)_value;
184 Object value = parsedLabel;
186 Datatype type = parent.getPossibleDatatype(graph);
189 Binding binding = Bindings.getBinding(type);
191 if (binding instanceof StringBinding) {
193 if (binding instanceof MutableStringBinding)
194 value = new MutableString(parsedLabel);
200 if (binding instanceof NumberBinding) {
201 parsedLabel = parsedLabel.replace(",", ".");
204 value = binding.parseValue(parsedLabel, new DataValueRepository());
207 //System.out.println("VariableWrite " + ObjectUtils.toString(value));
208 parent.setValue(graph, value, binding);
212 parent.setValue(graph, value);
217 // Add a comment to metadata.
218 CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
219 graph.addMetadata(cm.add("Set value " + ObjectUtils.toString(value)));
221 } catch (DataTypeSyntaxError e) {
222 throw new DatabaseException(e);
223 } catch (BindingException e) {
224 throw new DatabaseException(e);
229 public static Datatype standardGetDisplayValueDatatype(ReadGraph graph, Variable property_) throws DatabaseException {
230 return Datatypes.STRING;