private void DiscoveryTreeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                if (RootFolders.LocalMachine.Equals(e.Node.Tag))
                {
                    ServersTable.Rows.Clear();
                    ShowPanel(true);

                    if (e.Node.Nodes.Count == 1 && String.IsNullOrEmpty(e.Node.Nodes[0].Text))
                    {
                        e.Node.Nodes.Clear();

                        m_lds.BeginFindServers(
                            OnFindServersComplete,
                            new ExpandNodeData()
                        {
                            Parent = e.Node, Lds = m_lds
                        });
                    }
                    else
                    {
                        ShowApplicationDescriptions(e.Node.Nodes);
                    }

                    return;
                }

                if (RootFolders.LocalNetwork.Equals(e.Node.Tag))
                {
                    ServersTable.Rows.Clear();
                    ShowPanel(true);

                    if (e.Node.Nodes.Count == 1 && String.IsNullOrEmpty(e.Node.Nodes[0].Text))
                    {
                        e.Node.Nodes.Clear();

                        m_lds.BeginFindServersOnNetwork(
                            0,
                            1000,
                            OnFindServersOnNetworkComplete,
                            new ExpandNodeData()
                        {
                            Parent = e.Node, Lds = m_lds
                        });
                    }
                    else
                    {
                        ShowServerOnNetworks(e.Node.Nodes);
                    }

                    return;
                }

                if (RootFolders.GlobalDiscovery.Equals(e.Node.Tag))
                {
                    ServersTable.Rows.Clear();
                    ShowPanel(true);

                    if (e.Node.Nodes.Count == 1 && String.IsNullOrEmpty(e.Node.Nodes[0].Text))
                    {
                        e.Node.Nodes.Clear();

                        var servers = new ViewServersOnNetworkDialog(m_gds).ShowDialog(this, ref m_filters);

                        if (servers != null)
                        {
                            foreach (var server in servers)
                            {
                                TreeNode node = new TreeNode(String.Format("{0}", server.ServerName));
                                node.SelectedImageIndex = node.ImageIndex = ImageIndex.Server;
                                node.Tag = server;
                                node.Nodes.Add(new TreeNode());
                                e.Node.Nodes.Add(node);
                            }
                        }
                    }

                    ShowServerOnNetworks(e.Node.Nodes);

                    return;
                }

                if (RootFolders.CustomDiscovery.Equals(e.Node.Tag))
                {
                    ServersTable.Rows.Clear();
                    ShowPanel(true);
                    return;
                }

                if (e.Node.Tag is ApplicationDescription)
                {
                    EndpointsTable.Rows.Clear();
                    ShowPanel(false);

                    ApplicationDescription application = (ApplicationDescription)e.Node.Tag;

                    ApplicationNameTextBox.Text = (LocalizedText.IsNullOrEmpty(application.ApplicationName))?"---":application.ApplicationName.Text;
                    ApplicationTypeTextBox.Text = application.ApplicationType.ToString();
                    ApplicationUriTextBox.Text  = application.ApplicationUri;
                    ProductUriTextBox.Text      = application.ProductUri;

                    string discoveryUrl = SelectDiscoveryUrl(application);

                    if (discoveryUrl != null)
                    {
                        m_lds.BeginGetEndpoints(
                            discoveryUrl,
                            null,
                            OnGetEndpointsComplete,
                            new GetEndpointsData()
                        {
                            Parent = e.Node, Lds = m_lds
                        });
                    }
                }

                if (e.Node.Tag is ServerOnNetwork)
                {
                    EndpointsTable.Rows.Clear();
                    ShowPanel(false);

                    ServerOnNetwork server = (ServerOnNetwork)e.Node.Tag;

                    ApplicationNameTextBox.Text = server.ServerName;
                    ApplicationTypeTextBox.Text = "---";
                    ApplicationUriTextBox.Text  = "---";
                    ProductUriTextBox.Text      = "---";

                    try
                    {
                        Cursor = Cursors.WaitCursor;

                        m_lds.BeginGetEndpoints(
                            server.DiscoveryUrl,
                            null,
                            OnGetEndpointsComplete,
                            new GetEndpointsData()
                        {
                            Parent = e.Node, Lds = m_lds
                        });
                    }
                    finally
                    {
                        Cursor = Cursors.Default;
                    }
                }

                if (e.Node.Tag is ConfiguredEndpoint)
                {
                    EndpointsTable.Rows.Clear();
                    ShowPanel(false);

                    ConfiguredEndpoint server = (ConfiguredEndpoint)e.Node.Tag;

                    ApplicationNameTextBox.Text = "---";
                    ApplicationTypeTextBox.Text = "---";
                    ApplicationUriTextBox.Text  = "---";
                    ProductUriTextBox.Text      = "---";

                    m_lds.BeginGetEndpoints(
                        server.EndpointUrl.ToString(),
                        null,
                        OnGetEndpointsComplete,
                        new GetEndpointsData()
                    {
                        Parent = e.Node, Lds = m_lds
                    });
                }
            }
            catch (Exception ex)
            {
                Opc.Ua.Client.Controls.ExceptionDlg.Show(Text, ex);
            }
        }
        private void DiscoveryTreeView_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            if (e.Node.Nodes.Count != 1 || !String.IsNullOrEmpty(e.Node.Nodes[0].Text))
            {
                return;
            }

            e.Node.Nodes.Clear();

            if (RootFolders.LocalMachine.Equals(e.Node.Tag))
            {
                m_lds.BeginFindServers(
                    OnFindServersComplete,
                    new ExpandNodeData()
                {
                    Parent = e.Node, Lds = m_lds
                });

                return;
            }

            if (RootFolders.LocalNetwork.Equals(e.Node.Tag))
            {
                m_lds.BeginFindServersOnNetwork(
                    0,
                    100,
                    OnFindServersOnNetworkComplete,
                    new ExpandNodeData()
                {
                    Parent = e.Node, Lds = m_lds
                });

                return;
            }

            if (RootFolders.GlobalDiscovery.Equals(e.Node.Tag))
            {
                var servers = new ViewServersOnNetworkDialog(m_gds).ShowDialog(this, ref m_filters);

                if (servers != null)
                {
                    foreach (var server in servers)
                    {
                        TreeNode node = new TreeNode(String.Format("{0}", server.ServerName));
                        node.SelectedImageIndex = node.ImageIndex = ImageIndex.Server;
                        node.Tag = server;
                        node.Nodes.Add(new TreeNode());
                        e.Node.Nodes.Add(node);
                    }
                }

                return;
            }

            if (RootFolders.CustomDiscovery.Equals(e.Node.Tag))
            {
                return;
            }

            if (e.Node.Tag is Uri)
            {
                m_lds.BeginFindServers(
                    e.Node.Tag.ToString(),
                    null,
                    null,
                    null,
                    null,
                    OnFindServersComplete,
                    new ExpandNodeData()
                {
                    Parent = e.Node, Lds = m_lds
                });

                return;
            }
        }