PopupEditorControl.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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.Media;
  8. using InABox.Clients;
  9. using InABox.Core;
  10. namespace InABox.DynamicGrid
  11. {
  12. public class PopupEditorControl : DynamicEditorControl<Guid, PopupEditor>, IPopupEditorControl
  13. {
  14. static PopupEditorControl()
  15. {
  16. //DynamicEditorControlFactory.Register<PopupEditorControl, PopupEditor>();
  17. }
  18. private Type? _type;
  19. private Guid _value = Guid.Empty;
  20. private TextBox Editor;
  21. //public event DefineFilter OnDefineFilter;
  22. public PopupEditorControl()
  23. {
  24. OtherColumns = new Dictionary<string, string>();
  25. }
  26. public Dictionary<string, string> OtherColumns { get; }
  27. public event OnDefineFilter? OnDefineFilter;
  28. public override void Configure()
  29. {
  30. _type = (EditorDefinition as PopupEditor)?.Type;
  31. Host.LoadColumns(ColumnName, OtherColumns);
  32. if (EditorDefinition is DataLookupEditor dataLookup)
  33. Host.LoadColumns(ColumnName, dataLookup.OtherColumns);
  34. }
  35. protected override FrameworkElement CreateEditor()
  36. {
  37. var Grid = new Grid
  38. {
  39. VerticalAlignment = VerticalAlignment.Stretch,
  40. HorizontalAlignment = HorizontalAlignment.Stretch
  41. };
  42. Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
  43. Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(30, GridUnitType.Pixel) });
  44. Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(50, GridUnitType.Pixel) });
  45. Editor = new TextBox
  46. {
  47. VerticalAlignment = VerticalAlignment.Stretch,
  48. VerticalContentAlignment = VerticalAlignment.Center,
  49. HorizontalAlignment = HorizontalAlignment.Stretch,
  50. IsReadOnly = true
  51. };
  52. Editor.SetValue(Grid.ColumnProperty, 0);
  53. Editor.SetValue(Grid.RowProperty, 0);
  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. Margin = new Thickness(5, 0, 0, 0),
  62. Focusable = false
  63. };
  64. Select.SetValue(Grid.ColumnProperty, 1);
  65. Select.SetValue(Grid.RowProperty, 0);
  66. Select.Click += Select_Click;
  67. Grid.Children.Add(Select);
  68. var Clear = new Button
  69. {
  70. VerticalAlignment = VerticalAlignment.Stretch,
  71. VerticalContentAlignment = VerticalAlignment.Center,
  72. HorizontalAlignment = HorizontalAlignment.Stretch,
  73. Content = "Clear",
  74. Margin = new Thickness(5, 0, 0, 0),
  75. Focusable = false
  76. };
  77. Clear.SetValue(Grid.ColumnProperty, 2);
  78. Clear.SetValue(Grid.RowProperty, 0);
  79. Clear.Click += Clear_Click;
  80. Grid.Children.Add(Clear);
  81. return Grid;
  82. }
  83. private void Select_Click(object sender, RoutedEventArgs e)
  84. {
  85. if (_type != null)
  86. {
  87. var list = new PopupList(_type, _value, OtherColumns.Keys.ToArray());
  88. list.OnDefineFilter += t => { return Host.DefineFilter(t); };
  89. if (list.ShowDialog() == true)
  90. {
  91. _value = list.ID;
  92. //Dictionary<String, String> fieldmap = new Dictionary<String, String>();
  93. foreach (var key in OtherColumns.Keys)
  94. //String[] comps = othercolumn.Split(new String[] { "->" }, StringSplitOptions.None);
  95. //String field = comps.Last();
  96. //fieldmap[comps.First()] = field;
  97. OtherValues[OtherColumns[key]] = list.OtherValues[key];
  98. //fieldmap[OtherColumns[key]] = list.OtherValues[othercolumn]
  99. CheckChanged();
  100. var display = new List<string?>();
  101. //var columns = Entity.DefaultLookupColumns(_type) as IColumns;
  102. var columns = LookupFactory.DefineColumns(_type);
  103. foreach (var column in columns.ColumnNames())
  104. if (list.OtherValues.ContainsKey(column))
  105. display.Add(list.OtherValues[column]?.ToString());
  106. Editor.Text = string.Join(" / ", display.Where(x => x != null && !string.IsNullOrWhiteSpace(x.ToString())));
  107. }
  108. }
  109. }
  110. private void Clear_Click(object sender, RoutedEventArgs e)
  111. {
  112. if (_type != null)
  113. {
  114. _value = Guid.Empty;
  115. foreach (var otherfield in OtherColumns.Keys)
  116. OtherValues[OtherColumns[otherfield]] = null;
  117. CheckChanged();
  118. Editor.Text = "";
  119. }
  120. }
  121. public override int DesiredHeight()
  122. {
  123. return 25;
  124. }
  125. public override int DesiredWidth()
  126. {
  127. return int.MaxValue;
  128. }
  129. protected override Guid RetrieveValue()
  130. {
  131. return _value;
  132. }
  133. protected override void UpdateValue(Guid value)
  134. {
  135. _value = value;
  136. if (_type is null)
  137. {
  138. Logger.Send(LogType.Error, "", "PopupEditorControl.UpdateValue(): _type is null!");
  139. return;
  140. }
  141. var client = ClientFactory.CreateClient(_type);
  142. var columns = LookupFactory.DefineColumns(_type);
  143. var sort = LookupFactory.DefineSort(_type);
  144. var filter = Filter.Create(_type);
  145. filter.Expression = CoreUtils.GetMemberExpression(_type, "ID");
  146. filter.Operator = Operator.IsEqualTo;
  147. filter.Value = value;
  148. var lookup = client.Query(filter, columns, sort);
  149. var display = new List<object?>();
  150. if (lookup.Rows.Any())
  151. foreach (var col in lookup.Columns.Where(x => !x.ColumnName.Equals("ID")))
  152. display.Add(lookup.Rows.First()[col.ColumnName]);
  153. Editor.Text = string.Join(" / ", display.Where(x => x != null && !string.IsNullOrWhiteSpace(x.ToString())));
  154. }
  155. public override void SetFocus()
  156. {
  157. Editor.Focus();
  158. }
  159. public override void SetColor(Color color)
  160. {
  161. Editor.Background = new SolidColorBrush(color);
  162. }
  163. }
  164. }