Пример #1
0
        public bool Contains(SelectionCellRangeWithItems rangeWithItems)
        {
            if ((m_unsortedRanges.Count <= 0) || (SelectedCellsStorage.IsEmpty(rangeWithItems)))
            {
                return(false);
            }

            if (rangeWithItems.Length == 1)
            {
                return(this.IndexOfOverlap(rangeWithItems).Any());
            }

            var store = new SelectedCellsStorage(null);

            store.Add(rangeWithItems);

            foreach (var match in this.IndexOfOverlap(rangeWithItems))
            {
                store.Remove(m_unsortedRanges[match].Value);

                if (store.Count == 0)
                {
                    return(true);
                }
            }

            return(false);
        }
Пример #2
0
        public bool SelectCells(SelectionCellRangeWithItems cellRangeWithItems)
        {
            SelectionRange itemRange = cellRangeWithItems.ItemRange;

            if (itemRange.IsEmpty)
            {
                throw new ArgumentException("cellRangeWithItems.ItemRange can't be empty", "cellRangeWithItems");
            }

            if (cellRangeWithItems.Length == 1)
            {
                if (!m_cellsToUnselect.Remove(cellRangeWithItems))
                {
                    if (m_owner.SelectedCellsStore.Contains(cellRangeWithItems))
                    {
                        return(false);
                    }

                    if (m_cellsToSelect.Contains(cellRangeWithItems.CellRange))
                    {
                        return(false);
                    }

                    this.m_cellsToSelect.Add(cellRangeWithItems);
                }

                return(true);
            }
            else
            {
                bool selectionChanged = m_cellsToUnselect.Remove(cellRangeWithItems);

                SelectedCellsStorage tempStorage = new SelectedCellsStorage(m_owner);
                tempStorage.Add(cellRangeWithItems);

                // Remove the currently selected item from the new range to select
                foreach (SelectionCellRangeWithItems existingSelectionCellRangeWithItems in m_owner.SelectedCellsStore)
                {
                    tempStorage.Remove(existingSelectionCellRangeWithItems);
                }

                // Remove the pending item to be selected from the new range to select
                foreach (SelectionCellRangeWithItems existingSelectionCellRangeWithItems in m_cellsToSelect)
                {
                    tempStorage.Remove(existingSelectionCellRangeWithItems);
                }

                if (tempStorage.Count > 0)
                {
                    selectionChanged = true;

                    foreach (SelectionCellRangeWithItems cellRangeWithItemsToAdd in tempStorage)
                    {
                        m_cellsToSelect.Add(cellRangeWithItemsToAdd);
                    }
                }

                return(selectionChanged);
            }
        }
Пример #3
0
        private void RemoveAt(int index, bool repairIndex)
        {
            var wrapper        = m_ranges[index];
            var rangeWithItems = wrapper.Value;

            Debug.Assert(wrapper.Index == index);

            if (!SelectedCellsStorage.IsEmpty(rangeWithItems))
            {
                var removed = m_map.Remove(SelectedCellsStorage.GetArea(rangeWithItems), wrapper);
                Debug.Assert(removed, "Failed to remove the selection range.");

                // Since there should be only a single instance of the wrapper within the collection, try an altenate strategy.
                if (!removed)
                {
                    var entry = m_map.FirstOrDefault(e => e.Item == wrapper);
                    if (entry.Item == wrapper)
                    {
                        removed = m_map.Remove(entry);
                    }

                    Debug.Assert(removed, "Failed to find the selection range.");
                }
            }

            m_ranges.RemoveAt(index);

            if (repairIndex)
            {
                this.RepairIndex(index);
            }
        }
Пример #4
0
        public bool Contains(SelectionCellRange range)
        {
            if ((m_unsortedRanges.Count <= 0) || (SelectedCellsStorage.IsEmpty(range)))
            {
                return(false);
            }

            return(this.Contains(new SelectionCellRangeWithItems(range.ItemRange, null, range.ColumnRange)));
        }
Пример #5
0
 internal SelectionChanger(DataGridContext owner)
 {
     m_owner           = owner;
     m_itemsToSelect   = new SelectedItemsStorage(owner);
     m_itemsToUnselect = new SelectedItemsStorage(owner);
     m_cellsToSelect   = new SelectedCellsStorage(owner);
     m_cellsToUnselect = new SelectedCellsStorage(owner);
     m_toDeferSelect   = new List <object>(1);
     m_sourceChanges   = new List <SourceChangeInfo>(2);
 }
Пример #6
0
 public SelectionChanger( DataGridContext owner )
 {
   m_owner = owner;
   m_itemsToSelect = new SelectedItemsStorage( owner, 2 );
   m_itemsToUnselect = new SelectedItemsStorage( owner, 2 );
   m_cellsToSelect = new SelectedCellsStorage( owner, 2 );
   m_cellsToUnselect = new SelectedCellsStorage( owner, 2 );
   m_toDeferSelect = new List<object>( 1 );
   m_sourceChanges = new List<SourceChangeInfo>( 2 );
 }
Пример #7
0
        public object Clone()
        {
            var copy = new SelectedCellsStorage(m_dataGridContext);

            for (int i = 0; i < m_unsortedRanges.Count; i++)
            {
                copy.Insert(i, m_unsortedRanges[i].Value, false);
            }

            return(copy);
        }
Пример #8
0
        public bool Remove(SelectionCellRangeWithItems rangeWithItems)
        {
            if (SelectedCellsStorage.IsEmpty(rangeWithItems))
            {
                return(true);
            }

            // It improves performance to leave early and prevent empty enumerators creation.
            if (m_ranges.Count <= 0)
            {
                return(false);
            }

            // The SelectionCellRange.IsEmpty should be mapped to the item range's SelectionRange.IsEmpty property.
            Debug.Assert(!rangeWithItems.ItemRange.IsEmpty);

            var matches       = this.IndexOfOverlap(rangeWithItems).OrderBy(index => index).ToList();
            var rangeToRemove = rangeWithItems.CellRange;

            for (int i = matches.Count - 1; i >= 0; i--)
            {
                var index = matches[i];
                var currentRangeWithItems = m_ranges[index].Value;
                var currentRange          = currentRangeWithItems.CellRange;
                var overlap = rangeToRemove.Intersect(currentRange);

                Debug.Assert(!overlap.IsEmpty);

                var newRanges = currentRange.Exclude(overlap);

                if (newRanges.Length == 0)
                {
                    this.RemoveAt(index, true);
                }
                else
                {
                    var currentRangeItems = currentRangeWithItems.ItemRangeWithItems;

                    this[index] = new SelectionCellRangeWithItems(newRanges[0].ItemRange, currentRangeItems.GetItems(newRanges[0].ItemRange), newRanges[0].ColumnRange);

                    if (newRanges.Length > 1)
                    {
                        for (int j = 1; j < newRanges.Length; j++)
                        {
                            this.Insert(index + j, new SelectionCellRangeWithItems(newRanges[j].ItemRange, currentRangeItems.GetItems(newRanges[j].ItemRange), newRanges[j].ColumnRange), false);
                        }

                        this.RepairIndex(index + 1);
                    }
                }
            }

            return(matches.Count > 0);
        }
Пример #9
0
            internal ItemComparer(SelectionCellRangeWithItemsWrapper target)
            {
                if (target == null)
                {
                    throw new ArgumentNullException("target");
                }

                if (SelectedCellsStorage.IsEmpty(target.Value))
                {
                    throw new ArgumentException("The selection range must not be empty.", "target");
                }

                m_target = target;
            }
Пример #10
0
        public bool SelectJustThisCell(int itemIndex, object item, int columnIndex)
        {
            bool selectionDone = true;

            m_toDeferSelect.Clear();

            SelectionCellRangeWithItems rangeWithItemsToSelect =
                new SelectionCellRangeWithItems(itemIndex, item, columnIndex);

            SelectionCellRange cellRange = rangeWithItemsToSelect.CellRange;

            if (m_cellsToSelect.Contains(cellRange))
            {
                selectionDone = false;
            }

            m_cellsToSelect.Clear();
            SelectedCellsStorage selectedCellsInChange = m_owner.SelectedCellsStore;

            if (selectedCellsInChange.Contains(cellRange))
            {
                if (!m_cellsToUnselect.Contains(cellRange))
                {
                    selectionDone = false;
                }

                m_cellsToUnselect.Clear();

                foreach (SelectionCellRangeWithItems selectedCellRangeWithItems in selectedCellsInChange)
                {
                    m_cellsToUnselect.Add(selectedCellRangeWithItems);
                }

                m_cellsToUnselect.Remove(rangeWithItemsToSelect);
            }
            else
            {
                m_cellsToSelect.Add(rangeWithItemsToSelect);
                m_cellsToUnselect.Clear();

                foreach (SelectionCellRangeWithItems selectedCellRangeWithItems in selectedCellsInChange)
                {
                    m_cellsToUnselect.Add(selectedCellRangeWithItems);
                }
            }

            this.UnselectAllItems();
            return(selectionDone);
        }
Пример #11
0
        public bool Contains(SelectionCellRangeWithItems cellRangeWithItemsToCompare)
        {
            int count = m_list.Count;
            SelectionCellRange cellRangeToCompare = cellRangeWithItemsToCompare.CellRange;

            int cellRangeLength = cellRangeToCompare.Length;

            // If there is more than one Cell in the range, ensure
            // the range is completely contained within this SelectedCellsStorage
            if (cellRangeLength > 1)
            {
                SelectedCellsStorage cellStorage = new SelectedCellsStorage(null, cellRangeLength);
                cellStorage.Add(cellRangeWithItemsToCompare);

                // The range is completely contained when the store
                // is empty after removing all ranges contained within
                // this SelectedCellsStorage
                for (int i = 0; i < count; i++)
                {
                    SelectionCellRangeWithItems cellRangeWithItems = m_list[i];

                    cellStorage.Remove(cellRangeWithItems);

                    if (cellStorage.Count == 0)
                    {
                        return(true);
                    }
                }
            }
            else
            {
                for (int i = 0; i < count; i++)
                {
                    SelectionCellRangeWithItems rangeWithItem     = m_list[i];
                    SelectionCellRange          rangeIntersection = rangeWithItem.CellRange.Intersect(cellRangeToCompare);

                    if (!rangeIntersection.IsEmpty)
                    {
                        if (cellRangeWithItemsToCompare.ItemRangeWithItems.IsItemsEqual(
                                rangeIntersection.ItemRange, rangeWithItem.ItemRangeWithItems))
                        {
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }
Пример #12
0
 internal SelectionInfo(
     DataGridContext dataGridContext,
     SelectedItemsStorage removedItems,
     SelectedItemsStorage addedItems,
     SelectedCellsStorage removedCells,
     SelectedCellsStorage addedCells)
 {
     this.DataGridContext   = dataGridContext;
     this.AddedItems        = new ReadOnlyCollection <object>(new SelectionItemCollection(addedItems));
     this.RemovedItems      = new ReadOnlyCollection <object>(new SelectionItemCollection(removedItems));
     this.AddedItemRanges   = new ReadOnlyCollection <SelectionRange>(new SelectionItemRangeCollection(addedItems));
     this.RemovedItemRanges = new ReadOnlyCollection <SelectionRange>(new SelectionItemRangeCollection(removedItems));
     this.AddedCellRanges   = new ReadOnlyCollection <SelectionCellRange>(new SelectionCellRangeCollection(addedCells));
     this.RemovedCellRanges = new ReadOnlyCollection <SelectionCellRange>(new SelectionCellRangeCollection(removedCells));
 }
 internal SelectionInfo( 
   DataGridContext dataGridContext, 
   SelectedItemsStorage removedItems, 
   SelectedItemsStorage addedItems,
   SelectedCellsStorage removedCells,
   SelectedCellsStorage addedCells )
 {
   this.DataGridContext = dataGridContext;
   this.AddedItems = new ReadOnlyCollection<object>( new SelectionItemCollection( addedItems ) );
   this.RemovedItems = new ReadOnlyCollection<object>( new SelectionItemCollection( removedItems ) );
   this.AddedItemRanges = new ReadOnlyCollection<SelectionRange>( new SelectionItemRangeCollection( addedItems ) );
   this.RemovedItemRanges = new ReadOnlyCollection<SelectionRange>( new SelectionItemRangeCollection( removedItems ) );
   this.AddedCellRanges = new ReadOnlyCollection<SelectionCellRange>( new SelectionCellRangeCollection( addedCells ) );
   this.RemovedCellRanges = new ReadOnlyCollection<SelectionCellRange>( new SelectionCellRangeCollection( removedCells ) );
 }
Пример #14
0
        private void Insert(int index, SelectionCellRangeWithItems item, bool repairIndex)
        {
            var wrapper = new SelectionCellRangeWithItemsWrapper(item, index);

            m_ranges.Insert(index, wrapper);

            if (repairIndex)
            {
                this.RepairIndex(index + 1);
            }

            if (!SelectedCellsStorage.IsEmpty(item))
            {
                m_map.Add(SelectedCellsStorage.GetArea(item), wrapper);
            }
        }
Пример #15
0
        private IEnumerable <int> IndexOfOverlap(SelectionCellRange target)
        {
            if ((m_sortedRanges.Count <= 0) || SelectedCellsStorage.IsEmpty(target))
            {
                yield break;
            }

            var comparer = new ItemRangeComparer(target.ItemRange);

            var index = this.FindIndex(comparer);

            if (index < 0)
            {
                yield break;
            }

            while (index > 0)
            {
                if (comparer.Compare(m_sortedRanges[index - 1]) != 0)
                {
                    break;
                }

                index--;
            }

            for (int i = index; i < m_sortedRanges.Count; i++)
            {
                var wrapper = m_sortedRanges[i];
                if (comparer.Compare(wrapper) != 0)
                {
                    break;
                }

                var currentRangeWithItems = wrapper.Value;
                var overlap = target.Intersect(currentRangeWithItems.CellRange);

                if (!SelectedCellsStorage.IsEmpty(overlap))
                {
                    yield return(wrapper.Index);
                }
            }
        }
Пример #16
0
        private void Insert(int index, SelectionCellRangeWithItems item, bool repairIndex)
        {
            var wrapper = new SelectionCellRangeWithItemsWrapper(item, index);

            m_unsortedRanges.Insert(index, wrapper);

            if (repairIndex)
            {
                this.RepairIndex(index + 1);
            }

            if (!SelectedCellsStorage.IsEmpty(item))
            {
                var insertionIndex = ~this.FindIndex(new ItemComparer(wrapper));
                Debug.Assert(insertionIndex >= 0);

                m_sortedRanges.Insert(insertionIndex, wrapper);
            }
        }
Пример #17
0
        private void RemoveAt(int index, bool repairIndex)
        {
            var wrapper = m_unsortedRanges[index];

            Debug.Assert(wrapper.Index == index);

            if (!SelectedCellsStorage.IsEmpty(wrapper.Value))
            {
                var removalIndex = this.FindIndex(new ItemComparer(wrapper));
                Debug.Assert(removalIndex >= 0);

                m_sortedRanges.RemoveAt(removalIndex);
            }

            m_unsortedRanges.RemoveAt(index);

            if (repairIndex)
            {
                this.RepairIndex(index);
            }
        }
Пример #18
0
        private IEnumerable <int> IndexOfOverlap(SelectionCellRange range)
        {
            // It improves performance to leave early.
            if (m_map.Count <= 0)
            {
                yield break;
            }

            foreach (var entry in m_map.GetEntriesWithin(SelectedCellsStorage.GetArea(range)))
            {
                var candidate = entry.Item;
                var target    = candidate.Value;
                var overlap   = range.Intersect(target.CellRange);

                Debug.Assert(!SelectedCellsStorage.IsEmpty(overlap));

                if (!SelectedCellsStorage.IsEmpty(overlap))
                {
                    yield return(candidate.Index);
                }
            }
        }
Пример #19
0
        public bool Contains(SelectionCellRange cellRange)
        {
            int count = m_list.Count;

            int cellRangeLength = cellRange.Length;

            // Ensure every Cells are present in the SelectedCellsStorage
            if (cellRangeLength > 1)
            {
                SelectedCellsStorage cellStorage = new SelectedCellsStorage(null, cellRangeLength);
                cellStorage.Add(new SelectionCellRangeWithItems(cellRange.ItemRange,
                                                                null,
                                                                cellRange.ColumnRange));

                for (int i = 0; i < count; i++)
                {
                    cellStorage.Remove(m_list[i]);

                    if (cellStorage.Count == 0)
                    {
                        return(true);
                    }
                }
            }
            else
            {
                for (int i = 0; i < count; i++)
                {
                    if (!m_list[i].CellRange.Intersect(cellRange).IsEmpty)
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
Пример #20
0
    public SelectionInfo GetSelectionInfo()
    {
      List<SelectionRangeWithItems> removedRangeWithItems;
      List<SelectionRangeWithItems> unselectedItemsFromRemove = this.GetUnselectedItemsFromRemove( out removedRangeWithItems );
      List<SelectionCellRangeWithItems> removedCellsRangeWithItems;
      List<SelectionCellRangeWithItems> unselectedCellsFromRemove = this.GetUnselectedCellsFromRemove( out removedCellsRangeWithItems );

      SelectedItemsStorage itemsToUnselect = new SelectedItemsStorage( m_itemsToUnselect );
      SelectedCellsStorage cellsToUnselect = new SelectedCellsStorage( m_cellsToUnselect );

      foreach( SelectionRangeWithItems rangeWithItems in unselectedItemsFromRemove )
      {
        itemsToUnselect.Add( rangeWithItems );
      }

      foreach( SelectionCellRangeWithItems cellRangeWithItems in unselectedCellsFromRemove )
      {
        cellsToUnselect.Add( cellRangeWithItems );
      }

      return new SelectionInfo(
        m_owner, itemsToUnselect, new SelectedItemsStorage( m_itemsToSelect ),
        cellsToUnselect, new SelectedCellsStorage( m_cellsToSelect ) );
    }
Пример #21
0
 internal SelectedCellsStorage(SelectedCellsStorage collection)
 {
     m_dataGridContext = collection.m_dataGridContext;
     m_list            = new List <SelectionCellRangeWithItems>(collection.m_list);
     m_cellsCount      = collection.m_cellsCount;
 }
 public SelectionCellRangeCollection(SelectedCellsStorage list)
 {
     m_list = list;
 }
Пример #23
0
    public bool ToggleItemCellsSelection(
      DataGridContext dataGridContext,
      int itemIndex,
      object item )
    {
      SelectionRange itemRange = new SelectionRange( itemIndex );

      SelectionRange cellRange = new SelectionRange( 0,
        Math.Max( 0, dataGridContext.Columns.Count - 1 ) );

      // Select all visible cells for this itemIndex
      SelectionCellRangeWithItems selection = new SelectionCellRangeWithItems( itemRange,
        new object[] { item },
        cellRange );

      SelectedCellsStorage tempStorage = new SelectedCellsStorage( null, 1 );
      tempStorage.Add( selection );

      foreach( ColumnBase column in dataGridContext.ColumnsByVisiblePosition )
      {
        if( !column.Visible )
          tempStorage.Remove( new SelectionCellRangeWithItems( itemIndex, item, column.VisiblePosition ) );
      }

      int tempStorageCount = tempStorage.Count;
      bool allCellSelected = true;

      foreach( SelectionCellRangeWithItems allCellSelection in tempStorage )
      {
        if( !dataGridContext.SelectedCellsStore.Contains( allCellSelection ) )
        {
          allCellSelected = false;
          break;
        }
      }

      bool selectionDone = true;

      if( allCellSelected )
      {
        foreach( SelectionCellRangeWithItems allCellSelection in tempStorage )
        {
          selectionDone &= this.UnselectCells( dataGridContext, allCellSelection );
        }
      }
      else
      {
        foreach( SelectionCellRangeWithItems allCellSelection in tempStorage )
        {
          selectionDone &= this.SelectCells( dataGridContext, allCellSelection );
        }
      }

      return selectionDone;
    }
Пример #24
0
 internal SelectedCellsStorage( SelectedCellsStorage collection )
 {
   m_dataGridContext = collection.m_dataGridContext;
   m_list = new List<SelectionCellRangeWithItems>( collection.m_list );
   m_cellsCount = collection.m_cellsCount;
 }
Пример #25
0
    public bool UnselectCells( SelectionCellRangeWithItems cellRangeWithItems )
    {
      SelectionRange itemRange = cellRangeWithItems.ItemRange;
      SelectionRange columnRange = cellRangeWithItems.ColumnRange;
      SelectionCellRange cellRange = cellRangeWithItems.CellRange;

      if( itemRange.IsEmpty )
      {
        // We have no index we have to remove based on item
        bool selectionChanged = false;

        List<SelectionCellRangeWithItems> cellsRangeToRemove = new List<SelectionCellRangeWithItems>();
        List<object> itemsToUnselect = new List<object>( cellRangeWithItems.ItemRangeWithItems.Items );
        int count = itemsToUnselect.Count;

        for( int i = count - 1; i >= 0; i-- )
        {
          object itemToUnselect = itemsToUnselect[ i ];

          foreach( SelectionCellRangeWithItems existingSelectionCellRangeWithItems in m_cellsToSelect )
          {
            SelectionRange columnIntersection = columnRange.Intersect( existingSelectionCellRangeWithItems.ColumnRange );

            if( columnIntersection.IsEmpty )
              continue;

            int index = Array.IndexOf( existingSelectionCellRangeWithItems.ItemRangeWithItems.Items, itemToUnselect );

            if( index > -1 )
            {
              cellsRangeToRemove.Add(
                new SelectionCellRangeWithItems(
                  new SelectionRange( existingSelectionCellRangeWithItems.ItemRange.GetIndexFromItemOffset( index ) ),
                  new object[] { itemToUnselect },
                  columnIntersection ) );
            }
          }
        }

        // Remove the currently unselected item from the new range to select
        foreach( SelectionCellRangeWithItems cellRangeToRemove in cellsRangeToRemove )
        {
          selectionChanged |= m_cellsToSelect.Remove( cellRangeToRemove );
        }

        count = itemsToUnselect.Count;

        for( int i = 0; i < count; i++ )
        {
          object itemToUnselect = itemsToUnselect[ i ];

          foreach( SelectionCellRangeWithItems existingSelectionCellRangeWithItems in m_owner.SelectedCellsStore )
          {
            SelectionRange columnIntersection = columnRange.Intersect( existingSelectionCellRangeWithItems.ColumnRange );

            if( columnIntersection.IsEmpty )
              continue;

            int index = Array.IndexOf( existingSelectionCellRangeWithItems.ItemRangeWithItems.Items, itemToUnselect );

            if( index > -1 )
            {
              index = existingSelectionCellRangeWithItems.ItemRange.GetIndexFromItemOffset( index );

              SelectionCellRange cellRangeTemp = new SelectionCellRange(
                new SelectionRange( existingSelectionCellRangeWithItems.ItemRange.GetIndexFromItemOffset( index ) ),
                columnIntersection );

              if( !m_cellsToUnselect.Contains( cellRangeTemp ) )
              {
                selectionChanged = true;

                m_cellsToUnselect.Add( new SelectionCellRangeWithItems(
                  cellRangeTemp.ItemRange, new object[] { itemToUnselect }, cellRangeTemp.ColumnRange ) );
              }
            }
          }
        }

        return selectionChanged;
      }

      if( cellRangeWithItems.Length == 1 )
      {
        if( !m_cellsToSelect.Remove( cellRangeWithItems ) )
        {
          if( !m_owner.SelectedCellsStore.Contains( cellRange ) )
            return false;

          if( m_cellsToUnselect.Contains( cellRange ) )
            return false;

          m_cellsToUnselect.Add( cellRangeWithItems );
        }

        return true;
      }
      else
      {
        SelectedCellsStorage tempStorage = new SelectedCellsStorage( m_owner, 8 );
        tempStorage.Add( cellRangeWithItems );

        // Remove the currently selected item from the new range to select
        foreach( SelectionCellRangeWithItems existingSelectionCellRangeWithItems in m_cellsToSelect )
        {
          tempStorage.Remove( existingSelectionCellRangeWithItems );
        }

        bool selectionChanged = m_cellsToSelect.Remove( cellRangeWithItems );

        if( tempStorage.Count > 0 )
        {
          selectionChanged = true;

          foreach( SelectionCellRangeWithItems cellRangeWithItemsToAdd in tempStorage )
          {
            Debug.Assert( !m_cellsToUnselect.Contains( cellRangeWithItemsToAdd.CellRange ) );
            m_cellsToUnselect.Add( cellRangeWithItemsToAdd );
          }
        }

        return selectionChanged;
      }
    }
Пример #26
0
        public bool SelectItemCells(
            int itemIndex,
            object item,
            HashedLinkedList <ColumnBase> columnsByVisiblePosition,
            bool preserveSelection)
        {
            bool selectionDone = true;
            int  columnsCount  = columnsByVisiblePosition.Count;

            m_toDeferSelect.Clear();

            SelectionCellRangeWithItems rangeWithItemsToSelect =
                new SelectionCellRangeWithItems(
                    new SelectionRange(itemIndex),
                    new object[] { item },
                    new SelectionRange(0, columnsCount - 1));

            SelectionCellRange cellRange = rangeWithItemsToSelect.CellRange;

            m_cellsToSelect.Clear();
            SelectedCellsStorage selectedCellsInChange = m_owner.SelectedCellsStore;

            // Remove all currently selected Cells from the new selectionRange
            // to avoid duplicate SelectionCellRange
            SelectedCellsStorage tempStorage = new SelectedCellsStorage(null);

            tempStorage.Add(rangeWithItemsToSelect);

            for (int i = 0; i < selectedCellsInChange.Count; i++)
            {
                tempStorage.Remove(selectedCellsInChange[i]);
            }

            foreach (ColumnBase column in columnsByVisiblePosition)
            {
                if (!column.Visible)
                {
                    tempStorage.Remove(new SelectionCellRangeWithItems(itemIndex, item, column.VisiblePosition));
                }
            }

            int tempStorageCount = tempStorage.Count;

            // All Cells are already selected
            if (tempStorageCount == 0)
            {
                if (!m_cellsToUnselect.Contains(cellRange))
                {
                    selectionDone = false;
                }

                m_cellsToUnselect.Clear();

                if (!preserveSelection)
                {
                    foreach (SelectionCellRangeWithItems selectedCellRangeWithItems in selectedCellsInChange)
                    {
                        m_cellsToUnselect.Add(selectedCellRangeWithItems);
                    }
                }

                m_cellsToUnselect.Remove(rangeWithItemsToSelect);
            }
            else
            {
                // Add each range to selection
                for (int i = 0; i < tempStorageCount; i++)
                {
                    m_cellsToSelect.Add(tempStorage[i]);
                }

                m_cellsToUnselect.Clear();

                if (!preserveSelection)
                {
                    foreach (SelectionCellRangeWithItems selectedCellRangeWithItems in selectedCellsInChange)
                    {
                        tempStorage = new SelectedCellsStorage(null);
                        tempStorage.Add(selectedCellRangeWithItems);
                        tempStorage.Remove(rangeWithItemsToSelect);
                        tempStorageCount = tempStorage.Count;

                        for (int i = 0; i < tempStorageCount; i++)
                        {
                            m_cellsToUnselect.Add(tempStorage[i]);
                        }
                    }
                }
            }

            if (!preserveSelection)
            {
                this.UnselectAllItems();
            }

            return(selectionDone);
        }
Пример #27
0
    public bool SelectItemCells(
      int itemIndex,
      object item,
      HashedLinkedList<ColumnBase> columnsByVisiblePosition,
      bool preserveSelection )
    {
      bool selectionDone = true;
      int columnsCount = columnsByVisiblePosition.Count;
      m_toDeferSelect.Clear();

      SelectionCellRangeWithItems rangeWithItemsToSelect =
        new SelectionCellRangeWithItems(
        new SelectionRange( itemIndex ),
        new object[] { item },
        new SelectionRange( 0, columnsCount - 1 ) );

      SelectionCellRange cellRange = rangeWithItemsToSelect.CellRange;

      m_cellsToSelect.Clear();
      SelectedCellsStorage selectedCellsInChange = m_owner.SelectedCellsStore;

      // Remove all currently selected Cells from the new selectionRange
      // to avoid duplicate SelectionCellRange
      SelectedCellsStorage tempStorage = new SelectedCellsStorage( null, 1 );
      tempStorage.Add( rangeWithItemsToSelect );

      for( int i = 0; i < selectedCellsInChange.Count; i++ )
      {
        tempStorage.Remove( selectedCellsInChange[ i ] );
      }

      foreach( ColumnBase column in columnsByVisiblePosition )
      {
        if( !column.Visible )
          tempStorage.Remove( new SelectionCellRangeWithItems( itemIndex, item, column.VisiblePosition ) );
      }

      int tempStorageCount = tempStorage.Count;

      // All Cells are already selected
      if( tempStorageCount == 0 )
      {
        if( !m_cellsToUnselect.Contains( cellRange ) )
          selectionDone = false;

        m_cellsToUnselect.Clear();

        if( !preserveSelection )
        {
          foreach( SelectionCellRangeWithItems selectedCellRangeWithItems in selectedCellsInChange )
          {
            m_cellsToUnselect.Add( selectedCellRangeWithItems );
          }
        }

        m_cellsToUnselect.Remove( rangeWithItemsToSelect );
      }
      else
      {
        // Add each range to selection
        for( int i = 0; i < tempStorageCount; i++ )
        {
          m_cellsToSelect.Add( tempStorage[ i ] );
        }

        m_cellsToUnselect.Clear();

        if( !preserveSelection )
        {
          foreach( SelectionCellRangeWithItems selectedCellRangeWithItems in selectedCellsInChange )
          {
            tempStorage = new SelectedCellsStorage( null, 1 );
            tempStorage.Add( selectedCellRangeWithItems );
            tempStorage.Remove( rangeWithItemsToSelect );
            tempStorageCount = tempStorage.Count;

            for( int i = 0; i < tempStorageCount; i++ )
            {
              m_cellsToUnselect.Add( tempStorage[ i ] );
            }
          }
        }
      }

      if( !preserveSelection )
        this.UnselectAllItems();

      return selectionDone;
    }
Пример #28
0
 private static RSTree2D <SelectionCellRangeWithItemsWrapper> .Area GetArea(SelectionCellRangeWithItems range)
 {
     return(SelectedCellsStorage.GetArea(range.CellRange));
 }
Пример #29
0
        public bool UnselectCells(SelectionCellRangeWithItems cellRangeWithItems)
        {
            SelectionRange     itemRange   = cellRangeWithItems.ItemRange;
            SelectionRange     columnRange = cellRangeWithItems.ColumnRange;
            SelectionCellRange cellRange   = cellRangeWithItems.CellRange;

            if (itemRange.IsEmpty)
            {
                // We have no index we have to remove based on item
                bool selectionChanged = false;

                List <SelectionCellRangeWithItems> cellsRangeToRemove = new List <SelectionCellRangeWithItems>();
                List <object> itemsToUnselect = new List <object>(cellRangeWithItems.ItemRangeWithItems.Items);
                int           count           = itemsToUnselect.Count;

                for (int i = count - 1; i >= 0; i--)
                {
                    object itemToUnselect = itemsToUnselect[i];

                    foreach (SelectionCellRangeWithItems existingSelectionCellRangeWithItems in m_cellsToSelect)
                    {
                        SelectionRange columnIntersection = columnRange.Intersect(existingSelectionCellRangeWithItems.ColumnRange);

                        if (columnIntersection.IsEmpty)
                        {
                            continue;
                        }

                        int index = Array.IndexOf(existingSelectionCellRangeWithItems.ItemRangeWithItems.Items, itemToUnselect);

                        if (index > -1)
                        {
                            cellsRangeToRemove.Add(
                                new SelectionCellRangeWithItems(
                                    new SelectionRange(existingSelectionCellRangeWithItems.ItemRange.GetIndexFromItemOffset(index)),
                                    new object[] { itemToUnselect },
                                    columnIntersection));
                        }
                    }
                }

                // Remove the currently unselected item from the new range to select
                foreach (SelectionCellRangeWithItems cellRangeToRemove in cellsRangeToRemove)
                {
                    selectionChanged |= m_cellsToSelect.Remove(cellRangeToRemove);
                }

                count = itemsToUnselect.Count;

                for (int i = 0; i < count; i++)
                {
                    object itemToUnselect = itemsToUnselect[i];

                    foreach (SelectionCellRangeWithItems existingSelectionCellRangeWithItems in m_owner.SelectedCellsStore)
                    {
                        SelectionRange columnIntersection = columnRange.Intersect(existingSelectionCellRangeWithItems.ColumnRange);

                        if (columnIntersection.IsEmpty)
                        {
                            continue;
                        }

                        int index = Array.IndexOf(existingSelectionCellRangeWithItems.ItemRangeWithItems.Items, itemToUnselect);

                        if (index > -1)
                        {
                            index = existingSelectionCellRangeWithItems.ItemRange.GetIndexFromItemOffset(index);

                            SelectionCellRange cellRangeTemp = new SelectionCellRange(
                                new SelectionRange(existingSelectionCellRangeWithItems.ItemRange.GetIndexFromItemOffset(index)),
                                columnIntersection);

                            if (!m_cellsToUnselect.Contains(cellRangeTemp))
                            {
                                selectionChanged = true;

                                m_cellsToUnselect.Add(new SelectionCellRangeWithItems(
                                                          cellRangeTemp.ItemRange, new object[] { itemToUnselect }, cellRangeTemp.ColumnRange));
                            }
                        }
                    }
                }

                return(selectionChanged);
            }

            if (cellRangeWithItems.Length == 1)
            {
                if (!m_cellsToSelect.Remove(cellRangeWithItems))
                {
                    if (!m_owner.SelectedCellsStore.Contains(cellRange))
                    {
                        return(false);
                    }

                    if (m_cellsToUnselect.Contains(cellRange))
                    {
                        return(false);
                    }

                    m_cellsToUnselect.Add(cellRangeWithItems);
                }

                return(true);
            }
            else
            {
                SelectedCellsStorage tempStorage = new SelectedCellsStorage(m_owner);
                tempStorage.Add(cellRangeWithItems);

                // Remove the currently selected item from the new range to select
                foreach (SelectionCellRangeWithItems existingSelectionCellRangeWithItems in m_cellsToSelect)
                {
                    tempStorage.Remove(existingSelectionCellRangeWithItems);
                }

                bool selectionChanged = m_cellsToSelect.Remove(cellRangeWithItems);

                if (tempStorage.Count > 0)
                {
                    selectionChanged = true;

                    foreach (SelectionCellRangeWithItems cellRangeWithItemsToAdd in tempStorage)
                    {
                        Debug.Assert(!m_cellsToUnselect.Contains(cellRangeWithItemsToAdd.CellRange));
                        m_cellsToUnselect.Add(cellRangeWithItemsToAdd);
                    }
                }

                return(selectionChanged);
            }
        }
        internal SelectionCellRangeCollection(SelectedCellsStorage collection)
        {
            Debug.Assert(collection != null);

            m_storage = collection;
        }
Пример #31
0
    public bool Contains( SelectionCellRange cellRange )
    {
      int count = m_list.Count;

      int cellRangeLength = cellRange.Length;

      // Ensure every Cells are present in the SelectedCellsStorage
      if( cellRangeLength > 1 )
      {
        SelectedCellsStorage cellStorage = new SelectedCellsStorage( null, cellRangeLength );
        cellStorage.Add( new SelectionCellRangeWithItems( cellRange.ItemRange,
          null,
          cellRange.ColumnRange ) );

        for( int i = 0; i < count; i++ )
        {
          cellStorage.Remove( m_list[ i ] );

          if( cellStorage.Count == 0 )
            return true;
        }
      }
      else
      {
        for( int i = 0; i < count; i++ )
        {
          if( !m_list[ i ].CellRange.Intersect( cellRange ).IsEmpty )
            return true;
        }
      }

      return false;
    }
Пример #32
0
    public bool Contains( SelectionCellRangeWithItems cellRangeWithItemsToCompare )
    {
      int count = m_list.Count;
      SelectionCellRange cellRangeToCompare = cellRangeWithItemsToCompare.CellRange;

      int cellRangeLength = cellRangeToCompare.Length;
      // If there is more than one Cell in the range, ensure 
      // the range is completely contained within this SelectedCellsStorage
      if( cellRangeLength > 1 )
      {
        SelectedCellsStorage cellStorage = new SelectedCellsStorage( null, cellRangeLength );
        cellStorage.Add( cellRangeWithItemsToCompare );

        // The range is completely contained when the store 
        // is empty after removing all ranges contained within
        // this SelectedCellsStorage
        for( int i = 0; i < count; i++ )
        {
          SelectionCellRangeWithItems cellRangeWithItems = m_list[ i ];

          cellStorage.Remove( cellRangeWithItems );

          if( cellStorage.Count == 0 )
            return true;
        }
      }
      else
      {
        for( int i = 0; i < count; i++ )
        {
          SelectionCellRangeWithItems rangeWithItem = m_list[ i ];
          SelectionCellRange rangeIntersection = rangeWithItem.CellRange.Intersect( cellRangeToCompare );

          if( !rangeIntersection.IsEmpty )
          {
            if( cellRangeWithItemsToCompare.ItemRangeWithItems.IsItemsEqual(
              rangeIntersection.ItemRange, rangeWithItem.ItemRangeWithItems ) )
            {
              return true;
            }
          }
        }
      }

      return false;
    }
Пример #33
0
        public void UpdateSelectionAfterSourceDataItemReplaced(NotifyCollectionChangedEventArgs e)
        {
            Debug.Assert(e.OldItems.Count == e.NewItems.Count);
            Debug.Assert(e.OldStartingIndex == e.NewStartingIndex);

            SelectedItemsStorage selectedItemsStorage = m_owner.SelectedItemsStore;
            SelectedCellsStorage selectedCellsStorage = m_owner.SelectedCellsStore;
            int   oldItemIndex      = e.OldStartingIndex;
            IList oldItems          = e.OldItems;
            IList newItems          = e.NewItems;
            int   replacedItemCount = oldItems.Count;
            int   cellRangeCount    = selectedCellsStorage.Count;

            if (oldItemIndex >= 0)
            {
                int itemIndex = oldItemIndex;

                for (int i = 0; i < replacedItemCount; i++)
                {
                    object newItem = newItems[i];

                    if (selectedItemsStorage.Contains(itemIndex))
                    {
                        this.UnselectItems(new SelectionRangeWithItems(itemIndex, oldItems[i]));
                        this.SelectItems(new SelectionRangeWithItems(itemIndex, newItem));
                    }

                    SelectionCellRange replacedCellRange = new SelectionCellRange(
                        new SelectionRange(itemIndex), new SelectionRange(0, int.MaxValue - 1));

                    for (int j = 0; j < cellRangeCount; j++)
                    {
                        SelectionCellRangeWithItems cellRangeWithItems = selectedCellsStorage[j];
                        SelectionCellRange          cellRange          = cellRangeWithItems.CellRange;

                        if (!cellRange.Intersect(replacedCellRange).IsEmpty)
                        {
                            object[] items = cellRangeWithItems.ItemRangeWithItems.Items;

                            if (items != null)
                            {
                                items[cellRange.ItemRange.GetOffsetFromItemIndex(itemIndex)] = newItem;
                            }
                        }
                    }

                    itemIndex++;
                }
            }
            else
            {
                CollectionView sourceItems = m_owner.Items;

                for (int i = 0; i < replacedItemCount; i++)
                {
                    object newItem   = newItems[i];
                    int    itemIndex = sourceItems.IndexOf(newItem);

                    if (itemIndex < 0)
                    {
                        continue;
                    }

                    if (selectedItemsStorage.Contains(itemIndex))
                    {
                        this.UnselectItems(new SelectionRangeWithItems(itemIndex, oldItems[i]));
                        this.SelectItems(new SelectionRangeWithItems(itemIndex, newItem));
                    }

                    SelectionCellRange replacedCellRange = new SelectionCellRange(
                        new SelectionRange(itemIndex), new SelectionRange(0, int.MaxValue - 1));

                    for (int j = 0; j < cellRangeCount; j++)
                    {
                        SelectionCellRangeWithItems cellRangeWithItems = selectedCellsStorage[j];
                        SelectionCellRange          cellRange          = cellRangeWithItems.CellRange;

                        if (!cellRange.Intersect(replacedCellRange).IsEmpty)
                        {
                            object[] items = cellRangeWithItems.ItemRangeWithItems.Items;

                            if (items != null)
                            {
                                items[cellRange.ItemRange.GetOffsetFromItemIndex(itemIndex)] = newItem;
                            }
                        }
                    }
                }
            }
        }
Пример #34
0
    public bool SelectCells( SelectionCellRangeWithItems cellRangeWithItems )
    {
      SelectionRange itemRange = cellRangeWithItems.ItemRange;

      if( itemRange.IsEmpty )
        throw new ArgumentException( "cellRangeWithItems.ItemRange can't be empty", "cellRangeWithItems" );

      if( cellRangeWithItems.Length == 1 )
      {
        if( !m_cellsToUnselect.Remove( cellRangeWithItems ) )
        {
          if( m_owner.SelectedCellsStore.Contains( cellRangeWithItems ) )
            return false;

          if( m_cellsToSelect.Contains( cellRangeWithItems.CellRange ) )
            return false;

          this.m_cellsToSelect.Add( cellRangeWithItems );
        }

        return true;
      }
      else
      {
        bool selectionChanged = m_cellsToUnselect.Remove( cellRangeWithItems );

        SelectedCellsStorage tempStorage = new SelectedCellsStorage( m_owner, 8 );
        tempStorage.Add( cellRangeWithItems );

        // Remove the currently selected item from the new range to select
        foreach( SelectionCellRangeWithItems existingSelectionCellRangeWithItems in m_owner.SelectedCellsStore )
        {
          tempStorage.Remove( existingSelectionCellRangeWithItems );
        }

        // Remove the pending item to be selected from the new range to select
        foreach( SelectionCellRangeWithItems existingSelectionCellRangeWithItems in m_cellsToSelect )
        {
          tempStorage.Remove( existingSelectionCellRangeWithItems );
        }

        if( tempStorage.Count > 0 )
        {
          selectionChanged = true;

          foreach( SelectionCellRangeWithItems cellRangeWithItemsToAdd in tempStorage )
          {
            m_cellsToSelect.Add( cellRangeWithItemsToAdd );
          }
        }

        return selectionChanged;
      }
    }