From 51f9b4f473f69b36e4eec06efe402d813463abfe Mon Sep 17 00:00:00 2001 From: Michael Bucari-Tovo Date: Thu, 23 Jun 2022 20:45:09 -0600 Subject: [PATCH] More character replacement safety --- Source/FileManager/ReplacementCharacters.cs | 5 +- .../Dialogs/EditReplacementChars.Designer.cs | 54 ++++++++++--------- .../Dialogs/EditReplacementChars.cs | 6 +-- 3 files changed, 36 insertions(+), 29 deletions(-) diff --git a/Source/FileManager/ReplacementCharacters.cs b/Source/FileManager/ReplacementCharacters.cs index 7cbdf322..b81d3865 100644 --- a/Source/FileManager/ReplacementCharacters.cs +++ b/Source/FileManager/ReplacementCharacters.cs @@ -7,7 +7,7 @@ using System.Linq; namespace FileManager { - public class Replacement + public class Replacement : ICloneable { public const int FIXED_COUNT = 6; @@ -30,6 +30,8 @@ namespace FileManager Mandatory = mandatory; } + public object Clone() => new Replacement(CharacterToReplace, ReplacementString, Description, Mandatory); + public void Update(char charToReplace, string replacementString, string description) { ReplacementString = replacementString; @@ -53,6 +55,7 @@ namespace FileManager public static Replacement OpenAngleBracket(string replacement) => new('<', replacement, "Open Angle Bracket"); public static Replacement CloseAngleBracket(string replacement) => new('>', replacement, "Close Angle Bracket"); public static Replacement Pipe(string replacement) => new('|', replacement, "Vertical Line"); + } [JsonConverter(typeof(ReplacementCharactersConverter))] diff --git a/Source/LibationWinForms/Dialogs/EditReplacementChars.Designer.cs b/Source/LibationWinForms/Dialogs/EditReplacementChars.Designer.cs index 07bced72..f827c2de 100644 --- a/Source/LibationWinForms/Dialogs/EditReplacementChars.Designer.cs +++ b/Source/LibationWinForms/Dialogs/EditReplacementChars.Designer.cs @@ -29,14 +29,14 @@ private void InitializeComponent() { this.dataGridView1 = new System.Windows.Forms.DataGridView(); + this.charToReplaceCol = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.replacementStringCol = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.descriptionCol = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.defaultsBtn = new System.Windows.Forms.Button(); this.loFiDefaultsBtn = new System.Windows.Forms.Button(); this.saveBtn = new System.Windows.Forms.Button(); this.cancelBtn = new System.Windows.Forms.Button(); this.minDefaultBtn = new System.Windows.Forms.Button(); - this.charToReplaceCol = new System.Windows.Forms.DataGridViewTextBoxColumn(); - this.replacementStringCol = new System.Windows.Forms.DataGridViewTextBoxColumn(); - this.descriptionCol = new System.Windows.Forms.DataGridViewTextBoxColumn(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit(); this.SuspendLayout(); // @@ -54,6 +54,7 @@ this.descriptionCol}); this.dataGridView1.Location = new System.Drawing.Point(12, 12); this.dataGridView1.Name = "dataGridView1"; + this.dataGridView1.RowHeadersWidthSizeMode = System.Windows.Forms.DataGridViewRowHeadersWidthSizeMode.DisableResizing; this.dataGridView1.RowTemplate.Height = 25; this.dataGridView1.Size = new System.Drawing.Size(498, 393); this.dataGridView1.TabIndex = 0; @@ -61,6 +62,31 @@ this.dataGridView1.UserDeletingRow += new System.Windows.Forms.DataGridViewRowCancelEventHandler(this.dataGridView1_UserDeletingRow); this.dataGridView1.Resize += new System.EventHandler(this.dataGridView1_Resize); // + // charToReplaceCol + // + this.charToReplaceCol.HeaderText = "Char to Replace"; + this.charToReplaceCol.MinimumWidth = 70; + this.charToReplaceCol.Name = "charToReplaceCol"; + this.charToReplaceCol.Resizable = System.Windows.Forms.DataGridViewTriState.False; + this.charToReplaceCol.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable; + this.charToReplaceCol.Width = 70; + // + // replacementStringCol + // + this.replacementStringCol.HeaderText = "Replacement Text"; + this.replacementStringCol.MinimumWidth = 85; + this.replacementStringCol.Name = "replacementStringCol"; + this.replacementStringCol.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable; + this.replacementStringCol.Width = 85; + // + // descriptionCol + // + this.descriptionCol.HeaderText = "Description"; + this.descriptionCol.MinimumWidth = 100; + this.descriptionCol.Name = "descriptionCol"; + this.descriptionCol.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable; + this.descriptionCol.Width = 200; + // // defaultsBtn // this.defaultsBtn.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); @@ -116,28 +142,6 @@ this.minDefaultBtn.UseVisualStyleBackColor = true; this.minDefaultBtn.Click += new System.EventHandler(this.minDefaultBtn_Click); // - // charToReplaceCol - // - this.charToReplaceCol.HeaderText = "Char to Replace"; - this.charToReplaceCol.MinimumWidth = 70; - this.charToReplaceCol.Name = "charToReplaceCol"; - this.charToReplaceCol.Resizable = System.Windows.Forms.DataGridViewTriState.False; - this.charToReplaceCol.Width = 70; - // - // replacementStringCol - // - this.replacementStringCol.HeaderText = "Replacement Text"; - this.replacementStringCol.MinimumWidth = 85; - this.replacementStringCol.Name = "replacementStringCol"; - this.replacementStringCol.Width = 85; - // - // descriptionCol - // - this.descriptionCol.HeaderText = "Description"; - this.descriptionCol.MinimumWidth = 100; - this.descriptionCol.Name = "descriptionCol"; - this.descriptionCol.Width = 200; - // // EditReplacementChars // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); diff --git a/Source/LibationWinForms/Dialogs/EditReplacementChars.cs b/Source/LibationWinForms/Dialogs/EditReplacementChars.cs index 5e16550f..88388e4a 100644 --- a/Source/LibationWinForms/Dialogs/EditReplacementChars.cs +++ b/Source/LibationWinForms/Dialogs/EditReplacementChars.cs @@ -29,8 +29,8 @@ namespace LibationWinForms.Dialogs { var r = replacements[i]; - int row = dataGridView1.Rows.Add(r.CharacterToReplace, r.ReplacementString, r.Description); - dataGridView1.Rows[row].Tag = r; + int row = dataGridView1.Rows.Add(r.CharacterToReplace.ToString(), r.ReplacementString, r.Description); + dataGridView1.Rows[row].Tag = r.Clone(); if (r.Mandatory) @@ -90,7 +90,7 @@ namespace LibationWinForms.Dialogs { dataGridView1.Rows[e.RowIndex].ErrorText = $"Only 1 {charToReplaceCol.HeaderText} per entry"; } - else if (e.RowIndex >= Replacement.FIXED_COUNT && + else if (dataGridView1.Rows[e.RowIndex].Tag is Replacement repl && !repl.Mandatory && dataGridView1.Rows .Cast() .Where(r => r.Index != e.RowIndex)