Beispiel #1
0
        public GeneratorNode CreateItemsGeneratorNode(
            IList collection,
            GeneratorNode parent,
            GeneratorNode previous,
            GeneratorNode next,
            CustomItemContainerGenerator generator)
        {
            Debug.Assert(collection != null, "collection cannot be null for CreateItemsGeneratorNode()");
            Debug.Assert(generator != null);

            INotifyCollectionChanged notifyCollection = collection as INotifyCollectionChanged;

            Debug.Assert(notifyCollection != null, "collection must be a INotifyCollectionChanged for CreateItemsGeneratorNode()");

            ItemCollection itemCollection = collection as ItemCollection;

            if (itemCollection != null)
            {
                DataGridCollectionView dgcv = itemCollection.SourceCollection as DataGridCollectionView;
                if (dgcv != null)
                {
                    collection = dgcv;
                }
            }

            ItemsGeneratorNode node = new ItemsGeneratorNode(collection, parent);

            this.SetupCollectionGeneratorNode(node, parent, previous, next);


            node.AdjustLeafCount(node.Items.Count);

            return(node);
        }
Beispiel #2
0
        public void CleanGeneratorNode(GeneratorNode node)
        {
            HeadersFootersGeneratorNode headersFootersNode = node as HeadersFootersGeneratorNode;

            if (headersFootersNode != null)
            {
                this.CleanHeadersFootersNotification(headersFootersNode);
            }
            else
            {
                ItemsGeneratorNode itemsNode = node as ItemsGeneratorNode;
                if (itemsNode != null)
                {
                    this.UnregisterNodeCollectionChanged(( INotifyCollectionChanged )itemsNode.Items);
                    itemsNode.CollectionChanged -= m_itemsChangedHandler;
                }
                else
                {
                    GroupGeneratorNode groupNode = node as GroupGeneratorNode;
                    if (groupNode != null)
                    {
                        IList <object> subItems = groupNode.CollectionViewGroup.GetItems();

                        this.UnregisterNodeCollectionChanged(( INotifyCollectionChanged )subItems);

                        groupNode.CollectionChanged  -= m_groupsChangedHandler;
                        groupNode.IsExpandedChanging -= m_isExpandedChangingHandler;
                        groupNode.IsExpandedChanged  -= m_isExpandedChangedHandler;
                    }
                }
            }

            node.ExpansionStateChanged -= m_expansionStateChangedHandler;

            node.CleanGeneratorNode();
        }
Beispiel #3
0
        private static void ProcessItemsNodeBlockVisit(
            ItemsGeneratorNode itemsNode,
            DataGridContext sourceContext,
            int minIndex,
            int maxIndex,
            IDataGridContextVisitor visitor,
            DataGridContextVisitorType visitorType,
            bool visitDetails,
            bool containsDetails,
            int sourceDataItemIndex,
            ref int startSourceDataItemIndex,
            ref int endSourceDataItemIndex,
            ref bool stopVisit)
        {
            if (maxIndex < minIndex)
            {
                return;
            }

            int runningIndex = minIndex;

            sourceDataItemIndex += minIndex - itemsNode.CountDetailsBeforeGlobalIndex(minIndex);

            if (!containsDetails)
            {
                // If we contains no detail, we take a quick way out of it.
                if (startSourceDataItemIndex == -1)
                {
                    startSourceDataItemIndex = sourceDataItemIndex;
                }
                else
                {
                    if ((endSourceDataItemIndex + 1) != sourceDataItemIndex)
                    {
                        visitor.Visit(sourceContext, startSourceDataItemIndex, endSourceDataItemIndex, ref stopVisit);

                        if (stopVisit)
                        {
                            return;
                        }

                        startSourceDataItemIndex = sourceDataItemIndex;
                    }
                }

                endSourceDataItemIndex = sourceDataItemIndex + System.Math.Min(maxIndex - minIndex, itemsNode.Items.Count - 1);
                return;
            }

            int masterIndex;
            int detailStartIndex;
            int detailNodeIndex;

            while (runningIndex <= maxIndex)
            {
                DetailGeneratorNode detailNode = itemsNode.GetDetailNodeForIndex(runningIndex, out masterIndex, out detailStartIndex, out detailNodeIndex);

                if (detailNode != null)
                {
                    int detailEndIndex = System.Math.Min(detailNode.ItemCount - 1, detailStartIndex + (maxIndex - runningIndex));
                    sourceDataItemIndex -= detailStartIndex;
                    bool visitWasStopped;

                    (( IDataGridContextVisitable )detailNode.DetailGenerator).AcceptVisitor(
                        detailStartIndex, detailEndIndex, visitor, visitorType, visitDetails, out visitWasStopped);

                    stopVisit = stopVisit || visitWasStopped;

                    if (stopVisit)
                    {
                        break;
                    }

                    runningIndex += detailNode.ItemCount - detailStartIndex - 1;
                }
                else
                {
                    // set the first data index that will be visited for that items block
                    if (startSourceDataItemIndex == -1)
                    {
                        startSourceDataItemIndex = sourceDataItemIndex;
                        endSourceDataItemIndex   = sourceDataItemIndex;
                    }
                    else
                    {
                        if ((endSourceDataItemIndex + 1) != sourceDataItemIndex)
                        {
                            visitor.Visit(sourceContext, startSourceDataItemIndex, endSourceDataItemIndex, ref stopVisit);

                            if (stopVisit)
                            {
                                break;
                            }

                            startSourceDataItemIndex = sourceDataItemIndex;
                        }

                        endSourceDataItemIndex = sourceDataItemIndex;
                    }

                    sourceDataItemIndex++;
                }

                runningIndex++;
            }
        }
Beispiel #4
0
        public void ProcessVisit(
            DataGridContext sourceContext,
            int minIndex,
            int maxIndex,
            IDataGridContextVisitor visitor,
            DataGridContextVisitorType visitorType,
            bool visitDetails,
            out bool visitWasStopped)
        {
            visitWasStopped = false;

            // This is used only for DataGridContextVisitorType.ItemsBlock
            int startSourceDataItemIndex = -1;
            int endSourceDataItemIndex   = -1;

            if (minIndex < 0)
            {
                DataGridException.ThrowSystemException("The minimum index must be greater than or equal to zero.",
                                                       typeof(ArgumentException), sourceContext.DataGridControl.Name, "minIndex");
            }

            if ((visitorType & DataGridContextVisitorType.DataGridContext) == DataGridContextVisitorType.DataGridContext)
            {
                visitor.Visit(sourceContext, ref visitWasStopped);

                if (visitWasStopped)
                {
                    return;
                }
            }

            //Take a shortcut, if the visit is made only for contexts, and there is no child contexts
            //return right away.
            bool containsDetails = false;

            foreach (DataGridContext childContext in sourceContext.GetChildContexts())
            {
                containsDetails = true;
                break;
            }

            bool processed = false;

            do
            {
                //resets the flag that indicates if the node was already processed
                processed = false;

                int itemCount = this.CurrentNode.ItemCount;

                //If the whole current node is below the minIndex, jump over it.
                if ((this.Index + (itemCount - 1)) < minIndex)
                {
                    processed = true;
                }

                //when the index to visit eNequeos the range defined, exit the loop.
                if (this.Index > maxIndex)
                {
                    break;
                }

                int minForNode = System.Math.Max(0, minIndex - this.Index);             // this will give the base offset within the node where to start the visitating!
                int maxForNode = System.Math.Min(itemCount - 1, maxIndex - this.Index); //this will five the max offset within this node to visit (protected against overlfow )

                if (!processed)
                {
                    HeadersFootersGeneratorNode headersNode = this.CurrentNode as HeadersFootersGeneratorNode;

                    if (headersNode != null)
                    {
                        bool isHeaderFooter = (headersNode.Parent == null);

                        //If the node is a Headers or Footers node AND the visitorType does not contain HeadersFooters
                        if ((isHeaderFooter) && ((visitorType & DataGridContextVisitorType.HeadersFooters) == DataGridContextVisitorType.HeadersFooters))
                        {
                            GeneratorNodeHelper.ProcessHeadersNodeVisit(headersNode, sourceContext, minForNode, maxForNode, visitor, ref visitWasStopped);
                        }
                        else if ((!isHeaderFooter) && ((visitorType & DataGridContextVisitorType.GroupHeadersFooters) == DataGridContextVisitorType.GroupHeadersFooters))
                        {
                            GeneratorNodeHelper.ProcessHeadersNodeVisit(headersNode, sourceContext, minForNode, maxForNode, visitor, ref visitWasStopped);
                        }

                        processed = true;
                    }
                }

                if (!processed)
                {
                    ItemsGeneratorNode itemsNode = this.CurrentNode as ItemsGeneratorNode;

                    if (itemsNode != null)
                    {
                        if ((visitorType & DataGridContextVisitorType.ItemsBlock) == DataGridContextVisitorType.ItemsBlock)
                        {
                            GeneratorNodeHelper.ProcessItemsNodeBlockVisit(
                                itemsNode, sourceContext,
                                minForNode, maxForNode,
                                visitor, visitorType, visitDetails, containsDetails, m_sourceDataIndex,
                                ref startSourceDataItemIndex, ref endSourceDataItemIndex, ref visitWasStopped);
                        }
                        else if (((visitDetails) && (containsDetails)) ||
                                 ((visitorType & DataGridContextVisitorType.Items) == DataGridContextVisitorType.Items))
                        {
                            GeneratorNodeHelper.ProcessItemsNodeVisit(
                                itemsNode, sourceContext,
                                minForNode, maxForNode,
                                visitor, visitorType, visitDetails, m_sourceDataIndex, ref visitWasStopped);
                        }

                        processed = true;
                    }
                }

                if (!processed)
                {
                    GroupGeneratorNode groupNode = this.CurrentNode as GroupGeneratorNode;

                    if (groupNode != null)
                    {
                        if ((visitorType & DataGridContextVisitorType.Groups) == DataGridContextVisitorType.Groups)
                        {
                            visitor.Visit(
                                sourceContext,
                                groupNode.CollectionViewGroup,
                                groupNode.NamesTree,
                                groupNode.Level,
                                groupNode.IsExpanded,
                                groupNode.IsComputedExpanded,
                                ref visitWasStopped);
                        }

                        processed = true;
                    }
                }

                if (!processed)
                {
                    throw new DataGridInternalException("Unable to process the visit.", sourceContext.DataGridControl);
                }

                if (visitWasStopped)
                {
                    break;
                }

                if (this.MoveToChild())
                {
                    continue;
                }

                if (this.MoveToFollowing())
                {
                    continue;
                }

                break;
            }while(true); //loop is controled by continue and break statements.


            if ((visitorType & DataGridContextVisitorType.ItemsBlock) == DataGridContextVisitorType.ItemsBlock)
            {
                if (startSourceDataItemIndex != -1)
                {
                    bool stopVisit = false;
                    visitor.Visit(sourceContext, startSourceDataItemIndex, endSourceDataItemIndex, ref stopVisit);
                    visitWasStopped = visitWasStopped || stopVisit;
                }
            }
        }
Beispiel #5
0
        //FindItem skips over itemless nodes.
        public int FindItem(object item)
        {
            //finding items can only be done in "forward" direction
            int retval = -1;

            GeneratorNode originalNode            = m_currentNode;
            int           originalIndex           = m_index;
            int           originalSourceDataIndex = m_sourceDataIndex;

            while (retval == -1)
            {
                ItemsGeneratorNode itemsNode = m_currentNode as ItemsGeneratorNode;

                if (itemsNode != null)
                {
                    int tmpIndex = itemsNode.Items.IndexOf(item);
                    if (tmpIndex > -1)
                    {
                        tmpIndex += itemsNode.CountDetailsBeforeDataIndex(tmpIndex);
                        //item is directly from this items node... then return the appropriate index!
                        retval = m_index + tmpIndex;
                        break;
                    }
                    else
                    {
                        //if the item is from a detail, then I don't want to "use" it!!!
                        retval = -1;
                        //but continue looping.... to find occurances of this item somewhere else in the tree
                    }
                }
                else
                {
                    CollectionGeneratorNode collectionNode = m_currentNode as CollectionGeneratorNode;

                    if (collectionNode != null)
                    {
                        int tmpIndex = collectionNode.IndexOf(item);

                        if (tmpIndex != -1)
                        {
                            retval = m_index + tmpIndex;
                            break;
                        }
                    }
                }

                //if we reach this point, it's because the item we are looking
                //for is not in this node... Try to access the child
                if (this.MoveToChild())
                {
                    continue;
                }

                //if we reach this point, it's because we have no child...
                if (this.MoveToNext())
                {
                    continue;
                }

                //final try, try "advancing" to the next item.
                if (this.MoveToFollowing())
                {
                    continue;
                }

                //up to this, we are in an endpoint, we failed.
                break;
            }

            if (retval == -1)
            {
                m_currentNode     = originalNode;
                m_index           = originalIndex;
                m_sourceDataIndex = originalSourceDataIndex;
            }

            return(retval);
        }
Beispiel #6
0
        private static void ProcessItemsNodeVisit(
            ItemsGeneratorNode itemsNode,
            DataGridContext sourceContext,
            int minIndex,
            int maxIndex,
            IDataGridContextVisitor visitor,
            DataGridContextVisitorType visitorType,
            bool visitDetails,
            int sourceDataItemIndex,
            ref bool stopVisit)
        {
            int runningIndex = minIndex;

            sourceDataItemIndex += minIndex;

            int masterIndex;
            int detailStartIndex;
            int detailNodeIndex;

            while (runningIndex <= maxIndex)
            {
                DetailGeneratorNode detailNode = itemsNode.GetDetailNodeForIndex(runningIndex, out masterIndex, out detailStartIndex, out detailNodeIndex);

                if (detailNode != null)
                {
                    int detailEndIndex = System.Math.Min(detailNode.ItemCount - 1, detailStartIndex + (maxIndex - runningIndex));
                    sourceDataItemIndex -= detailStartIndex;

                    if (visitDetails)
                    {
                        bool visitWasStopped;

                        (( IDataGridContextVisitable )detailNode.DetailGenerator).AcceptVisitor(
                            detailStartIndex, detailEndIndex, visitor, visitorType, visitDetails, out visitWasStopped);

                        stopVisit = stopVisit || visitWasStopped;

                        if (stopVisit)
                        {
                            break;
                        }

                        runningIndex += detailNode.ItemCount - detailStartIndex - 1;
                    }
                }
                else
                {
                    if ((visitorType & DataGridContextVisitorType.Items) == DataGridContextVisitorType.Items)
                    {
                        object dataItem = itemsNode.GetAt(runningIndex);
                        visitor.Visit(sourceContext, sourceDataItemIndex, dataItem, ref stopVisit);

                        if (stopVisit)
                        {
                            break;
                        }
                    }

                    sourceDataItemIndex++;
                }

                runningIndex++;
            }
        }