/// <summary> /// Called by TabbedExplorerPresenter to do a save. Return true if all ok. /// </summary> /// <returns>True if saved</returns> public bool SaveIfChanged() { bool result = true; try { if (this.ApsimXFile != null && this.ApsimXFile.FileName != null) { QuestionResponseEnum choice = QuestionResponseEnum.No; if (!File.Exists(this.ApsimXFile.FileName)) { choice = MainPresenter.AskQuestion("The original file '" + this.ApsimXFile.FileName + "' no longer exists.\n \nClick \"Yes\" to save to this location or \"No\" to discard your work."); } else { // Need to hide the right hand panel because some views may not save // their contents until they get a 'Detach' call. this.HideRightHandPanel(); // need to test is ApsimXFile has changed and only prompt when changes have occured. // serialise ApsimXFile to buffer StringWriter o = new StringWriter(); this.ApsimXFile.Write(o); string newSim = o.ToString(); StreamReader simStream = new StreamReader(this.ApsimXFile.FileName); string origSim = simStream.ReadToEnd(); // read original file to buffer2 simStream.Close(); if (string.Compare(newSim, origSim) != 0) { choice = MainPresenter.AskQuestion("Do you want to save changes in file " + this.ApsimXFile.FileName + " ?"); } } if (choice == QuestionResponseEnum.Cancel) { // cancel this.ShowRightHandPanel(); result = false; } else if (choice == QuestionResponseEnum.Yes) { // save this.WriteSimulation(); result = true; } } } catch (Exception err) { MainPresenter.ShowError(new Exception("Cannot save the file. Error: ", err)); this.ShowRightHandPanel(); result = false; } return(result); }
/// <summary> /// Called by TabbedExplorerPresenter to do a save. Return true if all ok. /// </summary> /// <returns>True if saved</returns> public bool SaveIfChanged() { bool result = true; try { if (this.ApsimXFile != null && this.ApsimXFile.FileName != null) { // need to test is ApsimXFile has changed and only prompt when changes have occured. // serialise ApsimXFile to buffer StringWriter o = new StringWriter(); this.ApsimXFile.Write(o); string newSim = o.ToString(); StreamReader simStream = new StreamReader(this.ApsimXFile.FileName); string origSim = simStream.ReadToEnd(); // read original file to buffer2 simStream.Close(); QuestionResponseEnum choice = QuestionResponseEnum.No; if (string.Compare(newSim, origSim) != 0) { choice = MainPresenter.AskQuestion("Do you want to save changes in file " + ApsimXFile.FileName + " ?"); } if (choice == QuestionResponseEnum.Cancel) { // cancel result = false; } else if (choice == QuestionResponseEnum.Yes) { // save // Need to hide the right hand panel because some views may not have saved // their contents until they get a 'Detach' call. this.HideRightHandPanel(); this.WriteSimulation(); result = true; } } } catch (Exception err) { MainPresenter.ShowMessage("Cannot save the file. Error: " + err.Message, DataStore.ErrorLevel.Error); result = false; } return(result); }
/// <summary> /// Called by TabbedExplorerPresenter to do a save. Return true if all ok. /// </summary> /// <returns>True if saved</returns> public bool SaveIfChanged() { bool result = true; try { if (!string.IsNullOrEmpty(ApsimXFile?.FileName)) { QuestionResponseEnum choice = QuestionResponseEnum.No; if (!File.Exists(ApsimXFile.FileName)) { choice = MainPresenter.AskQuestion("The original file '" + StringUtilities.PangoString(this.ApsimXFile.FileName) + "' no longer exists.\n \nClick \"Yes\" to save to this location or \"No\" to discard your work."); } else if (FileHasPendingChanges()) { choice = MainPresenter.AskQuestion("Do you want to save changes in file " + StringUtilities.PangoString(this.ApsimXFile.FileName) + " ?"); } if (choice == QuestionResponseEnum.Cancel) { ShowRightHandPanel(); result = false; } else if (choice == QuestionResponseEnum.Yes) { WriteSimulation(ApsimXFile.FileName); result = true; } } } catch (Exception err) { MainPresenter.ShowError(new Exception("Cannot save the file. Error: ", err)); result = false; } return(result); }