LookupEditorControl.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Media;
  6. using InABox.Core;
  7. namespace InABox.DynamicGrid
  8. {
  9. public class LookupEditorControl : DynamicEditorControl<object, ILookupEditor>, ILookupEditorControl
  10. {
  11. static LookupEditorControl()
  12. {
  13. //DynamicEditorControlFactory.Register<LookupEditorControl, ILookupEditor, LookupEditor>();
  14. //DynamicEditorControlFactory.Register<LookupEditorControl, ILookupEditor, EnumLookupEditor>();
  15. //DynamicEditorControlFactory.Register<LookupEditorControl, ILookupEditor, ComboLookupEditor>();
  16. }
  17. private ComboBox Editor;
  18. private CoreTable LookupTable;
  19. private bool _loading = false;
  20. public ILookupEditor LookupEditorDefinition => EditorDefinition;
  21. public LookupEditorControl()
  22. {
  23. OtherColumns = new HashSet<string>();
  24. Lookups = new Dictionary<object, string>();
  25. Width = int.MaxValue;
  26. }
  27. public int Width { get; set; }
  28. private Dictionary<object, string> Lookups { get; set; }
  29. private HashSet<string> OtherColumns { get; }
  30. public override void Configure()
  31. {
  32. if (Loaded)
  33. return;
  34. if (EditorDefinition.LookupWidth != int.MaxValue)
  35. Width = EditorDefinition.LookupWidth;
  36. Editor.HorizontalAlignment = Width.Equals(int.MaxValue) ? HorizontalAlignment.Stretch : HorizontalAlignment.Left;
  37. if (!Width.Equals(int.MaxValue))
  38. Editor.Width = Width;
  39. Editor.IsEnabled = false;
  40. Host.LoadLookups(this);
  41. }
  42. public void LoadLookups(CoreTable values)
  43. {
  44. _loading = true;
  45. var keycol = ColumnName.Split('.').Last();
  46. var prefix = string.Join(".", ColumnName.Split('.').Reverse().Skip(1).Reverse());
  47. foreach (var col in values.Columns)
  48. if (!string.Equals(col.ColumnName, keycol) && !string.Equals(col.ColumnName, "Display"))
  49. OtherColumns.Add(col.ColumnName);
  50. var value = RetrieveValue();
  51. //Lookups = lookups;
  52. LookupTable = values;
  53. Editor.Items.Clear();
  54. Lookups.Clear();
  55. if (!EditorDefinition.IsEnumEditor())
  56. Editor.Items.Add("");
  57. foreach (var row in values.Rows)
  58. {
  59. var display = row["Display"];
  60. Editor.Items.Add(display);
  61. var val = row[keycol];
  62. if (val is not null)
  63. {
  64. Lookups[val] = display?.ToString() ?? "";
  65. }
  66. }
  67. var sel = value != null ? values.Rows.FirstOrDefault(r => r[keycol].Equals(value)) : null;
  68. if (EditorDefinition.IsEnumEditor())
  69. Editor.SelectedIndex = sel != null ? sel.Index : 0;
  70. else
  71. Editor.SelectedIndex = sel != null ? sel.Index + 1 : 0;
  72. //foreach (var key in lookups.Keys)
  73. // Editor.Items.Add(lookups[key]);
  74. //if ((value != null) && Lookups.ContainsKey(value))
  75. // Editor.SelectedIndex = Lookups.Keys.ToList().IndexOf(value);
  76. Editor.IsEnabled = EditorDefinition.Editable.IsEditable();
  77. _loading = false;
  78. }
  79. public override int DesiredHeight()
  80. {
  81. return 25;
  82. }
  83. public override void SetFocus()
  84. {
  85. Editor.Focus();
  86. }
  87. public List<Button> Buttons { get; private set; }
  88. protected override FrameworkElement CreateEditor()
  89. {
  90. var dock = new DockPanel();
  91. Buttons = CreateButtons(out var DisableEditor);
  92. foreach (var button in Buttons)
  93. {
  94. button.SetValue(DockPanel.DockProperty, Dock.Right);
  95. dock.Children.Add(button);
  96. dock.Width += button.Width + 5;
  97. }
  98. Editor = new ComboBox
  99. {
  100. VerticalAlignment = VerticalAlignment.Stretch,
  101. VerticalContentAlignment = VerticalAlignment.Center,
  102. HorizontalAlignment = Width.Equals(int.MaxValue) ? HorizontalAlignment.Stretch : HorizontalAlignment.Left
  103. };
  104. if (!Width.Equals(int.MaxValue))
  105. Width = Width;
  106. Editor.SelectionChanged += Combobox_SelectionChanged;
  107. Editor.IsEnabled = false;
  108. if (DisableEditor)
  109. {
  110. Editor.Background = new SolidColorBrush(Colors.Silver);
  111. Editor.IsEnabled = false;
  112. }
  113. dock.Children.Add(Editor);
  114. return dock;
  115. }
  116. private void Combobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  117. {
  118. if (!Editor.IsEnabled || _loading)
  119. return;
  120. if (LookupTable != null)
  121. {
  122. var keycol = ColumnName.Split('.').Last();
  123. var value = RetrieveValue();
  124. var row = LookupTable.Rows.FirstOrDefault(r => Equals(r[keycol], value));
  125. foreach (var field in LookupTable.Columns.Where(x => OtherColumns.Contains(x.ColumnName)))
  126. {
  127. OtherValues[field.ColumnName] = row?[field.ColumnName];
  128. }
  129. }
  130. CheckChanged();
  131. }
  132. public override int DesiredWidth()
  133. {
  134. return int.MaxValue;
  135. }
  136. protected override object RetrieveValue()
  137. {
  138. if (EditorDefinition.IsEnumEditor())
  139. {
  140. if (Editor.SelectedIndex >= 0 && Editor.SelectedIndex < Lookups.Keys.Count)
  141. return Lookups.Keys.ElementAt(Editor.SelectedIndex);
  142. }
  143. else
  144. {
  145. if (Editor.SelectedIndex > 0 && Editor.SelectedIndex <= Lookups.Keys.Count)
  146. return Lookups.Keys.ElementAt(Editor.SelectedIndex - 1);
  147. }
  148. return null;
  149. }
  150. protected override void UpdateValue(object value)
  151. {
  152. if (Editor == null)
  153. return;
  154. if (value == null)
  155. {
  156. Editor.SelectedItem = null;
  157. return;
  158. }
  159. if (!Loaded && !Editor.IsEnabled)
  160. {
  161. Lookups.Clear();
  162. Lookups[value] = "Loading";
  163. Editor.Items.Clear();
  164. Editor.Items.Add("");
  165. Editor.Items.Add("Loading...");
  166. Editor.SelectedIndex = EditorDefinition.IsEnumEditor() ? 0 : 1;
  167. return;
  168. }
  169. if (Lookups.ContainsKey(value))
  170. {
  171. if (EditorDefinition.IsEnumEditor())
  172. Editor.SelectedIndex = Lookups.Keys.ToList().IndexOf(value);
  173. else
  174. Editor.SelectedIndex = Lookups.Keys.ToList().IndexOf(value) + 1;
  175. }
  176. else
  177. {
  178. Editor.SelectedIndex = 0;
  179. }
  180. }
  181. public override void SetColor(Color color)
  182. {
  183. //Editor.Background = new SolidColorBrush(color);
  184. }
  185. public override void SetEnabled(bool enabled)
  186. {
  187. base.SetEnabled(enabled);
  188. Editor.IsEnabled = enabled;
  189. }
  190. }
  191. }