From aa22f7e1ef2a212775eed092e77877f9d9aaddf2 Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Sun, 10 Jul 2022 11:19:50 +0200 Subject: [PATCH 1/2] Delete function: set the default button to be "no" --- I18N Commander/UI WinForms/Components/SectionTree.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/I18N Commander/UI WinForms/Components/SectionTree.cs b/I18N Commander/UI WinForms/Components/SectionTree.cs index 1ecf74a..76d6952 100644 --- a/I18N Commander/UI WinForms/Components/SectionTree.cs +++ b/I18N Commander/UI WinForms/Components/SectionTree.cs @@ -159,7 +159,7 @@ public partial class SectionTree : UserControl 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) + 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, MessageBoxDefaultButton.Button2) == DialogResult.No) return; // Remove the section from the database: From 2ee393efec2bf9d2d9d5feee814baf8f5f4d617b Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Sun, 10 Jul 2022 11:20:28 +0200 Subject: [PATCH 2/2] Implemented the section's edit function --- I18N Commander/Processor/SectionProcessor.cs | 27 ++++++++++++++ .../Components/SectionTree.Designer.cs | 19 ++++++++++ .../UI WinForms/Components/SectionTree.cs | 33 ++++++++++++++++-- .../UI WinForms/Resources/Icons.Designer.cs | 10 ++++++ .../UI WinForms/Resources/Icons.resx | 3 ++ .../Resources/icons8-rename-512.png | Bin 0 -> 2240 bytes 6 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 I18N Commander/UI WinForms/Resources/icons8-rename-512.png 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 0000000000000000000000000000000000000000..f519913454d580915d8004e74054835b8919aaf6 GIT binary patch literal 2240 zcmai0ZB!Fy79M+6`RK7C#cE~U&Y5me#AK52nIVB0XhO@)j94N*Y6qvO- zL>Vucc4jOgltt4d0>gQEdF;GUHeuGoTt1%PD&It-R+fo+*(Kr8h{q_6 z87FC+Fo7;jG?TE9;1_41Wiy` z1UYknN4A)OA9G~V_hc6 z?kuE!BIHUnBFC+mK^2c1F%!k8&O|W_s-FUi2m@hW%d|BnggkrA)B$m0GD6Ehq3$qd zA*o0tHxoMCz+jYe&1z687DaIR5fLmN+atpi4Pkt7U4A@DGb|p5=}k@~i_1ynau6OL z;YNgTcnF8%R(7+QyP!pB^#Ac)9SsT?519-}z$uzA=ew6YZN3)E@mLn0=LWndIT8jqLx;JO!JSauz=seVn#p#&>3!ylj4rl6<56WGg3v$_94%<`R8IQay zj8S{~+2HBOVAqZNH8cVISnvWj;Wt;5@$;-PSD%?7@XQcljF~ZnnV8vQW`@i?yZJ=` zm{W=~WqGU|0I;IQQqvm}uLw93JFll}OD2kr`>)%gC^oFp-9C8az5H{nJ$8L@^ZoIH zo3dKNyN4ROx?AU#%t^-%1=Q?lYN?!Ga4|GHXb%cf7z6t~w~r z-=3_Xl)@k`yC6xj_2kuGI*Qtci%M5pO8h>0Y1vrYvXk~(ZH}myyxzWJ@qTv!`F`*> zdMA2IHnFW?pxtY7BUzxh+;Ej_f3C;5S(hEFY&yRDPjBtkG~3I%Lz>kqCjOIG-xw`V z8$HNI^^1{_!l~iN(zW{AY2SQX%)Bjp$OJV70M9<}dgcJNbxQ!?nP6#rT&k+#hrymI z848>k?+qVfl?R+luUQeq*Hxj^ssQB&$%dBgO~bY&z99!1!-8S#yK4V0B$pQMta#^i zHFQ(jY5i1_^W(_HH+31*+n4VTTod$C#l23!v9Wb$*)}@pU*6|)vP+Sa{pYBZ8yot{%bFil5q$VY;9!|}u>6Ij7JKXB!wrFH z<9^OHzP+Q+z{f$=D;Mo@c;_xHlwTpEWW&)VJNA4w0mLm~z3R1YV18@D&U>fob)%Et zkB1Ch@7Y&VydcnLrSie@{)@-II!l$YGt<-ly{_)_k)zu`8oP65fvLTI;^f#FN4sTm z(l*>@8!T$Z`kP8iOA7k8DH9iqgWm{fq80D-ixo4*!`=PjpV!n?R;eO6yMl5=KP@im zFT49}XIAHiw?@Q%l0|Cg(d@IAOC??Q?yl@5n>e*w{}#1bE!uyj0U_`^*FLnJ=%|he zZ5^Pw1LqTKjVj)S^;6c0+<%Fj38S+0ZG-dbe(uSzwfv%D{iSAW%RYhJ9$SGnRsDU- zv7-y4$%B2{1?iWFZ$FP#9~dfa53AfD@{=4Iw7v13?S|ybV