/// <summary>Set a property of an entry in the database (but not in the in-memory list).</summary> public override void SetProperty(WordListEntry entry, EntryProperty property, object value) { int index = IndexOf(entry); Debug.Assert(index >= 0); worker.SetProperty(index, property.ToString(), value); }
//Create a new TranslationPair when the user edits the row for new records. void grid_NewRowNeeded(object sender, DataGridViewRowEventArgs e) { pairInEdit = new WordListEntry(null, null, null); rowInEdit = grid.Rows.Count - 1; Debug.Print("New row added to the grid: e.Row = {0}, rowInEdit = {1}", e.Row, rowInEdit.Value); }
/// <summary>Get a property of an entry from the database.</summary> public override T GetProperty <T>(WordListEntry entry, EntryProperty property) { int index = IndexOf(entry); Debug.Assert(index >= 0); return((T)worker.GetProperty(index, property.ToString())); }
public override bool Remove(WordListEntry item) { bool existed = list.Contains(item); Do(new Deletion(this, IndexOf(item))); return(existed); }
public override void Undo() { item = list[index]; list[index] = oldItem; worker.SetEntry(index, oldItem); }
public override void Insert(int index, WordListEntry item) { if (index > Count || index < 0) { throw new ArgumentOutOfRangeException("index"); } Do(new Insertion(this, index, item)); }
public void Paste() { int row = grid.Rows.GetFirstRow(DataGridViewElementStates.Selected); // If there's no item selected, just paste at the end. if (row < 0 || row >= source.Count) { row = source.Count; } var data = Clipboard.GetDataObject(); var entries = WordListEntries.FromDataObject(data); if (entries != null && entries.Items.Count > 0) { Paste(entries, row); } else { string text = Clipboard.GetText().Trim(); if (text.Contains("\n")) { return; } // If the cell is being edited, Ctrl-V will be handled by the textbox itself // so this code is only visited when a paste occurs with textual data on an // entry which isn't currently being edited. WordListEntry item = RowSource(row); if (item == null || row == rowInEdit) { Paste(new WordListEntries(source, new[] { new WordListEntry(source, text, string.Empty) }), row); return; } string phrase = item.Phrase, translation = item.Translation; if (grid.CurrentCellAddress.X == 0) { phrase = text; } else { translation = text; } if (row >= 0 && row < source.Count) { // Re-create the row source so that an undo list entry gets made. source[row] = new WordListEntry(source, phrase, translation); if (!grid.IsRowVisible(row)) { grid.FirstDisplayedScrollingRowIndex = row; } } } }
//Called when the user enters data for a cell and commits that change. //Stores the edited value in the TranslationPair representing the edited row. //This TranslationPair will be committed when the row is left. void grid_CellValuePushed(object sender, DataGridViewCellValueEventArgs e) { WordListEntry rowSource; if (source == null) { return; } Debug.WriteLine(string.Format("Cell value pushed at R{0}C{1}: {2}", e.RowIndex, e.ColumnIndex, e.Value)); if (e.RowIndex < source.Count) { Debug.WriteLine(string.Format(" Normal row.")); if (pairInEdit == null) { Debug.WriteLine(string.Format(" pairInEdit was null")); var we = source[e.RowIndex]; pairInEdit = new WordListEntry(null, we.Phrase, we.Translation); } else { Debug.WriteLine(string.Format(" pairInEdit was non-null")); } rowSource = pairInEdit; rowInEdit = e.RowIndex; } else { Debug.WriteLine(string.Format(" New row.")); //If the user previously deleted the row for new data (for example, by //cancelling the row edit) the pairInEdit will be null. if (pairInEdit == null) { pairInEdit = new WordListEntry(null); rowInEdit = e.RowIndex; Debug.WriteLine(string.Format(" pairInEdit was null")); } else { Debug.WriteLine(string.Format(" pairInEdit was non-null")); } rowSource = pairInEdit; } //If the row was emptied, the Value property may be null! if (e.ColumnIndex == 0) { rowSource.Phrase = e.Value != null?e.Value.ToString() : ""; } else { rowSource.Translation = e.Value != null?e.Value.ToString() : ""; } dirty = true; }
public override void Do() { worker.SwapRows(indices); foreach (int i in indices) { list[i] = new WordListEntry(owner, list[i].Translation, list[i].Phrase); } }
public override void SeekExact(long hash) { if (hash >= _hashTable.Length) { _currentEntry = null; } _currentPosition = (int)hash; _currentEntry = _wordList.ReadEntry(_hashTable.ReadOffset(_currentPosition)); }
void ListChanged(object sender, ListChangedEventArgs e) { if (e.ListChangedType == ListChangedType.ItemDeleted) { if (item == null || List == null) return; if (!List.Contains(item)) { Item = null; RaiseItemDeleted(); } } }
public PagesEnum(WordListEntry entry, HashTable hashTable, WordList wordList, PagenumberList pagenumberList, TextTable textTable) { _entry = entry; _hashTable = hashTable; _wordList = wordList; _pagenumberList = pagenumberList; _textTable = textTable; _pagenumbers = _pagenumberList.ReadPagenumbers(_entry).AsEnumerable().GetEnumerator(); // read all due to parallelism }
protected WordListEntries(SerializationInfo info, StreamingContext context) { var entries = info.GetValue("Entries", typeof(KeyValuePair<string, string>[])) as KeyValuePair<string, string>[]; if (entries != null) { var list = new List<WordListEntry>(); foreach (var entry in entries) list.Add(new WordListEntry(null, entry.Key, entry.Value)); Items = list; } else { Items = new WordListEntry[] { }; } }
public void SetEntry(int index, WordListEntry item) { using (var txn = Connection.BeginTransaction()) { ExecuteSQL(@" UPDATE VocabItems SET Phrase = ?, Translation = ? WHERE SetID = ? AND ListPosition = ?" , item.Phrase, item.Translation, list.ID, index); txn.Commit(); } }
void grid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { if (source == null) { return; } bool showEditPrompt = (e.RowIndex == rowInEdit && string.IsNullOrEmpty(pairInEdit.Phrase) && string.IsNullOrEmpty(pairInEdit.Translation)) || (e.RowIndex == grid.RowCount - 1 && grid.AllowUserToAddRows); if (showEditPrompt) { e.CellStyle.NullValue = e.ColumnIndex == 0 ? "Add Phrase Here" : "Add Translation Here"; } if (e.ColumnIndex == grid.CurrentCellAddress.X && e.RowIndex == grid.CurrentCellAddress.Y) { e.CellStyle.BackColor = e.CellStyle.SelectionBackColor = Color.DarkSlateBlue; e.CellStyle.ForeColor = e.CellStyle.SelectionForeColor = Color.White; return; } if (showEditPrompt) { //This is either the new record row, not being typed in, or the new record row which is now being edited. e.CellStyle.BackColor = Color.BurlyWood; e.CellStyle.SelectionBackColor = Color.BurlyWood; e.CellStyle.ForeColor = Color.White; e.CellStyle.SelectionForeColor = Color.White; return; } if (e.RowIndex >= source.Count) { //Don't style the temporary row for now. It doesn't work very well. return; } WordListEntry rowSource = null; if (source != null) { rowSource = source[e.RowIndex]; } e.CellStyle.BackColor = (e.RowIndex % 2 == 0) ? SystemColors.Control : SystemColors.ControlLightLight; //e.CellStyle.SelectionBackColor = (e.RowIndex % 2 == 0) ? Color.SteelBlue : Color.DeepSkyBlue; e.CellStyle.SelectionBackColor = (e.RowIndex % 2 == 0) ? Color.SteelBlue : Color.SkyBlue; e.CellStyle.SelectionForeColor = Color.Black; }
void ListChanged(object sender, ListChangedEventArgs e) { if (e.ListChangedType == ListChangedType.ItemDeleted) { if (item == null || List == null) { return; } if (!List.Contains(item)) { Item = null; RaiseItemDeleted(); } } }
protected WordListEntries(SerializationInfo info, StreamingContext context) { var entries = info.GetValue("Entries", typeof(KeyValuePair <string, string>[])) as KeyValuePair <string, string>[]; if (entries != null) { var list = new List <WordListEntry>(); foreach (var entry in entries) { list.Add(new WordListEntry(null, entry.Key, entry.Value)); } Items = list; } else { Items = new WordListEntry[] { }; } }
protected IList <WordListEntry> GetEntries(DbDataReader reader) { var result = new List <WordListEntry>(); while (reader.Read()) { if (reader.IsDBNull(0) || reader.IsDBNull(1)) { throw new StrongTypingException("One of the fields of a word list entry was DBNull."); } var phrase = (string)reader.GetValue(0); var translation = (string)reader.GetValue(1); var entry = new WordListEntry(list, phrase, translation); result.Add(entry); } return(result); }
/// <summary> /// Inserts a single item at the given index. /// </summary> /// <remarks>This should be avoided unless there really is only one item to be added, /// because it will probably be quite slow with many items.</remarks> public void Insert(int index, WordListEntry item) { if (item == null) { throw new ArgumentNullException(); } if (index < 0 || index > list.Count) { throw new ArgumentOutOfRangeException("index"); } using (var txn = Connection.BeginTransaction()) { insertCommandSetID.Value = list.ID; insertCommandListPosition.Value = index; insertCommandPhrase.Value = item.Phrase; insertCommandTranslation.Value = item.Translation; insertCommand.ExecuteNonQuery(); txn.Commit(); } }
void grid_CancelRowEdit(object sender, QuestionEventArgs e) { if (source == null) { return; } //Count-2 because there will be a new extender row, which will be //occupying Count-1, thus the currently edited row will be Count-2. if (rowInEdit == grid.Rows.Count - 2 && rowInEdit == source.Count) { // If the user has canceled the edit of a newly created row, // replace the corresponding TranslationPair object with a new, empty one. pairInEdit = new WordListEntry(null); } else { // If the user has canceled the edit of an existing row, // release the corresponding TranslationPair object. pairInEdit = null; rowInEdit = null; } }
void grid_CancelRowEdit(object sender, QuestionEventArgs e) { if (source == null) return; //Count-2 because there will be a new extender row, which will be //occupying Count-1, thus the currently edited row will be Count-2. if (rowInEdit == grid.Rows.Count - 2 && rowInEdit == source.Count) { // If the user has canceled the edit of a newly created row, // replace the corresponding TranslationPair object with a new, empty one. pairInEdit = new WordListEntry(null); } else { // If the user has canceled the edit of an existing row, // release the corresponding TranslationPair object. pairInEdit = null; rowInEdit = null; } }
public override int IndexOf(WordListEntry item) { return(list.IndexOf(item)); }
public override bool Contains(WordListEntry item) { return(list.Contains(item)); }
void ListDeleted(object sender, EventArgs e) { Item = null; RaiseItemDeleted(); }
void ListDeleted(object sender, EventArgs e) { Item = null; RaiseItemDeleted(); }
public override void Add(WordListEntry item) { Insert(Count, item); }
void source_ListChanged(object sender, ListChangedEventArgs e) { if (ignoreNextListChangedEvent) { ignoreNextListChangedEvent = false; return; } switch (e.ListChangedType) { case ListChangedType.ItemAdded: if (rowInEdit != null && rowInEdit >= e.NewIndex) { rowInEdit++; } grid.Rows.Insert(e.NewIndex, 1); grid.InvalidateRow(e.NewIndex); grid.Invalidate(); grid.Update(); break; case ListChangedType.ItemChanged: grid.InvalidateRow(e.NewIndex); break; case ListChangedType.ItemDeleted: if (rowInEdit > e.NewIndex) { rowInEdit--; } else if (rowInEdit == e.NewIndex) { grid.CancelEdit(); rowInEdit = null; pairInEdit = null; } grid.Rows.RemoveAt(e.NewIndex); break; case ListChangedType.ItemMoved: if (rowInEdit != null) { if (rowInEdit == e.OldIndex) { rowInEdit = e.NewIndex; } else if (rowInEdit < e.OldIndex && rowInEdit > e.NewIndex) { //Moved row was before the current row, but moved to after it. rowInEdit--; } else if (rowInEdit > e.OldIndex && rowInEdit >= e.NewIndex) { //Moved row was after current row, but moved to before it rowInEdit++; } } DataGridViewRow row = grid.Rows[e.OldIndex]; grid.Rows.RemoveAt(e.OldIndex); grid.Rows.Insert(e.NewIndex, row); break; case ListChangedType.Reset: grid.CancelEdit(); UpdateData(); break; } }
//This event occurs whenever the user changes the current row. void grid_RowValidated(object sender, DataGridViewCellEventArgs e) { Debug.WriteLine(string.Format("Row R{0}C{1} validated", e.RowIndex, e.ColumnIndex)); if (e.RowIndex >= source.Count && e.RowIndex != grid.Rows.Count - 1) { Debug.WriteLine(string.Format(" This is a new row.")); //This could happen if either the last row was deleted or the editing row was committed to the list. //In the case of a deleted row, is doing nothing the correct behaviour? if (pairInEdit != null) { //Suppress the ListChangedType.ItemAdded event. //(But only if we're actually doing something! Don't move this back out of the if statement!) ignoreNextListChangedEvent = true; if (pairInEdit.Phrase == null) { pairInEdit.Phrase = string.Empty; } if (pairInEdit.Translation == null) { pairInEdit.Translation = string.Empty; } pairInEdit.AddTo(source, source.Count); pairInEdit = null; rowInEdit = null; } else { Debug.Assert(rowInEdit == null); } } else if (pairInEdit != null && e.RowIndex < source.Count) { Debug.WriteLine(string.Format(" This is a normal row.")); //A normal row was edited, and the changes were committed. //This check is absolutely necessary. RowValidated is often called spuriously by the DataGridView //even when the user hasn't done anything. It could also happen when inserting a group of rows. //In that case, we certainly don't want to set the i'th element because that incurs a database //access in SqliteWordList - and what's more, currently, the command is re-created every time. //In short, it's weird. if (grid.IsCurrentRowDirty) { Debug.WriteLine(string.Format(" Row was dirty.", e.RowIndex, e.ColumnIndex)); if (pairInEdit.Phrase == null) { pairInEdit.Phrase = string.Empty; } if (pairInEdit.Translation == null) { pairInEdit.Translation = string.Empty; } ignoreNextListChangedEvent = true; pairInEdit.Owner = source; source[e.RowIndex] = pairInEdit; } else { Debug.WriteLine(string.Format(" Row was not dirty.", e.RowIndex, e.ColumnIndex)); } pairInEdit = null; rowInEdit = null; } else if (grid.ContainsFocus) { Debug.WriteLine(string.Format(" No row was being edited.")); pairInEdit = null; rowInEdit = null; } dirty = false; }
void source_ListChanged(object sender, ListChangedEventArgs e) { if (ignoreNextListChangedEvent) { ignoreNextListChangedEvent = false; return; } switch (e.ListChangedType) { case ListChangedType.ItemAdded: if (rowInEdit != null && rowInEdit >= e.NewIndex) rowInEdit++; grid.Rows.Insert(e.NewIndex, 1); grid.InvalidateRow(e.NewIndex); grid.Invalidate(); grid.Update(); break; case ListChangedType.ItemChanged: grid.InvalidateRow(e.NewIndex); break; case ListChangedType.ItemDeleted: if (rowInEdit > e.NewIndex) rowInEdit--; else if (rowInEdit == e.NewIndex) { grid.CancelEdit(); rowInEdit = null; pairInEdit = null; } grid.Rows.RemoveAt(e.NewIndex); break; case ListChangedType.ItemMoved: if (rowInEdit != null) { if (rowInEdit == e.OldIndex) { rowInEdit = e.NewIndex; } else if (rowInEdit < e.OldIndex && rowInEdit > e.NewIndex) { //Moved row was before the current row, but moved to after it. rowInEdit--; } else if (rowInEdit > e.OldIndex && rowInEdit >= e.NewIndex) { //Moved row was after current row, but moved to before it rowInEdit++; } } DataGridViewRow row = grid.Rows[e.OldIndex]; grid.Rows.RemoveAt(e.OldIndex); grid.Rows.Insert(e.NewIndex, row); break; case ListChangedType.Reset: grid.CancelEdit(); UpdateData(); break; } }
//This event occurs whenever the user changes the current row. void grid_RowValidated(object sender, DataGridViewCellEventArgs e) { Debug.WriteLine(string.Format("Row R{0}C{1} validated", e.RowIndex, e.ColumnIndex)); if (e.RowIndex >= source.Count && e.RowIndex != grid.Rows.Count - 1) { Debug.WriteLine(string.Format(" This is a new row.")); //This could happen if either the last row was deleted or the editing row was committed to the list. //In the case of a deleted row, is doing nothing the correct behaviour? if (pairInEdit != null) { //Suppress the ListChangedType.ItemAdded event. //(But only if we're actually doing something! Don't move this back out of the if statement!) ignoreNextListChangedEvent = true; if (pairInEdit.Phrase == null) pairInEdit.Phrase = string.Empty; if (pairInEdit.Translation == null) pairInEdit.Translation = string.Empty; pairInEdit.AddTo(source, source.Count); pairInEdit = null; rowInEdit = null; } else Debug.Assert(rowInEdit == null); } else if (pairInEdit != null && e.RowIndex < source.Count) { Debug.WriteLine(string.Format(" This is a normal row.")); //A normal row was edited, and the changes were committed. //This check is absolutely necessary. RowValidated is often called spuriously by the DataGridView //even when the user hasn't done anything. It could also happen when inserting a group of rows. //In that case, we certainly don't want to set the i'th element because that incurs a database //access in SqliteWordList - and what's more, currently, the command is re-created every time. //In short, it's weird. if (grid.IsCurrentRowDirty) { Debug.WriteLine(string.Format(" Row was dirty.", e.RowIndex, e.ColumnIndex)); if (pairInEdit.Phrase == null) pairInEdit.Phrase = string.Empty; if (pairInEdit.Translation == null) pairInEdit.Translation = string.Empty; ignoreNextListChangedEvent = true; pairInEdit.Owner = source; source[e.RowIndex] = pairInEdit; } else Debug.WriteLine(string.Format(" Row was not dirty.", e.RowIndex, e.ColumnIndex)); pairInEdit = null; rowInEdit = null; } else if (grid.ContainsFocus) { Debug.WriteLine(string.Format(" No row was being edited.")); pairInEdit = null; rowInEdit = null; } dirty = false; }
public void Paste() { int row = grid.Rows.GetFirstRow(DataGridViewElementStates.Selected); // If there's no item selected, just paste at the end. if (row < 0 || row >= source.Count) row = source.Count; var data = Clipboard.GetDataObject(); var entries = WordListEntries.FromDataObject(data); if (entries != null && entries.Items.Count > 0) { Paste(entries, row); } else { string text = Clipboard.GetText().Trim(); if (text.Contains("\n")) return; // If the cell is being edited, Ctrl-V will be handled by the textbox itself // so this code is only visited when a paste occurs with textual data on an // entry which isn't currently being edited. WordListEntry item = RowSource(row); if (item == null || row == rowInEdit) { Paste(new WordListEntries(source, new[] { new WordListEntry(source, text, string.Empty) }), row); return; } string phrase = item.Phrase, translation = item.Translation; if(grid.CurrentCellAddress.X == 0) phrase = text; else translation = text; if (row >= 0 && row < source.Count) { // Re-create the row source so that an undo list entry gets made. source[row] = new WordListEntry(source, phrase, translation); if(!grid.IsRowVisible(row)) grid.FirstDisplayedScrollingRowIndex = row; } } }
public SetItem(SqliteWordList owner, int index, WordListEntry item) : base(owner) { this.index = index; this.item = item; }
public override void Do() { oldItem = list[index]; list[index] = item; worker.SetEntry(index, item); }
//Called when the user enters data for a cell and commits that change. //Stores the edited value in the TranslationPair representing the edited row. //This TranslationPair will be committed when the row is left. void grid_CellValuePushed(object sender, DataGridViewCellValueEventArgs e) { WordListEntry rowSource; if (source == null) return; Debug.WriteLine(string.Format("Cell value pushed at R{0}C{1}: {2}", e.RowIndex, e.ColumnIndex, e.Value)); if (e.RowIndex < source.Count) { Debug.WriteLine(string.Format(" Normal row.")); if (pairInEdit == null) { Debug.WriteLine(string.Format(" pairInEdit was null")); var we = source[e.RowIndex]; pairInEdit = new WordListEntry(null, we.Phrase, we.Translation); } else { Debug.WriteLine(string.Format(" pairInEdit was non-null")); } rowSource = pairInEdit; rowInEdit = e.RowIndex; } else { Debug.WriteLine(string.Format(" New row.")); //If the user previously deleted the row for new data (for example, by //cancelling the row edit) the pairInEdit will be null. if (pairInEdit == null) { pairInEdit = new WordListEntry(null); rowInEdit = e.RowIndex; Debug.WriteLine(string.Format(" pairInEdit was null")); } else Debug.WriteLine(string.Format(" pairInEdit was non-null")); rowSource = pairInEdit; } //If the row was emptied, the Value property may be null! if (e.ColumnIndex == 0) rowSource.Phrase = e.Value != null ? e.Value.ToString() : ""; else rowSource.Translation = e.Value != null ? e.Value.ToString() : ""; dirty = true; }
public Insertion(SqliteWordList owner, int index, WordListEntry item) : base(owner) { this.index = index; this.item = item; }
//Create a new TranslationPair when the user edits the row for new records. void grid_NewRowNeeded(object sender, DataGridViewRowEventArgs e) { pairInEdit = new WordListEntry(null, null, null); rowInEdit = grid.Rows.Count - 1; Debug.Print("New row added to the grid: e.Row = {0}, rowInEdit = {1}", e.Row, rowInEdit.Value); }