using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Windows.Forms;

namespace DiagramUI
{
    /// <summary>
    /// The tree node
    /// </summary>
    public class NamedNode : TreeNode
    {
        /// <summary>
        /// Linked component
        /// </summary>
        protected INamedComponent component;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="component">Linked component</param>
        public NamedNode(INamedComponent component, bool fullName)
        {
            this.component = component;
            if (component is NamedComponent)
            {
                NamedComponent comp = component as NamedComponent;
                comp.AddNode(this);
                Text = component.Name;
            }
            else
            {
                INamedComponent nc = component;
                IDesktop root = component.Root.Desktop;
                while (true)
                {
                    if (nc.Parent.Desktop == root)
                    {
                        break;
                    }
                    nc = nc.Parent;
                }
                if (fullName)
                {
                    Text = component.RootName;
                }
                else
                {
                    Text = component.GetName(nc.Desktop);
                }
            }
            ImageIndex = NamedComponent.GetImageNumber(component);
            SelectedImageIndex = ImageIndex;
        }

        /// <summary>
        /// Shows component form
        /// </summary>
        public void ShowForm()
        {
            if (component is NamedComponent)
            {
                NamedComponent comp = component as NamedComponent;
                comp.ShowForm();
                return;
            }
            ChildObjectLabel panel = ContainerObjectLabel.GetPanel(component) as ChildObjectLabel;
            panel.showForm();
        }


        /// <summary>
        /// Sets new name
        /// </summary>
        /// <param name="name">The name</param>
        public void SetName(string name)
        {

            if (component is NamedComponent)
            {
                NamedComponent comp = component as NamedComponent;
                comp.SetName(name);
            }
        }
    }
}
