PropertyInitializerGrid.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. using com.sun.tools.corba.se.idl.toJavaPortable;
  2. using InABox.Core;
  3. using InABox.DynamicGrid;
  4. using InABox.Wpf;
  5. using InABox.WPF;
  6. using PRS.Shared.Events;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows;
  13. using System.Windows.Controls;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. namespace PRS.Shared.Grids.EventEditor;
  17. public class PropertyInitializerEditItem : BaseObject
  18. {
  19. public PropertyInitializer PropertyInitializer { get; set; }
  20. }
  21. public class PropertyInitializerGrid : DynamicItemsListGrid<PropertyInitializerEditItem>
  22. {
  23. private string[] ColumnNames = null!;
  24. private Type? _entityType;
  25. public Type? EntityType
  26. {
  27. get => _entityType;
  28. set
  29. {
  30. if(value != _entityType)
  31. {
  32. Items.Clear();
  33. _entityType = value;
  34. if(value is null)
  35. {
  36. ColumnNames = [];
  37. }
  38. else
  39. {
  40. var properties = DatabaseSchema.Properties(value)
  41. .Where(x => x.IsSerializable)
  42. .Select(x => x.Name)
  43. .ToArray();
  44. Array.Sort(properties);
  45. ColumnNames = properties;
  46. }
  47. Reconfigure();
  48. Refresh(true, true);
  49. }
  50. }
  51. }
  52. public IEventDataModelDefinition DataModelDefinition { get; set; } = null!;
  53. private BitmapImage _delete = InABox.Wpf.Resources.delete.AsBitmapImage();
  54. protected override void Init()
  55. {
  56. base.Init();
  57. ActionColumns.Add(new DynamicTemplateColumn(row =>
  58. {
  59. var item = LoadItem(row);
  60. var button = new Button
  61. {
  62. Content = item.PropertyInitializer.Property.Name,
  63. VerticalAlignment = System.Windows.VerticalAlignment.Stretch,
  64. VerticalContentAlignment = System.Windows.VerticalAlignment.Center,
  65. HorizontalContentAlignment = System.Windows.HorizontalAlignment.Left,
  66. MinWidth = 100,
  67. Padding = new System.Windows.Thickness(5, 0, 5, 0),
  68. Background = new LinearGradientBrush(new GradientStopCollection()
  69. {
  70. new(Color.FromRgb(0xF0, 0xF0, 0xF0), 0.0),
  71. new(Color.FromRgb(0xE5, 0xE5, 0xE5), 1.0),
  72. })
  73. {
  74. StartPoint = new(0, 0),
  75. EndPoint = new(0, 1),
  76. },
  77. BorderBrush = Color.FromRgb(0xAC, 0xAC, 0xAC).ToBrush(),
  78. BorderThickness = new(0),
  79. Tag = row
  80. };
  81. button.Click += PropertyNode_Click;
  82. return button;
  83. })
  84. {
  85. HeaderText = "Property"
  86. });
  87. ActionColumns.Add(new DynamicTemplateColumn(row =>
  88. {
  89. var item = LoadItem(row);
  90. var panel = new DockPanel();
  91. var textBox = new TextBox
  92. {
  93. Background = Colors.LightYellow.ToBrush(),
  94. IsEnabled = false,
  95. BorderThickness = new(0),
  96. VerticalContentAlignment = VerticalAlignment.Center,
  97. HorizontalContentAlignment = HorizontalAlignment.Left,
  98. Padding = new(2, 0, 0, 0),
  99. Text = item.PropertyInitializer.Value
  100. };
  101. var button = new Button
  102. {
  103. Content = "Edit",
  104. Padding = new(5.0),
  105. MinWidth = 30,
  106. Tag = row
  107. };
  108. button.BorderBrush = Colors.Silver.ToBrush();
  109. button.BorderThickness = new Thickness(0.75, 0.0, 0.0, 0.0);
  110. button.Click += Value_Click;
  111. DockPanel.SetDock(button, Dock.Right);
  112. panel.Children.Add(button);
  113. DockPanel.SetDock(textBox, Dock.Left);
  114. panel.Children.Add(textBox);
  115. return panel;
  116. })
  117. {
  118. HeaderText = "Value"
  119. });
  120. ActionColumns.Add(new DynamicImageColumn(_delete, DeleteButton_Click));
  121. }
  122. private bool DeleteButton_Click(CoreRow? row)
  123. {
  124. if (row is null) return false;
  125. if (CanDeleteItems([row]))
  126. if (MessageWindow.ShowYesNo("Are you sure you wish to delete the selected records?", "Confirm Delete"))
  127. {
  128. DeleteItems([row]);
  129. SelectedRows = Array.Empty<CoreRow>();
  130. DoChanged();
  131. return true;
  132. }
  133. return false;
  134. }
  135. #region UI Component
  136. private class UIComponent : DynamicGridGridUIComponent<PropertyInitializerEditItem>
  137. {
  138. protected override Brush? GetCellSelectionBackgroundBrush()
  139. {
  140. return null;
  141. }
  142. }
  143. protected override IDynamicGridUIComponent<PropertyInitializerEditItem> CreateUIComponent()
  144. {
  145. return new UIComponent { Parent = this };
  146. }
  147. #endregion
  148. protected override void DoReconfigure(DynamicGridOptions options)
  149. {
  150. base.DoReconfigure(options);
  151. options.Clear();
  152. options.ReadOnly = EntityType is null;
  153. options.AddRows = true;
  154. }
  155. protected override void DoAdd(bool openEditorOnDirectEdit = false)
  156. {
  157. if (EntityType is null) return;
  158. if(DynamicGridColumnNameSelectorGrid.SelectColumnName(EntityType, ColumnNames, out var value))
  159. {
  160. var property = DatabaseSchema.PropertyStrict(EntityType, value);
  161. CreateItems(() =>
  162. {
  163. return [new()
  164. {
  165. PropertyInitializer = new(property, "")
  166. }];
  167. });
  168. }
  169. }
  170. private void Value_Click(object sender, RoutedEventArgs e)
  171. {
  172. if (sender is not FrameworkElement el || el.Tag is not CoreRow row) return;
  173. if (EntityType is null) return;
  174. var item = LoadItem(row);
  175. var window = new ExpressionEditorWindow(DataModelDefinition.GetVariables().Select(x => x.Name).ToArray())
  176. {
  177. Expression = item.PropertyInitializer.Value
  178. };
  179. if(window.ShowDialog() == true && item.PropertyInitializer.Value != window.Expression)
  180. {
  181. item.PropertyInitializer.Value = window.Expression;
  182. UpdateRow(row, item);
  183. DoChanged();
  184. }
  185. }
  186. private void PropertyNode_Click(object sender, RoutedEventArgs e)
  187. {
  188. if (sender is not FrameworkElement el || el.Tag is not CoreRow row) return;
  189. if (EntityType is null) return;
  190. var item = LoadItem(row);
  191. if(DynamicGridColumnNameSelectorGrid.SelectColumnName(EntityType, ColumnNames, out var value) && value != item.PropertyInitializer.Property.Name)
  192. {
  193. item.PropertyInitializer.Property = DatabaseSchema.PropertyStrict(EntityType, value);
  194. UpdateRow(row, item);
  195. DoChanged();
  196. }
  197. }
  198. }