/*
Copyright 2005 Danal Technology Inc.
All Rights Reserved

dsullivan@danal.com

This example is for tutorial purposes only. It may
not be used for any other purpose.
*/


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;

namespace DynamicSMOTree
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            HierCorrection = this.Size.Height - SMOHierarchy.Size.Height;
        }

        Server server = null;
        private void LoadInstance_Click(object sender, EventArgs e)
        {
            server = new Server(Instance.Text);
            SmoNode node = new SmoObjectNode(server, String.Empty );
            node.ToolTipText = server.Urn.Value;
            SMOHierarchy.Nodes.Add(node);
            node.AddChildren();
            
        }

        private void Expandme(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Node is SmoNode)
            {
                Cursor cursor = this.Cursor;
                this.SuspendLayout();
                this.Cursor = Cursors.WaitCursor;
                ((SmoNode)e.Node).AddChildren();
                this.Cursor = cursor;
                this.ResumeLayout();
            }
        }
        int HierCorrection = 0;
        private void FSizeChanged(object sender, EventArgs e)
        {

            SMOHierarchy.Size =
                new Size(this.Size.Width, this.Size.Height - HierCorrection);
        }

 
        private void contextMenuStrip1_MouseClick(object sender, MouseEventArgs e)
        {
            Clipboard.SetText(" ", TextDataFormat.Text);
            ContextMenuStrip ctx = sender as ContextMenuStrip;
            if (ctx != null)
            {
                TreeView tv = ctx.SourceControl as TreeView;
                if (tv != null)
                {
                    Point p = ctx.Location;
                    p = tv.PointToClient(p);
                    TreeNode tn = tv.GetNodeAt(p);
                    
                    SmoNode sn = tn as SmoNode;
                    if (sn != null)
                    {
                        Clipboard.SetText(sn.Urn.Value, TextDataFormat.Text);
                    }
                }
            };
        }

    }
}