private void UpdateView() { ListBox target = this.ActivePanel == ActiveVocabulary.FOREIGN_TO_NATIVE ? this.lsbForeignToNative : this.lsbNativeToForeign; EntrySet current = this.ActivePanel == ActiveVocabulary.FOREIGN_TO_NATIVE ? this.CurrentProject.ForeignEntries : this.CurrentProject.NativeEntries; target.Items.Clear(); for (int i = 0; i < current.AllEntries.Count; i++) { target.Items.Add(current.AllEntries.ElementAt(i).Value); } this.UnsavedChanges = true; this.Text = "Slovoca - " + this.CurrentProject.Location + " " + Properties.Resources.MAIN_WINDOW_UNSAVED_CHANGES_TAG; }
/// <summary> /// Loads the entries of the selected vocabulary part from the specified XML project file handler. /// </summary> /// <param name="vocabulary">Source project file handler.</param> /// <param name="target">Vocabulary part to fill.</param> private void LoadEntrySet(XElement vocabulary, ActiveVocabulary target) { EntrySet set = null; IEnumerable <XElement> entries = null; switch (target) { case ActiveVocabulary.FOREIGN_TO_NATIVE: set = this.ForeignEntries; entries = from entry in vocabulary.Elements("ForeignEntries").Elements("Entry") select entry; break; case ActiveVocabulary.NATIVE_TO_FOREIGN: set = this.NativeEntries; entries = from entry in vocabulary.Elements("NativeEntries").Elements("Entry") select entry; break; } foreach (XElement entry in entries) { List <string> translations = new List <string>(), pronounciations = new List <string>(); IEnumerable <XElement> words = from node in entry.Elements("Translations").Elements("Word") select node; foreach (XElement word in words) { translations.Add((string)(from item in word.Elements("Meaning") select item).First()); pronounciations.Add((string)(from item in word.Elements("Pronounciation") select item).First()); } XElement meaning = (from node in entry.Elements("Word") select node).First(), notes = (from node in entry.Elements("Notes") select node).First(); switch (target) { case ActiveVocabulary.FOREIGN_TO_NATIVE: set.AddEntry(new Entry((string)(from node in meaning.Elements("Meaning") select node).First(), (string)(from node in meaning.Elements("Pronounciation") select node).First(), translations.ToArray(), pronounciations.ToArray(), ((string)notes).Split('\n'), this.NativeEntries.Language)); break; case ActiveVocabulary.NATIVE_TO_FOREIGN: set.AddEntry(new Entry((string)(from node in meaning.Elements("Meaning") select node).First(), (string)(from node in meaning.Elements("Pronounciation") select node).First(), translations.ToArray(), pronounciations.ToArray(), ((string)notes).Split('\n'), this.ForeignEntries.Language)); break; } } }
/// <summary> /// Loads vocabulary entries from the project file. /// </summary> /// <returns>Error message or null if all went well.</returns> public string ReadFromDisk() { try { FileStream file = new FileStream(this.Location, FileMode.Open, FileAccess.Read, FileShare.Read); XElement vocabulary = XElement.Load(file); this.ForeignEntries = new EntrySet(new CultureInfo((string)(from culture in vocabulary.Descendants("ForeignLanguage") select culture).First()), false); this.NativeEntries = new EntrySet(new CultureInfo((string)(from culture in vocabulary.Descendants("NativeLanguage") select culture).First()), true); this.LoadEntrySet(vocabulary, ActiveVocabulary.FOREIGN_TO_NATIVE); this.LoadEntrySet(vocabulary, ActiveVocabulary.NATIVE_TO_FOREIGN); file.Close(); } catch (Exception exception) { return(exception.Message); } return(null); }