/// <summary> /// Loads a new word pack from <see cref="FolderPaths.WordPackDir"/> using <paramref name="name"/> as the file /// name. /// </summary> /// <param name="name"> /// The name of the word pack to load. /// </param> /// <param name="replace"> /// If the manager already contains a word pack with the given name, should it be replaced? /// </param> /// <returns> /// If the word pack was loaded from file and added to the manager successfully. /// </returns> public bool LoadFromName(string name, bool replace = false) { if (!Naming.IsValidName(name)) { return(false); } return(Add(WordPack.FromName(name), replace)); }
/// <summary> /// Returns a new word pack loaded from <see cref="FolderPaths.WordPackDir"/>, using <paramref name="name"/> /// as the file name. /// </summary> /// <param name="name"> /// The name of the word pack and file to load. /// </param> /// <returns> /// The word pack loaded from file on success, otherwise null. /// </returns> public static WordPack FromName(string name) { if (!Naming.IsValidName(name)) { return(null); } return(FromFile(FolderPaths.WordPackDir + "\\" + name + "." + Constants.PackFileExt)); }
private void SaveAsClicked(object sender, EventArgs e) { NameEditor ne = new NameEditor(m_pack.Name + " Copy"); ne.ShowDialog(this); if (ne.Cancelled) { return; } string name = ne.NameField.Trim(); if (!Naming.IsValidName(name)) { ErrorDialog er = new ErrorDialog(Dialogs.InvalidPackName(name ?? string.Empty)); er.ShowDialog(this); return; } m_pack.Name = name; string filename = FolderPaths.WordPackDir + "\\" + name + "." + Constants.PackFileExt; if (File.Exists(filename)) { ConfirmDialog cd = new ConfirmDialog(Dialogs.ReplacePack(name), "Replace pack?"); cd.ShowDialog(this); if (cd.Cancelled || !cd.Decision) { return; } } m_pack.Clear(); m_pack.Add(textBox.Lines); if (!m_pack.SaveToFile(filename)) { ErrorDialog er = new ErrorDialog(Dialogs.PackSaveFail); er.ShowDialog(this); } else { Changed = false; } }
/// <summary> /// Access the word pack with the given name. /// </summary> /// <remarks> /// Using the setter with a pack name that does not exist in the manager will add a new pack. /// </remarks> /// <param name="name"> /// The name of the word pack to access. /// </param> /// <returns> /// The word pack with the given name if it exists within the manager, otherwise null. /// </returns> /// <exception cref="ArgumentNullException"> /// When trying to set a null value. /// </exception> /// <exception cref="ArgumentException"> /// If the given name is invalid (<see cref="Naming.IsValidName(string)"/>) or it is not the same as the given /// packs' name. /// </exception> public WordPack this[string name] { get { return(Get(name)); } set { if (value == null) { throw new ArgumentNullException(); } if (!Naming.IsValidName(name) || value.Name != name) { throw new ArgumentException(); } if (Contains(name)) { Set(value); } else { Add(value); } } }
/// <summary> /// Constructs a new word pack with an optional name. /// </summary> /// <remarks> /// If the given name is not valid (<see cref="Naming.IsValidName(string)"/>) it will be replaced with /// "New WordPack". /// </remarks> /// <param name="name"> /// The name of the new pack. /// </param> public WordPack(string name = null) { Name = !Naming.IsValidName(name) ? "New Wordpack" : name; m_words = new List <string>(); }