MasterList.xaml.cs 10 KB

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