diff --git a/I18N Commander/Processor/SectionProcessor.cs b/I18N Commander/Processor/SectionProcessor.cs index be52f29..a008a78 100644 --- a/I18N Commander/Processor/SectionProcessor.cs +++ b/I18N Commander/Processor/SectionProcessor.cs @@ -108,4 +108,31 @@ public static class SectionProcessor return await db.Sections.CountAsync(n => n.Parent == section); } + + public static async Task
RenameSection(DataContext db, string selectedNodeKey, string alteredName) + { + // Read the section from the database: + var section = await db.Sections.FirstOrDefaultAsync(n => n.DataKey == selectedNodeKey); + if (section is null) + throw new ArgumentException($"The section with key {selectedNodeKey} does not exist in the database."); + + // Determine the new key: + var newKey = string.Join('_', alteredName.Split(' ', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries)).ToUpperInvariant(); + + // Check, if this key already exists: + if (await db.Sections.AnyAsync(n => n.DataKey == newKey)) + { + var rng = new Random(); + while (await db.Sections.AnyAsync(n => n.DataKey == newKey)) + { + // Add a random number to the end of the key: + newKey += $"_{rng.Next(1, 10_000)}"; + } + } + + section.Name = alteredName; + section.DataKey = newKey; + await db.SaveChangesAsync(); + return 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 39d41b2..ebd2639 100644 --- a/I18N Commander/UI WinForms/Components/SectionTree.Designer.cs +++ b/I18N Commander/UI WinForms/Components/SectionTree.Designer.cs @@ -33,6 +33,7 @@ this.flowLayoutBottom = new System.Windows.Forms.FlowLayoutPanel(); this.buttonAdd = new System.Windows.Forms.Button(); this.buttonRemove = new System.Windows.Forms.Button(); + this.buttonRename = new System.Windows.Forms.Button(); this.treeView = new System.Windows.Forms.TreeView(); this.toolTip = new System.Windows.Forms.ToolTip(this.components); this.tableLayout.SuspendLayout(); @@ -59,6 +60,7 @@ // this.flowLayoutBottom.Controls.Add(this.buttonAdd); this.flowLayoutBottom.Controls.Add(this.buttonRemove); + this.flowLayoutBottom.Controls.Add(this.buttonRename); this.flowLayoutBottom.Dock = System.Windows.Forms.DockStyle.Fill; this.flowLayoutBottom.Location = new System.Drawing.Point(0, 445); this.flowLayoutBottom.Margin = new System.Windows.Forms.Padding(0); @@ -97,6 +99,22 @@ this.buttonRemove.UseVisualStyleBackColor = true; this.buttonRemove.Click += new System.EventHandler(this.buttonRemove_Click); // + // buttonRename + // + this.buttonRename.AutoSize = true; + this.buttonRename.Enabled = false; + this.buttonRename.FlatAppearance.BorderSize = 0; + this.buttonRename.FlatStyle = System.Windows.Forms.FlatStyle.Flat; + this.buttonRename.Image = global::UI_WinForms.Resources.Icons.icons8_rename_512; + this.buttonRename.Location = new System.Drawing.Point(135, 3); + this.buttonRename.Name = "buttonRename"; + this.buttonRename.Size = new System.Drawing.Size(60, 60); + this.buttonRename.TabIndex = 2; + this.buttonRename.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageBeforeText; + this.toolTip.SetToolTip(this.buttonRename, "Rename the selected section"); + this.buttonRename.UseVisualStyleBackColor = true; + this.buttonRename.Click += new System.EventHandler(this.buttonRename_Click); + // // treeView // this.treeView.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; @@ -140,5 +158,6 @@ private Button buttonRemove; private TreeView treeView; private ToolTip toolTip; + private Button buttonRename; } } diff --git a/I18N Commander/UI WinForms/Components/SectionTree.cs b/I18N Commander/UI WinForms/Components/SectionTree.cs index 76d6952..a3479b3 100644 --- a/I18N Commander/UI WinForms/Components/SectionTree.cs +++ b/I18N Commander/UI WinForms/Components/SectionTree.cs @@ -178,7 +178,36 @@ public partial class SectionTree : UserControl // 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; + // If the selected node is not null, enable the remove & edit button: + this.buttonRename.Enabled = this.buttonRemove.Enabled = selectedNode is not null; + } + + private async void buttonRename_Click(object sender, EventArgs e) + { + // Ask the user if he really wants to rename the section: + if(MessageBox.Show("Are you sure, you want to rename the selected section? If you are already using this section in your code, you will need to manually refactor your code after renaming it.", "Rename section", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.No) + return; + + // Get the currently selected section: + var selectedNode = this.treeView.SelectedNode; + + // Ask the user for the new name: + var result = InputDialog.Show(new InputDialog.Options( + Message: "Please edit the section name.", + PreloadedText: selectedNode.Text, + ShowQuestionCheckbox: false, + Title: "Rename section" + )); + + // If the user canceled, return: + if(result.DialogResult == DialogResult.Cancel) + return; + + // Rename the section: + var alteredSection = await SectionProcessor.RenameSection(this.db, selectedNode.Name, result.Text); + + // Rename the selected node: + selectedNode.Text = alteredSection.Name; + selectedNode.Name = alteredSection.DataKey; // [sic] name is the key } } \ No newline at end of file diff --git a/I18N Commander/UI WinForms/Resources/Icons.Designer.cs b/I18N Commander/UI WinForms/Resources/Icons.Designer.cs index 8d0147b..a97cc64 100644 --- a/I18N Commander/UI WinForms/Resources/Icons.Designer.cs +++ b/I18N Commander/UI WinForms/Resources/Icons.Designer.cs @@ -159,5 +159,15 @@ namespace UI_WinForms.Resources { return ((System.Drawing.Bitmap)(obj)); } } + + /// + /// Looks up a localized resource of type System.Drawing.Bitmap. + /// + internal static System.Drawing.Bitmap icons8_rename_512 { + get { + object obj = ResourceManager.GetObject("icons8_rename_512", resourceCulture); + return ((System.Drawing.Bitmap)(obj)); + } + } } } diff --git a/I18N Commander/UI WinForms/Resources/Icons.resx b/I18N Commander/UI WinForms/Resources/Icons.resx index 4fb8808..0894a59 100644 --- a/I18N Commander/UI WinForms/Resources/Icons.resx +++ b/I18N Commander/UI WinForms/Resources/Icons.resx @@ -148,4 +148,7 @@ icons8-open-file-under-cursor-512.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a + + icons8-rename-512.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a + \ No newline at end of file diff --git a/I18N Commander/UI WinForms/Resources/icons8-rename-512.png b/I18N Commander/UI WinForms/Resources/icons8-rename-512.png new file mode 100644 index 0000000..f519913 Binary files /dev/null and b/I18N Commander/UI WinForms/Resources/icons8-rename-512.png differ