Improve display and function of character replacement
This commit is contained in:
parent
0cb18f9e1a
commit
b698697256
5 changed files with 272 additions and 249 deletions
|
|
@ -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 };
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue