MasterList.xaml.cs 9.9 KB

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