/// <summary> /// Replace SortObjects corresponding to hvoToReplace with new SortObjects for newObj. /// </summary> /// <param name="newObj"></param> /// <param name="hvoToReplace"></param> /// <param name="fAssumeSame">if true, we'll try to replace sort objects for hvoToReplace with newObj at the same indices. /// if false, we'll rely upon sorter to merge the new item into the right index, or else add to the end. /// Enhance: Is there some way we can compare the sort/filter results for newObj and hvoToReplace that is hvo indepedendent?</param> /// <returns>resulting list of newSortItems added to SortedObjects</returns> protected ArrayList ReplaceListItem(ICmObject newObj, int hvoToReplace, bool fAssumeSame) { ArrayList newSortItems = new ArrayList(); // Note: don't check NeedToReloadVirtualProperty here, so we can update the list, even if we need to // reload it at a later time. This allows joining/breaking wordforms in the Concordance tools, without // necessarily having to reload the entire list. Typically replacements will be with real ids, and those // should be stable to add in the new view. //if (NeedToReloadVirtualProperty) // return newSortItems; // we don't need to update the list, if we're planning to reload the whole thing. List<int> indicesOfSortItemsToRemove = new List<int>(IndicesOfSortItems(new List<int>(new int[] { hvoToReplace }))); ArrayList remainingInsertItems = new ArrayList(); int hvoNewObject = 0; if (newObj != null) { hvoNewObject = newObj.Hvo; // we don't want to add new sort items, if we've already added them, but we do want to allow // a replacement. if (hvoToReplace == hvoNewObject || IndexOfFirstSortItem(new List<int>(new int[] { hvoNewObject })) < 0) MakeItemsFor(newSortItems, newObj); remainingInsertItems = (ArrayList)newSortItems.Clone(); if (fAssumeSame) { //assume we're converting a dummy item to a real one. //In that case, the real item should have same basic content as the dummy item we are replacing, //so we can replace the item at the same sortItem indices. foreach (object itemToInsert in newSortItems) { if (indicesOfSortItemsToRemove.Count > 0) { int iToReplace = indicesOfSortItemsToRemove[0]; SortedObjects.RemoveAt(iToReplace); SortedObjects.Insert(iToReplace, itemToInsert); indicesOfSortItemsToRemove.RemoveAt(0); remainingInsertItems.RemoveAt(0); } else { break; } } } } // Although, ideally, during a dummy conversion there should be a one-to-one correspondence between // the sort items found for the dummy object, and the sort items generated for its real object, // it's possible that at the time we added the dummy item to the record sort list, it didn't // have the same properties matching a filter or sorter as the real item. Try to do the best we // can by removing remaining sort items for the dummy object and then adding any additional sort items // for the real object. // remove the remaining items. indicesOfSortItemsToRemove.Sort(); indicesOfSortItemsToRemove.Reverse(); foreach (int iToRemove in indicesOfSortItemsToRemove) { SortedObjects.RemoveAt(iToRemove); } // add the remaining items. if (m_sorter != null) { m_sorter.MergeInto(SortedObjects, remainingInsertItems); } else { // Add at the end. SortedObjects.AddRange(remainingInsertItems); } // update our current selected hvo, if necessary if (m_hvoCurrent == hvoToReplace) m_hvoCurrent = hvoNewObject; return newSortItems; }
/// <summary> /// This will remove the given hvosToRemove (if they exist in our sort items) and any items that refer to invalid objects. /// Reload the view if there were any changes, and adjust the CurrentIndex /// </summary> protected internal void RemoveUnwantedSortItems(List<int> hvosToRemove) { if (m_sortedObjects == null) return; // nothing to remove. bool fUpdatingListOrig = m_fUpdatingList; m_fUpdatingList = true; try { int currentIndex = CurrentIndex; int cOrigSortObjects = m_sortedObjects.Count; // Note: We start with a Set, since it can't have duplicates. // First remove the given hvos from our sort items. Set<int> unwantedIndices = new Set<int>(IndicesOfSortItems(hvosToRemove)); // then remove any remaining items that point to invalid objects. unwantedIndices.AddRange(IndicesOfInvalidSortItems()); // Put the now unique indices into a list, // so we can make sure they are processed in reverse order. List<int> sortedIndices = new List<int>(unwantedIndices.ToArray()); sortedIndices.Sort(); sortedIndices.Reverse(); foreach (int indexOfSortItem in sortedIndices) { if (indexOfSortItem >= 0) { m_sortedObjects.RemoveAt(indexOfSortItem); if (indexOfSortItem < currentIndex || SortedObjects.Count <= currentIndex) currentIndex--; } } if (m_sortedObjects.Count == 0) currentIndex = -1; else if (currentIndex >= m_sortedObjects.Count) currentIndex = m_sortedObjects.Count - 1; CurrentIndex = currentIndex; if (m_sortedObjects.Count != cOrigSortObjects) { SendPropChangedOnListChange(CurrentIndex, SortedObjects, ListChangedEventArgs.ListChangedActions.Normal); } } finally { m_fUpdatingList = fUpdatingListOrig; } }