using System; using System.Linq; using System.Reflection; using System.Windows.Forms; using FirmwareHeaderBuilder.Controller; using FirmwareHeaderBuilder.Model; using FirmwareHeaderBuilder.Model.ModelNaming; namespace FirmwareHeaderBuilder { public partial class Form1 : Form { private HeaderBuilder headerBuilder; private OpenFileDialog openFileDialog; public Form1() { InitializeComponent(); InitializeMyComponent(); InitializeUI(); } private void OptImageType_SelectedIndexChanged(object sender, EventArgs e) { ImageType imgType = ImageType.Parse((ushort)optImageType.SelectedIndex); headerBuilder.SelectImageType(imgType.Index); openFileDialog.FilterIndex = imgType.Filter.Index + 1; // Base 1?? } private void BtnBrowse_Click(object sender, EventArgs e) { if (openFileDialog.ShowDialog() == DialogResult.OK) { lbFilePath.Text = openFileDialog.FileName; if (headerBuilder.CheckHeaderExist(lbFilePath.Text)) { if (MessageBox.Show("已存在檔頭資訊,合併後將被複寫,是否繼續進行?", "載入", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK) { ErrorCode errCode = headerBuilder.ImportImage(lbFilePath.Text, true); PostErrorCode(errCode); } else { headerBuilder.ClearImage(); lbFilePath.Text = string.Empty; } } else { ErrorCode errCode = headerBuilder.ImportImage(lbFilePath.Text); PostErrorCode(errCode); } } } private void BtnMerge_Click(object sender, EventArgs e) { ErrorCode errCode = headerBuilder.InputModelName(txtModelName.Text); if (errCode == ErrorCode.NoError) { errCode = headerBuilder.Build(); PostErrorCode(errCode); } else { PostErrorCode(errCode); } } private void InitializeMyComponent() { headerBuilder = new HeaderBuilder(); optType.SelectedIndexChanged += ModuleNaming_SelectedIndexChanged; optInputConfiguration.SelectedIndexChanged += ModuleNaming_SelectedIndexChanged; optSafetyRegulation.SelectedIndexChanged += ModuleNaming_SelectedIndexChanged; //optRatedPower.SelectedIndexChanged += ModuleNaming_SelectedIndexChanged; txtRatedPower.TextChanged += ModuleNaming_SelectedIndexChanged; optConnector1.SelectedIndexChanged += ModuleNaming_SelectedIndexChanged; optConnector2.SelectedIndexChanged += ModuleNaming_SelectedIndexChanged; optConnector3.SelectedIndexChanged += ModuleNaming_SelectedIndexChanged; optNetworkOption.SelectedIndexChanged += ModuleNaming_SelectedIndexChanged; optGeneration.SelectedIndexChanged += ModuleNaming_SelectedIndexChanged; txtCustomization.TextChanged += ModuleNaming_SelectedIndexChanged; optImageType.SelectedIndexChanged += OptImageType_SelectedIndexChanged; optImageType.Items.Clear(); optImageType.Items.AddRange(ImageType.ListText.ToArray()); btnBrowse.Click += BtnBrowse_Click; btnMerge.Click += BtnMerge_Click; openFileDialog = new OpenFileDialog(); openFileDialog.Filter = OpenDialogFilter.GetFilterRuleText(); } private void ModuleNaming_SelectedIndexChanged(object sender, EventArgs e) { MajorType majorType = MajorType.Parse((string)optType.SelectedItem); InputConfiguration inputConfig = InputConfiguration.Parse((string)optInputConfiguration.SelectedItem); SafetyRegulation safetyRegulation = SafetyRegulation.Parse((string)optSafetyRegulation.SelectedItem); //RatedPower ratedPower = RatedPower.Parse((string)optRatedPower.SelectedItem); SocketType socketType1 = SocketType.Parse((string)optConnector1.SelectedItem); SocketType socketType2 = SocketType.Parse((string)optConnector2.SelectedItem); SocketType socketType3 = SocketType.Parse((string)optConnector3.SelectedItem); NetworkOption networkOption = NetworkOption.Parse((string)optNetworkOption.SelectedItem); Generation generation = Generation.Parse((string)optGeneration.SelectedItem); if (string.IsNullOrEmpty(txtRatedPower.Text)) { MessageBox.Show("請輸入額定功率代碼", "ModuleNaming", MessageBoxButtons.OK, MessageBoxIcon.Warning); txtRatedPower.Text = "000"; return; } if (majorType != null && inputConfig != null && safetyRegulation != null && socketType1 != null && socketType2 != null && socketType3 != null && //ratedPower != null && networkOption != null && generation != null) { txtModelName.Text = majorType.Code + inputConfig.Code + safetyRegulation.Code + txtRatedPower.Text + socketType1.Code + socketType2.Code + socketType3.Code + networkOption.Code + generation.Code + txtCustomization.Text; } } private void InitializeUI() { Text = Text + string.Format(" V{0}", Assembly.GetEntryAssembly().GetName().Version); optType.Items.AddRange(MajorType.ListName.ToArray()); optInputConfiguration.Items.AddRange(InputConfiguration.ListName.ToArray()); optSafetyRegulation.Items.AddRange(SafetyRegulation.ListName.ToArray()); //optRatedPower.Items.AddRange(RatedPower.ListName.ToArray()); optConnector1.Items.AddRange(SocketType.ListName.ToArray()); optConnector2.Items.AddRange(SocketType.ListName.ToArray()); optConnector3.Items.AddRange(SocketType.ListName.ToArray()); optNetworkOption.Items.AddRange(NetworkOption.ListName.ToArray()); optGeneration.Items.AddRange(Generation.ListName.ToArray()); optType.SelectedIndex = 0; optInputConfiguration.SelectedIndex = 0; optSafetyRegulation.SelectedIndex = 0; //optRatedPower.SelectedIndex = 0; optConnector1.SelectedIndex = 0; optConnector2.SelectedIndex = 0; optConnector3.SelectedIndex = 0; optNetworkOption.SelectedIndex = 0; optGeneration.SelectedIndex = 0; optImageType.SelectedIndex = 0; } private void PostErrorCode(ErrorCode code) { switch(code) { case ErrorCode.BuiltFileError: MessageBox.Show("創建檔案失敗", "執行", MessageBoxButtons.OK, MessageBoxIcon.Error); break; case ErrorCode.BuiltSuccess: MessageBox.Show("創建完成", "執行", MessageBoxButtons.OK, MessageBoxIcon.Information); break; case ErrorCode.FileNotExist: MessageBox.Show("映像檔不存在", "執行", MessageBoxButtons.OK, MessageBoxIcon.Warning); break; case ErrorCode.FilePathEmpty: MessageBox.Show("尚未選擇映像檔", "執行", MessageBoxButtons.OK, MessageBoxIcon.Warning); break; case ErrorCode.ImageDataNotReady: MessageBox.Show("尚未選擇映像檔", "執行", MessageBoxButtons.OK, MessageBoxIcon.Warning); break; case ErrorCode.ImageTypeIndexNotExist: MessageBox.Show("未定義的映像檔型態", "執行", MessageBoxButtons.OK, MessageBoxIcon.Error); break; case ErrorCode.MemoryAllocationError: MessageBox.Show("記憶體配置時發生錯誤", "執行", MessageBoxButtons.OK, MessageBoxIcon.Error); break; case ErrorCode.ModelNameEmpty: MessageBox.Show("尚未輸入 Model Name", "執行", MessageBoxButtons.OK, MessageBoxIcon.Warning); break; case ErrorCode.ModelNameInputTypeError: MessageBox.Show("輸入的 Model Name 出現不合法的字元", "執行", MessageBoxButtons.OK, MessageBoxIcon.Error); break; case ErrorCode.ModelNameLenError: MessageBox.Show("輸入的 Model Name 長度須為 14", "執行", MessageBoxButtons.OK, MessageBoxIcon.Error); break; case ErrorCode.ReadFileError: MessageBox.Show("讀取映像檔時發生錯誤", "執行", MessageBoxButtons.OK, MessageBoxIcon.Error); break; case ErrorCode.NoError: default: break; } } } }