private void SortTreeNodes(TreeNodeCollection treeNodes)
        {
            var sorted = true;

            foreach (TreeNode treeNode in treeNodes)
            {
                SortTreeNodes(treeNode.ChildNodes);
            }

            do
            {
                sorted = true;

                for (var i = 0; i < treeNodes.Count - 1; i++)
                {
                    var treeNode1 = treeNodes[i];
                    var treeNode2 = treeNodes[i + 1];

                    if (treeNode1.Text.CompareTo(treeNode2.Text) > 0)
                    {
                        treeNodes.RemoveAt(i + 1);
                        treeNodes.RemoveAt(i);

                        treeNodes.AddAt(i, treeNode2);
                        treeNodes.AddAt(i + 1, treeNode1);

                        sorted = false;
                    }
                }
            } while (!sorted);
        }
        public void TreeNodeCollection_Method_AddAt()
        {
            TreeNodeCollection tnc = new TreeNodeCollection();

            tnc.Add(new TreeNode());
            tnc.Add(new TreeNode());
            Assert.AreEqual(2, tnc.Count, "BeforeAddAt");
            tnc.AddAt(1, new TreeNode("TreeNodeName"));
            Assert.AreEqual(3, tnc.Count, "AfterAddAt1");
            Assert.AreEqual("TreeNodeName", tnc[1].Text, "AfterAdd2");
        }
    protected void ButtonRemove_Click(object sender, EventArgs e)
    {
        TreeNode node = TreeViewAutomation.SelectedNode;

        if (node != null)
        {
            string   pathString = node.ValuePath;
            string[] pathArray  = pathString.Split('/');

            IAutomation parentAutomation   = null;
            string      lastPathName       = string.Empty;
            IAutomation selectedAutomation = _automation;
            foreach (string pathName in pathArray)
            {
                if (pathName.Length > 0)
                {
                    lastPathName     = pathName;
                    parentAutomation = selectedAutomation;
                    switch (pathName)
                    {
                    case "0":
                        selectedAutomation = selectedAutomation.Failure;
                        break;

                    case "1":
                        selectedAutomation = selectedAutomation.Success;
                        break;

                    case "2":
                        selectedAutomation = selectedAutomation.Complete;
                        break;
                    }
                }
            }

            string title = string.Empty;
            if (parentAutomation != null)
            {
                switch (lastPathName)
                {
                case "0":
                    title = "failure: ";
                    parentAutomation.Failure = null;
                    break;

                case "1":
                    title = "success: ";
                    parentAutomation.Success = null;
                    break;

                case "2":
                    title = "complete: ";
                    parentAutomation.Complete = null;
                    break;
                }
            }
            else
            {
                title = "Start: ";
            }

            TreeNode parentNode = node.Parent;
            if (parentNode != null)
            {
                TreeNodeCollection nodes = parentNode.ChildNodes;
                int index = nodes.IndexOf(node);
                nodes.Remove(node);
                nodes.AddAt(index, CreateAutomationNode(null, title, lastPathName));
            }
            else
            {
                this.Automation = null;
            }
        }
    }
    protected void ButtonApply_Click(object sender, EventArgs e)
    {
        try
        {
            TreeNode node = TreeViewAutomation.SelectedNode;

            if (node != null)
            {
                string guid = DropDownListAutomation.SelectedValue;
                //string param = TextBoxParameter.Text;
                string title        = string.Empty;
                string lastPathName = string.Empty;

                IAutomation automation = AdminServer.TheInstance.AutomationManager.Create(guid);

                if ((int)automation.Parameter[0] != 0)
                {
                    object[] paraObj = new object[(int)automation.Parameter[0] + 1];

                    switch ((int)automation.Parameter[0])
                    {
                    case 0:
                        break;

                    case 1:
                        paraObj[0] = 1;
                        paraObj[1] = TextBoxParameter1.Text.Clone();
                        break;

                    case 2:
                        paraObj[0] = 2;
                        paraObj[1] = TextBoxParameter1.Text.Clone();
                        paraObj[2] = TextBoxParameter2.Text.Clone();
                        break;

                    case 3:
                        paraObj[0] = 3;
                        paraObj[1] = TextBoxParameter1.Text.Clone();
                        paraObj[2] = TextBoxParameter2.Text.Clone();

                        //如果该任务的类型为“发邮件”
                        if (automation.Guid == "{eca2ad62-0238-4996-82c0-852009463d94}")
                        {
                            string tempString = String.Empty;
                            foreach (ListItem item in MailContentTypeCheckBoxList.Items)
                            {
                                if (item.Selected == true)
                                {
                                    tempString += item.Value;
                                    tempString += " ";
                                }
                            }

                            paraObj[3] = tempString.Trim();
                        }
                        else
                        {
                            paraObj[3] = TextBoxParameter3.Text.Clone();
                        }

                        break;
                    }

                    automation.Parameter = paraObj;
                }

                //paraObj[paraIndex] = TextBoxParameter[paraIndex].Text.Clone();

                //automation.Parameter = paraObj;// param;

                if (_automation != null)
                {
                    string   pathString = node.ValuePath;
                    string[] pathArray  = pathString.Split('/');

                    IAutomation parentAutomation   = null;
                    IAutomation selectedAutomation = _automation;
                    foreach (string pathName in pathArray)
                    {
                        if (pathName.Length > 0)
                        {
                            lastPathName     = pathName;
                            parentAutomation = selectedAutomation;
                            switch (pathName)
                            {
                            case "0":
                                selectedAutomation = selectedAutomation.Failure;
                                break;

                            case "1":
                                selectedAutomation = selectedAutomation.Success;
                                break;

                            case "2":
                                selectedAutomation = selectedAutomation.Complete;
                                break;
                            }
                        }
                    }

                    if (parentAutomation != null)
                    {
                        switch (lastPathName)
                        {
                        case "0":
                            title = "failure: ";
                            parentAutomation.Failure = automation;
                            break;

                        case "1":
                            title = "success: ";
                            parentAutomation.Success = automation;
                            break;

                        case "2":
                            title = "complete: ";
                            parentAutomation.Complete = automation;
                            break;
                        }
                    }
                    else
                    {
                        Automation = automation;
                    }
                }
                else
                {
                    Automation = automation;
                }

                TreeNodeCollection nodes      = null;
                TreeNode           parentNode = node.Parent;
                if (parentNode != null)
                {
                    nodes = parentNode.ChildNodes;
                }
                else
                {
                    nodes = TreeViewAutomation.Nodes;
                }

                int index = nodes.IndexOf(node);
                if (index >= 0)
                {
                    nodes.Remove(node);
                    nodes.AddAt(index, CreateAutomationNode(automation, title, lastPathName));
                }

                if (_autoExpand)
                {
                    TreeViewAutomation.ExpandAll();
                }
            }

            LabelParameterInfo.Text    = string.Empty;
            LabelParameterInfo.Visible = false;
        }
        catch (BadParameterException ex)
        {
            LabelParameterInfo.Text    = ex.Message;
            LabelParameterInfo.Visible = true;
            //TextBoxParameter.Focus();
        }
    }