|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics.Contracts; |
| 4 | +using System.Linq; |
| 5 | +using System.Windows.Forms; |
| 6 | +using ReClassNET.Project; |
| 7 | + |
| 8 | +namespace ReClassNET.Forms |
| 9 | +{ |
| 10 | + public partial class EnumEditorForm : IconForm |
| 11 | + { |
| 12 | + private readonly EnumMetaData enumMetaData; |
| 13 | + |
| 14 | + public EnumEditorForm(EnumMetaData enumMetaData) |
| 15 | + { |
| 16 | + Contract.Requires(enumMetaData != null); |
| 17 | + |
| 18 | + InitializeComponent(); |
| 19 | + |
| 20 | + this.enumMetaData = enumMetaData; |
| 21 | + |
| 22 | + enumNameTextBox.Text = enumMetaData.Name; |
| 23 | + enumFlagCheckBox.Checked = enumMetaData.UseFlagsMode; |
| 24 | + |
| 25 | + foreach (var kv in enumMetaData.Values) |
| 26 | + { |
| 27 | + enumDataGridView.Rows.Add(kv.Key, kv.Value); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + private void enumDataGridView_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e) |
| 32 | + { |
| 33 | + long value = e.Row.Index; |
| 34 | + if (enumFlagCheckBox.Checked) |
| 35 | + { |
| 36 | + value = (long)Math.Pow(2, e.Row.Index); |
| 37 | + } |
| 38 | + |
| 39 | + e.Row.Cells[0].Value = value; |
| 40 | + } |
| 41 | + |
| 42 | + private void saveButton_Click(object sender, EventArgs e) |
| 43 | + { |
| 44 | + enumMetaData.Name = enumNameTextBox.Text; |
| 45 | + |
| 46 | + var values = new Dictionary<long, string>(); |
| 47 | + |
| 48 | + foreach (var row in enumDataGridView.Rows.Cast<DataGridViewRow>().Where(r => r.IsNewRow == false)) |
| 49 | + { |
| 50 | + if (!long.TryParse(Convert.ToString(row.Cells[0].Value), out var valueKey)) |
| 51 | + { |
| 52 | + continue; |
| 53 | + } |
| 54 | + |
| 55 | + var valueName = Convert.ToString(row.Cells[1].Value); |
| 56 | + |
| 57 | + values.Add(valueKey, valueName); |
| 58 | + } |
| 59 | + |
| 60 | + enumMetaData.SetData(enumFlagCheckBox.Checked, 4, values); |
| 61 | + } |
| 62 | + |
| 63 | + private void enumDataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) |
| 64 | + { |
| 65 | + void SetErrorText(string text) |
| 66 | + { |
| 67 | + enumDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText = text; |
| 68 | + } |
| 69 | + |
| 70 | + SetErrorText(null); |
| 71 | + |
| 72 | + var formattedValue = Convert.ToString(e.FormattedValue); |
| 73 | + |
| 74 | + if (e.ColumnIndex == 0 && !long.TryParse(formattedValue, out _)) |
| 75 | + { |
| 76 | + SetErrorText($"'{formattedValue}' is not a valid value."); |
| 77 | + } |
| 78 | + else if (e.ColumnIndex == 1 && string.IsNullOrWhiteSpace(formattedValue)) |
| 79 | + { |
| 80 | + SetErrorText("Empty names are not allowed."); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments