public void Add(IComponent component, string name)
{
IDesigner designer = null;
//input checks
if (component == null)
{
throw new ArgumentException("Cannot add null component to container", "component");
}
if (!(component is Control))
{
throw new ArgumentException("This Container only accepts System.Web.UI.Control-derived components", "component");
}
if (component.Site != null && component.Site.Container != this)
{
component.Site.Container.Remove(component);
}
//Check the name and create one if necessary
INameCreationService nameService = host.GetService(typeof(INameCreationService)) as INameCreationService;
if (nameService == null)
{
throw new Exception("The container must have access to a INameCreationService implementation");
}
if (name == null || !nameService.IsValidName(name))
{
name = nameService.CreateName(this, component.GetType());
System.Diagnostics.Trace.WriteLine("Generated name for component: " + name);
}
//check we don't already have component with same name
if (GetComponent(name) != null)
{
throw new ArgumentException("There is already a component with this name in the container", "name");
}
//we're definately adding it now, so broadcast
OnComponentAdding(component);
//get a site and set ID property
//this way (not PropertyDescriptor.SetValue) won't fire change events
((Control)component).ID = name;
component.Site = new DesignSite(component, this);
//Get designer. If first component, designer must be an IRootDesigner
if (components.Count == 0)
{
host.SetRootComponent(component);
designer = new RootDesigner(component);
}
//FIXME: Give Mono some base designers to find! We should never encounter this!
//else
// designer = TypeDescriptor.CreateDesigner (component, typeof(System.ComponentModel.Design.IDesigner));
if (designer == null)
{
//component.Site = null;
//throw new Exception ("Designer could not be obtained for this component.");
}
else
{
//track and initialise it
designers.Add(component, designer);
designer.Initialize(component);
}
//add references to referenceManager, unless root component
WebFormReferenceManager refMan = host.GetService(typeof(WebFormReferenceManager)) as WebFormReferenceManager;
if ((components.Count != 1) && (refMan != null))
{
refMan.AddReference(component.GetType());
}
//Finally put in container
components.Add(component);
//and broadcast completion
OnComponentAdded(component);
}