CodePopupEditorControl.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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>
  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 string CodeColumn { get; set; }
  24. protected override FrameworkElement CreateEditor()
  25. {
  26. var Grid = new Grid
  27. {
  28. VerticalAlignment = VerticalAlignment.Stretch,
  29. HorizontalAlignment = HorizontalAlignment.Stretch
  30. };
  31. Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(131, GridUnitType.Pixel) });
  32. Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(19, GridUnitType.Pixel) });
  33. Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
  34. Editor = new TextBox
  35. {
  36. CharacterCasing = CharacterCasing.Upper,
  37. VerticalAlignment = VerticalAlignment.Stretch,
  38. VerticalContentAlignment = VerticalAlignment.Center,
  39. HorizontalAlignment = HorizontalAlignment.Stretch
  40. };
  41. Editor.BorderThickness = new Thickness(1,1,0,1);
  42. Editor.BorderBrush = new SolidColorBrush(Colors.Silver);
  43. Editor.SetValue(Grid.ColumnProperty, 0);
  44. Editor.SetValue(Grid.RowProperty, 0);
  45. Editor.PreviewKeyDown += Editor_PreviewKeyDown;
  46. Editor.GotFocus += Editor_GotFocus;
  47. Editor.AcceptsTab = true;
  48. Editor.LostFocus += Editor_LostFocus;
  49. Grid.Children.Add(Editor);
  50. var Select = new Button
  51. {
  52. VerticalAlignment = VerticalAlignment.Stretch,
  53. VerticalContentAlignment = VerticalAlignment.Center,
  54. HorizontalAlignment = HorizontalAlignment.Stretch,
  55. Content = "..",
  56. Focusable = false
  57. };
  58. Select.BorderThickness = new Thickness(1);
  59. Select.BorderBrush = new SolidColorBrush(Colors.DimGray);
  60. Select.SetValue(Grid.ColumnProperty, 1);
  61. Select.SetValue(Grid.RowProperty, 0);
  62. Select.Click += Select_Click;
  63. Grid.Children.Add(Select);
  64. Description = new TextBox
  65. {
  66. VerticalAlignment = VerticalAlignment.Stretch,
  67. VerticalContentAlignment = VerticalAlignment.Center,
  68. HorizontalAlignment = HorizontalAlignment.Stretch,
  69. Margin = new Thickness(5, 0, 0, 0),
  70. IsReadOnly = true,
  71. Focusable = false,
  72. Background = new SolidColorBrush(Colors.WhiteSmoke),
  73. BorderBrush = new SolidColorBrush(Colors.Silver)
  74. };
  75. Description.SetValue(Grid.ColumnProperty, 2);
  76. Description.SetValue(Grid.RowProperty, 0);
  77. Grid.Children.Add(Description);
  78. return Grid;
  79. }
  80. private void Editor_GotFocus(object sender, RoutedEventArgs e)
  81. {
  82. origvalue = Editor.Text;
  83. }
  84. private void Editor_LostFocus(object sender, RoutedEventArgs e)
  85. {
  86. if (Editor.Text.Equals(origvalue))
  87. return;
  88. var id = _value;
  89. LookupValue(CodeColumn, Editor.Text);
  90. if (id == Guid.Empty)
  91. CheckChanged();
  92. }
  93. private void Editor_PreviewKeyDown(object sender, KeyEventArgs e)
  94. {
  95. if (e.Key.Equals(Key.Enter) || e.Key.Equals(Key.Tab))
  96. {
  97. if (string.IsNullOrEmpty(Editor.Text))
  98. {
  99. if (!Editor.Text.Equals(origvalue))
  100. {
  101. LookupValue("ID", Guid.Empty);
  102. CheckChanged();
  103. }
  104. Editor.MoveFocus(new TraversalRequest(Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift)
  105. ? FocusNavigationDirection.Previous
  106. : FocusNavigationDirection.Next));
  107. }
  108. else
  109. {
  110. if (!Editor.Text.Equals(origvalue))
  111. {
  112. var code = Editor.Text;
  113. LookupValue(CodeColumn, code);
  114. if (_value != Guid.Empty)
  115. {
  116. CheckChanged();
  117. Editor.MoveFocus(new TraversalRequest(Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift)
  118. ? FocusNavigationDirection.Previous
  119. : FocusNavigationDirection.Next));
  120. }
  121. else
  122. {
  123. if (DoSearch(code))
  124. Editor.MoveFocus(new TraversalRequest(Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift)
  125. ? FocusNavigationDirection.Previous
  126. : FocusNavigationDirection.Next));
  127. }
  128. }
  129. else
  130. {
  131. Editor.MoveFocus(new TraversalRequest(Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift)
  132. ? FocusNavigationDirection.Previous
  133. : FocusNavigationDirection.Next));
  134. }
  135. }
  136. e.Handled = true;
  137. }
  138. }
  139. private void Editor_PreviewKeyUp(object sender, KeyEventArgs e)
  140. {
  141. }
  142. private void Editor_KeyUp(object sender, KeyEventArgs e)
  143. {
  144. }
  145. private void Select_Click(object sender, RoutedEventArgs e)
  146. {
  147. DoSearch(Editor.Text);
  148. }
  149. private bool DoSearch(string code)
  150. {
  151. var result = false;
  152. if (_type != null)
  153. {
  154. var columns = LookupFactory.DefineLookupColumns(Host.GetEditorType(), _type, ColumnName);
  155. var list = new PopupList(_type, _value,
  156. columns.ColumnNames().Concat(CoreUtils.One(CodeColumn)).ToArray(),
  157. new Dictionary<string, string> { { CodeColumn, code } });
  158. list.OnDefineFilter += t => LookupFactory.DefineLookupFilter(Host.GetEditorType(), t, ColumnName, Host.GetItems());
  159. list.OnCustomiseGrid += grid =>
  160. {
  161. if (EditorDefinition.CanAdd)
  162. {
  163. grid.OnDoubleClick += (o, args) => args.Handled = true;
  164. grid.OnReconfigure += options =>
  165. {
  166. options.AddRows = Security.CanEdit(_type);
  167. options.EditRows = Security.CanEdit(_type);
  168. options.EditRows = Security.CanDelete(_type);
  169. options.ReadOnly = false;
  170. };
  171. grid.OnCreateItem += (_, item) =>
  172. {
  173. LookupFactory.OnCreateItem(Host.GetEditorType(), ColumnName, Host.GetItems(), item);
  174. EditorDefinition.OnCreateItem?.Invoke(item);
  175. };
  176. grid.Reconfigure();
  177. }
  178. };
  179. if (list.ShowDialog() == true)
  180. {
  181. result = true;
  182. _value = list.ID;
  183. foreach (var col in columns)
  184. OtherValues[col.Property] = list.OtherValues[col.Property];
  185. CheckChanged();
  186. Editor.Text = string.Format("{0}", list.OtherValues[CodeColumn]);
  187. var display = new Dictionary<string, object?>();
  188. foreach (var key in list.OtherValues.Keys.Where(x => !string.Equals(x, CodeColumn)))
  189. display[key] = list.OtherValues[key];
  190. Description.Text =
  191. LookupFactory.FormatLookup(_type, display, new[] { CodeColumn });
  192. }
  193. }
  194. return result;
  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. CheckChanged();
  219. }
  220. }
  221. private void SetEmptyValue()
  222. {
  223. Editor.Text = "";
  224. Description.Text = "";
  225. }
  226. private void LookupValue(string column, object value)
  227. {
  228. var client = ClientFactory.CreateClient(_type);
  229. var columns = LookupFactory.DefineLookupColumns(Host.GetEditorType(), _type, ColumnName);
  230. columns.Add(CodeColumn);
  231. var sort = LookupFactory.DefineSort(_type);
  232. var filter = Filter.Create(_type, column).IsEqualTo(value);
  233. var lookup = client.Query(filter, columns, sort);
  234. var display = new Dictionary<string,object?>();
  235. var code = "";
  236. var id = Guid.Empty;
  237. var displaycols = LookupFactory.DefineLookupColumns(Host.GetEditorType(), _type, ColumnName).ColumnNames();
  238. var row = lookup.Rows.FirstOrDefault();
  239. row ??= lookup.NewRow(true);
  240. code = row[CodeColumn]?.ToString();
  241. id = row.Get<Guid>("ID");
  242. foreach (var col in displaycols.Where(x => !x.Equals("ID") && !x.Equals(CodeColumn)))
  243. display[col] = row[col];
  244. foreach(var col in columns)
  245. {
  246. OtherValues[col.Property] = row[col.Property];
  247. }
  248. _value = id;
  249. Editor.Text = code;
  250. Description.Text = LookupFactory.FormatLookup(_type, display, new[] { CodeColumn });
  251. }
  252. private string GetCodeColumn(Type type)
  253. {
  254. var prop = DatabaseSchema.Properties(type)
  255. .Where(x => x.Editor is UniqueCodeEditor && !x.HasParentEntityLink())
  256. .FirstOrDefault() ?? throw new Exception($"No code property for {type.EntityName()}");
  257. return prop.Name;
  258. }
  259. public override void Configure()
  260. {
  261. _type = EditorDefinition.Type;
  262. CodeColumn = !string.IsNullOrEmpty(EditorDefinition.CodeColumn) ? EditorDefinition.CodeColumn : GetCodeColumn(_type);
  263. }
  264. public override void SetFocus()
  265. {
  266. Editor.Focus();
  267. }
  268. public override void SetColor(Color color)
  269. {
  270. Editor.Background = IsEnabled ? new SolidColorBrush(color) : new SolidColorBrush(Colors.WhiteSmoke);
  271. }
  272. }