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 { 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 CreateUIComponent() { return new DynamicGridTreeUIComponent(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 item) where T : Entity, IRemotable, IPersistent, new() { var result = new DynamicDataGrid().EditItems([item]); if (result) Refresh(false,true); } private void Delete(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(x => x.Type) == LocalityType.Country) { var menu = new ContextMenu(); menu.Items.Add(new MenuItem() { Header = "Create State", Command = new ActionCommand(() => CreateState(row.Get(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(x => x.Type) == LocalityType.State) { var menu = new ContextMenu(); menu.Items.Add(new MenuItem() { Header = "Create Locality", Command = new ActionCommand(() => CreateLocality(row.Get(x => x.ID))) }); menu.Items.Add(new Separator()); menu.Items.Add(new MenuItem() { Header = "Create State", Command = new ActionCommand(() => CreateState(row.Get(x => x.ParentID))) }); menu.Items.Add(new MenuItem() { Header = "Create Country", Command = new ActionCommand(CreateCountry) }); menu.IsOpen = true; } else CreateLocality(row.Get(x => x.ParentID)); } protected override void DoEdit() { var row = SelectedRows.FirstOrDefault(); if (row == null) return; if (row.Get(x => x.Type) == LocalityType.Country) Edit(row.ToObject()); else if (row.Get(x => x.Type) == LocalityType.State) { var state = row.ToObject(); state.Country.ID = row.Get(x => x.ParentID); state.CommitChanges(); Edit(state); } else { var locality = row.ToObject(); locality.State.ID = row.Get(x => x.ParentID); locality.CommitChanges(); Edit(locality); } } protected override void DoDelete() { var row = SelectedRows.FirstOrDefault(); if (row == null) return; if (row.Get(x => x.Type) == LocalityType.Country) Delete(row.Get(x => x.ID)); else if (row.Get(x => x.Type) == LocalityType.State) Delete(row.Get(x => x.ID)); else Delete(row.Get(x => x.ID)); } private bool ImportFile(Button button, CoreRow[] rows) { var grid = new DynamicDataGrid(); grid.Import(); return true; } #endregion }