/// <summary>
            /// Removes a connector subitem for a child.
            /// </summary>
            /// <param name="child">The child whose connector subitem we want to remove.</param>
            protected void RemoveSubItem(Node child)
            {
                int firstIndex;
                List <SubItemConnector> subitems = CollectSubItems(out firstIndex);

                // find the connector subitem for this child...
                for (int i = 0; i < subitems.Count; ++i)
                {
                    SubItemConnector subitem = subitems[i];

                    // when we found it...
                    if (subitem.Child == child)
                    {
                        // remove the subitem
                        _connectedChildren.Owner.RemoveSubItem(subitem);

                        // if we do not fullfil the minimum count, re add it at the end and clear the child
                        if (subitems.Count - 1 < _minCount)
                        {
                            subitem.Child = null;
                            _connectedChildren.Owner.AddSubItem(subitem, firstIndex + subitems.Count - 1);
                        }

                        // update stored indices on connector subitems
                        RebuildSubItemIndices();

                        return;
                    }
                }

                throw new Exception(Resources.ExceptionSubItemIsNoChild);
            }
            /// <summary>
            /// Generates a list of connector subitems.
            /// </summary>
            /// <param name="firstIndex">The first connector subitem found on the node.</param>
            /// <returns>Returns alist of all connector subitems.</returns>
            protected List <SubItemConnector> CollectSubItems(out int firstIndex)
            {
                List <SubItemConnector> list = new List <SubItemConnector>();

                firstIndex = -1;

                // for each subitem...
                for (int i = 0; i < _connectedChildren.Owner.SubItems.Count; ++i)
                {
                    // check if it is a connector subitem
                    SubItemConnector subconn = _connectedChildren.Owner.SubItems[i] as SubItemConnector;
                    if (subconn != null && subconn.Connector == this)
                    {
                        // remember the index of the first connector subitem found
                        if (firstIndex == -1)
                        {
                            firstIndex = i;
                        }

                        // add subitem to list
                        list.Add(subconn);
                    }
                    else
                    {
                        // subitems of a connector must be next to each other
                        if (firstIndex >= 0)
                        {
                            break;
                        }
                    }
                }

                // check if we have found any subitems
                if (list.Count < 1)
                {
                    throw new Exception(Resources.ExceptionNoSubItemForConnector);
                }

                // check that we have found enough of them and not too many
                Debug.Check(list.Count >= _minCount && list.Count <= _maxCount);

                return(list);
            }
        /// <summary>
        /// Exapnds and collapses the referenced node.
        /// </summary>
        /// <param name="nvdrb">The view data for this node.</param>
        public void ExpandNode(NodeViewDataReferencedBehavior nvdrb)
        {
            // if the referenced behaviours was not yet loaded, try so
            if (_referencedBehavior == null)
            {
                LoadReferencedBehavior();

                // if we could not load it, skip
                if (_referencedBehavior == null)
                {
                    return;
                }
            }

            // this code can be called without a change of IsExpanded so you cannot assume that the current children were from the other state
            if (nvdrb.IsExpanded)
            {
                // clear the generated sub-reference graph
                _genericChildrenLocal.ClearChildren();

                // remove any remaining subitems because of minimum count
                for (int i = 0; i < _subItems.Count; ++i)
                {
                    SubItemConnector subconn = _subItems[i] as SubItemConnector;
                    if (subconn != null && subconn.Connector == _genericChildren)
                    {
                        RemoveSubItem(subconn);
                        --i;
                    }
                }

                // assign the connector of the behaviour
                _genericChildren = _referencedBehavior.GenericChildren;
                _children.SetConnector(_genericChildren);

                // add all the subitems needed
                int count = Math.Max(_genericChildren.ChildCount, _genericChildren.MinCount);
                for (int i = 0; i < count; ++i)
                {
                    Node child = i < _genericChildren.ChildCount ? _genericChildren.GetChild(i) : null;
                    AddSubItem(new SubItemConnector(_genericChildren, child, i));
                }
            }
            else
            {
                // remove all subitems of the behaviour
                for (int i = 0; i < _subItems.Count; ++i)
                {
                    SubItemConnector subconn = _subItems[i] as SubItemConnector;
                    if (subconn != null && subconn.Connector == _genericChildren)
                    {
                        RemoveSubItem(subconn);
                        --i;
                    }
                }

                // assign the connector for the sub-reference graph
                _genericChildren = _genericChildrenLocal;
                _children.SetConnector(_genericChildren);

                // make the connector writable to edit it
                _genericChildren.IsReadOnly = false;

                // add all the subitems needed
                int count = Math.Max(_genericChildren.ChildCount, _genericChildren.MinCount);
                for (int i = 0; i < count; ++i)
                {
                    Node child = i < _genericChildren.ChildCount ? _genericChildren.GetChild(i) : null;
                    AddSubItem(new SubItemConnector(_genericChildren, child, i));
                }

                // clear the generated sub-reference graph
                _genericChildren.ClearChildren();

                // generate the dummy nodes for the sub-referenced behaviours
                foreach (Node child in ((Node)_referencedBehavior).Children)
                {
                    GenerateReferencedBehaviorsTree(nvdrb.RootBehavior, this, child);
                }

                // make the connector read-only so the user cannot modify it
                _genericChildren.IsReadOnly = true;
            }
        }