Editable.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System;
  2. namespace InABox.Core
  3. {
  4. public enum Editable
  5. {
  6. Enabled,
  7. Disabled,
  8. Hidden,
  9. /// <summary>
  10. /// Disabled when using a DynamicGrid with DirectEdit option set, but enabled in editor
  11. /// </summary>
  12. DisabledOnDirectEdit
  13. }
  14. /// <summary>
  15. /// Set whether or not this property is editable, overriding any <see cref="BaseEditor"/> on this property or any sub-properties.
  16. /// </summary>
  17. /// <remarks>
  18. /// What this actually does is that when <see cref="DatabaseSchema"/> is loading a property, it checks to find the outer-most parent property
  19. /// of that property with an <see cref="EditableAttribute"/>, or if the property has one of its own. If so, it overrides the <see cref="Editable"/>
  20. /// property of that property's <see cref="IProperty.Editor"/>, combining using the <see cref="EditableUtils.Combine(Editable, Editable)"/> function.
  21. /// </remarks>
  22. public class EditableAttribute: Attribute
  23. {
  24. public Editable Editable { get; set; }
  25. public EditableAttribute(Editable editable)
  26. {
  27. Editable = editable;
  28. }
  29. }
  30. public static class EditableUtils
  31. {
  32. public static bool IsEditable(this Editable editable)
  33. {
  34. return editable == Editable.Enabled || editable == Editable.DisabledOnDirectEdit;
  35. }
  36. public static bool IsDirectEditable(this Editable editable)
  37. {
  38. return editable == Editable.Enabled;
  39. }
  40. public static bool ColumnVisible(this Editable editable)
  41. {
  42. return editable != Editable.Hidden;
  43. }
  44. public static bool EditorVisible(this Editable editable)
  45. {
  46. return editable != Editable.Hidden;
  47. }
  48. /// <summary>
  49. /// Combine (restrictively) two editable enums.
  50. /// </summary>
  51. /// <param name="editable"></param>
  52. /// <param name="other"></param>
  53. /// <returns></returns>
  54. public static Editable Combine(this Editable editable, Editable other)
  55. {
  56. switch (editable)
  57. {
  58. case Editable.Enabled:
  59. return other;
  60. case Editable.DisabledOnDirectEdit:
  61. if (other == Editable.Disabled || other == Editable.Hidden)
  62. return other;
  63. return editable;
  64. case Editable.Disabled:
  65. if (other == Editable.Hidden)
  66. return other;
  67. return editable;
  68. case Editable.Hidden:
  69. default:
  70. return editable;
  71. }
  72. }
  73. }
  74. }