comparison FrmMain.cs @ 38:a9c1e60d0e4d

Fixes #134: Add/enable save and open in WinForms * Enable and complete saving and opening (mainly still intact anyway)
author IBBoard <dev@ibboard.co.uk>
date Mon, 24 Aug 2009 21:21:58 +0000
parents 6ab7ddc038f9
children 25dfeb1db285
comparison
equal deleted inserted replaced
37:c07d3b982129 38:a9c1e60d0e4d
148 ControlTranslator.TranslateControl(armyTree); 148 ControlTranslator.TranslateControl(armyTree);
149 149
150 // hack to load default files 150 // hack to load default files
151 WarFoundryLoader.GetDefault().AddLoadDirectory(new DirectoryInfo(Constants.ExecutablePath + Constants.DirectoryString + DefaultDataDir)); 151 WarFoundryLoader.GetDefault().AddLoadDirectory(new DirectoryInfo(Constants.ExecutablePath + Constants.DirectoryString + DefaultDataDir));
152 WarFoundryLoader.GetDefault().RegisterFactory(WarFoundryXmlFactory.GetFactory()); 152 WarFoundryLoader.GetDefault().RegisterFactory(WarFoundryXmlFactory.GetFactory());
153 WarFoundrySaver.SetFileSaver(new WarFoundryXmlSaver());
153 154
154 /* 155 /*
155 if (args.Length == 1) 156 if (args.Length == 1)
156 { 157 {
157 logger.Debug("Attempting to load from file"); 158 logger.Debug("Attempting to load from file");
649 650
650 DialogResult dr = openArmyDialog.ShowDialog(this); 651 DialogResult dr = openArmyDialog.ShowDialog(this);
651 652
652 if (dr == DialogResult.OK) 653 if (dr == DialogResult.OK)
653 { 654 {
654 /*
655 try 655 try
656 { 656 {
657 CurrentArmy = Factory.LoadArmy(openArmyDialog.FileName); 657 string newFilePath = openArmyDialog.FileName;
658 CurrentArmy = WarFoundryLoader.GetDefault().LoadArmy(new FileInfo(newFilePath));
659 loadedFilePath = newFilePath;
658 return true; 660 return true;
659 } 661 }
660 catch (InvalidFileException ex) 662 catch (InvalidFileException ex)
661 { 663 {
662 logger.Error(ex); 664 logger.Error(ex);
663 MessageBox.Show(this, ex.Message, Translation.GetTranslation("InvalidFileBoxTitle", "Invalid data file"), MessageBoxButtons.OK, MessageBoxIcon.Error); 665 MessageBox.Show(this, ex.Message, Translation.GetTranslation("InvalidFileBoxTitle", "Invalid data file"), MessageBoxButtons.OK, MessageBoxIcon.Error);
664 return false; 666 return false;
665 } 667 }
666 * */
667 return false;
668 } 668 }
669 else 669 else
670 { 670 {
671 return false; 671 return false;
672 } 672 }
740 740
741 private bool SaveCurrentArmy() 741 private bool SaveCurrentArmy()
742 { 742 {
743 bool saved = false; 743 bool saved = false;
744 744
745 if (loadedFilePath != null || PromptForFilePath()) 745 string filePath = loadedFilePath;
746 { 746
747 saved = SaveCurrentArmyToFile(); 747 if (filePath == null)
748 {
749 filePath = PromptForFilePath();
750 }
751
752 if (filePath != null)
753 {
754 saved = SaveCurrentArmyToFile(filePath);
748 } 755 }
749 756
750 return saved; 757 return saved;
751 } 758 }
752 759
753 private bool SaveCurrentArmyAs() 760 private bool SaveCurrentArmyAs()
754 { 761 {
755 bool saved = false; 762 bool saved = false;
756 763 string filePath = PromptForFilePath();
757 if (PromptForFilePath()) 764
758 { 765 if (filePath != null)
759 saved = SaveCurrentArmyToFile(); 766 {
767 saved = SaveCurrentArmyToFile(filePath);
760 } 768 }
761 769
762 return saved; 770 return saved;
763 } 771 }
764 772
765 private bool SaveCurrentArmyToFile() 773 private bool SaveCurrentArmyToFile(string filePath)
766 { 774 {
767 if (WarFoundrySaver.GetSaver().Save(CurrentArmy, loadedFilePath)) 775 if (WarFoundrySaver.GetSaver().Save(CurrentArmy, filePath))
768 { 776 {
777 loadedFilePath = filePath;
769 miSaveArmy.Enabled = false; 778 miSaveArmy.Enabled = false;
770 bttnSaveArmy.Enabled = false; 779 bttnSaveArmy.Enabled = false;
771 CommandStack.setCleanMark(); 780 CommandStack.setCleanMark();
772 return true; 781 return true;
773 } 782 }
774 else 783 else
775 { 784 {
776 loadedFilePath = null;
777 MessageBox.Show(this, Translation.GetTranslation("SaveFailed"), Translation.GetTranslation("SaveFailedTitle"), MessageBoxButtons.OK, MessageBoxIcon.Error); 785 MessageBox.Show(this, Translation.GetTranslation("SaveFailed"), Translation.GetTranslation("SaveFailedTitle"), MessageBoxButtons.OK, MessageBoxIcon.Error);
778 return false; 786 return false;
779 } 787 }
780 } 788 }
781 789
782 private bool PromptForFilePath() 790 private string PromptForFilePath()
783 { 791 {
784 if (saveArmyDialog.Filter == "") 792 if (saveArmyDialog.Filter == "")
785 { 793 {
786 string savePath = ArmiesPath; 794 string savePath = ArmiesPath;
787 795
797 805
798 DialogResult dr = saveArmyDialog.ShowDialog(this); 806 DialogResult dr = saveArmyDialog.ShowDialog(this);
799 807
800 if (dr == DialogResult.OK) 808 if (dr == DialogResult.OK)
801 { 809 {
802 loadedFilePath = saveArmyDialog.FileName; 810 return saveArmyDialog.FileName;
803 return true;
804 } 811 }
805 else 812 else
806 { 813 {
807 return false; 814 return null;
808 } 815 }
809 } 816 }
810 817
811 private void toolBar_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e) 818 private void toolBar_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
812 { 819 {