1 package org.simantics.selectionview.function;
3 import java.io.IOException;
4 import java.util.ArrayList;
6 import java.util.function.Consumer;
8 import org.eclipse.swt.graphics.FontData;
9 import org.eclipse.swt.graphics.RGB;
10 import org.eclipse.swt.widgets.ColorDialog;
11 import org.eclipse.swt.widgets.Control;
12 import org.eclipse.swt.widgets.FontDialog;
13 import org.simantics.Simantics;
14 import org.simantics.browsing.ui.NodeContext;
15 import org.simantics.browsing.ui.content.Labeler.DialogModifier;
16 import org.simantics.common.format.Formatter;
17 import org.simantics.databoard.Bindings;
18 import org.simantics.databoard.Datatypes;
19 import org.simantics.databoard.adapter.AdaptException;
20 import org.simantics.databoard.binding.Binding;
21 import org.simantics.databoard.binding.NumberBinding;
22 import org.simantics.databoard.binding.StringBinding;
23 import org.simantics.databoard.binding.error.BindingConstructionException;
24 import org.simantics.databoard.binding.error.BindingException;
25 import org.simantics.databoard.binding.mutable.MutableStringBinding;
26 import org.simantics.databoard.binding.mutable.Variant;
27 import org.simantics.databoard.parser.DataValuePrinter;
28 import org.simantics.databoard.parser.repository.DataTypeSyntaxError;
29 import org.simantics.databoard.parser.repository.DataValueRepository;
30 import org.simantics.databoard.primitives.MutableString;
31 import org.simantics.databoard.type.Datatype;
32 import org.simantics.databoard.util.ObjectUtils;
33 import org.simantics.datatypes.literal.Font;
34 import org.simantics.db.ReadGraph;
35 import org.simantics.db.Resource;
36 import org.simantics.db.Statement;
37 import org.simantics.db.WriteGraph;
38 import org.simantics.db.common.CommentMetadata;
39 import org.simantics.db.common.request.EnumerationMap;
40 import org.simantics.db.common.request.InstanceEnumerationMap;
41 import org.simantics.db.common.request.IsEnumeratedValue;
42 import org.simantics.db.common.request.UniqueRead;
43 import org.simantics.db.common.request.WriteRequest;
44 import org.simantics.db.common.utils.NameUtils;
45 import org.simantics.db.exception.DatabaseException;
46 import org.simantics.db.layer0.util.Layer0Utils;
47 import org.simantics.db.layer0.variable.ValueAccessor;
48 import org.simantics.db.layer0.variable.Variable;
49 import org.simantics.db.layer0.variable.Variables;
50 import org.simantics.layer0.Layer0;
51 import org.simantics.modeling.ModelingResources;
52 import org.simantics.scl.compiler.types.TVar;
53 import org.simantics.scl.compiler.types.Type;
54 import org.simantics.scl.compiler.types.Types;
55 import org.simantics.scl.reflection.annotations.SCLValue;
56 import org.simantics.selectionview.SelectionInput;
57 import org.simantics.selectionview.StandardSelectionInput;
58 import org.simantics.ui.colors.Colors;
59 import org.simantics.ui.fonts.Fonts;
60 import org.simantics.ui.selection.WorkbenchSelectionElement;
61 import org.simantics.ui.selection.WorkbenchSelectionUtils;
62 import org.simantics.utils.datastructures.collections.CollectionUtils;
63 import org.simantics.utils.ui.AdaptionUtils;
64 import org.simantics.utils.ui.ErrorLogger;
65 import org.simantics.utils.ui.ISelectionUtils;
69 final private static Binding datatype_binging = Bindings.getBindingUnchecked(Datatype.class);
71 @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
72 public static Object colorModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {
73 return new DialogModifier() {
76 public String getValue() {
81 public String isValid(String label) {
86 public void modify(final String label) {
87 Simantics.getSession().async(new WriteRequest() {
90 public void perform(WriteGraph graph) throws DatabaseException {
91 Variable displayValue = context.getParent(graph);
92 displayValue.setValue(graph, label, org.simantics.datatypes.literal.RGB.Integer.BINDING);
98 public String query(Object parentControl, Object controlItem, int columnIndex, NodeContext context, Consumer<String> applyCallback) {
99 Control ctrl = (Control) parentControl;
101 RGB initialValue = null;
102 final Variable v = AdaptionUtils.adaptToSingle(context, Variable.class);
105 org.simantics.datatypes.literal.RGB.Integer rgb = Simantics.getSession().syncRequest(new UniqueRead<org.simantics.datatypes.literal.RGB.Integer>() {
107 public org.simantics.datatypes.literal.RGB.Integer perform(ReadGraph graph) throws DatabaseException {
108 return v.getPossibleValue(graph, org.simantics.datatypes.literal.RGB.Integer.BINDING);
112 initialValue = Colors.rgb(rgb);
114 } catch (DatabaseException e) {
115 ErrorLogger.defaultLogError(e);
119 ColorDialog dialog = new ColorDialog(ctrl.getShell());
120 if (initialValue != null)
121 dialog.setRGB(initialValue);
122 RGB rgb = dialog.open();
124 applyCallback.accept("(" + rgb.red + "," + rgb.green + "," + rgb.blue + ")");
131 @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
132 public static Object fontModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {
133 return new DialogModifier() {
136 public String getValue() {
141 public String isValid(String label) {
146 public void modify(final String label) {
147 Simantics.getSession().async(new WriteRequest() {
150 public void perform(WriteGraph graph) throws DatabaseException {
151 Variable displayValue = context.getParent(graph);
152 displayValue.setValue(graph, label, Font.BINDING);
158 public String query(Object parentControl, Object controlItem, int columnIndex, NodeContext context, Consumer<String> applyCallback) {
159 Control ctrl = (Control) parentControl;
161 FontData[] initialValue = null;
162 final Variable v = AdaptionUtils.adaptToSingle(context, Variable.class);
165 Font font = Simantics.getSession().syncRequest(new UniqueRead<Font>() {
167 public Font perform(ReadGraph graph) throws DatabaseException {
168 return v.getPossibleValue(graph, Font.BINDING);
172 initialValue = new FontData[] { Fonts.swtFontData(font) };
174 } catch (DatabaseException e) {
175 ErrorLogger.defaultLogError(e);
179 FontDialog dialog = new FontDialog(ctrl.getShell());
180 if (initialValue != null)
181 dialog.setFontList(initialValue);
182 FontData font = dialog.open();
184 applyCallback.accept("(\"" + font.getName() + "\"," + font.getHeight() + ",\"" + Fonts.fromSwtStyle(font.getStyle()) + "\")");
191 @SCLValue(type = "ReadGraph -> Resource -> a -> b")
192 public static Object getEnumerationValues(ReadGraph graph, Resource resource, Object context) throws DatabaseException {
193 if(context instanceof Variable) {
194 Layer0 L0 = Layer0.getInstance(graph);
195 Variable parameter = ((Variable)context).browse(graph, "..");
196 Resource parameterResource = parameter.getRepresents(graph);
197 if(graph.sync(new IsEnumeratedValue(parameterResource))) {
198 Map<String, Resource> map = graph.sync(new InstanceEnumerationMap(parameterResource));
199 return new ArrayList<String>(map.keySet());
200 } else if(graph.isInstanceOf(parameterResource, L0.Boolean)) {
201 return CollectionUtils.toList("true", "false");
207 @SCLValue(type = "ReadGraph -> Resource -> a -> b")
208 public static Object getPropertyChildName(ReadGraph graph, Resource resource, Object context) throws DatabaseException {
209 if(context instanceof Variable) {
210 Variable variable = (Variable)context;
211 String label = variable.getParent(graph).getPossiblePropertyValue(graph, "HasLabel", Bindings.STRING);
214 return variable.getParent(graph).getName(graph);
216 throw new DatabaseException("Unknown context " + context);
219 @SCLValue(type = "WriteGraph -> Variable -> a -> b -> String")
220 public static String inputModifier(WriteGraph graph, Variable variable, Object value, Object _binding) throws DatabaseException {
222 // System.err.println("inputModifier " + variable.getURI(graph));
223 Layer0 L0 = Layer0.getInstance(graph);
225 Variable parent = variable.getParent(graph);
226 Resource property = variable.getPredicateResource(graph);
228 Resource container = parent.getRepresents(graph);
229 if(container == null) return null;
230 if(property == null) return null;
232 Statement object = graph.getPossibleStatement(container, property);
233 if(object == null) return null;
235 Resource objectResource = object.getObject();
236 if(graph.sync(new IsEnumeratedValue(objectResource))) {
238 Resource type = graph.getSingleObject(objectResource, L0.PartOf);
240 Map<String, Resource> enumMap = graph.syncRequest(new EnumerationMap(type));
241 Resource newLiteral = enumMap.get(value);
242 graph.deny(container, property, objectResource);
243 graph.claim(container, property, newLiteral);
249 Resource newType = Layer0Utils.getPossibleLiteralType(graph, variable);
250 if(newType == null) {
251 Type type = Layer0Utils.getSCLType(graph, variable);
252 // This means that type is a wildcard e.g. "a"
253 if(Types.canonical(type) instanceof TVar) {
254 newType = Layer0Utils.inferLiteralTypeFromString(graph, value.toString());
256 throw new DatabaseException("Failed to find type for property " + NameUtils.getSafeName(graph, property));
260 boolean correctType = graph.getPossibleType(objectResource, newType) != null;
261 boolean asserted = object.isAsserted(container);
262 if(asserted || !correctType) {
266 Statement dt = graph.getPossibleStatement(objectResource, L0.HasDataType);
267 Datatype custom = dt.isAsserted(objectResource) ? null : (Datatype)graph.getValue(dt.getObject(), datatype_binging);
269 objectResource = graph.newResource();
270 graph.claim(objectResource, L0.InstanceOf, null, newType);
271 graph.claim(container, property, objectResource);
273 // Only set HasValueType if the calculated new SCL type differs from the asserted value type
274 String newValueType = Layer0Utils.getSCLType(custom);
275 String currentValueType = graph.getPossibleRelatedValue(objectResource, L0.HasValueType, Bindings.STRING);
276 if (!newValueType.equals(currentValueType)) {
277 graph.addLiteral(objectResource, L0.HasValueType, L0.HasValueType_Inverse, L0.String, newValueType, Bindings.STRING);
279 graph.addLiteral(objectResource, L0.HasDataType, L0.HasDataType_Inverse, L0.DataType, custom, datatype_binging);
284 if(newType != null) {
286 if(!correctType && !asserted) // if not correct type and not asserted, remove the old value
287 graph.deny(container, property, objectResource);
289 objectResource = graph.newResource();
290 graph.claim(objectResource, L0.InstanceOf, newType);
291 graph.claim(container, property, objectResource);
299 Datatype datatype = variable.getDatatype(graph);
300 Binding binding = (Binding)_binding;
301 Layer0Utils.claimAdaptedValue(graph, objectResource, value, binding, datatype);
307 @SCLValue(type = "ReadGraph -> a -> Resource")
308 public static Resource singleResourceTransformation(ReadGraph graph, Object input) throws DatabaseException {
309 return WorkbenchSelectionUtils.getPossibleResource(graph, input);
313 @SCLValue(type = "ReadGraph -> a -> Variable")
314 public static Variable singleVariableTransformation(ReadGraph graph, Object input) throws DatabaseException {
315 Variable single = WorkbenchSelectionUtils.getPossibleVariable(graph, input);
316 if(single != null) return single;
317 return ISelectionUtils.filterSingleSelection(input, Variable.class);
320 @SCLValue(type = "ReadGraph -> a -> Variable")
321 public static Variable singleResourceToVariableTransformation(ReadGraph graph, Object input) throws DatabaseException {
322 Resource r = WorkbenchSelectionUtils.getPossibleResource(graph, input);
325 return Variables.getPossibleVariable(graph, r);
328 @SCLValue(type = "ReadGraph -> a -> SelectionInput")
329 public static SelectionInput standardSelectionInputTransformation(ReadGraph graph, Object input) throws DatabaseException {
330 WorkbenchSelectionElement wse = WorkbenchSelectionUtils.getPossibleSelectionElement(input);
333 return new StandardSelectionInput(wse);
336 @SCLValue(type = "ValueAccessor")
337 public static ValueAccessor displayUnitValueAccessor = new ValueAccessor() {
340 public Object getValue(ReadGraph graph, Variable context) throws DatabaseException {
341 return Variables.getPossibleUnit(graph, context.getParent(graph));
345 public Object getValue(ReadGraph graph, Variable context, Binding binding) throws DatabaseException {
347 Object value = Variables.getPossibleUnit(graph, context.getParent(graph));
348 if(value == null) return null;
349 Binding srcBinding = Bindings.OBJECT.getContentBinding(value);
350 return Bindings.adapt(value, srcBinding, binding);
351 } catch (AdaptException e) {
352 throw new DatabaseException(e);
353 } catch (BindingException e) {
354 throw new DatabaseException(e);
359 public void setValue(WriteGraph graph, Variable context, Object value) throws DatabaseException {
360 throw new UnsupportedOperationException();
364 public void setValue(WriteGraph graph, Variable context, Object value, Binding binding) throws DatabaseException {
365 throw new UnsupportedOperationException();
369 public Datatype getDatatype(ReadGraph graph, Variable context) throws DatabaseException {
370 return org.simantics.db.layer0.function.All.getDatatypeFromValue(graph, context);
375 @SCLValue(type = "ValueAccessor")
376 public static ValueAccessor displayPropertyValueAccessor = new ValueAccessor() {
379 public Object getValue(ReadGraph graph, Variable context) throws DatabaseException {
380 return getValue(graph, context, Bindings.STRING);
384 public Object getValue(ReadGraph graph, Variable context, Binding binding) throws DatabaseException {
385 Layer0 L0 = Layer0.getInstance(graph);
386 Variable property = context.getParent(graph);
387 Resource predicate = property.getPossiblePredicateResource(graph);
388 if(predicate == null) return property.getName(graph);
389 String value = graph.getPossibleRelatedValue2(predicate, L0.HasLabel, Bindings.STRING);
391 value = graph.getRelatedValue(predicate, L0.HasName, Bindings.STRING);
393 return Bindings.adapt(value, binding, Bindings.STRING);
394 } catch (AdaptException e) {
395 throw new DatabaseException(e);
400 public void setValue(WriteGraph graph, Variable context, Object value) throws DatabaseException {
401 throw new UnsupportedOperationException();
405 public void setValue(WriteGraph graph, Variable context, Object value, Binding binding) throws DatabaseException {
406 throw new UnsupportedOperationException();
410 public Datatype getDatatype(ReadGraph graph, Variable context)
411 throws DatabaseException {
412 return Datatypes.STRING;
417 @SCLValue(type = "ValueAccessor")
418 public static ValueAccessor displayValueValueAccessor = new ValueAccessor() {
421 public Object getValue(ReadGraph graph, Variable context) throws DatabaseException {
422 return getValue(graph, context, Bindings.STRING);
425 public boolean isPrimitive(Datatype dt) {
426 if(Datatypes.STRING.equals(dt)) return true;
430 private String possibleExpression(ReadGraph graph, Variable variable) throws DatabaseException {
432 Layer0 L0 = Layer0.getInstance(graph);
433 Resource object = variable.getPossibleRepresents(graph);
434 if(object != null && graph.isInstanceOf(object, L0.SCLValue)) {
435 String expression = graph.getPossibleRelatedValue(object, L0.SCLValue_expression);
436 if (expression != null)
437 return "=" + expression;
444 public Object getValue(ReadGraph graph, Variable context, Binding _binding) throws DatabaseException {
446 Variable property = context.getParent(graph);
448 String expression = possibleExpression(graph, property);
449 if(expression != null) return expression;
452 Resource formatter = property.getPossiblePropertyValue(graph, Variables.FORMATTER);
453 if(formatter != null) {
455 Formatter fmt = graph.adaptContextual(formatter, property, Variable.class, Formatter.class);
456 value = fmt.format(property.getValue(graph));
460 Variant variant = property.getVariantValue(graph);
461 value = variant.getValue();
463 Binding binding = variant.getBinding();
464 if(binding != null) {
465 Datatype dt = binding.type();
467 if(!isPrimitive(dt)) {
469 value = DataValuePrinter.writeValueSingleLine(binding, value);
470 } catch (IOException e) {
472 } catch (BindingException e) {
482 return Bindings.adapt(value != null ? value.toString() : "null", _binding, Bindings.STRING);
483 } catch (AdaptException e) {
484 throw new DatabaseException(e);
489 public void setValue(WriteGraph graph, Variable context, Object value) throws DatabaseException {
491 Binding binding = Bindings.getBinding(value.getClass());
492 setValue(graph, context, value, binding);
493 } catch (BindingConstructionException e) {
494 throw new DatabaseException(e);
499 public void setValue(WriteGraph graph, Variable context, Object _value, Binding _binding) throws DatabaseException {
502 if(!(_value instanceof String)) throw new DatabaseException("setValue for HasDisplayValue only accepts String (got " + _value.getClass().getSimpleName() + ")");
504 String text = (String)_value;
505 if(text.startsWith("=")) {
506 Variable property = context.getParent(graph);
507 Layer0Utils.setExpression(graph, property, text, ModelingResources.getInstance(graph).SCLValue);
511 String parsedLabel = (String)_value;
512 Object value = parsedLabel;
514 Datatype type = context.getParent(graph).getPossibleDatatype(graph);
517 Binding binding = Bindings.getBinding(type);
519 if (binding instanceof StringBinding) {
521 if (binding instanceof MutableStringBinding)
522 value = new MutableString(parsedLabel);
528 if (binding instanceof NumberBinding) {
529 parsedLabel = parsedLabel.replace(",", ".");
532 value = binding.parseValue(parsedLabel, new DataValueRepository());
535 //System.out.println("VariableWrite " + ObjectUtils.toString(value));
536 context.getParent(graph).setValue(graph, value, binding);
540 context.getParent(graph).setValue(graph, value);
545 // Add a comment to metadata.
546 CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
547 graph.addMetadata(cm.add("Set value " + ObjectUtils.toString(value)));
549 } catch (DataTypeSyntaxError e) {
550 throw new DatabaseException(e);
551 } catch (BindingException e) {
552 throw new DatabaseException(e);
557 public Datatype getDatatype(ReadGraph graph, Variable context)
558 throws DatabaseException {
559 return Datatypes.STRING;