public void Add(IComponent component, string name)
        {
            // we have to have a component
            if(component==null)
            {
                throw new ArgumentException("component");
            }

            // if we dont have a name, create one
            if(name==null || name.Trim().Length==0)
            {
                // we need the naming service
                INameCreationService nameCreationService = GetService(typeof(INameCreationService))as INameCreationService;
                if(nameCreationService==null)
                {
                    throw new Exception("Failed to get INameCreationService.");
                }
                name = nameCreationService.CreateName(this,component.GetType());
            }

            // if we own the component and the name has changed
            // we just rename the component
            if (component.Site != null && component.Site.Container == this &&
                name!=null && string.Compare(name,component.Site.Name,true)!=0)
            {
                // name validation and component changing/changed events
                // are fired in the Site.Name property so we don't have
                // to do it here...
                component.Site.Name=name;
                // bail out
                return;
            }
            // create a site for the component
            ISite site = new SiteImpl(component,name,this);
            // create component-site association
            component.Site=site;
            // the container-component association was established when
            // we created the site through site.host.
            // we need to fire adding/added events. create a component event args
            // for the component we are adding.

            ComponentEventArgs evtArgs = new ComponentEventArgs(component);

            // fire off adding event
            if(ComponentAdding!=null)
            {
                try
                {
                    ComponentAdding(this,evtArgs);
                }
                catch{}
            }

            // if this is the root component
            IDesigner designer = null;
            if(rootComponent==null)
            {
                // set the root component
                rootComponent=component;
                // create the root designer
                designer = (IRootDesigner)TypeDescriptor.CreateDesigner(component,typeof(IRootDesigner));
            }
            else
            {
                designer = TypeDescriptor.CreateDesigner(component,typeof(IDesigner));
            }
            if(designer!=null)
            {
                // add the designer to the list
                designers.Add(component,designer);
                // initialize the designer
                designer.Initialize(component);
            }
            // add to container component list
            sites.Add(site.Name,site);
            // now fire off added event
            if(ComponentAdded!=null)
            {
                try
                {
                    ComponentAdded(this,evtArgs);
                }
                catch{}
            }
        }
Exemple #2
0
        public void Add(IComponent component, string name)
        {
            // we have to have a component
            if (component == null)
            {
                throw new ArgumentException("component");
            }

            // if we dont have a name, create one
            if (name == null || name.Trim().Length == 0)
            {
                // we need the naming service
                INameCreationService nameCreationService = GetService(typeof(INameCreationService)) as INameCreationService;
                if (nameCreationService == null)
                {
                    throw new Exception("Failed to get INameCreationService.");
                }
                name = nameCreationService.CreateName(this, component.GetType());
            }

            // if we own the component and the name has changed
            // we just rename the component
            if (component.Site != null && component.Site.Container == this &&
                name != null && string.Compare(name, component.Site.Name, true) != 0)
            {
                // name validation and component changing/changed events
                // are fired in the Site.Name property so we don't have
                // to do it here...
                component.Site.Name = name;
                // bail out
                return;
            }
            // create a site for the component
            ISite site = new SiteImpl(component, name, this);

            // create component-site association
            component.Site = site;
            // the container-component association was established when
            // we created the site through site.host.
            // we need to fire adding/added events. create a component event args
            // for the component we are adding.

            ComponentEventArgs evtArgs = new ComponentEventArgs(component);

            // fire off adding event
            if (ComponentAdding != null)
            {
                try
                {
                    ComponentAdding(this, evtArgs);
                }
                catch {}
            }

            // if this is the root component
            IDesigner designer = null;

            if (rootComponent == null)
            {
                // set the root component
                rootComponent = component;
                // create the root designer
                designer = (IRootDesigner)TypeDescriptor.CreateDesigner(component, typeof(IRootDesigner));
            }
            else
            {
                designer = TypeDescriptor.CreateDesigner(component, typeof(IDesigner));
            }
            if (designer != null)
            {
                // add the designer to the list
                designers.Add(component, designer);
                // initialize the designer
                designer.Initialize(component);
            }
            // add to container component list
            sites.Add(site.Name, site);
            // now fire off added event
            if (ComponentAdded != null)
            {
                try
                {
                    ComponentAdded(this, evtArgs);
                }
                catch {}
            }
        }