LookupEditorControl.cs 7.7 KB

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