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