|
@@ -0,0 +1,179 @@
|
|
|
+using System;
|
|
|
+using System.Linq;
|
|
|
+using System.Threading;
|
|
|
+using System.Windows;
|
|
|
+using System.Windows.Controls;
|
|
|
+using InABox.Clients;
|
|
|
+using InABox.Core;
|
|
|
+using InABox.DynamicGrid;
|
|
|
+using InABox.Wpf;
|
|
|
+using InABox.WPF;
|
|
|
+using Microsoft.Win32;
|
|
|
+using Microsoft.Xaml.Behaviors.Core;
|
|
|
+using ContextMenu = Fluent.ContextMenu;
|
|
|
+
|
|
|
+namespace PRSDesktop;
|
|
|
+
|
|
|
+public class LocalityTree : DynamicDataGrid<LocalitySummary>
|
|
|
+{
|
|
|
+ protected override void Init()
|
|
|
+ {
|
|
|
+ base.Init();
|
|
|
+ HiddenColumns.Add(x=>x.Type);
|
|
|
+ ContextMenu = new ContextMenu();
|
|
|
+ ContextMenu.Items.Add(new Separator());
|
|
|
+ AddButton("Import", PRSDesktop.Resources.download.AsBitmapImage(), ImportFile);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ protected override void DoReconfigure(DynamicGridOptions options)
|
|
|
+ {
|
|
|
+ base.DoReconfigure(options);
|
|
|
+ options.AddRows = true;
|
|
|
+ options.EditRows = true;
|
|
|
+ options.DeleteRows = true;
|
|
|
+ options.FilterRows = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ protected override IDynamicGridUIComponent<LocalitySummary> CreateUIComponent()
|
|
|
+ {
|
|
|
+ return new DynamicGridTreeUIComponent<LocalitySummary, Guid>(x => x.ID, x => x.ParentID, Guid.Empty) { Parent = this};
|
|
|
+ }
|
|
|
+
|
|
|
+ #region Editing
|
|
|
+
|
|
|
+ private void CreateCountry()
|
|
|
+ {
|
|
|
+ var country = new Country();
|
|
|
+ Edit(country);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void CreateState(Guid country)
|
|
|
+ {
|
|
|
+ var state = new State();
|
|
|
+ state.Country.ID = country;
|
|
|
+ Edit(state);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void CreateLocality(Guid state)
|
|
|
+ {
|
|
|
+ var locality = new Locality();
|
|
|
+ locality.State.ID = state;
|
|
|
+ Edit(locality);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void Edit<T>(T item) where T : Entity, IRemotable, IPersistent, new()
|
|
|
+ {
|
|
|
+ var result = new DynamicDataGrid<T>().EditItems([item]);
|
|
|
+ if (result)
|
|
|
+ Refresh(false,true);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void Delete<T>(Guid id) where T : Entity, IRemotable, IPersistent, new()
|
|
|
+ {
|
|
|
+ if (MessageWindow.ShowYesNo($"Are you sure you wish to delete this {typeof(T).EntityName().Split('.').Last()}?", "Confirm Delete"))
|
|
|
+ {
|
|
|
+ Progress.ShowModal("Deleteing Items", progress =>
|
|
|
+ {
|
|
|
+ var item = new T() { ID = id };
|
|
|
+ Client.Delete([item], "Deleted from Post Codes Setup Screen");
|
|
|
+ });
|
|
|
+ Refresh(false, true);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ protected override void DoAdd(bool openEditorOnDirectEdit = false)
|
|
|
+ {
|
|
|
+ var row = SelectedRows.FirstOrDefault();
|
|
|
+ if (row == null)
|
|
|
+ CreateCountry();
|
|
|
+ else if (row.Get<LocalitySummary, LocalityType>(x => x.Type) == LocalityType.Country)
|
|
|
+ {
|
|
|
+ var menu = new ContextMenu();
|
|
|
+ menu.Items.Add(new MenuItem()
|
|
|
+ {
|
|
|
+ Header = "Create State",
|
|
|
+ Command = new ActionCommand(() => CreateState(row.Get<LocalitySummary, Guid>(x => x.ID)))
|
|
|
+ });
|
|
|
+ menu.Items.Add(new Separator());
|
|
|
+ menu.Items.Add(new MenuItem()
|
|
|
+ {
|
|
|
+ Header = "Create Country",
|
|
|
+ Command = new ActionCommand(CreateCountry)
|
|
|
+ });
|
|
|
+ menu.IsOpen = true;
|
|
|
+
|
|
|
+ }
|
|
|
+ else if (row.Get<LocalitySummary, LocalityType>(x => x.Type) == LocalityType.State)
|
|
|
+ {
|
|
|
+ var menu = new ContextMenu();
|
|
|
+ menu.Items.Add(new MenuItem()
|
|
|
+ {
|
|
|
+ Header = "Create Locality",
|
|
|
+ Command = new ActionCommand(() => CreateLocality(row.Get<LocalitySummary, Guid>(x => x.ID)))
|
|
|
+ });
|
|
|
+ menu.Items.Add(new Separator());
|
|
|
+ menu.Items.Add(new MenuItem()
|
|
|
+ {
|
|
|
+ Header = "Create State",
|
|
|
+ Command = new ActionCommand(() => CreateState(row.Get<LocalitySummary, Guid>(x => x.ParentID)))
|
|
|
+ });
|
|
|
+ menu.Items.Add(new MenuItem()
|
|
|
+ {
|
|
|
+ Header = "Create Country",
|
|
|
+ Command = new ActionCommand(CreateCountry)
|
|
|
+ });
|
|
|
+ menu.IsOpen = true;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ CreateLocality(row.Get<LocalitySummary, Guid>(x => x.ParentID));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ protected override void DoEdit()
|
|
|
+ {
|
|
|
+ var row = SelectedRows.FirstOrDefault();
|
|
|
+ if (row == null)
|
|
|
+ return;
|
|
|
+ if (row.Get<LocalitySummary, LocalityType>(x => x.Type) == LocalityType.Country)
|
|
|
+ Edit(row.ToObject<Country>());
|
|
|
+ else if (row.Get<LocalitySummary, LocalityType>(x => x.Type) == LocalityType.State)
|
|
|
+ {
|
|
|
+ var state = row.ToObject<State>();
|
|
|
+ state.Country.ID = row.Get<LocalitySummary, Guid>(x => x.ParentID);
|
|
|
+ state.CommitChanges();
|
|
|
+ Edit(state);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ var locality = row.ToObject<Locality>();
|
|
|
+ locality.State.ID = row.Get<LocalitySummary, Guid>(x => x.ParentID);
|
|
|
+ locality.CommitChanges();
|
|
|
+ Edit(locality);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ protected override void DoDelete()
|
|
|
+ {
|
|
|
+ var row = SelectedRows.FirstOrDefault();
|
|
|
+ if (row == null)
|
|
|
+ return;
|
|
|
+ if (row.Get<LocalitySummary, LocalityType>(x => x.Type) == LocalityType.Country)
|
|
|
+ Delete<Country>(row.Get<LocalitySummary, Guid>(x => x.ID));
|
|
|
+ else if (row.Get<LocalitySummary, LocalityType>(x => x.Type) == LocalityType.State)
|
|
|
+ Delete<State>(row.Get<LocalitySummary, Guid>(x => x.ID));
|
|
|
+ else
|
|
|
+ Delete<Locality>(row.Get<LocalitySummary, Guid>(x => x.ID));
|
|
|
+ }
|
|
|
+
|
|
|
+ private bool ImportFile(Button button, CoreRow[] rows)
|
|
|
+ {
|
|
|
+ var grid = new DynamicDataGrid<Locality>();
|
|
|
+ grid.Import();
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ #endregion
|
|
|
+
|
|
|
+}
|