Exemplo n.º 1
0
        /// <summary>
        /// Add the children nodes of a particular node in the tree.
        /// While adding nodes if a match is found for objToSelect then nodeRepresentingCurrentChoice assigned to it
        /// and is returned. Otherwise if no node in the tree matches objToSelect this method returns null.
        /// </summary>
        public virtual LabelNode AddChildrenAndLookForSelected(ICmObject objToSelect,
                                                               Stack <ICmObject> ownershipStack, IEnumerable <ICmObject> chosenObjs)
        {
            LabelNode nodeRepresentingCurrentChoice = null;
            // JohnT: if this.Nodes[0] is not a LabelNode, it is a dummy node we added so that
            // its parent LOOKS like something we can expand. That is the usual case for a node
            // we can expand. Therefore finding one of those, or finding more or less than one
            // node, is evidence that we haven't previously computed the real children of this,
            // and should do so.
            bool fExpanded = Nodes.Count != 1 || (Nodes[0] as LabelNode) != null;

            if (!fExpanded)
            {
                Nodes.Clear();
                nodeRepresentingCurrentChoice = AddSecondaryNodesAndLookForSelected(this,
                                                                                    Nodes, nodeRepresentingCurrentChoice, objToSelect, ownershipStack, chosenObjs);
                foreach (ObjectLabel label in ((ObjectLabel)Tag).SubItems)
                {
                    if (!WantNodeForLabel(label))
                    {
                        continue;
                    }
                    LabelNode node = Create(label, m_stylesheet, m_displayUsage);
                    if (chosenObjs != null)
                    {
                        node.Checked = chosenObjs.Contains(label.Object);
                    }
                    Nodes.Add(node);
                    nodeRepresentingCurrentChoice = CheckForSelection(label, objToSelect,
                                                                      node, nodeRepresentingCurrentChoice);
                    nodeRepresentingCurrentChoice = AddSecondaryNodesAndLookForSelected(
                        node, node.Nodes, nodeRepresentingCurrentChoice, objToSelect,
                        ownershipStack, chosenObjs);
                }
            }
            else
            {
                // Even if we don't have to create children for this, we need to search the
                // children for matches, and perhaps expand some of them.
                foreach (LabelNode node in Nodes)
                {
                    nodeRepresentingCurrentChoice = CheckForSelection(node.Label,
                                                                      objToSelect, node, nodeRepresentingCurrentChoice);
                }
            }
            if (nodeRepresentingCurrentChoice == null)
            {
                foreach (LabelNode node in Nodes)
                {
                    if (ownershipStack.Contains(node.Label.Object))
                    {
                        nodeRepresentingCurrentChoice = node.AddChildrenAndLookForSelected(
                            objToSelect, ownershipStack, chosenObjs);
                        return(nodeRepresentingCurrentChoice);
                    }
                }
            }
            else
            {
                Expand();
                nodeRepresentingCurrentChoice.EnsureVisible();
            }
            return(nodeRepresentingCurrentChoice);
        }