CodePopupEditorControl.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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.Silver);
  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.Gray);
  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.WhiteSmoke),
  79. BorderBrush = new SolidColorBrush(Colors.Silver)
  80. };
  81. Description.SetValue(Grid.ColumnProperty, 2);
  82. Description.SetValue(Grid.RowProperty, 0);
  83. Grid.Children.Add(Description);
  84. return Grid;
  85. }
  86. private void Editor_GotFocus(object sender, RoutedEventArgs e)
  87. {
  88. origvalue = Editor.Text;
  89. }
  90. private void Editor_LostFocus(object sender, RoutedEventArgs e)
  91. {
  92. if (Editor.Text.Equals(origvalue))
  93. return;
  94. var id = _value;
  95. LookupValue(CodeColumn, Editor.Text);
  96. if (id == Guid.Empty)
  97. CheckChanged();
  98. }
  99. private void Editor_PreviewKeyDown(object sender, KeyEventArgs e)
  100. {
  101. if (e.Key.Equals(Key.Enter) || e.Key.Equals(Key.Tab))
  102. {
  103. if (string.IsNullOrEmpty(Editor.Text))
  104. {
  105. if (!Editor.Text.Equals(origvalue))
  106. {
  107. LookupValue("ID", Guid.Empty);
  108. CheckChanged();
  109. }
  110. Editor.MoveFocus(new TraversalRequest(Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift)
  111. ? FocusNavigationDirection.Previous
  112. : FocusNavigationDirection.Next));
  113. }
  114. else
  115. {
  116. if (!Editor.Text.Equals(origvalue))
  117. {
  118. var code = Editor.Text;
  119. LookupValue(CodeColumn, code);
  120. if (_value != Guid.Empty)
  121. {
  122. CheckChanged();
  123. Editor.MoveFocus(new TraversalRequest(Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift)
  124. ? FocusNavigationDirection.Previous
  125. : FocusNavigationDirection.Next));
  126. }
  127. else
  128. {
  129. if (DoSearch(code))
  130. Editor.MoveFocus(new TraversalRequest(Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift)
  131. ? FocusNavigationDirection.Previous
  132. : FocusNavigationDirection.Next));
  133. }
  134. }
  135. else
  136. {
  137. Editor.MoveFocus(new TraversalRequest(Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift)
  138. ? FocusNavigationDirection.Previous
  139. : FocusNavigationDirection.Next));
  140. }
  141. }
  142. e.Handled = true;
  143. }
  144. }
  145. private void Editor_PreviewKeyUp(object sender, KeyEventArgs e)
  146. {
  147. }
  148. private void Editor_KeyUp(object sender, KeyEventArgs e)
  149. {
  150. }
  151. private void Select_Click(object sender, RoutedEventArgs e)
  152. {
  153. DoSearch(Editor.Text);
  154. }
  155. private bool DoSearch(string code)
  156. {
  157. var result = false;
  158. if (_type != null)
  159. {
  160. var columns = LookupFactory.DefineColumns(_type);
  161. var cols = OtherColumns.Keys.ToList();
  162. foreach (var column in columns.ColumnNames())
  163. if (!cols.Contains(column))
  164. cols.Add(column);
  165. var list = new PopupList(_type, _value, cols.ToArray(), new Dictionary<string, string> { { CodeColumn, code } });
  166. list.OnDefineFilter += t => { return Host.DefineFilter(t); };
  167. if (list.ShowDialog() == true)
  168. {
  169. result = true;
  170. _value = list.ID;
  171. foreach (var key in OtherColumns.Keys)
  172. OtherValues[OtherColumns[key]] = list.OtherValues[key];
  173. CheckChanged();
  174. Editor.Text = string.Format("{0}", list.OtherValues[CodeColumn]);
  175. var display = new Dictionary<string, object?>();
  176. foreach (var key in list.OtherValues.Keys.Where(x => !string.Equals(x, CodeColumn)))
  177. display[key] = list.OtherValues[key];
  178. Description.Text =
  179. LookupFactory.FormatLookup(_type, display,
  180. new[] { CodeColumn });
  181. }
  182. }
  183. return result;
  184. }
  185. //private void Clear_Click(object sender, RoutedEventArgs e)
  186. //{
  187. // if (_type != null)
  188. // {
  189. // _value = Guid.Empty;
  190. // foreach (var otherfield in OtherColumns.Keys)
  191. // OtherValues[OtherColumns[otherfield]] = null;
  192. // CheckChanged();
  193. // Editor.Text = "";
  194. // }
  195. //}
  196. public override int DesiredHeight()
  197. {
  198. return 25;
  199. }
  200. public override int DesiredWidth()
  201. {
  202. return int.MaxValue;
  203. }
  204. protected override Guid RetrieveValue()
  205. {
  206. return _value;
  207. }
  208. protected override void UpdateValue(Guid value)
  209. {
  210. var oldvalue = _value;
  211. _value = value;
  212. if (oldvalue != value)
  213. {
  214. if (Equals(value, Guid.Empty))
  215. SetEmptyValue();
  216. else
  217. LookupValue("ID", value);
  218. }
  219. }
  220. private void SetEmptyValue()
  221. {
  222. Editor.Text = "";
  223. Description.Text = "";
  224. }
  225. private void LookupValue(string column, object value)
  226. {
  227. var client = ClientFactory.CreateClient(_type);
  228. var columns = LookupFactory.DefineColumns(_type);
  229. var cols = columns.ColumnNames();
  230. foreach (var key in OtherColumns.Where(x => !cols.Contains(x.Key)))
  231. columns.Add(key.Key);
  232. var sort = LookupFactory.DefineSort(_type);
  233. var filter = Activator.CreateInstance(typeof(Filter<>).MakeGenericType(_type));
  234. CoreUtils.SetPropertyValue(filter, "Expression", CoreUtils.GetMemberExpression(_type, column));
  235. CoreUtils.SetPropertyValue(filter, "Operator", Operator.IsEqualTo);
  236. CoreUtils.SetPropertyValue(filter, "Value", value);
  237. var lookup = client.Query(filter, columns, sort);
  238. var display = new Dictionary<string,object?>();
  239. var code = "";
  240. var id = Guid.Empty;
  241. var displaycols = LookupFactory.DefineColumns(_type).ColumnNames();
  242. var row = lookup.Rows.FirstOrDefault();
  243. row ??= lookup.NewRow(true);
  244. code = row[CodeColumn]?.ToString();
  245. id = (Guid)row["ID"];
  246. foreach (var col in displaycols.Where(x => !x.Equals("ID") && !x.Equals(CodeColumn)))
  247. display[col] = row[col];
  248. foreach (var key in OtherColumns.Keys)
  249. if (lookup.Columns.Any(x => x.ColumnName.Equals(key)))
  250. OtherValues[OtherColumns[key]] = row[key];
  251. _value = id;
  252. Editor.Text = code;
  253. Description.Text =
  254. LookupFactory.FormatLookup(_type, display, new[] { CodeColumn });
  255. }
  256. private string GetCodeColumn(Type type)
  257. {
  258. var prop = DatabaseSchema.Properties(type)
  259. .Where(x => x.Editor is BaseCodeEditor && !x.HasParentEntityLink())
  260. .FirstOrDefault() ?? throw new Exception($"No code property for {type.EntityName()}");
  261. return prop.Name;
  262. }
  263. public override void Configure()
  264. {
  265. if(EditorDefinition is not CodePopupEditor codePopupEditor)
  266. {
  267. throw new Exception("Configuring CodePopupEditor without CodePopupEditor");
  268. }
  269. _type = (EditorDefinition as CodePopupEditor)!.Type;
  270. Host.LoadColumns(ColumnName, OtherColumns);
  271. Host.LoadColumns(ColumnName, codePopupEditor.OtherColumns);
  272. CodeColumn = !string.IsNullOrEmpty(codePopupEditor.CodeColumn) ? codePopupEditor.CodeColumn : GetCodeColumn(_type);
  273. }
  274. public override void SetFocus()
  275. {
  276. Editor.Focus();
  277. }
  278. public override void SetColor(Color color)
  279. {
  280. Editor.Background = IsEnabled ? new SolidColorBrush(color) : new SolidColorBrush(Colors.WhiteSmoke);
  281. }
  282. }
  283. }