DynamicFormFieldControl.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using InABox.Core;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics.CodeAnalysis;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using Xceed.Wpf.Toolkit.PropertyGrid.Converters;
  11. namespace InABox.DynamicGrid
  12. {
  13. public delegate void FieldChangedEvent();
  14. public interface IDynamicFormFieldControl
  15. {
  16. public event FieldChangedEvent? FieldChangedEvent;
  17. void Deserialize(DFLoadStorageEntry storage);
  18. void Serialize(DFSaveStorageEntry storage);
  19. public object? GetValue();
  20. /// <summary>
  21. /// Sets the value in this control.
  22. /// </summary>
  23. /// <param name="value">The value to set. This will be the return value from a call to <see cref="DFLayoutFieldProperties.ParseValue(object)"/>.</param>
  24. public void SetValue(object? value);
  25. public object? GetEntityValue();
  26. public void SetEntityValue(object? value);
  27. /// <summary>
  28. /// Gets additional data for this field by the specific field name of <paramref name="field"/>.
  29. /// </summary>
  30. /// <param name="field">A name which specifies what data is requested.</param>
  31. /// <returns>The additional data.</returns>
  32. public object? GetData(string field);
  33. /// <summary>
  34. /// Gets additional data for this field that should be saved into the form data.
  35. /// </summary>
  36. /// <returns>The additional data.</returns>
  37. public Dictionary<string, object?>? GetAdditionalValues();
  38. /// <summary>
  39. /// Check that the data is valid - if it is not, output a message for the user.
  40. /// This function gets called when the user completes a form, or edits an already completed form.
  41. /// </summary>
  42. /// <param name="message">The message to the user.</param>
  43. /// <returns><see langword="true"/> if the data is valid.</returns>
  44. public bool Validate([NotNullWhen(false)] out string? message);
  45. }
  46. public abstract class DynamicFormFieldControl<TField, TProperties, TValue, TSerialized> : DynamicFormControl<TField>, IDynamicFormFieldControl
  47. where TField : DFLayoutField<TProperties>
  48. where TProperties : DFLayoutFieldProperties<TValue, TSerialized>, new()
  49. {
  50. public event FieldChangedEvent? FieldChangedEvent;
  51. public TField Field { get => Control; set => Control = value; }
  52. public void Serialize(DFSaveStorageEntry storage)
  53. {
  54. Field.Properties.SerializeValue(storage, GetSerializedValue());
  55. }
  56. public void Deserialize(DFLoadStorageEntry storage)
  57. {
  58. SetSerializedValue(Field.Properties.DeserializeValue(storage));
  59. }
  60. protected void ChangeField() => FieldChangedEvent?.Invoke();
  61. /// <summary>
  62. /// Checks whether the user has supplied a field - for use with <see cref="Validate(out string?)"/>.
  63. /// </summary>
  64. /// <returns><see langword="true"/> if the user has not supplied a value.</returns>
  65. protected abstract bool IsEmpty();
  66. public virtual bool Validate([NotNullWhen(false)] out string? message)
  67. {
  68. if(Field.Properties.Required && IsEmpty())
  69. {
  70. message = $"Field [{Field.Name}] is required!";
  71. return false;
  72. }
  73. message = null;
  74. return true;
  75. }
  76. protected override void AfterSetControl(TField control)
  77. {
  78. base.AfterSetControl(control);
  79. if (!string.IsNullOrWhiteSpace(control.Properties.Expression))
  80. {
  81. IsEnabled = false;
  82. }
  83. }
  84. public abstract TSerialized GetSerializedValue();
  85. public abstract void SetSerializedValue(TSerialized value);
  86. public abstract TValue GetValue();
  87. public abstract void SetValue(TValue? value);
  88. public virtual object? GetEntityValue() => GetValue();
  89. public virtual void SetEntityValue(object? value) => SetValue(CoreUtils.ChangeType<TValue>(value));
  90. public virtual object? GetData(string dataField)
  91. {
  92. return null;
  93. }
  94. public virtual Dictionary<string, object?>? GetAdditionalValues()
  95. {
  96. return null;
  97. }
  98. object? IDynamicFormFieldControl.GetValue() => GetValue();
  99. void IDynamicFormFieldControl.SetValue(object? value) => SetValue(value != null ? (TValue)value : default);
  100. }
  101. }