DFLayoutDateTimeFieldProperties.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using System;
  2. namespace InABox.Core
  3. {
  4. public class DFLayoutDateTimeFieldProperties : DFLayoutFieldProperties<DateTime, DateTime?>
  5. {
  6. [EditorSequence(-995)]
  7. [DateTimeEditor]
  8. public override DateTime Default { get; set; }
  9. public DFLayoutDateTimeFieldProperties()
  10. {
  11. Format = "mm HH dd MMM yy";
  12. }
  13. public string Format { get; set; }
  14. public override string FormatValue(DateTime? value)
  15. {
  16. return string.Format("{0" + (string.IsNullOrWhiteSpace(Format) ? "" : ":" + Format.Replace(":", "\\:")) + "}", value);
  17. }
  18. public override DateTime GetValue(DateTime? value)
  19. {
  20. return value ?? Default;
  21. }
  22. public override void SerializeValue(DFSaveStorageEntry entry, DateTime? value)
  23. {
  24. if (value != null)
  25. {
  26. entry.SetValue(value);
  27. }
  28. }
  29. public override DateTime? DeserializeValue(DFLoadStorageEntry entry)
  30. {
  31. var value = entry.GetValue();
  32. if (value is DateTime date)
  33. return date;
  34. if (DateTime.TryParse(value as string, out var result))
  35. return result;
  36. return null;
  37. }
  38. protected override void LoadProperties()
  39. {
  40. base.LoadProperties();
  41. Format = GetProperty("Format", Format);
  42. }
  43. protected override void SaveProperties()
  44. {
  45. base.SaveProperties();
  46. SetProperty("Format", Format);
  47. }
  48. }
  49. }