CodePopupEditorControl.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. {
  13. public class CodePopupEditorControl : DynamicEditorControl<Guid, CodePopupEditor>, IPopupEditorControl
  14. {
  15. static CodePopupEditorControl()
  16. {
  17. //DynamicEditorControlFactory.Register<CodePopupEditorControl, CodePopupEditor>();
  18. }
  19. private Type _type;
  20. private Guid _value = Guid.Empty;
  21. private TextBox Description;
  22. private TextBox Editor;
  23. private string origvalue = "";
  24. public CodePopupEditorControl()
  25. {
  26. OtherColumns = new Dictionary<string, string>();
  27. }
  28. public Dictionary<string, string> OtherColumns { get; }
  29. public string CodeColumn { get; set; }
  30. protected override FrameworkElement CreateEditor()
  31. {
  32. var Grid = new Grid
  33. {
  34. VerticalAlignment = VerticalAlignment.Stretch,
  35. HorizontalAlignment = HorizontalAlignment.Stretch
  36. };
  37. Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(130, GridUnitType.Pixel) });
  38. Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(20, GridUnitType.Pixel) });
  39. Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
  40. Editor = new TextBox
  41. {
  42. CharacterCasing = CharacterCasing.Upper,
  43. VerticalAlignment = VerticalAlignment.Stretch,
  44. VerticalContentAlignment = VerticalAlignment.Center,
  45. HorizontalAlignment = HorizontalAlignment.Stretch
  46. };
  47. Editor.BorderThickness = new Thickness(0.75);
  48. Editor.BorderBrush = new SolidColorBrush(Colors.Black);
  49. Editor.SetValue(Grid.ColumnProperty, 0);
  50. Editor.SetValue(Grid.RowProperty, 0);
  51. Editor.PreviewKeyDown += Editor_PreviewKeyDown;
  52. Editor.GotFocus += Editor_GotFocus;
  53. Editor.AcceptsTab = true;
  54. Editor.LostFocus += Editor_LostFocus;
  55. Grid.Children.Add(Editor);
  56. var Select = new Button
  57. {
  58. VerticalAlignment = VerticalAlignment.Stretch,
  59. VerticalContentAlignment = VerticalAlignment.Center,
  60. HorizontalAlignment = HorizontalAlignment.Stretch,
  61. Content = "..",
  62. Focusable = false
  63. };
  64. Select.BorderThickness = new Thickness(0, 0.75, 0.75, 0.75);
  65. Select.BorderBrush = new SolidColorBrush(Colors.Black);
  66. Select.SetValue(Grid.ColumnProperty, 1);
  67. Select.SetValue(Grid.RowProperty, 0);
  68. Select.Click += Select_Click;
  69. Grid.Children.Add(Select);
  70. Description = new TextBox
  71. {
  72. VerticalAlignment = VerticalAlignment.Stretch,
  73. VerticalContentAlignment = VerticalAlignment.Center,
  74. HorizontalAlignment = HorizontalAlignment.Stretch,
  75. Margin = new Thickness(5, 0, 0, 0),
  76. IsReadOnly = true,
  77. Focusable = false,
  78. Background = new SolidColorBrush(Colors.Gainsboro)
  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.DefineColumns(_type);
  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 => { return Host.DefineFilter(t); };
  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,
  179. new[] { CodeColumn });
  180. }
  181. }
  182. return result;
  183. }
  184. //private void Clear_Click(object sender, RoutedEventArgs e)
  185. //{
  186. // if (_type != null)
  187. // {
  188. // _value = Guid.Empty;
  189. // foreach (var otherfield in OtherColumns.Keys)
  190. // OtherValues[OtherColumns[otherfield]] = null;
  191. // CheckChanged();
  192. // Editor.Text = "";
  193. // }
  194. //}
  195. public override int DesiredHeight()
  196. {
  197. return 25;
  198. }
  199. public override int DesiredWidth()
  200. {
  201. return int.MaxValue;
  202. }
  203. protected override Guid RetrieveValue()
  204. {
  205. return _value;
  206. }
  207. protected override void UpdateValue(Guid value)
  208. {
  209. var oldvalue = _value;
  210. _value = value;
  211. if (oldvalue != value)
  212. {
  213. if (Equals(value, Guid.Empty))
  214. SetEmptyValue();
  215. else
  216. LookupValue("ID", value);
  217. }
  218. }
  219. private void SetEmptyValue()
  220. {
  221. Editor.Text = "";
  222. Description.Text = "";
  223. }
  224. private void LookupValue(string column, object value)
  225. {
  226. var client = ClientFactory.CreateClient(_type);
  227. var columns = LookupFactory.DefineColumns(_type);
  228. var cols = columns.ColumnNames();
  229. foreach (var key in OtherColumns.Where(x => !cols.Contains(x.Key)))
  230. columns.Add(key.Key);
  231. var sort = LookupFactory.DefineSort(_type);
  232. var filter = Activator.CreateInstance(typeof(Filter<>).MakeGenericType(_type));
  233. CoreUtils.SetPropertyValue(filter, "Expression", CoreUtils.GetMemberExpression(_type, column));
  234. CoreUtils.SetPropertyValue(filter, "Operator", Operator.IsEqualTo);
  235. CoreUtils.SetPropertyValue(filter, "Value", value);
  236. var lookup = client.Query(filter, columns, sort);
  237. var display = new Dictionary<string,object?>();
  238. var code = "";
  239. var id = Guid.Empty;
  240. var displaycols = LookupFactory.DefineColumns(_type).ColumnNames();
  241. var row = lookup.Rows.FirstOrDefault();
  242. row ??= lookup.NewRow(true);
  243. code = row[CodeColumn]?.ToString();
  244. id = (Guid)row["ID"];
  245. foreach (var col in displaycols.Where(x => !x.Equals("ID") && !x.Equals(CodeColumn)))
  246. display[col] = row[col];
  247. foreach (var key in OtherColumns.Keys)
  248. if (lookup.Columns.Any(x => x.ColumnName.Equals(key)))
  249. OtherValues[OtherColumns[key]] = row[key];
  250. _value = id;
  251. Editor.Text = code;
  252. Description.Text =
  253. LookupFactory.FormatLookup(_type, display, new[] { CodeColumn });
  254. }
  255. private string GetCodeColumn()
  256. {
  257. var comps = ColumnName.Split('.').ToList();
  258. comps.RemoveAt(comps.Count - 1);
  259. var prefix = string.Format("{0}.", string.Join(".", comps));
  260. var cols = Host.Columns.Where(x => !x.ColumnName.Equals(ColumnName) && x.ColumnName.StartsWith(prefix));
  261. foreach (var col in cols)
  262. {
  263. var editor = Host.GetEditor(col);
  264. if (editor is CodeEditor || editor is UniqueCodeEditor)
  265. return col.ColumnName.Split('.').Last();
  266. }
  267. return "";
  268. }
  269. public override void Configure()
  270. {
  271. if(EditorDefinition is not CodePopupEditor codePopupEditor)
  272. {
  273. throw new Exception("Configuring CodePopupEditor without CodePopupEditor");
  274. }
  275. _type = (EditorDefinition as CodePopupEditor)!.Type;
  276. Host.LoadColumns(ColumnName, OtherColumns);
  277. Host.LoadColumns(ColumnName, codePopupEditor.OtherColumns);
  278. CodeColumn = !string.IsNullOrEmpty(codePopupEditor.CodeColumn) ? codePopupEditor.CodeColumn : GetCodeColumn();
  279. }
  280. public override void SetFocus()
  281. {
  282. Editor.Focus();
  283. }
  284. public override void SetColor(Color color)
  285. {
  286. Editor.Background = new SolidColorBrush(color);
  287. }
  288. }
  289. }