private void HandleAddMechResults(AddMechResult result, MechModel mechForAddition) { switch (result) { case AddMechResult.CHASSIS_AND_VARIANT_EXIST: this.ResponseMessage.Text = String.Format("{0} - {1} already in inventory", mechForAddition.MechModelName, mechForAddition.ModelVariantName); this.ResponseMessage.ForeColor = Color.Red; break; case AddMechResult.SUCCESSFULLY_ADDED_NEW_CHASSIS_AND_VARIANT: this.ResponseMessage.Text = String.Format("Successfully added {0} - {1} to inventory!", mechForAddition.MechModelName, mechForAddition.ModelVariantName); UpdateExistingMechsDropdown(mechForAddition); this.ResponseMessage.ForeColor = Color.Green; break; case AddMechResult.SUCCESSFULLY_ADDED_NEW_VARIANT: this.ResponseMessage.Text = String.Format("Successfully added {0} variant to {1}", mechForAddition.ModelVariantName, mechForAddition.MechModelName); this.ResponseMessage.ForeColor = Color.Green; break; } this.ResponseMessage.Visible = true; }
private void AddMechButton_Click(object sender, EventArgs e) { if (ValidateForm()) { string mechModel; if (this.ExistingMechs.SelectedIndex == 0) { mechModel = this.NewChassisName.Text; } else { mechModel = ((DropDownItem)this.ExistingMechs.Items[this.ExistingMechs.SelectedIndex]).DisplayValue; } string mechVariant = this.VariantName.Text; Factions faction = (Factions)System.Enum.Parse(typeof(Factions), ((DropDownItem)this.FactionSelector.Items[this.FactionSelector.SelectedIndex]).Value); string weightIncrement = ((DropDownItem)this.WeightIncrement.Items[this.WeightIncrement.SelectedIndex]).Value; MechClasses mechClass = (MechClasses)System.Enum.Parse(typeof(MechClasses), ((DropDownItem)this.WeightClassSelector.Items[this.WeightClassSelector.SelectedIndex]).Value); MechModel mechForAddition = MechModelMaker.GenerateMechModel(mechClass, mechModel, mechVariant, weightIncrement, faction); AddMechResult result = MChooserXMLWriter.AddMechToFile(mechForAddition); HandleAddMechResults(result, mechForAddition); } else { this.ResponseMessage.Text = "Please fill out all relevent data for new mech or variant"; this.ResponseMessage.ForeColor = Color.Red; this.ResponseMessage.Visible = true; } }
public static AddMechResult AddMechToFile(MechModel mech) { AddMechResult result = AddMechResult.CHASSIS_AND_VARIANT_EXIST; FileInfo fileInfo = new FileInfo(Constants.StaticValues.OwnedMechsFile); XDocument doc = XDocument.Load(fileInfo.FullName); XElement model = (from el in doc.Root.Elements(mech.MechClass.ToString().ToLower()) .Elements(StaticValues.XMLMechTag) where (string)el.Attribute(StaticValues.XMLModelNameTag) == mech.MechModelName select el) .FirstOrDefault(); if (CheckEntryExists(model, mech) == false) { if (model == null) { XElement weightClass = doc.Root.Element(mech.MechClass.ToString().ToLower()); weightClass.Add( new XElement(StaticValues.XMLMechTag, new object[] { new XAttribute(StaticValues.XMLMechfactionTag, mech.MechFaction), new XAttribute(StaticValues.XMLWeightIncrementTag, mech.GetMechWeight()), new XAttribute(StaticValues.XMLModelNameTag, mech.MechModelName), new XElement(StaticValues.XMLModelVariantTag, mech.ModelVariantName) })); result = AddMechResult.SUCCESSFULLY_ADDED_NEW_CHASSIS_AND_VARIANT; } else { model.Add( new XElement(StaticValues.XMLModelVariantTag, mech.ModelVariantName)); result = AddMechResult.SUCCESSFULLY_ADDED_NEW_VARIANT; } doc.Save(fileInfo.FullName); } else { result = AddMechResult.CHASSIS_AND_VARIANT_EXIST; } return(result); }