Mercurial > repos > IBBoard.WarFoundry.GUI.WinForms
changeset 110:39b93ca5fb9c
Fixes #234: Invalid data file doesn't stop load
* Refactor army change code
* Only set CurrentArmy value once we know it is safe and properly loaded
* Separate out smaller methods with sensible names
* Make CurrentGameSystem and CurrentArmy non-static
Also:
* Use parameter in FrmNewArmy constructor instead of ignoring it
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sat, 19 Dec 2009 15:45:21 +0000 |
parents | f4d3e64bdb18 |
children | 195e1639feb4 |
files | FrmMain.cs FrmNewArmy.cs |
diffstat | 2 files changed, 63 insertions(+), 33 deletions(-) [+] |
line wrap: on
line diff
--- a/FrmMain.cs Thu Dec 17 20:54:55 2009 +0000 +++ b/FrmMain.cs Sat Dec 19 15:45:21 2009 +0000 @@ -581,10 +581,12 @@ } catch (RequiredDataMissingException ex) { + log.Error("Required data missing from race file", ex); MessageBox.Show(this, ex.Message, Translation.GetTranslation("InvalidRaceFileBoxTitle", "invalid race file"), MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (InvalidFileException ex) { + log.Error("Race file was invalid", ex); MessageBox.Show(this, ex.Message, Translation.GetTranslation("InvalidRaceFileBoxTitle", "invalid race file"), MessageBoxButtons.OK, MessageBoxIcon.Error); } } @@ -814,16 +816,16 @@ } } - public static GameSystem CurrentGameSystem + public GameSystem CurrentGameSystem { get { return WarFoundryCore.CurrentGameSystem; } set { WarFoundryCore.CurrentGameSystem = value; } } - public static Army CurrentArmy + public Army CurrentArmy { get { return WarFoundryCore.CurrentArmy; } - set { WarFoundryCore.CurrentArmy = value; } + set { SetArmy(value); } } private void FrmMain_GameSystemChanged(GameSystem oldSystem, GameSystem newSystem) @@ -834,16 +836,45 @@ } private void FrmMain_ArmyChanged(Army oldArmy, Army newArmy) - { + { + CommandStack.Reset(); + loadedFilePath = null; + miSaveArmy.Enabled = false; + bttnSaveArmy.Enabled = false; + SetPointsPanelText(); SetAppTitle(); + } + private void SetArmy(Army newArmy) + { + IgnoreArmy(CurrentArmy); + CloseAllUnitWindows(); + + if (newArmy == null) + { + SetNullArmyState(); + } + else + { + ListenToArmy(newArmy); + SetNonNullArmyState(newArmy); + } + + WarFoundryCore.CurrentArmy = newArmy; + } + + private void IgnoreArmy(Army oldArmy) + { if (oldArmy != null) { oldArmy.UnitAdded -= UnitAddedMethod; oldArmy.UnitRemoved -= UnitRemovedMethod; oldArmy.PointsValueChanged -= PointsValueChangedMethod; } + } + private void CloseAllUnitWindows() + { FrmUnit[] unitForms = DictionaryUtils.ToArray(unitWindows); foreach (FrmUnit window in unitForms) @@ -852,41 +883,40 @@ } unitWindows.Clear(); + } - if (CurrentArmy==null) - { - miSaveArmyAs.Enabled = false; - miCloseArmy.Enabled = false; - miExportArmyAs.Enabled = false; - DisableCategoryButtons(); - armyTree.Hide(); - } - else + private void ListenToArmy(Army newArmy) + { + if (newArmy != null) { newArmy.UnitAdded += UnitAddedMethod; newArmy.UnitRemoved += UnitRemovedMethod; newArmy.PointsValueChanged += PointsValueChangedMethod; - //TODO: Clear all buttons - miSaveArmyAs.Enabled = true; - miCloseArmy.Enabled = true; - miExportArmyAs.Enabled = true; - EnableCategoryButtons(); - armyTree.Show(); + } + } - if (newArmy.Race.HasCategoryOverrides()) - { - RemoveCategoryButtons(); - AddCategoryButtons(newArmy.Race.Categories); - } + private void SetNullArmyState() + { + miSaveArmyAs.Enabled = false; + miCloseArmy.Enabled = false; + miExportArmyAs.Enabled = false; + DisableCategoryButtons(); + armyTree.Hide(); + } + + private void SetNonNullArmyState(Army newArmy) + { + if (newArmy.Race.HasCategoryOverrides()) + { + RemoveCategoryButtons(); + AddCategoryButtons(newArmy.Race.Categories); } - CommandStack.Reset(); - - loadedFilePath = null; - miSaveArmy.Enabled = false; - bttnSaveArmy.Enabled = false; - - SetPointsPanelText(); + EnableCategoryButtons(); + miSaveArmyAs.Enabled = true; + miCloseArmy.Enabled = true; + miExportArmyAs.Enabled = true; + armyTree.Show(); } private void AddCategoryButtons()
--- a/FrmNewArmy.cs Thu Dec 17 20:54:55 2009 +0000 +++ b/FrmNewArmy.cs Sat Dec 19 15:45:21 2009 +0000 @@ -51,9 +51,9 @@ gameSystems.Items.Add(gameSystem); } - if (WarFoundryCore.CurrentGameSystem != null) + if (system != null) { - gameSystems.SelectedItem = FrmMain.CurrentGameSystem; + gameSystems.SelectedItem = system; } else if (gameSystems.Items.Count == 1) {