DynamicEditorControl.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. using System.Collections.Generic;
  2. using System.ComponentModel;
  3. using System.Linq;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Media;
  7. using InABox.Core;
  8. namespace InABox.DynamicGrid
  9. {
  10. public delegate void EditorControlValueChangedHandler(IDynamicEditorControl sender, Dictionary<string, object> values);
  11. public interface IDynamicEditorControl
  12. {
  13. string ColumnName { get; set; }
  14. bool Loaded { get; set; }
  15. BaseEditor EditorDefinition { get; set; }
  16. bool IsEnabled { get; set; }
  17. void SetFocus();
  18. int DesiredHeight();
  19. event EditorControlValueChangedHandler OnEditorValueChanged;
  20. void Configure();
  21. void SetEnabled(bool enabled);
  22. void SetVisible(bool enabled);
  23. void SetValue(object value);
  24. object? GetValue();
  25. }
  26. public interface ILookupEditorControl : IDynamicEditorControl
  27. {
  28. Dictionary<string, string> OtherColumns { get; }
  29. event OnDefineLookup OnDefineLookups;
  30. event OnLookupsDefined OnLookupsDefined;
  31. void LoadLookups(CoreTable values);
  32. }
  33. public interface IPopupEditorControl
  34. {
  35. Dictionary<string, string> OtherColumns { get; }
  36. event OnDefineFilter OnDefineFilter;
  37. }
  38. public abstract class BaseDynamicEditorControl : ContentControl, IDynamicEditorControl
  39. {
  40. public static readonly DependencyProperty ColumnNameProperty =
  41. DependencyProperty.Register(nameof(ColumnName), typeof(string), typeof(BaseDynamicEditorControl));
  42. public static readonly DependencyProperty ColorProperty =
  43. DependencyProperty.Register("Color", typeof(Color), typeof(BaseDynamicEditorControl));
  44. public new static readonly DependencyProperty IsEnabledProperty =
  45. DependencyProperty.Register(nameof(IsEnabled), typeof(bool), typeof(BaseDynamicEditorControl));
  46. private BaseEditor _editordefinition;
  47. public BaseDynamicEditorControl()
  48. {
  49. Color = Colors.LightYellow;
  50. }
  51. public Color Color
  52. {
  53. get => (Color)GetValue(ColorProperty);
  54. set
  55. {
  56. SetValue(ColorProperty, value);
  57. if (EditorDefinition != null)
  58. SetColor(Color);
  59. }
  60. }
  61. public string ColumnName
  62. {
  63. get => (string)GetValue(ColumnNameProperty);
  64. set => SetValue(ColumnNameProperty, value);
  65. }
  66. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  67. public BaseEditor EditorDefinition
  68. {
  69. get => _editordefinition;
  70. set
  71. {
  72. _editordefinition = value;
  73. Content = CreateEditor();
  74. SetColor(Color);
  75. }
  76. }
  77. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  78. public bool Loaded { get; set; }
  79. public virtual event EditorControlValueChangedHandler OnEditorValueChanged;
  80. public abstract int DesiredHeight();
  81. public abstract void SetFocus();
  82. public virtual void Configure()
  83. {
  84. }
  85. public abstract void SetEnabled(bool enabled);
  86. public abstract void SetVisible(bool visible);
  87. public abstract void SetValue(object? value);
  88. public abstract object? GetValue();
  89. public new bool IsEnabled
  90. {
  91. get => (bool)GetValue(IsEnabledProperty);
  92. set
  93. {
  94. SetValue(IsEnabledProperty, value);
  95. SetEnabled(value);
  96. }
  97. }
  98. protected abstract FrameworkElement CreateEditor();
  99. public abstract int DesiredWidth();
  100. public abstract void SetColor(Color color);
  101. protected List<Button> CreateButtons(out bool bDisableEditor)
  102. {
  103. var buttons = new List<Button>();
  104. bDisableEditor = false;
  105. if (EditorDefinition != null && EditorDefinition is IButtonEditor)
  106. {
  107. var ce = (IButtonEditor)EditorDefinition;
  108. if (ce.Buttons != null)
  109. foreach (var ceb in ce.Buttons.Reverse())
  110. {
  111. if (ceb.DisableEditor)
  112. bDisableEditor = true;
  113. var button = new Button
  114. {
  115. HorizontalAlignment = HorizontalAlignment.Left,
  116. VerticalAlignment = VerticalAlignment.Stretch,
  117. VerticalContentAlignment = VerticalAlignment.Center,
  118. HorizontalContentAlignment = HorizontalAlignment.Center,
  119. Content = ceb.Caption,
  120. Width = ceb.Width,
  121. Margin = new Thickness(5, 0, 0, 0),
  122. Padding = new Thickness(5, 0, 5, 0),
  123. Tag = ceb,
  124. IsEnabled = ceb.IsEnabled
  125. };
  126. button.Click += (o, e) =>
  127. {
  128. var b = (Button)o;
  129. var eb = b.Tag as EditorButton;
  130. eb.Click(this);
  131. };
  132. ceb.OnEnabled += (enabled) =>
  133. {
  134. button.IsEnabled = enabled;
  135. };
  136. buttons.Add(button);
  137. }
  138. }
  139. return buttons;
  140. }
  141. }
  142. public abstract class DynamicEditorControl<T> : BaseDynamicEditorControl
  143. {
  144. //{
  145. // get { return (EditorControlValueChangedHandler)GetValue(OnEditorValueChangedProperty); }
  146. // set { SetValue(OnEditorValueChangedProperty, value) }
  147. //}
  148. protected bool _changed;
  149. private bool Updating;
  150. public DynamicEditorControl()
  151. {
  152. Loaded = false;
  153. OtherValues = new Dictionary<string, object>();
  154. MinHeight = 25;
  155. Focusable = false;
  156. }
  157. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  158. public bool Changed
  159. {
  160. get => _changed;
  161. set => _changed = value;
  162. }
  163. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  164. public T Value
  165. {
  166. get => RetrieveValue();
  167. set => UpdateValue(value);
  168. }
  169. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  170. protected virtual Dictionary<string, object?> OtherValues { get; }
  171. //public static readonly DependencyProperty OnEditorValueChangedProperty =
  172. // DependencyProperty.Register(nameof(OnEditorValueChanged), typeof(EditorControlValueChangedHandler), typeof(DynamicEditorControl<T>));
  173. public override event EditorControlValueChangedHandler OnEditorValueChanged;
  174. protected bool CheckChanged()
  175. {
  176. //Logger.Send(LogType.Information, "", string.Format("{0}({1}).CheckChanged()", GetType().EntityName().Split('.').Last(), ColumnName));
  177. if (Loaded && !Updating)
  178. {
  179. Updating = true;
  180. try
  181. {
  182. var values = new Dictionary<string, object>();
  183. var sColumn = string.IsNullOrEmpty(ColumnName) ? "" : ColumnName;
  184. values[sColumn] = RetrieveValue();
  185. foreach (var key in OtherValues.Keys)
  186. values[key] = OtherValues[key];
  187. OnEditorValueChanged?.Invoke(this, values);
  188. }
  189. finally
  190. {
  191. Changed = true;
  192. Updating = false;
  193. }
  194. }
  195. return Changed;
  196. }
  197. public override void SetValue(object? value)
  198. {
  199. UpdateValue(value != null ? (T)value : default);
  200. }
  201. public override object? GetValue()
  202. {
  203. return RetrieveValue();
  204. }
  205. protected abstract T RetrieveValue();
  206. protected abstract void UpdateValue(T value);
  207. public override bool ShouldSerializeContent()
  208. {
  209. return false;
  210. }
  211. public override void SetEnabled(bool enabled)
  212. {
  213. var element = Content as FrameworkElement;
  214. element.IsEnabled = enabled;
  215. SetColor(enabled ? Color : Colors.WhiteSmoke);
  216. }
  217. public override void SetVisible(bool visible)
  218. {
  219. Visibility = visible ? Visibility.Visible : Visibility.Collapsed;
  220. }
  221. }
  222. }