/// <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 when the user wants to halt the execution of a job. /// </summary> /// <param name="sender">Sender object.</param> /// <param name="args">Event arguments.</param> private async Task OnStopJobs(object sender, EventArgs args) { string[] jobIDs = view.GetSelectedJobIDs(); if (jobIDs.Length < 1) { throw new Exception("Unable to stop jobs: no jobs are selected"); } // Get the grammar right when asking for confirmation. string msg = "Are you sure you want to stop " + (jobIDs.Length > 1 ? "these " + jobIDs.Length + " jobs" : "this job") + "? There is no way to resume execution!"; if (presenter.AskQuestion(msg) != QuestionResponseEnum.Yes) { return; } foreach (string id in jobIDs) { await cloudInterface.StopJobAsync(id, cancelToken.Token); } }
/// <summary> /// User has clicked the stop button. /// </summary> /// <param name="sender">The sender of the event.</param> /// <param name="e">The event argument.</param> private async void OnStopClicked(object sender, EventArgs e) { int numJobs = jobListView.SelectedIndicies.Length; if (numJobs < 1) { throw new Exception("Unable to stop jobs: no jobs are selected"); } // Get the grammar right when asking for confirmation. string msg = "Are you sure you want to stop " + (numJobs > 1 ? "these " + numJobs + " jobs" : "this job") + "? There is no way to resume execution!"; if (presenter.AskQuestion(msg) == QuestionResponseEnum.Yes) { foreach (int listViewIndex in jobListView.SelectedIndicies) { var jobListIndex = ConvertListViewIndexToJobIndex(listViewIndex); await cloudInterface.StopJobAsync(jobList[jobListIndex].Id, cancelToken.Token); } } }
/// <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); }