CodePopupEditorControl.cs 11 KB

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