Пример #1
0
        // Sets the value of the current object control
        protected void SetObjectValue()
        {
            ArrayList nodes =
                ObjectTreeNode.GetNodesOfType(_type);
            ComboBox cb = (ComboBox)_valueControl;

            // Get rid of the nodes that refer to duplicate or null
            // objects; for duplicates, choose the node with the
            // shortest path
            ArrayList objects = new ArrayList();

            for (int i = 0; i < nodes.Count;)
            {
                ObjectTreeNode node = (ObjectTreeNode)nodes[i];
                Object         obj  = node.ObjectInfo.Obj;
                if (obj == null)
                {
                    // The remaining nodes move up, so we
                    // don't increment the index
                    nodes.Remove(node);
                    continue;
                }

                ObjNode objNode      = new ObjNode(obj, node);
                ObjNode objNodeFound =
                    (ObjNode)Utils.FindHack(objects, objNode);
                if (objNodeFound == null)
                {
                    objects.Add(objNode);
                    i++;
                }
                else
                {
                    // See if this node is better than the one
                    // previously found, nodes move up in both
                    // cases so don't increment index
                    if (objNode._pathCount < objNodeFound._pathCount)
                    {
                        nodes.Remove(objNodeFound._node);
                        objects.Remove(objNodeFound);
                        objects.Add(objNode);
                    }
                    else
                    {
                        nodes.Remove(node);
                    }
                }
            }

            // Now measure all of the remaining nodes to figure
            // out how big to make the combo box
            Graphics g        = cb.CreateGraphics();
            int      maxWidth = 0;

            foreach (TreeNode node in nodes)
            {
                TraceUtil.WriteLineVerbose(null,
                                           "SetObjectValue looking node: "
                                           + ((BrowserTreeNode)node).GetName());
                int width = (int)g.MeasureString(node.ToString(),
                                                 cb.Font).Width;
                if (width > maxWidth)
                {
                    maxWidth = width;
                }
            }
            g.Dispose();


            // The first value is "null" to allow the object to
            // be set to null
            // Second is Missing.Value to allow it to be optional
            _values.Clear();
            _values.Add(null);
            _values.Add(Missing.Value);
            _values.AddRange(nodes);

            if (maxWidth > cb.Width)
            {
                cb.DropDownWidth = maxWidth;
            }
            cb.Items.Clear();
            cb.Items.Add("null");
            cb.Items.Add("Missing.Value");
            cb.Items.AddRange(nodes.ToArray());

            if (_valueControl2 != null)
            {
                ((TextBox)_valueControl2).Text = "";
            }

            // Select the object if it exists
            if (_value != null)
            {
                bool found = false;
                foreach (ObjectTreeNode node in nodes)
                {
                    if (_value == node.ObjectInfo.Obj)
                    {
                        cb.SelectedItem = node;
                        found           = true;
                        // Enable the combo box
                        cb.Enabled = true;
                        if (_valueControl2 != null)
                        {
                            _valueControl2.Enabled = false;
                        }
                        break;
                    }
                }

                // Just set the text box to be the string value of whatever
                // this is
                if (!found)
                {
                    if (_valueControl2 != null)
                    {
                        cb.Enabled                     = false;
                        _valueControl2.Enabled         = true;
                        ((TextBox)_valueControl2).Text = _value.ToString();
                    }
                }
            }
            else
            {
                // Make the default for optional parameters missing
                // so nothing has to be done to specify them
                if (_optional)
                {
                    cb.Enabled = true;
                    if (_valueControl2 != null)
                    {
                        _valueControl2.Enabled = false;
                    }
                    cb.SelectedIndex = OBJECT_MISSING;
                }
                else
                {
                    if (_valueControl2 != null)
                    {
                        // Enable text
                        cb.Enabled             = false;
                        _valueControl2.Enabled = true;
                    }
                    cb.SelectedIndex = OBJECT_NULL;
                }
            }
        }