private void cmdAdd_Click(object sender, RoutedEventArgs e) { try { if (string.IsNullOrEmpty(txtFirst.Text) == true) throw new Exception("You need to add a valid first value"); if (string.IsNullOrEmpty(txtSecond.Text) == true) throw new Exception("You need to add a valid second value"); DictionaryEntry entry = new DictionaryEntry(); entry.AEntry = txtFirst.Text; entry.BEntry = txtSecond.Text; CrammerDictionary crammerDict = (Application.Current as App).CurrentDictionary; if (crammerDict.entryExists(entry)) { txtFirst.Text = ""; txtSecond.Text = ""; throw new Exception("Entry already exists and will not be added"); } crammerDict.addEntry(entry); crammerDict.save(true); txtFirst.Text = ""; txtSecond.Text = ""; txtFirst.Focus(); } catch (Exception ex) { (Application.Current as App).ErrorMessage = ex.Message; this.NavigationService.Navigate(new Uri("/ErrorMessage.xaml", UriKind.Relative)); } }
/// <summary> /// A new entry is available /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void cmdAddEntry_Click(object sender, EventArgs e) { try { DictionaryEntry entry = new DictionaryEntry(); entry.AEntry = txtFirst.Text; entry.BEntry = txtSecond.Text; if (mDictionary.entryExists(entry)) { MessageBox.Show("Entry already exists and will not be added", "Crammer", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); txtFirst.Text = ""; txtSecond.Text = ""; cmdAddEntry.Enabled = false; return; } toolStripButtonSave.Enabled = true; mDictionary.addEntry(entry); //mDictionary.save(); //setDirty(); int n = dataGridView1.Rows.Add(); dataGridView1.Rows[n].Cells[0].Value = entry.AEntry; dataGridView1.Rows[n].Cells[1].Value = entry.BEntry; dataGridView1.Rows[n].Cells[2].Value = entry.Active; dataGridView1.Rows[n].Cells[3].Value = entry; txtFirst.Text = ""; txtSecond.Text = ""; cmdAddEntry.Enabled = false; txtFirst.Focus(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Crammer", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } }
private void cmdAdd_Click(object sender, RoutedEventArgs e) { try { if (string.IsNullOrEmpty(txtFirst.Text)) throw new Exception("First value cannot be empty"); if (string.IsNullOrEmpty(txtSecond.Text)) throw new Exception("Second value cannot be empty"); DictionaryEntry entry = new DictionaryEntry(); entry.AEntry = txtFirst.Text; entry.BEntry = txtSecond.Text; IEnumerable<DictionaryEntry> entryHits = mCurrentDict.Entries.Where((c) => c.AEntry.ToLower() == txtFirst.Text.ToLower()); if (entryHits != null && entryHits.Count() > 0) { txtFirst.Text = ""; txtSecond.Text = ""; (Application.Current as App).ErrorMessage = "Entry already exists and will not be added"; this.NavigationService.Navigate(new Uri("/ErrorMessage.xaml", UriKind.Relative)); return; } mCurrentDict.addEntry(entry); mCurrentDict.save(true); listEntries.Items.Add(entry.AEntry); cmdClearFields_Click(null, null); } catch (Exception ex) { (Application.Current as App).ErrorMessage = ex.Message; this.NavigationService.Navigate(new Uri("/ErrorMessage.xaml", UriKind.Relative)); } }
/// <summary> /// Imports an external text file with delimited entries to create a new dictionary. /// </summary> private async Task<bool> importFile() { mDictEntries.Clear(); string[] delimiter = null; if (rbCSV.IsChecked == true) delimiter = new string[] { ";" }; else { if (string.IsNullOrEmpty(txtCustomSeparator.Text)) { //var dlg = new Windows.UI.Popups.MessageDialog("You must specify a delimiter string"); //await dlg.ShowAsync(); //return (false); throw new Exception("You must specify a delimiter string"); } delimiter = new string[] { txtCustomSeparator.Text }; } IList<string> fileLines = await FileIO.ReadLinesAsync(mCopiedImportFile); if (fileLines.Count == 0) { var dlg = new Windows.UI.Popups.MessageDialog("File " + mCopiedImportFile.Path + " does not contain any valid lines"); await dlg.ShowAsync(); return (false); } // Use current date ticks and increment by one to distinguish entries which may otherwise // get an exact same tick value depending on the speed of the processor. long currentTimeStampTicks = DateTime.Now.Ticks; foreach (string line in fileLines) { string entry = line.Trim(); if (string.IsNullOrEmpty(entry)) continue; string[] elems = entry.Split(delimiter, StringSplitOptions.RemoveEmptyEntries); if (elems.Length < 2) continue; DictionaryEntry dictEntry = new DictionaryEntry(); if (string.IsNullOrEmpty(elems[0]) == false) dictEntry.AEntry = elems[0].Trim(); else dictEntry.AEntry = elems[0]; if (string.IsNullOrEmpty(elems[1]) == false) dictEntry.BEntry = elems[1].Trim(); else dictEntry.BEntry = elems[1]; dictEntry.Stamp = new DateTime(currentTimeStampTicks++); mDictEntries.Add(dictEntry); } if (mDictEntries.Count == 0) { var dlg = new Windows.UI.Popups.MessageDialog("File " + Path.GetFileName(mCopiedImportFile.Path) + " did not yield any valid entries"); await dlg.ShowAsync(); return (false); } var successDlg = new Windows.UI.Popups.MessageDialog("Successfully imported " + mDictEntries.Count + " entries from:\r\n" + Path.GetFileName(mCopiedImportFile.Path)); await successDlg.ShowAsync(); return (true); }
public void removeEntry(DictionaryEntry entry) { if (!entryExists(entry)) throw new Exception("An entry with values: " + entry.AEntry + " / " + entry.BEntry + " does not exist in the dictionary"); mEntries.Remove(entry); save(true); }
/// <summary> /// Load a Crammer dictionary /// </summary> /// <param name="dictionary"></param> public void load(bool activeOnly) { IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings; if (!settings.TryGetValue<string>(CrammerConstants.CURRENT_DICT_NAME, out mDictionaryName)) { mDictionary = XElement.Load(DEFAULT_DICTIONARY); } else { //if (!settings.TryGetValue<XElement>(mDictionaryName, out mDictionary)) // throw new Exception("Failed to load dictionary: " + mDictionaryName); mDictionary = StorageIOHelper.getDictionary(mDictionaryName); } getDictionaryTitle(); createStateFile(); IEnumerable<XElement> rows = mDictionary.Descendants(DICT_ROW); mEntries.Clear(); mInactiveEntries.Clear(); foreach (XElement row in rows) { DictionaryEntry entry = new DictionaryEntry(row); if (activeOnly ) { if (entry.Active) mEntries.Add(entry); else mInactiveEntries.Add(entry); } else { mEntries.Add(entry); } } setFonts(); }
public bool entryExists(DictionaryEntry entry) { IEnumerable<DictionaryEntry> matches = from c in mEntries where c.AEntry == entry.AEntry select c; return ( matches.Count() > 0 ); }
/// <summary> /// Adds a specific entry /// </summary> /// <param name="entry"></param> public void addEntry(DictionaryEntry entry) { // Check if entry already exists if (entryExists(entry)) throw new Exception("An entry with values: " + entry.AEntry + " / " + entry.BEntry + " already exists"); mEntries.Add(entry); }
/// <summary> /// Imports an external text file with delimited entries to create a new dictionary. /// </summary> private void importFile() { mDictEntries.Clear(); string importFile = txtImportFile.Text; if ( string.IsNullOrEmpty(importFile)) throw new Exception("Please provide a valid file as input for the dictionary"); if ( File.Exists(importFile) == false) throw new Exception("File " + importFile + " is not valid or is not accessible"); string[] delimiter = null; if (rbCSV.Checked) delimiter = new string[] {";"}; else { if (string.IsNullOrEmpty(txtDivider.Text)) throw new Exception("You must specify a delimiter string"); delimiter = new string[] { txtDivider.Text }; } string[] fileLines = File.ReadAllLines(importFile, Encoding.Default); if (fileLines.Length == 0) throw new Exception("File " + importFile + " does not contain any valid lines"); // Use current date ticks and increment by one to distinguish entries which may otherwise // get an exact same tick value depending on the speed of the processor. long currentTimeStampTicks = DateTime.Now.Ticks; foreach (string line in fileLines) { string entry = line.Trim(); if (string.IsNullOrEmpty(entry)) continue; string[] elems = entry.Split(delimiter, StringSplitOptions.RemoveEmptyEntries); if (elems.Length < 2) continue; DictionaryEntry dictEntry = new DictionaryEntry(); if ( string.IsNullOrEmpty(elems[0]) == false ) dictEntry.AEntry = elems[0].Trim(); else dictEntry.AEntry = elems[0]; if ( string.IsNullOrEmpty(elems[1]) == false ) dictEntry.BEntry = elems[1].Trim(); else dictEntry.BEntry = elems[1]; dictEntry.Stamp = new DateTime(currentTimeStampTicks++); mDictEntries.Add(dictEntry); } if (mDictEntries.Count == 0) { tabControl1.SelectedTab = tabImport; throw new Exception("File " + importFile + " did not yield any valid entries"); } MessageBox.Show("Successfully imported " + mDictEntries.Count + " entries from:\r\n" + importFile, "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); }
private void cmdDelete_Click(object sender, RoutedEventArgs e) { try { if (mCurrentEntry == null) throw new Exception("Double click list to select an entry to delete"); mCurrentDict.removeEntry(mCurrentEntry); mCurrentDict.save(true); listEntries.Items.Remove(mCurrentEntry.AEntry); cmdClearFields_Click(null, null); mCurrentEntry = null; } catch (Exception ex) { (Application.Current as App).ErrorMessage = ex.Message; this.NavigationService.Navigate(new Uri("/ErrorMessage.xaml", UriKind.Relative)); } }
private void listEntries_DoubleTap(object sender, GestureEventArgs e) { object selectedItem = listEntries.SelectedItem; if (selectedItem == null) return; string aEntry = selectedItem as string; if ( aEntry == null ) return; IEnumerable<DictionaryEntry> entries = mCurrentDict.Entries.Where((c) => c.AEntry == aEntry); if (entries == null || entries.Count() == 0) return; cmdDelete.IsEnabled = true; cmdSave.IsEnabled = true; mCurrentEntry = entries.First(); txtFirst.Text = mCurrentEntry.AEntry; txtSecond.Text = mCurrentEntry.BEntry; }
/// <summary> /// Load a Crammer dictionary /// </summary> /// <param name="dictionary"></param> public void load(string dictionary, bool activeOnly) { if (string.IsNullOrEmpty(dictionary)) throw new Exception("No dictionary name given"); mDictionaryFile = dictionary; createStateFile(); mDictionary = XElement.Load(dictionary); getDictionaryTitle(); IEnumerable<XElement> rows = mDictionary.Descendants(DICT_ROW); mEntries.Clear(); mInactiveEntries.Clear(); foreach (XElement row in rows) { DictionaryEntry entry = new DictionaryEntry(row); if (activeOnly ) { if (entry.Active) mEntries.Add(entry); else mInactiveEntries.Add(entry); } else { mEntries.Add(entry); } } }
private void listEntries_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e) { try { if (listEntries.SelectedItem == null) return; mCurrentEntry = listEntries.SelectedItem as DictionaryEntry; if (mCurrentEntry == null) return; txtA.TextChanged -= this.txtA_TextChanged; txtA.Text = mCurrentEntry.AEntry; txtB.Text = mCurrentEntry.BEntry; txtA.TextChanged += this.txtA_TextChanged; enableButtons(true); } catch (Exception ex) { if (this.Frame != null) { this.Frame.Navigate(typeof(MessagePopup), ex.Message); } } }
private void clearAfterChange() { txtA.Text = ""; txtB.Text = ""; enableButtons(false); listEntries.DataContext = null; mCurrentEntry = null; if ((Application.Current as App).PageVisited != CrammerConstants.EDITED_ENTRIES) (Application.Current as App).PageVisited = CrammerConstants.EDITED_ENTRIES; }
private async void save_Click_1(object sender, RoutedEventArgs e) { // Do not allow a save if either of the entry values are empty try { if (string.IsNullOrEmpty(txtA.Text) || string.IsNullOrEmpty(txtB.Text)) { var dlg = new Windows.UI.Popups.MessageDialog("An entry must contain two valid values. Cannot save"); await dlg.ShowAsync(); return; } waitRing.IsActive = true; if (mCurrentEntry == null) { DictionaryEntry entry = new DictionaryEntry(); entry.AEntry = txtA.Text; entry.BEntry = txtB.Text; if ((Application.Current as App).CurrentDictionary.entryExists(entry)) { var dlg = new Windows.UI.Popups.MessageDialog("Entry already exists and will not be added"); await dlg.ShowAsync(); txtA.Text = ""; txtB.Text = ""; return; } (Application.Current as App).CurrentDictionary.addEntry(entry); await (Application.Current as App).CurrentDictionary.save(true); } else { mCurrentEntry.AEntry = txtA.Text; mCurrentEntry.BEntry = txtB.Text; await (Application.Current as App).CurrentDictionary.save(true); mCurrentEntry = null; save.IsEnabled = false; } clearAfterChange(); enableButtons(false); setDictStats(); } catch (Exception ex) { waitRing.IsActive = false; if (this.Frame != null) { this.Frame.Navigate(typeof(MessagePopup), ex.Message); } } finally { waitRing.IsActive = false; } }