/// <summary> /// Save the specified document as a new file, allowing the user to specify the new filepath. /// </summary> public void SaveFileAs(TextFileDocumentViewModel document) { string newFilePath = null; if (DialogProvider.UserSelectsNewFilePath(document.FilePath, out newFilePath)) { SaveFile(document, newFilePath); } }
/// <summary> /// Save the specified document, allowing the user to select a filename for new files. /// </summary> public void SaveFile(TextFileDocumentViewModel document) { if (string.IsNullOrEmpty(document.FilePath)) { SaveFileAs(document); } else { SaveFile(document, document.FilePath); } }
/// <summary> /// Open a file, create a view-model and it to the collection of document view-models. /// </summary> public void OpenFile(string filePath) { try { var fileContent = File.ReadAllText(filePath); var newDocumentViewModel = new TextFileDocumentViewModel(filePath, fileContent, false); this.Documents.Add(newDocumentViewModel); } catch (Exception ex) { DialogProvider.ErrorMessage("Failed to open document " + filePath + "\n" + "Exception occurred:\n" + ex.ToString()); } }
/// <summary> /// Determine if the file can be closed. /// If the file is modified, but not saved, the user is asked /// to confirm that the document should be closed. /// </summary> public bool QueryCanCloseFile(TextFileDocumentViewModel document) { if (document.IsModified) { // // Ask the user to confirm closing a modified document. // if (!this.DialogProvider.QueryCloseModifiedDocument(document)) { // User doesn't want to close it. return(false); } } return(true); }
/// <summary> /// Save the specified document to the specified filepath. /// </summary> public void SaveFile(TextFileDocumentViewModel document, string newFilePath) { try { File.WriteAllText(newFilePath, document.Text); document.FilePath = newFilePath; document.IsModified = false; } catch (Exception ex) { DialogProvider.ErrorMessage("Failed to save document " + newFilePath + "\n" + "Exception occurred: \n" + ex.ToString()); } }
/// <summary> /// Close the specified document. /// Returns 'true' if the user allowed the file to be closed, /// or 'false' if the user canceled closing of the file. /// </summary> public bool CloseFile(TextFileDocumentViewModel document) { if (!QueryCanCloseFile(document)) { // // User has chosen not to close the file. // return(false); } this.Documents.Remove(document); // // File has been closed. // return(true); }
/// <summary> /// Close the specified document. /// Returns 'true' if the user allowed the file to be closed, /// or 'false' if the user canceled closing of the file. /// </summary> public bool CloseFile(TextFileDocumentViewModel document) { if (!QueryCanCloseFile(document)) { // // User has chosen not to close the file. // return false; } this.Documents.Remove(document); // // File has been closed. // return true; }
/// <summary> /// Determine if the file can be closed. /// If the file is modified, but not saved, the user is asked /// to confirm that the document should be closed. /// </summary> public bool QueryCanCloseFile(TextFileDocumentViewModel document) { if (document.IsModified) { // // Ask the user to confirm closing a modified document. // if (!this.DialogProvider.QueryCloseModifiedDocument(document)) { // User doesn't want to close it. return false; } } return true; }
/// <summary> /// Create a new file and add it to the view-model. /// </summary> public void NewFile() { var newDocument = new TextFileDocumentViewModel(string.Empty, string.Empty, false); this.Documents.Add(newDocument); }
/// <summary> /// Allow the user to confirm whether they want to close a modified document. /// </summary> public bool QueryCloseModifiedDocument(TextFileDocumentViewModel document) { string msg = document.FileName + " has been modified but not saved.\n" + "Do you really want to close it?"; var result = MessageBox.Show(this, msg, "File modified but not saved", MessageBoxButton.YesNo, MessageBoxImage.Warning); return result == MessageBoxResult.Yes; }