]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.selectionview/src/org/simantics/selectionview/DisplayValueVariableAdapter.java
2eaba75d60db700d72c2d1a337363df7c265ef43
[simantics/platform.git] / bundles / org.simantics.selectionview / src / org / simantics / selectionview / DisplayValueVariableAdapter.java
1 package org.simantics.selectionview;
2
3 import java.io.IOException;
4
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
35 public class DisplayValueVariableAdapter extends SimpleContextualAdapter<Variable, ModelledVariablePropertyDescriptor> {
36         
37         final String key;
38         
39         public DisplayValueVariableAdapter() {
40                 key = SelectionViewResources.URIs.HasDisplayValue;
41         }
42
43         public DisplayValueVariableAdapter(String key) {
44                 this.key = key;
45         }
46
47         class Impl extends StandardGraphPropertyVariable {
48
49                 private Datatype datatype;
50
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;
54                 }
55                 
56                 @SuppressWarnings("unchecked")
57                 @Override
58                 public <T> T getValue(ReadGraph graph) throws DatabaseException {
59                         return (T)standardGetDisplayValue1(graph, this);
60                 }
61                 
62                 @SuppressWarnings("unchecked")
63                 @Override
64                 public <T> T getValue(ReadGraph graph, Binding binding) throws DatabaseException {
65                         return (T)standardGetDisplayValue2(graph, this, binding);
66                 }
67                 
68                 @Override
69                 public Datatype getDatatype(ReadGraph graph) throws DatabaseException {
70                         return datatype;
71                 }
72                         
73                 @Override
74                 public void setValue(WriteGraph graph, Object value) throws DatabaseException {
75                         standardSetDisplayValue2(graph, this, value);
76                 }
77                 
78
79                 @Override
80                 public void setValue(WriteGraph graph, Object _value, Binding _binding) throws DatabaseException {
81                         standardSetDisplayValue3(graph, this, _value, _binding);
82                 }
83                 
84         }
85         
86         public static boolean isPrimitive(Datatype dt) {
87                 if(Datatypes.STRING.equals(dt)) return true;
88                 else return false;
89         }
90
91         private static String possibleExpression(ReadGraph graph, Variable variable) throws DatabaseException {
92                 
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;
100                 }
101                 return null;
102                 
103         }
104
105         @Override
106         public Variable adapt(ReadGraph graph, Resource source, ModelledVariablePropertyDescriptor context) throws DatabaseException {
107                 
108 //      String value = getDisplayValue(graph, context.getVariable());
109         return new Impl(graph, context.getVariable(), context.getSubject(), Datatypes.STRING);
110                 
111         }
112
113     public static Object standardGetDisplayValue1(ReadGraph graph, Variable property_) throws DatabaseException {
114
115         Variable property = property_.getParent(graph); 
116         
117         String expression = possibleExpression(graph, property);
118         if(expression != null) return expression;
119         
120         Object value = property.getValue(graph);
121
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);
126         } else {
127
128                 Datatype dt = property.getPossibleDatatype(graph);
129                 if(dt != null) {
130                         if(!isPrimitive(dt)) {
131                                 Binding binding = Bindings.getBinding(dt);
132                                 try {
133                                         value = DataValuePrinter.writeValueSingleLine(binding, value);
134                                 } catch (IOException e) {
135                                         e.printStackTrace();
136                                 } catch (BindingException e) {
137                                         e.printStackTrace();
138                                 }
139                         }
140                 }
141
142         }
143
144         return value != null ? value.toString() : "null";
145         
146     }
147
148     public static Object standardGetDisplayValue2(ReadGraph graph, Variable property, Binding binding) throws DatabaseException {
149         
150                 try {
151                         return Bindings.adapt(standardGetDisplayValue1(graph, property), Bindings.STRING, binding);
152                 } catch (AdaptException e) {
153                         throw new DatabaseException(e);
154                 }
155         
156     }
157
158     public static void standardSetDisplayValue2(WriteGraph graph, Variable property_, Object _value) throws DatabaseException {
159                         
160                 try {
161                         Binding binding = Bindings.getBinding(_value.getClass());
162                         standardSetDisplayValue3(graph, property_, _value, binding);
163                 } catch (BindingConstructionException e) {
164                         throw new DatabaseException(e);
165                 }
166
167     }
168
169     public static void standardSetDisplayValue3(WriteGraph graph, Variable property_, Object _value, Binding binding_) throws DatabaseException {
170
171         try {
172                 
173                 Variable parent = property_.getParent(graph);
174                 
175                         if(!(_value instanceof String)) throw new DatabaseException("setValue for HasDisplayValue only accepts String (got " + _value.getClass().getSimpleName() + ")");
176
177                         String text = (String)_value;
178                         if(text.startsWith("=")) {
179                             Layer0Utils.setExpression(graph, parent, text, ModelingResources.getInstance(graph).SCLValue);
180                                 return;
181                         }
182
183                 String parsedLabel = (String)_value;
184             Object value = parsedLabel;
185                 
186             Datatype type = parent.getPossibleDatatype(graph);
187             if (type != null) {
188
189                     Binding binding = Bindings.getBinding(type);
190
191                     if (binding instanceof StringBinding) {
192                         
193                         if (binding instanceof MutableStringBinding)
194                             value = new MutableString(parsedLabel);
195                         else
196                             value = parsedLabel;
197                         
198                     } else {
199                         
200                         if (binding instanceof NumberBinding) {
201                             parsedLabel = parsedLabel.replace(",", ".");
202                         }
203
204                         value = binding.parseValue(parsedLabel, new DataValueRepository());
205                     }
206
207                     //System.out.println("VariableWrite " + ObjectUtils.toString(value));
208                     parent.setValue(graph, value, binding);
209                     
210             } else {
211
212                 parent.setValue(graph, value);
213                 
214             }
215
216
217             // Add a comment to metadata.
218             CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
219             graph.addMetadata(cm.add("Set value " + ObjectUtils.toString(value)));
220             
221         } catch (DataTypeSyntaxError e) {
222             throw new DatabaseException(e);
223         } catch (BindingException e) {
224             throw new DatabaseException(e);
225         }
226         
227     }
228
229     public static Datatype standardGetDisplayValueDatatype(ReadGraph graph, Variable property_) throws DatabaseException {
230         return Datatypes.STRING;
231     }
232         
233 }