LocalityTree.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using System;
  2. using System.Linq;
  3. using System.Threading;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using InABox.Clients;
  7. using InABox.Core;
  8. using InABox.DynamicGrid;
  9. using InABox.Wpf;
  10. using InABox.WPF;
  11. using Microsoft.Win32;
  12. using Microsoft.Xaml.Behaviors.Core;
  13. using ContextMenu = Fluent.ContextMenu;
  14. namespace PRSDesktop;
  15. public class LocalityTree : DynamicDataGrid<LocalitySummary>
  16. {
  17. protected override void Init()
  18. {
  19. base.Init();
  20. HiddenColumns.Add(x=>x.Type);
  21. ContextMenu = new ContextMenu();
  22. ContextMenu.Items.Add(new Separator());
  23. AddButton("Import", PRSDesktop.Resources.download.AsBitmapImage(), ImportFile);
  24. }
  25. protected override void DoReconfigure(DynamicGridOptions options)
  26. {
  27. base.DoReconfigure(options);
  28. options.AddRows = true;
  29. options.EditRows = true;
  30. options.DeleteRows = true;
  31. options.FilterRows = true;
  32. }
  33. protected override IDynamicGridUIComponent<LocalitySummary> CreateUIComponent()
  34. {
  35. return new DynamicGridTreeUIComponent<LocalitySummary, Guid>(x => x.ID, x => x.ParentID, Guid.Empty) { Parent = this};
  36. }
  37. #region Editing
  38. private void CreateCountry()
  39. {
  40. var country = new Country();
  41. Edit(country);
  42. }
  43. private void CreateState(Guid country)
  44. {
  45. var state = new State();
  46. state.Country.ID = country;
  47. Edit(state);
  48. }
  49. private void CreateLocality(Guid state)
  50. {
  51. var locality = new Locality();
  52. locality.State.ID = state;
  53. Edit(locality);
  54. }
  55. private void Edit<T>(T item) where T : Entity, IRemotable, IPersistent, new()
  56. {
  57. var result = new DynamicDataGrid<T>().EditItems([item]);
  58. if (result)
  59. Refresh(false,true);
  60. }
  61. private void Delete<T>(Guid id) where T : Entity, IRemotable, IPersistent, new()
  62. {
  63. if (MessageWindow.ShowYesNo($"Are you sure you wish to delete this {typeof(T).EntityName().Split('.').Last()}?", "Confirm Delete"))
  64. {
  65. Progress.ShowModal("Deleteing Items", progress =>
  66. {
  67. var item = new T() { ID = id };
  68. Client.Delete([item], "Deleted from Post Codes Setup Screen");
  69. });
  70. Refresh(false, true);
  71. }
  72. }
  73. protected override void DoAdd(bool openEditorOnDirectEdit = false)
  74. {
  75. var row = SelectedRows.FirstOrDefault();
  76. if (row == null)
  77. CreateCountry();
  78. else if (row.Get<LocalitySummary, LocalityType>(x => x.Type) == LocalityType.Country)
  79. {
  80. var menu = new ContextMenu();
  81. menu.Items.Add(new MenuItem()
  82. {
  83. Header = "Create State",
  84. Command = new ActionCommand(() => CreateState(row.Get<LocalitySummary, Guid>(x => x.ID)))
  85. });
  86. menu.Items.Add(new Separator());
  87. menu.Items.Add(new MenuItem()
  88. {
  89. Header = "Create Country",
  90. Command = new ActionCommand(CreateCountry)
  91. });
  92. menu.IsOpen = true;
  93. }
  94. else if (row.Get<LocalitySummary, LocalityType>(x => x.Type) == LocalityType.State)
  95. {
  96. var menu = new ContextMenu();
  97. menu.Items.Add(new MenuItem()
  98. {
  99. Header = "Create Locality",
  100. Command = new ActionCommand(() => CreateLocality(row.Get<LocalitySummary, Guid>(x => x.ID)))
  101. });
  102. menu.Items.Add(new Separator());
  103. menu.Items.Add(new MenuItem()
  104. {
  105. Header = "Create State",
  106. Command = new ActionCommand(() => CreateState(row.Get<LocalitySummary, Guid>(x => x.ParentID)))
  107. });
  108. menu.Items.Add(new MenuItem()
  109. {
  110. Header = "Create Country",
  111. Command = new ActionCommand(CreateCountry)
  112. });
  113. menu.IsOpen = true;
  114. }
  115. else
  116. CreateLocality(row.Get<LocalitySummary, Guid>(x => x.ParentID));
  117. }
  118. protected override void DoEdit()
  119. {
  120. var row = SelectedRows.FirstOrDefault();
  121. if (row == null)
  122. return;
  123. if (row.Get<LocalitySummary, LocalityType>(x => x.Type) == LocalityType.Country)
  124. Edit(row.ToObject<Country>());
  125. else if (row.Get<LocalitySummary, LocalityType>(x => x.Type) == LocalityType.State)
  126. {
  127. var state = row.ToObject<State>();
  128. state.Country.ID = row.Get<LocalitySummary, Guid>(x => x.ParentID);
  129. state.CommitChanges();
  130. Edit(state);
  131. }
  132. else
  133. {
  134. var locality = row.ToObject<Locality>();
  135. locality.State.ID = row.Get<LocalitySummary, Guid>(x => x.ParentID);
  136. locality.CommitChanges();
  137. Edit(locality);
  138. }
  139. }
  140. protected override void DoDelete()
  141. {
  142. var row = SelectedRows.FirstOrDefault();
  143. if (row == null)
  144. return;
  145. if (row.Get<LocalitySummary, LocalityType>(x => x.Type) == LocalityType.Country)
  146. Delete<Country>(row.Get<LocalitySummary, Guid>(x => x.ID));
  147. else if (row.Get<LocalitySummary, LocalityType>(x => x.Type) == LocalityType.State)
  148. Delete<State>(row.Get<LocalitySummary, Guid>(x => x.ID));
  149. else
  150. Delete<Locality>(row.Get<LocalitySummary, Guid>(x => x.ID));
  151. }
  152. private bool ImportFile(Button button, CoreRow[] rows)
  153. {
  154. var grid = new DynamicDataGrid<Locality>();
  155. grid.Import();
  156. return true;
  157. }
  158. #endregion
  159. }