CodePopupEditorControl.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Controls.Primitives;
  7. using System.Windows.Input;
  8. using System.Windows.Media;
  9. using InABox.Clients;
  10. using InABox.Core;
  11. namespace InABox.DynamicGrid;
  12. public class CodePopupEditorControl : DynamicEditorControl<Guid, CodePopupEditor>, IPopupEditorControl
  13. {
  14. static CodePopupEditorControl()
  15. {
  16. //DynamicEditorControlFactory.Register<CodePopupEditorControl, CodePopupEditor>();
  17. }
  18. private Type _type;
  19. private Guid _value = Guid.Empty;
  20. private TextBox Description;
  21. private TextBox Editor;
  22. private string origvalue = "";
  23. public CodePopupEditorControl()
  24. {
  25. OtherColumns = new Dictionary<string, string>();
  26. }
  27. public Dictionary<string, string> OtherColumns { get; }
  28. public string CodeColumn { get; set; }
  29. protected override FrameworkElement CreateEditor()
  30. {
  31. var Grid = new Grid
  32. {
  33. VerticalAlignment = VerticalAlignment.Stretch,
  34. HorizontalAlignment = HorizontalAlignment.Stretch
  35. };
  36. Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(130, GridUnitType.Pixel) });
  37. Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(20, GridUnitType.Pixel) });
  38. Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
  39. Editor = new TextBox
  40. {
  41. CharacterCasing = CharacterCasing.Upper,
  42. VerticalAlignment = VerticalAlignment.Stretch,
  43. VerticalContentAlignment = VerticalAlignment.Center,
  44. HorizontalAlignment = HorizontalAlignment.Stretch
  45. };
  46. Editor.BorderThickness = new Thickness(0.75);
  47. Editor.BorderBrush = new SolidColorBrush(Colors.Silver);
  48. Editor.SetValue(Grid.ColumnProperty, 0);
  49. Editor.SetValue(Grid.RowProperty, 0);
  50. Editor.PreviewKeyDown += Editor_PreviewKeyDown;
  51. Editor.GotFocus += Editor_GotFocus;
  52. Editor.AcceptsTab = true;
  53. Editor.LostFocus += Editor_LostFocus;
  54. Grid.Children.Add(Editor);
  55. var Select = new Button
  56. {
  57. VerticalAlignment = VerticalAlignment.Stretch,
  58. VerticalContentAlignment = VerticalAlignment.Center,
  59. HorizontalAlignment = HorizontalAlignment.Stretch,
  60. Content = "..",
  61. Focusable = false
  62. };
  63. Select.BorderThickness = new Thickness(0, 0.75, 0.75, 0.75);
  64. Select.BorderBrush = new SolidColorBrush(Colors.Gray);
  65. Select.SetValue(Grid.ColumnProperty, 1);
  66. Select.SetValue(Grid.RowProperty, 0);
  67. Select.Click += Select_Click;
  68. Grid.Children.Add(Select);
  69. Description = new TextBox
  70. {
  71. VerticalAlignment = VerticalAlignment.Stretch,
  72. VerticalContentAlignment = VerticalAlignment.Center,
  73. HorizontalAlignment = HorizontalAlignment.Stretch,
  74. Margin = new Thickness(5, 0, 0, 0),
  75. IsReadOnly = true,
  76. Focusable = false,
  77. Background = new SolidColorBrush(Colors.WhiteSmoke),
  78. BorderBrush = new SolidColorBrush(Colors.Silver)
  79. };
  80. Description.SetValue(Grid.ColumnProperty, 2);
  81. Description.SetValue(Grid.RowProperty, 0);
  82. Grid.Children.Add(Description);
  83. return Grid;
  84. }
  85. private void Editor_GotFocus(object sender, RoutedEventArgs e)
  86. {
  87. origvalue = Editor.Text;
  88. }
  89. private void Editor_LostFocus(object sender, RoutedEventArgs e)
  90. {
  91. if (Editor.Text.Equals(origvalue))
  92. return;
  93. var id = _value;
  94. LookupValue(CodeColumn, Editor.Text);
  95. if (id == Guid.Empty)
  96. CheckChanged();
  97. }
  98. private void Editor_PreviewKeyDown(object sender, KeyEventArgs e)
  99. {
  100. if (e.Key.Equals(Key.Enter) || e.Key.Equals(Key.Tab))
  101. {
  102. if (string.IsNullOrEmpty(Editor.Text))
  103. {
  104. if (!Editor.Text.Equals(origvalue))
  105. {
  106. LookupValue("ID", Guid.Empty);
  107. CheckChanged();
  108. }
  109. Editor.MoveFocus(new TraversalRequest(Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift)
  110. ? FocusNavigationDirection.Previous
  111. : FocusNavigationDirection.Next));
  112. }
  113. else
  114. {
  115. if (!Editor.Text.Equals(origvalue))
  116. {
  117. var code = Editor.Text;
  118. LookupValue(CodeColumn, code);
  119. if (_value != Guid.Empty)
  120. {
  121. CheckChanged();
  122. Editor.MoveFocus(new TraversalRequest(Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift)
  123. ? FocusNavigationDirection.Previous
  124. : FocusNavigationDirection.Next));
  125. }
  126. else
  127. {
  128. if (DoSearch(code))
  129. Editor.MoveFocus(new TraversalRequest(Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift)
  130. ? FocusNavigationDirection.Previous
  131. : FocusNavigationDirection.Next));
  132. }
  133. }
  134. else
  135. {
  136. Editor.MoveFocus(new TraversalRequest(Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift)
  137. ? FocusNavigationDirection.Previous
  138. : FocusNavigationDirection.Next));
  139. }
  140. }
  141. e.Handled = true;
  142. }
  143. }
  144. private void Editor_PreviewKeyUp(object sender, KeyEventArgs e)
  145. {
  146. }
  147. private void Editor_KeyUp(object sender, KeyEventArgs e)
  148. {
  149. }
  150. private void Select_Click(object sender, RoutedEventArgs e)
  151. {
  152. DoSearch(Editor.Text);
  153. }
  154. private bool DoSearch(string code)
  155. {
  156. var result = false;
  157. if (_type != null)
  158. {
  159. var columns = LookupFactory.DefineLookupColumns(Host.GetEditorType(), _type, ColumnName);
  160. var cols = OtherColumns.Keys.ToList();
  161. foreach (var column in columns.ColumnNames())
  162. if (!cols.Contains(column))
  163. cols.Add(column);
  164. var list = new PopupList(_type, _value, cols.ToArray(), new Dictionary<string, string> { { CodeColumn, code } });
  165. list.OnDefineFilter += t => LookupFactory.DefineLookupFilter(Host.GetEditorType(), t, ColumnName, Host.GetItems());
  166. if (list.ShowDialog() == true)
  167. {
  168. result = true;
  169. _value = list.ID;
  170. foreach (var key in OtherColumns.Keys)
  171. OtherValues[OtherColumns[key]] = list.OtherValues[key];
  172. CheckChanged();
  173. Editor.Text = string.Format("{0}", list.OtherValues[CodeColumn]);
  174. var display = new Dictionary<string, object?>();
  175. foreach (var key in list.OtherValues.Keys.Where(x => !string.Equals(x, CodeColumn)))
  176. display[key] = list.OtherValues[key];
  177. Description.Text =
  178. LookupFactory.FormatLookup(_type, display, new[] { CodeColumn });
  179. }
  180. }
  181. return result;
  182. }
  183. //private void Clear_Click(object sender, RoutedEventArgs e)
  184. //{
  185. // if (_type != null)
  186. // {
  187. // _value = Guid.Empty;
  188. // foreach (var otherfield in OtherColumns.Keys)
  189. // OtherValues[OtherColumns[otherfield]] = null;
  190. // CheckChanged();
  191. // Editor.Text = "";
  192. // }
  193. //}
  194. public override int DesiredHeight()
  195. {
  196. return 25;
  197. }
  198. public override int DesiredWidth()
  199. {
  200. return int.MaxValue;
  201. }
  202. protected override Guid RetrieveValue()
  203. {
  204. return _value;
  205. }
  206. protected override void UpdateValue(Guid value)
  207. {
  208. var oldvalue = _value;
  209. _value = value;
  210. if (oldvalue != value)
  211. {
  212. if (Equals(value, Guid.Empty))
  213. SetEmptyValue();
  214. else
  215. LookupValue("ID", value);
  216. }
  217. }
  218. private void SetEmptyValue()
  219. {
  220. Editor.Text = "";
  221. Description.Text = "";
  222. }
  223. private void LookupValue(string column, object value)
  224. {
  225. var client = ClientFactory.CreateClient(_type);
  226. var columns = LookupFactory.DefineLookupColumns(Host.GetEditorType(), _type, ColumnName);
  227. foreach (var key in OtherColumns)
  228. columns.Add(key.Key);
  229. var sort = LookupFactory.DefineSort(_type);
  230. var filter = Filter.Create(_type, column).IsEqualTo(value);
  231. var lookup = client.Query(filter, columns, sort);
  232. var display = new Dictionary<string,object?>();
  233. var code = "";
  234. var id = Guid.Empty;
  235. var displaycols = LookupFactory.DefineLookupColumns(Host.GetEditorType(), _type, ColumnName).ColumnNames();
  236. var row = lookup.Rows.FirstOrDefault();
  237. row ??= lookup.NewRow(true);
  238. code = row[CodeColumn]?.ToString();
  239. id = (Guid)row["ID"];
  240. foreach (var col in displaycols.Where(x => !x.Equals("ID") && !x.Equals(CodeColumn)))
  241. display[col] = row[col];
  242. foreach (var key in OtherColumns.Keys)
  243. if (lookup.Columns.Any(x => x.ColumnName.Equals(key)))
  244. OtherValues[OtherColumns[key]] = row[key];
  245. _value = id;
  246. Editor.Text = code;
  247. Description.Text = LookupFactory.FormatLookup(_type, display, new[] { CodeColumn });
  248. }
  249. private string GetCodeColumn(Type type)
  250. {
  251. var prop = DatabaseSchema.Properties(type)
  252. .Where(x => x.Editor is BaseCodeEditor && !x.HasParentEntityLink())
  253. .FirstOrDefault() ?? throw new Exception($"No code property for {type.EntityName()}");
  254. return prop.Name;
  255. }
  256. public override void Configure()
  257. {
  258. _type = EditorDefinition.Type;
  259. Host.LoadColumns(ColumnName, OtherColumns);
  260. Host.LoadColumns(ColumnName, EditorDefinition.OtherColumns);
  261. CodeColumn = !string.IsNullOrEmpty(EditorDefinition.CodeColumn) ? EditorDefinition.CodeColumn : GetCodeColumn(_type);
  262. }
  263. public override void SetFocus()
  264. {
  265. Editor.Focus();
  266. }
  267. public override void SetColor(Color color)
  268. {
  269. Editor.Background = IsEnabled ? new SolidColorBrush(color) : new SolidColorBrush(Colors.WhiteSmoke);
  270. }
  271. }