Improve display and function of character replacement

This commit is contained in:
Michael Bucari-Tovo 2022-06-21 23:39:24 -06:00
parent 0cb18f9e1a
commit b698697256
5 changed files with 272 additions and 249 deletions

View file

@ -29,18 +29,21 @@
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();
//
// dataGridView1
//
this.dataGridView1.AllowUserToResizeColumns = false;
this.dataGridView1.AllowUserToResizeRows = false;
this.dataGridView1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
@ -52,35 +55,12 @@
this.dataGridView1.Location = new System.Drawing.Point(12, 12);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.RowTemplate.Height = 25;
this.dataGridView1.Size = new System.Drawing.Size(416, 393);
this.dataGridView1.Size = new System.Drawing.Size(498, 393);
this.dataGridView1.TabIndex = 0;
this.dataGridView1.CellEndEdit += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellEndEdit);
this.dataGridView1.CellValidating += new System.Windows.Forms.DataGridViewCellValidatingEventHandler(this.dataGridView1_CellValidating);
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.Width = 70;
//
// replacementStringCol
//
this.replacementStringCol.HeaderText = "Replacement String";
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;
//
// defaultsBtn
//
this.defaultsBtn.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
@ -106,7 +86,7 @@
// saveBtn
//
this.saveBtn.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.saveBtn.Location = new System.Drawing.Point(346, 430);
this.saveBtn.Location = new System.Drawing.Point(428, 430);
this.saveBtn.Name = "saveBtn";
this.saveBtn.Size = new System.Drawing.Size(82, 25);
this.saveBtn.TabIndex = 1;
@ -117,7 +97,7 @@
// cancelBtn
//
this.cancelBtn.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.cancelBtn.Location = new System.Drawing.Point(258, 430);
this.cancelBtn.Location = new System.Drawing.Point(340, 430);
this.cancelBtn.Name = "cancelBtn";
this.cancelBtn.Size = new System.Drawing.Size(82, 25);
this.cancelBtn.TabIndex = 1;
@ -125,11 +105,45 @@
this.cancelBtn.UseVisualStyleBackColor = true;
this.cancelBtn.Click += new System.EventHandler(this.cancelBtn_Click);
//
// minDefaultBtn
//
this.minDefaultBtn.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.minDefaultBtn.Location = new System.Drawing.Point(172, 430);
this.minDefaultBtn.Name = "minDefaultBtn";
this.minDefaultBtn.Size = new System.Drawing.Size(80, 25);
this.minDefaultBtn.TabIndex = 1;
this.minDefaultBtn.Text = "Barebones";
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);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(440, 467);
this.ClientSize = new System.Drawing.Size(522, 467);
this.Controls.Add(this.minDefaultBtn);
this.Controls.Add(this.loFiDefaultsBtn);
this.Controls.Add(this.cancelBtn);
this.Controls.Add(this.saveBtn);
@ -145,12 +159,13 @@
#endregion
private System.Windows.Forms.DataGridView dataGridView1;
private System.Windows.Forms.DataGridViewTextBoxColumn charToReplaceCol;
private System.Windows.Forms.DataGridViewTextBoxColumn replacementStringCol;
private System.Windows.Forms.DataGridViewTextBoxColumn descriptionCol;
private System.Windows.Forms.Button defaultsBtn;
private System.Windows.Forms.Button loFiDefaultsBtn;
private System.Windows.Forms.Button saveBtn;
private System.Windows.Forms.Button cancelBtn;
private System.Windows.Forms.Button minDefaultBtn;
private System.Windows.Forms.DataGridViewTextBoxColumn charToReplaceCol;
private System.Windows.Forms.DataGridViewTextBoxColumn replacementStringCol;
private System.Windows.Forms.DataGridViewTextBoxColumn descriptionCol;
}
}

View file

@ -15,25 +15,30 @@ namespace LibationWinForms.Dialogs
InitializeComponent();
dataGridView1_Resize(this, EventArgs.Empty);
}
public EditReplacementChars(Configuration config) : this()
{
this.config = config;
LoadTable(config.ReplacementCharacters.Replacements);
}
private void LoadTable(List<Replacement> replacements)
private void LoadTable(IReadOnlyList<Replacement> replacements)
{
dataGridView1.Rows.Clear();
foreach (var r in replacements)
for (int i = 0; i < replacements.Count; i++)
{
var r = replacements[i];
int row = dataGridView1.Rows.Add(r.CharacterToReplace, r.ReplacementString, r.Description);
dataGridView1.Rows[row].Tag = r;
if (ReplacementCharacters.Default.Replacements.Any(rep => rep.CharacterToReplace == r.CharacterToReplace))
if (r.Mandatory)
{
r.Mandatory = true;
dataGridView1.Rows[row].Cells[charToReplaceCol.Index].ReadOnly = true;
dataGridView1.Rows[row].Cells[descriptionCol.Index].ReadOnly = true;
dataGridView1.Rows[row].Cells[charToReplaceCol.Index].Style.BackColor = System.Drawing.Color.LightGray;
dataGridView1.Rows[row].Cells[descriptionCol.Index].Style.BackColor = System.Drawing.Color.LightGray;
}
}
}
@ -45,67 +50,14 @@ namespace LibationWinForms.Dialogs
}
private void loFiDefaultsBtn_Click(object sender, EventArgs e)
{
LoadTable(ReplacementCharacters.LoFiDefault.Replacements);
}
=> LoadTable(ReplacementCharacters.LoFiDefault.Replacements);
private void defaultsBtn_Click(object sender, EventArgs e)
{
LoadTable(ReplacementCharacters.Default.Replacements);
}
=> LoadTable(ReplacementCharacters.Default.Replacements);
private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if (e.RowIndex < 0) return;
private void minDefaultBtn_Click(object sender, EventArgs e)
=> LoadTable(ReplacementCharacters.Minimum.Replacements);
var cellValue = e.FormattedValue?.ToString();
if (dataGridView1.Rows[e.RowIndex].Tag is Replacement row && row.Mandatory)
{
if (e.ColumnIndex == replacementStringCol.Index)
{
//Ensure replacement string doesn't contain an illegal character.
var replaceString = cellValue ?? string.Empty;
if (replaceString != string.Empty && replaceString.Any(c => FileUtility.invalidChars.Contains(c)))
{
dataGridView1.Rows[e.RowIndex].ErrorText = $"{replaceString} contains an illegal path character";
e.Cancel = true;
}
}
return;
}
if (e.ColumnIndex == charToReplaceCol.Index)
{
if (cellValue.Length != 1)
{
dataGridView1.Rows[e.RowIndex].ErrorText = "Only 1 character to replace per entry";
e.Cancel = true;
}
else if (
dataGridView1.Rows
.Cast<DataGridViewRow>()
.Where(r => r.Index != e.RowIndex)
.OfType<Replacement>()
.Any(r => r.CharacterToReplace == cellValue[0])
)
{
dataGridView1.Rows[e.RowIndex].ErrorText = $"The {cellValue[0]} character is already being replaced";
e.Cancel = true;
}
}
else if (e.ColumnIndex == descriptionCol.Index || e.ColumnIndex == replacementStringCol.Index)
{
var value = dataGridView1.Rows[e.RowIndex].Cells[charToReplaceCol.Index].Value;
if (value is null || value is string str && string.IsNullOrEmpty(str))
{
dataGridView1.Rows[e.RowIndex].ErrorText = $"You must choose a character to replace";
e.Cancel = true;
}
}
}
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
@ -113,10 +65,14 @@ namespace LibationWinForms.Dialogs
dataGridView1.Rows[e.RowIndex].ErrorText = string.Empty;
var cellValue = dataGridView1.Rows[e.RowIndex].Cells[charToReplaceCol.Index].Value?.ToString();
var charToReplaceStr = dataGridView1.Rows[e.RowIndex].Cells[charToReplaceCol.Index].Value?.ToString();
var replacement = dataGridView1.Rows[e.RowIndex].Cells[replacementStringCol.Index].Value?.ToString() ?? string.Empty;
var description = dataGridView1.Rows[e.RowIndex].Cells[descriptionCol.Index].Value?.ToString() ?? string.Empty;
if (string.IsNullOrEmpty(cellValue) || cellValue.Length > 1)
//Validate the whole row. If it passes all validation, add or update the row's tag.
if (string.IsNullOrEmpty(charToReplaceStr) && replacement == string.Empty && description == string.Empty)
{
//Invalid entry, so delete row
var row = dataGridView1.Rows[e.RowIndex];
if (!row.IsNewRow)
{
@ -126,26 +82,38 @@ namespace LibationWinForms.Dialogs
}));
}
}
else if (string.IsNullOrEmpty(charToReplaceStr))
{
dataGridView1.Rows[e.RowIndex].ErrorText = $"You must choose a character to replace";
}
else if (charToReplaceStr.Length > 1)
{
dataGridView1.Rows[e.RowIndex].ErrorText = $"Only 1 {charToReplaceCol.HeaderText} per entry";
}
else if (e.RowIndex >= Replacement.FIXED_COUNT &&
dataGridView1.Rows
.Cast<DataGridViewRow>()
.Where(r => r.Index != e.RowIndex)
.Select(r => r.Tag)
.OfType<Replacement>()
.Any(r => r.CharacterToReplace == charToReplaceStr[0])
)
{
dataGridView1.Rows[e.RowIndex].ErrorText = $"The {charToReplaceStr[0]} character is already being replaced";
}
else if (ReplacementCharacters.ContainsInvalid(replacement))
{
dataGridView1.Rows[e.RowIndex].ErrorText = $"Your {replacementStringCol.HeaderText} contains illegal characters";
}
else
{
char charToReplace = cellValue[0];
string description = dataGridView1.Rows[e.RowIndex].Cells[descriptionCol.Index].Value?.ToString() ?? string.Empty;
string replacement = dataGridView1.Rows[e.RowIndex].Cells[replacementStringCol.Index].Value?.ToString() ?? string.Empty;
//valid entry. Add or update Replacement in row's Tag
var charToReplace = charToReplaceStr[0];
var mandatory = false;
if (dataGridView1.Rows[e.RowIndex].Tag is Replacement existing)
{
mandatory = existing.Mandatory;
}
dataGridView1.Rows[e.RowIndex].Tag =
new Replacement()
{
CharacterToReplace = charToReplace,
ReplacementString = replacement,
Description = description,
Mandatory = mandatory
};
existing.Update(charToReplace, replacement, description);
else
dataGridView1.Rows[e.RowIndex].Tag = new Replacement(charToReplace, replacement, description);
}
}
@ -155,7 +123,6 @@ namespace LibationWinForms.Dialogs
.Cast<DataGridViewRow>()
.Select(r => r.Tag)
.OfType<Replacement>()
.Where(r => r.ReplacementString != null && (r.ReplacementString == string.Empty || !r.ReplacementString.Any(c => FileUtility.invalidChars.Contains(c))))
.ToList();
config.ReplacementCharacters = new ReplacementCharacters { Replacements = replacements };

View file

@ -15,7 +15,8 @@ namespace LibationWinForms.Dialogs
private void editCharreplacementBtn_Click(object sender, EventArgs e)
{
var form = new EditReplacementChars(config);
form.ShowDialog();
form.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
form.ShowDialog(this);
}
private void Load_DownloadDecrypt(Configuration config)