comparison FrmMain.cs @ 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 3fae39208d06
children 9c5f7c5b0f1c
comparison
equal deleted inserted replaced
109:f4d3e64bdb18 110:39b93ca5fb9c
579 { 579 {
580 CurrentArmy = new Army(newArmy.SelectedRace, newArmy.ArmyName, newArmy.ArmySize); 580 CurrentArmy = new Army(newArmy.SelectedRace, newArmy.ArmyName, newArmy.ArmySize);
581 } 581 }
582 catch (RequiredDataMissingException ex) 582 catch (RequiredDataMissingException ex)
583 { 583 {
584 log.Error("Required data missing from race file", ex);
584 MessageBox.Show(this, ex.Message, Translation.GetTranslation("InvalidRaceFileBoxTitle", "invalid race file"), MessageBoxButtons.OK, MessageBoxIcon.Error); 585 MessageBox.Show(this, ex.Message, Translation.GetTranslation("InvalidRaceFileBoxTitle", "invalid race file"), MessageBoxButtons.OK, MessageBoxIcon.Error);
585 } 586 }
586 catch (InvalidFileException ex) 587 catch (InvalidFileException ex)
587 { 588 {
589 log.Error("Race file was invalid", ex);
588 MessageBox.Show(this, ex.Message, Translation.GetTranslation("InvalidRaceFileBoxTitle", "invalid race file"), MessageBoxButtons.OK, MessageBoxIcon.Error); 590 MessageBox.Show(this, ex.Message, Translation.GetTranslation("InvalidRaceFileBoxTitle", "invalid race file"), MessageBoxButtons.OK, MessageBoxIcon.Error);
589 } 591 }
590 } 592 }
591 } 593 }
592 } 594 }
812 //it must be one of our extra buttons for the categories 814 //it must be one of our extra buttons for the categories
813 AddUnitFromCategory((Category)e.Button.Tag); 815 AddUnitFromCategory((Category)e.Button.Tag);
814 } 816 }
815 } 817 }
816 818
817 public static GameSystem CurrentGameSystem 819 public GameSystem CurrentGameSystem
818 { 820 {
819 get { return WarFoundryCore.CurrentGameSystem; } 821 get { return WarFoundryCore.CurrentGameSystem; }
820 set { WarFoundryCore.CurrentGameSystem = value; } 822 set { WarFoundryCore.CurrentGameSystem = value; }
821 } 823 }
822 824
823 public static Army CurrentArmy 825 public Army CurrentArmy
824 { 826 {
825 get { return WarFoundryCore.CurrentArmy; } 827 get { return WarFoundryCore.CurrentArmy; }
826 set { WarFoundryCore.CurrentArmy = value; } 828 set { SetArmy(value); }
827 } 829 }
828 830
829 private void FrmMain_GameSystemChanged(GameSystem oldSystem, GameSystem newSystem) 831 private void FrmMain_GameSystemChanged(GameSystem oldSystem, GameSystem newSystem)
830 { 832 {
831 SetAppTitle(); 833 SetAppTitle();
832 RemoveCategoryButtons(); 834 RemoveCategoryButtons();
833 AddCategoryButtons(); 835 AddCategoryButtons();
834 } 836 }
835 837
836 private void FrmMain_ArmyChanged(Army oldArmy, Army newArmy) 838 private void FrmMain_ArmyChanged(Army oldArmy, Army newArmy)
837 { 839 {
840 CommandStack.Reset();
841 loadedFilePath = null;
842 miSaveArmy.Enabled = false;
843 bttnSaveArmy.Enabled = false;
844 SetPointsPanelText();
838 SetAppTitle(); 845 SetAppTitle();
839 846 }
847
848 private void SetArmy(Army newArmy)
849 {
850 IgnoreArmy(CurrentArmy);
851 CloseAllUnitWindows();
852
853 if (newArmy == null)
854 {
855 SetNullArmyState();
856 }
857 else
858 {
859 ListenToArmy(newArmy);
860 SetNonNullArmyState(newArmy);
861 }
862
863 WarFoundryCore.CurrentArmy = newArmy;
864 }
865
866 private void IgnoreArmy(Army oldArmy)
867 {
840 if (oldArmy != null) 868 if (oldArmy != null)
841 { 869 {
842 oldArmy.UnitAdded -= UnitAddedMethod; 870 oldArmy.UnitAdded -= UnitAddedMethod;
843 oldArmy.UnitRemoved -= UnitRemovedMethod; 871 oldArmy.UnitRemoved -= UnitRemovedMethod;
844 oldArmy.PointsValueChanged -= PointsValueChangedMethod; 872 oldArmy.PointsValueChanged -= PointsValueChangedMethod;
845 } 873 }
846 874 }
875
876 private void CloseAllUnitWindows()
877 {
847 FrmUnit[] unitForms = DictionaryUtils.ToArray(unitWindows); 878 FrmUnit[] unitForms = DictionaryUtils.ToArray(unitWindows);
848 879
849 foreach (FrmUnit window in unitForms) 880 foreach (FrmUnit window in unitForms)
850 { 881 {
851 window.Close(); 882 window.Close();
852 } 883 }
853 884
854 unitWindows.Clear(); 885 unitWindows.Clear();
855 886 }
856 if (CurrentArmy==null) 887
857 { 888 private void ListenToArmy(Army newArmy)
858 miSaveArmyAs.Enabled = false; 889 {
859 miCloseArmy.Enabled = false; 890 if (newArmy != null)
860 miExportArmyAs.Enabled = false;
861 DisableCategoryButtons();
862 armyTree.Hide();
863 }
864 else
865 { 891 {
866 newArmy.UnitAdded += UnitAddedMethod; 892 newArmy.UnitAdded += UnitAddedMethod;
867 newArmy.UnitRemoved += UnitRemovedMethod; 893 newArmy.UnitRemoved += UnitRemovedMethod;
868 newArmy.PointsValueChanged += PointsValueChangedMethod; 894 newArmy.PointsValueChanged += PointsValueChangedMethod;
869 //TODO: Clear all buttons 895 }
870 miSaveArmyAs.Enabled = true; 896 }
871 miCloseArmy.Enabled = true; 897
872 miExportArmyAs.Enabled = true; 898 private void SetNullArmyState()
873 EnableCategoryButtons(); 899 {
874 armyTree.Show(); 900 miSaveArmyAs.Enabled = false;
875 901 miCloseArmy.Enabled = false;
876 if (newArmy.Race.HasCategoryOverrides()) 902 miExportArmyAs.Enabled = false;
877 { 903 DisableCategoryButtons();
878 RemoveCategoryButtons(); 904 armyTree.Hide();
879 AddCategoryButtons(newArmy.Race.Categories); 905 }
880 } 906
881 } 907 private void SetNonNullArmyState(Army newArmy)
882 908 {
883 CommandStack.Reset(); 909 if (newArmy.Race.HasCategoryOverrides())
884 910 {
885 loadedFilePath = null; 911 RemoveCategoryButtons();
886 miSaveArmy.Enabled = false; 912 AddCategoryButtons(newArmy.Race.Categories);
887 bttnSaveArmy.Enabled = false; 913 }
888 914
889 SetPointsPanelText(); 915 EnableCategoryButtons();
916 miSaveArmyAs.Enabled = true;
917 miCloseArmy.Enabled = true;
918 miExportArmyAs.Enabled = true;
919 armyTree.Show();
890 } 920 }
891 921
892 private void AddCategoryButtons() 922 private void AddCategoryButtons()
893 { 923 {
894 if (CurrentGameSystem!=null) 924 if (CurrentGameSystem!=null)