MasterList.xaml.cs 10 KB

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