void AddNewSubrule(IRule r)
        {
            SaveCurrentRule();

            TreeNode s = RulesTreeView.SelectedNode;
            if (s == null)
                return;

            history_.Save();

            TreeNode n = new TreeNode(r.Name);
            n.Tag = r;

            s.Nodes.Add(n);
            RulesTreeView.SelectedNode = n;
            RulesTreeView.Select();

            RestoreSelectedRule();
            this.currentRule_.SelectName();
        }
 private static void setNodeColour(TreeNode node, IRule rule)
 {
     if (rule.enabled_)
         node.BackColor = Color.White;
     else
         node.BackColor = Color.Red;
 }
        private void AppendRule(IRule r)
        {
            TreeNode n = new TreeNode(r.Name);
            n.Tag = r;

            RulesTreeView.Nodes.Add(n);

            if (r.Children != null && r.Children.Count > 0) foreach (IRule rnew in r.Children) AddNewRule(rnew, n);
            else
            {
                RulesTreeView.SelectedNode = n;
                RulesTreeView.Select();
            }
        }
        private void AddNewRule(IRule r)
        {
            SaveCurrentRule();
            history_.Save();

            TreeNode n = new TreeNode(r.Name);
            n.Tag = r;

            TreeNode s = RulesTreeView.SelectedNode;
            if (s != null)
            {
                TreeNode p = s.Parent;
                if (p == null)
                    RulesTreeView.Nodes.Insert(RulesTreeView.Nodes.IndexOf(s) + 1, n);
                else
                    p.Nodes.Insert(p.Nodes.IndexOf(s) + 1, n);
            }
            else
            {
                RulesTreeView.Nodes.Add(n);
            }

            if (r.Children != null && r.Children.Count > 0) foreach (IRule rnew in r.Children) AddNewRule(rnew, n);
            else
            {
                RulesTreeView.SelectedNode = n;
                RulesTreeView.Select();
            }

            RestoreSelectedRule();
            this.currentRule_.SelectName();
        }
        private void RecurseNode(TreeNode n, ref IRule r)
        {
            if (n.Nodes.Count == 0) return;

            r.Children = new List<IRule>();
            foreach (TreeNode n1 in n.Nodes)
            {
                IRule r1 = (IRule)n1.Tag;
                if (n1.Nodes.Count > 0) RecurseNode(n1, ref r1);
                r.Children.Add(r1);
            }
        }
        void RestoreSelectedRule()
        {
            if (RulesTreeView.SelectedNode == null)
            {
                if (currentRule_ != null)
                {
                    currentRule_.DisposeControl();
                    currentRule_ = null;
                    NoRuleSelectedLabel.Show();
                }
            }
            else
            {
                NoRuleSelectedLabel.Hide();

                IRule oldrule = currentRule_;

                currentRule_ = (IRule)RulesTreeView.SelectedNode.Tag;

                this.SuspendLayout();

                Point pos = new Point();
                ruleControl_ = null;
                ruleControl_ = currentRule_.CreateControl(this, this.RuleControlSpace.Controls, pos);
                ruleControl_.Size = this.RuleControlSpace.Size;

                currentRule_.Name = RulesTreeView.SelectedNode.Text;

                currentRule_.Restore();

                if (oldrule != null)
                {
                    if (!currentRule_.Equals(oldrule))
                        oldrule.DisposeControl();
                }

                ruleControl_.Visible = true;

                this.ResumeLayout();
            }
            UpdateEnabledStates();
            setTreeViewColours();
        }
        //Base code from http://www.dotnetjohn.com/articles.aspx?articleid=173

        private string Serialize(IRule rule)
        {
            System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
            System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(IRule));
            XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
            xs.Serialize(xmlTextWriter, rule);
            memoryStream = (System.IO.MemoryStream)xmlTextWriter.BaseStream;

            return UTF8ByteArrayToString(memoryStream.ToArray());
        }