MasterList.xaml.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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.Media.Imaging;
  7. using InABox.Core;
  8. using InABox.WPF;
  9. using InABox.Wpf;
  10. namespace InABox.DynamicGrid
  11. {
  12. /// <summary>
  13. /// Interaction logic for MasterList.xaml
  14. /// </summary>
  15. public partial class MasterList : ThemableWindow
  16. {
  17. private bool AllItems;
  18. private readonly bool AllowAllItems = true;
  19. private bool bLoaded;
  20. private readonly IDynamicGrid grid;
  21. /// <summary>
  22. ///
  23. /// </summary>
  24. /// <param name="type"></param>
  25. /// <param name="groupby"></param>
  26. /// <param name="selectedgroup"></param>
  27. /// <param name="allowallitems">Show an "All Items" entry at tge top of the Groups List Box</param>
  28. /// <param name="dataGrid">If null, finds a DynamicDataGrid of the specified entity type.
  29. /// Otherwise, MasterList uses this parameter as the DynamicGrid type</param>
  30. public MasterList(Type type, string? groupby = null, string? selectedgroup = null, bool allowallitems = false, Type? dataGrid = null,
  31. IDynamicGrid? dynamicGrid = null)
  32. {
  33. InitializeComponent();
  34. Type = type;
  35. AllowAllItems = allowallitems;
  36. SelectedGroup = selectedgroup;
  37. CurrentGroup = selectedgroup;
  38. GroupBy = groupby;
  39. if(dynamicGrid is null)
  40. {
  41. if(dataGrid == null)
  42. {
  43. dataGrid = DynamicGridUtils.FindDynamicGrid(typeof(DynamicDataGrid<>), type);
  44. }
  45. grid = (Activator.CreateInstance(dataGrid) as IDynamicGrid) ?? throw new Exception($"Invalid Grid Type {dataGrid}");
  46. }
  47. else
  48. {
  49. grid = dynamicGrid;
  50. }
  51. grid.Margin = new Thickness(5, 5, 5, 5);
  52. ((DependencyObject)grid).SetValue(Grid.ColumnProperty, 1);
  53. ((DependencyObject)grid).SetValue(Grid.RowProperty, 0);
  54. layoutGrid.Children.Add((UIElement)grid);
  55. DataModelType = typeof(AutoDataModel<>).MakeGenericType(type);
  56. grid.Reconfigure(options =>
  57. {
  58. options.SelectColumns = true;
  59. options.FilterRows = true;
  60. options.ShowHelp = true;
  61. if (DataModelType != null)
  62. options.Print = true;
  63. });
  64. if (!string.IsNullOrWhiteSpace(GroupBy))
  65. grid.AddHiddenColumn(GroupBy);
  66. grid.OnPrintData += PrintData;
  67. grid.AfterRefresh += Grid_AfterReload;
  68. grid.OnFilterRecord += Grid_OnFilterRecord;
  69. grid.OnCreateItem += Grid_OnCreateItem;
  70. grid.Refresh(true, true);
  71. }
  72. public Type Type { get; set; }
  73. private Type DataModelType { get; }
  74. public object? CurrentGroup { get; set; }
  75. public string? GroupBy { get; set; }
  76. public string? SelectedGroup { get; set; }
  77. public List<Tuple<string, object?, BitmapImage>> GroupList { get; private set; }
  78. private void Grid_AfterReload(object sender, AfterRefreshEventArgs args)
  79. {
  80. if (bLoaded)
  81. return;
  82. if (!string.IsNullOrEmpty(GroupBy))
  83. {
  84. CoreTable lookups = null;
  85. var col = grid.MasterColumns.First(x => x.ColumnName.Equals(GroupBy)).Editor as ILookupEditor;
  86. if (col != null)
  87. lookups = col.Values(GroupBy);
  88. //grid.ConfigureColumns(grid.MasterColumns, true);
  89. //grid.AddHiddenColumn(GroupBy);
  90. GroupList = new List<Tuple<string, object?, BitmapImage>>();
  91. if (AllowAllItems)
  92. GroupList.Add(new Tuple<string, object?, BitmapImage>("All Items", null, Wpf.Resources.doc_misc.AsBitmapImage()));
  93. var column = grid.MasterColumns.Where(x => x.ColumnName.Equals(GroupBy)).FirstOrDefault();
  94. if (column != null)
  95. foreach (var row in grid.Data.Rows)
  96. {
  97. var entry = row[GroupBy];
  98. string display = "Unassigned";
  99. if (entry != null)
  100. {
  101. display = entry.ToString()!;
  102. if (lookups != null)
  103. {
  104. var lookup = lookups.Rows.FirstOrDefault(r => r[GroupBy] != null && r[GroupBy].Equals(entry));
  105. if (lookup != null)
  106. display = lookup.Get<string>("Display");
  107. }
  108. }
  109. if (!GroupList.Any(x => (x.Item1 == null && display == "") || (x.Item1 != null && x.Item1.Equals(display))))
  110. GroupList.Add(new Tuple<string, object?, BitmapImage>(entry != null ? display : "Unassigned", entry,
  111. Wpf.Resources.doc_misc.AsBitmapImage()));
  112. }
  113. //foreach (var key in column.Lookups.Keys)
  114. // GroupList.Add(new Tuple<string, string, BitmapImage>(column.Lookups[key].ToString(), key.ToString(), Wpf.Resources.edit.AsBitmapImage()));
  115. if (!string.IsNullOrWhiteSpace(SelectedGroup))
  116. if (!GroupList.Any(x => Equals(x.Item2, SelectedGroup)))
  117. GroupList.Add(new Tuple<string, object?, BitmapImage>(SelectedGroup, SelectedGroup,
  118. Wpf.Resources.doc_misc.AsBitmapImage()));
  119. GroupList = GroupList.OrderBy(x => (x.Item1 == "All Items" ? "0" : x.Item1 == "Unassigned" ? "1" : "2") + x.Item1).ToList();
  120. Groups.ItemsSource = GroupList;
  121. if (string.IsNullOrEmpty(SelectedGroup))
  122. {
  123. CurrentGroup = "";
  124. Groups.Visibility = Visibility.Visible;
  125. }
  126. else
  127. {
  128. CurrentGroup = SelectedGroup;
  129. Groups.Visibility = Visibility.Collapsed;
  130. }
  131. }
  132. else
  133. {
  134. Groups.Visibility = Visibility.Collapsed;
  135. }
  136. //grid.Refresh(true, true);
  137. bLoaded = true;
  138. var item = GroupList != null
  139. ? GroupList.FirstOrDefault(x => (SelectedGroup == null && x.Item2 == null) || (x.Item2 != null && x.Item2.Equals(SelectedGroup)))
  140. : null;
  141. if (!AllowAllItems && item == null)
  142. item = GroupList?.FirstOrDefault();
  143. AllItems = AllowAllItems && item == null;
  144. Groups.SelectedIndex = item != null ? GroupList.IndexOf(item) : 0;
  145. Title = Type.Name + " Master List" + (item == null ? "" : " - " + item.Item1);
  146. Groups.Focus();
  147. }
  148. private void PrintData(object sender)
  149. {
  150. var filtertype = typeof(Filter<>).MakeGenericType(Type);
  151. var filter = Activator.CreateInstance(filtertype, "ID") as IFilter;
  152. filter.Property = "ID";
  153. filter.Operator = Operator.InList;
  154. filter.Value = grid.SelectedRows.Select(r => r.Get<Guid>("ID")).ToArray();
  155. var model = Activator.CreateInstance(DataModelType, filter) as DataModel;
  156. }
  157. private void Grid_OnCreateItem(object sender, object item)
  158. {
  159. if (!string.IsNullOrEmpty(GroupBy) && CurrentGroup != null)
  160. CoreUtils.SetPropertyValue(item, GroupBy, CurrentGroup);
  161. }
  162. private bool Grid_OnFilterRecord(CoreRow row)
  163. {
  164. if (AllItems)
  165. return true;
  166. var result = GroupBy == null || CurrentGroup == null || Equals(CurrentGroup, row[GroupBy]);
  167. return result; // ((CurrentGroup == null) && (row[GroupBy] == null)) || ((CurrentGroup != null) && (CurrentGroup.Equals(row[GroupBy])));
  168. }
  169. private void Groups_SelectionChanged(object sender, SelectionChangedEventArgs e)
  170. {
  171. if (!bLoaded)
  172. return;
  173. var newCurrentGroup = CurrentGroup;
  174. if (e.AddedItems.Count == 0 || Groups.SelectedIndex == 0)
  175. {
  176. AllItems = AllowAllItems;
  177. newCurrentGroup = GroupList.Any() ? GroupList.First().Item2 : null;
  178. }
  179. else
  180. {
  181. AllItems = false;
  182. var selected = (Tuple<string, object, BitmapImage>)e.AddedItems[0];
  183. newCurrentGroup = selected.Item2;
  184. }
  185. if (!Equals(newCurrentGroup, CurrentGroup))
  186. {
  187. CurrentGroup = newCurrentGroup;
  188. grid.Refresh(false, false);
  189. }
  190. }
  191. private void Window_Loaded(object sender, RoutedEventArgs e)
  192. {
  193. var center = new Point(Left + Width / 2, Top + Height / 2);
  194. var screen = WpfScreen.GetScreenFrom(this);
  195. var maxwidth = (int)screen.WorkingArea.Width - 200;
  196. ((FrameworkElement)grid).Width = Math.Min(Math.Max(grid.DesiredWidth(),450),maxwidth);
  197. // var DesiredWidth = Math.Max(600, grid.DesiredWidth());
  198. // if (DesiredWidth > maxwidth)
  199. // DesiredWidth = maxwidth;
  200. // if (DesiredWidth > Width)
  201. // Width = DesiredWidth;
  202. // if (!string.IsNullOrEmpty(GroupBy))
  203. // Width += Groups.DesiredSize.Width + 45.0F;
  204. //
  205. // var maxheight = (int)screen.WorkingArea.Height - 200;
  206. // var DesiredHeight = (int)(Width * 0.75);
  207. // if (DesiredHeight > maxheight)
  208. // DesiredHeight = maxheight;
  209. // if (DesiredHeight > Height)
  210. // Height = DesiredHeight;
  211. Left = center.X - Width / 2;
  212. Top = center.Y - Height / 2;
  213. }
  214. }
  215. }