--- /dev/null
+package org.simantics.selectionview;\r
+\r
+import java.io.IOException;\r
+\r
+import org.simantics.common.format.Formatter;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.Datatypes;\r
+import org.simantics.databoard.adapter.AdaptException;\r
+import org.simantics.databoard.binding.Binding;\r
+import org.simantics.databoard.binding.NumberBinding;\r
+import org.simantics.databoard.binding.StringBinding;\r
+import org.simantics.databoard.binding.error.BindingConstructionException;\r
+import org.simantics.databoard.binding.error.BindingException;\r
+import org.simantics.databoard.binding.mutable.MutableStringBinding;\r
+import org.simantics.databoard.parser.DataValuePrinter;\r
+import org.simantics.databoard.parser.repository.DataTypeSyntaxError;\r
+import org.simantics.databoard.parser.repository.DataValueRepository;\r
+import org.simantics.databoard.primitives.MutableString;\r
+import org.simantics.databoard.type.Datatype;\r
+import org.simantics.databoard.util.ObjectUtils;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.CommentMetadata;\r
+import org.simantics.db.common.adaption.SimpleContextualAdapter;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.util.Layer0Utils;\r
+import org.simantics.db.layer0.variable.ModelledVariablePropertyDescriptor;\r
+import org.simantics.db.layer0.variable.StandardGraphPropertyVariable;\r
+import org.simantics.db.layer0.variable.Variable;\r
+import org.simantics.db.layer0.variable.Variables;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.modeling.ModelingResources;\r
+\r
+public class DisplayValueVariableAdapter extends SimpleContextualAdapter<Variable, ModelledVariablePropertyDescriptor> {\r
+ \r
+ final String key;\r
+ \r
+ public DisplayValueVariableAdapter() {\r
+ key = SelectionViewResources.URIs.HasDisplayValue;\r
+ }\r
+\r
+ public DisplayValueVariableAdapter(String key) {\r
+ this.key = key;\r
+ }\r
+\r
+ class Impl extends StandardGraphPropertyVariable {\r
+\r
+ private Datatype datatype;\r
+\r
+ public Impl(ReadGraph graph, Variable parent, Resource parentResource, Datatype datatype) throws DatabaseException {\r
+ super(graph, parent, null, parentResource, graph.getResource(key));\r
+ this.datatype = datatype;\r
+ }\r
+ \r
+ @SuppressWarnings("unchecked")\r
+ @Override\r
+ public <T> T getValue(ReadGraph graph) throws DatabaseException {\r
+ return (T)standardGetDisplayValue1(graph, this);\r
+ }\r
+ \r
+ @SuppressWarnings("unchecked")\r
+ @Override\r
+ public <T> T getValue(ReadGraph graph, Binding binding) throws DatabaseException {\r
+ return (T)standardGetDisplayValue2(graph, this, binding);\r
+ }\r
+ \r
+ @Override\r
+ public Datatype getDatatype(ReadGraph graph) throws DatabaseException {\r
+ return datatype;\r
+ }\r
+ \r
+ @Override\r
+ public void setValue(WriteGraph graph, Object value) throws DatabaseException {\r
+ standardSetDisplayValue2(graph, this, value);\r
+ }\r
+ \r
+\r
+ @Override\r
+ public void setValue(WriteGraph graph, Object _value, Binding _binding) throws DatabaseException {\r
+ standardSetDisplayValue3(graph, this, _value, _binding);\r
+ }\r
+ \r
+ }\r
+ \r
+ public static boolean isPrimitive(Datatype dt) {\r
+ if(Datatypes.STRING.equals(dt)) return true;\r
+ else return false;\r
+ }\r
+\r
+ private static String possibleExpression(ReadGraph graph, Variable variable) throws DatabaseException {\r
+ \r
+ Layer0 L0 = Layer0.getInstance(graph);\r
+ ModelingResources MOD = ModelingResources.getInstance(graph);\r
+ Resource object = variable.getPossibleRepresents(graph);\r
+ if(object != null && graph.isInstanceOf(object, MOD.SCLValue)) {\r
+ String expression = graph.getPossibleRelatedValue(object, L0.SCLValue_expression);\r
+ if (expression != null)\r
+ return "=" + expression;\r
+ }\r
+ return null;\r
+ \r
+ }\r
+\r
+ @Override\r
+ public Variable adapt(ReadGraph graph, Resource source, ModelledVariablePropertyDescriptor context) throws DatabaseException {\r
+ \r
+// String value = getDisplayValue(graph, context.getVariable());\r
+ return new Impl(graph, context.getVariable(), context.getSubject(), Datatypes.STRING);\r
+ \r
+ }\r
+\r
+ public static Object standardGetDisplayValue1(ReadGraph graph, Variable property_) throws DatabaseException {\r
+\r
+ Variable property = property_.getParent(graph); \r
+ \r
+ String expression = possibleExpression(graph, property);\r
+ if(expression != null) return expression;\r
+ \r
+ Object value = property.getValue(graph);\r
+\r
+ Resource formatter = property.getPossiblePropertyValue(graph, Variables.FORMATTER);\r
+ if(formatter != null) {\r
+ Formatter fmt = graph.adaptContextual(formatter, property, Variable.class, Formatter.class);\r
+ value = fmt.format(value);\r
+ } else {\r
+\r
+ Datatype dt = property.getPossibleDatatype(graph);\r
+ if(dt != null) {\r
+ if(!isPrimitive(dt)) {\r
+ Binding binding = Bindings.getBinding(dt);\r
+ try {\r
+ value = DataValuePrinter.writeValueSingleLine(binding, value);\r
+ } catch (IOException e) {\r
+ e.printStackTrace();\r
+ } catch (BindingException e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+ }\r
+\r
+ }\r
+\r
+ return value != null ? value.toString() : "null";\r
+ \r
+ }\r
+\r
+ public static Object standardGetDisplayValue2(ReadGraph graph, Variable property, Binding binding) throws DatabaseException {\r
+ \r
+ try {\r
+ return Bindings.adapt(standardGetDisplayValue1(graph, property), Bindings.STRING, binding);\r
+ } catch (AdaptException e) {\r
+ throw new DatabaseException(e);\r
+ }\r
+ \r
+ }\r
+\r
+ public static void standardSetDisplayValue2(WriteGraph graph, Variable property_, Object _value) throws DatabaseException {\r
+ \r
+ try {\r
+ Binding binding = Bindings.getBinding(_value.getClass());\r
+ standardSetDisplayValue3(graph, property_, _value, binding);\r
+ } catch (BindingConstructionException e) {\r
+ throw new DatabaseException(e);\r
+ }\r
+\r
+ }\r
+\r
+ public static void standardSetDisplayValue3(WriteGraph graph, Variable property_, Object _value, Binding binding_) throws DatabaseException {\r
+\r
+ try {\r
+ \r
+ Variable parent = property_.getParent(graph);\r
+ \r
+ if(!(_value instanceof String)) throw new DatabaseException("setValue for HasDisplayValue only accepts String (got " + _value.getClass().getSimpleName() + ")");\r
+\r
+ String text = (String)_value;\r
+ if(text.startsWith("=")) {\r
+ Layer0Utils.setExpression(graph, parent, text, ModelingResources.getInstance(graph).SCLValue);\r
+ return;\r
+ }\r
+\r
+ String parsedLabel = (String)_value;\r
+ Object value = parsedLabel;\r
+ \r
+ Datatype type = parent.getPossibleDatatype(graph);\r
+ if (type != null) {\r
+\r
+ Binding binding = Bindings.getBinding(type);\r
+\r
+ if (binding instanceof StringBinding) {\r
+ \r
+ if (binding instanceof MutableStringBinding)\r
+ value = new MutableString(parsedLabel);\r
+ else\r
+ value = parsedLabel;\r
+ \r
+ } else {\r
+ \r
+ if (binding instanceof NumberBinding) {\r
+ parsedLabel = parsedLabel.replace(",", ".");\r
+ }\r
+\r
+ value = binding.parseValue(parsedLabel, new DataValueRepository());\r
+ }\r
+\r
+ //System.out.println("VariableWrite " + ObjectUtils.toString(value));\r
+ parent.setValue(graph, value, binding);\r
+ \r
+ } else {\r
+\r
+ parent.setValue(graph, value);\r
+ \r
+ }\r
+\r
+\r
+ // Add a comment to metadata.\r
+ CommentMetadata cm = graph.getMetadata(CommentMetadata.class);\r
+ graph.addMetadata(cm.add("Set value " + ObjectUtils.toString(value)));\r
+ \r
+ } catch (DataTypeSyntaxError e) {\r
+ throw new DatabaseException(e);\r
+ } catch (BindingException e) {\r
+ throw new DatabaseException(e);\r
+ }\r
+ \r
+ }\r
+\r
+ public static Datatype standardGetDisplayValueDatatype(ReadGraph graph, Variable property_) throws DatabaseException {\r
+ return Datatypes.STRING;\r
+ }\r
+ \r
+}\r