diff --git a/I18N Commander/Processor/SectionProcessor.cs b/I18N Commander/Processor/SectionProcessor.cs index bb8f74f..be52f29 100644 --- a/I18N Commander/Processor/SectionProcessor.cs +++ b/I18N Commander/Processor/SectionProcessor.cs @@ -82,4 +82,30 @@ public static class SectionProcessor await db.SaveChangesAsync(); return section; } + + public static async Task RemoveSection(DataContext db, string selectedKey) + { + // Remove the section from the database: + var section2Delete = await db.Sections.FirstOrDefaultAsync(n => n.DataKey == selectedKey); + if (section2Delete is null) + throw new ArgumentException($"The section with key {selectedKey} does not exist in the database."); + + // Next, remove all children of the section, and the children's children, etc.: + var children = await db.Sections.Where(n => n.Parent == section2Delete).ToListAsync(); + foreach (var child in children) + await RemoveSection(db, child.DataKey); + + db.Sections.Remove(section2Delete); + await db.SaveChangesAsync(); + } + + public static async Task NumberChildren(DataContext db, string selectedKey) + { + // Read the section from the database: + var section = await db.Sections.FirstOrDefaultAsync(n => n.DataKey == selectedKey); + if (section is null) + throw new ArgumentException($"The section with key {selectedKey} does not exist in the database."); + + return await db.Sections.CountAsync(n => n.Parent == section); + } } \ No newline at end of file diff --git a/I18N Commander/UI WinForms/Components/SectionTree.Designer.cs b/I18N Commander/UI WinForms/Components/SectionTree.Designer.cs index 9229662..9cf2bb7 100644 --- a/I18N Commander/UI WinForms/Components/SectionTree.Designer.cs +++ b/I18N Commander/UI WinForms/Components/SectionTree.Designer.cs @@ -91,6 +91,7 @@ this.buttonRemove.Text = "Remove"; this.buttonRemove.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageBeforeText; this.buttonRemove.UseVisualStyleBackColor = true; + this.buttonRemove.Click += new System.EventHandler(this.buttonRemove_Click); // // treeView // @@ -102,6 +103,7 @@ this.treeView.Name = "treeView"; this.treeView.Size = new System.Drawing.Size(290, 439); this.treeView.TabIndex = 1; + this.treeView.NodeMouseClick += new System.Windows.Forms.TreeNodeMouseClickEventHandler(this.treeView_NodeMouseClick); // // SectionTree // diff --git a/I18N Commander/UI WinForms/Components/SectionTree.cs b/I18N Commander/UI WinForms/Components/SectionTree.cs index 81c0df4..85308b7 100644 --- a/I18N Commander/UI WinForms/Components/SectionTree.cs +++ b/I18N Commander/UI WinForms/Components/SectionTree.cs @@ -31,10 +31,10 @@ public partial class SectionTree : UserControl this.treeView.ImageList = imgList; // Subscribe to the load event: - this.Load += this.OnLoad; + this.Load += this.LoadNodes; } - private async void OnLoad(object? sender, EventArgs e) + private async void LoadNodes(object? sender, EventArgs e) { // A dictionary to cache all known tree nodes: var treeNodes = new Dictionary(); @@ -146,4 +146,37 @@ public partial class SectionTree : UserControl node.EnsureVisible(); this.treeView.SelectedNode = node; } + + private async void buttonRemove_Click(object sender, EventArgs e) + { + // Get the currently selected section, which will be removed: + var selectedNode = this.treeView.SelectedNode; + + // Get the number of children: + // (notice, that the node's name is its key) + var numberChildren = await SectionProcessor.NumberChildren(this.db, selectedNode.Name); + + // Ask the user, if he really wants to remove the section: + if(MessageBox.Show(numberChildren > 0 ? $"Are you sure, you want to remove the section '{selectedNode.Text}', its {numberChildren} children and so on?" : $"Are you sure, you want to remove the section '{selectedNode.Text}'?", "Remove section", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + return; + + // Remove the section from the database: + // (notice, that the node's name is its key) + await SectionProcessor.RemoveSection(this.db, selectedNode.Name); + + // Remove all nodes from the tree control: + this.treeView.Nodes.Clear(); + + // Reload the tree: + this.LoadNodes(this, EventArgs.Empty); + } + + private void treeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) + { + // Get the currently selected section: + var selectedNode = this.treeView.SelectedNode; + + // If the selected node is not null, enable the remove button: + this.buttonRemove.Enabled = selectedNode is not null; + } } \ No newline at end of file